46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
import 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 {
|
|
@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))
|
|
.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);
|
|
|
|
if (balance.token) {
|
|
// ERC20 token, has all meta data
|
|
this.balances.pushObject(balance);
|
|
} else {
|
|
// ETH, missing meta data
|
|
this.balances.pushObject({
|
|
...balance,
|
|
...{ token: { name: 'Ether', symbol: 'ETH'} }
|
|
});
|
|
}
|
|
}
|
|
|
|
this.balancesLoaded = true;
|
|
}
|
|
}
|