Skip to content

Commit

Permalink
Fabo/new emoney rewards (#357)
Browse files Browse the repository at this point in the history
* intent to fix the emoney rewards

* intent to fix emoney rewards

* simplified code

* comments

* hack to fix reduce function

* eligable -> eligible

* correct reducer fix

Co-authored-by: Ana G. <40721795+Bitcoinera@users.noreply.github.com>
Co-authored-by: Jordan Bibla <jbibla@gmail.com>
  • Loading branch information
3 people authored Feb 24, 2020
1 parent 157cd42 commit f2df617
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions lib/reducers/emoneyV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,35 @@ async function expectedRewardsPerToken(
inflations,
totalBackedValues
) {
const percentTotalStakedNGM = BigNumber(validator.votingPower).times(1000)
const totalNGMStakedToValidator = validator.tokens
const division = BigNumber(percentTotalStakedNGM)
const percentOfAllEligibleStake = validator.votingPower
const totalStakeToValidator = validator.tokens // used to answer the question "How many rewards per ONE token invested?"
const percentOfAllRewardsPerToken = BigNumber(percentOfAllEligibleStake)
.times(BigNumber(1).minus(BigNumber(commission)))
.div(BigNumber(totalNGMStakedToValidator))
.div(BigNumber(totalStakeToValidator))

// Now we need to multiply each total supply of backed tokens with its corresponding
// inflation
let accumulator = BigNumber(0)
const totalBackedValueDictionary = _.keyBy(totalBackedValues, 'denom')

inflations.forEach(inflation => {
accumulator = BigNumber(accumulator)
.plus(accumulator)
.plus(
BigNumber(inflation.inflation)
.times(totalBackedValueDictionary[inflation.denom].amount)
.div(1000000)
const rewardsSumInEur = inflations.reduce((sum, inflation) => {
return BigNumber(sum).plus(
BigNumber(inflation.inflation).times(
totalBackedValueDictionary[inflation.denom].eurValue // we use the eur value to be able to sum up the individual token values
)
})
const totalBackedValuesTimesInflations = accumulator
// First we calculate the total value of rewards we get for staking one single
// NGM in this particular validator
let delegatorSharePerToken = totalBackedValuesTimesInflations.div(division)
// In testnet some validators have commission at 100% and therefore division is
// equal to 0. Dividing by 0 we get infinity, and we want to make sure that
// we don't display such value
const infinity = new BigNumber(Infinity)
// This is just how BigNumber works. Comparing to 0 and if true, bignumber equals the given parameter.
// Documentation on this method can be found here: https://mikemcl.github.io/bignumber.js/#cmp
delegatorSharePerToken =
delegatorSharePerToken.comparedTo(infinity) === 0
? BigNumber(0)
: delegatorSharePerToken.div(1000000)
// Now we get the total net value in EUR of all token's total supplies
let eurAccumulator = 0
totalBackedValues.forEach(totalBackedValue => {
eurAccumulator += totalBackedValue.eurValue
})
const totalEURGains = eurAccumulator * delegatorSharePerToken.toNumber()
)
}, 0)

// now we calculate the total rewards in eur per token delegated to the validator
const totalEURGainsPerTokenInvested = BigNumber(rewardsSumInEur).times(
percentOfAllRewardsPerToken
)

// How many NGM tokens can we buy with the total gain in EUR we make in a year's time?
// 0.50€ is the price the NGM tokens will be first sold. Therefore, this is the official value
// until they reach an exchange
const pricePerNGM = 0.5
const ngmGains = totalEURGains / pricePerNGM
// We divide by 1 because we assume 1 NGM > gain per 1 NGM
const expectedReturns = parseFloat(ngmGains / 1).toFixed(2)
// TODO the FE is the one converting to percentage now, so we need to divide by 100
return expectedReturns / 100
const ngmGains = totalEURGainsPerTokenInvested / pricePerNGM

return ngmGains.toFixed(2) // we don't need more then a precision of 2
}

module.exports = {
Expand Down

0 comments on commit f2df617

Please sign in to comment.