-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Bundle model and processor for handling bundle events
- Loading branch information
Showing
6 changed files
with
254 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- CreateTable | ||
CREATE TABLE "Bundle" ( | ||
"bundleNftId" BIGINT NOT NULL, | ||
"poolNftId" BIGINT NOT NULL, | ||
"lifetime" BIGINT NOT NULL, | ||
"locked" BOOLEAN NOT NULL DEFAULT false, | ||
"closed" BOOLEAN NOT NULL DEFAULT false, | ||
"balance" BIGINT NOT NULL, | ||
"lockedAmount" BIGINT NOT NULL, | ||
"created_blockNumber" INTEGER NOT NULL, | ||
"created_timestamp" BIGINT NOT NULL, | ||
"created_txHash" TEXT NOT NULL, | ||
"created_from" TEXT NOT NULL, | ||
"modified_blockNumber" INTEGER NOT NULL, | ||
"modified_timestamp" BIGINT NOT NULL, | ||
"modified_txHash" TEXT NOT NULL, | ||
"modified_from" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Bundle_pkey" PRIMARY KEY ("bundleNftId") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { IBundleService__factory } from "./generated/contracts/gif"; | ||
import { logger } from "./logger"; | ||
import { Bundle } from "./types/bundle"; | ||
import { DecodedLogEntry } from "./types/logdata"; | ||
|
||
export default class BundleProcessor { | ||
private prisma: PrismaClient; | ||
|
||
constructor(prisma: PrismaClient) { | ||
this.prisma = prisma; | ||
} | ||
|
||
async persistBundles(bundles: Bundle[]): Promise<void> { | ||
for (const bundle of bundles) { | ||
await this.prisma.bundle.upsert({ | ||
where: { bundleNftId: bundle.bundleNftId }, | ||
update: { | ||
poolNftId: bundle.poolNftId, | ||
lifetime: bundle.lifetime, | ||
locked: bundle.locked, | ||
closed: bundle.closed, | ||
balance: bundle.balance, | ||
lockedAmount: bundle.lockedAmount, | ||
modified_blockNumber: bundle.modified.blockNumber, | ||
modified_timestamp: bundle.modified.timestamp, | ||
modified_txHash: bundle.modified.txHash, | ||
modified_from: bundle.modified.from | ||
}, | ||
create: { | ||
bundleNftId: bundle.bundleNftId, | ||
poolNftId: bundle.poolNftId, | ||
lifetime: bundle.lifetime, | ||
locked: bundle.locked, | ||
closed: bundle.closed, | ||
balance: bundle.balance, | ||
lockedAmount: bundle.lockedAmount, | ||
created_blockNumber: bundle.created.blockNumber, | ||
created_timestamp: bundle.created.timestamp, | ||
created_txHash: bundle.created.txHash, | ||
created_from: bundle.created.from, | ||
modified_blockNumber: bundle.modified.blockNumber, | ||
modified_timestamp: bundle.modified.timestamp, | ||
modified_txHash: bundle.modified.txHash, | ||
modified_from: bundle.modified.from | ||
} | ||
}); | ||
} | ||
} | ||
|
||
async processBundleCreatedEvent(event: DecodedLogEntry, bundles: Map<BigInt, Bundle>): Promise<Map<BigInt, Bundle>> { | ||
if (event.event_name !== 'LogBundleServiceBundleCreated') { | ||
throw new Error(`Invalid event type ${event.event_name}`); | ||
} | ||
|
||
logger.info(`Processing bundle created event ${event.tx_hash} - ${event.event_name} - ${event.data}`); | ||
const data = this.decodeBundleServiceEvent(event); | ||
if (data === null || data === undefined) { | ||
logger.error(`Failed to decode event ${event.tx_hash} - ${event.event_name} - ${event.data}`); | ||
return bundles; | ||
} | ||
if (data.name !== 'LogBundleServiceBundleCreated') { | ||
throw new Error(`Invalid event name ${data.name}`); | ||
} | ||
|
||
const bundleNftId = data.args[0] as BigInt; | ||
const poolNftId = data.args[1] as BigInt; | ||
const lifetime = data.args[2] as BigInt; | ||
|
||
// TODO: validate poolNftId | ||
|
||
const bundle = { | ||
bundleNftId, | ||
poolNftId, | ||
lifetime, | ||
locked: false, | ||
closed: false, | ||
balance: BigInt(0), | ||
lockedAmount: BigInt(0), | ||
created: { | ||
blockNumber: event.block_number, | ||
timestamp:BigInt(new Date(event.block_time).getTime()), | ||
txHash: event.tx_hash, | ||
from: event.tx_from | ||
}, | ||
modified: { | ||
blockNumber: event.block_number, | ||
timestamp:BigInt(new Date(event.block_time).getTime()), | ||
txHash: event.tx_hash, | ||
from: event.tx_from | ||
} | ||
} as Bundle; | ||
bundles.set(bundleNftId, bundle); | ||
return bundles; | ||
} | ||
|
||
async processBundleClosedEvent(event: DecodedLogEntry, bundles: Map<BigInt, Bundle>): Promise<Map<BigInt, Bundle>> { | ||
if (event.event_name !== 'LogBundleServiceBundleClosed') { | ||
throw new Error(`Invalid event type ${event.event_name}`); | ||
} | ||
|
||
logger.info(`Processing bundle closed event ${event.tx_hash} - ${event.event_name} - ${event.data}`); | ||
const data = this.decodeBundleServiceEvent(event); | ||
if (data === null || data === undefined) { | ||
logger.error(`Failed to decode event ${event.tx_hash} - ${event.event_name} - ${event.data}`); | ||
return bundles; | ||
} | ||
if (data.name !== 'LogBundleServiceBundleClosed') { | ||
throw new Error(`Invalid event name ${data.name}`); | ||
} | ||
|
||
const bundleNftId = data.args[0] as BigInt; | ||
const bundle = bundles.get(bundleNftId); | ||
if (bundle === undefined) { | ||
throw new Error(`Bundle not found ${bundleNftId}`); | ||
} | ||
bundle.closed = true; | ||
bundle.modified = { | ||
blockNumber: event.block_number, | ||
timestamp:BigInt(new Date(event.block_time).getTime()), | ||
txHash: event.tx_hash, | ||
from: event.tx_from | ||
}; | ||
return bundles; | ||
} | ||
|
||
decodeBundleServiceEvent(event: DecodedLogEntry) { | ||
const topic0 = event.topic0; | ||
let topic1 = event.topic1; | ||
if (topic1 === null || topic1 === undefined || topic1 === '') { | ||
topic1 = '0x'; | ||
} | ||
let topic2 = event.topic2; | ||
if (topic2 === null || topic2 === undefined || topic2 === '') { | ||
topic2 = '0x'; | ||
} | ||
let topic3 = event.topic3; | ||
if (topic3 === null || topic3 === undefined || topic3 === '') { | ||
topic3 = '0x'; | ||
} | ||
return IBundleService__factory.createInterface().parseLog({ topics: [topic0, topic1, topic2, topic3], data: event.data }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
export interface Bundle { | ||
bundleNftId: bigint; | ||
poolNftId: bigint; | ||
lifetime: bigint; | ||
locked: boolean; | ||
closed: boolean; | ||
balance: bigint; | ||
lockedAmount: bigint; | ||
created: { | ||
blockNumber: number; | ||
timestamp: bigint; | ||
txHash: string; | ||
from: string; | ||
} | ||
modified: { | ||
blockNumber: number; | ||
timestamp: bigint; | ||
txHash: string; | ||
from: string; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { ObjectType } from "./objecttype"; | ||
|
||
export interface OracleRequest { | ||
oracleNftId: bigint; | ||
|