‘이더리움 댑 개발’ 세미나 16. Ethers

What is Ethers?

  • The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem.
    • 지갑 기능 제공
      • Keep your private keys in your client, safe and sound
      • Import and export JSON wallets (Geth, Parity and crowdsale)
      • Import and export BIP 39 mnemonic phrases (12 word backup phrases) and HD Wallets (English, Italian, Japanese, Korean, Simplified Chinese, Traditional Chinese; more coming soon)
    • 컨트랙트 지원
      • Meta-classes create JavaScript objects from any contract ABI, including ABIv2 and Human-Readable ABI
      • Connect to Ethereum nodes over JSON-RPC, INFURA, Etherscan, Alchemy, Cloudflare or MetaMask.
    • ENS names are first-class citizens; they can be used anywhere an Ethereum addresses can be used
    • Tiny (~88kb compressed; 284kb uncompressed), Complete functionality for all your Ethereum needs
    • MIT License, Fully TypeScript ready, with definition files and full TypeScript source, Large collection of test cases which are maintained and added to

Getting Started

Installing
  • npm install –save ethers
Importing
  • Node.js
    • const { ethers } = require(“ethers”);
  • ES6
    • import { ethers } from “ethers”;

 

Web Browser

It is generally better practice (for security reasons) to copy the ethers library to your own webserver and serve it yourself.

 

Common Terminology
  • Provider
    • A Provider (in ethers) is a class which provides an abstraction for a connection to the Ethereum Network. It provides read-only access to the Blockchain and its status.
  • Signer
    • A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages and transactions to authorize the network to charge your account ether to perform operations.
  • Contract
    • A Contract is an abstraction which represents a connection to a specific contract on the Ethereum Network, so that applications can use it like a normal JavaScript object.
Connecting to Ethereum: Metamask

이더리움에서 개발을 시작하는 가장 쉽고 빠른 방법은 메타마스크를 사용하는 것입니다. 메타마스크는 이더리움 네트워크에 대한 연결과 개인 키를 가지고 서명할 수 있는 기능을 제공합니다.

  • const provider = new ethers.providers.Web3Provider(window.ethereum)
    • 메타마스크는 window.ethereum으로 자신을 inject 합니다.
      • 메타마스크는 web3를 지원하기 때문에 provider 또한 web3 provider입니다. 
      • ethers가 메타마스크를 지원하려면 web3 provider를 지원해야 합니다.
        • ethers는 web3 provider를 래핑하는 Web3Provider를 제공합니다.
  • const signer = provider.getSigner()
    • 메타마스크는 지갑 기능으로 서명 기능을 제공합니다.
    • ethers는 서명을 위해 Signer를 제공합니다.
      • provider가 지갑 기능을 제공할 경우 provider.getSigner()로 Signer 객체를 얻을 수 있습니다.

 

Connecting to Ethereum: RPC
  • const provider = new ethers.providers.JsonRpcProvider();

 

Querying the Blockchain

provider는 블록체인에 읽기 전용 연결을 만듭니다.

  • 현재 상태를 query하고, 로그를 fetch하고, 배포된 코드를 look up하는 것과 같은 일들을 할 수 있습니다.
Contracts

Contract는 온체인 컨트랙트를 자바스크립 객체처럼 사용할 수 있도록 합니다.

온체인 컨트랙트와 커뮤니케이션 하려면 abi가 필요합니다.

Contract는 메타 클래스와 같은 역할을 합니다. 런타입 시에 abi를 받아 구체적인 Contract 객체를 생성합니다.

온체인 컨트랙트의 일부만 사용한다면 전체 abi가 아닌 일부 abi만을 제공해서 사용하지 않는 메소드들은 안전하게 무시할 수 있습니다. abi 뿐만 아니라 human-readable abi를 사용할 수도 있습니다.

 

  • ganache-cli를 실행하고, 트러플을 사용해서 컨트랙트를 배포하고, ethers에서 이러한 방식으로 컨트랙트 주소를 얻기 위해서는
    • 메타마스크의 Localhost 8545 ChainID 설정과 truffle-config.js의 network_id와 ganache-cli networkId 설정을 같은 값으로 맞춰주어야 합니다.
      • network Id를 1337로 설정한다고 하면
        • 메타마스크를 실행하고 설정 메뉴를 선택하고 네트워크를 선택하고 Localhost 8545를 선택합니다.
        • ChainID를 1337로 설정합니다.
      • truffle-config.js 파일을 열고 networks의 develop의 network_id를 “1337”로 설정합니다.
      • ganache-cli –networkId 1337와 같이 ganache-cli를 실행합니다.
About the Author
(주)뉴테크프라임 대표 김현남입니다. 저에 대해 좀 더 알기를 원하시는 분은 아래 링크를 참조하세요. http://www.umlcert.com/kimhn/

Leave a Reply

*