Skip to content

Commit

Permalink
CMN-654: Fix volumes (include input and output) and scale according t…
Browse files Browse the repository at this point in the history
…o decimals (#31)
  • Loading branch information
woocash2 authored Jul 5, 2024
1 parent ec8e20f commit f8eca95
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/grapqhl/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
}
`;
Expand All @@ -228,7 +232,9 @@ function pairSwapVolumeQuery(
pairSwapVolume(poolId: "${poolId}", fromMillis: ${fromMillis}, toMillis: ${toMillis}) {
pool
amount0_in
amount0_out
amount1_in
amount1_out
}
}
`;
Expand Down
7 changes: 7 additions & 0 deletions src/models/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 37 additions & 16 deletions src/services/coingeckoIntegration.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) =>
Expand All @@ -61,23 +66,39 @@ export class CoingeckoIntegration {
return tickers;
}

private async pairVolume(poolId: string): Promise<PairSwapVolume> {
private async pairVolume(
pool: PoolV2,
tokenInfo: TokenInfoById,
): Promise<TotalPairSwapVolume> {
// 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(
Expand Down Expand Up @@ -105,7 +126,7 @@ export class CoingeckoIntegration {

private poolToTicker(
pool: PoolV2,
poolVolume: PairSwapVolume,
poolVolume: TotalPairSwapVolume,
lastPrice: number | null,
liquidityInUsd: number,
lowestHighest: LowestHighestSwapPrice,
Expand All @@ -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
Expand Down

0 comments on commit f8eca95

Please sign in to comment.