Skip to content

Commit

Permalink
inlcude gamestate and timestamp in whitelist table && update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lorcan-codes committed Feb 11, 2024
1 parent 0409797 commit f6c9836
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
6 changes: 6 additions & 0 deletions indexer/src/entity/whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export class Whitelist {
@Column({type: 'text'})
proof!: string;

@Column()
timestamp!: number;

@Column()
gameState!: string;

@CreateDateColumn()
createdAt!: Date;
}
2 changes: 1 addition & 1 deletion indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions indexer/src/migrations/1707646272528-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class migration1707646272528 implements MigrationInterface {
name = 'migration1707646272528'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "gameState"`);
await queryRunner.query(`ALTER TABLE "whitelist" DROP COLUMN "timestamp"`);
}

}
77 changes: 65 additions & 12 deletions indexer/src/utils/checkWhitelistProofs.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
// @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);
}
};
29 changes: 0 additions & 29 deletions indexer/src/utils/saveWhitelistProofsFromFileToDB.ts

This file was deleted.

0 comments on commit f6c9836

Please sign in to comment.