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

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

Open
0xjjpa opened this issue Jul 18, 2021 · 8 comments
Open

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

0xjjpa opened this issue Jul 18, 2021 · 8 comments
Labels

Comments

@0xjjpa
Copy link

0xjjpa commented Jul 18, 2021

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.

@0xjjpa 0xjjpa changed the title Provide UI to allow dynamic change of RPC provider Provide useRPC hook to allow dynamic change of RPC provider Jul 18, 2021
@brunoalano
Copy link

+1

@tt-marek
Copy link
Contributor

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
Copy link

jhirn commented Nov 23, 2021

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:

#397

Linking #356 as well.

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

@tt-marek
Copy link
Contributor

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

@jhirn
Copy link

jhirn commented Nov 23, 2021

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
Copy link

jhirn commented Nov 24, 2021

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
Copy link
Author

0xjjpa commented Nov 30, 2021

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
Copy link
Contributor

@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

@dmaretskyi dmaretskyi added the enhancement New feature or request label Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants