如何使用Ton Connect UI发送Notcoin(Jetton)交易?
发送Notcoin(Jetton)交易的修改方案
Notcoin是TON区块链上的Jetton代币,不能直接像转账TON那样直接发送到接收地址,必须通过Notcoin的Jetton合约对应的用户Jetton钱包来发起转账操作。以下是基于你现有代码的修改方案:
核心修改思路
Jetton转账需要向你自己的Notcoin Jetton钱包地址发送一条包含转账指令的消息,而不是直接给接收方发TON。需要借助TonWeb的Jetton工具类来生成正确的交易payload。
修改后的完整代码
import TonWeb from 'tonweb'; // 确保已引入TonWeb,浏览器端可通过CDN引入 let tonConnectUI; // TonConnectUI实例 let tonBalance; // 用户TON余额 let notBalance; // 用户Notcoin余额(注意:需转换为最小单位,Notcoin的decimals是9,即1 NOT = 1e9 最小单位) async function send_notcoin_transaction() { const tonweb = new TonWeb(); const NOTCOIN_JETTON_CONTRACT_ADDRESS = "你的Notcoin合约地址"; // 替换为实际地址 const RECIPIENT_TON_ADDRESS = "接收方的TON地址"; // 替换为实际接收地址 const userTonAddress = tonConnectUI.wallet.account.address; // 当前连接用户的TON地址 // 1. 获取用户对应的Notcoin Jetton钱包地址 const jettonContract = new tonweb.token.jetton.JettonContract(tonweb.provider, { address: NOTCOIN_JETTON_CONTRACT_ADDRESS }); const userJettonWalletAddress = await jettonContract.getJettonWalletAddress(userTonAddress); // 2. 构建Notcoin转账的payload const jettonWallet = new tonweb.token.jetton.JettonWallet(tonweb.provider, { address: userJettonWalletAddress }); const transferPayload = await jettonWallet.createTransferBody({ queryId: Date.now(), // 随机queryId,防止重复交易 jettonAmount: new TonWeb.utils.BN(notBalance), // 要转账的Notcoin数量(最小单位) toAddress: RECIPIENT_TON_ADDRESS, responseAddress: userTonAddress, // 转账结果通知地址,填自己的TON地址即可 forwardAmount: "0", // 转发给接收方的TON数量(一般填0) forwardPayload: "" // 可选附加数据,不需要则留空 }); // 3. 构建TonConnect需要的交易对象 const transaction = { validUntil: Math.floor(Date.now() / 1000) + 60, // 交易有效期60秒 messages: [ { address: userJettonWalletAddress.toString(true, true, true), // 转换为标准TON地址格式 amount: "1000000", // 支付Jetton转账的手续费,约0.001 TON(需确保用户TON余额足够) payload: transferPayload.toString('base64') // 将payload转为base64格式 } ] }; // 4. 发送交易 const result = await tonConnectUI.sendTransaction(transaction); return result; }
关键注意事项
- 单位转换:Notcoin的小数位数是9,如果你获取的
notBalance是显示的"NOT"数量(比如1.5 NOT),需要先转换为最小单位:notBalance = Math.floor(1.5 * 1e9)。 - 手续费:
amount字段填写的是支付Jetton转账的TON手续费,至少需要0.001 TON(即1e6 nanoton),要确保用户的TON余额足够支付这笔手续费+预留的TON余额。 - 地址格式:所有地址必须是TON标准格式(可通过
toString(true, true, true)转换),避免因格式错误导致交易失败。
内容的提问来源于stack exchange,提问作者Farhad Rad
相关产品推荐
相关产品推荐

