8

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 ???

Erfan Poursina
  • 119
  • 1
  • 4
  • 8

3 Answers3

8

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.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Alright. Thanks a lot.I got the point.But how can I get the full nonce ??? ("successfully mined" + "waiting to be mined") – Erfan Poursina Jun 15 '21 at 15:12
  • There's currently no `web3` (or underlying JSON-RPC) method to get amount of "waiting to be mined" transactions per address. You can keep a separate count of waiting transactions in your app (in a variable or a separate DB). Or if you don't have the historic data, you'll need to calculate it manually. – Petr Hejda Jun 15 '21 at 15:50
5
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.

-1
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
  }

);
东方不败
  • 115
  • 1
  • 4
  • 2
    Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See [How to Answer](https://stackoverflow.com/help/how-to-answer). – Sfili_81 Nov 18 '21 at 07:43