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

Equivalent to web3 BatchRequest? #656

Closed
jamesmorgan opened this issue Nov 20, 2019 · 12 comments
Closed

Equivalent to web3 BatchRequest? #656

jamesmorgan opened this issue Nov 20, 2019 · 12 comments
Labels
discussion Questions, feedback and general information.

Comments

@jamesmorgan
Copy link

jamesmorgan commented Nov 20, 2019

Hi guys, just wondering if there is a etherejs equivalent to BatchRequest in webjs?

My current thoughts I can manually do this by maintaining the correct nonce value between each submission.

@jamesmorgan jamesmorgan changed the title Equivalent to web3 BatchRequest\\ Equivalent to web3 BatchRequest? Nov 20, 2019
@ricmoo
Copy link
Member

ricmoo commented Nov 20, 2019

There is not. :)

You may already know this, but I will expand a bit here for others that happen across this issue in the future. :)

There is often a misunderstanding of what batching does in JSON-RPC (it is part of the JSON-RPC specification more so than anything to do with Ethereum). A batch request of A and B will always take longer than the requests A and B made at the same time, as batching is designed to ensure serial execution, therefore has a negative effect on performance in a system which only offers eventual consistency anyways.

Batching is not without its uses, but its use is limited and is specific only to certain backends and usually requires a more complex API that is only compatible with those backends. It can be used using the lower level provider.send on the JsonRpcProvider, but there is no convenience method offered beyond that.

It also does not address consistency, since even if two transactions are sent and accepted by the network in some order, it is not certain they will be mined in that order. Ensuring order with proper nonce management is the only way to guarantee consistency.

Feel free to convince me otherwise though, if you have a compelling reason to add support for it. :)

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Nov 20, 2019
@jamesmorgan
Copy link
Author

Agree, it's only my second time looking at batching many requests. Our use case is to batch send several transactions via a CSV loaded into a back office tool. Not a biggy, I can work around it manually i think.

Thanks for the mega quick and solid response as well 👍

@ricmoo
Copy link
Member

ricmoo commented Dec 15, 2019

I'm going to close this now (I think the question is resolved?), but please feel free to re-open if you have more questions. Or continue commenting as I monitor closed issues.

Thanks! :)

@ricmoo ricmoo closed this as completed Dec 15, 2019
@IstoraMandiri
Copy link

Just wanted to mention that unless I am mistaken there are probably some use cases for batch requests from an optimisation point of view, especially when making many requests at the same time.

The main benefit isn't necessarily in publishing transactions, but for reading data; consider that you want read 1000 entries from a contract. Yes, you could make 1000 separate http requests, but this could be made more efficient by batching:

  • Overall overhead and network traffic is reduced as each request has additional headers / metadata.
  • Most browsers will have an upper limit on the number of simultaneous requests that are allowed to be made (around 10?). If you need to read lots of calls for some reason, it would make sense to batch them to overcome this limitation for a better UX.

@aboutlo
Copy link

aboutlo commented May 10, 2020

UX wise aka read-only I agree it could be nice to have a batch feature.
A classic situation if you want to read the balances of all the token for a given address.

MakerDao came up with an interesting solution https://github.com/makerdao/multicall.js however it's based on a multicall smart contract to aggregate the calls.

@aboutlo
Copy link

aboutlo commented May 10, 2020

never mind I just found #788

@ricmoo
Copy link
Member

ricmoo commented May 10, 2020

There is a multicall-based Provider in the works. :)

@PfanP
Copy link

PfanP commented Feb 7, 2021

So is there any solution for batch call?

@ricmoo
Copy link
Member

ricmoo commented Apr 18, 2021

I've added ethers.providers.JsonRpcBatchProvider in 5.1.1. Try it out and let me know if it works for you.

pull bot pushed a commit to shapeshift/ethers.js that referenced this issue Jun 4, 2021
@yanikitat
Copy link

Hi @ricmoo. I misunderstood your words:

It also does not address consistency, since even if two transactions are sent and accepted by the network in some order, it is not certain they will be mined in that order

You are talking that we need to manage order of transactions through nonce. But as I know I'm sending transaction to provider already with nonce.
It means that if I send for example transaction A with nonce=1 and transaction B with nonce=2 (it doesn't matter in which order will I send these txs), network will execute at the first to with nonce=1, and only then tx with nonce=2, am I right?
I can understand why they can be mined in different order? I will glad to hear your explanation, thank you)

@ricmoo
Copy link
Member

ricmoo commented Jan 22, 2023

@yanikitat Yes, that is true if from the same account.

The issue is consistency, where tx1 is sender A sends B an NFT and tx2 is sender B sends that NFT to C. As long as tx1 happens before tx2, all is good. But if tx2 happens first (they are different from accounts, so their nonce is independent) then B is trying to send an NFT they do not get own, and the tx will fail. So, the order of the transactions matter And depending which is mined first, will result in very different outcomes.

Does that make sense?

@yanikitat
Copy link

Thanks a lot @ricmoo!! I didn't think about the different accounts. It make sense of course

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

6 participants