From 427d1f4240148711a820cbcc3b0a232068b9097d Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Fri, 9 Aug 2024 15:17:16 -0300 Subject: [PATCH] fix: condition check for null or undefined value (#45) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 🤖 Linear Closes ZKS-160 ## Description Fix error on `getTokenPrices` if condition check --- .../pricing/src/services/coingecko.service.ts | 6 +--- .../unit/services/coingecko.service.spec.ts | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/libs/pricing/src/services/coingecko.service.ts b/libs/pricing/src/services/coingecko.service.ts index f1b64c6..205f241 100644 --- a/libs/pricing/src/services/coingecko.service.ts +++ b/libs/pricing/src/services/coingecko.service.ts @@ -54,7 +54,7 @@ export class CoingeckoService implements IPricingService { const missingTokenIds: string[] = []; const cachedMap = cachedTokenPrices.reduce( (result, price, index) => { - if (price !== null) result[tokenIds.at(index) as string] = price; + if (price) result[tokenIds.at(index) as string] = price; else missingTokenIds.push(tokenIds.at(index) as string); return result; @@ -69,10 +69,6 @@ export class CoingeckoService implements IPricingService { return { ...cachedMap, ...missingTokenPrices }; } - private formatTokenCacheKey(tokenId: string, currency: string) { - return `${tokenId}.${currency}`; - } - /** * Retrieves multiple token prices from the cache at once. * @param keys - An array of cache keys. diff --git a/libs/pricing/test/unit/services/coingecko.service.spec.ts b/libs/pricing/test/unit/services/coingecko.service.spec.ts index 0fa0436..cb6260a 100644 --- a/libs/pricing/test/unit/services/coingecko.service.spec.ts +++ b/libs/pricing/test/unit/services/coingecko.service.spec.ts @@ -112,6 +112,34 @@ describe("CoingeckoService", () => { ]); }); + it("fetches cached prices and missing from coingecko", async () => { + const tokenIds = ["token1", "token2"]; + const expectedResponse: TokenPrices = { + token2: { usd: 4.56 }, + }; + + jest.spyOn(cache.store, "mget").mockResolvedValueOnce([1.25, undefined]); + jest.spyOn(axios, "get").mockResolvedValueOnce({ + data: expectedResponse, + }); + + const result = await service.getTokenPrices(tokenIds); + + expect(result).toEqual({ + token1: 1.25, + token2: 4.56, + }); + expect(axios.get).toHaveBeenCalledWith(`simple/price`, { + params: { + vs_currencies: BASE_CURRENCY, + ids: "token2", + precision: DECIMALS_PRECISION.toString(), + }, + }); + expect(cache.store.mget).toHaveBeenCalledWith("token1", "token2"); + expect(cache.store.mset).toHaveBeenCalledWith([["token2", 4.56]]); + }); + it("throw ApiNotAvailable when Coingecko returns a 500 family exception", async () => { const tokenIds = ["token1", "token2"];