I've been writing unit tests for a contract using Hardhat + Ethers + Typescript, let's call it ContractA. This ContractA has a hardcoded address of a deployed ERC20 token in it like this TokenA = '0x...'
.
So in the tests I do:
ContractA = await ethers.getContractFactory("ContractA");
contractA = await upgrades.deployProxy(ContractA);
and then I proceed to test some functions, which work normally.
But when I try to test a particular function in ContractA that uses the hardcoded ERC20 address like this:
function buyExtra(address Owner, uint256 count) public whenNotPaused {
...
IERC20Upgradeable(TokenA).safeTransferFrom(msg.sender, address(this), count);
...
}
it doesn't work, and I received the error
Error: VM Exception while processing transaction: revert Address: call to non-contract.
PS: I think I understand the reason, that is because the hardcoded address given in ContractA is the address of the ERC20 token in Rinkeby. As such, it does not exist in the test environment.
So what should I do? Is it possible to deploy the ERC20 contract in the test environment with the exact the same address it has in Rinkeby? Is this what I need to do? Btw, I don't have the ERC20 contract in my project.