Skip to content

Commit

Permalink
Add ethers example with events (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor authored Dec 3, 2021
1 parent 462daff commit 2f19fd2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/ethers-v5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { Dai__factory } from '../types/ethers-contracts/factories/Dai__factory'

const RPC_HOST = 'https://mainnet.infura.io/v3/6d6c70e65c77429482df5b64a4d0c943'
const DAI_ADDRESS = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const BLOCK_NUMBER = 13730326

async function main() {
const provider = new ethers.providers.JsonRpcProvider(RPC_HOST)
const dai = Dai__factory.connect(DAI_ADDRESS, provider)
const balance = await dai.balanceOf('0x70b144972C5Ef6CB941A5379240B74239c418CD4')

console.log(`Our DAI balance is: ${utils.formatEther(balance)}`)

console.log(`Listing Transfer events for block ${BLOCK_NUMBER}`)
const eventsFilter = dai.filters.Transfer()
const events = await dai.queryFilter(eventsFilter, BLOCK_NUMBER, BLOCK_NUMBER)

for (const event of events) {
console.log(`${event.args.src} -> ${event.args.dst} | ${utils.formatEther(event.args.wad)} DAI`)
}
}

main().catch((e) => {
Expand Down

0 comments on commit 2f19fd2

Please sign in to comment.