0

I don't know why the transfer function does not work. I try npx hardhat test but it seems to fail when transferring.

function deposit(address token, uint256 amount) external onlyOwner {
    uint256 balance = ERC20(token).balanceOf(msg.sender);
    require(balance > amount, "not enough money to deposit");
    ERC20(token).approve(address(this), amount);
    ERC20(token).transferFrom(msg.sender, address(this), amount);
} 

This is the hardhat js code which executes the contract

 let IWETH = await ethers.getContractAt(
      "IWETH",
      "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
    );
    await IWETH.deposit({
      value: ethers.utils.parseUnits("100.0", "ether"),
    });
    await contract
      .connect(owner)
      .deposit(
        "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
        ethers.utils.parseUnits("2", "ether")
      ); 

I seems like it failed at ERC20(token).transferFrom(msg.sender, address(this), amount);

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dadan
  • 1
  • Does this answer your question? [Solidity - transfer amount exceeds spender allowance](https://stackoverflow.com/questions/68014659/solidity-transfer-amount-exceeds-spender-allowance) – Petr Hejda Sep 01 '21 at 20:15
  • what type of error do you get? can you give me more information? – cherry Sep 05 '21 at 08:44

0 Answers0