Create exchange rate service , use CORS proxy for requests
This commit is contained in:
@@ -10,6 +10,7 @@ import isValidAmount from 'kredits-web/utils/is-valid-amount';
|
||||
|
||||
export default class AddReimbursementComponent extends Component {
|
||||
@service kredits;
|
||||
@service exchangeRates;
|
||||
|
||||
@alias('kredits.contributorsSorted') contributors;
|
||||
|
||||
@@ -19,6 +20,11 @@ export default class AddReimbursementComponent extends Component {
|
||||
@tracked expenses = A([]);
|
||||
@tracked expenseFormVisible = true;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.exchangeRates.fetchRates();
|
||||
}
|
||||
|
||||
get isValidTotal () {
|
||||
return isValidAmount(this.total);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
<p>
|
||||
{{this.exchangeRates.btceur}}, {{this.exchangeRates.btcusd}}
|
||||
</p>
|
||||
<form onsubmit={{action "submit"}} novalidate>
|
||||
<label>
|
||||
<p class="label">Contributor:</p>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import Service from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import config from 'kredits-web/config/environment';
|
||||
|
||||
// Need to go through proxy for CORS headers
|
||||
const bitstampBaseUrl = `${config.corsProxy}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 class ExchangeRatesService extends Service {
|
||||
@tracked btceur = 0;
|
||||
@tracked btcusd = 0;
|
||||
|
||||
async fetchRates (source='bitstamp') {
|
||||
switch(source) {
|
||||
case 'bitstamp':
|
||||
this.btceur = await fetchFromBitstamp('btceur');
|
||||
this.btcusd = await fetchFromBitstamp('btcusd');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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