Add util for fetching bitcoin exchange rate

This commit is contained in:
2020-09-30 12:10:21 +02:00
parent 7288d75237
commit b5700092a8
4 changed files with 127 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
const bitstampBaseUrl = 'https://www.bitstamp.net/api/v2';
async function fetchFromBitstamp(currencyPair) {
try {
const res = await fetch(`${bitstampBaseUrl}/ticker/${currencyPair}/`).then(r => r.json());
return res.vwap; // Last 24 hours volume weighted average price
} catch(e) {
console.error('Could not fetch exchange rate from Bitstamp:', e);
return 0;
}
}
export default function fetchExchangeRate(currencyPair, source='bitstamp') {
if (!currencyPair) throw 'Currency pair required';
switch(source) {
case 'bitstamp':
return fetchFromBitstamp(currencyPair);
}
}