ethers.js

심우진·2022년 2월 17일

hre <- web3provider

현재 블록 번호 조회
await provider.getBlockNumber()

Querying the Blockchain

계정 잔액 가져오기
balance = await provider.getBalance("ethers.eth")

'0.082826475815887608'
ethers.utils.formatEther(balance)

"1000000000000000000"
ethers.utils.parseEther("1.0")

Read

ERC-20 토큰 이름
가져오기 await daiContract.name() // Dai Stablecoin

심볼
await daiContract.symbol() //'DAI'

잔액 밸런스가져오기 = await daiContract.balanceOf("ricmoo.firefly.eth")

사용자에게 표시할 잔액
ethers.utils.formatUnits(balance, 18)

State Changing

const daiWithSigner = contract.connect(signer);

const dai = ethers.utils.parseUnits("1.0", 18); // 1DAI, Decimal

address로 다이를 보냄
tx = daiWithSigner.transfer("address.who", dai);

getGasPrice

가스가격 // { BigNumber: "180336311245" }
gasPrice = awiat provider.getGasPrice()

// '180.336311245'
utils.formatUnits(gasPrice, "gwei")

JsonRpcProvider

JSON_RPC_API = INFURA, ALCHAMY

Provider

Signer

connect(provider) 특정 서명자
getAddress(서명자주소)
isSigner(오브젝트) -> bool

getBalance() -> 잔액
getChainId() -> 지갑이 연결된 체인ID
getGasPrice() -> 현재 가스 가격
getTransactionCount() -> 해당 계정의 트랜잭션 수, nonce

Contract

Contract는 EVM 바이트 코드의 추상화.
호출과 트랜잭션을 온체인 계약으로 직렬화, 역직렬화 허용

ContractFactory는 계약의 바이트코드를 추상화한 것으로 배포를 용이하게 함

0개의 댓글