| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
- blockchain
 - API
 - REACT
 - graphQL
 - Props
 - express.js
 - HTML
 - node.js
 - tailwindcss
 - Redux
 - evm
 - JavaScript
 - 삶
 - SSR
 - bitcoin
 - hardhat
 - CSS
 - built in object
 - error
 - useState
 - concept
 - CLASS
 - nextJS
 - typeScript
 - solidity
 - web
 - Interface
 - middleware
 - 기준
 - Ethereum
 
- Today
 
- Total
 
ReasonJun
Solidity : interface 본문
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.
- Interfaces cannot be inherited, they must be inherited with a contract.
 - You cannot define a fully implemented function. Functions that are not implemented must be specified.
 - The visibility specifier of the function must be external.
 - Interfaces cannot define constructors.
 - State variables cannot be defined.
 - 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());
    }
}
'Blockchain > Solidity' 카테고리의 다른 글
| Solidity : class / object / method (object-oriented programming language) (0) | 2023.10.17 | 
|---|---|
| Solidity : Difference between 'abstract' and 'interface' (1) | 2023.10.17 | 
| Solidity : abstract (0) | 2023.10.17 | 
| Solidity : Overriding (virtual / override / super) (0) | 2023.10.17 | 
| Solidity : Overloading (1) | 2023.10.16 |