Skip to content

Instantly share code, notes, and snippets.

@snowkidind
Created March 12, 2023 13:28
Show Gist options
  • Save snowkidind/b4d67662a7953d585f14209adfabb08d to your computer and use it in GitHub Desktop.
Save snowkidind/b4d67662a7953d585f14209adfabb08d to your computer and use it in GitHub Desktop.
Working on keeping eth balances via erigon trace api
// NOTICE NOTHING SPECIAL NOR PRETTY JUST EXAMPLE HACKING AT PRESENT
const env = require('node-env-file')
env(__dirname + '/.env')
const ethers = require('ethers')
const { multiEth, decimals } = require('./utils')
const { d } = decimals
const provider = multiEth.getProvider('mainnet')
const stateDiffs = async (block) => {
const req = await provider.getBlock(block)
const miner = req.miner.toLowerCase()
const req2 = await provider.send('trace_replayBlockTransactions', [block, ["stateDiff"]])
req2.forEach((r, index) => {
console.log(r.transactionHash)
for (let dd in r.stateDiff) {
// +, when we have a new entry in the state DB
// * when existing data have changed in the state DB, providing the from (old) and the to (new) values
// -, when we have a removal from the state DB
// =, when the data remained the same.
if (typeof r.stateDiff[dd].balance === 'object') {
let txFee = ''
if (dd === miner) {
txFee = 'fee'
}
// + A new Entry in the state DB
if (r.stateDiff[dd].balance['+']) {
const f = ethers.BigNumber.from('0')
const t = ethers.BigNumber.from(r.stateDiff[dd].balance['+'])
const diff = d(t.sub(f).toString(), 18)
const from = d(f.toString(), 18)
const to = d(t.toString(), 18)
console.log(dd + ' before: ' + from + ' after: ' + to + ' diff: ' + diff.toString() + ' ' + txFee)
}
// * existing data has changed in the state DB
if (r.stateDiff[dd].balance['*']) {
const f = ethers.BigNumber.from(r.stateDiff[dd].balance['*'].from)
const t = ethers.BigNumber.from(r.stateDiff[dd].balance['*'].to)
const diff = d(t.sub(f).toString(), 18)
const from = d(f.toString(), 18)
const to = d(t.toString(), 18)
console.log(dd + ' before: ' + from + ' after: ' + to + ' diff: ' + diff.toString())
}
// Minuses mean the address no longer takes EVM space (or something similar)
if (r.stateDiff[dd].balance['-']) {
// in this case need to zero existing balance of address
const amount = ethers.BigNumber.from(r.stateDiff[dd].balance['-'])
console.log(dd + ' before: ' + amount.toString() + ' after: 0 diff: ' + amount.mul(ethers.BigNumber.from('-1')).toString())
}
// Pretty sure this is nonexistant
if (r.stateDiff[dd].balance['=']) {
console.log(r.transactionHash)
console.log('NoChange', r.stateDiff[dd].balance['='])
}
}
}
console.log()
})
}
; (async () => {
try {
for (let i = 10000109; i < 10000110; i++) {
console.log('Block: ' + i)
await stateDiffs(i)
}
} catch (error) {
console.log(error)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment