From 01b56975944749dc3a284c74b88e48f2009ef81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trung=20B=E1=BA=A1ch?= <66735837+trungbach@users.noreply.github.com> Date: Wed, 13 Mar 2024 15:14:35 +0700 Subject: [PATCH] fix: accumulate tvl with reduce (#203) --- packages/oraidex-server/src/db-query.ts | 4 ++-- packages/oraidex-server/tests/db-query.spec.ts | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/oraidex-server/src/db-query.ts b/packages/oraidex-server/src/db-query.ts index 21fa80b9..35b6f814 100644 --- a/packages/oraidex-server/src/db-query.ts +++ b/packages/oraidex-server/src/db-query.ts @@ -267,10 +267,10 @@ export class DbQuery { let latestLiquidityPools = 0; const poolsInfo = cache.get(CACHE_KEY.POOLS_INFO); if (poolsInfo) { - poolsInfo.reduce((acc, cur) => { + latestLiquidityPools = poolsInfo.reduce((acc, cur) => { acc += cur.totalLiquidity; return acc; - }, latestLiquidityPools); + }, 0); } else { latestLiquidityPools = await this.getLatestLiquidityPools(); } diff --git a/packages/oraidex-server/tests/db-query.spec.ts b/packages/oraidex-server/tests/db-query.spec.ts index d1ae4ac9..57cffb54 100644 --- a/packages/oraidex-server/tests/db-query.spec.ts +++ b/packages/oraidex-server/tests/db-query.spec.ts @@ -17,6 +17,10 @@ import { AllPairsInfo } from "../src/helper"; describe("test-db-query", () => { afterEach(jest.restoreAllMocks); + afterEach(() => { + cache.set(CACHE_KEY.POOLS_INFO, []); + }); + it("test-getSwapVolume-should-throw-error-when-input-pair-invalid", async () => { // arrange const input: GetHistoricalChart = { @@ -287,7 +291,6 @@ describe("test-db-query", () => { }); it.each<["day" | "week" | "month", number[]]>([ - ["day", [8, 6, 11]], ["week", [10, 11]], ["month", [10, 11]] ])("test-getLiquidityChartAllPools", async (type, expectedResult) => {