Dynamically load all balances from Safe

This commit is contained in:
2020-08-13 16:13:50 +02:00
parent 1dd63b2c7a
commit 5f90bc23bc
4 changed files with 40 additions and 20 deletions
@@ -1,6 +1,12 @@
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 loading () {
return !this.communityFunds.balancesLoaded;
}
} }
+6 -9
View File
@@ -1,4 +1,4 @@
<table class="token-balances"> <table class="token-balances {{if this.loading 'loading'}}">
<thead> <thead>
<tr> <tr>
<th>Token</th> <th>Token</th>
@@ -7,15 +7,12 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each this.balances as |balance|}}
<tr> <tr>
<th>ETH</th> <th>{{balance.token.symbol}}</th>
<td class="amount">{{fmt-crypto-currency this.communityFunds.balanceETH.balance 'ETH'}}</td> <td class="amount">{{fmt-crypto-currency balance.balance balance.token.symbol}}</td>
<td class="fiat-amount">{{this.communityFunds.balanceETH.balanceUsd}} USD</td> <td class="fiat-amount">{{balance.balanceUsd}} USD</td>
</tr>
<tr>
<th>WBTC</th>
<td class="amount">{{fmt-crypto-currency this.communityFunds.balanceWBTC.balance 'WBTC'}}</td>
<td class="fiat-amount">{{this.communityFunds.balanceWBTC.balanceUsd}} USD</td>
</tr> </tr>
{{/each}}
</tbody> </tbody>
</table> </table>
+19 -9
View File
@@ -1,22 +1,22 @@
import Service from '@ember/service'; import Service from '@ember/service';
import { tracked } from '@glimmer/tracking'; import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array';
import { timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators'; import { task } from 'ember-concurrency-decorators';
import config from 'kredits-web/config/environment'; import config from 'kredits-web/config/environment';
const txServiceBaseUrl = `${config.gnosisSafe.txServiceHost}/api/v1/safes/${config.gnosisSafe.address}`; const txServiceBaseUrl = `${config.gnosisSafe.txServiceHost}/api/v1/safes/${config.gnosisSafe.address}`;
export default class CommunityFundsService extends Service { export default class CommunityFundsService extends Service {
@tracked balanceETH = { balance: 0, balanceUsd: 0, usdConversion: 0 }; @tracked balancesLoaded = false;
@tracked balanceWBTC = { balance: 0, balanceUsd: 0, usdConversion: 0 }; @tracked balances = A([]);
@task @task
*fetchBalances () { *fetchBalances () {
const uri = `${txServiceBaseUrl}/balances/usd/`; const uri = `${txServiceBaseUrl}/balances/usd/`;
yield fetch(uri).then(res => res.json()) yield fetch(uri).then(res => res.json())
.then(res => { .then(res => this.processBalances(res))
this.processBalances(res);
})
.catch(err => { .catch(err => {
console.log(`[community-funds] Fetching balances failed:`); console.log(`[community-funds] Fetching balances failed:`);
console.error(err); console.error(err);
@@ -24,9 +24,19 @@ export default class CommunityFundsService extends Service {
} }
processBalances (res) { processBalances (res) {
// TODO proper selection/find for (const balance of res) {
const { balance, balanceUsd, usdConversion } = res[0]; if (balance.token) {
this.balanceETH = { balance, balanceUsd, usdConversion }; // ERC20 token, has all meta data
this.balanceWBTC = res[1]; this.balances.pushObject(balance);
} else {
// ETH, missing meta data
this.balances.pushObject({
...balance,
...{ token: { name: 'Ether', symbol: 'ETH'} }
});
}
}
this.balancesLoaded = true;
} }
} }
@@ -1,5 +1,12 @@
section#funds { section#funds {
table.token-balances { table.token-balances {
opacity: 1;
transition: opacity 0.3s linear;
&.loading {
opacity: 0;
}
thead { thead {
display: none; display: none;
} }