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?