How to get signer for a contract?

1k Views Asked by At

I have a contract A that deploys another contract (B).

  const A = await deployer.loadArtifact("A");
  const a = await deployer.deploy(A) as A;

I can get address of contract B via function and create a reference to contract b:

      let bAddress = await a.getBAddres();
      let b = await hre.ethers.getContractAt("contracts/B.sol:B", bAddress, ???);

If I will call a function from contract b - I will receive an error: sending a transaction requires a signer So I need a signer to properly call functions from contract B. What exactly should I pass getContractAt?

I tried to pass

  1. the deployer, which is (const deployer = new Deployer(hre, wallet);)
  2. owner from const [owner, addr1, addr2] = await hre.ethers.getSigners();
  3. tried to get signer from address: let signer = await hre.ethers.getSigner(bAddress);
1

There are 1 best solutions below

0
Petr Hejda On

getContractAt() is a function of the Hardhat Ethers plugin - not of the core ethers library. More context in this answer.

It accepts instance of ethers.Signer as the 3rd param.

Assuming you're using a JSON RPC provider, you can retrieve the signer object using await provider.getSigner().

Ethers docs: https://docs.ethers.org/v5/api/providers/jsonrpc-provider/#JsonRpcProvider-getSigner