How can I use different addresses to call functions in Hardhat tests and scripts?
Asked Answered
E

1

17

When running tests and scripts, all functions are called by the first address provided by Hardhat.

I'd like to know if there's a way to change the calling address within the same test or script.

Thanks in advance!

Enclave answered 25/6, 2021 at 6:19 Comment(0)
A
26

You can use the connect() method.

Code example from https://hardhat.org/tutorial/testing-contracts.html#using-a-different-account

const [owner, addr1, addr2] = await ethers.getSigners();

// Transfer 50 tokens from owner to addr1
await hardhatToken.transfer(addr1.address, 50);

// Transfer 50 tokens from addr1 to addr2
await hardhatToken.connect(addr1).transfer(addr2.address, 50);
Antislavery answered 25/6, 2021 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.