Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add swap price worker #174

Draft
wants to merge 21 commits into
base: david/acx-3489-identify-deposit-input-token-swap-2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b0cc306
feat: support duplicated deposits and handle fill event matching
melisaguevara Jan 23, 2025
c57dfcc
Ensure colliding deposits create new rows in relayHashInfo
melisaguevara Jan 28, 2025
bda8283
feat: handle depositId bigInt type from sdk (#166)
melisaguevara Jan 30, 2025
7f2bd36
use database locks to prevent race conditions between indexers when u…
melisaguevara Jan 31, 2025
ac74b98
Apply suggestions from code review
melisaguevara Jan 31, 2025
a33ba1d
feat(indexer): add coingecko price collection from tokens against usd
Dec 11, 2024
50d4ef4
fix migration, add docs, use date for database
daywiss Dec 19, 2024
6eb3c34
refactor price lookups to use workers
daywiss Dec 24, 2024
a4d530f
remove config, include bridge fee calculation, input and output token…
daywiss Dec 30, 2024
06e8001
add relay hash info migration
daywiss Jan 3, 2025
e119ed7
add inputPriceUsd, outputPriceUsd to relay hash info
daywiss Jan 3, 2025
54f9f95
test bridge fee calc, use price symbols to store prices, store input/…
daywiss Jan 3, 2025
e71caee
change saves to updates, handle invalid fills
daywiss Jan 6, 2025
d316db1
use sdk coingecko, use floats for prices
daywiss Jan 7, 2025
dac9d4b
Fix migrations
amateima Jan 13, 2025
30b3876
use relay hash to find entities
daywiss Jan 13, 2025
1266efa
use fill id to kick off worker, remove dependency on deposit
daywiss Feb 4, 2025
44a1581
remove comment
daywiss Feb 4, 2025
353b102
include worker calls in profiling, update yesterday func to subtract …
daywiss Feb 5, 2025
bc6199e
feat: add swap events
amateima Feb 3, 2025
fbc0b2d
feat(indexer): add swap pricing worker
daywiss Feb 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/error-handling/src/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { AssertError } from "../errors/AssertError";
* @returns An assertion of `value`.
* @throws {@link AssertError} if assert's validity fails
*/
export function assert(value: unknown, message: string): asserts value {
export function assert(
value: unknown,
message: string,
): asserts value is NonNullable<unknown> {
try {
return assertModule.ok(value, message);
} catch (e: unknown) {
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@across-protocol/sdk": "^3.3.23",
"@across-protocol/sdk": "^3.4.15",
"pg": "^8.4.0",
"reflect-metadata": "^0.1.13",
"superstruct": "2.0.3-1",
Expand Down
33 changes: 33 additions & 0 deletions packages/indexer-database/src/entities/HistoricPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";

@Entity()
@Unique("UK_hp_baseCurrency_quoteCurrency_date", [
"baseCurrency",
"quoteCurrency",
"date",
])
export class HistoricPrice {
@PrimaryGeneratedColumn()
id: number;

@Column()
baseCurrency: string;

@Column({ default: "usd" })
quoteCurrency: string;

@Column({ type: "date" })
date: Date;

@Column({ type: "decimal" })
price: string;

@CreateDateColumn()
createdAt: Date;
}
35 changes: 33 additions & 2 deletions packages/indexer-database/src/entities/RelayHashInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { V3FundsDeposited } from "./evm/V3FundsDeposited";
import { FilledV3Relay } from "./evm/FilledV3Relay";
import { RequestedV3SlowFill } from "./evm/RequestedV3SlowFill";
import { SwapBeforeBridge } from "./evm/SwapBeforeBridge";

export enum RelayStatus {
Unfilled = "unfilled",
Expand All @@ -24,7 +25,10 @@ export enum RelayStatus {
}

@Entity()
@Unique("UK_relayHashInfo_relayHash", ["relayHash"])
@Unique("UK_relayHashInfo_relayHash_depositEvent", [
"relayHash",
"depositEventId",
])
@Index("IX_rhi_originChainId_depositId", ["originChainId", "depositId"])
@Index("IX_rhi_depositTxHash", ["depositTxHash"])
@Index("IX_rhi_origin_deadline_status", [
Expand All @@ -40,7 +44,7 @@ export class RelayHashInfo {
relayHash: string;

@Column({ type: "decimal" })
depositId: number;
depositId: string;

@Column()
originChainId: number;
Expand Down Expand Up @@ -84,6 +88,16 @@ export class RelayHashInfo {
})
slowFillRequestEvent: RequestedV3SlowFill;

@Column({ nullable: true })
swapBeforeBridgeEventId: number;

@OneToOne(() => SwapBeforeBridge, { nullable: true })
@JoinColumn({
name: "swapBeforeBridgeEventId",
foreignKeyConstraintName: "FK_relayHashInfo_swapBeforeBridgeEventId",
})
swapBeforeBridgeEvent: SwapBeforeBridge;

@Column()
fillDeadline: Date;

Expand All @@ -93,9 +107,26 @@ export class RelayHashInfo {
@Column({ nullable: true })
depositRefundTxHash: string;

// swap varas
@Column({ nullable: true, type: "decimal" })
swapFeeInputAmount: string;
@Column({ nullable: true, type: "decimal" })
swapFeeUsdAmount: string;
@Column({ nullable: true })
swapInputTokenName: string;
@Column({ nullable: true })
swapOutputTokenName: string;

@CreateDateColumn()
createdAt: Date;

@Column({ nullable: true, type: "decimal" })
bridgeFeeUsd: string;
@Column({ nullable: true, type: "decimal" })
inputPriceUsd: string;
@Column({ nullable: true, type: "decimal" })
outputPriceUsd: string;

@UpdateDateColumn()
updatedAt: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class FilledV3Relay {
relayHash: string;

@Column({ type: "decimal" })
depositId: number;
depositId: string;

@Column()
originChainId: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class RequestedSpeedUpV3Deposit {
originChainId: number;

@Column({ type: "decimal" })
depositId: number;
depositId: string;

@Column()
depositor: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RequestedV3SlowFill {
relayHash: string;

@Column({ type: "decimal" })
depositId: number;
depositId: string;

@Column()
originChainId: number;
Expand Down
60 changes: 60 additions & 0 deletions packages/indexer-database/src/entities/evm/SwapBeforeBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";

@Entity({ schema: "evm" })
@Unique("UK_swapBeforeBridge_chainId_blockHash_logIndex", [
"chainId",
"blockHash",
"logIndex",
])
export class SwapBeforeBridge {
@PrimaryGeneratedColumn()
id: number;

@Column()
swapToken: string;

@Column()
acrossInputToken: string;

@Column()
acrossOutputToken: string;

@Column({ type: "decimal" })
swapTokenAmount: string;

@Column({ type: "decimal" })
acrossInputAmount: string;

@Column({ type: "decimal" })
acrossOutputAmount: string;

@Column()
exchange: string;

@Column()
blockHash: string;

@Column()
blockNumber: number;

@Column()
transactionHash: string;

@Column()
logIndex: number;

@Column()
chainId: number;

@Column()
finalised: boolean;

@CreateDateColumn()
createdAt: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
} from "typeorm";

@Entity({ schema: "evm" })
@Unique("UK_v3FundsDeposited_depositId_originChainId", [
"depositId",
"originChainId",
@Unique("UK_v3FundsDeposited_relayHash_block_logIdx", [
"relayHash",
"blockNumber",
"logIndex",
])
export class V3FundsDeposited {
@PrimaryGeneratedColumn()
Expand All @@ -19,7 +20,7 @@ export class V3FundsDeposited {
relayHash: string;

@Column({ type: "decimal" })
depositId: number;
depositId: string;

@Column()
originChainId: number;
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-database/src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "./evm/RequestedSpeedUpV3Deposit";
export * from "./evm/RelayedRootBundle";
export * from "./evm/ExecutedRelayerRefundRoot";
export * from "./evm/TokensBridged";
export * from "./evm/SwapBeforeBridge";

// Others
export * from "./Bundle";
Expand All @@ -24,3 +25,4 @@ export * from "./WebhookRequest";
export * from "./WebhookClient";

export * from "./IndexerProgressInfo";
export * from "./HistoricPrice";
14 changes: 12 additions & 2 deletions packages/indexer-database/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import "reflect-metadata";
import { DataSource, LessThan, Not, In } from "typeorm";
import {
DataSource,
InsertResult,
UpdateResult,
In,
LessThan,
Not,
} from "typeorm";
import * as entities from "./entities";
import { DatabaseConfig } from "./model";

export { DataSource, LessThan, Not, In };
export { DataSource, InsertResult, UpdateResult, In, LessThan, Not };

export const createDataSource = (config: DatabaseConfig): DataSource => {
return new DataSource({
Expand All @@ -29,6 +36,7 @@ export const createDataSource = (config: DatabaseConfig): DataSource => {
entities.RequestedV3SlowFill,
entities.TokensBridged,
entities.V3FundsDeposited,
entities.SwapBeforeBridge,
// Bundle
entities.Bundle,
entities.BundleBlockRange,
Expand All @@ -41,6 +49,8 @@ export const createDataSource = (config: DatabaseConfig): DataSource => {
entities.WebhookClient,
// Indexer
entities.IndexerProgressInfo,
// Historic Price
entities.HistoricPrice,
],
migrationsTableName: "_migrations",
migrations: ["migrations/*.ts"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm";

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

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "historic_price" (
"id" SERIAL NOT NULL,
"baseCurrency" character varying NOT NULL,
"quoteCurrency" character varying NOT NULL DEFAULT 'usd',
"date" date NOT NULL,
"price" double precision NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
CONSTRAINT "UK_hp_baseCurrency_quoteCurrency_date" UNIQUE ("baseCurrency", "quoteCurrency", "date"),
CONSTRAINT "PK_77dc3f4978cdfb03f1bb3a7444b" PRIMARY KEY ("id"))
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "historic_price"`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";

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

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "bridgeFeeUsd" double precision`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "inputPriceUsd" double precision`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "outputPriceUsd" double precision`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "bridgeFeeUsd"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "outputPriceUsd"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "inputPriceUsd"`,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { MigrationInterface, QueryRunner } from "typeorm";

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

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" DROP CONSTRAINT "UK_v3FundsDeposited_depositId_originChainId"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP CONSTRAINT "UK_relayHashInfo_relayHash"`,
);
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" ADD CONSTRAINT "UK_v3FundsDeposited_relayHash_block_logIdx" UNIQUE ("relayHash", "blockNumber", "logIndex")`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD CONSTRAINT "UK_relayHashInfo_relayHash_depositEvent" UNIQUE ("relayHash", "depositEventId")`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP CONSTRAINT "UK_relayHashInfo_relayHash_depositEvent"`,
);
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" DROP CONSTRAINT "UK_v3FundsDeposited_relayHash_block_logIdx"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD CONSTRAINT "UK_relayHashInfo_relayHash" UNIQUE ("relayHash")`,
);
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" ADD CONSTRAINT "UK_v3FundsDeposited_depositId_originChainId" UNIQUE ("depositId", "originChainId")`,
);
}
}
Loading