Search code examples
interfaceethereumsoliditysmartcontracts

Do all solidity functions in an imported interface get compiled into the smart contract bytecode, even if they aren't used?


I'm trying to keep a contract as small as possible. I know that if you import a solidity library, only the functions actually utilized in that library get compiled into the contract, increasing it's size.

I'm not sure if that goes for importing interfaces.

For example:

interface IDoThings {
    function transfer(address from, address to, uint256 amount) external;
    function setValue(uint256 newVal) external;
    function owner() external view returns (address);
}

contract DoingThings {
    function getOwnerOfAnotherContract(address target) public view returns (address) {
        return IDoThings(target).owner();
    }
}

When this get's compiled down, will the function selectors for transfer & setValue also be included in the contract's bytecode, or will only the owner function selector be included?


Solution

  • interface is like typescript types, they are used in compilation time and in the javascript bundle you do not see any type definitions.

    If you compile your code at https://remix.ethereum.org/

    enter image description here

    this is what you will get

    [
        {
            "inputs": [
                {
                    "internalType": "address",
                    "name": "target",
                    "type": "address"
                }
            ],
            "name": "getOwnerOfAnotherContract",
            "outputs": [
                {
                    "internalType": "address",
                    "name": "",
                    "type": "address"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        }
    ]
    

    there is nothing about interface or its function signatures