From 3c32a0d24ca7a928c2ccf2364e66be290c23eb3d Mon Sep 17 00:00:00 2001 From: lorcan-codes <126797224+lorcan-codes@users.noreply.github.com> Date: Mon, 29 Jan 2024 21:13:26 +0500 Subject: [PATCH] add function to update pending snapshots --- .gitignore | 5 ++- indexer/src/entity/mints.ts | 23 +++++++++++ indexer/src/indexer.ts | 38 +++++++++++++++++++ .../src/migrations/1706466839411-migration.ts | 16 ++++++++ indexer/src/utils/db.ts | 3 +- 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 indexer/src/entity/mints.ts create mode 100644 indexer/src/migrations/1706466839411-migration.ts diff --git a/.gitignore b/.gitignore index 616937a..55b04bf 100644 --- a/.gitignore +++ b/.gitignore @@ -134,4 +134,7 @@ dmypy.json .pyre/ # indexer modules -node_modules/ \ No newline at end of file +node_modules/ + +certificates +*.crt \ No newline at end of file diff --git a/indexer/src/entity/mints.ts b/indexer/src/entity/mints.ts new file mode 100644 index 0000000..1f01f32 --- /dev/null +++ b/indexer/src/entity/mints.ts @@ -0,0 +1,23 @@ +import { Column, CreateDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from "typeorm" + +@Entity() +export class Mints { + @Index() + @PrimaryColumn({type: "integer", unique: true}) + generation!: number; + + @Column() + userId!: string; + + @Column() + txHash!: string; + + @Column() + status!: string; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedDate!: Date +} diff --git a/indexer/src/indexer.ts b/indexer/src/indexer.ts index a3a3910..83af435 100644 --- a/indexer/src/indexer.ts +++ b/indexer/src/indexer.ts @@ -8,6 +8,7 @@ import { parseEventContent } from "./utils/events"; import { logger } from "./utils/logger"; import { getLastSavedBlock } from "./utils/db"; import { viewRefresher } from "./viewRefresher"; +import { Mints } from "./entity/mints"; type TransactionEvent = { from_address: string, @@ -223,6 +224,42 @@ const updateTransactions = async () => { } } +const updatePendingMints = async () => { + const pendingMints = await AppDataSource.manager.find( + Mints + ) + + for (let mint in pendingMints) { + const mintDetail = pendingMints[mint]; + const tx = (await starknet.getTransactionStatus( + pendingMints[mint].txHash + )); + // @ts-ignore + if(tx.execution_status === "REJECTED" || tx.execution_status === "REVERTED"){ + logger.info( + { + transactionHash: mintDetail.txHash, + status: tx.execution_status, + }, + "Removing failed mint" + ) + AppDataSource.manager.remove(mintDetail); + } else if(mintDetail.status !== tx.finality_status) { + logger.info( + { + txHash: mintDetail.txHash, + execution_status: tx.execution_status, + finality_status: tx.finality_status, + }, + "Updating mint status" + ) + mintDetail.status = tx.finality_status; + await AppDataSource.manager.save(mintDetail); + } + } + +} + const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); export const indexer = async () => { try { @@ -230,6 +267,7 @@ export const indexer = async () => { await processNextBlock(); await viewRefresher(); await updateTransactions(); + await updatePendingMints(); await wait(3000); } } catch (e) { diff --git a/indexer/src/migrations/1706466839411-migration.ts b/indexer/src/migrations/1706466839411-migration.ts new file mode 100644 index 0000000..186577c --- /dev/null +++ b/indexer/src/migrations/1706466839411-migration.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class migration1706466839411 implements MigrationInterface { + name = 'migration1706466839411' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "mints" ("generation" integer NOT NULL, "userId" character varying NOT NULL, "txHash" character varying NOT NULL, "status" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedDate" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_391ba31dd81cfe86148e7c915ba" PRIMARY KEY ("generation"))`); + await queryRunner.query(`CREATE INDEX "IDX_391ba31dd81cfe86148e7c915b" ON "mints" ("generation") `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "public"."IDX_391ba31dd81cfe86148e7c915b"`); + await queryRunner.query(`DROP TABLE "mints"`); + } + +} diff --git a/indexer/src/utils/db.ts b/indexer/src/utils/db.ts index e3e278a..c1a2ab4 100644 --- a/indexer/src/utils/db.ts +++ b/indexer/src/utils/db.ts @@ -12,6 +12,7 @@ import * as fs from 'fs'; // Uncomment below in local development // import 'dotenv/config' +import { Mints } from "../entity/mints"; // We need to store bigints in jsonb column, typeorm doesn't support that. // Transformers in typeorm run _before_ typeorm's JSON.stringify run, so it is problematic @@ -43,7 +44,7 @@ export const AppDataSource = new DataSource({ }, synchronize: false, logging: false, - entities: [Block, Event, Transaction, Refresh, Balance, Creator, Infinite], + entities: [Block, Event, Transaction, Refresh, Balance, Creator, Infinite, Mints], migrations: ["dist/migrations/*.js"], subscribers: [], });