Skip to content

Commit

Permalink
CMN-654: Fix low high (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
woocash2 authored Jul 8, 2024
1 parent f8eca95 commit e8f4465
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 42 deletions.
51 changes: 24 additions & 27 deletions src/grapqhl/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PairSwapVolume,
PoolV2,
SwapAmounts,
TotalLowestHighestSwapPrice,
} from "../models/pool";
import { Observable, mergeMap } from "rxjs";
import { poolsV2ConnectionsQuery as poolReservesV2 } from "./v2/queries";
Expand Down Expand Up @@ -73,12 +74,7 @@ export async function pairLowestHighestSwapPrice(
tokenInfo: TokenInfoById,
fromMillis: bigint,
toMillis: bigint,
): Promise<LowestHighestSwapPrice> {
let swapPrice: LowestHighestSwapPrice = {
pool: pool.id,
min_price_0in: null,
max_price_0in: null,
};
): Promise<LowestHighestSwapPrice | null> {
const query = client.iterate({
query: lowestHighestSwapsPriceQuery(pool.id, fromMillis, toMillis),
});
Expand All @@ -91,32 +87,31 @@ export async function pairLowestHighestSwapPrice(
if (swapPrices.length > 2 || swapPrices.length == 0) {
console.error(`Expected 1 or 2 swap prices, got ${swapPrices.length}`);
} else {
if (
swapPrices[0].min_price_0in === null ||
swapPrices[0].max_price_0in === null
) {
return swapPrice;
}
const decimals0 = tokenInfo.getDecimals(pool.token0);
const decimals1 = tokenInfo.getDecimals(pool.token1);
if (decimals0 && decimals1) {
const minPrice =
swapPrices[0].min_price_0in * 10 ** (decimals0 - decimals1);
const maxPrice =
swapPrices[0].max_price_0in * 10 ** (decimals0 - decimals1);
swapPrice = {
pool: pool.id,
min_price_0in: minPrice,
max_price_0in: maxPrice,
};
}
// If swapPrices[1] exists, then input prices are in swapPrices[0], and output prices are in
// swapPrices[1]. If swapPrices[1] doesn't exist then all prices must be null, which is also
// handled by the logic below. See the corresponding query in common-amm-indexer.
const minPrice0In = swapPrices[0].min_price_0in;
const maxPrice0In = swapPrices[0].max_price_0in;
const minPrice0Out = swapPrices[1]
? swapPrices[1].min_price_0out
: null;
const maxPrice0Out = swapPrices[1]
? swapPrices[1].max_price_0out
: null;

return {
pool: swapPrices[0].pool,
min_price_0in: minPrice0In,
max_price_0in: maxPrice0In,
min_price_0out: minPrice0Out,
max_price_0out: maxPrice0Out,
};
}
}
} catch (err) {
console.error(err);
}

return swapPrice;
return null;
}

/// Query all pair swap volumes from the GraphQL server.
Expand Down Expand Up @@ -202,7 +197,9 @@ function lowestHighestSwapsPriceQuery(
lowestHighestSwapPrice(poolId: "${poolId}", fromMillis: ${fromMillis}, toMillis: ${toMillis}) {
pool
min_price_0in
min_price_0out
max_price_0in
max_price_0out
}
}
`;
Expand Down
11 changes: 9 additions & 2 deletions src/models/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ export interface TotalPairSwapVolume {
export interface LowestHighestSwapPrice {
pool: string;
min_price_0in: number | null;
min_price_0out: number | null;
max_price_0in: number | null;
max_price_0out: number | null;
}

export interface TotalLowestHighestSwapPrice {
lowestPrice: number | null;
highestPrice: number | null;
}

export interface SwapAmounts {
Expand Down Expand Up @@ -98,7 +105,7 @@ export class Pools {
if (!this.graphqlClient) {
return null;
}
// We want to query the volumes for the nearest minut.
// We want to query the volumes for the nearest minute.
// This way we can leverage GraphQL caching functionality.
const fromNearestMinute = (fromMillis / 60000n) * 60000n;
const toNearestMinute = (toMillis / 60000n) * 60000n;
Expand All @@ -119,7 +126,7 @@ export class Pools {
if (!this.graphqlClient) {
return null;
}
// We want to query the volumes for the nearest minut.
// We want to query the volumes for the nearest minute.
// This way we can leverage GraphQL caching functionality.
const fromNearestMinute = (fromMillis / 60000n) * 60000n;
const toNearestMinute = (toMillis / 60000n) * 60000n;
Expand Down
51 changes: 38 additions & 13 deletions src/services/coingeckoIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
LowestHighestSwapPrice,
Pools,
PoolV2,
TotalLowestHighestSwapPrice,
TotalPairSwapVolume,
} from "../models/pool";
import { PairSwapVolume } from "../models/pool";
Expand Down Expand Up @@ -104,7 +105,11 @@ export class CoingeckoIntegration {
private async pairLowestHighestSwapPrice(
pool: PoolV2,
tokenInfo: TokenInfoById,
): Promise<LowestHighestSwapPrice> {
): Promise<TotalLowestHighestSwapPrice> {
let swapPrice: TotalLowestHighestSwapPrice = {
lowestPrice: null,
highestPrice: null,
};
let now_millis = new Date().getTime();
let yesterday_millis = now_millis - DAY_IN_MILLIS;
const price = await this.pools.poolLowestHighestSwapPrice(
Expand All @@ -113,23 +118,43 @@ export class CoingeckoIntegration {
BigInt(yesterday_millis),
BigInt(now_millis),
);
if (!price) {
return {
pool: pool.id,
min_price_0in: null,
max_price_0in: null,
const decimals0 = tokenInfo.getDecimals(pool.token0);
const decimals1 = tokenInfo.getDecimals(pool.token1);
if (price !== null && decimals0 && decimals1) {
let minPrice = price.min_price_0in;
let maxPrice = price.max_price_0in;
if (price.min_price_0out !== null) {
minPrice =
minPrice === null
? price.min_price_0out
: Math.min(minPrice, price.min_price_0out);
}
if (price.max_price_0out !== null) {
maxPrice =
maxPrice === null
? price.max_price_0out
: Math.max(maxPrice, price.max_price_0out);
}

// both are null, or both aren't
if (minPrice !== null && maxPrice !== null) {
minPrice *= 10 ** (decimals0 - decimals1);
maxPrice *= 10 ** (decimals0 - decimals1);
}
swapPrice = {
lowestPrice: minPrice,
highestPrice: maxPrice,
};
} else {
return price;
}
return swapPrice;
}

private poolToTicker(
pool: PoolV2,
poolVolume: TotalPairSwapVolume,
lastPrice: number | null,
liquidityInUsd: number,
lowestHighest: LowestHighestSwapPrice,
lowestHighest: TotalLowestHighestSwapPrice,
): Ticker {
return {
ticker_id: `${pool.token0}_${pool.token1}`,
Expand All @@ -141,12 +166,12 @@ export class CoingeckoIntegration {
target_volume: poolVolume.token1Volume.toString(),
liquidity_in_usd: liquidityInUsd.toString(),
high:
lowestHighest.max_price_0in !== null
? lowestHighest.max_price_0in.toString()
lowestHighest.highestPrice !== null
? lowestHighest.highestPrice.toString()
: null,
low:
lowestHighest.min_price_0in !== null
? lowestHighest.min_price_0in.toString()
lowestHighest.lowestPrice !== null
? lowestHighest.lowestPrice.toString()
: null,
};
}
Expand Down

0 comments on commit e8f4465

Please sign in to comment.