Search code examples
ethereumsoliditysmartcontractsweb3pybrownie

Are Interfaces used for the sole purpose of generating ABI's in Solidity?


I have been following Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (https://www.youtube.com/watch?v=M576WGiDBdQ&t=28658s).

At a certain point in the tutorial (around hour 9), the following line of code exists:

weth=interface.IWeth(SomeAddress)

tx=weth.deposit({"from":account, "value": 0.01*10**18})

My understanding is that interfaces get compiled into ABI for use by some external entity.

the IWeth interface is as follows:

pragma solidity ^0.4.19;

/*this is the interface for the WETH contract */
interface IWeth {
    function allowance(address owner, address spender)
        external
        view
        returns (uint256 remaining);

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

    function balanceOf(address owner) external view returns (uint256 balance);

    function decimals() external view returns (uint8 decimalPlaces);

    function name() external view returns (string memory tokenName);

    function symbol() external view returns (string memory tokenSymbol);

    function totalSupply() external view returns (uint256 totalTokensIssued);

    function transfer(address to, uint256 value)
        external
        returns (bool success);

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

    function deposit() external;

    function withdraw(uint256 wad) external;
}

The above interface is not implemented anywhere in the project but the project compiles and runs just fine. In my limited understanding of OOP, shouldn't an interface be implemented somewhere? if so, where is it implemented? If not, does the interface ( in this case IWeth) exist for the sole purpose of generating an ABI?

Also, I'm confused about how the interface is used as in weth=interface.IWeth(SomeAddress). In general, how can we write ...InterfaceName(someArg) when it is not even declared in the interface file?

Thanks in advance!


Solution

  • You can use an interface in your contract to call functions in another contract. Interfaces are most useful when you need to add functionality but instead of adding complexity to your application you use it from another contract. They reduce code duplication.

    • brownie is already loading interfaces. This is from docs

    The InterfaceContainer object (available as interface) provides access to the interfaces within your project’s interfaces/ folder.

    • interface.IWeth(SomeAddress) this tells Ethereum virtual machine to create a contract instance of given contract address with the functionalities of the interface. And then your app is calling deposit of that contract instance