Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⏲️ perf: get blocks with multicall #131

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate-datasets-weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# 2. Generate Citizen Data JSON 🧮
- run: npm run generate-2-json

# 3. Generate Historical Citizen Data CSVs 🧮
# 3. Generate Historical Citizen Voting Escrow Data CSVs 🧮
- run: npm run generate-3-voting-escrow

# 4. Generate Citizen Count CSV 🧮
Expand Down
2 changes: 1 addition & 1 deletion data-sources/citizens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm run generate-1-csv
npm run generate-2-json
```

### 3. Generate Historical Citizen Data CSVs
### 3. Generate Historical Citizen Voting Escrow Data CSVs

```
npm run generate-3-voting-escrow
Expand Down
33 changes: 30 additions & 3 deletions data-sources/citizens/generate-3-voting-escrow-datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const createObjectCsvWriter = require('csv-writer').createObjectCsvWriter

const VotingEscrow = require('../abis/VotingEscrow.json')
import { ethers } from 'ethers'
import { MulticallWrapper } from 'ethers-multicall-provider'
const EthDater = require('ethereum-block-by-date')

const ethersProvider = new ethers.JsonRpcProvider(
const ethersProvider = MulticallWrapper.wrap(new ethers.JsonRpcProvider(
'https://ethereum.publicnode.com'
)
))
console.info('ethersProvider:', ethersProvider)

const dater = new EthDater(ethersProvider)
Expand All @@ -24,6 +25,10 @@ loadVotingEscrowData()
async function loadVotingEscrowData() {
console.info('loadVotingEscrowData')

const blockPerWeekArray = await getBlockPerWeekArray()
console.info('blockPerWeekArray.length:', blockPerWeekArray.length)
console.debug('blockPerWeekArray:', blockPerWeekArray)

const citizensJson = require('../output/citizens.json')
for (const passportId in citizensJson) {
console.info('passportId:', passportId)
Expand All @@ -47,14 +52,16 @@ async function loadVotingEscrowData() {
// Iterate every week from the week of [Sun May-29-2022 → Sun Jun-05-2022] until now
const weekEndDate: Date = new Date('2022-06-05T00:00:00Z')
console.info('weekEndDate:', weekEndDate)
let weekNumber: number = 1
const nowDate: Date = new Date()
console.info('nowDate:', nowDate)
while (nowDate.getTime() > weekEndDate.getTime()) {
const weekBeginDate: Date = new Date(weekEndDate.getTime() - 7*24*60*60*1000)
console.info('week:', `[${weekBeginDate.toISOString()} → ${weekEndDate.toISOString()}]`)

// Get Ethereum block by date
const blockByDate = await dater.getDate(weekEndDate)
console.info('weekNumber:', weekNumber)
const blockByDate = blockPerWeekArray[weekNumber - 1]
// console.debug('blockByDate:', blockByDate)

// Get Citizen's voting escrow at the current block
Expand All @@ -75,12 +82,32 @@ async function loadVotingEscrowData() {

// Increase week end date by 7 days
weekEndDate.setDate(weekEndDate.getDate() + 7)
weekNumber++
}

writer.writeRecords(csvRows)
}
}

/**
* Multicall for getting the Ethereum block for all the weeks.
*/
async function getBlockPerWeekArray() {
console.info('getBlockPerWeekArray')

// Populate an Array with one call per week
const calls = []
const weekEndDate: Date = new Date('2022-06-05T00:00:00Z')
const nowDate: Date = new Date()
while (nowDate.getTime() > weekEndDate.getTime()) {
calls.push(dater.getDate(weekEndDate))
weekEndDate.setDate(weekEndDate.getDate() + 7)
}
console.info('calls.length:', calls.length)

return Promise.all(calls)
}

async function getVotingEscrowAtBlock(ethAddress: string, blockNumber: number): Promise<number> {
console.info('getVotingEscrowAtBlock')
return await votingEscrowContract.balanceOfAt(ethAddress, blockNumber)
Expand Down
150 changes: 60 additions & 90 deletions data-sources/citizens/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions data-sources/citizens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"csv-writer": "^1.6.0",
"ethereum-block-by-date": "^1.4.9",
"ethers": "^6.9.1",
"ethers-multicall-provider": "^6.4.0",
"jsonfile": "^6.1.0",
"papaparse": "^5.4.1",
"typescript": "^4.8.4"
Expand Down
Loading