Where is Transfer (in IERC20.sol) implemented?

Hi,

I'm wondering where Transfer is defined?

emit Transfer(sender, recipient, amount);

thanks,
Steve

It is defined in the interface contract for IERC20.

pragma solidity ^0.5.2;

/**
 * @title ERC20 interface
 * @dev see https://eips.ethereum.org/EIPS/eip-20
 */
interface IERC20 {
    function transfer(address to, uint256 value) external returns (bool);

    function approve(address spender, uint256 value) external returns (bool);

    function transferFrom(address from, address to, uint256 value) external returns (bool);

    function totalSupply() external view returns (uint256);

    function balanceOf(address who) external view returns (uint256);

    function allowance(address owner, address spender) external view returns (uint256);

    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(address indexed owner, address indexed spender, uint256 value);
}
2 Likes

Thanks @itinance. Can you also tell me where/how this Transfer interface is implemented?

1 Like

You haven’t mentioned, which implementation you are referring to. I thought it would be ERC20.sol. And there you will see something like this

contract ERC20 is Initializable, IERC20 {

Btw, can you please clarify your question once more? Apparently I got it wrong

1 Like

Yes, ERC20.sol:

contract ERC20 is IERC20

So I do see the interface for the event Transfer in IERC20.sol but I don’t see anywhere where the code for Transfer is implemented. So I’m wondering how and where Transfer is implemented. Does EVM take care of this?

1 Like

I don’t know what you mean actually.

In IERC20.sol the transfer-Event is defined as follows:

event Transfer(address indexed from, address indexed to, uint256 value);

And ERC20 is dispatching this event:

function _transfer(address from, address to, uint256 value) internal {
    require(to != address(0));

    _balances[from] = _balances[from].sub(value);
    _balances[to] = _balances[to].add(value);
    emit Transfer(from, to, value);
}

Both, transfer and transferFrom are executing _transfer.
Is this explaining your issue? Anything else you want to know?

1 Like

Hi @itinance,

I come from a Java background and am new to Solidity but it seems to me that both Java and Solidity have the same concept of interfaces that need to be implemented; so, I see that ERC20.sol implements the functions from the interface IERC20.sol.

ERC20.sol:

@dev Implementation of the IERC20 interface.

This implementation is agnostic to the way tokens are created.

In ERC20.sol, I see that it implements all of the function in the IERC20.sol; however, ERC20.sol does not implement the Transfer and Approval events:

IERC20.sol:

/**
 * @dev Emitted when `value` tokens are moved from one account (`from`) to
 * another (`to`).
 *
 * Note that `value` may be zero.
 */
event Transfer(address indexed from, address indexed to, uint256 value);

/**
 * @dev Emitted when the allowance of a `spender` for an `owner` is set by
 * a call to `approve`. `value` is the new allowance.
 */
event Approval(address indexed owner, address indexed spender, uint256 value);

My question is: where is the implementing code for Transfer() and Approval()?

Thanks,
Steve

1 Like

As per @itinance Transfer and Approval events are declared in IERC20.sol

From the Solidity documentation:

https://solidity.readthedocs.io/en/latest/contracts.html#events
Events are inheritable members of contracts.

https://solidity.readthedocs.io/en/latest/contracts.html#interfaces
Contracts can inherit interfaces as they would inherit other contracts.

There is no implementation other than the declaration for events. Whilst functions can be declared in the interface and must be implemented in a contract.

1 Like

OK, thanks to both @abcoathup and @itinance, so interesting that there is no no implementation other than the declaration for events. That makes me think that both of these events are part of the EVM and the EVM already has defined what it does for a Transfer?

Maybe the best way for me to ask is to ask what happens when this is called in ERC20.sol:

emit Transfer(from, to, value);

1 Like

Hi @Steve_L

You are correct, events are an abstraction of the EVM logging functionality.
Emitting an event causes the event arguments to be stored in the transaction's log.

For more detail, see the Solidity documentation:

https://solidity.readthedocs.io/en/latest/contracts.html#events
Solidity events give an abstraction on top of the EVM’s logging functionality. Applications can subscribe and listen to these events through the RPC interface of an Ethereum client.

Events are inheritable members of contracts. When you call them, they cause the arguments to be stored in the transaction’s log - a special data structure in the blockchain. These logs are associated with the address of the contract, are incorporated into the blockchain, and stay there as long as a block is accessible (forever as of the Frontier and Homestead releases, but this might change with Serenity). The Log and its event data is not accessible from within contracts (not even from the contract that created them).

2 Likes

Housekeeping, I marked @itinance reply as the solution to the question.

Thanks so much :pray: @itinance for answering community questions.

Questions and answers increase the knowledge base of the whole community :rocket: and is very much appreciated @Steve_L and @itinance

1 Like

Thanks @itinance and @abcoathup!

2 Likes