From 1560e53002d04c399c1f3e1416e9a71714ab08a6 Mon Sep 17 00:00:00 2001 From: Col Date: Fri, 13 Dec 2019 04:36:13 +0100 Subject: [PATCH] Move expected returns to separate resolver (#172) * Move expected returns to separate resolver * Scale result by 1/1000000 --- lib/cosmosV0-source.js | 10 +++++++++ lib/reducers/cosmosV0-reducers.js | 35 +++++++++++++------------------ lib/reducers/cosmosV2-reducers.js | 17 +++------------ lib/resolvers.js | 5 +++++ 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/lib/cosmosV0-source.js b/lib/cosmosV0-source.js index 0e02099ad0..b8ebf27344 100644 --- a/lib/cosmosV0-source.js +++ b/lib/cosmosV0-source.js @@ -341,6 +341,16 @@ class CosmosV0API extends PerBlockCacheDataSource { return response } + async getExpectedReturns(validator) { + const annualProvision = await this.getAnnualProvision() + const expectedReturns = this.reducers.expectedRewardsPerToken( + validator, + validator.commission, + annualProvision + ) + return expectedReturns + } + async getRewards(delegatorAddress, validatorsDictionary, delegations = null) { if (!delegations) { delegations = await this.getDelegationsForDelegatorAddress( diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index aeb49fbbcd..15c2cd89bc 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -64,23 +64,26 @@ const calculateTokens = (validator, shares) => { /* if you don't get this, write fabian@lunie.io */ // expected rewards if delegator stakes x tokens const expectedRewardsPerToken = (validator, commission, annualProvision) => { - if (validator.status === 0 || validator.jailed === true) { + if (validator.status === 'INACTIVE' || validator.jailed === true) { return 0 } // share of all provisioned block rewards all delegators of this validator get - const totalAnnualValidatorRewards = validator.voting_power * annualProvision + const totalAnnualValidatorRewards = BigNumber(validator.votingPower).times( + annualProvision + ) // the validator takes a cut in amount of the commission - const totalAnnualDelegatorRewards = - totalAnnualValidatorRewards * (1 - commission) + const totalAnnualDelegatorRewards = totalAnnualValidatorRewards.times( + BigNumber(1).minus(commission) + ) // validator.tokens is the amount of all tokens delegated to that validator // one token delegated would receive x percentage of all delegator rewards - const delegatorSharePerToken = 1 / validator.tokens - const annualDelegatorRewardsPerToken = - totalAnnualDelegatorRewards * delegatorSharePerToken - - return annualDelegatorRewardsPerToken + const delegatorSharePerToken = BigNumber(1).div(validator.tokens) + const annualDelegatorRewardsPerToken = totalAnnualDelegatorRewards.times( + delegatorSharePerToken + ) + return annualDelegatorRewardsPerToken.div(1000000) } // reduce deposits to one number and filter by required denom @@ -188,12 +191,7 @@ function getValidatorStatus(validator) { } } -function validatorReducer( - networkId, - signedBlocksWindow, - validator, - annualProvision -) { +function validatorReducer(networkId, signedBlocksWindow, validator) { const statusInfo = getValidatorStatus(validator) let websiteURL = validator.description.website if (!websiteURL || websiteURL === '[do-not-modify]') { @@ -226,12 +224,7 @@ function validatorReducer( maxChangeCommission: validator.commission.max_change_rate, status: statusInfo.status, statusDetailed: statusInfo.status_detailed, - delegatorShares: validator.delegator_shares, // needed to calculate delegation token amounts from shares - expectedReturns: expectedRewardsPerToken( - validator, - validator.commission.rate, - annualProvision - ) + delegatorShares: validator.delegator_shares // needed to calculate delegation token amounts from shares } } diff --git a/lib/reducers/cosmosV2-reducers.js b/lib/reducers/cosmosV2-reducers.js index 80fa955fda..b343fdaf99 100644 --- a/lib/reducers/cosmosV2-reducers.js +++ b/lib/reducers/cosmosV2-reducers.js @@ -5,8 +5,7 @@ const { getDeposit, tallyReducer, atoms, - getValidatorStatus, - expectedRewardsPerToken + getValidatorStatus } = cosmosV0Reducers function proposalReducer( @@ -41,12 +40,7 @@ function delegationReducer(delegation, validator) { } } -function validatorReducer( - networkId, - signedBlocksWindow, - validator, - annualProvision -) { +function validatorReducer(networkId, signedBlocksWindow, validator) { const statusInfo = getValidatorStatus(validator) let websiteURL = validator.description.website if (!websiteURL || websiteURL === '[do-not-modify]') { @@ -83,12 +77,7 @@ function validatorReducer( maxChangeCommission: validator.commission.commission_rates.max_change_rate, status: statusInfo.status, statusDetailed: statusInfo.status_detailed, - delegatorShares: validator.delegator_shares, // needed to calculate delegation token amounts from shares - expectedReturns: expectedRewardsPerToken( - validator, - validator.commission.commission_rates.rate, - annualProvision - ) + delegatorShares: validator.delegator_shares // needed to calculate delegation token amounts from shares } } diff --git a/lib/resolvers.js b/lib/resolvers.js index 8147cb0fd5..35ed761cbe 100644 --- a/lib/resolvers.js +++ b/lib/resolvers.js @@ -200,6 +200,11 @@ const resolvers = { } }, Validator: { + expectedReturns: (validator, _, { dataSources }) => { + return selectFrom(dataSources, validator.networkId).getExpectedReturns( + validator + ) + }, selfStake: (validator, _, { dataSources }) => { return selectFrom(dataSources, validator.networkId).getSelfStake( validator