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);
}
}
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.
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.
@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?