diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index 93fd9d025f..6b977c4bd6 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -1,4 +1,3 @@ -const { flatten } = require('lodash') const BigNumber = require('bignumber.js') /** @@ -375,22 +374,12 @@ async function overviewReducer( balances, delegations, undelegations, - rewards, stakingDenom, fiatValueAPI, fiatCurrency, reducers ) { stakingDenom = denomLookup(stakingDenom) - - const totalRewards = flatten(rewards) - // this filter is here for multidenom networks. If there is the field denoms inside rewards, we filter - // only the staking denom rewards for 'totalRewards' - .filter(reward => - reward.denom ? denomLookup(reward.denom) === stakingDenom : reward - ) - .reduce((sum, { amount }) => BigNumber(sum).plus(amount), 0) - .toFixed(6) const liquidStake = BigNumber( ( balances.find(({ denom }) => denomLookup(denom) === stakingDenom) || { @@ -409,14 +398,6 @@ async function overviewReducer( const totalStake = liquidStake.plus(delegatedStake).plus(undelegatingStake) return { - rewards: - // there are some accounts which are not staking and therefore have no rewards - rewards[0] - ? rewards[0].constructor === Array - ? reducers.rewardReducer(rewards) - : rewards - : null, - totalRewards: totalRewards, liquidStake: liquidStake, totalStake, totalStakeFiatValue: fiatValueAPI diff --git a/lib/resolvers.js b/lib/resolvers.js index c01af5a4eb..024a0b67bd 100644 --- a/lib/resolvers.js +++ b/lib/resolvers.js @@ -13,6 +13,7 @@ const { getNetworkTransactionGasEstimates } = require('../data/network-fees') const database = require('./database') const config = require('../config.js') const { logOverview } = require('./statistics') +const BigNumber = require('bignumber.js') function createDBInstance(network) { const networkSchemaName = network ? network.replace(/-/g, '_') : false @@ -181,7 +182,41 @@ const resolvers = { accountInformation: (account, _, { dataSources }) => remoteFetch(dataSources, account.networkId).getAccountInfo( account.address + ), + rewards: async ( + { networkId, address, fiatCurrency }, + _, + { dataSources } + ) => { + await localStore(dataSources, networkId).dataReady + const validatorsDictionary = localStore(dataSources, networkId).validators + return remoteFetch(dataSources, networkId).getRewards( + address, + validatorsDictionary, + fiatCurrency + ) + }, + totalRewards: async ( + { networkId, address, fiatCurrency }, + _, + { dataSources } + ) => { + await localStore(dataSources, networkId).dataReady + const validatorsDictionary = localStore(dataSources, networkId).validators + const rewards = await remoteFetch(dataSources, networkId).getRewards( + address, + validatorsDictionary, + fiatCurrency ) + const stakingDenom = await remoteFetch( + dataSources, + networkId + ).getStakingViewDenom() + return rewards + .filter(({ denom }) => denom === stakingDenom) + .reduce((sum, { amount }) => BigNumber(sum).plus(amount), 0) + .toFixed(6) + } }, Proposal: { validator: (proposal, _, { dataSources }) => { diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index 8102aa36d6..3364033341 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -27,6 +27,7 @@ class CosmosV0API extends RESTDataSource { this.validatorConsensusBech32Prefix = `${network.address_prefix}valcons` this.gasPrices = gasPrices this.store = store + this.viewDenom = network.coinLookup[0].viewDenom this.setReducers() } @@ -87,6 +88,10 @@ class CosmosV0API extends RESTDataSource { return stakingParameters.bond_denom } + getStakingViewDenom() { + return this.viewDenom + } + async getSignedBlockWindow() { const slashingParams = await this.query('/slashing/parameters') return slashingParams.signed_blocks_window @@ -493,17 +498,11 @@ class CosmosV0API extends RESTDataSource { ), this.getStakingDenom() ]) - const rewards = await this.getRewards( - delegatorAddress, - validatorsDictionary, - fiatCurrency - ) const fiatValueAPI = this.calculateFiatValue ? this : null return this.reducers.overviewReducer( balances, delegations, undelegations, - rewards, stakingDenom, fiatValueAPI, fiatCurrency, diff --git a/lib/source/polkadotV0-source.js b/lib/source/polkadotV0-source.js index 0215056a66..cf2f6eec71 100644 --- a/lib/source/polkadotV0-source.js +++ b/lib/source/polkadotV0-source.js @@ -3,6 +3,7 @@ const BigNumber = require('bignumber.js') class polkadotAPI { constructor(network, store) { this.network = network + this.stakingViewDenom = network.coinLookup[0].viewDenom this.setReducers() this.store = store } @@ -49,7 +50,7 @@ class polkadotAPI { ) return this.reducers.blockReducer( - this.networkId, + this.network.id, blockHeight, blockHash, sessionIndex.toNumber(), @@ -171,8 +172,6 @@ class polkadotAPI { totalStake: accountBalances[0].total, totalStakeFiatValue: '', liquidStake: accountBalances[0].amount, - totalRewards: '', - rewards: undefined, accountInformation: undefined } } @@ -202,6 +201,10 @@ class polkadotAPI { getUndelegationsForDelegatorAddress() { return [] } + + getStakingViewDenom() { + return this.stakingViewDenom + } } module.exports = polkadotAPI diff --git a/lib/statistics.js b/lib/statistics.js index a980f3c86f..11aea3834f 100644 --- a/lib/statistics.js +++ b/lib/statistics.js @@ -123,9 +123,11 @@ const logOverview = (overview, fingerprint) => { data.value = overview.totalStake.toString() store(data) // store totalRewards - data.action = 'totalRewards' - data.value = overview.totalRewards.toString() - store(data) + if (overview.totalRewards) { + data.action = 'totalRewards' + data.value = overview.totalRewards.toString() + store(data) + } // store rewards // summing rewards with one denom if (overview.rewards) {