Skip to content

Instantly share code, notes, and snippets.

@daragao
Created March 2, 2021 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daragao/f7a1e0d61ba8445e276c24193034baab to your computer and use it in GitHub Desktop.
Save daragao/f7a1e0d61ba8445e276c24193034baab to your computer and use it in GitHub Desktop.
Simple Web3 script to print events
const Web3 = require('web3')
const BN = require('bn.js')
const Big = require('big.js')
// what is a wad https://makerdao.com/purple/#sec-3-1
const wadToEth = (value) => {
return (new Big(value)).div((new BN(10)).pow(new BN(18)))
}
const printEvent = (web3, eventType) => (event) => {
const receiver = web3.eth.abi.decodeParameter('address', event.topics[1])
const amount = web3.eth.abi.decodeParameters(['uint256'], event.data)[0] // data only has a single element for this event
let eventMsg = `${eventType} Event (${event.id})\n`
eventMsg += `\tBlock: ${event.blockNumber}\n`
eventMsg += `\tTransaction: ${event.transactionHash}\n`
eventMsg += `\tReceiver: ${receiver}\n`
eventMsg += `\tAmount (wad): ${amount} (${wadToEth(amount).toString()} ETH)\n`
console.log(eventMsg)
}
// This application assumes no weird errors will occur
(() => {
try {
const url = 'wss://mainnet.infura.io/ws/v3/' + process.env.INFURA_PROJECT_ID
const web3 = new Web3(url)
const wEthAddress = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' // address of wETH https://github.com/gnosis/canonical-weth
// events found here https://github.com/gnosis/canonical-weth/blob/master/contracts/WETH9.sol
const withdrawalSignature = web3.utils.sha3('Withdrawal(address,uint256)')
const depositSignature = web3.utils.sha3('Deposit(address,uint)')
// subscribe to the withdrawals event
const subscribeOptions = { address: wEthAddress, topics: [withdrawalSignature] }
const withdrawalEvent = web3.eth.subscribe('logs', subscribeOptions)
withdrawalEvent.on('data', printEvent(web3, 'Withdrawal'))
// subscribe to the deposits event
subscribeOptions.topics[0] = depositSignature
const depositEvent = web3.eth.subscribe('logs', subscribeOptions)
depositEvent.on('data', printEvent(web3, 'Deposit'))
} catch (error) {
console.error(error)
}
})()
/*
$ node printEvent.js
web3-shh package will be deprecated in version 1.3.5 and will no longer be supported.
web3-bzz package will be deprecated in version 1.3.5 and will no longer be supported.
Withdrawal Event (log_0bf96e32)
Block: 11961830
Transaction: 0x6e14e35d6cf2dc6ef4e2d1bd90004dc8e5834e3983ed07bf17ec13a76eb15a69
Receiver: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Amount (wad): 1122029110699420581 (1.122029110699420581 ETH)
Withdrawal Event (log_9fdb6db1)
Block: 11961830
Transaction: 0xf35bd5e30a3bc6e8ede7cb8a5c9c18bb1179b0b46df1c738770f5353451a96d2
Receiver: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Amount (wad): 1545745010161161424 (1.545745010161161424 ETH)
Withdrawal Event (log_0280e2c5)
Block: 11961830
Transaction: 0x8bb5991b28cea213ce7379b27afc18172494e2c4aa3bfba5cf6936964374af65
Receiver: 0x74de5d4FCbf63E00296fd95d33236B9794016631
Amount (wad): 468112873550093531781 (468.112873550093531781 ETH)
Withdrawal Event (log_696fa21d)
Block: 11961830
Transaction: 0x0fdbe47107a3aa54dd5eb17320259c22a6f04b28073fa68a0a75a173844cfa01
Receiver: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Amount (wad): 19862978831382806733 (19.862978831382806733 ETH)
Withdrawal Event (log_7b9dc920)
Block: 11961830
Transaction: 0x24482403c85ef4a11ecaa8d4dbee00dbeb1ea0441337bac1652c4dc156eb6b20
Receiver: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Amount (wad): 4770097787130794203 (4.770097787130794203 ETH)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment