Cannot switch signers with ethers.js for a contract interaction using Hardhat

Viewed 453

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?

1 Answers

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.

Related