Skip to content

Commit

Permalink
fix: currency api uses the right keys
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 18, 2024
1 parent 1360f58 commit 0664f16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/swap/.env.manual
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ REDIS_HOST='0.0.0.0'
REDIS_PORT='6379'
MOCK_BTC_KES_RATE='8708520.117232416'
# CURRENCY_API_KEY='foo-bar-baz'
DATABASE_URL=postgresql://bs:password@0.0.0.0:5432/swap
DATABASE_URL=mongodb://bs:password@mongodb:27017
INTASEND_PUBLIC_KEY=ISPubKey_test_925ab885-f06d-4ace-8507-4186413a59a4
INTASEND_PRIVATE_KEY=ISSecretKey_test_3d887e44-33c4-4455-978e-d2ae7b10907d
FEDIMINT_CLIENTD_BASE_URL=http://0.0.0.0:7070
Expand Down
8 changes: 6 additions & 2 deletions apps/swap/src/fx/fx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/axios';
import { Currency, CustomStore } from '@bitsacco/common';
import {
Currency,
CustomStore,
mapToSupportedCurrency,
} from '@bitsacco/common';

interface CurrencyApiResponse {
meta: {
Expand Down Expand Up @@ -69,7 +73,7 @@ export class FxService {
const response = await firstValueFrom(
this.httpService
.get(
`https://api.currencyapi.com/v3/latest?apikey=${api_key}&base_currency=${baseCurrency}&currencies=${targetCurrency}`,
`https://api.currencyapi.com/v3/latest?apikey=${api_key}&base_currency=${mapToSupportedCurrency(baseCurrency)}&currencies=${mapToSupportedCurrency(targetCurrency)}`,
{
headers: {
'Content-Type': 'application/json',
Expand Down
21 changes: 20 additions & 1 deletion libs/common/src/utils/currency.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupportedCurrencies, Currency } from '../types';
import { mapToCurrency } from './currency';
import { mapToCurrency, mapToSupportedCurrency } from './currency';

describe('mapToCurrency', () => {
it('should map KES to Currency.KES', () => {
Expand All @@ -19,3 +19,22 @@ describe('mapToCurrency', () => {
}).toThrow('Unsupported currency: UNSUPPORTED');
});
});

describe('mapToSupportedCurrency', () => {
it('should map Currency.KES to SupportedCurrencies.KES', () => {
const result = mapToSupportedCurrency(Currency.KES);
expect(result).toBe(SupportedCurrencies.KES);
});

it('should map Currency.BTC to SupportedCurrencies.BTC', () => {
const result = mapToSupportedCurrency(Currency.BTC);
expect(result).toBe(SupportedCurrencies.BTC);
});

it('should throw an error for unsupported currencies', () => {
expect(() => {
// @ts-ignore - Intentionally passing an invalid value
mapToSupportedCurrency('UNSUPPORTED');
}).toThrow('Unsupported currency: UNSUPPORTED');
});
});
13 changes: 13 additions & 0 deletions libs/common/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ export function mapToCurrency(currency: SupportedCurrencyType): Currency {
}
}

export function mapToSupportedCurrency(
currency: Currency,
): SupportedCurrencyType {
switch (currency) {
case Currency.BTC:
return 'BTC' as SupportedCurrencyType;
case Currency.KES:
return 'KES' as SupportedCurrencyType;
default:
throw new Error(`Unsupported currency: ${currency}`);
}
}

export function fiatToBtc({
amountFiat,
btcToFiatRate,
Expand Down

0 comments on commit 0664f16

Please sign in to comment.