1. Intro to Web3.js

  • to call a smart contract, we need to query below

    • The address of the smart contract
    • The function we want to call
    • The variables that we want to pass to the function
  • <script language="javascript" type="text/javascript" src="web3.min.js"></script>로 JS Calling

2. Web3 Providers

  • Infura: API를 통해 ethereum node를 유지할 수 있게 하는 서비스

    window.addEventListener('load', function() {
    
      // Checking if Web3 has been injected by the browser (Mist/MetaMask)
      if (typeof web3 !== 'undefined') {
        // Use Mist/MetaMask's provider
        web3js = new Web3(web3.currentProvider);
      } else {
        // Handle the case where the user doesn't have web3. Probably
        // show them a message telling them to install Metamask in
        // order to use our app.
      }
    
      // Now you can start your app & access web3js freely:
      startApp()
    
    })
    

3. Talking to Contracts

Contract Addresss

  • Contract를 compile하고 deploy하면 fixed address가 나온다.

Contract ABI

  • ABI: Application Binary Interface
    • contract method를 JSON 형태로 나타낸 것
    • compile 하고 deploy까지 마치면 compiler가 ABI를 준다

4. Calling Contract Functions

call: used for view and pure

  • local node에서만 작동하고, blockchain에 tx를 발생시키지 않는다 (view, pure가 Read-Only이기 때문)
  • myContract.methods.myMethod(123).call()과 같이, 인자를 주어 함수를 call 할 수 있다.

send: except for view and pure

  • blockchain에 tx 발생시키고 data도 변경한다
  • myContract.methods.myMethod(123).send()과 같이, 인자를 주어 함수를 send 할 수 있다.

Solidity public

  • 변수를 public으로 선언하면, public getter 함수가 변수명에 맞춰 자동으로 생성된다

5. Metamask & Accounts

  • 계정 확인을 위한 interval 함수는 다음과 같다

    var accountInterval = setInterval(function() {
      // Check if account has changed
      if (web3.eth.accounts[0] !== userAccount) {
        userAccount = web3.eth.accounts[0];
        // Call some function to update the UI with the new account
        updateInterface();
      }
    }, 100);
    

-

6. Displaying our Zombie Army

  • HTML에서 $의 의미: jQuery 선택자
    • jQuery: HTML document traversal, manipulation, event handling을 가능하게 하는 JS library

7. Sending Transactions

send의 특징 (call과 비교하여)

  1. from address를 필요로 한다: msg.sender가 될 address가 있어야 함수가 작동하기 때문
  2. cost gas
  3. significant delay: 함수가 chain에 미치는 영향이 정상적으로 반영되어야 하기 때문
  • ETH의 경우 평균적으로 15초가 소요된다
  • 따라서 async 한 코드 흐름이 필요하다

receipterror

  • receipt: zombie가 정상적으로 create 되고, 체인에 저장됐다는 것을 알려주는 event listener
  • error: tx가 정상적으로 반영되는 것을 막는 issue가 있다는 것을 알려주는 event listener
  • send, call에 대해 위 두가지 event listener를 달아주면 함수의 정상작동 여부를 확인할 수 있다. 특히, send의 경우에 on chain function이기 때문에 사실상 필수적이다.

8. Calling Payable Functions

wei

  • wei는 ether의 가장 작은 단위로, 10^18 wei가 1

9. Subscribing to Events

Filtering using indexed

  • 모든 user에 대해 event alert을 보내는 것은 비효율적이기 때문에, events.Transfer에 filter를 걸어야 한다.
  • filter를 거는 대상은 indexed로 특정할 수 있다.
    • indexed는 다음과 같이 event를 선언할 때 변수명 앞에 설정할 수 있다.
    • event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);

Querying past events

  • getPastEvents를 통해 fromBlock부터 toBlock까지의 event를 추적할 수 있다.

10. Wrappint It up

11. Lesson 6 Complete!