Skip to content

Commit

Permalink
Merge pull request #217 from 67P/feature/budget-balances
Browse files Browse the repository at this point in the history
Use new balance APIs from akkounts, add Lightning balance
  • Loading branch information
raucao authored Jan 14, 2024
2 parents 26f2f2a + dbedf1d commit 895bf56
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 18 deletions.
7 changes: 5 additions & 2 deletions app/components/budget-balances/component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';

export default class BudgetBalancesComponent extends Component {
@service communityFunds;
@alias('communityFunds.balances') balances;

get balancesSorted () {
return this.communityFunds.balances
.sortBy('confirmed_balance').reverse();
}

get loading () {
return !this.communityFunds.balancesLoaded;
Expand Down
16 changes: 12 additions & 4 deletions app/components/budget-balances/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
</tr>
</thead>
<tbody>
{{#each this.balances as |balance|}}
{{#each this.balancesSorted as |balance|}}
<tr>
<th>{{balance.token.symbol}}</th>
<td class="amount">{{balance.confirmed_balance}}</td>
<td class="fiat-amount">~{{balance.balanceUSD}} USD</td>
<th>
<img src={{balance.token.icon}}
alt={{balance.token.description}}
title={{balance.token.description}} />
</th>
<td class="amount">
{{fmt-number balance.confirmed_balance}} <span class="unit">sats</span>
</td>
<td class="fiat-amount">
~{{balance.balanceUSD}} USD
</td>
</tr>
{{/each}}
</tbody>
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/fmt-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { helper } from '@ember/component/helper';

export default helper(function fmtNumber(number) {
const lang = navigator.language || navigator.userLanguage;
return number.toLocaleString(lang);
});
34 changes: 24 additions & 10 deletions app/services/community-funds.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { tracked } from '@glimmer/tracking';
import Service, { inject as service } from '@ember/service';
import config from 'kredits-web/config/environment';

export default class CommunityFundsService extends Service {
Expand All @@ -12,28 +12,42 @@ export default class CommunityFundsService extends Service {

@task
*fetchBalances () {
yield fetch(config.btcBalanceAPI).then(res => res.json())
.then(res => {
return this.processBalances(res);
const promises = [];
const balances = config.communityFundsAPI.balances;

for (const item of Object.keys(balances)) {
const c = balances[item];
promises.push(
this.fetchBalance(c.url)
.then(res => { return this.processBalance(res, c) })
)
}

yield Promise.all(promises)
.then(() => {
this.balancesLoaded = true;
})
.catch(err => {
console.log(`[community-funds] Fetching balances failed:`);
console.error(err);
});
}

async processBalances (res) {
async fetchBalance(url) {
return fetch(url).then(res => res.json());
}

async processBalance (res, config) {
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;
const balanceUSD = (res.confirmed_balance / 100000000) * this.exchangeRates.btcusd;
res.balanceUSD = Math.round(balanceUSD).toLocaleString(lang);

this.balances.pushObject({
...res,
...{ token: { name: 'BTC', symbol: 'BTC'} }
...{ token: { icon: `/img/${config.icon}`, symbol: config.symbol, description: config.description } }
});

this.balancesLoaded = true;
}
}
6 changes: 6 additions & 0 deletions app/services/exchange-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export default class ExchangeRatesService extends Service {
@tracked btceur = 0;
@tracked btcusd = 0;

get exchangeRatesLoaded () {
return (this.btceur !== 0) && (this.btcusd !== 0);
}

async fetchRates (source='bitstamp') {
if (this.exchangeRatesLoaded) return;

switch(source) {
case 'bitstamp':
this.btceur = await fetchFromBitstamp('btceur');
Expand Down
11 changes: 10 additions & 1 deletion app/styles/components/_budget-balances.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ section#funds {
}

th, td {
font-size: 1.2rem;
vertical-align: text-bottom;

img {
max-height: 1.5rem;
max-width: 1.5rem;
}
}

th {
Expand All @@ -35,8 +39,13 @@ section#funds {
}

&.fiat-amount {
font-size: 1.2rem;
color: rgba(255,255,255,0.8);
}

span.unit {
font-size: 1.5rem;
}
}
}
}
20 changes: 19 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,22 @@ module.exports = function(environment) {
'BTC': '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599'
},

btcBalanceAPI: 'https://api.kosmos.org/kredits/onchain_btc_balance',
communityFundsAPI: {
balances: {
onchain: {
icon: 'icon-btc.png',
symbol: 'BTC',
description: 'BTC on chain',
url: 'https://api.kosmos.org/btcpay/onchain_btc_balance'
},
lightning: {
icon: 'icon-btc-lightning.png',
symbol: 'BTC',
description: 'BTC on Lightning Network',
url: 'https://api.kosmos.org/btcpay/lightning_btc_balance'
}
}
},

corsProxy: 'https://cors.5apps.com/?uri='
};
Expand All @@ -66,6 +81,9 @@ module.exports = function(environment) {
protocol: 'http',
gatewayUrl: 'http://localhost:8080/ipfs'
};

ENV.communityFundsAPI.balances.onchain.url = 'http://localhost:3000/api/btcpay/onchain_btc_balance';
ENV.communityFundsAPI.balances.lightning.url = 'http://localhost:3000/api/btcpay/lightning_btc_balance';
}

if (environment === 'test') {
Expand Down
Binary file added public/img/icon-btc-lightning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/icon-btc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 895bf56

Please sign in to comment.