Merge pull request #217 from 67P/feature/budget-balances

Use new balance APIs from akkounts, add Lightning balance
This commit was merged in pull request #217.
This commit is contained in:
Râu Cao
2024-01-14 17:29:07 +01:00
committed by GitHub
9 changed files with 82 additions and 18 deletions
+5 -2
View File
@@ -1,10 +1,13 @@
import Component from '@glimmer/component'; import Component from '@glimmer/component';
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';
export default class BudgetBalancesComponent extends Component { export default class BudgetBalancesComponent extends Component {
@service communityFunds; @service communityFunds;
@alias('communityFunds.balances') balances;
get balancesSorted () {
return this.communityFunds.balances
.sortBy('confirmed_balance').reverse();
}
get loading () { get loading () {
return !this.communityFunds.balancesLoaded; return !this.communityFunds.balancesLoaded;
+12 -4
View File
@@ -7,11 +7,19 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each this.balances as |balance|}} {{#each this.balancesSorted as |balance|}}
<tr> <tr>
<th>{{balance.token.symbol}}</th> <th>
<td class="amount">{{balance.confirmed_balance}}</td> <img src={{balance.token.icon}}
<td class="fiat-amount">~{{balance.balanceUSD}} USD</td> 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> </tr>
{{/each}} {{/each}}
</tbody> </tbody>
+6
View File
@@ -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);
});
+24 -10
View File
@@ -1,7 +1,7 @@
import Service, { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array'; import { A } from '@ember/array';
import { task } from 'ember-concurrency-decorators'; 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'; import config from 'kredits-web/config/environment';
export default class CommunityFundsService extends Service { export default class CommunityFundsService extends Service {
@@ -12,9 +12,20 @@ export default class CommunityFundsService extends Service {
@task @task
*fetchBalances () { *fetchBalances () {
yield fetch(config.btcBalanceAPI).then(res => res.json()) const promises = [];
.then(res => { const balances = config.communityFundsAPI.balances;
return this.processBalances(res);
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 => { .catch(err => {
console.log(`[community-funds] Fetching balances failed:`); console.log(`[community-funds] Fetching balances failed:`);
@@ -22,18 +33,21 @@ export default class CommunityFundsService extends Service {
}); });
} }
async processBalances (res) { async fetchBalance(url) {
return fetch(url).then(res => res.json());
}
async processBalance (res, config) {
await this.exchangeRates.fetchRates(); await this.exchangeRates.fetchRates();
// Format and round the approximate USD value // Format and round the approximate USD value
const lang = navigator.language || navigator.userLanguage; 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); res.balanceUSD = Math.round(balanceUSD).toLocaleString(lang);
this.balances.pushObject({ this.balances.pushObject({
...res, ...res,
...{ token: { name: 'BTC', symbol: 'BTC'} } ...{ token: { icon: `/img/${config.icon}`, symbol: config.symbol, description: config.description } }
}); });
this.balancesLoaded = true;
} }
} }
+6
View File
@@ -19,7 +19,13 @@ export default class ExchangeRatesService extends Service {
@tracked btceur = 0; @tracked btceur = 0;
@tracked btcusd = 0; @tracked btcusd = 0;
get exchangeRatesLoaded () {
return (this.btceur !== 0) && (this.btcusd !== 0);
}
async fetchRates (source='bitstamp') { async fetchRates (source='bitstamp') {
if (this.exchangeRatesLoaded) return;
switch(source) { switch(source) {
case 'bitstamp': case 'bitstamp':
this.btceur = await fetchFromBitstamp('btceur'); this.btceur = await fetchFromBitstamp('btceur');
+10 -1
View File
@@ -16,8 +16,12 @@ section#funds {
} }
th, td { th, td {
font-size: 1.2rem;
vertical-align: text-bottom; vertical-align: text-bottom;
img {
max-height: 1.5rem;
max-width: 1.5rem;
}
} }
th { th {
@@ -35,8 +39,13 @@ section#funds {
} }
&.fiat-amount { &.fiat-amount {
font-size: 1.2rem;
color: rgba(255,255,255,0.8); color: rgba(255,255,255,0.8);
} }
span.unit {
font-size: 1.5rem;
}
} }
} }
} }
+19 -1
View File
@@ -45,7 +45,22 @@ module.exports = function(environment) {
'BTC': '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' '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=' corsProxy: 'https://cors.5apps.com/?uri='
}; };
@@ -66,6 +81,9 @@ module.exports = function(environment) {
protocol: 'http', protocol: 'http',
gatewayUrl: 'http://localhost:8080/ipfs' 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') { if (environment === 'test') {
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB