|
|
|
@ -30,6 +30,7 @@ import ms from 'ms'; |
|
|
|
export class ExchangeRateDataService { |
|
|
|
private currencies: string[] = []; |
|
|
|
private currencyPairs: DataGatheringItem[] = []; |
|
|
|
private derivedCurrencyFactors: { [currencyPair: string]: number } = {}; |
|
|
|
private exchangeRates: { [currencyPair: string]: number } = {}; |
|
|
|
|
|
|
|
public constructor( |
|
|
|
@ -135,8 +136,14 @@ export class ExchangeRateDataService { |
|
|
|
public async initialize() { |
|
|
|
this.currencies = await this.prepareCurrencies(); |
|
|
|
this.currencyPairs = []; |
|
|
|
this.derivedCurrencyFactors = {}; |
|
|
|
this.exchangeRates = {}; |
|
|
|
|
|
|
|
for (const { currency, factor, rootCurrency } of DERIVED_CURRENCIES) { |
|
|
|
this.derivedCurrencyFactors[`${currency}${rootCurrency}`] = 1 / factor; |
|
|
|
this.derivedCurrencyFactors[`${rootCurrency}${currency}`] = factor; |
|
|
|
} |
|
|
|
|
|
|
|
for (const { |
|
|
|
currency1, |
|
|
|
currency2, |
|
|
|
@ -266,10 +273,14 @@ export class ExchangeRateDataService { |
|
|
|
return this.toCurrency(aValue, aFromCurrency, aToCurrency); |
|
|
|
} |
|
|
|
|
|
|
|
const derivedCurrencyFactor = |
|
|
|
this.derivedCurrencyFactors[`${aFromCurrency}${aToCurrency}`]; |
|
|
|
let factor: number; |
|
|
|
|
|
|
|
if (aFromCurrency === aToCurrency) { |
|
|
|
factor = 1; |
|
|
|
} else if (derivedCurrencyFactor) { |
|
|
|
factor = derivedCurrencyFactor; |
|
|
|
} else { |
|
|
|
const dataSource = |
|
|
|
this.dataProviderService.getDataSourceForExchangeRates(); |
|
|
|
@ -357,9 +368,22 @@ export class ExchangeRateDataService { |
|
|
|
for (const date of dates) { |
|
|
|
factors[format(date, DATE_FORMAT)] = 1; |
|
|
|
} |
|
|
|
} else { |
|
|
|
const dataSource = |
|
|
|
this.dataProviderService.getDataSourceForExchangeRates(); |
|
|
|
|
|
|
|
return factors; |
|
|
|
} |
|
|
|
|
|
|
|
const derivedCurrencyFactor = |
|
|
|
this.derivedCurrencyFactors[`${currencyFrom}${currencyTo}`]; |
|
|
|
|
|
|
|
if (derivedCurrencyFactor) { |
|
|
|
for (const date of dates) { |
|
|
|
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor; |
|
|
|
} |
|
|
|
|
|
|
|
return factors; |
|
|
|
} |
|
|
|
|
|
|
|
const dataSource = this.dataProviderService.getDataSourceForExchangeRates(); |
|
|
|
const symbol = `${currencyFrom}${currencyTo}`; |
|
|
|
|
|
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
|
@ -389,8 +413,7 @@ export class ExchangeRateDataService { |
|
|
|
try { |
|
|
|
if (currencyFrom === DEFAULT_CURRENCY) { |
|
|
|
for (const date of dates) { |
|
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = |
|
|
|
1; |
|
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = 1; |
|
|
|
} |
|
|
|
} else { |
|
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
|
@ -440,9 +463,7 @@ export class ExchangeRateDataService { |
|
|
|
try { |
|
|
|
const factor = |
|
|
|
(1 / |
|
|
|
marketPriceBaseCurrencyFromCurrency[ |
|
|
|
format(date, DATE_FORMAT) |
|
|
|
]) * |
|
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)]) * |
|
|
|
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)]; |
|
|
|
|
|
|
|
if (isNaN(factor)) { |
|
|
|
@ -464,7 +485,6 @@ export class ExchangeRateDataService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return factors; |
|
|
|
} |
|
|
|
|