Add more fiat currencies using fx rates from FreeCurrencyAPI

This commit is contained in:
natsoni 2024-03-07 10:48:32 +01:00
parent bfaddfc345
commit f121d16544
No known key found for this signature in database
GPG Key ID: C65917583181743B
7 changed files with 727 additions and 38 deletions

View File

@ -7,7 +7,7 @@ import cpfpRepository from '../repositories/CpfpRepository';
import { RowDataPacket } from 'mysql2';
class DatabaseMigration {
private static currentVersion = 71;
private static currentVersion = 72;
private queryTimeout = 3600_000;
private statisticsAddedIndexed = false;
private uniqueLogs: string[] = [];
@ -606,6 +606,36 @@ class DatabaseMigration {
await this.$executeQuery('ALTER TABLE `federation_txos` ADD emergencyKey TINYINT NOT NULL DEFAULT 0');
await this.updateToSchemaVersion(71);
}
if (databaseSchemaVersion < 72) {
await this.$executeQuery('ALTER TABLE `prices` ADD `BGN` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `BRL` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `CNY` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `CZK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `DKK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `HKD` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `HRK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `HUF` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `IDR` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `ILS` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `INR` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `ISK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `KRW` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `MXN` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `MYR` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `NOK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `NZD` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `PHP` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `PLN` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `RON` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `RUB` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `SEK` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `SGD` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `THB` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `TRY` float DEFAULT "-1"');
await this.$executeQuery('ALTER TABLE `prices` ADD `ZAR` float DEFAULT "-1"');
await this.updateToSchemaVersion(72);
}
}
/**

View File

@ -1,5 +1,6 @@
import DB from '../database';
import logger from '../logger';
import config from '../config';
import priceUpdater from '../tasks/price-updater';
export interface ApiPrice {
@ -11,17 +12,81 @@ export interface ApiPrice {
CHF: number,
AUD: number,
JPY: number,
BGN: number,
BRL: number,
CNY: number,
CZK: number,
DKK: number,
HKD: number,
HRK: number,
HUF: number,
IDR: number,
ILS: number,
INR: number,
ISK: number,
KRW: number,
MXN: number,
MYR: number,
NOK: number,
NZD: number,
PHP: number,
PLN: number,
RON: number,
RUB: number,
SEK: number,
SGD: number,
THB: number,
TRY: number,
ZAR: number,
}
const ApiPriceFields = `
UNIX_TIMESTAMP(time) as time,
USD,
EUR,
GBP,
CAD,
CHF,
AUD,
JPY
`;
const ApiPriceFields = config.MEMPOOL.CURRENCY_API_KEY ?
`
UNIX_TIMESTAMP(time) as time,
USD,
EUR,
GBP,
CAD,
CHF,
AUD,
JPY,
BGN,
BRL,
CNY,
CZK,
DKK,
HKD,
HRK,
HUF,
IDR,
ILS,
INR,
ISK,
KRW,
MXN,
MYR,
NOK,
NZD,
PHP,
PLN,
RON,
RUB,
SEK,
SGD,
THB,
TRY,
ZAR
`:
`
UNIX_TIMESTAMP(time) as time,
USD,
EUR,
GBP,
CAD,
CHF,
AUD,
JPY
`;
export interface ExchangeRates {
USDEUR: number,
@ -30,6 +95,32 @@ export interface ExchangeRates {
USDCHF: number,
USDAUD: number,
USDJPY: number,
USDBGN?: number,
USDBRL?: number,
USDCNY?: number,
USDCZK?: number,
USDDKK?: number,
USDHKD?: number,
USDHRK?: number,
USDHUF?: number,
USDIDR?: number,
USDILS?: number,
USDINR?: number,
USDISK?: number,
USDKRW?: number,
USDMXN?: number,
USDMYR?: number,
USDNOK?: number,
USDNZD?: number,
USDPHP?: number,
USDPLN?: number,
USDRON?: number,
USDRUB?: number,
USDSEK?: number,
USDSGD?: number,
USDTHB?: number,
USDTRY?: number,
USDZAR?: number,
}
export interface Conversion {
@ -45,6 +136,32 @@ export const MAX_PRICES = {
CHF: 100000000,
AUD: 100000000,
JPY: 10000000000,
BGN: 1000000000,
BRL: 1000000000,
CNY: 1000000000,
CZK: 10000000000,
DKK: 1000000000,
HKD: 1000000000,
HRK: 1000000000,
HUF: 10000000000,
IDR: 100000000000,
ILS: 1000000000,
INR: 10000000000,
ISK: 10000000000,
KRW: 100000000000,
MXN: 1000000000,
MYR: 1000000000,
NOK: 1000000000,
NZD: 1000000000,
PHP: 10000000000,
PLN: 1000000000,
RON: 1000000000,
RUB: 10000000000,
SEK: 1000000000,
SGD: 100000000,
THB: 10000000000,
TRY: 10000000000,
ZAR: 10000000000,
};
class PricesRepository {
@ -64,17 +181,49 @@ class PricesRepository {
}
try {
await DB.query(`
INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY)
VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ? )`,
[time, prices.USD, prices.EUR, prices.GBP, prices.CAD, prices.CHF, prices.AUD, prices.JPY]
);
if (!config.MEMPOOL.CURRENCY_API_KEY) { // Store only the 7 main currencies
await DB.query(`
INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY)
VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ? )`,
[time, prices.USD, prices.EUR, prices.GBP, prices.CAD, prices.CHF, prices.AUD, prices.JPY]
);
} else { // Store all 7 main currencies + all the currencies obtained with the external API
await DB.query(`
INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY, BGN, BRL, CNY, CZK, DKK, HKD, HRK, HUF, IDR, ILS, INR, ISK, KRW, MXN, MYR, NOK, NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, TRY, ZAR)
VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ? )`,
[time, prices.USD, prices.EUR, prices.GBP, prices.CAD, prices.CHF, prices.AUD, prices.JPY, prices.BGN, prices.BRL, prices.CNY, prices.CZK, prices.DKK,
prices.HKD, prices.HRK, prices.HUF, prices.IDR, prices.ILS, prices.INR, prices.ISK, prices.KRW, prices.MXN, prices.MYR, prices.NOK, prices.NZD,
prices.PHP, prices.PLN, prices.RON, prices.RUB, prices.SEK, prices.SGD, prices.THB, prices.TRY, prices.ZAR]
);
}
} catch (e) {
logger.err(`Cannot save exchange rate into db. Reason: ` + (e instanceof Error ? e.message : e));
throw e;
}
}
public async $saveAdditionalCurrencyPrices(time: number, prices: ApiPrice, legacyCurrencies: string[]): Promise<void> {
try {
await DB.query(`
UPDATE prices
SET BGN = ?, BRL = ?, CNY = ?, CZK = ?, DKK = ?, HKD = ?, HRK = ?, HUF = ?, IDR = ?, ILS = ?, INR = ?, ISK = ?, KRW = ?, MXN = ?, MYR = ?, NOK = ?, NZD = ?, PHP = ?, PLN = ?, RON = ?, RUB = ?, SEK = ?, SGD = ?, THB = ?, TRY = ?, ZAR = ?
WHERE UNIX_TIMESTAMP(time) = ?`,
[prices.BGN, prices.BRL, prices.CNY, prices.CZK, prices.DKK, prices.HKD, prices.HRK, prices.HUF, prices.IDR, prices.ILS, prices.INR, prices.ISK, prices.KRW, prices.MXN, prices.MYR, prices.NOK, prices.NZD, prices.PHP, prices.PLN, prices.RON, prices.RUB, prices.SEK, prices.SGD, prices.THB, prices.TRY, prices.ZAR, time]
);
for (const currency of legacyCurrencies) {
await DB.query(`
UPDATE prices
SET ${currency} = ?
WHERE UNIX_TIMESTAMP(time) = ?`,
[prices[currency], time]
);
}
} catch (e) {
logger.err(`Cannot update exchange rate into db. Reason: ` + (e instanceof Error ? e.message : e));
throw e;
}
}
public async $getOldestPriceTime(): Promise<number> {
const [oldestRow] = await DB.query(`
SELECT UNIX_TIMESTAMP(time) AS time
@ -118,6 +267,28 @@ class PricesRepository {
return times.map(time => time.time);
}
public async $getPricesTimesWithMissingFields(): Promise<{time: number, USD: number, eur_missing: boolean, gbp_missing: boolean, cad_missing: boolean, chf_missing: boolean, aud_missing: boolean, jpy_missing: boolean}[]> {
const [times] = await DB.query(`
SELECT UNIX_TIMESTAMP(time) AS time,
USD,
CASE WHEN EUR = -1 THEN TRUE ELSE FALSE END AS eur_missing,
CASE WHEN GBP = -1 THEN TRUE ELSE FALSE END AS gbp_missing,
CASE WHEN CAD = -1 THEN TRUE ELSE FALSE END AS cad_missing,
CASE WHEN CHF = -1 THEN TRUE ELSE FALSE END AS chf_missing,
CASE WHEN AUD = -1 THEN TRUE ELSE FALSE END AS aud_missing,
CASE WHEN JPY = -1 THEN TRUE ELSE FALSE END AS jpy_missing
FROM prices
WHERE USD != -1
AND -1 IN (EUR, GBP, CAD, CHF, AUD, JPY, BGN, BRL, CNY, CZK, DKK, HKD, HRK, HUF, IDR, ILS, INR, ISK, KRW,
MXN, MYR, NOK, NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, TRY, ZAR)
ORDER BY time
`);
if (!Array.isArray(times)) {
return [];
}
return times as {time: number, USD: number, eur_missing: boolean, gbp_missing: boolean, cad_missing: boolean, chf_missing: boolean, aud_missing: boolean, jpy_missing: boolean}[];
}
public async $getPricesTimesAndId(): Promise<{time: number, id: number, USD: number}[]> {
const [times] = await DB.query(`
SELECT
@ -167,14 +338,48 @@ class PricesRepository {
const computeFx = (usd: number, other: number): number =>
Math.round(Math.max(other, 0) / Math.max(usd, 1) * 100) / 100;
const exchangeRates: ExchangeRates = {
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
};
const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ?
{
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
USDBGN: computeFx(latestPrice.USD, latestPrice.BGN),
USDBRL: computeFx(latestPrice.USD, latestPrice.BRL),
USDCNY: computeFx(latestPrice.USD, latestPrice.CNY),
USDCZK: computeFx(latestPrice.USD, latestPrice.CZK),
USDDKK: computeFx(latestPrice.USD, latestPrice.DKK),
USDHKD: computeFx(latestPrice.USD, latestPrice.HKD),
USDHRK: computeFx(latestPrice.USD, latestPrice.HRK),
USDHUF: computeFx(latestPrice.USD, latestPrice.HUF),
USDIDR: computeFx(latestPrice.USD, latestPrice.IDR),
USDILS: computeFx(latestPrice.USD, latestPrice.ILS),
USDINR: computeFx(latestPrice.USD, latestPrice.INR),
USDISK: computeFx(latestPrice.USD, latestPrice.ISK),
USDKRW: computeFx(latestPrice.USD, latestPrice.KRW),
USDMXN: computeFx(latestPrice.USD, latestPrice.MXN),
USDMYR: computeFx(latestPrice.USD, latestPrice.MYR),
USDNOK: computeFx(latestPrice.USD, latestPrice.NOK),
USDNZD: computeFx(latestPrice.USD, latestPrice.NZD),
USDPHP: computeFx(latestPrice.USD, latestPrice.PHP),
USDPLN: computeFx(latestPrice.USD, latestPrice.PLN),
USDRON: computeFx(latestPrice.USD, latestPrice.RON),
USDRUB: computeFx(latestPrice.USD, latestPrice.RUB),
USDSEK: computeFx(latestPrice.USD, latestPrice.SEK),
USDSGD: computeFx(latestPrice.USD, latestPrice.SGD),
USDTHB: computeFx(latestPrice.USD, latestPrice.THB),
USDTRY: computeFx(latestPrice.USD, latestPrice.TRY),
USDZAR: computeFx(latestPrice.USD, latestPrice.ZAR),
} : {
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
};
return {
prices: rates as ApiPrice[],
@ -206,14 +411,48 @@ class PricesRepository {
const computeFx = (usd: number, other: number): number =>
Math.round(Math.max(other, 0) / Math.max(usd, 1) * 100) / 100;
const exchangeRates: ExchangeRates = {
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
};
const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ?
{
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
USDBGN: computeFx(latestPrice.USD, latestPrice.BGN),
USDBRL: computeFx(latestPrice.USD, latestPrice.BRL),
USDCNY: computeFx(latestPrice.USD, latestPrice.CNY),
USDCZK: computeFx(latestPrice.USD, latestPrice.CZK),
USDDKK: computeFx(latestPrice.USD, latestPrice.DKK),
USDHKD: computeFx(latestPrice.USD, latestPrice.HKD),
USDHRK: computeFx(latestPrice.USD, latestPrice.HRK),
USDHUF: computeFx(latestPrice.USD, latestPrice.HUF),
USDIDR: computeFx(latestPrice.USD, latestPrice.IDR),
USDILS: computeFx(latestPrice.USD, latestPrice.ILS),
USDINR: computeFx(latestPrice.USD, latestPrice.INR),
USDISK: computeFx(latestPrice.USD, latestPrice.ISK),
USDKRW: computeFx(latestPrice.USD, latestPrice.KRW),
USDMXN: computeFx(latestPrice.USD, latestPrice.MXN),
USDMYR: computeFx(latestPrice.USD, latestPrice.MYR),
USDNOK: computeFx(latestPrice.USD, latestPrice.NOK),
USDNZD: computeFx(latestPrice.USD, latestPrice.NZD),
USDPHP: computeFx(latestPrice.USD, latestPrice.PHP),
USDPLN: computeFx(latestPrice.USD, latestPrice.PLN),
USDRON: computeFx(latestPrice.USD, latestPrice.RON),
USDRUB: computeFx(latestPrice.USD, latestPrice.RUB),
USDSEK: computeFx(latestPrice.USD, latestPrice.SEK),
USDSGD: computeFx(latestPrice.USD, latestPrice.SGD),
USDTHB: computeFx(latestPrice.USD, latestPrice.THB),
USDTRY: computeFx(latestPrice.USD, latestPrice.TRY),
USDZAR: computeFx(latestPrice.USD, latestPrice.ZAR),
} : {
USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
USDCAD: computeFx(latestPrice.USD, latestPrice.CAD),
USDCHF: computeFx(latestPrice.USD, latestPrice.CHF),
USDAUD: computeFx(latestPrice.USD, latestPrice.AUD),
USDJPY: computeFx(latestPrice.USD, latestPrice.JPY),
};
return {
prices: rates as ApiPrice[],

View File

@ -0,0 +1,72 @@
import { query } from '../../utils/axios-query';
import { ConversionFeed, ConversionRates } from '../price-updater';
const emptyRates = {
AUD: -1,
BGN: -1,
BRL: -1,
CAD: -1,
CHF: -1,
CNY: -1,
CZK: -1,
DKK: -1,
EUR: -1,
GBP: -1,
HKD: -1,
HRK: -1,
HUF: -1,
IDR: -1,
ILS: -1,
INR: -1,
ISK: -1,
JPY: -1,
KRW: -1,
MXN: -1,
MYR: -1,
NOK: -1,
NZD: -1,
PHP: -1,
PLN: -1,
RON: -1,
RUB: -1,
SEK: -1,
SGD: -1,
THB: -1,
TRY: -1,
ZAR: -1,
};
class FreeCurrencyApi implements ConversionFeed {
private API_KEY: string;
constructor(apiKey: string) {
this.API_KEY = apiKey;
}
public async $getQuota(): Promise<any> {
const response = await query(`https://api.freecurrencyapi.com/v1/status?apikey=${this.API_KEY}`);
if (response && response['quotas']) {
return response['quotas'];
}
return null;
}
public async $fetchLatestConversionRates(): Promise<ConversionRates> {
const response = await query(`https://api.freecurrencyapi.com/v1/latest?apikey=${this.API_KEY}`);
if (response && response['data']) {
return response['data'];
}
return emptyRates;
}
public async $fetchConversionRates(date: string): Promise<ConversionRates> {
const response = await query(`https://api.freecurrencyapi.com/v1/historical?date=${date}&apikey=${this.API_KEY}`);
if (response && response['data'] && response['data'][date]) {
return response['data'][date];
}
return emptyRates;
}
}
export default FreeCurrencyApi;

View File

@ -8,6 +8,7 @@ import BitflyerApi from './price-feeds/bitflyer-api';
import CoinbaseApi from './price-feeds/coinbase-api';
import GeminiApi from './price-feeds/gemini-api';
import KrakenApi from './price-feeds/kraken-api';
import FreeCurrencyApi from './price-feeds/free-currency-api';
export interface PriceFeed {
name: string;
@ -23,6 +24,16 @@ export interface PriceHistory {
[timestamp: number]: ApiPrice;
}
export interface ConversionFeed {
$getQuota(): Promise<any>;
$fetchLatestConversionRates(): Promise<any>;
$fetchConversionRates(date: string): Promise<any>;
}
export interface ConversionRates {
[currency: string]: number
}
function getMedian(arr: number[]): number {
const sortedArr = arr.slice().sort((a, b) => a - b);
const mid = Math.floor(sortedArr.length / 2);
@ -33,6 +44,8 @@ function getMedian(arr: number[]): number {
class PriceUpdater {
public historyInserted = false;
private additionalCurrenciesHistoryInserted = false;
private additionalCurrenciesHistoryRunning = false;
private timeBetweenUpdatesMs = 360_0000 / config.MEMPOOL.PRICE_UPDATES_PER_HOUR;
private cyclePosition = -1;
private firstRun = true;
@ -42,6 +55,10 @@ class PriceUpdater {
private feeds: PriceFeed[] = [];
private currencies: string[] = ['USD', 'EUR', 'GBP', 'CAD', 'CHF', 'AUD', 'JPY'];
private latestPrices: ApiPrice;
private currencyConversionFeed: ConversionFeed | undefined;
private newCurrencies: string[] = ['BGN', 'BRL', 'CNY', 'CZK', 'DKK', 'HKD', 'HRK', 'HUF', 'IDR', 'ILS', 'INR', 'ISK', 'KRW', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'ZAR'];
private lastTimeConversionsRatesFetched: number = 0;
private latestConversionsRatesFromFeed: ConversionRates = {};
private ratesChangedCallback: ((rates: ApiPrice) => void) | undefined;
constructor() {
@ -53,6 +70,7 @@ class PriceUpdater {
this.feeds.push(new BitfinexApi());
this.feeds.push(new GeminiApi());
this.currencyConversionFeed = new FreeCurrencyApi(config.MEMPOOL.CURRENCY_API_KEY);
this.setCyclePosition();
}
@ -70,6 +88,32 @@ class PriceUpdater {
CHF: -1,
AUD: -1,
JPY: -1,
BGN: -1,
BRL: -1,
CNY: -1,
CZK: -1,
DKK: -1,
HKD: -1,
HRK: -1,
HUF: -1,
IDR: -1,
ILS: -1,
INR: -1,
ISK: -1,
KRW: -1,
MXN: -1,
MYR: -1,
NOK: -1,
NZD: -1,
PHP: -1,
PLN: -1,
RON: -1,
RUB: -1,
SEK: -1,
SGD: -1,
THB: -1,
TRY: -1,
ZAR: -1,
};
}
@ -99,6 +143,18 @@ class PriceUpdater {
if ((Math.round(new Date().getTime() / 1000) - this.lastHistoricalRun) > 3600 * 24) {
// Once a day, look for missing prices (could happen due to network connectivity issues)
this.historyInserted = false;
this.additionalCurrenciesHistoryInserted = false;
}
if (config.MEMPOOL.CURRENCY_API_KEY && this.currencyConversionFeed && (Math.round(new Date().getTime() / 1000) - this.lastTimeConversionsRatesFetched) > 3600 * 24) {
// Once a day, fetch conversion rates from api: we don't need more granularity for fiat currencies and have a limited number of requests
try {
this.latestConversionsRatesFromFeed = await this.currencyConversionFeed.$fetchLatestConversionRates();
this.lastTimeConversionsRatesFetched = Math.round(new Date().getTime() / 1000);
logger.debug(`Fetched currencies conversion rates from external API: ${JSON.stringify(this.latestConversionsRatesFromFeed)}`);
} catch (e) {
logger.err(`Cannot fetch conversion rates from the API. Reason: ${(e instanceof Error ? e.message : e)}`);
}
}
try {
@ -106,6 +162,9 @@ class PriceUpdater {
if (this.historyInserted === false && config.DATABASE.ENABLED === true) {
await this.$insertHistoricalPrices();
}
if (this.additionalCurrenciesHistoryInserted === false && config.DATABASE.ENABLED === true && config.MEMPOOL.CURRENCY_API_KEY && !this.additionalCurrenciesHistoryRunning) {
await this.$insertMissingAdditionalPrices();
}
} catch (e: any) {
logger.err(`Cannot save BTC prices in db. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.mining);
}
@ -185,6 +244,14 @@ class PriceUpdater {
}
}
if (config.MEMPOOL.CURRENCY_API_KEY && this.latestPrices.USD > 0 && Object.keys(this.latestConversionsRatesFromFeed).length > 0) {
for (const conversionCurrency of this.newCurrencies) {
if (this.latestConversionsRatesFromFeed[conversionCurrency] > 0 && this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency] < MAX_PRICES[conversionCurrency]) {
this.latestPrices[conversionCurrency] = Math.round(this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency]);
}
}
}
if (config.DATABASE.ENABLED === true && this.cyclePosition === 0) {
// Save everything in db
try {
@ -320,6 +387,75 @@ class PriceUpdater {
logger.debug(`Inserted ${totalInserted} ${type === 'day' ? 'dai' : 'hour'}ly historical prices into the db`, logger.tags.mining);
}
}
/**
* Find missing prices for additional currencies and insert them in the database
* We calculate the additional prices from the USD price and the conversion rates
*/
private async $insertMissingAdditionalPrices(): Promise<void> {
this.additionalCurrenciesHistoryRunning = true;
const priceTimesToFill = await PricesRepository.$getPricesTimesWithMissingFields();
if (priceTimesToFill.length === 0) {
return;
}
logger.debug(`Fetching missing conversion rates from external API to fill ${priceTimesToFill.length} rows`, logger.tags.mining);
let conversionRates: { [timestamp: number]: ConversionRates } = {};
let totalInserted = 0;
let requestCounter = 0;
for (const priceTime of priceTimesToFill) {
const missingLegacyCurrencies = this.getMissingLegacyCurrencies(priceTime); // In the case a legacy currency (EUR, GBP, CAD, CHF, AUD, JPY)
const year = new Date(priceTime.time * 1000).getFullYear(); // is missing, we use the same process as for the new currencies
const yearTimestamp = new Date(year, 0, 1).getTime() / 1000;
if (conversionRates[yearTimestamp] === undefined) {
try {
if (requestCounter >= 10) {
await new Promise(resolve => setTimeout(resolve, 60_000)); // avoid getting 429'd
requestCounter = 0;
}
conversionRates[yearTimestamp] = await this.currencyConversionFeed?.$fetchConversionRates(`${year}-01-01`);
++requestCounter;
} catch (e) {
logger.err(`Cannot fetch conversion rates from the API for year ${year}. Reason: ${(e instanceof Error ? e.message : e)}`);
}
}
if (conversionRates[yearTimestamp] === undefined) {
continue;
}
const prices: ApiPrice = this.getEmptyPricesObj();
let willInsert = false;
for (const conversionCurrency of this.newCurrencies.concat(missingLegacyCurrencies)) {
if (conversionRates[yearTimestamp][conversionCurrency] > 0 && priceTime.USD * conversionRates[yearTimestamp][conversionCurrency] < MAX_PRICES[conversionCurrency]) {
prices[conversionCurrency] = year >= 2013 ? Math.round(priceTime.USD * conversionRates[yearTimestamp][conversionCurrency]) : Math.round(priceTime.USD * conversionRates[yearTimestamp][conversionCurrency] * 100) / 100;
willInsert = true;
}
}
if (willInsert) {
await PricesRepository.$saveAdditionalCurrencyPrices(priceTime.time, prices, missingLegacyCurrencies);
++totalInserted;
}
}
logger.debug(`Inserted ${totalInserted} missing additional currency prices into the db`, logger.tags.mining);
this.additionalCurrenciesHistoryInserted = true;
this.additionalCurrenciesHistoryRunning = false;
}
// Helper function to get legacy missing currencies in a row (EUR, GBP, CAD, CHF, AUD, JPY)
private getMissingLegacyCurrencies(priceTime: any): string[] {
const missingCurrencies: string[] = [];
['eur', 'gbp', 'cad', 'chf', 'aud', 'jpy'].forEach(currency => {
if (priceTime[`${currency}_missing`]) {
missingCurrencies.push(currency.toUpperCase());
}
});
return missingCurrencies;
}
}
export default new PriceUpdater();

