3

I recently started learning to use hardhat to interact with and deploy smart contracts and I noticed that while brownie can accept a from address field for chain transactions, hardhat doesn't.

  • Does hardhat automatically supply this based on the address that deployed the transaction?
  • How do I call a transaction with a different address using hardhat?

E.g, Barebone code to deploy a Box contract and call a function store which accepts an integer:

Brownie

  • to deploy
    box = Box.deploy({"from": account})

  • to create a tx
    box_tx = box.store(42, {"from": account})               from account here*

hardhat

  • to deploy
const box = await deploy("Box", {
   from: account,
   args: [],
});
  • to create a tx
    const boxTx = await box.store(42)               No from account here*, how do we add an account?

2 Answers 2

2

hardhat is using ethers.js by default, you can read about overrides.

You can call it this way

const boxTx = await box.store(42, {
  from: account 
})

hope that answers your question

0

Identical issue is resolved in this answer here on official etherjs github repo.

NOTE: By default, first account (i.e, signer at 0 index) from the list of accounts returned by ethers.getSigners method is used for all contract deployment and all transactions.

const [_, secondSigner, thirdSigner] = await ethers.getSigners();
await box.connect(secondSigner).store(42);
await box.connect(thirdSigner).store(42);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.