From 46feca07e27596ee00c2be62db8985a566e3ab7f Mon Sep 17 00:00:00 2001 From: Aahna Ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:16:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AE=20feat:=20calculate=20total=5Frevo?= =?UTF-8?q?ked=5Fpassports=20(#125)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #125 --- .../citizens/generate-4-citizen-count-csv.ts | 99 ++++++++-- .../output/citizen-count-per-week.csv | 180 +++++++++--------- 2 files changed, 173 insertions(+), 106 deletions(-) diff --git a/data-sources/citizens/generate-4-citizen-count-csv.ts b/data-sources/citizens/generate-4-citizen-count-csv.ts index 92c8e645e..b460edd0b 100644 --- a/data-sources/citizens/generate-4-citizen-count-csv.ts +++ b/data-sources/citizens/generate-4-citizen-count-csv.ts @@ -7,7 +7,7 @@ const Papa = require('papaparse') const ethersProvider = new ethers.JsonRpcProvider( 'https://ethereum.publicnode.com' ) -console.info('ethersProvider:', ethersProvider) +console.debug('ethersProvider:', ethersProvider) const passportContract = new ethers.Contract( '0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333', @@ -23,50 +23,58 @@ loadPassportMintsByWeek() async function loadPassportMintsByWeek() { console.info('loadPassportMintsByWeek') + const revocations = await fetchRevocations() + console.debug('revocations:', revocations) + const writer = csvWriter.createObjectCsvWriter({ path: 'output/citizen-count-per-week.csv', header: [ { id: 'week_end', title: 'week_end' }, { id: 'total_citizens', title: 'total_citizens' }, { id: 'new_citizens', title: 'new_citizens' }, - { id: 'total_expired_passports', title: 'total_expired_passports' } + { id: 'total_expired_passports', title: 'total_expired_passports' }, + { id: 'total_revoked_passports', title: 'total_revoked_passports' } ] }) let csvRows = [] const nextId: number = await getNextId() - console.info('nextId:', nextId) + console.debug('nextId:', nextId) let id: number = 0 // 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) + console.debug('weekEndDate:', weekEndDate) const nowDate: Date = new Date() - console.info('nowDate:', nowDate) + console.debug('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()}]`) + console.debug('week:', `[${weekBeginDate.toISOString()} → ${weekEndDate.toISOString()}]`) let newCitizensCount: number = 0 while ((id < nextId) && (await getTimestamp(id) < (weekEndDate.getTime() / 1000))) { - console.info('id:', id) + console.debug('id:', id) newCitizensCount++ - console.info('newCitizensCount:', newCitizensCount) + console.debug('newCitizensCount:', newCitizensCount) id++ } const totalExpiredPassports: number = getTotalExpiredPassports(weekEndDate, id) - console.info('totalExpiredPassports:', totalExpiredPassports) + console.debug('totalExpiredPassports:', totalExpiredPassports) + + const totalRevokedPassports: number = getTotalRevokedPassports(weekEndDate, id, revocations) + console.debug('totalRevokedPassports:', totalRevokedPassports) // Export to CSV const csvRow = { week_end: weekEndDate.toISOString().substring(0, 10), total_citizens: id, new_citizens: newCitizensCount, - total_expired_passports: totalExpiredPassports + total_expired_passports: totalExpiredPassports, + total_revoked_passports: totalRevokedPassports } csvRows.push(csvRow) @@ -92,6 +100,9 @@ async function getTimestamp(id: number): Promise { } } +/** + * Calculates the total number of expired passports up until `weekEndDate` + */ function getTotalExpiredPassports(weekEndDate: Date, maxPassportID: number): number { console.info('getTotalExpiredPassports') @@ -99,26 +110,26 @@ function getTotalExpiredPassports(weekEndDate: Date, maxPassportID: number): num let totalExpiredPassports = 0 for (let passportID = 0; passportID < maxPassportID; passportID++) { - console.info(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`) + // console.debug(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`) // Fetch voting escrow data from the citizen's data CSV const citizenFilePath: string = `output/citizen-${passportID}.csv` - console.info('Fetching citizen data:', citizenFilePath) + // console.debug('Fetching citizen data:', citizenFilePath) const file: File = fs.readFileSync(citizenFilePath) const csvData = file.toString() - // console.info('csvData:\n', csvData) + // console.debug('csvData:\n', csvData) Papa.parse(csvData, { header: true, skipEmptyLines: true, dynamicTyping: true, complete: (result: any) => { - // console.info('result:', result) + // console.debug('result:', result) result.data.forEach((row: any, i: number) => { if (row.week_end == weekEndDateString) { - console.info(`row.week_end ${row.week_end}, row.voting_escrow: ${row.voting_escrow}`) + // console.debug(`row.week_end ${row.week_end}, row.voting_escrow: ${row.voting_escrow}`) if (row.voting_escrow < 1.5) { // https://etherscan.io/address/0x279c0b6bfcbba977eaf4ad1b2ffe3c208aa068ac#readContract#F9 - console.info('Passport ID expired:', passportID) + // console.debug('Passport ID expired:', passportID) totalExpiredPassports++ } } @@ -130,4 +141,60 @@ function getTotalExpiredPassports(weekEndDate: Date, maxPassportID: number): num return totalExpiredPassports } +/** + * Calculates the total number of revoked passports up until `weekEndDate` + */ +function getTotalRevokedPassports(weekEndDate: Date, maxPassportID: number, revocations: any): number { + console.info('getTotalRevokedPassports') + + const weekEndDateString: string = weekEndDate.toISOString().substring(0, 10) + + let totalRevokedPassports = 0 + for (let passportID = 0; passportID < maxPassportID; passportID++) { + // console.debug(`weekEndDate: ${weekEndDateString}, passportID: ${passportID}`) + + revocations.forEach((row: any, i: number) => { + // console.debug('row:', row) + const blockTimestamp: number = Number(row.blockTimestamp) + const revocationDate: Date = new Date(blockTimestamp * 1_000) + // console.debug('revocationDate:', revocationDate) + if (revocationDate.getTime() <= weekEndDate.getTime()) { + const tokenID: number = Number(row._tokenId) + // console.debug('tokenID:', tokenID) + if (tokenID == passportID) { + console.debug('Passport ID revoked:', passportID) + totalRevokedPassports++ + } + } + }) + } + + return totalRevokedPassports +} + +async function fetchRevocations() { + console.info('fetchRevocations') + + // https://github.com/nation3/subgraphs/blob/main/passportissuance/schema.graphql + const GRAPHQL_URL: string = 'https://api.thegraph.com/subgraphs/name/nation3/passportissuance' + const response = await fetch(GRAPHQL_URL, { + method: 'POST', + body: JSON.stringify({ + query: ` + { + revokes(first: 420) { + _tokenId + blockTimestamp + } + } + ` + }) + }) + + const { data } = await response.json() + console.debug('data:', data) + + return data.revokes +} + export {} diff --git a/data-sources/citizens/output/citizen-count-per-week.csv b/data-sources/citizens/output/citizen-count-per-week.csv index 3c9bd248b..2ee6faafc 100644 --- a/data-sources/citizens/output/citizen-count-per-week.csv +++ b/data-sources/citizens/output/citizen-count-per-week.csv @@ -1,90 +1,90 @@ -week_end,total_citizens,new_citizens,total_expired_passports -2022-06-05,155,155,5 -2022-06-12,157,2,5 -2022-06-19,160,3,5 -2022-06-26,163,3,5 -2022-07-03,167,4,5 -2022-07-10,167,0,5 -2022-07-17,167,0,5 -2022-07-24,170,3,5 -2022-07-31,172,2,5 -2022-08-07,173,1,5 -2022-08-14,174,1,5 -2022-08-21,175,1,5 -2022-08-28,175,0,5 -2022-09-04,176,1,5 -2022-09-11,177,1,5 -2022-09-18,178,1,5 -2022-09-25,178,0,5 -2022-10-02,178,0,5 -2022-10-09,181,3,5 -2022-10-16,182,1,5 -2022-10-23,184,2,5 -2022-10-30,184,0,5 -2022-11-06,188,4,5 -2022-11-13,221,33,6 -2022-11-20,231,10,6 -2022-11-27,233,2,6 -2022-12-04,235,2,6 -2022-12-11,240,5,6 -2022-12-18,241,1,6 -2022-12-25,241,0,6 -2023-01-01,241,0,6 -2023-01-08,244,3,6 -2023-01-15,248,4,6 -2023-01-22,250,2,6 -2023-01-29,254,4,7 -2023-02-05,257,3,7 -2023-02-12,258,1,7 -2023-02-19,258,0,7 -2023-02-26,259,1,7 -2023-03-05,260,1,7 -2023-03-12,260,0,7 -2023-03-19,261,1,7 -2023-03-26,262,1,7 -2023-04-02,263,1,7 -2023-04-09,263,0,8 -2023-04-16,264,1,9 -2023-04-23,264,0,10 -2023-04-30,265,1,10 -2023-05-07,265,0,11 -2023-05-14,265,0,11 -2023-05-21,265,0,12 -2023-05-28,265,0,12 -2023-06-04,265,0,12 -2023-06-11,265,0,15 -2023-06-18,265,0,29 -2023-06-25,265,0,32 -2023-07-02,266,1,33 -2023-07-09,269,3,33 -2023-07-16,272,3,35 -2023-07-23,273,1,39 -2023-07-30,274,1,49 -2023-08-06,275,1,53 -2023-08-13,275,0,57 -2023-08-20,275,0,58 -2023-08-27,278,3,61 -2023-09-03,279,1,64 -2023-09-10,281,2,69 -2023-09-17,282,1,76 -2023-09-24,283,1,77 -2023-10-01,283,0,78 -2023-10-08,284,1,78 -2023-10-15,284,0,79 -2023-10-22,284,0,80 -2023-10-29,284,0,82 -2023-11-05,285,1,85 -2023-11-12,285,0,87 -2023-11-19,285,0,95 -2023-11-26,285,0,116 -2023-12-03,285,0,125 -2023-12-10,285,0,130 -2023-12-17,285,0,134 -2023-12-24,285,0,138 -2023-12-31,286,1,141 -2024-01-07,287,1,149 -2024-01-14,288,1,152 -2024-01-21,289,1,154 -2024-01-28,289,0,162 -2024-02-04,289,0,165 -2024-02-11,289,0,167 +week_end,total_citizens,new_citizens,total_expired_passports,total_revoked_passports +2022-06-05,155,155,5,0 +2022-06-12,157,2,5,0 +2022-06-19,160,3,5,0 +2022-06-26,163,3,5,0 +2022-07-03,167,4,5,0 +2022-07-10,167,0,5,0 +2022-07-17,167,0,5,0 +2022-07-24,170,3,5,0 +2022-07-31,172,2,5,0 +2022-08-07,173,1,5,0 +2022-08-14,174,1,5,0 +2022-08-21,175,1,5,0 +2022-08-28,175,0,5,0 +2022-09-04,176,1,5,0 +2022-09-11,177,1,5,0 +2022-09-18,178,1,5,0 +2022-09-25,178,0,5,0 +2022-10-02,178,0,5,0 +2022-10-09,181,3,5,0 +2022-10-16,182,1,5,0 +2022-10-23,184,2,5,0 +2022-10-30,184,0,5,0 +2022-11-06,188,4,5,0 +2022-11-13,221,33,6,0 +2022-11-20,231,10,6,0 +2022-11-27,233,2,6,0 +2022-12-04,235,2,6,0 +2022-12-11,240,5,6,0 +2022-12-18,241,1,6,0 +2022-12-25,241,0,6,0 +2023-01-01,241,0,6,0 +2023-01-08,244,3,6,0 +2023-01-15,248,4,6,0 +2023-01-22,250,2,6,0 +2023-01-29,254,4,7,0 +2023-02-05,257,3,7,0 +2023-02-12,258,1,7,0 +2023-02-19,258,0,7,0 +2023-02-26,259,1,7,0 +2023-03-05,260,1,7,0 +2023-03-12,260,0,7,0 +2023-03-19,261,1,7,0 +2023-03-26,262,1,7,0 +2023-04-02,263,1,7,0 +2023-04-09,263,0,8,0 +2023-04-16,264,1,9,0 +2023-04-23,264,0,10,0 +2023-04-30,265,1,10,0 +2023-05-07,265,0,11,0 +2023-05-14,265,0,11,0 +2023-05-21,265,0,12,0 +2023-05-28,265,0,12,0 +2023-06-04,265,0,12,0 +2023-06-11,265,0,15,0 +2023-06-18,265,0,29,0 +2023-06-25,265,0,32,0 +2023-07-02,266,1,33,0 +2023-07-09,269,3,33,0 +2023-07-16,272,3,35,0 +2023-07-23,273,1,39,0 +2023-07-30,274,1,49,0 +2023-08-06,275,1,53,0 +2023-08-13,275,0,57,0 +2023-08-20,275,0,58,0 +2023-08-27,278,3,61,0 +2023-09-03,279,1,64,0 +2023-09-10,281,2,69,0 +2023-09-17,282,1,76,0 +2023-09-24,283,1,77,0 +2023-10-01,283,0,78,0 +2023-10-08,284,1,78,1 +2023-10-15,284,0,79,1 +2023-10-22,284,0,80,1 +2023-10-29,284,0,82,1 +2023-11-05,285,1,85,1 +2023-11-12,285,0,87,1 +2023-11-19,285,0,95,1 +2023-11-26,285,0,116,1 +2023-12-03,285,0,125,1 +2023-12-10,285,0,130,2 +2023-12-17,285,0,134,2 +2023-12-24,285,0,138,3 +2023-12-31,286,1,141,3 +2024-01-07,287,1,149,4 +2024-01-14,288,1,152,5 +2024-01-21,289,1,154,5 +2024-01-28,289,0,162,5 +2024-02-04,289,0,165,5 +2024-02-11,289,0,167,5