BlueWallet/models/fiatUnit.ts

99 lines
3.5 KiB
TypeScript
Raw Normal View History

import Frisbee from 'frisbee';
2021-07-18 15:51:40 +02:00
import untypedFiatUnit from './fiatUnits.json';
2021-07-18 15:51:40 +02:00
export const FiatUnitSource = {
CoinDesk: 'CoinDesk',
Yadio: 'Yadio',
BitcoinduLiban: 'BitcoinduLiban',
2021-07-06 16:07:47 +03:00
Exir: 'Exir',
2021-07-18 15:51:40 +02:00
} as const;
2021-07-18 15:51:40 +02:00
const RateExtractors = {
CoinDesk: async (ticker: string): Promise<number> => {
const api = new Frisbee({ baseURI: 'https://api.coindesk.com' });
const res = await api.get(`/v1/bpi/currentprice/${ticker}.json`);
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
let json;
try {
json = typeof res.body === 'string' ? JSON.parse(res.body) : res.body;
} catch (e) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.bpi?.[ticker]?.rate_float; // eslint-disable-line
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
2021-07-18 15:51:40 +02:00
Yadio: async (ticker: string): Promise<number> => {
const api = new Frisbee({ baseURI: 'https://api.yadio.io/json' });
const res = await api.get(`/${ticker}`);
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
let json;
try {
json = typeof res.body === 'string' ? JSON.parse(res.body) : res.body;
} catch (e) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.[ticker]?.price;
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
2021-07-18 15:51:40 +02:00
BitcoinduLiban: async (ticker: string): Promise<number> => {
const api = new Frisbee({ baseURI: 'https://bitcoinduliban.org' });
const res = await api.get('/api.php?key=lbpusd');
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
let json;
try {
json = typeof res.body === 'string' ? JSON.parse(res.body) : res.body;
} catch (e) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.[`BTC/${ticker}`];
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
2021-07-06 16:07:47 +03:00
rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
2021-07-18 15:51:40 +02:00
Exir: async (ticker: string): Promise<number> => {
2021-07-06 16:07:47 +03:00
const api = new Frisbee({ baseURI: 'https://api.exir.io' });
const res = await api.get('/v1/ticker?symbol=btc-irt');
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
let json;
try {
json = typeof res.body === 'string' ? JSON.parse(res.body) : res.body;
} catch (e) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.last;
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
2021-07-18 15:51:40 +02:00
} as const;
2020-11-21 19:16:20 -05:00
2021-07-18 15:51:40 +02:00
type FiatUnit = {
[key: string]: {
endPointKey: string;
symbol: string;
locale: string;
source: 'CoinDesk' | 'Yadio' | 'Exir' | 'BitcoinduLiban';
};
};
export const FiatUnit = untypedFiatUnit as FiatUnit;
2021-07-18 15:51:40 +02:00
export async function getFiatRate(ticker: string): Promise<number> {
return await RateExtractors[FiatUnit[ticker].source](ticker);
2020-11-20 21:47:13 -05:00
}