You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TronWeb执行TRC20 USDT转账失败(交易回滚)求助

TRC20 USDT转账有交易ID但未触发智能合约的解决方法

尝试通过TronWeb进行TRC20标准USDT转账,已获取交易ID,但始终无法触发智能合约。实现代码如下:

const TronWeb = require('tronweb');

// Create a TRON instance
const tronWeb = new TronWeb({
  fullHost: 'https://api.trongrid.io', // Use the TRON full node URL
});

// Function to send USDT TRC20 tokens
async function sendUsdt(addressTo, amount, privateKey) {
  try {
    // Convert the amount to SUN
    const amountSun = tronWeb.toSun(amount.toString());

    // Get the contract address of USDT TRC20 token
    const usdtContractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'; // USDT TRC20 contract address

    // Get the address associated with the private key
    const ownerAddress = tronWeb.address.fromPrivateKey(privateKey);

    // Create the transfer transaction data
    const transferData = await tronWeb.transactionBuilder.triggerSmartContract(
      usdtContractAddress,
      'transfer',
      {
        _to: addressTo,
        _value: amountSun
      },
      [],
      ownerAddress // Set the owner_address parameter
    );
    transferData.transaction.fee = fee;

    // Sign the transaction
    const signedTransaction = await tronWeb.trx.sign(
      transferData.transaction,
      privateKey
    );

    // Broadcast the transaction
    const receipt = await tronWeb.trx.sendRawTransaction(
      signedTransaction
    );

    console.log('Transaction receipt:', receipt);
  } catch (error) {
    console.error('Failed to send USDT:', error);
  }
}

// Usage: Call the sendUsdt function with the recipient's address, amount, and private key
const recipientAddress = 'TRECIPENT';
const amount = 10; // Amount of USDT to send
const privateKey = 'MY_PRIV_KEY'; // Set your private key here
const fee = 100000; // Fee value in SUN

sendUsdt(recipientAddress, amount, privateKey,fee);

交易ID:e76d76d8898e7418cdb558573e90a3cc7b28e94da245df3990aa46f7d4fecc88


问题分析与修复方案

1. 函数参数不匹配

调用sendUsdt时传入了4个参数,但函数定义仅接收3个,导致fee变量在函数内部未定义。TronWeb会自动计算合规手续费,无需手动设置,直接删除transferData.transaction.fee = fee该行即可。

2. TRC20转账参数格式错误

triggerSmartContract调用transfer方法时,参数必须按照TRON ABI规范以数组形式传递,而非对象。USDT的transfer方法签名为transfer(address,uint256),且USDT是6位小数,不能用TRX的toSun转换,需自行计算最小单位(amount * 10**6)。

3. 地址格式校验

确保recipientAddress是正确的Base58格式TRON地址,避免拼写错误。

修复后的完整代码

const TronWeb = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.trongrid.io',
});

async function sendUsdt(addressTo, amount, privateKey) {
  try {
    // USDT最小单位转换(6位小数)
    const amountUsdtUnit = Math.round(amount * 10**6).toString();
    const usdtContractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
    const ownerAddress = tronWeb.address.fromPrivateKey(privateKey);

    // 正确构造智能合约调用参数
    const transferData = await tronWeb.transactionBuilder.triggerSmartContract(
      usdtContractAddress,
      'transfer',
      {}, // 空配置项
      [
        { type: 'address', value: addressTo },
        { type: 'uint256', value: amountUsdtUnit }
      ],
      ownerAddress
    );

    // 签名并广播交易
    const signedTransaction = await tronWeb.trx.sign(transferData.transaction, privateKey);
    const receipt = await tronWeb.trx.sendRawTransaction(signedTransaction);

    console.log('Transaction receipt:', receipt);
  } catch (error) {
    console.error('Failed to send USDT:', error);
  }
}

// 调用示例(替换为实际参数)
const recipientAddress = 'TRECIPENT';
const amount = 10;
const privateKey = 'MY_PRIV_KEY';

sendUsdt(recipientAddress, amount, privateKey);

额外检查项

  • 确认发送地址有足够TRX支付手续费(至少0.01 TRX)
  • 确认发送地址USDT余额≥转账金额
  • 通过交易ID查看详情时,contractResult为["0x0"]表示合约执行成功

内容的提问来源于stack exchange,提问作者erenA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:05:38