forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiatUnit.ts
186 lines (166 loc) · 6.22 KB
/
fiatUnit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import untypedFiatUnit from './fiatUnits.json';
export const FiatUnitSource = {
Coinbase: 'Coinbase',
CoinDesk: 'CoinDesk',
CoinGecko: 'CoinGecko',
Yadio: 'Yadio',
YadioConvert: 'YadioConvert',
Exir: 'Exir',
wazirx: 'wazirx',
Bitstamp: 'Bitstamp',
BNR: 'BNR',
} as const;
const RateExtractors = {
Coinbase: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.coinbase.com/v2/prices/BTC-${ticker.toUpperCase()}/buy`);
json = await res.json();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.data?.amount;
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;
},
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();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.bpi?.[ticker]?.rate_float;
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;
},
CoinGecko: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${ticker.toLowerCase()}`);
json = await res.json();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
const rate = json?.bitcoin?.[ticker] || json?.bitcoin?.[ticker.toLowerCase()];
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
Bitstamp: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://www.bitstamp.net/api/v2/ticker/btc${ticker.toLowerCase()}`);
json = await res.json();
} catch (e: any) {
throw new Error(`Could not update rate from Bitstamp for ${ticker}: ${e.message}`);
}
if (Array.isArray(json)) {
throw new Error(`Unsupported ticker for Bitstamp: ${ticker}`);
}
let rate = +json?.last;
if (!rate) throw new Error(`Could not update rate from Bitstamp for ${ticker}: data is wrong`);
rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate from Bitstamp for ${ticker}: data is wrong`);
return rate;
},
BNR: async (): Promise<number> => {
try {
const response = await fetch('https://www.bnr.ro/nbrfxrates.xml');
const xmlData = await response.text();
// Fetching USD to RON rate
const pattern = /<Rate currency="USD">([\d.]+)<\/Rate>/;
const matches = xmlData.match(pattern);
if (matches && matches[1]) {
const usdToRonRate = parseFloat(matches[1]);
if (!isNaN(usdToRonRate) && usdToRonRate > 0) {
// Fetch BTC to USD rate using CoinGecko extractor
const btcToUsdRate = await RateExtractors.CoinGecko('USD');
// Convert BTC to RON using the USD to RON exchange rate
return btcToUsdRate * usdToRonRate;
}
}
throw new Error('Could not find a valid exchange rate for USD to RON');
} catch (error: any) {
throw new Error(`Could not fetch RON exchange rate: ${error.message}`);
}
},
Yadio: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.yadio.io/json/${ticker}`);
json = await res.json();
} 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;
},
YadioConvert: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.yadio.io/convert/1/BTC/${ticker}`);
json = await res.json();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.rate;
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;
},
Exir: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch('https://api.exir.io/v1/ticker?symbol=btc-irt');
json = await res.json();
} catch (e: any) {
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;
},
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();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.ticker?.buy;
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;
},
} as const;
type FiatUnit = {
[key: string]: {
endPointKey: string;
symbol: string;
locale: string;
source: 'CoinDesk' | 'Yadio' | 'Exir' | 'wazirx' | 'Bitstamp';
};
};
export const FiatUnit = untypedFiatUnit as FiatUnit;
export type FiatUnitType = {
endPointKey: string;
symbol: string;
locale: string;
source: keyof typeof FiatUnitSource;
};
export async function getFiatRate(ticker: string): Promise<number> {
return await RateExtractors[FiatUnit[ticker].source](ticker);
}