From 6fb3155a182a458aa078a56f832e6285dea1f149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trung=20B=E1=BA=A1ch?= <66735837+trungbach@users.noreply.github.com> Date: Thu, 7 Mar 2024 17:49:39 +0700 Subject: [PATCH] Fix/volume latest 24h (#195) * fix: update api get volume pair * fix: fixed error accumulate swap ops and add testcase --- packages/oraidex-server/src/db-query.ts | 36 +++++++++++++- packages/oraidex-server/src/index.ts | 1 - .../oraidex-server/tests/db-query.spec.ts | 48 +++++++++++++++++++ packages/oraidex-sync/src/tx-parsing.ts | 2 +- 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/packages/oraidex-server/src/db-query.ts b/packages/oraidex-server/src/db-query.ts index bc3efc28..a065a13d 100644 --- a/packages/oraidex-server/src/db-query.ts +++ b/packages/oraidex-server/src/db-query.ts @@ -6,7 +6,13 @@ import { oraichainTokens, parseTokenInfoRawDenom } from "@oraichain/oraidex-common"; -import { DuckDb, PoolAmountHistory, SwapOperationData, parseAssetInfoOnlyDenom } from "@oraichain/oraidex-sync"; +import { + DuckDb, + PoolAmountHistory, + SwapOperationData, + getDate24hBeforeNow, + parseAssetInfoOnlyDenom +} from "@oraichain/oraidex-sync"; import { ARRANGED_PAIRS_CHART, AllPairsInfo, getAssetInfosFromPairString } from "./helper"; import "./polyfill"; import { CACHE_KEY, cache } from "./map-cache"; @@ -78,6 +84,19 @@ export class DbQuery { return result; } + async getSwapVolumeForPairByRangeTime(pair: string, then: number, now: number, basePriceInUsdt: number) { + const sql = `SELECT SUM(volume) AS value + FROM swap_ohlcv + WHERE pair = ? AND timestamp >= ? AND timestamp <= ?`; + const params = [pair, then, now]; + const result = await this.duckDb.conn.all(sql, ...params); + if (result.length === 0) return 0; + const swapVolume = new BigDecimal(Math.trunc(basePriceInUsdt * result[0].value)) + .div(10 ** CW20_DECIMALS) + .toNumber(); + return swapVolume; + } + async getSwapVolumeAllPair(query: GetHistoricalChart): Promise { const { type } = query; const promiseVolumes = ARRANGED_PAIRS_CHART.map((p) => { @@ -134,6 +153,21 @@ export class DbQuery { value: new BigDecimal(Math.trunc(basePriceInUsdt * item.value)).div(10 ** CW20_DECIMALS).toNumber() }); } + + // get volume latest 24h for the last record + const now = new Date(); + const then = getDate24hBeforeNow(now); + const swapVolumeLatest24h = await this.getSwapVolumeForPairByRangeTime( + pair, + Math.trunc(then.getTime() / 1000), + Math.trunc(now.getTime() / 1000), + basePriceInUsdt + ); + if (swapVolumeLatest24h) + swapVolume[swapVolume.length - 1] = { + ...swapVolume[swapVolume.length - 1], + value: swapVolumeLatest24h + }; return swapVolume; } diff --git a/packages/oraidex-server/src/index.ts b/packages/oraidex-server/src/index.ts index 0d60e7bd..3ffc92bd 100644 --- a/packages/oraidex-server/src/index.ts +++ b/packages/oraidex-server/src/index.ts @@ -226,7 +226,6 @@ app.get("/volume/v2/historical/chart", async (req, res) => { const then = startTime ? parseInt(startTime as string) : getSpecificDateBeforeNow(new Date(latestTimestamp * 1000), 25920000).getTime() / 1000; - console.dir({ then, latestTimestamp }, { depth: null }); const volumeInfos = []; for (const { asset_infos } of pairsOnlyDenom) { const volume = await duckDb.getVolumeRange(timeFrame, then, latestTimestamp, pairToString(asset_infos)); diff --git a/packages/oraidex-server/tests/db-query.spec.ts b/packages/oraidex-server/tests/db-query.spec.ts index 31186269..de144af4 100644 --- a/packages/oraidex-server/tests/db-query.spec.ts +++ b/packages/oraidex-server/tests/db-query.spec.ts @@ -367,4 +367,52 @@ describe("test-db-query", () => { expect(item.value).toEqual(expectedResult[index]); }); }); + + it.each<[string, number, number, number, number]>([["orai-usdt", 1710820213, 1710906613, 1, 2]])( + "test-getSwapVolumeForPairByRangeTime", + async (pair, then, now, basePriceInUsdt, expectedResult) => { + // setup + const duckdb = await DuckDb.create(":memory:"); + const dbQuery = new DbQuery(duckdb); + + await duckdb.createSwapOhlcv(); + await duckdb.insertOhlcv([ + { + uniqueKey: "1", + timestamp: 1708387200, // Tuesday, 20 February 2024 00:00:00 + pair, + volume: 1000000n, + open: 2, + close: 2, + low: 2, + high: 2 + }, + { + uniqueKey: "2", + timestamp: 1710906600, // Wednesday, 20 March 2024 03:50:13 + pair, + volume: 1000000n, + open: 2, + close: 2, + low: 2, + high: 2 + }, + { + uniqueKey: "3", + timestamp: 1710906613, // Wednesday, 20 March 2024 03:50:13 + pair, + volume: 1000000n, + open: 2, + close: 2, + low: 2, + high: 2 + } + ]); + + // act + const result = await dbQuery.getSwapVolumeForPairByRangeTime(pair, then, now, basePriceInUsdt); + // assert + expect(result).toEqual(expectedResult); + } + ); }); diff --git a/packages/oraidex-sync/src/tx-parsing.ts b/packages/oraidex-sync/src/tx-parsing.ts index 8135f2dd..0e503280 100644 --- a/packages/oraidex-sync/src/tx-parsing.ts +++ b/packages/oraidex-sync/src/tx-parsing.ts @@ -105,7 +105,7 @@ function extractSwapOperations(txData: BasicTxData, wasmAttributes: (readonly At commissionAmounts.push(attr.value); } else if (attr.key === "spread_amount") { spreadAmounts.push(attr.value); - } else if (attr.key === "to") { + } else if (attr.key === "receiver") { senders.push(attr.value); } }