我想用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
我应该尽量避免这种情况吗?先谢谢你了!
1条答案
按热度按时间qeeaahzv1#
我认为你们的费用太低了,无法做成一笔可成交的交易。您可以尝试这个代码吹,并确保您有足够的trx在您的地址(超过20)