2020-10-13 10:27:52 +02:00
|
|
|
import logger from '../logger';
|
2020-11-15 08:22:47 +01:00
|
|
|
import axios from 'axios';
|
2021-01-06 17:31:33 +01:00
|
|
|
import { IConversionRates } from '../mempool.interfaces';
|
2021-08-29 21:30:11 +02:00
|
|
|
import config from '../config';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
class FiatConversion {
|
2021-01-06 17:31:33 +01:00
|
|
|
private conversionRates: IConversionRates = {
|
2020-11-22 10:48:55 +01:00
|
|
|
'USD': 0
|
2019-07-21 16:59:47 +02:00
|
|
|
};
|
2021-01-06 17:31:33 +01:00
|
|
|
private ratesChangedCallback: ((rates: IConversionRates) => void) | undefined;
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
2021-01-06 17:31:33 +01:00
|
|
|
public setProgressChangedCallback(fn: (rates: IConversionRates) => void) {
|
|
|
|
this.ratesChangedCallback = fn;
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:59:47 +02:00
|
|
|
public startService() {
|
2020-10-13 10:27:52 +02:00
|
|
|
logger.info('Starting currency rates service');
|
2021-08-29 21:30:11 +02:00
|
|
|
setInterval(this.updateCurrency.bind(this), 1000 * config.MEMPOOL.PRICE_FEED_UPDATE_INTERVAL);
|
2019-07-21 16:59:47 +02:00
|
|
|
this.updateCurrency();
|
|
|
|
}
|
|
|
|
|
2020-11-22 10:48:55 +01:00
|
|
|
public getConversionRates() {
|
|
|
|
return this.conversionRates;
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 08:22:47 +01:00
|
|
|
private async updateCurrency(): Promise<void> {
|
|
|
|
try {
|
2021-01-30 10:25:22 +01:00
|
|
|
const response = await axios.get('https://price.bisq.wiz.biz/getAllMarketPrices', { timeout: 10000 });
|
2020-11-22 10:48:55 +01:00
|
|
|
const usd = response.data.data.find((item: any) => item.currencyCode === 'USD');
|
|
|
|
this.conversionRates = {
|
|
|
|
'USD': usd.price,
|
|
|
|
};
|
2021-01-06 17:31:33 +01:00
|
|
|
if (this.ratesChangedCallback) {
|
|
|
|
this.ratesChangedCallback(this.conversionRates);
|
|
|
|
}
|
2020-11-15 08:22:47 +01:00
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
logger.err('Error updating fiat conversion rates: ' + (e instanceof Error ? e.message : e));
|
2020-11-15 08:22:47 +01:00
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new FiatConversion();
|