0

I'm looking at the function below and they access the balance of the contract through an interface, but interface functions don't have functionality so how is it returning a value? And the function isn't defined anywhere else in the repo

function withdrawTokens(address token) public {
        require(msg.sender == IOwnable(factory).owner());
        if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
            payable(IOwnable(factory).owner()).transfer(address(this).balance);
        } else {
            uint256 balance = IERC20(token).balanceOf(address(this));
            safeTransfer(token, IOwnable(factory).owner(), balance);
        }
    }

1 Answer 1

0

Interface is a virtual function that calls the specific function on the specified contract. To make this work the contract being called must have the function external or public, and the function must be defined.

In this case of IERC20(token).balanceOf(address(this)) IERC20 calls the function balanceOf located on the contract token, defined below:

function balanceOf(address account) public view virtual override returns (uint256) { 
    return _balances[account];
}
2
  • Follow up question: On the same contract the dev also makes a call called totalBalanceOf to the same address(token), but how did he know the contract contained the function if the contract code isn't anywhere (github nor etherscan) Commented Feb 11, 2022 at 1:28
  • He wouldn't unless he has the source code or ABI code from target contract. In the case the EVM cannot find the function on the target contract (whether that means wrong argument, typing, or name), the external call will get reverted then in turn revert the local function.
    – FudgyDRS
    Commented Feb 11, 2022 at 1:43

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.