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);