ethers.js

kyuu·2023년 7월 5일

Contract

Meta-Class

메타 클래스는, 프로퍼티가 런타임에 결정되는 클래스임.Contract 객체는 주어진 abi를 통해 어떤 메소드가 사용 가능한지 파악할 수 있음. 따라서 런타임에 추가된 프로퍼티와 상호 작용하는 일반적인 방법을 알아보자.

Read-only Methods

계약에 작성된 pure or view 함수들은, read-only 함수로, 블록체인의 상태를 변경하지 않기 때문에 read-only 메소드라 할 수 있음.

Write Methods (non-constant)

블록체인의 상태를 변경하는 메소드의 경우, transaction이 필요함. 따라서 miner에게 줄 약간의 지불 금액과, signed 데이터가 필요함.

result를 return할 수 없음. 필요하면 logging을 통해 EVM에 query하는 과정을 통하여 블록체인의 상태 변화를 확인해야 함.(transaction receipt를 받아 확인할 수 있음)

입력 인자로는 함수에 필요한 데이터, [, overrides]객체가 필요한데, overides 객체는 아래와 같음.

  • overrides.gasPrice : the price to pay per gas
  • overrides.gasLimit: 트랜젝션이 소비할 수 있는 최대 한도 가스량
  • overrides.value : 함수 호출과 함께 보낼 ether(in wei)의 양
  • overrides.nonce : Signer가 사용할 nonce

Promise<TransactionResponse>를 반환하며, 이를 위하여 signer가 필요함. TransactionResponse부터 알아보자.

transactionResponse는 Transaction이 가지는 모든 property를 갖는다.(inherited)
채굴되게 되면, 여러 정보들을 확인할 수 있음.

  • Transaction.blockNumber/blockHash/timestamp/confirmations/raw
  • transaction.wait => TransactionReceipt 프로미스 반환, confirm block부터 chain에 포함.
  • TransactionReceipt.to/from/contractAddress/transactionIndex/type/root/gasUsed/.../status 등의 정보 확인 가능. status는 1일 경우 성공, 0일 경우 reverted.

컨트랙트 함수 호출 이후, wait()로 받은 receipt에는 아래 정보가 추가됨.

  • receipt.events : log의 array
  • receipt.events[n].args : parsed arguments of events
  • receipt.events[n].decode : log topic 및 data를 parse하기 위한 메소드
  • receipt.events[n].event : name of the event.
profile
!..!

0개의 댓글