1
//abi not included here, provider is alchemy

const wallet = new ethers.Wallet(PRIVATE_KEY,provider);
tokenB = new ethers.Contract(
USDTaddress,
usdtabi,
    wallet
);
 await tokenB.transfer(account2.address,ethers.BigNumber.from(100),{gasLimit: 250000, gasPrice: ethers.utils.parseUnits('100', 'gwei')});

    let balanceUSDT = await tokenB.balanceOf(account2.address);
console.log(`Current account2 USDT balance:${balanceUSDT.toString()}`);
    

This code throws no errors but the USDT balance shows 0. This is on a hardhat local net with the contract instantiated already. Other functions including totalsupply work well. Thanks

1 Answer 1

0

When you call the transfer function, the transaction is still pending to be mined by a miner, and hence until then, you will not see your balance increase. To fix this you can wait until the transaction is mined.

const tx = await tokenB.transfer(account2.address,ethers.BigNumber.from(100),{gasLimit: 250000, gasPrice: ethers.utils.parseUnits('100', 'gwei')});

await tx.wait(); // wait for tx to be mined

let balanceUSDT = await tokenB.balanceOf(account2.address);
5
  • Thanks for the answer, but I did have this already:``` const receipt = await provider.waitForTransaction(tx.hash,3,0); ``` Which just times out.
    – alex234
    Jul 30, 2021 at 7:55
  • Can you try not passing the third argument? provider.waitForTransaction(tx.hash,3). The third argument is milliseconds for timeout error. If you pass 0, it will immediately trigger error. Jul 30, 2021 at 21:39
  • Thanks, I tried that, but it still timed out regardless of the time, I even setup a mocharc file to change the timeout and that didn't work either. I'm currently trying to get the logs of the transaction, but if you have any other solutions, I'd be glad to try it out.
    – alex234
    Jul 31, 2021 at 11:11
  • Can you just try once with tx.wait(), also you mention mocharc file, are you writing the code in test cases? Oh now I see, mocha's default 2 seconds is not enough. Can you post the exact error if you get any (earlier I thought you get timeout from ethers.js) Jul 31, 2021 at 13:50
  • I solved the problem by restarting VScode. No kidding.
    – alex234
    Jul 31, 2021 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.