How to use SafeERC20 with interfaces

I know for SafeERC20 you just add

using SafeERC20 for IERC20;

However, if I want to define interfaces, what would the syntax be? I currently have it as

interface ERC20Interface {
  function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
  function balanceOf(address account) external returns (uint256);
}

contract Test is ERC721Pausable
{
  using SafeERC20 for ERC20Interface;
}

For some reason, when I call safeTransferFrom it says it's not found

The SafeERC20 functions are defined to work on IERC20, so safeTransferFrom(IERC20 token, ...). You can't change that with the using for statement.

Hey, is it possible to do it for an interface inheriting from IERC20 then? For example, in the following code, got this error" TypeError: Member "safeTransfer" not found or not visible after ... lookup...".

// SPDX-License-Identifier: MIT

pragma solidity 0.8.17;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

interface IToken is IERC20 {}

contract TestToken is IToken, ERC20("Test Token", "TT") {}

contract Test {
    using SafeERC20 for IERC20;

    IToken public token;

    constructor (address _tokenAddr) {
        token = IToken(_tokenAddr);
    }

    function doSth() external {
        token.safeTransfer(msg.sender, 1 ether);
    }
}

I believe Solidity doesn't allow this. It needs to be the exact type. So you should write IERC20(token).safeTransfer(...)

1 Like

Thanks, that's what I figured.