I can deploy a contract using ethers and Hardhat with the following:

const myContract = await hre.ethers.getContractFactory("SomeContract");
const deployedContract = await myContract.deploy();

I can even interact with the methods from that deployed contract if I use that deployedContract variable. However what if the contract has already been deployed and I want to interact with it? From what I've read, I need to create an instance of that contract. And I've tried to do that like this:

const provider = new ethers.providers.JsonRpcProvider();
const fs = require('fs');
const abi = JSON.parse(fs.readFileSync('./abi/SomeContract.json', 'utf8'));
const contractInstance = new ethers.Contract('contract address goes here', abi, provider);

I believe an instance is created on that contractInstance variable, but if I try to call methods on that instance in the console or from a script, I get a WARNING: Calling an account which is not a contract error.

Maybe I'm misunderstanding something about how contract instances are supposed to work?


I ran into the same question and stumbled upon a section in the OpenZeppelin docs that may work for you (it did for me):

For example:

const MyContract = await ethers.getContractFactory("MyContract");
const contract = await MyContract.attach(
  "0x..." // The deployed contract address

// Now you can call functions of the contract
await contract.doTheThing();

This is how I use to instantiate contract at a specific address in Hardhat:

const accounts = await hre.ethers.getSigners()
const MyContract = await ethers.getContractFactory("MyContract");
const myContract = new ethers.Contract(MyContract, MyContract.interface, accounts[0]);

Hope it helps! :)

  • How does hardhat know the previously deployed address? Also I think it might be accounts[0].address
    – NickJ
    Apr 25 at 23:00
  • ethers.Contract first argument must be an address. So for example const contract = await MyContract.deploy(); contract.address Sep 9 at 2:55

This would seem like a wrong chain issue. Are you sure that Hardhat is pointed at the same chain as the contract? You can change the chain Hardhat runs on by either changing the value of defaultNetwork in hardhat.config or by running with the --network flag (for example --network rinkeby), provided there is a corresponding object in networks in hardhat.config.

Incidentally, you don't need to use fs to import the ABI. You can import it using an import or require.

const someContractAbi = require("./abi/SomeContract.json");

Depending on the setup, you may need to select the abi object from the ABI, though that doesn't seem to be your issue. For importing, you'd likely want a separate file that you copy-paste the abi object into.

const someContractAbi = require("./abi/SomeContract.json").abi;

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.