0

I'm trying to understand the following code:

pragma solidity ^0.6.0;

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

contract Recipient {
    IERC20 private _token;

    event DoneStuff(address from, address to, uint256 amount);

    constructor (address token) public {
        _token = IERC20(token);
    }

    function doStuff() external {
        address from = msg.sender;
        address to = address(this);
        uint256 amount = 1e18;

        _token.transferFrom(from, to, amount);
        emit DoneStuff(from, to, amount);
    }
}

Here, the contract imports the IERC20 which is just an interface. I'm trying to understand why the contract can execute this _token.transferFrom(from, to, amount); without having the actual transferFrom function? Because the contract just imports the interface and does not have the transferFrom function implemented.

Could you help me clear my mind?

1 Answer 1

0

To call a function from another contract the compiler needs the target address, function signature and parameters.

At runtime the EVM will locate the contract's bytecode and execute the required function with the passed parameters.

2
  • It means that the _token is the existing contract that has transferFrom function? And this contract is just calling the "transferFrom" funciton from contract token? Am I right?
    – Ender
    May 20, 2021 at 7:47
  • 1
    @Ender Yes, exactly.
    – Ismael
    May 20, 2021 at 16:50

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.