ton jetton 发送字符串的方式

func (t TonApiServiceImpl) TransferToken(ctx context.Context, tokenContractAddr string, toAddr string, amount big.Int) (string, error) {
	recipientAddr := tongo.MustParseAddress(toAddr)

	w, err := wallet.DefaultWalletFromSeed(t.cfg.LotteryOwnerSeed, t.lclient)
	if err != nil {
		t.logger.Error(fmt.Sprintf("Unable to create wallet: err:%v LotteryOwnerSeed:%s", err, t.cfg.LotteryOwnerSeed))
		return "", err
	}
	master := tongo.MustParseAccountID(tokenContractAddr)
	j := jetton.New(master, t.lclient)
	b, err := j.GetBalance(ctx, w.GetAddress())
	if err != nil || b == nil {
		t.logger.Error(fmt.Sprintf("Unable to get jetton wallet balance: err:%v LotteryOwnerSeed:%s", err, t.cfg.LotteryOwnerSeed))
		return "", err
	}

	if amount.Cmp(b) == 1 {
		return "", fmt.Errorf("%v jettons needed, but only %v on balance", amount, b)
	}
	owner := w.GetAddress()

	ForwardPayloadBoc := boc.NewCell()
	err = tlb.Marshal(ForwardPayloadBoc, wallet.TextComment("ForwardPayload Round:1"))
	if err != nil {
		return "", err
	}

	jettonTransfer := jetton.TransferMessage{
		Jetton:              j,
		Sender:              w.GetAddress(),
		JettonAmount:        &amount,
		Destination:         recipientAddr.ID,
		ResponseDestination: &owner,
		AttachedTon:         50_000_000,
		ForwardTonAmount:    1,
		ForwardPayload:      ForwardPayloadBoc,
		CustomPayload:       CustomPayloadBoc,
	}
	txId, err := w.SendV2(ctx, 0, jettonTransfer)
	if err != nil {
		t.logger.Error(fmt.Sprintf("Unable to send transfer message: %v", err))
	}
	return txId.Hex(), nil
}

这样就可以构造一个字符串了:

	ForwardPayloadBoc := boc.NewCell()
	err = tlb.Marshal(ForwardPayloadBoc, wallet.TextComment("ForwardPayload Round:1"))
	if err != nil {
		return "", err
	}

然后tonviewer就可以看到payload的显示了:

posted @ 2024-11-07 16:37  若-飞  阅读(7)  评论(0编辑  收藏  举报