web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.3k stars 4.95k forks source link

Multiples transactions with sendSignedTransaction from Node #1279

Closed yograterol closed 5 years ago

yograterol commented 6 years ago

Hi,

I'm doing an Express Server with async functions to call sendSignedTransaction.

The first call works well but the second concurrent call retrieves an error with {} content.

For example:

Client A sends a request to the backend - The backend starts a transaction. Client B sends a request to the backend with less of 5 sec of difference - Web3 returns {} while the last call doesn't finish.

The backend is using web3 1.0.0-beta.27

There is my code:

https://github.com/EthereumCommonwealth/Callisto-Faucet/blob/master/backend/index.js#L62

DalderupMaurice commented 6 years ago

Use this:

const nonce = await web3.eth.getTransactionCount(fromAddress, "pending");

This includes the pending transactions, besides only the mined transactions for the nonce.

However, I am having a similar issue. Per-user registration I need to do 2 transactions. Usually, these 2 go well. Until 2 or more people register at the same time. Then I get "Replacement error underprice" or "nonce too low", even when using the pending addition.

-- EDIT -- I've fixed the above issue of mine ^ using batch requests: https://github.com/DalderupMaurice/web3.js-snippets/blob/89034700429e08488486f59e485f8fb7f00951cb/server/server/helpers/web3-v0.x/transactionHelper.js#L21

10d9e commented 6 years ago

Initializing a variable with getTransactionCount() and incrementing whenever I send a raw transaction seems to resolve this issue for me.

// global
var seedTxnCount = await web3.eth.getTransactionCount(account.address)
...
// within function to submit txn
 var rawTx = {
      nonce: seedTxnCount++,
      gasPrice: 1000000000,
      gasLimit: estimatedGas,
      to: 0xblah,
      value: 0,
      data: encodedABI
}
nivida commented 5 years ago

Does this error still occur in the latest version of Web3?

nivida commented 5 years ago

Closed due to the inactivity of the creator of this issue.