ReasonJun

Solidity : interface 본문

Blockchain/Solidity

Solidity : interface

ReasonJun 2023. 10. 17. 14:32
728x90

A Solidity interface is a contract that can only contain function declarations. Interfaces are used to define the common functionality of a group of related contracts.

 

  1. Interfaces cannot be inherited, they must be inherited with a contract.
  2. You cannot define a fully implemented function. Functions that are not implemented must be specified.
  3. The visibility specifier of the function must be external.
  4. Interfaces cannot define constructors.
  5. State variables cannot be defined.
  6. Modifier cannot be defined.

 

To create an interface, you use the interface keyword. For example, the following code defines an interface for a token contract:

interface IERC20 {
    function totalSupply() public view returns (uint256);
    function balanceOf(address account) public view returns (uint256);
    function transfer(address recipient, uint256 amount) public returns (bool);
    function allowance(address owner, address spender) public view returns (uint256);
    function approve(address spender, uint256 amount) public returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) public returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

This interface defines six functions:

  • totalSupply(): Returns the total supply of tokens.
  • balanceOf(): Returns the balance of a given account.
  • transfer(): Transfers tokens from one account to another.
  • allowance(): Returns the amount of tokens that one account is allowed to spend on behalf of another account.
  • approve(): Approves an account to spend tokens on behalf of the sender.
  • transferFrom(): Transfers tokens from one account to another on behalf of a third account.

Any contract that implements the IERC20 interface must implement all of these functions.

 

Interfaces can be used in a number of ways. For example, you can use an interface to define the requirements for a contract that you want to interact with. You can also use an interface to create a library of reusable functionality.

 

Here are some of the benefits of using interfaces in Solidity:

  • Improved readability: Interfaces can make your code more readable by making it clear which functions are required and which functions are optional.
  • Increased flexibility: Interfaces can make your code more flexible by allowing you to create different implementations of the same functionality.
  • Reduced code duplication: Interfaces can help to reduce code duplication by allowing you to define common functionality in a single contract.

Overall, interfaces are a powerful feature of Solidity that can be used to improve the quality, flexibility, and readability of your code.

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// It is similar to an abstract contract, but is created using the 'interface' keyword.
// There is no function, only implementation is possible. / external Only / no constructor / no state variable / Enumeration structure possible
// All internal functions are abstract functions.

interface BankingSystem {
    function deposit (uint) external  returns (uint);
    function withdraw (uint) external  returns (bytes32);
}

// example

contract Counter {
    uint public count;
    function increment() external {
        count += 1;
    }
}

interface ICounter {
    function count() external view returns (uint);

    function increment() external;
}


contract MyContract {
    function incrementCounter(address _counter) external {
        ICounter(_counter).increment();
    }

    function getCount(address _counter) external view returns (uint) {
        return ICounter(_counter).count();
    }
}

example : 

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

interface System {
    function versionCheck() external returns(uint);
    function errorCheck() external returns (bool);
    function boot() external returns(uint, bool);
}

contract Computer is System {
    function versionCheck() public pure override returns(uint) {
        return 3;
    }

    function errorCheck() public pure override returns(bool) {
        return true;
    }

    function boot () public pure override returns(uint, bool) {
        return (versionCheck(), errorCheck());
    }
}

contract Phone is System {
    function versionCheck() public pure override returns(uint) {
        return 25;
    }

    function errorCheck() public pure override returns(bool) {
        return true;
    }

    function boot () public pure override returns(uint, bool) {
        return (versionCheck(), errorCheck());
    }
}

 

728x90
Comments