transaction underpriced in BEP-20 Token transaction

Viewed 20917

I had do some transaction in Binance Smart Chain in Binance-Peg BUSD-T and it worked successfully. But after 5 transactions. I face to a problem that said Returned error: transaction underpriced ! This is my code:

const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

const contract = new web3.eth.Contract(abi, usdtContractAddr, {
  from: 'SENDER_ADDRESS', // default from address
  gasPrice: '200000000' // default gas price in wei, 20 gwei in this case
});

web3.eth.accounts.wallet.add('SENDER_PRIVATE_KEY');
const receipt = await contract.methods.transfer('TO_ADDRESS', '1000000000000000000').send({
    from: 'SENDER_ADDRESS',
    gas: 100000
});

I have increased my gas 10% and add a nonce more than the value which was given to me by calling web3.eth.getTransactionCount('ADDRESS'). But non of them works. I used to do a lot of transactions in Binance-Peg BUSD-T and so it is a big problem for me. Is there a way to solve this problem ???

3 Answers

The "transaction underpriced" error occurs, when you're trying to replace a transaction and the replacing gas price is too low.

web3.eth.getTransactionCount() only returns the amount of mined transactions. But you can have N (not just one) of transactions that are waiting to be mined with already higher nonce.

Example:

  • You have submitted 4 transactions - nonces 1, 2, 3, and 4.
  • Transactions 1 and 2 are successfully mined.
  • getTransactionCount() returns 2
  • When you're trying to submit another tx with nonce 3 or 4, it's trying to replace the already existing transactions.

Solution:

Use even higher gas price if you want to replace the existing transaction.

Or if you want to submit a new transaction (and not replace the previous), use higher nonce (sum of "successfully mined" + "waiting to be mined" + 1) that your address haven't used.

const contract = new web3.eth.Contract(abi, usdtContractAddr, {
  from: 'SENDER_ADDRESS', // default from address
  gasPrice: '200000000' // default gas price in wei, 20 gwei in this case
});

To be precise, the gasPrice is not 20 gwei, its 0.2 gwei. Adding 2 extra zeros to the gasPrice should remove the underpriced error, since 0.2 gwei is less than a minimum gasPrice on BSC blockchain.

const tx = await router.swapExactTokensForTokens(
  amountIn,
  amountOutMin,
  [tokenIn, tokenOut],
  addresses.recipient,
  Date.now() + 1000 * 60 * 10, //10 minutes
  // Math.floor(Date.now() / 1000) + 60 * 20,

  {
    gasPrice: ethers.utils.parseUnits('6','gwei').toString(),
    gasLimit: 177302
  }

);
Related