Hi everyone,
I'm trying to make a simple contract, that receives USDC as the method of payment.
Here is the code:
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract owned {
address owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner,
"Only the contract owner can call this function");
_;
}
}
contract HandleUSDC is owned {
address public _USDC = 0x2058A9D7613eEE744279e3856Ef0eAda5FCbaA7e;
IERC20 public USDC = IERC20(_USDC);
function allowSpender(address spender, uint256 amount) external returns(bool) {
return USDC.approve(spender, amount);
}
function sendFromTo(address from, address to, uint256 amount) external{
USDC.transferFrom(from, to, amount);
}
function checkBalance_USDC(address wallet) public view returns(uint) {
return USDC.balanceOf(wallet);
}
function checkAllowance(address owner, address spender) public view returns(uint) {
return USDC.allowance(owner, spender);
}
}
So what I do is that I approve the contract ( address(this) ) to have access to 10 tokens, by calling allowSpender().
That works fine (I hope), but then:
- I can't check the allowance using checkAllowance() method, no matter in which order I put the addresses of msg.sender and address(this)
- When I try to call sendFromTo() using these parameters
from: msg.sender
to: address(this)
amount: 10
I get this error from remix:
execution reverted: ERC20: transfer amount exceeds allowance
I don't understand what I'm doing wrong and how I can fix this issue
BTW when I approve, MetaMask shows "Contract Interaction", instead of "Approve USDC spend limit".
P.S. I know similar questions have been asked, but none of them share the solution once they solve the problem!