1

Having these interfaces

interface IERC20 {
    function totalSupply() external view returns (uint);
    function balanceOf(address account) external view returns (uint);
    function transfer(address recipient, uint amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

interface IUniswapV2Router {
  function getAmountsOut(uint256 amountIn, address[] memory path) external view returns (uint256[] memory amounts);
  function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) external returns (uint256[] memory amounts);
}

interface IUniswapV2Pair {
  function token0() external view returns (address);
  function token1() external view returns (address);
  function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data) external;
}

how could I mock on a test this: IERC20(_token).approve(router, _amount);?

I've seen a couple libraries that can mock another contract, but couldn't find a solution to a contract instance from an interface

1 Answer 1

0

got to actually build out the contract.

For a mock ERC20, the correct answer is that you can use OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC20

If you're like me though and want just one file and the bare minimum, I made one here: https://github.com/themandalore/charon/blob/main/contracts/mocks/MockERC20.sol

4
  • Right, but this implies on having these contracts locally, what if I just want to create an instance through an interface? Commented Jun 2, 2022 at 13:49
  • can't do that if you actually want to interact with it, there's no logic.
    – thefett
    Commented Jun 2, 2022 at 15:55
  • cryptomarketpool.com/interface-in-solidity-smart-contracts/…. are you sure? check out this article Commented Jun 2, 2022 at 17:56
  • yup I'm sure. You usually just use an interface to interact with something you know is there. So for example IERC20(tokenAddress).transfer(_to,_amount); I don't need to know the specific contract logic of the tokenAddress, I just need to know it implements IERC20
    – thefett
    Commented Jun 3, 2022 at 12:25

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.