diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index 176c5e25fb..0cd67df975 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -303,6 +303,7 @@ function undelegationReducer(undelegation, validator) { function rewardReducer(reward, validator) { return { amount: atoms(reward.amount), + denom: reward.denom, validator } } @@ -312,11 +313,17 @@ function overviewReducer( delegations, undelegations, rewards, - stakingDenom + stakingDenom, + 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( @@ -336,7 +343,10 @@ function overviewReducer( ) return { - // rewards, + rewards: + rewards[0].constructor === Array + ? reducers.rewardReducer(rewards) + : rewards, totalRewards: totalRewards, liquidStake: liquidStake, totalStake: liquidStake.plus(delegatedStake).plus(undelegatingStake) diff --git a/lib/reducers/emoneyV0-reducers.js b/lib/reducers/emoneyV0-reducers.js index 2cdd0f1faa..96345e825b 100644 --- a/lib/reducers/emoneyV0-reducers.js +++ b/lib/reducers/emoneyV0-reducers.js @@ -5,6 +5,7 @@ const _ = require('lodash') const EMoneyAPIUrl = `https://api.e-money.com/v1/` const exchangeAPIURL = `https://api.exchangeratesapi.io/latest?` const Sentry = require('@sentry/node') +const { atoms } = terraV3Reducers async function totalBackedValueReducer(totalBackedValue) { const exchangeRates = await fetchTokenExchangeRates() @@ -119,8 +120,30 @@ async function expectedRewardsPerToken( return expectedReturns / 100 } +function rewardReducer(rewards, validatorsDictionary) { + const formattedRewards = rewards.map( + reward => + (reward = { + reward: reward.reward, + validator: validatorsDictionary[reward.validator_address] + }) + ) + let multiDenomRewardsArray = [] + formattedRewards.map(({ reward, validator }) => + reward.forEach(denomReward => { + multiDenomRewardsArray.push({ + denom: denomReward.denom, + amount: atoms(denomReward.amount), + validator: validator + }) + }) + ) + return multiDenomRewardsArray +} + module.exports = { ...terraV3Reducers, expectedRewardsPerToken, - totalBackedValueReducer + totalBackedValueReducer, + rewardReducer } diff --git a/lib/resolvers.js b/lib/resolvers.js index ac7e7389bd..1285a0a147 100644 --- a/lib/resolvers.js +++ b/lib/resolvers.js @@ -291,9 +291,17 @@ const resolvers = { // filter to a specific validator if (operatorAddress) { - rewards = rewards.filter( - ({ validator }) => validator.operatorAddress === operatorAddress - ) + rewards = rewards.filter(reward => { + // multidenom rewards + if (reward.validator === undefined) { + return reward.filter( + ({ validator }) => validator.operatorAddress === operatorAddress + ) + // non multidenom rewards + } else { + return reward.validator.operatorAddress === operatorAddress + } + }) } return rewards }, diff --git a/lib/schema.js b/lib/schema.js index 917c77141c..f516c62c34 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -21,6 +21,7 @@ const typeDefs = gql` type Reward { validator: Validator + denom: String amount: String } @@ -176,6 +177,7 @@ const typeDefs = gql` totalStake: String! liquidStake: String! totalRewards: String! + rewards: [Reward] accountInformation: AccountInformation } diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index 2a27297151..06eff1177f 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -470,7 +470,8 @@ class CosmosV0API extends RESTDataSource { delegations, undelegations, rewards, - stakingDenom + stakingDenom, + this.reducers ) } diff --git a/lib/source/emoneyV0-source.js b/lib/source/emoneyV0-source.js index b31e7e2981..1115ddb6eb 100644 --- a/lib/source/emoneyV0-source.js +++ b/lib/source/emoneyV0-source.js @@ -135,6 +135,17 @@ class EMoneyV0API extends TerraV3API { return `?` } } + + async getRewards(delegatorAddress, validatorsDictionary) { + this.checkAddress(delegatorAddress) + const result = await this.query( + `distribution/delegators/${delegatorAddress}/rewards` + ) + const rewards = (result.rewards || []).filter( + ({ reward }) => reward && reward.length > 0 + ) + return this.reducers.rewardReducer(rewards, validatorsDictionary) + } } module.exports = EMoneyV0API