aboutlo / ether-swr

Ether-SWR is a React hook that fetches Ethereum data. It streamlines the chores to keep the internal state of the Decentralized App (DApp), batches the RPC calls to an Ethereum node and cache the responses
MIT License
139 stars 12 forks source link

Is sending multiple requests in one useEtherSWR call the same as batching calls? #17

Closed jviray closed 2 years ago

jviray commented 2 years ago

Similar to the example in the README I'm making multiple requests in one, but instead of using the same contract, I'm calling functions on different contracts (all with the same abi). Looks something like this.

const ABIs = [
 [address_1, abi],
 [address 2, abi],
 [address 3, abi]
]

const { data: totals, error: errors } = useEtherSWR(
  [
    [address_1, 'getValue'],
    [address_2, 'getValue'],
    [address_3, 'getValue'],
  ],
  { ABIs: new Map(ABIs) }
);

Everything returns fine, but when I check the testnet logs, it's still showing 3 separate contract calls being made. Is this the expected behavior?

Maybe i'm misunderstanding the difference between "batching" and/or "multicall", but I'm tying to reduce the load on our Infura node and I want to reduce the amount of contract calls.

aboutlo commented 2 years ago

hey @jviray it's correct right now multicall doesn't batch yet. It's syntax sugar but I would like to implement https://github.com/makerdao/multicall.js directly in the fetcher.

jviray commented 2 years ago

@aboutlo awesome, I see the TO DO comment in the fetcher file about using https://github.com/Destiner/ethcall.

useDapp has a useChainCalls hook that uses MakerDao's multicall and also caches each request. Just need a easy to use fetcher (with multicall + caching) like you have here though, so I'll be looking forward to it.

There's also https://github.com/indexed-finance/multicall which enables multi-calls without needing to deploy an aggregator contract

sambacha commented 2 years ago

@aboutlo awesome, I see the TO DO comment in the fetcher file about using https://github.com/Destiner/ethcall.

useDapp has a useChainCalls hook that uses MakerDao's multicall and also caches each request. Just need a easy to use fetcher (with multicall + caching) like you have here though, so I'll be looking forward to it.

There's also https://github.com/indexed-finance/multicall which enables multi-calls without needing to deploy an aggregator contract

You might be interested in the work 1inch has done with chunking requests for multicall, see https://github.com/1inch/multicall

Additionally you might find the Hughes batching system interesting for multicall usage (not the same as makerdaos) https://dl.acm.org/doi/abs/10.1145/3457337.3457839

You can create your own bespoke multicall by creating a contract interface and using that via eth_call. Arbitrium uses a similar technique I believe

aboutlo commented 2 years ago

hey @jviray and @sambacha have a look #18

aboutlo commented 2 years ago

fixed with #19

sambacha commented 2 years ago

Thanks 💯