Skip to content

Commit

Permalink
Move expected returns to separate resolver (#172)
Browse files Browse the repository at this point in the history
* Move expected returns to separate resolver

* Scale result by 1/1000000
  • Loading branch information
colw authored and faboweb committed Dec 13, 2019
1 parent 755e768 commit 1560e53
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
10 changes: 10 additions & 0 deletions lib/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 14 additions & 21 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@ const calculateTokens = (validator, shares) => {
/* if you don't get this, write [email protected] */
// 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
Expand Down Expand Up @@ -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]') {
Expand Down Expand Up @@ -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
}
}

Expand Down
17 changes: 3 additions & 14 deletions lib/reducers/cosmosV2-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const {
getDeposit,
tallyReducer,
atoms,
getValidatorStatus,
expectedRewardsPerToken
getValidatorStatus
} = cosmosV0Reducers

function proposalReducer(
Expand Down Expand Up @@ -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]') {
Expand Down Expand Up @@ -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
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1560e53

Please sign in to comment.