TrueFiEng / useDApp

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

useDapp is calling provider urls automatically and spiking up the request limit #944

Closed ashrhmn closed 2 years ago

ashrhmn commented 2 years ago

Doing Multiple Request automatically to the readonlyUrls passed in the config

To Reproduce

const config: DAppProviderProps["config"] = { readOnlyUrls: RPC_URLS }; Here RPC_URLS is of type { [chainId: number]: string } with all the RPC URLs.

Software versions

Additional context

I am being rate limited on my infura.io account for doing more than 100k requests a day. I checked the dashboard about what were the requests and found these.

image

As I am not doing these requests manually, is the useDapp module sending requests at this rate automatically?

image

As you can see, networks other than rinkeby was also used to request to the provider but I was doing the dev testing using only rinkeby. So, why are these requests spiking up automatically?

rzadp commented 2 years ago

Hey @ashrhmn.

useDApp is automatically quering for block number, and quering for new state for every new block, in order to keep the state up-to-date.

You can configure this behaviour using refresh in your useDApp config, to make the requests less frequently, for example every 5 blocks.

However, the amount of calls to eth_chainId is wrong - in version 1.0.8, we made some changes to address this, but apparently they still exist. We should not continiously query for the chain Id that is known.

There is something weird, because on an example app, I reproduced the reporting in Infura with a lot of eth_chainid calls, BUT - I do not see them in browser devtools network inspector, just at the beginning which is expected.

rzadp commented 2 years ago

I took another whack at it yesterday and today.

So it does not seem like I have a large number of eth_chainid calls - just 1-3 at the beginning, and none later (which is the expected behaviour).

Screenshot 2022-08-26 at 10 38 02

So @ashrhmn there must be something wrong with your setup, that it racks up so many eth_chainid calls. Is your list of readOnlyUrls a list of strings? Internally it will create a StaticJsonRpcProvider which knows not to query for chainId - this is since useDapp 1.0.8 I think.


As for other networks - do you not have any hooks using the other chains? It might be just requests to keep the block number up-to-date.

ashrhmn commented 2 years ago

I am not sure what I am doing wrong here. This is just all for the config.

function MyApp({ Component, pageProps }: AppProps) {
  const config: DAppProviderProps["config"] = {
    readOnlyUrls: RPC_URLS,
  };
  return (
    <DAppProvider config={config}>
      <Component {...pageProps} />
      <Toaster position="top-right" />
    </DAppProvider>
  );
}
export const RPC_URLS: { [chainId: number]: string } = {
  1: `https://mainnet.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}` as string,
  3: `https://ropsten.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}` as string,
  4: `https://rinkeby.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}` as string,
  5: `https://goerli.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}` as string,
  42: `https://kovan.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}` as string,
  56: `https://bsc-dataseed.binance.org/`,
  43114: `https://api.avax.network/ext/bc/C/rpc`,
  97: `https://data-seed-prebsc-1-s1.binance.org:8545/`,
  80001: `https://matic-mumbai.chainstacklabs.com`,
  4002: `https://rpc.testnet.fantom.network`,
  43113: `https://api.avax-test.network/ext/bc/C/rpc`,
  137: `https://polygon-rpc.com`,
};
rzadp commented 2 years ago

@ashrhmn Ok looks good

I am being rate limited on my infura.io account for doing more than 100k requests a day. I checked the dashboard about what were the requests and found these.

So what kind of usage do you have? Do you have the app deployed and used? Also I see some (faling) requests for net_version - That I don't think comes from useDApp.

ashrhmn commented 2 years ago

I previously had the useDapp hooks set up, but after the issue, I switched to this approach for fetching. Still, the problem is not fixed. Is there anything wrong with getDefaultProvider?


  useEffect(() => {
    try {
      (async () => {
        if (!projectAddress || !projectChainId || !RPC_URLS[projectChainId])
          return;
        const contract =
          collectionType === "721"
            ? Collection721__factory.connect(
                projectAddress,
                getDefaultProvider(RPC_URLS[projectChainId])
              )
            : Collection1155__factory.connect(
                projectAddress,
                getDefaultProvider(RPC_URLS[projectChainId])
              );
        setFeeAddressBgProc((v) => v + 1);
        setMaxMintInTotalPerWalletBgProc((v) => v + 1);
        setBaseURIBgProc((v) => v + 1);
        const [
          feeToAddress,
          baseURI,
          maxMintInTotalPerWallet,
          revealTime,
        ] = await Promise.all([
          contract.feeDestination().catch((e) => {
            console.log("Error getting feeDestination  : ", e);
            return "";
          }),
          contract.baseURI().catch((e) => {
            console.log("Error getting baseURI  : ", e);
            return "";
          }),
          contract.maxMintInTotalPerWallet().catch((e) => {
            console.log("Error getting maxMintInTotalPerWallet : ", e);
            return 0;
          }),
          contract.revealTime().catch((e) => {
            console.log("Error getting revealTime  : ", e);
            return BigNumber.from(0);
          }),
        ]);

        setConfigSet((c) => ({
          ...c,
          feeToAddress,
          baseURI,
          maxMintInTotalPerWallet: +maxMintInTotalPerWallet.toString(),
          revealTime: revealTime.toNumber(),
        }));
        setFeeAddressBgProc((v) => v - 1);
        setBaseURIBgProc((v) => v - 1);
        setMaxMintInTotalPerWalletBgProc((v) => v - 1);
      })();
    } catch (error) {
      setMaxMintInTotalPerWalletBgProc((v) => v - 1);
      setBaseURIBgProc((v) => v - 1);
      setFeeAddressBgProc((v) => v - 1);
      console.log("Error fetching : ", error);
    }
  }, [collectionType, projectAddress, projectChainId]);
rzadp commented 2 years ago

@ashrhmn It might be. Instead, try:

import { providers } from 'ethers'
const { StaticJsonRpcProvider } = providers

new StaticJsonRpcProvider(RPC_URLS[projectChainId])
ashrhmn commented 2 years ago

I tested for a day. Using StaticJsonRpcProvider drastically reduced network calls. The last 24h reporting says a total of around 5k calls to the API.

image

Still, I am having a lot of chainId requests but that is negligible I guess.