Skip to content

Commit

Permalink
Fix/volume latest 24h (#195)
Browse files Browse the repository at this point in the history
* fix: update api get volume pair

* fix: fixed error accumulate swap ops and add testcase
  • Loading branch information
trungbach authored Mar 7, 2024
1 parent 7322f7e commit 6fb3155
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
36 changes: 35 additions & 1 deletion packages/oraidex-server/src/db-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<HistoricalChartResponse[]> {
const { type } = query;
const promiseVolumes = ARRANGED_PAIRS_CHART.map((p) => {
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion packages/oraidex-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
48 changes: 48 additions & 0 deletions packages/oraidex-server/tests/db-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
});
2 changes: 1 addition & 1 deletion packages/oraidex-sync/src/tx-parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 6fb3155

Please sign in to comment.