4

(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

3

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

2

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:)

1
  • This works, should be accepted. Commented Nov 22, 2023 at 20:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.