TrueFiEng / useDApp

Framework for rapid Dapp development. Simple. Robust. Extendable. Testable
https://usedapp.io
MIT License
1.6k stars 373 forks source link

Provide `useRPC` hook to allow dynamic change of RPC provider #282

Open 0xjjpa opened 3 years ago

0xjjpa commented 3 years ago

Bug or feature?

Feature

What issue is this feature related to?

Multiple DApps come embedded with their own RPC providers like Infura, Quiknode, or Alchemy for them to work. This is mostly to avoid the UX experience of having to connect to Metamask before they can show any information. However, as a result, the DApp developer has to foot the bill on the provider used, share its credentials in the UI, and make the decision on the provider used by the final user, sadly affecting the user's privacy (i.e. most providers do IP tracking). Yes, there are public providers like LinkPool or other community-oriented projects like RYO Cloud, but usually, the user is not given any option to change these.

What's the feature you would like?

A hook a la useRPC that would allow to configure the RPC endpoint used for the DApp and reload the state of the application upon update. As I'm unsure how these would affect other providers, I'm bringing this feature here rather than just doing it solo.

What alternatives you have considered?

This is already somehow possible with the custom hooks feature, but it would be nice to have it OOB as part of useDApp

Additional context

The current practice for frontends is to embed an RPC endpoint, whitelist the contract used, and hope no one will exhaust it via some eth_getBalance calls. This could be a good step in a better direction.

brunoalano commented 3 years ago

+1

tt-marek commented 3 years ago

If I understand you would like to dynamically change rpc provider?

You can do that via useUpdateConfig:

const updateConfig = useUpdateConfig()
updateConfig({readOnlyUrls: {[ChainId.Mainnet]: '...'})
jhirn commented 3 years ago

If I understand you would like to dynamically change rpc provider?

You can do that via useUpdateConfig:

const updateConfig = useUpdateConfig()
updateConfig({readOnlyUrls: {[ChainId.Mainnet]: '...'})

Hi @tt-marek. Is there a way to substitute the JsonRPCProvider with either Infura or Static provider from ethers via configuraiton? My goal is to reduce the number of eth_ChainId calls as described in another issue I opened:

https://github.com/EthWorks/useDApp/issues/397

Linking https://github.com/EthWorks/useDApp/issues/356 as well.

Here's an issue on ethers.js describing why Static or Infura would avoid the unnecessary ChainID calls. https://github.com/ethers-io/ethers.js/issues/901

tt-marek commented 3 years ago

A yeah, that we would need to introduce as a new item in the config....

jhirn commented 3 years ago

A yeah, that we would need to introduce as a new item in the config....

Awesome. Thank you! @tt-marek

Are you able to suggest how I might change this globally prior to it being a config option? I've tried to trace where the provider is set but have not been able to crack that nut.

jhirn commented 3 years ago

So I was looking for web3-react to be using one of the ethers.js providers but they're not.

From what I can gather, it's most likely FrameConnector or InjectedConnector that's doing the offending eth_chainId calls . Going to test applying a patch similar to what's recommended in the ethers.js issue above and hopefully reduce the overhead.

Edit: After digging around for a few hours, I believe the problem might be legitimate calls for eth_ChainId to infura when using the read only connector. It would be ideal to not query Infura for ChainId but instead read it from the currently set readOnlyChainId. Still just a hunch, but not giving up on this.

0xjjpa commented 3 years ago

If I understand you would like to dynamically change rpc provider?

You can do that via useUpdateConfig:

const updateConfig = useUpdateConfig()
updateConfig({readOnlyUrls: {[ChainId.Mainnet]: '...'})

Gotcha, indeed, that's what I was looking for and I missed in the documentation. In my particular case, I had readOnlyUrls set but not readOnlyChainId so I had to create a custom hook call to overload this behavior.

The new configuration proposal in #409 seems to make this way more obvious, so we can take this conversation there and close this particular issue.

FWIW I'm happy this issue highlighted @jhirn 's issue as in other project we had to replace the JsonRpcProvider with a StaticJsonRpcProvider as it was calling an insane amount of times eth_chainId. Might be worth providing this configuration option and describe in the documentation, as it's not obvious (we even thought it was a tx parameter issue, and wasted quite some time debugging this).

dmaretskyi commented 2 years ago

@0xjjpa with the latest version you can pass a provider instance (compatible with BaseProvider from ethers) to the readOnlyUrls instead of the URL. This should let you use custom provider types, and configure the refresh interval on them