Skip to content

Instantly share code, notes, and snippets.

@fvictorio
Last active August 13, 2021 12:08
Show Gist options
  • Save fvictorio/3f10a0b7637be91e83923af6eeba9aeb to your computer and use it in GitHub Desktop.
Save fvictorio/3f10a0b7637be91e83923af6eeba9aeb to your computer and use it in GitHub Desktop.
Getting tokens after forking mainnet

If you want to fund your accounts with some token (for example DAI) after forking mainnet, there are two things you can try.

One is to impersonate an account that has that token, and either use that address or send tokens from it to one of your test accounts. For example:

const [signer] = await ethers.getSigners()

// impersonate account; replace with an address that actually has your token
const addressWithTokens = "0x49a77c5703e877759307175756E6EFDb3669ed77"
await network.provider.send("hardhat_impersonateAccount", [addressWithTokens])
const impersonatedSigner = await ethers.getSigner(addressWithTokens)

// create the token instance
const token = await ethers.getContractAt("ERC20", tokenAddress)

// connect it to the impersonated signer and send it to your signer
await token.connect(impersonatedSigner).transfer(signer.address, "1000000000000000000")

This assumes that the impersonated account has enough ETH to make the transfer. You can send ETH to it first if you want, but keep in mind that if the account is a contract, then it might not accept receiving ETH.

Alternatively, since the test accounts start with 1000 ETH each, you can use any on-chain exchange to swap that ether for the token you want. How to do that depends on what exchange you want to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment