상세 컨텐츠

본문 제목

Solidity - Interface

Programming Language/Solidity

by Yongari 2023. 1. 5. 21:17

본문

You can interact with other contracts by declaring an Interface.
인터페이스를 선언하여 다른 계약과 상호 작용할 수 있습니다.


Interface

  • They cannot inherit other contracts or interfaces.
    다른 계약이나 인터페이스를 상속할 수 없습니다.
  • All declared functions must be external.
    선언된 모든 함수는 외부 함수여야 합니다.
  • They cannot declare a constructor.
    생성자를 선언할 수 없습니다.
  • They cannot declare state variables.
    상태 변수를 선언할 수 없습니다.



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

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();
    }
}

// Uniswap example
// 유니스왑 예제 
interface UniswapV2Factory {
    function getPair(
        address tokenA,
        address tokenB
    ) external view returns (address pair);
}


interface UniswapV2Pair {
    function getReserves()
        external
        view
        returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

contract UniswapExample {
    address private factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
    address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    function getTokenReserves() external view returns (uint, uint) {
    	
        address pair = UniswapV2Factory(factory).getPair(dai, weth);
        (uint reserve0, uint reserve1, ) = UniswapV2Pair(pair).getReserves();
        return (reserve0, reserve1);
    }
}

Try on Remix

 

Remix - Ethereum IDE

 

remix.ethereum.org

 

'Programming Language > Solidity' 카테고리의 다른 글

Solidity - Call  (0) 2023.01.17
Solidity - Fallback  (0) 2023.01.13
Solidity - Visibility  (0) 2023.01.04
Solidity - Calling Parent Contracts  (0) 2023.01.04
Solidity - Shadowing Inherited State Variables  (0) 2022.12.31

관련글 더보기