Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Dec 19, 2023
1 parent 9ff9e45 commit cd65d96
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('CodefiTokenPricesServiceV2', () => {
});
});

it('throws if one of the token addresses cannot be found in the response data', async () => {
it('should not throw if one of the token addresses cannot be found in the response data', async () => {
nock('https://price-api.metafi.codefi.network')
.get('/v2/chains/1/spot-prices')
.query({
Expand All @@ -74,13 +74,28 @@ describe('CodefiTokenPricesServiceV2', () => {
},
});

await expect(
new CodefiTokenPricesServiceV2().fetchTokenPrices({
chainId: '0x1',
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
const result = await new CodefiTokenPricesServiceV2().fetchTokenPrices({
chainId: '0x1',
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
currency: 'ETH',
});
expect(result).toStrictEqual({
'0xAAA': {
tokenAddress: '0xAAA',
value: undefined,
currency: 'ETH',
}),
).rejects.toThrow('Could not find price for "0xAAA" in "ETH"');
},
'0xBBB': {
tokenAddress: '0xBBB',
value: 33689.98134554716,
currency: 'ETH',
},
'0xCCC': {
tokenAddress: '0xCCC',
value: 148.1344197578456,
currency: 'ETH',
},
});
});

it('throws if the currency cannot be found in the response data', async () => {
Expand All @@ -100,13 +115,29 @@ describe('CodefiTokenPricesServiceV2', () => {
},
});

await expect(
new CodefiTokenPricesServiceV2().fetchTokenPrices({
chainId: '0x1',
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
const result = await new CodefiTokenPricesServiceV2().fetchTokenPrices({
chainId: '0x1',
tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'],
currency: 'ETH',
});

expect(result).toStrictEqual({
'0xAAA': {
tokenAddress: '0xAAA',
value: undefined,
currency: 'ETH',
}),
).rejects.toThrow('Could not find price for "0xAAA" in "ETH"');
},
'0xBBB': {
tokenAddress: '0xBBB',
value: 33689.98134554716,
currency: 'ETH',
},
'0xCCC': {
tokenAddress: '0xCCC',
value: 148.1344197578456,
currency: 'ETH',
},
});
});

it('throws if the request fails consistently', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ export class CodefiTokenPricesServiceV2
];

if (!price) {
throw new Error(
// console error instead of throwing to not interrupt the fetching of other tokens in case just one fails
console.error(
`Could not find price for "${tokenAddress}" in "${currency}"`,
);
}
Expand Down

0 comments on commit cd65d96

Please sign in to comment.