Skip to content

Commit

Permalink
use sdk coingecko, use floats for prices
Browse files Browse the repository at this point in the history
Signed-off-by: david <david@umaproject.org>
  • Loading branch information
daywiss committed Jan 7, 2025
1 parent e7d59ca commit f40359a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/indexer-database/src/entities/HistoricPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class HistoricPrice {
@Column({ type: "date" })
date: Date;

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

@CreateDateColumn()
Expand Down
10 changes: 5 additions & 5 deletions packages/indexer-database/src/entities/RelayHashInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export class RelayHashInfo {
@CreateDateColumn()
createdAt: Date;

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

@UpdateDateColumn()
updatedAt: Date;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { MigrationInterface, QueryRunner } from "typeorm";

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

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

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "historic_price" DROP COLUMN "price"`);
await queryRunner.query(
`ALTER TABLE "historic_price" ADD "price" numeric NOT NULL`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "outputPriceUsd"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "outputPriceUsd" integer`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "inputPriceUsd"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "inputPriceUsd" integer`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" DROP COLUMN "bridgeFeeUsd"`,
);
await queryRunner.query(
`ALTER TABLE "relay_hash_info" ADD "bridgeFeeUsd" character varying`,
);
}
}
47 changes: 37 additions & 10 deletions packages/indexer/src/messaging/priceWorker.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import Redis from "ioredis";
import { DateTime } from "luxon";
import winston from "winston";
import { Job, Worker } from "bullmq";
import { DataSource, entities } from "@repo/indexer-database";
import { IndexerQueues } from "./service";
import { ethers } from "ethers";
import { yesterday, findTokenByAddress } from "../utils";
import { CoingeckoClient } from "../utils/coingeckoClient";
import { findTokenByAddress } from "../utils";
// import { CoingeckoClient } from "../utils/coingeckoClient";
import { RetryProvidersFactory } from "../web3/RetryProvidersFactory";
import { assert } from "@repo/error-handling";
import * as across from "@across-protocol/sdk";

export type PriceMessage = {
depositId: number;
originChainId: number;
};

// Convert now to a consistent price timestamp yesterday for lookup purposes
export function yesterday(now: Date) {
return DateTime.fromJSDate(now)
.minus({ days: 1 })
.set({ hour: 23, minute: 59, second: 0, millisecond: 0 })
.toJSDate();
}

/**
* This worker listens to the `PriceQuery` queue and processes each job by:
* - Retrieving the deposit and relay hash information from the database using the deposit ID and origin chain ID.
Expand All @@ -26,7 +36,7 @@ export type PriceMessage = {
*/
export class PriceWorker {
private worker: Worker;
private coingeckoClient: CoingeckoClient;
private coingeckoClient: across.coingecko.Coingecko;
private relayHashInfoRepository;
private depositRepository;
private historicPriceRepository;
Expand All @@ -36,7 +46,7 @@ export class PriceWorker {
private postgres: DataSource,
private logger: winston.Logger,
) {
this.coingeckoClient = new CoingeckoClient();
this.coingeckoClient = across.coingecko.Coingecko.get(logger);
this.relayHashInfoRepository = this.postgres.getRepository(
entities.RelayHashInfo,
);
Expand Down Expand Up @@ -66,14 +76,18 @@ export class PriceWorker {
},
});
// we have this price at this time in the db
if (cachedPrice) return Number(cachedPrice.price);
if (cachedPrice) {
return Number(cachedPrice.price);
}

const fetchedPrice = await this.coingeckoClient.getHistoricDailyPrice(
priceTime.getTime(),
// use the coingecko id to fetch basecurrency price in usd
tokenInfo.coingeckoId,
const cgFormattedDate =
DateTime.fromJSDate(priceTime).toFormat("dd-LL-yyyy");
const price = await this.coingeckoClient.getContractHistoricDayPrice(
address,
cgFormattedDate,
quoteCurrency,
chainId,
);
const price = fetchedPrice.market_data?.current_price[quoteCurrency];
assert(
price,
`Unable to fetch price for ${quoteCurrency} in ${baseCurrency}(${tokenInfo.coingeckoId}) at ${priceTime}`,
Expand Down Expand Up @@ -160,6 +174,19 @@ export class PriceWorker {
(info) => info.depositTxHash === (deposit && deposit.transactionHash),
);

if (
relayHashInfo?.bridgeFeeUsd &&
relayHashInfo?.inputPriceUsd &&
relayHashInfo?.outputPriceUsd
) {
const errorMessage = "Skipping already processed relay hash";
this.logger.error({
at: "PriceWorker",
message: errorMessage,
...params,
});
return;
}
const errorMessage =
"Failed to retrieve relay hash information or deposit record from the database.";

Expand Down
44 changes: 0 additions & 44 deletions packages/indexer/src/utils/coingeckoClient.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/indexer/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export * from "./contractUtils";
export * from "./contractFactoryUtils";
export * from "./bundleBuilderUtils";
export * from "./spokePoolUtils";
export * from "./coingeckoClient";
export * from "./currencyUtils";

0 comments on commit f40359a

Please sign in to comment.