-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hub pool events indexing (#482)
- Loading branch information
Showing
16 changed files
with
1,638 additions
and
0 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
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,14 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class HubPoolProcessedBlock1720909029359 implements MigrationInterface { | ||
name = 'HubPoolProcessedBlock1720909029359'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "hub_pool_processed_block" ("id" SERIAL NOT NULL, "chainId" integer NOT NULL, "latestBlock" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_5ee19e9910bd8cac851b865d51a" PRIMARY KEY ("id"))`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "hub_pool_processed_block"`); | ||
} | ||
|
||
} |
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,32 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class SetPoolRebalanceRouteEvent1721045885663 implements MigrationInterface { | ||
name = "SetPoolRebalanceRouteEvent1721045885663"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TABLE "events"."set_pool_rebalance_route_event" ( | ||
"id" SERIAL NOT NULL, | ||
"blockNumber" integer NOT NULL, | ||
"blockHash" character varying NOT NULL, | ||
"transactionIndex" integer NOT NULL, | ||
"address" character varying NOT NULL, | ||
"chainId" integer NOT NULL, | ||
"transactionHash" character varying NOT NULL, | ||
"logIndex" integer NOT NULL, | ||
"destinationChainId" integer NOT NULL, | ||
"l1Token" character varying NOT NULL, | ||
"destinationToken" character varying NOT NULL, | ||
"createdAt" TIMESTAMP NOT NULL DEFAULT now(), | ||
"updatedAt" TIMESTAMP NOT NULL DEFAULT now(), | ||
CONSTRAINT "UK_sprre_transactionHash_logIndex" UNIQUE ("transactionHash", "logIndex"), | ||
CONSTRAINT "PK_1acd4d3e93afc21bfedc3d99892" PRIMARY KEY ("id")) | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP TABLE "events"."set_pool_rebalance_route_event"`, | ||
); | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
src/modules/scraper/adapter/messaging/HubPoolBlocksEventsConsumer.ts
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,59 @@ | ||
import { OnQueueFailed, Process, Processor } from "@nestjs/bull"; | ||
import { Logger } from "@nestjs/common"; | ||
import { Job } from "bull"; | ||
import { DataSource } from "typeorm"; | ||
|
||
import { EthProvidersService } from "../../../web3/services/EthProvidersService"; | ||
import { HubPoolBlocksEventsQueueMessage, ScraperQueue } from "."; | ||
import { AppConfig } from "../../../configuration/configuration.service"; | ||
import { SetPoolRebalanceRoute } from "../../../web3/model/hubpool-events"; | ||
import { SetPoolRebalanceRouteEvent } from "../../../web3/model/SetPoolRebalanceRouteEvent.entity"; | ||
|
||
@Processor(ScraperQueue.HubPoolBlocksEvents) | ||
export class HubPoolBlocksEventsConsumer { | ||
private logger = new Logger(HubPoolBlocksEventsConsumer.name); | ||
|
||
constructor( | ||
private providers: EthProvidersService, | ||
private appConfig: AppConfig, | ||
private dataSource: DataSource, | ||
) {} | ||
|
||
@Process({ concurrency: 1 }) | ||
private async process(job: Job<HubPoolBlocksEventsQueueMessage>) { | ||
const { chainId, from, to } = job.data; | ||
const { address } = this.appConfig.values.web3.hubPoolContracts[chainId]; | ||
const setPoolRebalanceRouteEvents = (await this.providers | ||
.getHubPoolEventQuerier(chainId, address) | ||
.getSetPoolRebalanceRouteEvents(from, to)) as SetPoolRebalanceRoute[]; | ||
this.logger.log(`(${from}, ${to}) - chainId ${chainId} - found ${setPoolRebalanceRouteEvents.length} SetPoolRebalanceRouteEvent`); | ||
|
||
if (setPoolRebalanceRouteEvents.length > 0) { | ||
for (const event of setPoolRebalanceRouteEvents) { | ||
await this.dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(SetPoolRebalanceRouteEvent) | ||
.values({ | ||
blockNumber: event.blockNumber, | ||
blockHash: event.blockHash, | ||
transactionIndex: event.transactionIndex, | ||
address: event.address, | ||
chainId, | ||
transactionHash: event.transactionHash, | ||
logIndex: event.logIndex, | ||
destinationChainId: event.args.destinationChainId.toNumber(), | ||
l1Token: event.args.l1Token, | ||
destinationToken: event.args.destinationToken, | ||
}) | ||
.orIgnore() | ||
.execute(); | ||
} | ||
} | ||
} | ||
|
||
@OnQueueFailed() | ||
private onQueueFailed(job: Job, error: Error) { | ||
this.logger.error(`${JSON.stringify(job.data)} failed: ${error}`); | ||
} | ||
} |
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,16 @@ | ||
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from "typeorm"; | ||
|
||
@Entity() | ||
export class HubPoolProcessedBlock { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
chainId: number; | ||
|
||
@Column() | ||
latestBlock: number; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
} |
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
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
46 changes: 46 additions & 0 deletions
46
src/modules/web3/model/SetPoolRebalanceRouteEvent.entity.ts
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,46 @@ | ||
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, Unique, UpdateDateColumn } from "typeorm"; | ||
|
||
@Entity({ | ||
schema: "events", | ||
}) | ||
@Unique("UK_sprre_transactionHash_logIndex", ["transactionHash", "logIndex"]) | ||
export class SetPoolRebalanceRouteEvent { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
blockNumber: number; | ||
|
||
@Column() | ||
blockHash: string; | ||
|
||
@Column() | ||
transactionIndex: number; | ||
|
||
@Column() | ||
address: string; | ||
|
||
@Column() | ||
chainId: number; | ||
|
||
@Column() | ||
transactionHash: string; | ||
|
||
@Column() | ||
logIndex: number; | ||
|
||
@Column() | ||
destinationChainId: number; | ||
|
||
@Column() | ||
l1Token: string; | ||
|
||
@Column() | ||
destinationToken: string; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
} |
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,13 @@ | ||
import { BigNumber, Event } from "ethers"; | ||
|
||
export interface SetPoolRebalanceRoute extends Event { | ||
args: [ | ||
BigNumber, | ||
string, | ||
string, | ||
] & { | ||
destinationChainId: BigNumber; | ||
l1Token: string; | ||
destinationToken: string; | ||
} | ||
} |
Oops, something went wrong.