2

I have web application and background queue that executes long lasting tasks. Both of them using truffle-privatekey-provider with same private key to sign/send transactions.

Now I ma experiencing issue with nonce mismatch - when background worker makes 5 tasks its nonce increases to 5 from zero. Meanwhile web application doesn't knows about it and trying to use old nonce (1) and failed.

I do not want to calculate nonce before each transaction (moreover - I am using truffle-contract to abstract myself from raw transactions).

Is there any ways to solve it?

Alex G.P.
  • 1,277
  • 11
  • 27
  • not sure what you are asking / or want to do but why dont you copy the instance {...web3} – Arash Kiani Aug 23 '18 at 15:34
  • @ArashKiani imagine one module, lets' say eth that contains provider and perform connection initialization. It exports connected web3 and contrac instances. When trying to use it from independent apps simultaneously you've got this problem. – Alex G.P. Aug 23 '18 at 15:39
  • interesting so you don't want to instantiate contract multiple times – Arash Kiani Aug 23 '18 at 15:43
  • use contract multiple times : const contract = new web3.eth.Contract(contractInterface,to,input) – Arash Kiani Aug 23 '18 at 15:44
  • I know how to use contract. Imagine you export contract from module and user it in 2 different processes. – Alex G.P. Aug 23 '18 at 15:45
  • i know you know, its actually a great idea haven't done that before... – Arash Kiani Aug 23 '18 at 15:48
  • try something like this : const executable = Object.assign(contract).methods[func_name].apply(null,parameters) – Arash Kiani Aug 23 '18 at 15:50
  • Your provider uses nonce-tracker from MetaMask provide-engine, this will cache the last used nonce, this works if every transaction is in the same process but it will fail if used from two separate processes. One option is to remove the cache so every time a nonce is needed it will hit your ethereum node, other option is to extend nonce-tracker to use a local database to store nonces and it will be shared by several processes. – Ismael Aug 24 '18 at 06:09

0 Answers0