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

Same contract & ABI, many networks #115

Open
kasparkallas opened this issue Jan 17, 2023 · 2 comments
Open

Same contract & ABI, many networks #115

kasparkallas opened this issue Jan 17, 2023 · 2 comments

Comments

@kasparkallas
Copy link

I'm wondering if there's any special support for same contract & ABI deployed on many networks? Currently, many types are created that have are effectively duplicates. Not a big issue but I would imagine nowadays it's a common use-case where same contract is deployed to many networks.

@kasparkallas
Copy link
Author

kasparkallas commented Jan 18, 2023

Ended up using eth-sdk for the purpose of fetching the ABI and running the TypeChain. Created a custom function to use one network's TypeChain, more specifically the Factory, as the base for other networks.

You could say to go directly to TypeChain but the use of eth-sdk is still valuable for faster development process with new contracts and contract updates.

Example:

// Custom helper function for fetching network-based contract SDK without generating separate TypeChain/SDK for all the networks.
export const getContractSdk = (
  chainId: number,
  providerOrSigner: providers.Provider | Signer
) => {
  const network = findNetworkByChainId(chainId);
  const networkContractAddress = network?.contractAddress;
  const doesNetworkSupportContract = !!networkContractAddress;
  if (!doesNetworkSupportContract) {
    throw new Error(
      `Contract not available for network [${chainId}:${network?.name}].`
    );
  }

  // A specific network's TypeChain Factory used as the base for other networks.
  return Contract__factory.connect(
    networkContractAddress,
    providerOrSigner
  );
};

NOTE: For some reason, using import { Contract__factory } from "./client/types"; failed with Next.js & Webpack with the weird error: Module not found: Can't resolve './client/types'.

Fortunately, using the direct imports import { Contract__factory } from "./client/{esm/cjs}/types/factories/mainnet"; both worked.

@mauricedesaxe
Copy link

I have a similar situation. I have the same contract & ABI, deployed MANY times on one network. I don't want to have a duplicate abi file 30+ times 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants