Skip to content

Commit

Permalink
feat(indexer): store time when transfer is completed (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Dec 9, 2024
1 parent 3401292 commit 8ba230a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
13 changes: 13 additions & 0 deletions indexer/db/migrations/1733751857295-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = class Data1733751857295 {
name = 'Data1733751857295'

async up(db) {
await db.query(`ALTER TABLE "transfer" ADD "completed_at" TIMESTAMP WITH TIME ZONE`)
await db.query(`ALTER TABLE "completed_transfer" ADD "timestamp" TIMESTAMP WITH TIME ZONE`)
}

async down(db) {
await db.query(`ALTER TABLE "transfer" DROP COLUMN "completed_at"`)
await db.query(`ALTER TABLE "completed_transfer" DROP COLUMN "timestamp"`)
}
}
3 changes: 2 additions & 1 deletion indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "squid-evm-template",
"private": true,
"scripts": {
"build": "rm -rf lib && tsc"
"build": "rm -rf lib && tsc",
"migration:generate": "sqd migration generate"
},
"dependencies": {
"@subsquid/evm-abi": "^0.3.0",
Expand Down
2 changes: 2 additions & 0 deletions indexer/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Transfer @entity {
txHash: String!
blockNumber: String!
timestamp: DateTime! @index
completedAt: DateTime
nonce: String! @index
sourceNetwork: Network!
source: String! @index
Expand All @@ -36,4 +37,5 @@ type CompletedTransfer @entity {
id: ID!
nonce: String! @index @unique
destNetwork: Network!
timestamp: DateTime
}
11 changes: 8 additions & 3 deletions indexer/src/common/tempState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export class TempState {

if (transfers.length > 0) {
for (const t of transfers) {
const completed = this._completed.get(t.nonce)!;
t.status = Status.Completed;
completedToDelete.push(this._completed.get(t.nonce)!);
t.completedAt = completed.timestamp;
completedToDelete.push(completed);
this._completed.delete(t.nonce);
}
if (completedToDelete.length > 0) {
Expand Down Expand Up @@ -129,8 +131,11 @@ export class TempState {
this._ctx.log.info(`Transfer requested: ${transfer.nonce}`);
}

public transferCompleted(nonce: string) {
this._completed.set(nonce, new CompletedTransfer({ id: randomUUID(), nonce, destNetwork: this._network }));
public transferCompleted(nonce: string, ts: Date) {
this._completed.set(
nonce,
new CompletedTransfer({ id: randomUUID(), nonce, destNetwork: this._network, timestamp: ts }),
);
this._ctx.log.info(`Transfer completed: ${nonce}`);
}

Expand Down
5 changes: 3 additions & 2 deletions indexer/src/eth/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const handler = async (ctx: Context) => {
const promises = [];

for (let block of ctx.blocks) {
const timestamp = new Date(block.header.timestamp);
for (let log of block.logs) {
const address = log.address.toLowerCase();
const topic = log.topics[0].toLowerCase();
Expand All @@ -32,7 +33,7 @@ const handler = async (ctx: Context) => {
id: randomUUID(),
txHash: log.transactionHash,
blockNumber: block.header.height.toString(),
timestamp: new Date(block.header.timestamp),
timestamp,
nonce: ethNonce(`${block.header.height}${log.transactionIndex}`),
sourceNetwork: Network.Ethereum,
source: token,
Expand All @@ -46,7 +47,7 @@ const handler = async (ctx: Context) => {
);
} else if (address === MSGQ && topic === MSGQ_MESSAGE_PROCESSED) {
const [_, __, nonce] = messageQueueAbi.events.MessageProcessed.decode(log);
promises.push(tempState.transferCompleted(gearNonce(nonce, false)));
promises.push(tempState.transferCompleted(gearNonce(nonce, false), timestamp));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion indexer/src/gear/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const handler = async (ctx: ProcessorContext) => {
);

const nonce = ethNonce(`${block_number}${transaction_index}`);
promises.push(tempState.transferCompleted(nonce));
promises.push(tempState.transferCompleted(nonce, timestamp));
break;
}
}
Expand Down
5 changes: 4 additions & 1 deletion indexer/src/model/generated/completedTransfer.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, DateTimeColumn as DateTimeColumn_} from "@subsquid/typeorm-store"
import {Network} from "./_network"

@Entity_()
Expand All @@ -16,4 +16,7 @@ export class CompletedTransfer {

@Column_("varchar", {length: 8, nullable: false})
destNetwork!: Network

@DateTimeColumn_({nullable: true})
timestamp!: Date | undefined | null
}
3 changes: 3 additions & 0 deletions indexer/src/model/generated/transfer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class Transfer {
@DateTimeColumn_({nullable: false})
timestamp!: Date

@DateTimeColumn_({nullable: true})
completedAt!: Date | undefined | null

@Index_()
@StringColumn_({nullable: false})
nonce!: string
Expand Down

0 comments on commit 8ba230a

Please sign in to comment.