Created
October 28, 2020 07:00
-
-
Save ncitron/4880d24902d59c4f69888c59066ae501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Web3 = require('web3'); | |
let uniABI = require('./uniABI.json').abi; | |
const main = async () => { | |
let web3 = new Web3('https://mainnet.infura.io/v3/b6c1c2a638ef45098692c3557068e65d'); | |
const uniAddress = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"; | |
let uni = new web3.eth.Contract(uniABI, uniAddress); | |
let delegations = []; | |
let startingBlock = 10_750_000; | |
for(let i = startingBlock; i < await web3.eth.getBlockNumber()+10_000; i+=10_000) { | |
console.log('indexing delegates'); | |
console.log(i); | |
try { | |
delegations.push(...await uni.getPastEvents('DelegateVotesChanged', { | |
fromBlock: (i-10_000), | |
toBlock: i | |
})); | |
} catch { | |
delegations.push(...await uni.getPastEvents('DelegateVotesChanged', { | |
fromBlock: (i-10_000), | |
toBlock: i | |
})); | |
} | |
} | |
const delegateAccounts = {}; | |
delegations.forEach(e => { | |
const { delegate, newBalance } = e.returnValues; | |
delegateAccounts[delegate] = newBalance; | |
}); | |
let delegates = []; | |
Object.keys(delegateAccounts).forEach((account) => { | |
const voteWeight = +delegateAccounts[account]; | |
if (voteWeight === 0) return; | |
delegates.push({ | |
delegate: account, | |
votes: voteWeight / 1e18 | |
}); | |
}); | |
delegates = delegates.sort((a, b) => { | |
return a.votes < b.votes ? 1 : -1; | |
}); | |
delegates = delegates.slice(0, 100); | |
delegates.forEach(delegate => { | |
console.log(`"${delegate.delegate}",`); | |
}) | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment