Web3js

如何使用 web3.js 進行 USDT 交易

  • September 26, 2021

我正在嘗試發送 ERC20 USDT 繫繩令牌。但我遇到了一些錯誤。誰能告訴我交易USDT的程式碼?謝謝。

我很高興能在幾天前解決這個問題時為您提供幫助。

const sendERC20Transaction = async (receiver, amount) => {
var Tx     = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io/v3/your-project-id')
web3.eth.accounts.wallet.add('privateKey of fromwallet');
var contractAbi =[];
var tokenAddress = ''//Tether token(USDT)
var fromAddress = '0x3df...'
var tokenInst = new web3.eth.Contract(contractAbi,tokenAddress);
tokenInst.methods.transfer(receiver, amounts).send({from: fromAddress, gas: 100000},function (error, result){ //get callback from function which is your transaction key
   if(!error){
       console.log(result);
       handleSuccessTrue();
   } else{
       console.log(error);
       web3.eth.getBalance(fromAddress, (err,bal) => { alert('Your account has ' + web3.utils.fromWei(bal, 'ether') + ', Insufficient funds for gas * price + value on your wallet')});
       handleSuccessFalse();
   }
});
//Finally, you can check if usdt tranaction success through this code.
tokenInst.methods.balanceOf(receiver).call().then(console.log)
.catch(console.error);

如果此程式碼對您有所幫助,我們將不勝感激。

謝謝。

引用自:https://ethereum.stackexchange.com/questions/110589