I'm unit testing a contract that only accepts DAI as payment for an ERC20 token - so a transfer function of DAI from the user to the contract looks like this:

// DAI address on mainnet: 0x6B175474E89094C44Da98b954EedeAC495271d0F

function buyToken() external {

  // some code

  IERC20 dai = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);
  dai.transferFrom(msg.sender, address(this), totalPrice);

  // more code


I'm not too sure how to go about testing this buyToken() function in Ethers.js/Hardhat on my local environment. I'm thinking of deploying a dummy ERC20 token contract at the above address to mimic DAI, but is it possible to deploy it at 0x6B175474E89094C44Da98b954EedeAC495271d0F specifically?

How should I go about this without testing it on the mainnet itself?

2 Answers 2


I don't think you can spoof addresses like that, even locally. The same rules apply everywhere in generating addresses.

What you can do is fork mainnet. At least Hardhat supports this easily, and I think also Truffle supports this in some fashion nowadays. Once you've forked mainnet, you can run tests against your local version and impersonate accounts. You can for example impersonate some account which has the said tokens, if needed. Or add yourself a million Eth and exchange those to the said token in an exchange.

You can read more about Hardhat mainnet forking here: https://hardhat.org/guides/mainnet-forking.html

  • Thanks, that sounds like a great idea! I'll checkout the mainnet forking tool.
    – Pottedtree
    Jul 2, 2021 at 8:46

If you want your contract's address should not be changed after the deployment on anywhere(whether it is public or local), may be you can try to develop upgradable smart contracts. These won't change your state data of your smart contract including your smart contract address.

You can develop uppgradable smart contracts either using hardhat or truffle

For Harhat --> https://docs.openzeppelin.com/learn/upgrading-smart-contracts

For Truffle --> https://dev.to/paulrberg/how-to-write-upgradeable-smart-contracts-with-truffle-50-and-zeppelinos-20-4d6k

using Truffle proxy plugin --> https://www.trufflesuite.com/blog/a-sweet-upgradeable-contract-experience-with-openzeppelin-and-truffle

  • I don't think the question was about keeping the same contract address, but to get a specific contract address. Jul 2, 2021 at 8:47
  • Thanks for your help, but I don't think this answers the question. It's not regarding my own address, but rather that of one that has been deployed by someone else.
    – Pottedtree
    Jul 2, 2021 at 8:47
  • Then try some forking tools provided by hardhat..Although building upgradable smart contracts is the best practice.. If you want to work with other's contracts lto do things like forking, You'll be need their project Key which they used to deploy. Jul 2, 2021 at 9:15

Your Answer

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

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