2

(Found this from the public Alchemy discord, and wanted to ask the question here so it can get indexed online 💙)


I'm trying to instantiate contracts in my javascript code so that I can call ABI methods.

However, when I try to use the alchemy-sdk object to create a new Contract, I get an error.

This is how I'm setting it up:

const CompoundorContract = new ethers.Contract(
  CONSTANTS.ADDRESSES.COMPOUNDER_CONTRACT_ADDRESS,
  CONSTANTS.CONTRACTS.COMPOUNDOR_ABI,
  alchemy.core
);

and this is the error I get:

Error: invalid signer or provider (argument="signerOrProvider", value={"config":{"apiKey":"my_key","network":"opt-mainnet","maxRetries":5}},code=INVALID_ARGUMENT,version=contracts/5.6.2

Does anyone know how to fix this?

2 Answers 2

2

Try this example of using the alchemy-sdk to connect to eth mainnet to load a contract instance.

I think the key difference is that you should use await alchemy.config.getProvider() instead of what you're currently doing with alchemy.core

This is because you need to instantiate one instance of an Alchemy provider connection vs. using the namespaced sdk module alchemy.core

1

instead of passing alchemy.core, pass return value of this

const ethersProvider = await alchemy.config.getProvider()

final:

const CompoundorContract = new 

ethers.Contract(
  CONSTANTS.ADDRESSES.COMPOUNDER_CONTRACT_ADDRESS,
  CONSTANTS.CONTRACTS.COMPOUNDOR_ABI,
  ethersProvider
);

please leave a comment if it does not work.

vote up plz, if it works:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.