View File

@ -268,4 +268,134 @@ export const fiatCurrencies = {
code: 'USD',
indexed: true,
},
BGN: {
name: 'Bulgarian Lev',
code: 'BGN',
indexed: true,
},
BRL: {
name: 'Brazilian Real',
code: 'BRL',
indexed: true,
},
CNY: {
name: 'Chinese Yuan',
code: 'CNY',
indexed: true,
},
CZK: {
name: 'Czech Koruna',
code: 'CZK',
indexed: true,
},
DKK: {
name: 'Danish Krone',
code: 'DKK',
indexed: true,
},
HKD: {
name: 'Hong Kong Dollar',
code: 'HKD',
indexed: true,
},
HRK: {
name: 'Croatian Kuna',
code: 'HRK',
indexed: true,
},
HUF: {
name: 'Hungarian Forint',
code: 'HUF',
indexed: true,
},
IDR: {
name: 'Indonesian Rupiah',
code: 'IDR',
indexed: true,
},
ILS: {
name: 'Israeli Shekel',
code: 'ILS',
indexed: true,
},
INR: {
name: 'Indian Rupee',
code: 'INR',
indexed: true,
},
ISK: {
name: 'Icelandic Krona',
code: 'ISK',
indexed: true,
},
KRW: {
name: 'South Korean Won',
code: 'KRW',
indexed: true,
},
MXN: {
name: 'Mexican Peso',
code: 'MXN',
indexed: true,
},
MYR: {
name: 'Malaysian Ringgit',
code: 'MYR',
indexed: true,
},
NOK: {
name: 'Norwegian Krone',
code: 'NOK',
indexed: true,
},
NZD: {
name: 'New Zealand Dollar',
code: 'NZD',
indexed: true,
},
PHP: {
name: 'Philippine Peso',
code: 'PHP',
indexed: true,
},
PLN: {
name: 'Polish Zloty',
code: 'PLN',
indexed: true,
},
RON: {
name: 'Romanian Leu',
code: 'RON',
indexed: true,
},
RUB: {
name: 'Russian Ruble',
code: 'RUB',
indexed: true,
},
SEK: {
name: 'Swedish Krona',
code: 'SEK',
indexed: true,
},
SGD: {
name: 'Singapore Dollar',
code: 'SGD',
indexed: true,
},
THB: {
name: 'Thai Baht',
code: 'THB',
indexed: true,
},
TRY: {
name: 'Turkish Lira',
code: 'TRY',
indexed: true,
},
ZAR: {
name: 'South African Rand',
code: 'ZAR',
indexed: true,
},
};

