Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does MyCrypto calculate the correct transaction nonce? #2221

Closed
tarekskr opened this issue Oct 18, 2018 · 9 comments
Closed

How does MyCrypto calculate the correct transaction nonce? #2221

tarekskr opened this issue Oct 18, 2018 · 9 comments

Comments

@tarekskr
Copy link

How does MyCrypto calculate the correct transaction nonce when multiple transactions are being issued?

It's well known that eth_getTransactionCount does not return the correct number of pending transactions, so I was wondering how you guys do it. Could be really beneficial for the community to learn from.

@tayvano
Copy link
Contributor

tayvano commented Oct 23, 2018

We don't actually. You can refresh the nonce, which pulls straight from the node you are connected to and may or may not take into account pending txs, depending on the node you are pulling from (e.g. geth vs parity)

We've discussed manually incrementing the nonce on the client side but it hasn't been implemented and I'm not sure if we ever will as (at least for now) less risky to have a user have a transaction dropped from the pool than accidentally send 2 of the same transaction unintentionally.

@tarekskr
Copy link
Author

Thanks @tayvano . So this means that a transaction can be dropped if I post it using MetaMask for instance, then switch to MyCrypto and post another transaction while the previous one is still pending?

@tayvano
Copy link
Contributor

tayvano commented Oct 24, 2018

In theory, yes, if you do so very quickly and from different nodes. If there is another transaction pending, you will receive an error message. Basically you would have to broadcast the second transaction before the first transaction enters the txpool of the node you are using. With the network being pretty stable these days (unlike cryptokitties days), you would have to be awfully quick about it.

@tarekskr
Copy link
Author

Actually the reason I am saying it will be dropped is because both transactions will have the same nonce. Consider the following:

a. My wallet has 30 transactions in total.
b. I post a new transaction using MetaMask. MetaMask will assign a nonce of 30 to it.
c. While my transaction is still pending, I go to MyCrypto and post another one. Since MyCrypto uses getTransactionCount (which doesn't include pending transactions), MyCrypto will also set the nonce at 30.

If the new MyCrypto transaction has a higher gas price, it should replace the MetaMask one, no?

@tayvano
Copy link
Contributor

tayvano commented Oct 24, 2018

Sorry, yes you are correct. It wouldn't show an error and you could inadvertently send a second tx to the queue that replaces the first transaction if the gas priced used for the second transaction is gas price of first transaction + 10 gwei

@tayvano
Copy link
Contributor

tayvano commented Oct 24, 2018

Do you think it would be better a different way? Any proposals?

@tarekskr
Copy link
Author

I actually have this exact problem in an app of mine.

Ideally getTransactionCount should also return the pending transaction count if you pass the pending block parameter, but it doesn't. It's a bug that has been there for years and I am really not sure why it's not being fixed.

I was considering searching the TX pool myself for pending transactions involving my wallet's address, but I couldn't find enough documentation on that anywhere. I guessed you guys might doing that, which is why I asked here 🙂 I'll definitely update you here if I am able to figure this out.

ps: I also asked MetaMask how they did it, but it turns out that they only keep track of the transactions you post using MetaMask itself.

@tayvano
Copy link
Contributor

tayvano commented Oct 24, 2018

So back.when we were still MEW we had huge issues losing transactions, especially during cryptokitties times and threw together this not perfect but effective script.

It no longer applies really since there are so many node providers via MyCrypto but a similar thing could be used for your app if you are only using one node. You could use this to create a simple db that You could easily and scalably read from to get the correct nonce

https://github.com/MyEtherWallet/api-tx-broadcast

@tarekskr
Copy link
Author

Awesome, will check it out! Thank you so much 🙏🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants