Skip to content

Commit a0ec2ad

Browse files
committed
⏲️ perf: get block balances with multicall
#130
1 parent dba15ce commit a0ec2ad

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

data-sources/citizens/generate-3-voting-escrow-datasets.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { MulticallWrapper } from 'ethers-multicall-provider'
77
const EthDater = require('ethereum-block-by-date')
88

99
const ethersProvider = MulticallWrapper.wrap(new ethers.JsonRpcProvider(
10-
'https://ethereum.publicnode.com'
10+
'https://lb.nodies.app/v1/5e9daed367d1454fab7c75f0ec8aceff'
1111
))
1212
console.info('ethersProvider:', ethersProvider)
1313

@@ -49,6 +49,9 @@ async function loadVotingEscrowData() {
4949
})
5050
const csvRows = []
5151

52+
const votingEscrowAtBlockArray = await getVotingEscrowAtBlockArray(blockPerWeekArray, ethAddress)
53+
// console.debug('votingEscrowAtBlockArray:', votingEscrowAtBlockArray)
54+
5255
// Iterate every week from the week of [Sun May-29-2022 → Sun Jun-05-2022] until now
5356
const weekEndDate: Date = new Date('2022-06-05T00:00:00Z')
5457
console.info('weekEndDate:', weekEndDate)
@@ -65,7 +68,7 @@ async function loadVotingEscrowData() {
6568
// console.debug('blockByDate:', blockByDate)
6669

6770
// Get Citizen's voting escrow at the current block
68-
const votingEscrowWei: number = await getVotingEscrowAtBlock(ethAddress, blockByDate.block)
71+
const votingEscrowWei: number = votingEscrowAtBlockArray[weekNumber - 1]
6972
console.info('votingEscrowWei:', votingEscrowWei)
7073
const votingEscrowEther: string = ethers.formatUnits(votingEscrowWei)
7174
console.info('votingEscrowEther:', votingEscrowEther)
@@ -108,7 +111,20 @@ async function getBlockPerWeekArray() {
108111
return Promise.all(calls)
109112
}
110113

111-
async function getVotingEscrowAtBlock(ethAddress: string, blockNumber: number): Promise<number> {
112-
console.info('getVotingEscrowAtBlock')
113-
return await votingEscrowContract.balanceOfAt(ethAddress, blockNumber)
114+
/**
115+
* Multicall for getting the voting-escrow balance for the block
116+
* each week's block number.
117+
*/
118+
async function getVotingEscrowAtBlockArray(blockPerWeekArray: any, ethAddress: string) {
119+
console.info('getVotingEscrowAtBlockArray', ethAddress)
120+
121+
// Populate an Array with one call per block number
122+
const calls: any = []
123+
blockPerWeekArray.forEach((element: any) => {
124+
// console.debug('element:', element)
125+
calls.push(votingEscrowContract.balanceOfAt(ethAddress, element.block))
126+
})
127+
// console.debug('calls.length:', calls.length)
128+
129+
return Promise.all(calls)
114130
}

0 commit comments

Comments
 (0)