[Solidity] 230701 study
1. Chainlink Data Feeds Introduction
Smart contract & real world
- ETH를 USD 단위로 인출하려면, smart contract는 USD가 얼마인지 알고 있어야 한다. ETH-USD를 단순하게 페어링하는 방법은 다음과 같다
- Binance API에서 따온다
- JS 스크립트를 직접 구현한다
- 하지만 위와 같은 2가지 방법은 centralized point of failure로 이어지기 때문에 smart contract의 취지에 어긋난다
- 그래서 DON과 DDS를 사용해야한다
- DON: Decentralized Oracle Network
- DDS: Decentralized Data Sources
Chainlink
- Chainlink는 DONs를 위한 프레임워크이다.
- multiple oracle에 분배된 multiple data를 수집할 수 있게 해준다
- 데이터를 저렴하게, 정확한 방법으로 수집하는 방법임과 동시에 decentralized manner에 맞게 수집하게 해 준다
- OCR: Off-Chain Reporting
- off-chain에서 consensus에 다다르면, 이를 single tx로 만들어서 on-chain에 올리는 것이 Chainlink의 작업 방식
2. Importing from NPM and Github
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";로 chainlink의AggregatorV3Interface를 불러온다
3. Aggregator V3 interface
- Data Feeds도 Verified, Monitored, Provisonal, Custom, Specialized,
Deprecating등으로 나뉜다
4. Working with Tuples
-
latestRoundData()roundId: each price update gets unique round IDanswer: the current pricestartedAt: when the round starteddupdatedAt: when the round was updatedansweredInRound: theroundIdwhen the answer was computed
-
Solidity에서의 tuple 자료형
- syntactic grouping of expressions
- 함수가 복수개의 variable을 반환했을 때, 그 결과는 tuple에 저장할 수 있다
priceFeedcontract의latestRoundData에서 여러 개의 값이 반환되지만, 2번째 리턴값(int price)만 저장하고 반환받고 싶을 때 아래와 같이 작성한다.function getLatestPrice() public view returns (int) { (,int price,,,) = priceFeed.latestRoundData(); return price; }