I don't fully understand what it means to impersonate an account in a hardhat.
- Does it mean that I can access some wallet balance?
- Does it work only for forked chains?
- How does it work with contract address?
Impersonating accounts only works on hardhat network including forked chains. If you could impersonate accounts on live networks it would be a major security issue. You can access the balance and interact with other contracts as if you had the private key of that account which is why it only works on hardhat.
You can impersonate contracts. Here is an example taken from the docs
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: ["0x364d6D0333432C3Ac016Ca832fb8594A8cE43Ca6"],
});
const signer = await ethers.getSigner("0x364d6D0333432C3Ac016Ca832fb8594A8cE43Ca6")
contract.connect(signer).balanceOf(accountAddress)
await hre.network.provider.request({
method: "hardhat_stopImpersonatingAccount",
params: ["0x364d6D0333432C3Ac016Ca832fb8594A8cE43Ca6"],
});