View File

@ -35,6 +35,11 @@ export class FiatSelectorComponent implements OnInit {
this.stateService.fiatCurrency$.subscribe((fiat) => {
this.fiatForm.get('fiat')?.setValue(fiat);
});
if (!this.stateService.env.ADDITIONAL_CURRENCIES) {
this.currencies = this.currencies.filter((currency: any) => {
return ['AUD', 'CAD', 'EUR', 'JPY', 'GBP', 'CHF', 'USD'].includes(currency[0]);
});
}
}
changeFiat() {

View File

@ -13,6 +13,32 @@ export interface ApiPrice {
CHF: number,
AUD: number,
JPY: number,
BGN?: number,
BRL?: number,
CNY?: number,
CZK?: number,
DKK?: number,
HKD?: number,
HRK?: number,
HUF?: number,
IDR?: number,
ILS?: number,
INR?: number,
ISK?: number,
KRW?: number,
MXN?: number,
MYR?: number,
NOK?: number,
NZD?: number,
PHP?: number,
PLN?: number,
RON?: number,
RUB?: number,
SEK?: number,
SGD?: number,
THB?: number,
TRY?: number,
ZAR?: number,
}
export interface ExchangeRates {
USDEUR: number,
@ -21,6 +47,32 @@ export interface ExchangeRates {
USDCHF: number,
USDAUD: number,
USDJPY: number,
USDBGN?: number,
USDBRL?: number,
USDCNY?: number,
USDCZK?: number,
USDDKK?: number,
USDHKD?: number,
USDHRK?: number,
USDHUF?: number,
USDIDR?: number,
USDILS?: number,
USDINR?: number,
USDISK?: number,
USDKRW?: number,
USDMXN?: number,
USDMYR?: number,
USDNOK?: number,
USDNZD?: number,
USDPHP?: number,
USDPLN?: number,
USDRON?: number,
USDRUB?: number,
USDSEK?: number,
USDSGD?: number,
USDTHB?: number,
USDTRY?: number,
USDZAR?: number,
}
export interface Conversion {
prices: ApiPrice[],
@ -60,10 +112,19 @@ export class PriceService {
getEmptyPrice(): Price {
return {
price: {
price: this.stateService.env.ADDITIONAL_CURRENCIES ? {
USD: 0, EUR: 0, GBP: 0, CAD: 0, CHF: 0, AUD: 0, JPY: 0, BGN: 0, BRL: 0, CNY: 0, CZK: 0, DKK: 0, HKD: 0, HRK: 0, HUF: 0, IDR: 0,
ILS: 0, INR: 0, ISK: 0, KRW: 0, MXN: 0, MYR: 0, NOK: 0, NZD: 0, PHP: 0, PLN: 0, RON: 0, RUB: 0, SEK: 0, SGD: 0, THB: 0, TRY: 0,
ZAR: 0
} :
{
USD: 0, EUR: 0, GBP: 0, CAD: 0, CHF: 0, AUD: 0, JPY: 0,
},
exchangeRates: {
exchangeRates: this.stateService.env.ADDITIONAL_CURRENCIES ? {
USDEUR: 0, USDGBP: 0, USDCAD: 0, USDCHF: 0, USDAUD: 0, USDJPY: 0, USDBGN: 0, USDBRL: 0, USDCNY: 0, USDCZK: 0, USDDKK: 0, USDHKD: 0,
USDHRK: 0, USDHUF: 0, USDIDR: 0, USDILS: 0, USDINR: 0, USDISK: 0, USDKRW: 0, USDMXN: 0, USDMYR: 0, USDNOK: 0, USDNZD: 0, USDPHP: 0,
USDPLN: 0, USDRON: 0, USDRUB: 0, USDSEK: 0, USDSGD: 0, USDTHB: 0, USDTRY: 0, USDZAR: 0
} : {
USDEUR: 0, USDGBP: 0, USDCAD: 0, USDCHF: 0, USDAUD: 0, USDJPY: 0,
},
};
@ -92,7 +153,17 @@ export class PriceService {
return undefined;
}
return {
price: {
price: this.stateService.env.ADDITIONAL_CURRENCIES ? {
USD: conversion.prices[0].USD, EUR: conversion.prices[0].EUR, GBP: conversion.prices[0].GBP, CAD: conversion.prices[0].CAD,
CHF: conversion.prices[0].CHF, AUD: conversion.prices[0].AUD, JPY: conversion.prices[0].JPY, BGN: conversion.prices[0].BGN,
BRL: conversion.prices[0].BRL, CNY: conversion.prices[0].CNY, CZK: conversion.prices[0].CZK, DKK: conversion.prices[0].DKK,
HKD: conversion.prices[0].HKD, HRK: conversion.prices[0].HRK, HUF: conversion.prices[0].HUF, IDR: conversion.prices[0].IDR,
ILS: conversion.prices[0].ILS, INR: conversion.prices[0].INR, ISK: conversion.prices[0].ISK, KRW: conversion.prices[0].KRW,
MXN: conversion.prices[0].MXN, MYR: conversion.prices[0].MYR, NOK: conversion.prices[0].NOK, NZD: conversion.prices[0].NZD,
PHP: conversion.prices[0].PHP, PLN: conversion.prices[0].PLN, RON: conversion.prices[0].RON, RUB: conversion.prices[0].RUB,
SEK: conversion.prices[0].SEK, SGD: conversion.prices[0].SGD, THB: conversion.prices[0].THB, TRY: conversion.prices[0].TRY,
ZAR: conversion.prices[0].ZAR
} : {
USD: conversion.prices[0].USD, EUR: conversion.prices[0].EUR, GBP: conversion.prices[0].GBP, CAD: conversion.prices[0].CAD,
CHF: conversion.prices[0].CHF, AUD: conversion.prices[0].AUD, JPY: conversion.prices[0].JPY
},
@ -122,9 +193,15 @@ export class PriceService {
exchangeRates: conversion.exchangeRates,
};
for (const price of conversion.prices) {
historicalPrice.prices[price.time] = {
USD: price.USD, EUR: price.EUR, GBP: price.GBP, CAD: price.CAD,
CHF: price.CHF, AUD: price.AUD, JPY: price.JPY
historicalPrice.prices[price.time] = this.stateService.env.ADDITIONAL_CURRENCIES ? {
USD: price.USD, EUR: price.EUR, GBP: price.GBP, CAD: price.CAD, CHF: price.CHF, AUD: price.AUD,
JPY: price.JPY, BGN: price.BGN, BRL: price.BRL, CNY: price.CNY, CZK: price.CZK, DKK: price.DKK,
HKD: price.HKD, HRK: price.HRK, HUF: price.HUF, IDR: price.IDR, ILS: price.ILS, INR: price.INR,
ISK: price.ISK, KRW: price.KRW, MXN: price.MXN, MYR: price.MYR, NOK: price.NOK, NZD: price.NZD,
PHP: price.PHP, PLN: price.PLN, RON: price.RON, RUB: price.RUB, SEK: price.SEK, SGD: price.SGD,
THB: price.THB, TRY: price.TRY, ZAR: price.ZAR
} : {
USD: price.USD, EUR: price.EUR, GBP: price.GBP, CAD: price.CAD, CHF: price.CHF, AUD: price.AUD, JPY: price.JPY
};
}