Approve USDC using IERC20.approve

Hi! I am also new to the blockchain space and working on a dApp with a team. We are currently trying to use the openZepplin IERC20 interface to send USDC in our smart contract. We’re having some trouble figuring out how to use it on the Ropsten testnet. We found the contract address of a test USDC token and have been able to call the IERC20 function on it and view our wallets usdc balance. However we are struggling to figure out how and where to call the token.approve function. Whenever we call it in the same function or even from a separate contract, calling transferFrom still gives us an error, not enough funds approved, and calling token.allowance returns 0. Any help would be greatly appreciated thanks!!

Here is some sample code below of what we want to do with the IERC20 interface:

contract usdcSending{
    address public sender;
    address public usdcContractAd = 0x0D9C8723B343A8368BebE0B5E89273fF8D712e3C;
    uint256 public heldAmount = 0;
    uint256 public approvedFunds = 0;
    IERC20 token = IERC20(contractAd2);
    
    
    constructor() public{
    sender = msg.sender;
    heldAmount = token.balanceOf(sender);
  }
    
    function payIn(uint256 amount) public{
        token.approve(address(this), 100); //doesn't seem to work
        approvedFunds = token.allowance(sender, address(this));
        heldAmount = token.balanceOf(sender);
        token.transferFrom(msg.sender, address(this), amount);
    }
    
    function payOut(address recipient, uint256 amount) public{
        token.transferFrom(address(this), recipient, amount);
    }
    
}
1 Like
contract usdcSending{
address public sender;
address public usdcContractAd = 0x0D9C8723B343A8368BebE0B5E89273fF8D712e3C;
uint256 public heldAmount = 0;
uint256 public approvedFunds = 0;
IERC20 token = IERC20(contractAd2);

constructor() public{
sender = msg.sender;
heldAmount = token.balanceOf(sender);
}

function payIn(uint256 amount) public{
    token.approve(address(this), 100); //doesn't seem to work
    approvedFunds = token.allowance(sender, address(this));
    heldAmount = token.balanceOf(sender);
    token.transferFrom(msg.sender, address(this), amount);
}

function payOut(address recipient, uint256 amount) public{
    token.transferFrom(address(this), recipient, amount);
}
}
1 Like

Hi @jj7302,

Welcome to the community forum :wave:

Thanks for posting here.

A token holder can approve an allowance for another contract to use by directly calling approve on the token contract. The holder can then call a function on the other contract which will be able to transferFrom the holder with the amount of the approved allowance.

See the following example:
Example on how to use ERC20 token in another contract

To withdraw tokens from a contract, the contract can just use transfer in a function to withdraw. Though the withdraw function should be protected by some form of access control otherwise any address can call.

See the following example:
Example of withdrawing an ERC20 from another contract

1 Like

Hi @jj7302,

Did you need any more information?

1 Like

@abcoathup thank you for all your help and for following up!! I still don’t completely understand where we are supposed to make the call to IERC20.increaseAllowance. If we are using Web3 to allow our application to interact with the contract would we use Web3 instead of truffle to make the call?

1 Like

Hi @jj7302,

Two transactions are required, one to approve an allowance and the second to call payIn which then calls transferFrom.

If you are creating a dapp, then you make both calls from web3. See: https://docs.openzeppelin.com/learn/deploying-and-interacting#interacting-programmatically

Example on how to use ERC20 token in another contract just uses the Truffle console for convenience to interact with the contracts.

1 Like

Hi @abcoathup,

Oh okay that makes a lot of sense. Thanks for all the help!

1 Like