1

Hello im making some smart contracts and i would like the user to pay in stablecoin for example usdc.

uint price=500;

function sendUSDC() public payable {
 address.send(price in USDC)
} 

I would like when calling this func to send USDC and i dont know how to do it.Could someone please help or at least send a tutorial or something.

1 Answer 1

1

You need to include token interface in your contract, you can add below code or you can just import openzeppelin's ERC20 contract interface.

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

and do following function in your contract:

IERC20(erc20TokenContractAddress).transferFrom(msg.sender, toAddress, amount);

Before doing that, you need to get approval from wallet owner by using a different agent, say JavaScript/client.

IERC20(erc20TokenContractAddress).approve(spenderAccount, amount)

Explained in detail here: https://medium.com/@harsha_90164/accept-dai-usdc-or-any-erc20-token-to-a-smart-contract-89b836228144

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.