|
|
@ -30,7 +30,7 @@ import ms from 'ms'; |
|
|
export class ExchangeRateDataService { |
|
|
export class ExchangeRateDataService { |
|
|
private currencies: string[] = []; |
|
|
private currencies: string[] = []; |
|
|
private currencyPairs: DataGatheringItem[] = []; |
|
|
private currencyPairs: DataGatheringItem[] = []; |
|
|
private derivedCurrencyFactors = new Map<string, number>(); |
|
|
private derivedCurrencyFactors: { [currencyPair: string]: number } = {}; |
|
|
private exchangeRates: { [currencyPair: string]: number } = {}; |
|
|
private exchangeRates: { [currencyPair: string]: number } = {}; |
|
|
|
|
|
|
|
|
public constructor( |
|
|
public constructor( |
|
|
@ -136,13 +136,12 @@ export class ExchangeRateDataService { |
|
|
public async initialize() { |
|
|
public async initialize() { |
|
|
this.currencies = await this.prepareCurrencies(); |
|
|
this.currencies = await this.prepareCurrencies(); |
|
|
this.currencyPairs = []; |
|
|
this.currencyPairs = []; |
|
|
|
|
|
this.derivedCurrencyFactors = {}; |
|
|
this.exchangeRates = {}; |
|
|
this.exchangeRates = {}; |
|
|
this.derivedCurrencyFactors = new Map(); |
|
|
|
|
|
|
|
|
|
|
|
// Initialize derived currencies factor map in both directions
|
|
|
|
|
|
for (const { currency, factor, rootCurrency } of DERIVED_CURRENCIES) { |
|
|
for (const { currency, factor, rootCurrency } of DERIVED_CURRENCIES) { |
|
|
this.derivedCurrencyFactors.set(`${currency}${rootCurrency}`, 1 / factor); |
|
|
this.derivedCurrencyFactors[`${currency}${rootCurrency}`] = 1 / factor; |
|
|
this.derivedCurrencyFactors.set(`${rootCurrency}${currency}`, factor); |
|
|
this.derivedCurrencyFactors[`${rootCurrency}${currency}`] = factor; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (const { |
|
|
for (const { |
|
|
@ -274,17 +273,13 @@ export class ExchangeRateDataService { |
|
|
return this.toCurrency(aValue, aFromCurrency, aToCurrency); |
|
|
return this.toCurrency(aValue, aFromCurrency, aToCurrency); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const derivedCurrencyFactor = |
|
|
|
|
|
this.derivedCurrencyFactors[`${aFromCurrency}${aToCurrency}`]; |
|
|
let factor: number; |
|
|
let factor: number; |
|
|
|
|
|
|
|
|
if (aFromCurrency === aToCurrency) { |
|
|
if (aFromCurrency === aToCurrency) { |
|
|
factor = 1; |
|
|
factor = 1; |
|
|
} else { |
|
|
} else if (derivedCurrencyFactor) { |
|
|
const derivedCurrencyFactor = this.getDerivedCurrencyFactor( |
|
|
|
|
|
aFromCurrency, |
|
|
|
|
|
aToCurrency |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (derivedCurrencyFactor !== null) { |
|
|
|
|
|
factor = derivedCurrencyFactor; |
|
|
factor = derivedCurrencyFactor; |
|
|
} else { |
|
|
} else { |
|
|
const dataSource = |
|
|
const dataSource = |
|
|
@ -339,7 +334,6 @@ export class ExchangeRateDataService { |
|
|
marketPriceBaseCurrencyToCurrency; |
|
|
marketPriceBaseCurrencyToCurrency; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isNumber(factor) && !isNaN(factor)) { |
|
|
if (isNumber(factor) && !isNaN(factor)) { |
|
|
return factor * aValue; |
|
|
return factor * aValue; |
|
|
@ -374,27 +368,22 @@ export class ExchangeRateDataService { |
|
|
for (const date of dates) { |
|
|
for (const date of dates) { |
|
|
factors[format(date, DATE_FORMAT)] = 1; |
|
|
factors[format(date, DATE_FORMAT)] = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return factors; |
|
|
return factors; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Check if this is a conversion between a derived currency and its root currency
|
|
|
const derivedCurrencyFactor = |
|
|
const derivedCurrencyFactor = this.getDerivedCurrencyFactor( |
|
|
this.derivedCurrencyFactors[`${currencyFrom}${currencyTo}`]; |
|
|
currencyFrom, |
|
|
|
|
|
currencyTo |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (derivedCurrencyFactor !== null) { |
|
|
if (derivedCurrencyFactor) { |
|
|
// Use the fixed mathematical factor for derived currencies
|
|
|
|
|
|
for (const date of dates) { |
|
|
for (const date of dates) { |
|
|
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor; |
|
|
factors[format(date, DATE_FORMAT)] = derivedCurrencyFactor; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return factors; |
|
|
return factors; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Continue with standard exchange rate logic
|
|
|
const dataSource = this.dataProviderService.getDataSourceForExchangeRates(); |
|
|
{ |
|
|
|
|
|
const dataSource = |
|
|
|
|
|
this.dataProviderService.getDataSourceForExchangeRates(); |
|
|
|
|
|
const symbol = `${currencyFrom}${currencyTo}`; |
|
|
const symbol = `${currencyFrom}${currencyTo}`; |
|
|
|
|
|
|
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
@ -424,8 +413,7 @@ export class ExchangeRateDataService { |
|
|
try { |
|
|
try { |
|
|
if (currencyFrom === DEFAULT_CURRENCY) { |
|
|
if (currencyFrom === DEFAULT_CURRENCY) { |
|
|
for (const date of dates) { |
|
|
for (const date of dates) { |
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = |
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)] = 1; |
|
|
1; |
|
|
|
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
const marketData = await this.marketDataService.getRange({ |
|
|
@ -475,9 +463,7 @@ export class ExchangeRateDataService { |
|
|
try { |
|
|
try { |
|
|
const factor = |
|
|
const factor = |
|
|
(1 / |
|
|
(1 / |
|
|
marketPriceBaseCurrencyFromCurrency[ |
|
|
marketPriceBaseCurrencyFromCurrency[format(date, DATE_FORMAT)]) * |
|
|
format(date, DATE_FORMAT) |
|
|
|
|
|
]) * |
|
|
|
|
|
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)]; |
|
|
marketPriceBaseCurrencyToCurrency[format(date, DATE_FORMAT)]; |
|
|
|
|
|
|
|
|
if (isNaN(factor)) { |
|
|
if (isNaN(factor)) { |
|
|
@ -499,21 +485,10 @@ export class ExchangeRateDataService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return factors; |
|
|
return factors; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private getDerivedCurrencyFactor( |
|
|
|
|
|
currencyFrom: string, |
|
|
|
|
|
currencyTo: string |
|
|
|
|
|
): number | null { |
|
|
|
|
|
const factor = this.derivedCurrencyFactors.get( |
|
|
|
|
|
`${currencyFrom}${currencyTo}` |
|
|
|
|
|
); |
|
|
|
|
|
return factor ?? null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async prepareCurrencies(): Promise<string[]> { |
|
|
private async prepareCurrencies(): Promise<string[]> { |
|
|
let currencies: string[] = [DEFAULT_CURRENCY]; |
|
|
let currencies: string[] = [DEFAULT_CURRENCY]; |
|
|
|
|
|
|
|
|
|