Skip to content

Commit

Permalink
feat(hubpool): set pool rebalance route for token mappings (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
  • Loading branch information
james-a-morris authored Sep 26, 2024
1 parent 00e747f commit fdf9b44
Show file tree
Hide file tree
Showing 9 changed files with 10,294 additions and 7,441 deletions.
2 changes: 1 addition & 1 deletion packages/indexer-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@across-protocol/sdk": "^3.1.30",
"@across-protocol/sdk": "^3.2.2",
"pg": "^8.4.0",
"reflect-metadata": "^0.1.13",
"superstruct": "2.0.3-1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";

@Entity({ schema: "evm" })
@Unique("UK_setPoolRebalanceRoute_transactionHash_transactionIndex_logIndex", [
"transactionHash",
"transactionIndex",
"logIndex",
])
export class SetPoolRebalanceRoute {
@PrimaryGeneratedColumn()
id: number;

@Column({ nullable: false })
destinationChainId: number;

@Column({ nullable: false })
l1Token: string;

@Column({ nullable: false })
destinationToken: string;

@Column({ nullable: false })
blockNumber: number;

@Column({ nullable: false })
transactionHash: string;

@Column({ nullable: false })
transactionIndex: number;

@Column({ nullable: false })
logIndex: number;

@CreateDateColumn()
createdAt: Date;
}
1 change: 1 addition & 0 deletions packages/indexer-database/src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./evm/ProposedRootBundle";
export * from "./evm/RootBundleCanceled";
export * from "./evm/RootBundleDisputed";
export * from "./evm/RootBundleExecuted";
export * from "./evm/SetPoolRebalanceRoute";
// SpokePool
export * from "./evm/V3FundsDeposited";
export * from "./evm/FilledV3Relay";
Expand Down
1 change: 1 addition & 0 deletions packages/indexer-database/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const createDataSource = (config: DatabaseConfig): DataSource => {
entities.RootBundleCanceled,
entities.RootBundleDisputed,
entities.RootBundleExecuted,
entities.SetPoolRebalanceRoute,
// SpokePool
entities.ExecutedRelayerRefundRoot,
entities.FilledV3Relay,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class SetPoolRebalanceRoute1727374473839 implements MigrationInterface {
name = "SetPoolRebalanceRoute1727374473839";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "evm"."set_pool_rebalance_route" (
"id" SERIAL NOT NULL,
"destinationChainId" integer NOT NULL,
"l1Token" character varying NOT NULL,
"destinationToken" character varying NOT NULL,
"blockNumber" integer NOT NULL,
"transactionHash" character varying NOT NULL,
"transactionIndex" integer NOT NULL,
"logIndex" integer NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
CONSTRAINT "UK_setPoolRebalanceRoute_transactionHash_transactionIndex_logIndex" UNIQUE ("transactionHash", "transactionIndex", "logIndex"),
CONSTRAINT "PK_93edcf0d94f29e5cd34513baf9d" PRIMARY KEY ("id")
)
`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "evm"."set_pool_rebalance_route"`);
}
}
2 changes: 1 addition & 1 deletion packages/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@across-protocol/constants": "^3.1.13",
"@across-protocol/contracts": "^3.0.8",
"@across-protocol/sdk": "^3.1.30",
"@across-protocol/sdk": "^3.2.2",
"@types/lodash": "^4.17.7",
"bullmq": "^5.12.12",
"ethers": "^5.7.2",
Expand Down
93 changes: 93 additions & 0 deletions packages/indexer/src/database/HubPoolRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,97 @@ export class HubPoolRepository extends utils.BaseRepository {
});
await this.insert(entities.RootBundleExecuted, formattedEvents, throwError);
}

public async formatAndSaveSetPoolRebalanceRouteEvents(
setPoolRebalanceRouteEvents: (across.interfaces.DestinationTokenWithBlock & {
l2ChainId: number;
})[],
throwError?: boolean,
) {
const formattedEvents = setPoolRebalanceRouteEvents.map((event) => {
return {
...event,
destinationChainId: event.l2ChainId,
destinationToken: event.l2Token,
l1Token: event.l1Token,
};
});
await this.insert(
entities.SetPoolRebalanceRoute,
formattedEvents,
throwError,
);
}

