Skip to content

Commit

Permalink
fix: select proper currency types
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 18, 2024
1 parent 0664f16 commit e71da0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions apps/swap/src/fx/fx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Currency,
CustomStore,
mapToSupportedCurrency,
SupportedCurrencyType,
} from '@bitsacco/common';

interface CurrencyApiResponse {
Expand Down Expand Up @@ -36,8 +37,8 @@ export class FxService {
}

async getExchangeRate(
baseCurrency: Currency,
targetCurrency: Currency,
baseCurrency: SupportedCurrencyType,
targetCurrency: SupportedCurrencyType,
): Promise<number> {
const cacheKey = `${baseCurrency}-${targetCurrency}`;

Expand Down Expand Up @@ -73,7 +74,7 @@ export class FxService {
const response = await firstValueFrom(
this.httpService
.get(
`https://api.currencyapi.com/v3/latest?apikey=${api_key}&base_currency=${mapToSupportedCurrency(baseCurrency)}&currencies=${mapToSupportedCurrency(targetCurrency)}`,
`https://api.currencyapi.com/v3/latest?apikey=${api_key}&base_currency=${baseCurrency}&currencies=${targetCurrency}`,
{
headers: {
'Content-Type': 'application/json',
Expand All @@ -100,7 +101,7 @@ export class FxService {
baseCurrency: Currency,
targetCurrency: Currency,
) {
const rate = await this.getExchangeRate(baseCurrency, targetCurrency);
const rate = await this.getExchangeRate(mapToSupportedCurrency(baseCurrency), mapToSupportedCurrency(targetCurrency));
return 1 / rate;
}
}
5 changes: 3 additions & 2 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type ReceivePaymentSuccessEvent,
fiatToBtc,
btcToFiat,
SupportedCurrencyType,
} from '@bitsacco/common';
import { v4 as uuidv4 } from 'uuid';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
Expand Down Expand Up @@ -72,8 +73,8 @@ export class SwapService {

let convertedAmount: string | undefined;
const fxRate = await this.fxService.getExchangeRate(
Currency.BTC,
Currency.KES,
'BTC' as SupportedCurrencyType,
'KES' as SupportedCurrencyType,
);

if (from === Currency.KES && to === Currency.BTC && amount) {
Expand Down

0 comments on commit e71da0d

Please sign in to comment.