Skip to content

Instantly share code, notes, and snippets.

@HoneyIsMoney
Created September 11, 2021 19:18
Show Gist options
  • Save HoneyIsMoney/20a3d1c6240422539ddb84e22e1c50aa to your computer and use it in GitHub Desktop.
Save HoneyIsMoney/20a3d1c6240422539ddb84e22e1c50aa to your computer and use it in GitHub Desktop.
const {ethers} = require('ethers')
const BN = ethers.BigNumber
const alvinAddress = '0x50DBde932A94b0c23D27cdd30Fbc6B987610c831'
const fromBlock = 14834211
const toBlock = 18032324
const ZERO_ADDRESS = '0x' + '0'.repeat(40)
const rpc = 'https://rpc.xdaichain.com/'
const run = async ( ) => {
const provider = ethers.getDefaultProvider(rpc)
const Alvin = new ethers.Contract(
alvinAddress,
['event Transfer(address indexed from, address indexed to, uint256 value)'],
provider
)
const snapshot = {}
snapshot[ZERO_ADDRESS] = BN.from(0)
const transferFilter = Alvin.filters.Transfer()
// 1. get events
const events = await Alvin.queryFilter(transferFilter, fromBlock, toBlock)
// 2. for event in events
for (e in events) {
// 3. get values from event
const [from, to, amount] = events[e].args
// 4. snapshot[from] -= amount
snapshot[from] = snapshot[from].sub(amount)
// 5. check if the 'to' address has a balance, if not initialise
snapshot[to] === undefined ? snapshot[to] = BN.from(0) : null
// 6. snapshot[to] = vale
snapshot[to] = snapshot[to].add(amount)
}
console.log(snapshot)
}
run()
.then(() => process.exit(0))
.catch((e) => {
console.log(e)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment