Show wallet balance for BTC instead of Gnosis Safe

This commit is contained in:
2022-05-24 16:18:47 +02:00
parent 44b631c688
commit e05449a7ff
5 changed files with 24 additions and 32 deletions
+17 -23
View File
@@ -1,44 +1,38 @@
import Service from '@ember/service';
import Service, { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array';
import { task } from 'ember-concurrency-decorators';
import config from 'kredits-web/config/environment';
const txServiceBaseUrl = `${config.gnosisSafe.txServiceHost}/api/v1/safes/${config.gnosisSafe.address}`;
export default class CommunityFundsService extends Service {
@service exchangeRates;
@tracked balancesLoaded = false;
@tracked balances = A([]);
@task
*fetchBalances () {
const uri = `${txServiceBaseUrl}/balances/usd/`;
yield fetch(uri).then(res => res.json())
.then(res => this.processBalances(res))
yield fetch(config.btcBalanceAPI).then(res => res.json())
.then(res => {
return this.processBalances(res);
})
.catch(err => {
console.log(`[community-funds] Fetching balances failed:`);
console.error(err);
});
}
processBalances (res) {
for (const balance of res) {
// Format and round the approximate USD value
const lang = navigator.language || navigator.userLanguage;
balance.balanceUsd = Math.round(balance.balanceUsd).toLocaleString(lang);
async processBalances (res) {
await this.exchangeRates.fetchRates();
// Format and round the approximate USD value
const lang = navigator.language || navigator.userLanguage;
const balanceUSD = res.confirmed_balance * this.exchangeRates.btcusd;
res.balanceUSD = Math.round(balanceUSD).toLocaleString(lang);
if (balance.token) {
// ERC20 token, has all meta data
this.balances.pushObject(balance);
} else {
// RBTC, missing meta data
this.balances.pushObject({
...balance,
...{ token: { name: 'RBTC', symbol: 'RBTC'} }
});
}
}
this.balances.pushObject({
...res,
...{ token: { name: 'BTC', symbol: 'BTC'} }
});
this.balancesLoaded = true;
}
+1 -1
View File
@@ -8,7 +8,7 @@ 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
return parseFloat(res.vwap); // Last 24 hours volume weighted average price
} catch(e) {
console.error('Could not fetch exchange rate from Bitstamp:', e);
return 0;