Skip to content

Optimize functions like Viem ( Multicall, Provider, ENS, Events ) #5008

Open
@cpuchainorg

Description

@cpuchainorg

Describe the Feature

Hello,

I have created ethers-opt that could bring some optimization features that Viem offers.

For example)

1. Viem aggregates eth_call requests to multicall3 by default https://viem.sh/docs/clients/custom#batchmulticall-optional and the feature is also implemented with ext-provider-multicall for ethers v6 however it is not available by default.

I have made Provider class inside ethers-opt library to make it default https://github.com/cpuchain/ethers-opt/blob/main/src/provider.ts#L304. ( So that any contract reads batched by Promise.all will be automatically included inside a single or two multicall requests which also makes possible to fetch 1K ERC20 balances in a single call for example. )

2. Viem uses Universal Resolver which should be used by default cause it not only allows to do recursive calls for resolving ENS records ( like finding resolver for a specific name -> looking up records would be done in a single call within universal resolver which makes possible to lookup multiple records or multiple names within a single eth_call, think that what would be possible when combined with the multicaller )

3. Static network should be default and it should not consume additional CUs by calling eth_chainId for every request.

I have solved this by making it a cached promise on https://github.com/cpuchain/ethers-opt/blob/main/src/provider.ts#L144

4. maxFeePerGas value shouldn't be a 2x value of block.baseFee and the multiplier value should be always configurable

I have overridden provider.getFeeData() to expose the base fee of block as is ( https://github.com/cpuchain/ethers-opt/blob/main/src/provider.ts#L209 ) and configured multiplier ( https://github.com/cpuchain/ethers-opt/blob/main/src/signer.ts#L92C72-L92C85 ) because fee calculations shouldn't be multiplied otherwise it would break or over-estimate when it comes to build something like Paymasters of how many fees should be paid, etc.

5. eth_getLogs params should be much easier for use and should be allowed to batch those in chunks

Because most RPC providers would limit block ranges chunking requests is inevitable and ethers.js should export params / necessary functions for batching and make it easier for use.

For example I have copy-pasted all those necessary functions from ethers.js because they are not exported https://github.com/cpuchain/ethers-opt/blob/main/src/ethers.ts#L61 and should be exported and allowed to be overritten.

Also chunks / batching should be possible with further optimized functions for an easier indexing like how those rust indexers offers right now.

6. Add functions to decode / type internal transactions

There are only like 2 ~ 3 common methods to fetch internal calls and I have made the temporary functions / type definitions. https://github.com/cpuchain/ethers-opt/blob/main/src/traceBlock.ts However I believe that ethers could support those by default because you only need to type those from either Geth or Erigon to do it.

7. Discovering multiple EIP-6963 Browser Providers

Currently only a single random browser injected wallets would be detected but what if multiple wallets are installed?

https://github.com/cpuchain/ethers-opt/blob/main/src/browserProvider.ts#L131

So that is why I have it here

8. eth_getBlockReceipts

https://github.com/cpuchain/ethers-opt/blob/main/src/blockReceipts.ts

9. Some simple reorg solution

Ether noobs wouldn't be able to code reorg handler easily and I have made some simple function to do it https://github.com/cpuchain/ethers-opt/blob/main/src/blockHashes.ts,

Maybe there should be some function provided by ethers it would be great

10. Support Permit / Permit2

Instead of expecting people to understand how to handle signatures using Permit / Permit2 could be done at the simplest form with the following https://github.com/cpuchain/ethers-opt/blob/main/src/permit.ts#L7

11. Static ENS resolvers / Expecting wildcard resolvers

Instead of sending 3~5 requests to fetch redundant data why we can not hardcode those if 99% of cases are covered and do only fallbacks for custom resolvers

https://github.com/cpuchain/ethers-opt/blob/main/src/ens/eth.ts#L55

With some care those functions and features can be easily implemented, however if not it would simply drain CUs and RPC credits and make DApps slower to use since nodes are slow and waiting those requests for a simple task would be even more slower.

@ricmoo Maybe it would be great when I no longer need to maintain additional package with those simple issues being optimized and fixed.

Code Example

https://github.com/cpuchain/ethers-opt

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or improvement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions