Skip to content

Commit

Permalink
fix: condition check for null or undefined value (#45)
Browse files Browse the repository at this point in the history
# 🤖 Linear

Closes ZKS-160

## Description

Fix error on `getTokenPrices` if condition check
  • Loading branch information
0xnigir1 authored Aug 9, 2024
1 parent d94bdd0 commit 427d1f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
6 changes: 1 addition & 5 deletions libs/pricing/src/services/coingecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions libs/pricing/test/unit/services/coingecko.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down

0 comments on commit 427d1f4

Please sign in to comment.