Add util for fetching bitcoin exchange rate
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user