diff --git a/indexer/src/entity/whitelist.ts b/indexer/src/entity/whitelist.ts index 1a1d1da..8425ee4 100644 --- a/indexer/src/entity/whitelist.ts +++ b/indexer/src/entity/whitelist.ts @@ -9,6 +9,12 @@ export class Whitelist { @Column({type: 'text'}) proof!: string; + @Column() + timestamp!: number; + + @Column() + gameState!: string; + @CreateDateColumn() createdAt!: Date; } diff --git a/indexer/src/indexer.ts b/indexer/src/indexer.ts index 5b95d76..4822c41 100644 --- a/indexer/src/indexer.ts +++ b/indexer/src/indexer.ts @@ -276,7 +276,7 @@ const updatePendingMints = async () => { const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export const indexer = async () => { - // await checkWhitelistProofs(); + await checkWhitelistProofs(); try { while (true) { await pullEvents(); diff --git a/indexer/src/migrations/1707646272528-migration.ts b/indexer/src/migrations/1707646272528-migration.ts new file mode 100644 index 0000000..c23de4b --- /dev/null +++ b/indexer/src/migrations/1707646272528-migration.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class migration1707646272528 implements MigrationInterface { + name = 'migration1707646272528' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "whitelist" ADD "timestamp" integer NOT NULL`); + await queryRunner.query(`ALTER TABLE "whitelist" ADD "gameState" character varying NOT NULL`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "gameState"`); + await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "timestamp"`); + } + +} diff --git a/indexer/src/utils/checkWhitelistProofs.ts b/indexer/src/utils/checkWhitelistProofs.ts index 5ea83a6..4d3cd3b 100644 --- a/indexer/src/utils/checkWhitelistProofs.ts +++ b/indexer/src/utils/checkWhitelistProofs.ts @@ -1,15 +1,68 @@ -import fs from 'fs/promises' -import { saveWhitelistProofsFromFileToDB } from "./saveWhitelistProofsFromFileToDB" +import fs from "fs/promises"; +import { Whitelist } from "../entity/whitelist"; +import { AppDataSource } from "./db"; + +type DetailsObject = { + [generation: string]: { + user_id: string; + game_state: string; + timestamp: number; + }; +} export const checkWhitelistProofs = async () => { + try { + const res = await AppDataSource.query(`SELECT EXISTS(SELECT 1 FROM whitelist) AS isNotEmpty;`); + const isNotEmpty = res[0].isnotempty; + if (isNotEmpty) { + console.log("Whitelist is not empty. Skipping."); + return; + } + const whitelistFiles = (await fs.readdir("whitelist")).filter((filename) => filename.endsWith(".json") && filename !== "details.json"); + const detailsString = await fs.readFile(`whitelist/details.json`, "utf-8") + const details: DetailsObject = JSON.parse(detailsString); + whitelistFiles.forEach(async (filename) => { + console.log(`Saving ${filename} ...`); + await saveWhitelistProofsFromFileToDB(`whitelist/${filename}`, details); + console.log(`${filename} saved to DB.`); + }); + } catch (err) { + console.error("checkWhitelistProofs", err); + } + +}; + + + +const saveWhitelistProofsFromFileToDB = async (filename: string, details: DetailsObject) => { + console.log(`Retrieving whitelist proofs from "${filename}" file.`); + try { + const dataRaw = await fs.readFile(filename, "utf8"); + const data = JSON.parse(dataRaw); + + const whitelist = []; try { - const whitelistFiles = await fs.readdir('whitelist') - whitelistFiles.forEach(async (filename) => { - console.log(`Saving ${filename} ...`) - await saveWhitelistProofsFromFileToDB(`whitelist/${filename}`) - console.log(`${filename} saved to DB.`) - }) - }catch(err) { - console.error('Error saving whitelist proofs to DB', err) - } -} \ No newline at end of file + // @ts-ignore + for (const generation in data) { + const proofs = data[generation]; + if (!proofs.length) continue; + const proofText = proofs.join(","); + const whitelistRow = new Whitelist(); + whitelistRow.generation = Number(generation); + whitelistRow.proof = proofText; + if(!details[generation]) { + console.error(`No details found for generation ${generation}`); + } + whitelistRow.timestamp = details[generation].timestamp; + whitelistRow.gameState = details[generation].game_state; + whitelist.push(whitelistRow); + } + await AppDataSource.manager.save(whitelist); + console.log("Data inserted successfully."); + } catch (err) { + console.error("Error inserting data", err); + } + } catch (err) { + console.error("Error reading file", err); + } +}; diff --git a/indexer/src/utils/saveWhitelistProofsFromFileToDB.ts b/indexer/src/utils/saveWhitelistProofsFromFileToDB.ts deleted file mode 100644 index 6e1d9ed..0000000 --- a/indexer/src/utils/saveWhitelistProofsFromFileToDB.ts +++ /dev/null @@ -1,29 +0,0 @@ -import fs from 'fs/promises' -import { Whitelist } from "../entity/whitelist"; -import { AppDataSource } from './db'; -import { exit } from 'process'; - -export const saveWhitelistProofsFromFileToDB = async (filename:string) => { - console.log(`Retrieving whitelist proofs from "${filename}" file.`); - const dataRaw = await fs.readFile(filename, "utf8"); - const data = JSON.parse(dataRaw); - - const whitelist = []; - try { - // @ts-ignore - for (const generation in data) { - const proofs = data[generation]; - if(!proofs.length) continue; - const proofText = proofs.join(","); - const whitelistRow = new Whitelist(); - whitelistRow.generation = Number(generation); - whitelistRow.proof = proofText; - whitelist.push(whitelistRow); - } - await AppDataSource.manager.save(whitelist); - console.log("Data inserted successfully."); - } catch (err) { - console.error("Error inserting data", err); - exit(1); - } -};