/**
* Finds the L1 token associated with an L2 token, closest in time (block number).
* @param l2Token - The L2 token address.
* @param chainId - The destination chain ID.
* @param l1BlockNumber - Optional L1 block number to find the closest match less than or equal to this value.
* @returns The L1 token address or undefined if not found.
*/
public async findL1TokenFromL2Token(
l2Token: string,
chainId: number,
l1BlockNumber?: number,
): Promise<string | undefined> {
// Build the base query
const queryBuilder = this.postgres
.getRepository(entities.SetPoolRebalanceRoute)
.createQueryBuilder("poolRebalanceRoot")
.where("poolRebalanceRoot.destinationToken = :l2Token", { l2Token })
.andWhere("poolRebalanceRoot.destinationChainId = :chainId", { chainId });

// If l1BlockNumber is provided, find the closest one that is <= the provided block number
if (l1BlockNumber !== undefined) {
queryBuilder.andWhere("poolRebalanceRoot.blockNumber <= :l1BlockNumber", {
l1BlockNumber,
});
}

// Order by blockNumber descending to get the closest match
queryBuilder.orderBy("poolRebalanceRoot.blockNumber", "DESC");

// Execute the query to find the closest matching entry
const result = await queryBuilder.getOne();

// Return the L1 token if a result is found, otherwise undefined
return result?.l1Token;
}

/**
* Finds the L2 token associated with an L1 token, closest in time (block number).
* @param l1Token - The L1 token address.
* @param chainId - The destination chain ID.
* @param l1BlockNumber - Optional L1 block number to find the closest match less than or equal to this value.
* @returns The L2 token address or undefined if not found.
*/
public async findL2TokenFromL1Token(
l1Token: string,
chainId: number,
l1BlockNumber?: number,
): Promise<string | undefined> {
// Build the base query
const queryBuilder = this.postgres
.getRepository(entities.SetPoolRebalanceRoute)
.createQueryBuilder("poolRebalanceRoot")
.where("poolRebalanceRoot.l1Token = :l1Token", { l1Token })
.andWhere("poolRebalanceRoot.destinationChainId = :chainId", { chainId });

// If l1BlockNumber is provided, find the closest one that is <= the provided block number
if (l1BlockNumber !== undefined) {
queryBuilder.andWhere("poolRebalanceRoot.blockNumber <= :l1BlockNumber", {
l1BlockNumber,
});
}

// Order by blockNumber descending to get the closest match
queryBuilder.orderBy("poolRebalanceRoot.blockNumber", "DESC");

// Execute the query to find the closest matching entry
const result = await queryBuilder.getOne();

// Return the L2 token if a result is found, otherwise undefined
return result?.destinationToken;
}
}
10 changes: 10 additions & 0 deletions packages/indexer/src/services/hubPoolIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class Indexer extends BaseIndexer {
hubPoolClient.getCancelledRootBundlesInBlockRange(fromBlock, toBlock);
const rootBundleDisputedEvents =
hubPoolClient.getDisputedRootBundlesInBlockRange(fromBlock, toBlock);
const setPoolRebalanceRouteEvents =
hubPoolClient.getTokenMappingsModifiedInBlockRange(fromBlock, toBlock);
// we do not have a block range query for executed root bundles
const rootBundleExecutedEvents = hubPoolClient.getExecutedRootBundles();

Expand All @@ -178,6 +180,7 @@ export class Indexer extends BaseIndexer {
(event) =>
event.blockNumber >= fromBlock && event.blockNumber <= toBlock,
),
setPoolRebalanceRouteEvents,
};
}

Expand All @@ -188,13 +191,17 @@ export class Indexer extends BaseIndexer {
rootBundleCanceledEvents: across.interfaces.CancelledRootBundle[];
rootBundleDisputedEvents: across.interfaces.DisputedRootBundle[];
rootBundleExecutedEvents: across.interfaces.ExecutedRootBundle[];
setPoolRebalanceRouteEvents: (across.interfaces.DestinationTokenWithBlock & {
l2ChainId: number;
})[];
}) {
const { hubPoolRepository } = this;
const {
proposedRootBundleEvents,
rootBundleCanceledEvents,
rootBundleDisputedEvents,
rootBundleExecutedEvents,
setPoolRebalanceRouteEvents,
} = params;
await hubPoolRepository.formatAndSaveProposedRootBundleEvents(
proposedRootBundleEvents,
Expand All @@ -208,5 +215,8 @@ export class Indexer extends BaseIndexer {
await hubPoolRepository.formatAndSaveRootBundleExecutedEvents(
rootBundleExecutedEvents,
);
await hubPoolRepository.formatAndSaveSetPoolRebalanceRouteEvents(
setPoolRebalanceRouteEvents,
);
}
}
Loading

0 comments on commit fdf9b44

Please sign in to comment.