Solidity Ethers.js – How to Specify Interacting, Owner, or Deployer Address?

ethers.jssolidity

How can I specify a particular address interacting from or deploying a contract in ethersjs? Basically, how can I specify who is the signer or owner or user address in ethersjs?

In the tests, it seems like the first account from the list of dummy signers will always be picked as the address doing all the stuff.

In Truffle, there is something along this:

deployer.deploy(myContract, { from: "0x123" });

I'm using hardhat and ethersjs. How can I do something similar by specifying an address interacting or deploying a contract in ethersjs too?

Best Answer

Both Contract and ContractFactory objects have a method called connect that you can use to do this. See docs for Contract and ContractFactory.

Assuming you have an instance of the Signer object called signer, you can do something like this:

// Connect signer to contract and call the eat method on contract
contract.connect(signer).eat('Pizza');