How to use SafeERC20?

Hi Everyone,

I have a question on SafeERC20. I have incorporated it into our project. I tried it with a well-known non-standard contract. I cloned the non-standard contract repo locally and deployed it to my local chain. When I was using ERC20’s approve on this non-standard contract it was failing. After I made the changes for SafeERC20 [with using SafeERC20 for IERC20; and moving transfer to safeTransfer etc.] the approve and subsequent transfer started working as expected.

Now, I have the following questions: 1. I did not use safeApprove directly. How is it working even though I am calling from my JavaScript code only approve and not safeApprove? 2. Can I or should I be calling safeApprove, or preferably safeIncreaseAllowance from JavaScript? How?

Many thanks.

Nihat

1 Like

Generally speaking, as a standard ERC20 token, it should implement the following interface:

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    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 for non-standard ERC20 token, it will not implement the above functions totally, such as USDT, its contract like

function transfer(address recipient, uint256 amount) external

You can see above, its functions do not have a return value, so the SafeERC20 contract is doing compatibility.

safeApprove is just equal to approve.

I think it depends on you, safeApprove is ok, safeIncreaseAllowance is ok, too.
If you use web3.js, you can use like followings:

var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
var myContract = new web3.eth.Contract(CONTRACT_ABI,CONTRACT_ADDRESS);
myContract.methods.approve(to, amount);

For more details, you can have a look at the web3.js documentation

1 Like

Thank you for your feedback. I am using ethers.js and more or less I am using exactly the equivalent version of your example code (with approve) and it is working fine. However, I still have this burning question.

Can you do the following from JavaScript?
myContract.methods.safeApprove(to, amount);

Or, should you be doing the following from JavaScript?
myContract.methods.safeIncreaseAllowance(to, amount);

Neither of the two is working from JavaScript for me.

Thanks.

1 Like

If you use ethers.js, I think it should be contract.Function(xx), and for more details, you can have a look at their documentation: Contract (ethers.io)

1 Like

Hi @ngurmen,

Your contract wraps the non-compliant token using SafeERC20.

Users could then call approve on the non-compliant token. Then they could call the functions in your contract.

Have a look at the following Example on how to use ERC20 token in another contract. You could change the contract to use SafeERC20.

1 Like

Thanks for your message.

1 Like