From f8eca95595bac92efa63538ff2021aa93c854096 Mon Sep 17 00:00:00 2001 From: woocash2 <59764862+woocash2@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:06:40 +0200 Subject: [PATCH] CMN-654: Fix volumes (include input and output) and scale according to decimals (#31) --- src/grapqhl/pools.ts | 6 ++++ src/models/pool.ts | 7 ++++ src/services/coingeckoIntegration.ts | 53 +++++++++++++++++++--------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/grapqhl/pools.ts b/src/grapqhl/pools.ts index f54c1a3..f1671bc 100644 --- a/src/grapqhl/pools.ts +++ b/src/grapqhl/pools.ts @@ -153,7 +153,9 @@ export async function pairSwapVolume( pool: poolId, // can't do 0n b/c it breaks in runtime with serialization error. amount0_in: 0 as unknown as bigint, + amount0_out: 0 as unknown as bigint, amount1_in: 0 as unknown as bigint, + amount1_out: 0 as unknown as bigint, }; const query = client.iterate({ query: pairSwapVolumeQuery(poolId, fromMillis, toMillis), @@ -212,7 +214,9 @@ function pairSwapVolumesQuery(fromMillis: bigint, toMillis: bigint): string { pairSwapVolumes(fromMillis: ${fromMillis}, toMillis: ${toMillis}) { pool amount0_in + amount0_out amount1_in + amount1_out } } `; @@ -228,7 +232,9 @@ function pairSwapVolumeQuery( pairSwapVolume(poolId: "${poolId}", fromMillis: ${fromMillis}, toMillis: ${toMillis}) { pool amount0_in + amount0_out amount1_in + amount1_out } } `; diff --git a/src/models/pool.ts b/src/models/pool.ts index 5307cd1..699b2cf 100644 --- a/src/models/pool.ts +++ b/src/models/pool.ts @@ -20,7 +20,14 @@ export interface PoolV2 { export interface PairSwapVolume { pool: string; amount0_in: bigint; + amount0_out: bigint; amount1_in: bigint; + amount1_out: bigint; +} + +export interface TotalPairSwapVolume { + token0Volume: number; + token1Volume: number; } export interface LowestHighestSwapPrice { diff --git a/src/services/coingeckoIntegration.ts b/src/services/coingeckoIntegration.ts index 766159d..77b7231 100644 --- a/src/services/coingeckoIntegration.ts +++ b/src/services/coingeckoIntegration.ts @@ -1,4 +1,9 @@ -import { LowestHighestSwapPrice, Pools, PoolV2 } from "../models/pool"; +import { + LowestHighestSwapPrice, + Pools, + PoolV2, + TotalPairSwapVolume, +} from "../models/pool"; import { PairSwapVolume } from "../models/pool"; import { UsdPriceCache, NamedUsdPriceCaches } from "./coingeckoPriceCache"; import { TokenInfoById } from "../models/tokens"; @@ -40,7 +45,7 @@ export class CoingeckoIntegration { pools.map((pool) => this.pools.lastPoolSwapPrice(pool, this.tokenInfo)), ); const poolVolumes = await Promise.all( - pools.map((pool) => this.pairVolume(pool.id)), + pools.map((pool) => this.pairVolume(pool, this.tokenInfo)), ); const lowestHighest = await Promise.all( pools.map((pool) => @@ -61,23 +66,39 @@ export class CoingeckoIntegration { return tickers; } - private async pairVolume(poolId: string): Promise { + private async pairVolume( + pool: PoolV2, + tokenInfo: TokenInfoById, + ): Promise { + // token0 volume, token1 volume + let volume = { + token0Volume: 0, + token1Volume: 0, + }; let now_millis = new Date().getTime(); let yesterday_millis = now_millis - DAY_IN_MILLIS; - const volume = await this.pools.poolSwapVolume( - poolId, + const poolVolume = await this.pools.poolSwapVolume( + pool.id, BigInt(yesterday_millis), BigInt(now_millis), ); - if (!volume) { - return { - pool: poolId, - amount0_in: 0n, - amount1_in: 0n, - }; - } else { - return volume; + if (poolVolume) { + let decimals0 = this.tokenInfo.getDecimals(pool.token0); + let decimals1 = this.tokenInfo.getDecimals(pool.token1); + if (decimals0 && decimals1) { + const vol0 = + Number(poolVolume.amount0_in + poolVolume.amount0_out) / + 10 ** decimals0; + const vol1 = + Number(poolVolume.amount1_in + poolVolume.amount1_out) / + 10 ** decimals1; + volume = { + token0Volume: vol0, + token1Volume: vol1, + }; + } } + return volume; } private async pairLowestHighestSwapPrice( @@ -105,7 +126,7 @@ export class CoingeckoIntegration { private poolToTicker( pool: PoolV2, - poolVolume: PairSwapVolume, + poolVolume: TotalPairSwapVolume, lastPrice: number | null, liquidityInUsd: number, lowestHighest: LowestHighestSwapPrice, @@ -116,8 +137,8 @@ export class CoingeckoIntegration { target_currency: pool.token1, pool_id: pool.id, last_price: lastPrice !== null ? lastPrice.toString() : null, - base_volume: poolVolume.amount0_in.toString(), - target_volume: poolVolume.amount1_in.toString(), + base_volume: poolVolume.token0Volume.toString(), + target_volume: poolVolume.token1Volume.toString(), liquidity_in_usd: liquidityInUsd.toString(), high: lowestHighest.max_price_0in !== null