Skip to content

Commit

Permalink
Split rewards from other queries (#543)
Browse files Browse the repository at this point in the history
* moving rewards to resolved in overview

* lint

* working

* Add totalRewards resolver in Overview

* fix

* cleanup

* cleanup

* cleanup

* getViewDenom() is not async

* add rewardsV2 query

* add coinLookup property to all networks

* Update lib/source/cosmosV0-source.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* Update data/networks.js

Co-Authored-By: Fabian <[email protected]>

* suggestions, cleanup

* Remove rewardsV2

* revert schema changes

Co-authored-by: Fabian <[email protected]>
Co-authored-by: Bitcoinera <[email protected]>
  • Loading branch information
3 people authored Apr 1, 2020
1 parent 2c5bacd commit d6882c2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
19 changes: 0 additions & 19 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { flatten } = require('lodash')
const BigNumber = require('bignumber.js')

/**
Expand Down Expand Up @@ -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) || {
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }) => {
Expand Down
11 changes: 5 additions & 6 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions lib/source/polkadotV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -49,7 +50,7 @@ class polkadotAPI {
)

return this.reducers.blockReducer(
this.networkId,
this.network.id,
blockHeight,
blockHash,
sessionIndex.toNumber(),
Expand Down Expand Up @@ -171,8 +172,6 @@ class polkadotAPI {
totalStake: accountBalances[0].total,
totalStakeFiatValue: '',
liquidStake: accountBalances[0].amount,
totalRewards: '',
rewards: undefined,
accountInformation: undefined
}
}
Expand Down Expand Up @@ -202,6 +201,10 @@ class polkadotAPI {
getUndelegationsForDelegatorAddress() {
return []
}

getStakingViewDenom() {
return this.stakingViewDenom
}
}

module.exports = polkadotAPI
8 changes: 5 additions & 3 deletions lib/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d6882c2

Please sign in to comment.