[Solidity] 230721 study
1. Settings
- 일반적인 앱과 달리, smart contract는 public API를 가져오는 것이 철학에 맞지 않다. 그래서 oracle을 가져와 데이터를 사용해야 한다.
- 우리가 사용하는 dependency와 그 설치 방법은 다음과 같다
- npm 구동:
npm init -y - 패키지 설치:
npm i truffle openzeppelin-solidity loom-js loom-truffle-provider bn.js axios
- npm 구동:
2. Calling Other Contracts
- 일반적인 방법으로 Hardcoding하면, oracle이 deployed 된 경우 우리는 smart contract, front-end를 다 다시 짜야한다.
- 따라서 smart contract address를 저장하는 변수를 하나 선언해서, oracle에서 쓸 수 있는 함수들을 호출하는 방법을 사용할 수 있다
-
pragma solidity 0.5.0; contract CallerContract { address private oracleAddress; function setOracleInstanceAddress (address _oracleInstanceAddress) public { oracleAddress = _oracleInstanceAddress; } } - 위와 같은 함수를 통해 oracle address를 지정하는 것으로 시작한다.
-
3. Calling Other Contracts (cont’d)
- Interface의 의미와 그 기능에 대해 알아보자
-
Interface는 contract랑 비슷하지만, 다음과 같은 차이점을 갖는다
- 변수,
constructor를 선언할 수 없다 - contract를 상속할 수도 없다
- 모든 함수는
external로 선언해야 한다. interface의 내용은 항상 다른 contract에 의해 호출되기 때문이다.
- 변수,
contractInstance = interfaceName(_address)와 같이 선언한 뒤,contractInstance.makeSandwich(param1, param2)와 같이 사용할 수 있다.interfaceName private contractInstance와 같이,private으로 설정해줘야 한다.
4. Function Modifiers
- Lesson 1, 2에서 했던 그
modifier가 맞다.
5. Using a Mapping to Keep Track of Requests
- Oracle에서 Eth price를 직접 가져오는게 아니고, 다음과 같은 과정을 필요로 한다
- smart contract가 oracle에 있는
getLatestEthPrice함수를 call getLatestEthPrice가 Ethprice와 관련된 uniqueid를 반환한다- oracle이 이
id를 바탕으로 Binance API에서 Eth price를 가져온다. -
- 의 결과를 oracle이
callbackfunction을 이용해서 caller contract에게 전달한다
- 의 결과를 oracle이
- smart contract가 oracle에 있는
6. The Callback Function
- Binance Public API에서 값을 가져오는 것은 asynchronous하기 때문에,
callbackfunction도 이에 맞춰야 한다. callbackfunction의 작동 방식은 다음과 같다.require문으로 적절한(valid)id가 함수를 호출했는지 확인- 적절한
id가 호출됐다면, 함수를 마저 실행하고,myRequestsmapping에서 해당 내용을 삭제한다 -
- 가 성공적으로 수행됐을 때 event를 fire(
emit)함으로써 front-end가 update를 확인하고 수행하게 한다
- 가 성공적으로 수행됐을 때 event를 fire(
7. The onlyOracle Modifier
- oracle contract가 아닌, 임의의 contract가 호출되는 것을 막아야 한다 –>
modifier를 사용modifier에서 제한하고 싶은 내용을require로 확인한 뒤에는_;을 넣어, modifier를 호출했던 함수로 돌아가 남은 내용을 실행하도록 유도해야 한다.
8. The getLatestEthPrice Function
9. The getLatestEthPrice Function (cont’d)
10. The setLatestEthPrice Function
- Owner만 호출할 수 있도록 함수 구성:
onlyOwnermodifier 추가하기