3

Hardhat specifies that to use a different account for contract interactions you should use the connect() method, passing it a Signer, as such:

const [owner, addr1] = await ethers.getSigners();
/* ... */
await greeter.connect(addr1).setGreeting("Hello!");

Where greeter is the contract instance.

However, when I use a Signer as they specify to, I get the following error:

Error: invalid address or ENS name (argument="name", value="<SignerWithAddress 0x59F...34C>", code=INVALID_ARGUMENT, version=contracts/5.6.0)

The internet says to use an address, such as this issue suggesting to use something like addr1.address. But when I do, the following error results:

VoidSigner cannot sign transactions (operation="signTransaction", code=UNSUPPORTED_OPERATION, version=abstract-signer/5.6.0)

How can I switch signers/accounts when making contract calls with ethers.js and Hardhat?

Schmidt
  • 43
  • 1
  • 5
  • 1
    It seems that you are passing just the address string - not the whole `Wallet` object that you got from `getSigners()`, to the `connect()` function. Can you verify that e.g. with `console.log()` before using the `connect()` function? – Petr Hejda Jul 08 '22 at 17:23

1 Answers1

5

The error you get is because you are not passing in the full address object. Specifically, I get your exact error when I do this:

This does not work, but reproduces your error:

[deployer, account1] = await ethers.getSigners();
address1 = account1.address;
/* ... */
await greeter.connect(address1).setGreeting("Hello!");

This works, however:

[deployer, account1] = await ethers.getSigners();
/* ... */
await greeter.connect(account1).setGreeting("Hello!");

Notice that we do not call ".address" on account1 in the second example.

Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63