Browse Source

Refactoring

pull/2834/head
Thomas Kaul 2 years ago
parent
commit
810f257dbf
  1. 15
      apps/api/src/app/benchmark/benchmark.service.ts
  2. 248
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

15
apps/api/src/app/benchmark/benchmark.service.ts

@ -235,14 +235,17 @@ export class BenchmarkService {
}) })
]); ]);
const exchangeRates = await this.exchangeRateDataService.getExchangeRates({ const exchangeRates =
startDate, await this.exchangeRateDataService.getExchangeRatesByCurrency({
currencyFrom: currentSymbolItem.currency, startDate,
currencyTo: userCurrency currencies: [currentSymbolItem.currency],
}); targetCurrency: userCurrency
});
const exchangeRateAtStartDate = const exchangeRateAtStartDate =
exchangeRates[format(startDate, DATE_FORMAT)]; exchangeRates[currentSymbolItem.currency]?.[
format(startDate, DATE_FORMAT)
];
if (!exchangeRateAtStartDate) { if (!exchangeRateAtStartDate) {
Logger.error( Logger.error(

248
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -57,7 +57,7 @@ export class ExchangeRateDataService {
[currency: string]: { [dateString: string]: number }; [currency: string]: { [dateString: string]: number };
} = {}; } = {};
for (let currency of uniq(currencies)) { for (let currency of currencies) {
exchangeRatesByCurrency[currency] = await this.getExchangeRates({ exchangeRatesByCurrency[currency] = await this.getExchangeRates({
startDate, startDate,
currencyFrom: currency, currencyFrom: currency,
@ -83,129 +83,6 @@ export class ExchangeRateDataService {
return exchangeRatesByCurrency; return exchangeRatesByCurrency;
} }
public async getExchangeRates({
currencyFrom,
currencyTo,
endDate = new Date(),
startDate
}: {
currencyFrom: string;
currencyTo: string;
endDate?: Date;
startDate: Date;
}) {
const dates = eachDayOfInterval({ end: endDate, start: startDate });
let factors: { [dateString: string]: number } = {};
if (currencyFrom === currencyTo) {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = 1;
}
} else {
const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
symbol
}
]
});
if (marketData?.length > 0) {
for (const { date, marketPrice } of marketData) {
factors[format(date, DATE_FORMAT)] = marketPrice;
}
} else {
// Calculate indirectly via base currency
let marketPriceBaseCurrencyFromCurrency: {
[dateString: string]: number;
} = {};
let marketPriceBaseCurrencyToCurrency: {
[dateString: string]: number;
} = {};
try {
if (currencyFrom === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
1;
}
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyFrom}`
}
]
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
}
} catch {}
try {
if (currencyTo === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1;
}
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: {
gte: startDate,
lt: endDate
},
uniqueAssets: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyTo}`
}
]
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
}
} catch {}
for (const date of dates) {
try {
const factor =
(1 /
marketPriceBaseCurrencyFromCurrency[
format(date, DATE_FORMAT)
]) *
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)];
factors[format(date, DATE_FORMAT)] = factor;
} catch {
Logger.error(
`No exchange rate has been found for ${currencyFrom}${currencyTo} at ${format(
date,
DATE_FORMAT
)}`,
'ExchangeRateDataService'
);
}
}
}
}
return factors;
}
public hasCurrencyPair(currency1: string, currency2: string) { public hasCurrencyPair(currency1: string, currency2: string) {
return this.currencyPairs.some(({ symbol }) => { return this.currencyPairs.some(({ symbol }) => {
return ( return (
@ -449,6 +326,129 @@ export class ExchangeRateDataService {
return undefined; return undefined;
} }
private async getExchangeRates({
currencyFrom,
currencyTo,
endDate = new Date(),
startDate
}: {
currencyFrom: string;
currencyTo: string;
endDate?: Date;
startDate: Date;
}) {
const dates = eachDayOfInterval({ end: endDate, start: startDate });
let factors: { [dateString: string]: number } = {};
if (currencyFrom === currencyTo) {
for (const date of dates) {
factors[format(date, DATE_FORMAT)] = 1;
}
} else {
const dataSource =
this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${currencyFrom}${currencyTo}`;
const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
symbol
}
]
});
if (marketData?.length > 0) {
for (const { date, marketPrice } of marketData) {
factors[format(date, DATE_FORMAT)] = marketPrice;
}
} else {
// Calculate indirectly via base currency
let marketPriceBaseCurrencyFromCurrency: {
[dateString: string]: number;
} = {};
let marketPriceBaseCurrencyToCurrency: {
[dateString: string]: number;
} = {};
try {
if (currencyFrom === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
1;
}
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: { gte: startDate, lt: endDate },
uniqueAssets: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyFrom}`
}
]
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
}
} catch {}
try {
if (currencyTo === DEFAULT_CURRENCY) {
for (const date of dates) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] = 1;
}
} else {
const marketData = await this.marketDataService.getRange({
dateQuery: {
gte: startDate,
lt: endDate
},
uniqueAssets: [
{
dataSource,
symbol: `${DEFAULT_CURRENCY}${currencyTo}`
}
]
});
for (const { date, marketPrice } of marketData) {
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)] =
marketPrice;
}
}
} catch {}
for (const date of dates) {
try {
const factor =
(1 /
marketPriceBaseCurrencyFromCurrency[
format(date, DATE_FORMAT)
]) *
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)];
factors[format(date, DATE_FORMAT)] = factor;
} catch {
Logger.error(
`No exchange rate has been found for ${currencyFrom}${currencyTo} at ${format(
date,
DATE_FORMAT
)}`,
'ExchangeRateDataService'
);
}
}
}
}
return factors;
}
private async prepareCurrencies(): Promise<string[]> { private async prepareCurrencies(): Promise<string[]> {
let currencies: string[] = []; let currencies: string[] = [];

Loading…
Cancel
Save