Skip to content

Commit

Permalink
Merge pull request #67 from protofire/hotfix/top-delegates-calculation
Browse files Browse the repository at this point in the history
(Hotfix) Top delegates calculation
  • Loading branch information
Mariano Aguero authored Jul 5, 2019
2 parents 2de78c5 + a9a3689 commit 4bec7f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/delegate/delegate.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getByAddress = async (req, res, next) => {
const { address } = req.params
const delegateService = getDelegateService()
try {
const result = await delegateService.getDelegate(address)
const result = await delegateService.getDelegateSummary(address)
res.json(result)
} catch (error) {
next(error)
Expand Down
17 changes: 12 additions & 5 deletions server/helpers/services/delegateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DelegateService {
}

// Returns the delegate summary
getDelegate = async delegateAddress => {
getDelegateSummary = async delegateAddress => {
const { getDelegateSummary } = this.source
const summary = await getDelegateSummary(delegateAddress)
return {
Expand All @@ -38,6 +38,15 @@ class DelegateService {
}
}

// Returns the delegate
getDelegate = async delegateAddress => {
const delegateSummary = await this.getDelegateSummary(delegateAddress)
const { summary } = delegateSummary
return {
...summary
}
}

// Receives a delegateAddress and returns the TOTAL reward (protocol reward, no the reward cut) of that delegate for the next round
getDelegateProtocolNextReward = async delegateAddress => {
const protocolService = getProtocolService()
Expand All @@ -61,14 +70,13 @@ class DelegateService {
// Receives a delegateAddress and returns the REAL reward of the delegate (nextReward*rewardCut)
getDelegateNextReward = async delegateAddress => {
// DelegateReward = DelegateProtocolNextReward * rewardCut
let [summary, protocolNextReward] = await promiseRetry(retry => {
let [delegate, protocolNextReward] = await promiseRetry(retry => {
return Promise.all([
this.getDelegate(delegateAddress),
this.getDelegateProtocolNextReward(delegateAddress)
]).catch(err => retry())
})

const { pendingRewardCut } = summary
const { pendingRewardCut } = delegate
const rewardCut = MathBN.div(pendingRewardCut, PROTOCOL_DIVISION_BASE)
return MathBN.mul(protocolNextReward, rewardCut)
}
Expand Down Expand Up @@ -133,7 +141,6 @@ class DelegateService {
// FORMULA: delegatorStakedAmount / delegateTotalStake
const participationInTotalStakeRatio = MathBN.div(delegatorAmountToStake, delegateTotalStake)
// Then calculates the reward with FORMULA: participationInTotalStakeRatio * rewardToDelegators
const result = MathBN.mul(rewardsToDelegators, participationInTotalStakeRatio)
return MathBN.mul(rewardsToDelegators, participationInTotalStakeRatio)
} else {
return 0
Expand Down
23 changes: 22 additions & 1 deletion test/delegate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,32 @@ describe('## DelegateService test', () => {

// then
expect(getSummaryStub.called)
expect(result.summary).to.deep.equal(resultExpected)
expect(result).to.deep.equal(resultExpected)
// restore stubs
getSummaryStub.restore()
})
})
it('getDelegateSummary should return a delegate summary', async () => {
// given
const delegate = createTranscoder()
// stubs the delegateGraphql service
const getSummaryStub = sinon.stub(delegatesGraphql, 'getDelegateSummary').returns(delegate)
const resultExpected = {
summary: {
...delegate,
totalStake: tokenAmountInUnits(delegate.totalStake)
}
}

// when
const result = await delegateService.getDelegateSummary()

// then
expect(getSummaryStub.called)
expect(result).to.deep.equal(resultExpected)
// restore stubs
getSummaryStub.restore()
})
describe('# getDelegateProtocolNextReward', () => {
// bondedStake = 400
// totalSupply = 1000
Expand Down

0 comments on commit 4bec7f3

Please sign in to comment.