[Solidity] 230801 study
1. Getting Set up
- deploy된 contract와 JS를 연동하기 위해서는
web3.eth.Contract를 통해 interact 해야 한다.const myContract = new web3js.eth.Contract(myCOntractJSON.abi, myContractJSON.networks[networkId].address)
networkId를 하드 코딩하는 것도 가능하기는 하지만,await web3js.eth.net.getId()와 같이 다룬다면 특별한 관리 없이 networkId를 얻을 수 있다.
2. Listening for Events
- event를 async로 들어야 interaciton이 가능하다
3. Adding a Request to the Processing Queue
- event가 다음과 같이 선언됐다고 가정하자:
event TransferTokens(address from, address to, uint256 amount)- 다음과 같이 event의 return value를 나눌 수 있다
-
async function parseEvent(event) { const from = event.returnValues.from; const to = event.returnValues.to; const amount = event.returnValues.amount; }
arrName.push({key: value})로 push 할 수 있다.
4. Looping Trough the Processing Queue
- JS는 single-threaded이기 떄문에
pendingRequestsarray를 있는 그대로 처리하면 시간이 매우 오래 걸린다- 따라서 array를 일정 크기로 잘라 수행해야 한다.
5. Processing the Queue
- array에 대해
shift함수는 다음과 같은 기능을 수행한다- 인자로 주어진 element를 return한다.
- 1.에서 return한 인자를 array에서 지운다
- array 길이를 1만큼 줄인다
6. The Retry Loop
7. Using Try and Catch in JavaScript
8. Using Try and Catch in JavaScript - Cont’d
try에 실행 내용이,catch에는 실행 내용에 대해 exception이 throw 됐을 때 실행할 내용이 적혀있어야 한다
9. Working with Numbers in Ethereum and JavaScript
10. Returning multiple values in JavaScript
const {one, two} = myAwesomeFuction()과 같이, 중괄호 ({})를 사용하여 multiple value를 뱉는 function의 결과값을 변수에 저장할 수 있다.
11. Wrapping Up the Oracle
12. Returning multiple values in JavaScript
13. Deploy the Contracts
14. Putting Everything together
node Client.js를 터미널에서 실행하는 것으로 이번 실습 코드를 실행시킨다.