ton jetton 傳送字串的方式

若-飞發表於2024-11-07
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的顯示了:

ton jetton 傳送字串的方式

相關文章