javascript Tronweb USDT 20转账失败-交易返回>

z31licg0  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(723)

我想用TRC 20通过tronWeb转移USDT。我得到了TXID和一切,但不知何故,我不能触发智能合约。下面是我的代码:

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);

这是事务的输出:

https://tronscan.org/#/transaction/e76d76d8898e7418cdb558573e90a3cc7b28e94da245df3990aa46f7d4fecc88

我应该尽量避免这种情况吗?先谢谢你了!

qeeaahzv

qeeaahzv1#

我认为你们的费用太低了,无法做成一笔可成交的交易。您可以尝试这个代码吹,并确保您有足够的trx在您的地址(超过20)

const TronWeb = require('tronweb');
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://api.trongrid.io");
const solidityNode = new HttpProvider("https://api.trongrid.io");
const eventServer = new HttpProvider("https://api.trongrid.io");

async function sendTrc20Transaction(fromAddress, toAddress, amount,
  privateKey) {

  const CONTRACT = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
  const tronWeb = new TronWeb(fullNode, solidityNode, eventServer, privateKey);
  const options = {
    feeLimit: 50_000_000,
    callValue: 0
  };
  const tx = await tronWeb.transactionBuilder.triggerSmartContract(
    CONTRACT, 'transfer(address,uint256)', options,
    [{
      type: 'address',
      value: toAddress
    }, {
      type: 'uint256',
      value: amount * 1000000
    }],
    tronWeb.address.toHex(fromAddress)
  );
  const signedTx = await tronWeb.trx.sign(tx.transaction);
  const broadcastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  const {
    message
  } = broadcastTx;
  if (message) {
    console.log("Error:", Buffer.from(message, 'hex').toString());
    return false
  }
  return true;
}

相关问题