Skip to content

Commit

Permalink
Merge pull request #5 from yuki-wtf/handle-pending-mints
Browse files Browse the repository at this point in the history
add function to update pending snapshots
  • Loading branch information
lorcan-codes authored Jan 29, 2024
2 parents f648d0f + 3c32a0d commit 132a3b0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,7 @@ dmypy.json
.pyre/

# indexer modules
node_modules/
node_modules/

certificates
*.crt
23 changes: 23 additions & 0 deletions indexer/src/entity/mints.ts
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -223,13 +224,50 @@ 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 {
while (true) {
await processNextBlock();
await viewRefresher();
await updateTransactions();
await updatePendingMints();
await wait(3000);
}
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions indexer/src/migrations/1706466839411-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class migration1706466839411 implements MigrationInterface {
name = 'migration1706466839411'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_391ba31dd81cfe86148e7c915b"`);
await queryRunner.query(`DROP TABLE "mints"`);
}

}
3 changes: 2 additions & 1 deletion indexer/src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [],
});
Expand Down

0 comments on commit 132a3b0

Please sign in to comment.