Skip to content

Commit e462740

Browse files
authored
Merge pull request #126 from nation3/125-calculate-revoked
Calculate `total_revoked_passports` (#125)
2 parents 16c05df + 46feca0 commit e462740

File tree

2 files changed

+173
-106
lines changed

2 files changed

+173
-106
lines changed

data-sources/citizens/generate-4-citizen-count-csv.ts

+83-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Papa = require('papaparse')
77
const ethersProvider = new ethers.JsonRpcProvider(
88
'https://ethereum.publicnode.com'
99
)
10-
console.info('ethersProvider:', ethersProvider)
10+
console.debug('ethersProvider:', ethersProvider)
1111

1212
const passportContract = new ethers.Contract(
1313
'0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333',
@@ -23,50 +23,58 @@ loadPassportMintsByWeek()
2323
async function loadPassportMintsByWeek() {
2424
console.info('loadPassportMintsByWeek')
2525

26+
const revocations = await fetchRevocations()
27+
console.debug('revocations:', revocations)
28+
2629
const writer = csvWriter.createObjectCsvWriter({
2730
path: 'output/citizen-count-per-week.csv',
2831
header: [
2932
{ id: 'week_end', title: 'week_end' },
3033
{ id: 'total_citizens', title: 'total_citizens' },
3134
{ id: 'new_citizens', title: 'new_citizens' },
32-
{ id: 'total_expired_passports', title: 'total_expired_passports' }
35+
{ id: 'total_expired_passports', title: 'total_expired_passports' },
36+
{ id: 'total_revoked_passports', title: 'total_revoked_passports' }
3337
]
3438
})
3539
let csvRows = []
3640

3741
const nextId: number = await getNextId()
38-
console.info('nextId:', nextId)
42+
console.debug('nextId:', nextId)
3943

4044
let id: number = 0
4145

4246
// Iterate every week from the week of [Sun May-29-2022 → Sun Jun-05-2022] until now
4347
const weekEndDate: Date = new Date('2022-06-05T00:00:00Z')
44-
console.info('weekEndDate:', weekEndDate)
48+
console.debug('weekEndDate:', weekEndDate)
4549
const nowDate: Date = new Date()
46-
console.info('nowDate:', nowDate)
50+
console.debug('nowDate:', nowDate)
4751
while (nowDate.getTime() > weekEndDate.getTime()) {
4852
const weekBeginDate: Date = new Date(weekEndDate.getTime() - 7*24*60*60*1000)
49-
console.info('week:', `[${weekBeginDate.toISOString()}${weekEndDate.toISOString()}]`)
53+
console.debug('week:', `[${weekBeginDate.toISOString()}${weekEndDate.toISOString()}]`)
5054

5155
let newCitizensCount: number = 0
5256
while ((id < nextId) && (await getTimestamp(id) < (weekEndDate.getTime() / 1000))) {
53-
console.info('id:', id)
57+
console.debug('id:', id)
5458

5559
newCitizensCount++
56-
console.info('newCitizensCount:', newCitizensCount)
60+
console.debug('newCitizensCount:', newCitizensCount)
5761

5862
id++
5963
}
6064

6165
const totalExpiredPassports: number = getTotalExpiredPassports(weekEndDate, id)
62-
console.info('totalExpiredPassports:', totalExpiredPassports)
66+
console.debug('totalExpiredPassports:', totalExpiredPassports)
67+
68+
const totalRevokedPassports: number = getTotalRevokedPassports(weekEndDate, id, revocations)
69+
console.debug('totalRevokedPassports:', totalRevokedPassports)
6370

6471
// Export to CSV
6572
const csvRow = {
6673
week_end: weekEndDate.toISOString().substring(0, 10),
6774
total_citizens: id,
6875
new_citizens: newCitizensCount,
69-
total_expired_passports: totalExpiredPassports
76+
total_expired_passports: totalExpiredPassports,
77+
total_revoked_passports: totalRevokedPassports
7078
}
7179
csvRows.push(csvRow)
7280

@@ -92,33 +100,36 @@ async function getTimestamp(id: number): Promise<number> {
92100
}
93101
}
94102

103+
/**
104+
* Calculates the total number of expired passports up until `weekEndDate`
105+
*/
95106
function getTotalExpiredPassports(weekEndDate: Date, maxPassportID: number): number {
96107
console.info('getTotalExpiredPassports')
97108

98109
const weekEndDateString: string = weekEndDate.toISOString().substring(0, 10)
99110

100111
let totalExpiredPassports = 0
101112
for (let passportID = 0; passportID < maxPassportID; passportID++) {
102-
console.info(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`)
113+
// console.debug(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`)
103114

104115
// Fetch voting escrow data from the citizen's data CSV
105116
const citizenFilePath: string = `output/citizen-${passportID}.csv`
106-
console.info('Fetching citizen data:', citizenFilePath)
117+
// console.debug('Fetching citizen data:', citizenFilePath)
107118
const file: File = fs.readFileSync(citizenFilePath)
108119
const csvData = file.toString()
109-
// console.info('csvData:\n', csvData)
120+
// console.debug('csvData:\n', csvData)
110121
Papa.parse(csvData, {
111122
header: true,
112123
skipEmptyLines: true,
113124
dynamicTyping: true,
114125
complete: (result: any) => {
115-
// console.info('result:', result)
126+
// console.debug('result:', result)
116127
result.data.forEach((row: any, i: number) => {
117128
if (row.week_end == weekEndDateString) {
118-
console.info(`row.week_end ${row.week_end}, row.voting_escrow: ${row.voting_escrow}`)
129+
// console.debug(`row.week_end ${row.week_end}, row.voting_escrow: ${row.voting_escrow}`)
119130
if (row.voting_escrow < 1.5) {
120131
// https://etherscan.io/address/0x279c0b6bfcbba977eaf4ad1b2ffe3c208aa068ac#readContract#F9
121-
console.info('Passport ID expired:', passportID)
132+
// console.debug('Passport ID expired:', passportID)
122133
totalExpiredPassports++
123134
}
124135
}
@@ -130,4 +141,60 @@ function getTotalExpiredPassports(weekEndDate: Date, maxPassportID: number): num
130141
return totalExpiredPassports
131142
}
132143

144+
/**
145+
* Calculates the total number of revoked passports up until `weekEndDate`
146+
*/
147+
function getTotalRevokedPassports(weekEndDate: Date, maxPassportID: number, revocations: any): number {
148+
console.info('getTotalRevokedPassports')
149+
150+
const weekEndDateString: string = weekEndDate.toISOString().substring(0, 10)
151+
152+
let totalRevokedPassports = 0
153+
for (let passportID = 0; passportID < maxPassportID; passportID++) {
154+
// console.debug(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`)
155+
156+
revocations.forEach((row: any, i: number) => {
157+
// console.debug('row:', row)
158+
const blockTimestamp: number = Number(row.blockTimestamp)
159+
const revocationDate: Date = new Date(blockTimestamp * 1_000)
160+
// console.debug('revocationDate:', revocationDate)
161+
if (revocationDate.getTime() <= weekEndDate.getTime()) {
162+
const tokenID: number = Number(row._tokenId)
163+
// console.debug('tokenID:', tokenID)
164+
if (tokenID == passportID) {
165+
console.debug('Passport ID revoked:', passportID)
166+
totalRevokedPassports++
167+
}
168+
}
169+
})
170+
}
171+
172+
return totalRevokedPassports
173+
}
174+
175+
async function fetchRevocations() {
176+
console.info('fetchRevocations')
177+
178+
// https://github.com/nation3/subgraphs/blob/main/passportissuance/schema.graphql
179+
const GRAPHQL_URL: string = 'https://api.thegraph.com/subgraphs/name/nation3/passportissuance'
180+
const response = await fetch(GRAPHQL_URL, {
181+
method: 'POST',
182+
body: JSON.stringify({
183+
query: `
184+
{
185+
revokes(first: 420) {
186+
_tokenId
187+
blockTimestamp
188+
}
189+
}
190+
`
191+
})
192+
})
193+
194+
const { data } = await response.json()
195+
console.debug('data:', data)
196+
197+
return data.revokes
198+
}
199+
133200
export {}
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
1-
week_end,total_citizens,new_citizens,total_expired_passports
2-
2022-06-05,155,155,5
3-
2022-06-12,157,2,5
4-
2022-06-19,160,3,5
5-
2022-06-26,163,3,5
6-
2022-07-03,167,4,5
7-
2022-07-10,167,0,5
8-
2022-07-17,167,0,5
9-
2022-07-24,170,3,5
10-
2022-07-31,172,2,5
11-
2022-08-07,173,1,5
12-
2022-08-14,174,1,5
13-
2022-08-21,175,1,5
14-
2022-08-28,175,0,5
15-
2022-09-04,176,1,5
16-
2022-09-11,177,1,5
17-
2022-09-18,178,1,5
18-
2022-09-25,178,0,5
19-
2022-10-02,178,0,5
20-
2022-10-09,181,3,5
21-
2022-10-16,182,1,5
22-
2022-10-23,184,2,5
23-
2022-10-30,184,0,5
24-
2022-11-06,188,4,5
25-
2022-11-13,221,33,6
26-
2022-11-20,231,10,6
27-
2022-11-27,233,2,6
28-
2022-12-04,235,2,6
29-
2022-12-11,240,5,6
30-
2022-12-18,241,1,6
31-
2022-12-25,241,0,6
32-
2023-01-01,241,0,6
33-
2023-01-08,244,3,6
34-
2023-01-15,248,4,6
35-
2023-01-22,250,2,6
36-
2023-01-29,254,4,7
37-
2023-02-05,257,3,7
38-
2023-02-12,258,1,7
39-
2023-02-19,258,0,7
40-
2023-02-26,259,1,7
41-
2023-03-05,260,1,7
42-
2023-03-12,260,0,7
43-
2023-03-19,261,1,7
44-
2023-03-26,262,1,7
45-
2023-04-02,263,1,7
46-
2023-04-09,263,0,8
47-
2023-04-16,264,1,9
48-
2023-04-23,264,0,10
49-
2023-04-30,265,1,10
50-
2023-05-07,265,0,11
51-
2023-05-14,265,0,11
52-
2023-05-21,265,0,12
53-
2023-05-28,265,0,12
54-
2023-06-04,265,0,12
55-
2023-06-11,265,0,15
56-
2023-06-18,265,0,29
57-
2023-06-25,265,0,32
58-
2023-07-02,266,1,33
59-
2023-07-09,269,3,33
60-
2023-07-16,272,3,35
61-
2023-07-23,273,1,39
62-
2023-07-30,274,1,49
63-
2023-08-06,275,1,53
64-
2023-08-13,275,0,57
65-
2023-08-20,275,0,58
66-
2023-08-27,278,3,61
67-
2023-09-03,279,1,64
68-
2023-09-10,281,2,69
69-
2023-09-17,282,1,76
70-
2023-09-24,283,1,77
71-
2023-10-01,283,0,78
72-
2023-10-08,284,1,78
73-
2023-10-15,284,0,79
74-
2023-10-22,284,0,80
75-
2023-10-29,284,0,82
76-
2023-11-05,285,1,85
77-
2023-11-12,285,0,87
78-
2023-11-19,285,0,95
79-
2023-11-26,285,0,116
80-
2023-12-03,285,0,125
81-
2023-12-10,285,0,130
82-
2023-12-17,285,0,134
83-
2023-12-24,285,0,138
84-
2023-12-31,286,1,141
85-
2024-01-07,287,1,149
86-
2024-01-14,288,1,152
87-
2024-01-21,289,1,154
88-
2024-01-28,289,0,162
89-
2024-02-04,289,0,165
90-
2024-02-11,289,0,167
1+
week_end,total_citizens,new_citizens,total_expired_passports,total_revoked_passports
2+
2022-06-05,155,155,5,0
3+
2022-06-12,157,2,5,0
4+
2022-06-19,160,3,5,0
5+
2022-06-26,163,3,5,0
6+
2022-07-03,167,4,5,0
7+
2022-07-10,167,0,5,0
8+
2022-07-17,167,0,5,0
9+
2022-07-24,170,3,5,0
10+
2022-07-31,172,2,5,0
11+
2022-08-07,173,1,5,0
12+
2022-08-14,174,1,5,0
13+
2022-08-21,175,1,5,0
14+
2022-08-28,175,0,5,0
15+
2022-09-04,176,1,5,0
16+
2022-09-11,177,1,5,0
17+
2022-09-18,178,1,5,0
18+
2022-09-25,178,0,5,0
19+
2022-10-02,178,0,5,0
20+
2022-10-09,181,3,5,0
21+
2022-10-16,182,1,5,0
22+
2022-10-23,184,2,5,0
23+
2022-10-30,184,0,5,0
24+
2022-11-06,188,4,5,0
25+
2022-11-13,221,33,6,0
26+
2022-11-20,231,10,6,0
27+
2022-11-27,233,2,6,0
28+
2022-12-04,235,2,6,0
29+
2022-12-11,240,5,6,0
30+
2022-12-18,241,1,6,0
31+
2022-12-25,241,0,6,0
32+
2023-01-01,241,0,6,0
33+
2023-01-08,244,3,6,0
34+
2023-01-15,248,4,6,0
35+
2023-01-22,250,2,6,0
36+
2023-01-29,254,4,7,0
37+
2023-02-05,257,3,7,0
38+
2023-02-12,258,1,7,0
39+
2023-02-19,258,0,7,0
40+
2023-02-26,259,1,7,0
41+
2023-03-05,260,1,7,0
42+
2023-03-12,260,0,7,0
43+
2023-03-19,261,1,7,0
44+
2023-03-26,262,1,7,0
45+
2023-04-02,263,1,7,0
46+
2023-04-09,263,0,8,0
47+
2023-04-16,264,1,9,0
48+
2023-04-23,264,0,10,0
49+
2023-04-30,265,1,10,0
50+
2023-05-07,265,0,11,0
51+
2023-05-14,265,0,11,0
52+
2023-05-21,265,0,12,0
53+
2023-05-28,265,0,12,0
54+
2023-06-04,265,0,12,0
55+
2023-06-11,265,0,15,0
56+
2023-06-18,265,0,29,0
57+
2023-06-25,265,0,32,0
58+
2023-07-02,266,1,33,0
59+
2023-07-09,269,3,33,0
60+
2023-07-16,272,3,35,0
61+
2023-07-23,273,1,39,0
62+
2023-07-30,274,1,49,0
63+
2023-08-06,275,1,53,0
64+
2023-08-13,275,0,57,0
65+
2023-08-20,275,0,58,0
66+
2023-08-27,278,3,61,0
67+
2023-09-03,279,1,64,0
68+
2023-09-10,281,2,69,0
69+
2023-09-17,282,1,76,0
70+
2023-09-24,283,1,77,0
71+
2023-10-01,283,0,78,0
72+
2023-10-08,284,1,78,1
73+
2023-10-15,284,0,79,1
74+
2023-10-22,284,0,80,1
75+
2023-10-29,284,0,82,1
76+
2023-11-05,285,1,85,1
77+
2023-11-12,285,0,87,1
78+
2023-11-19,285,0,95,1
79+
2023-11-26,285,0,116,1
80+
2023-12-03,285,0,125,1
81+
2023-12-10,285,0,130,2
82+
2023-12-17,285,0,134,2
83+
2023-12-24,285,0,138,3
84+
2023-12-31,286,1,141,3
85+
2024-01-07,287,1,149,4
86+
2024-01-14,288,1,152,5
87+
2024-01-21,289,1,154,5
88+
2024-01-28,289,0,162,5
89+
2024-02-04,289,0,165,5
90+
2024-02-11,289,0,167,5

0 commit comments

Comments
 (0)