2021-09-24 21:09:48 +02:00
|
|
|
import { baseCurrency } from '@ghostfolio/common/config';
|
2021-07-28 16:11:19 +02:00
|
|
|
import { DATE_FORMAT, getYesterday } from '@ghostfolio/common/helper';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-09-24 21:09:48 +02:00
|
|
|
import { DataSource } from '@prisma/client';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { format } from 'date-fns';
|
2021-09-24 21:09:48 +02:00
|
|
|
import { isEmpty, isNumber, uniq } from 'lodash';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-14 16:55:40 +02:00
|
|
|
import { DataProviderService } from './data-provider/data-provider.service';
|
2021-09-18 19:32:22 +02:00
|
|
|
import { IDataGatheringItem } from './interfaces/interfaces';
|
2021-09-24 21:09:48 +02:00
|
|
|
import { PrismaService } from './prisma.service';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExchangeRateDataService {
|
2021-09-24 21:09:48 +02:00
|
|
|
private currencies: string[] = [];
|
2021-09-18 19:32:22 +02:00
|
|
|
private currencyPairs: IDataGatheringItem[] = [];
|
2021-08-14 11:06:21 +02:00
|
|
|
private exchangeRates: { [currencyPair: string]: number } = {};
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-09-24 21:09:48 +02:00
|
|
|
public constructor(
|
|
|
|
private readonly dataProviderService: DataProviderService,
|
|
|
|
private readonly prismaService: PrismaService
|
|
|
|
) {
|
2021-04-13 21:53:58 +02:00
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:09:48 +02:00
|
|
|
public getCurrencies() {
|
|
|
|
return this.currencies?.length > 0 ? this.currencies : [baseCurrency];
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCurrencyPairs() {
|
|
|
|
return this.currencyPairs;
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
public async initialize() {
|
2021-09-24 21:09:48 +02:00
|
|
|
this.currencies = await this.prepareCurrencies();
|
2021-08-14 11:06:21 +02:00
|
|
|
this.currencyPairs = [];
|
|
|
|
this.exchangeRates = {};
|
2021-04-18 19:06:54 +02:00
|
|
|
|
2021-09-24 21:09:48 +02:00
|
|
|
for (const {
|
|
|
|
currency1,
|
|
|
|
currency2,
|
|
|
|
dataSource
|
|
|
|
} of this.prepareCurrencyPairs(this.currencies)) {
|
2021-09-18 19:32:22 +02:00
|
|
|
this.addCurrencyPairs({ currency1, currency2, dataSource });
|
2021-08-14 11:06:21 +02:00
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
await this.loadCurrencies();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async loadCurrencies() {
|
|
|
|
const result = await this.dataProviderService.getHistorical(
|
2021-08-14 11:06:21 +02:00
|
|
|
this.currencyPairs,
|
2021-04-13 21:53:58 +02:00
|
|
|
'day',
|
|
|
|
getYesterday(),
|
|
|
|
getYesterday()
|
|
|
|
);
|
|
|
|
|
2021-08-24 21:09:02 +02:00
|
|
|
if (isEmpty(result)) {
|
|
|
|
// Load currencies directly from data provider as a fallback
|
|
|
|
// if historical data is not yet available
|
|
|
|
const historicalData = await this.dataProviderService.get(
|
2021-09-18 19:32:22 +02:00
|
|
|
this.currencyPairs.map(({ dataSource, symbol }) => {
|
|
|
|
return { dataSource, symbol };
|
2021-08-24 21:09:02 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.keys(historicalData).forEach((key) => {
|
|
|
|
result[key] = {
|
|
|
|
[format(getYesterday(), DATE_FORMAT)]: {
|
|
|
|
marketPrice: historicalData[key].marketPrice
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:06:54 +02:00
|
|
|
const resultExtended = result;
|
|
|
|
|
|
|
|
Object.keys(result).forEach((pair) => {
|
|
|
|
const [currency1, currency2] = pair.match(/.{1,3}/g);
|
|
|
|
const [date] = Object.keys(result[pair]);
|
|
|
|
|
|
|
|
// Calculate the opposite direction
|
|
|
|
resultExtended[`${currency2}${currency1}`] = {
|
|
|
|
[date]: {
|
|
|
|
marketPrice: 1 / result[pair][date].marketPrice
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-09-18 19:32:22 +02:00
|
|
|
this.currencyPairs.forEach(({ symbol }) => {
|
|
|
|
const [currency1, currency2] = symbol.match(/.{1,3}/g);
|
2021-07-28 16:11:19 +02:00
|
|
|
const date = format(getYesterday(), DATE_FORMAT);
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-09-18 19:32:22 +02:00
|
|
|
this.exchangeRates[symbol] = resultExtended[symbol]?.[date]?.marketPrice;
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-09-18 19:32:22 +02:00
|
|
|
if (!this.exchangeRates[symbol]) {
|
2021-04-18 19:06:54 +02:00
|
|
|
// Not found, calculate indirectly via USD
|
2021-09-18 19:32:22 +02:00
|
|
|
this.exchangeRates[symbol] =
|
2021-09-24 21:09:48 +02:00
|
|
|
resultExtended[`${currency1}${'USD'}`]?.[date]?.marketPrice *
|
|
|
|
resultExtended[`${'USD'}${currency2}`]?.[date]?.marketPrice;
|
2021-04-18 19:06:54 +02:00
|
|
|
|
|
|
|
// Calculate the opposite direction
|
2021-08-14 11:06:21 +02:00
|
|
|
this.exchangeRates[`${currency2}${currency1}`] =
|
2021-09-18 19:32:22 +02:00
|
|
|
1 / this.exchangeRates[symbol];
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public toCurrency(
|
|
|
|
aValue: number,
|
2021-09-24 21:09:48 +02:00
|
|
|
aFromCurrency: string,
|
|
|
|
aToCurrency: string
|
2021-04-13 21:53:58 +02:00
|
|
|
) {
|
2021-09-24 21:09:48 +02:00
|
|
|
const hasNaN = Object.values(this.exchangeRates).some((exchangeRate) => {
|
|
|
|
return isNaN(exchangeRate);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hasNaN) {
|
2021-04-18 19:06:54 +02:00
|
|
|
// Reinitialize if data is not loaded correctly
|
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
let factor = 1;
|
|
|
|
|
|
|
|
if (aFromCurrency !== aToCurrency) {
|
2021-08-14 11:06:21 +02:00
|
|
|
if (this.exchangeRates[`${aFromCurrency}${aToCurrency}`]) {
|
|
|
|
factor = this.exchangeRates[`${aFromCurrency}${aToCurrency}`];
|
|
|
|
} else {
|
|
|
|
// Calculate indirectly via USD
|
2021-09-24 21:09:48 +02:00
|
|
|
const factor1 = this.exchangeRates[`${aFromCurrency}${'USD'}`];
|
|
|
|
const factor2 = this.exchangeRates[`${'USD'}${aToCurrency}`];
|
2021-08-14 11:06:21 +02:00
|
|
|
|
|
|
|
factor = factor1 * factor2;
|
|
|
|
|
|
|
|
this.exchangeRates[`${aFromCurrency}${aToCurrency}`] = factor;
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:44:24 +02:00
|
|
|
if (isNumber(factor) && !isNaN(factor)) {
|
2021-08-08 19:24:51 +02:00
|
|
|
return factor * aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback with error, if currencies are not available
|
|
|
|
console.error(
|
|
|
|
`No exchange rate has been found for ${aFromCurrency}${aToCurrency}`
|
|
|
|
);
|
|
|
|
return aValue;
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
2021-04-18 19:06:54 +02:00
|
|
|
|
2021-09-18 19:32:22 +02:00
|
|
|
private addCurrencyPairs({
|
|
|
|
currency1,
|
|
|
|
currency2,
|
|
|
|
dataSource
|
|
|
|
}: {
|
2021-09-24 21:09:48 +02:00
|
|
|
currency1: string;
|
|
|
|
currency2: string;
|
2021-09-18 19:32:22 +02:00
|
|
|
dataSource: DataSource;
|
|
|
|
}) {
|
|
|
|
this.currencyPairs.push({
|
|
|
|
dataSource,
|
|
|
|
symbol: `${currency1}${currency2}`
|
|
|
|
});
|
|
|
|
this.currencyPairs.push({
|
|
|
|
dataSource,
|
|
|
|
symbol: `${currency2}${currency1}`
|
|
|
|
});
|
2021-04-18 19:06:54 +02:00
|
|
|
}
|
2021-09-24 21:09:48 +02:00
|
|
|
|
|
|
|
private async prepareCurrencies(): Promise<string[]> {
|
|
|
|
const currencies: string[] = [];
|
|
|
|
|
2021-09-25 16:45:21 +02:00
|
|
|
(
|
|
|
|
await this.prismaService.account.findMany({
|
|
|
|
distinct: ['currency'],
|
|
|
|
orderBy: [{ currency: 'asc' }],
|
|
|
|
select: { currency: true }
|
|
|
|
})
|
|
|
|
).forEach((account) => {
|
|
|
|
currencies.push(account.currency);
|
2021-09-24 21:09:48 +02:00
|
|
|
});
|
|
|
|
|
2021-09-25 16:45:21 +02:00
|
|
|
(
|
|
|
|
await this.prismaService.settings.findMany({
|
|
|
|
distinct: ['currency'],
|
|
|
|
orderBy: [{ currency: 'asc' }],
|
|
|
|
select: { currency: true }
|
|
|
|
})
|
|
|
|
).forEach((userSettings) => {
|
|
|
|
currencies.push(userSettings.currency);
|
2021-09-24 21:09:48 +02:00
|
|
|
});
|
|
|
|
|
2021-09-25 16:45:21 +02:00
|
|
|
(
|
|
|
|
await this.prismaService.symbolProfile.findMany({
|
|
|
|
distinct: ['currency'],
|
|
|
|
orderBy: [{ currency: 'asc' }],
|
|
|
|
select: { currency: true }
|
|
|
|
})
|
|
|
|
).forEach((symbolProfile) => {
|
|
|
|
currencies.push(symbolProfile.currency);
|
2021-09-24 21:09:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return uniq(currencies).sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
private prepareCurrencyPairs(aCurrencies: string[]) {
|
|
|
|
return aCurrencies
|
|
|
|
.filter((currency) => {
|
|
|
|
return currency !== baseCurrency;
|
|
|
|
})
|
|
|
|
.map((currency) => {
|
|
|
|
return {
|
|
|
|
currency1: baseCurrency,
|
|
|
|
currency2: currency,
|
2021-10-15 22:22:45 +02:00
|
|
|
dataSource: this.dataProviderService.getPrimaryDataSource(),
|
2021-09-24 21:09:48 +02:00
|
|
|
symbol: `${baseCurrency}${currency}`
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|