BlueWallet/models/fiatUnit.ts

106 lines
3.4 KiB
TypeScript
Raw Normal View History

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',
2022-09-24 16:36:06 -04:00
YadioConvert: 'YadioConvert',
2021-07-06 16:07:47 +03:00
Exir: 'Exir',
2021-10-14 12:48:16 -04:00
wazirx: 'wazirx',
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> => {
let json;
try {
const res = await fetch(`https://api.coindesk.com/v1/bpi/currentprice/${ticker}.json`);
json = await res.json();
2022-01-27 11:20:25 -05:00
} catch (e: any) {
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;
},
Yadio: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.yadio.io/json/${ticker}`);
json = await res.json();
2022-01-27 11:20:25 -05:00
} catch (e: any) {
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;
},
2022-09-24 16:36:06 -04:00
YadioConvert: async (ticker: string): Promise<number> => {
let json;
try {
2022-09-24 16:36:06 -04:00
const res = await fetch(`https://api.yadio.io/convert/1/BTC/${ticker}`);
json = await res.json();
2022-01-27 11:20:25 -05:00
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
2022-09-24 16:36:06 -04:00
let rate = json?.rate;
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;
},
Exir: async (ticker: string): Promise<number> => {
2021-07-06 16:07:47 +03:00
let json;
try {
const res = await fetch('https://api.exir.io/v1/ticker?symbol=btc-irt');
json = await res.json();
2022-01-27 11:20:25 -05:00
} catch (e: any) {
2021-07-06 16:07:47 +03:00
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-10-14 12:48:16 -04:00
wazirx: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.wazirx.com/api/v2/tickers/btcinr`);
json = await res.json();
2022-01-27 11:20:25 -05:00
} catch (e: any) {
2021-10-14 12:48:16 -04:00
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.ticker?.buy; // 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
} 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;
2022-09-24 16:36:06 -04:00
source: 'CoinDesk' | 'Yadio' | 'Exir' | 'wazirx';
2021-07-18 15:51:40 +02:00
};
};
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
}