I have a faucet contract with an onlyOwner function to transfer Ether to an account. I also have a public API which users can call with their public key: the API then users the owner's keypair to send a transaction to the faucet to fund that account.

Let's say I have the following situation:

  1. Users A, B and C call the API almost at the same time.
  2. The API creates 3 transactions with nonces 0, 1 and 2 and sends them to the blockchain.
  3. For some reason, transaction with nonce 1 fails.
  4. Transaction 0 is mined, transaction 2 remains in the pool.

What's the best way to manages nonces? I understand that it should be a mix of using web3's function getNonce and keeping a history in the API.

Also, what should I do in that situation? I understand that transaction 2 will remain there until a transaction with nonce 1 is send. One option is to wait for another user to ask for funds (and let the API use nonce 1), but that could take some time. Another option is for the API to send a dummy transaction to the contract with nonce 1 (which seems like a waste of gas).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.