Search code examples
soliditysmartcontractshardhat

Get deployed contract by address


According to this doc. We can use ethers.getContract to get deployed contract.

I have deployed my contract at 0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2 and my creator address is 0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385 now to get contract i am doing

deployedContract = await ethers.getContract(
                              "0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2",
                              "0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385"
                              )

But its throwing error

 Error: No Contract deployed with name 0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2

But you can see contract is deployed https://goerli.etherscan.io/address/0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2

Can someone help me here?


Solution

  • It's just simple. You can use getContractAt function.

    getContractAt: <T extends ethers.Contract>(
      nameOrAbi: string | any[],
      address: string,
      signer?: ethers.Signer | string
    ) => Promise<T>;
    

    So in your case, it could be:

    const raffle = await ethers.getcontractAt(
      "Raffle",
      "0x33F4623337b8F9EDc9529a79F0d68B2BeC98d5E2",
      "0x6e0F5B57FEdc8911722c92dcD5D7D0cf69ceA385"
    );
    

    In addition, you got the error since you used the contract address for the contract name.

      getContract: <T extends ethers.Contract>(
        name: string,
        signer?: ethers.Signer | string
      ) => Promise<T>;