Skip to content

Commit

Permalink
Merge ef98bf2 into bb5bf36
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano-aguero committed Aug 9, 2019
2 parents bb5bf36 + ef98bf2 commit 129a04d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 44 deletions.
24 changes: 13 additions & 11 deletions server/helpers/delegatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,21 @@ const getWeeklySharesPerRound = async (delegatorAddress, currentRound) => {
new Big('0')
)

const totalDelegatorShares = new Big(totalDelegatorSharesWithoutDecimals).toFixed(
TO_FIXED_VALUES_DECIMALS
)

const averageShares = utils.MathBN.divAsBig(totalDelegatorShares, 7).toFixed(
TO_FIXED_VALUES_DECIMALS
)

// Formats shares to 4 decimals
let totalDelegatorShares = new Big(totalDelegatorSharesWithoutDecimals)
let averageShares = utils.MathBN.divAsBig(totalDelegatorShares, 7)
// Format totalDelegatorShares in token units
totalDelegatorShares = utils.tokenAmountInUnits(totalDelegatorShares)
totalDelegatorShares = new Big(totalDelegatorShares).toFixed(TO_FIXED_VALUES_DECIMALS)
// Formats average shares in token units
averageShares = utils.tokenAmountInUnits(averageShares)
averageShares = new Big(averageShares).toFixed(TO_FIXED_VALUES_DECIMALS)
// Formats shares to 4 decimals and in token units
delegatorShares.forEach(shareElement => {
const newReward = new Big(shareElement.rewardTokens)
shareElement.rewardTokens = newReward.toFixed(TO_FIXED_VALUES_DECIMALS)
let newReward = utils.tokenAmountInUnits(shareElement.rewardTokens)
newReward = new Big(newReward).toFixed(TO_FIXED_VALUES_DECIMALS)
shareElement.rewardTokens = newReward
})

return {
weekRoundShares: delegatorShares,
averageShares,
Expand Down
16 changes: 12 additions & 4 deletions server/helpers/notification/notificateDelegatorUtils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const promiseRetry = require('promise-retry')
const mongoose = require('../../../config/mongoose')
const { getProtocolService } = require('../services/protocolService')
const utils = require('../utils')
const subscriberUtils = require('../subscriberUtils')
const delegatorEmailUtils = require('../sendDelegatorEmail')
const delegatorTelegramUtils = require('../sendDelegatorTelegram')
const delegatorsUtils = require('../delegatorUtils')
const Share = require('../../share/share.model')
const { DAILY_FREQUENCY, WEEKLY_FREQUENCY } = require('../../../config/constants')
const { getProtocolService } = require('../services/protocolService')
const { getDelegateService } = require('../services/delegateService')

const { getDelegatorService } = require('../services/delegatorService')
const sendEmailRewardCallNotificationToDelegators = async currentRoundInfo => {
if (!currentRoundInfo) {
throw new Error('No currentRoundInfo provided on sendEmailRewardCallNotificationToDelegators()')
Expand All @@ -20,6 +20,7 @@ const sendEmailRewardCallNotificationToDelegators = async currentRoundInfo => {
let emailsToSend = []
const protocolService = getProtocolService()
const delegateService = getDelegateService()
const delegatorService = getDelegatorService()

const [constants] = await promiseRetry(retry => {
return Promise.all([protocolService.getLivepeerDefaultConstants()]).catch(err => retry())
Expand Down Expand Up @@ -59,13 +60,20 @@ const sendEmailRewardCallNotificationToDelegators = async currentRoundInfo => {

if (subscriber.emailFrequency === DAILY_FREQUENCY) {
// If daily subscription send normal email
const [delegateCalledReward, delegatorRoundReward] = await promiseRetry(retry => {
let [delegateCalledReward, delegatorRoundReward] = await promiseRetry(retry => {
return Promise.all([
delegateService.getDidDelegateCalledReward(delegator.delegateAddress),
Share.getDelegatorShareAmountOnRound(currentRoundInfo.id, delegator.address)
]).catch(err => retry())
})

delegatorRoundReward = utils.tokenAmountInUnits(delegatorRoundReward)
// If there are no shares for that user, return the next delegatorReward as default
if (delegatorRoundReward === '0') {
console.error(
`[Notificate-Delegators] - share for round ${currentRoundId} of delegator ${delegator.address} not found, returning next reward`
)
delegatorRoundReward = await delegatorService.getDelegatorNextReward(delegator.address)
}
delegatorTemplateData = {
delegateCalledReward,
delegatorRoundReward
Expand Down
11 changes: 9 additions & 2 deletions server/helpers/telegramUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ const getDelegatorTelegramBody = async subscriber => {
const fileTemplateBonded = path.join(__dirname, filenameBonded)
const sourceBonded = fs.readFileSync(fileTemplateBonded, 'utf8')

const earningNextReturn = await Share.getDelegatorShareAmountOnRound(
let earningNextReturn = await Share.getDelegatorShareAmountOnRound(
currentRound,
delegator.address
)

earningNextReturn = utils.tokenAmountInUnits(earningNextReturn)
// If there are no shares for that user, return the next delegatorReward as default
if (earningNextReturn === '0') {
console.error(
`[Telegram-utils] - share for round ${currentRound} of delegator ${delegator.address} not found, returning next reward`
)
earningNextReturn = await delegatorService.getDelegatorNextReward(delegator.address)
}
// Calculate earned lpt
const lptEarned = utils.formatBalance(earningNextReturn, earningDecimals, 'wei')

Expand Down
9 changes: 2 additions & 7 deletions server/share/share.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ ShareSchema.statics = {
return null
}
const roundShare = await this.findOne({ delegator: delegatorAddress, round: roundId })
// If there are no shares for that user, return the next delegatorReward as default
// If there are no shares for that user, returns 0
if (!roundShare) {
const { getDelegatorService } = require('../helpers/services/delegatorService')
const delegatorService = getDelegatorService()
console.error(
`[ShareSchema] - share for round ${roundId} of delegator ${delegatorAddress} not found, returning next reward`
)
return await delegatorService.getDelegatorNextReward(delegatorAddress)
return 0
}
return roundShare.rewardTokens
}
Expand Down
11 changes: 10 additions & 1 deletion server/subscriber/subscriber.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const create = async (req, res, next) => {
// Get round info
const protocolService = getProtocolService()
const delegateService = getDelegateService()
const delegatorService = getDelegatorService()
const currentRoundInfo = await protocolService.getCurrentRoundInfo()
const currentRound = currentRoundInfo.id

Expand All @@ -94,10 +95,18 @@ const create = async (req, res, next) => {
if (role === constants.ROLE.DELEGATOR) {
const { sendDelegatorNotificationEmail } = require('../helpers/sendDelegatorEmail')

const delegatorRoundReward = await Share.getDelegatorShareAmountOnRound(
let delegatorRoundReward = await Share.getDelegatorShareAmountOnRound(
currentRound,
delegator.address
)
delegatorRoundReward = utils.tokenAmountInUnits(delegatorRoundReward)
// If there are no shares for that user, return the next delegatorReward as default
if (delegatorRoundReward === '0') {
console.error(
`[Notificate-Delegators] - share for round ${currentRound} of delegator ${delegator.address} not found, returning next reward`
)
delegatorRoundReward = await delegatorService.getDelegatorNextReward(delegator.address)
}

const delegatorTemplateData = {
delegateCalledReward,
Expand Down
55 changes: 36 additions & 19 deletions test/delegatorUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
const { TO_FIXED_VALUES_DECIMALS } = require('../config/constants')
const Big = require('big.js')
describe('## DelegatorsUtils test', () => {
describe('# getWeeklySharesPerRound', () => {
it('if no delegatorAddress given, throws an error', async () => {
Expand Down Expand Up @@ -93,9 +92,12 @@ describe('## DelegatorsUtils test', () => {
// given
const delegatorAddress = 1
const currentRound = 7
const share1 = testUtil.createShare(delegatorAddress, '7', '100')
const share2 = testUtil.createShare(delegatorAddress, '5', '200')
const share3 = testUtil.createShare(delegatorAddress, '1', '300')
const reward1 = utils.unitAmountInTokenUnits('100')
const reward2 = utils.unitAmountInTokenUnits('200')
const reward3 = utils.unitAmountInTokenUnits('300')
const share1 = testUtil.createShare(delegatorAddress, '7', reward1)
const share2 = testUtil.createShare(delegatorAddress, '5', reward2)
const share3 = testUtil.createShare(delegatorAddress, '1', reward3)
const shares = [share3, share2, share1]
const sharesExpected = shares
const delegator = testUtil.createDelegator(delegatorAddress)
Expand Down Expand Up @@ -140,13 +142,20 @@ describe('## DelegatorsUtils test', () => {
// given
const delegatorAddress = 1
const currentRound = 7
const share1 = testUtil.createShare(delegatorAddress, '1', '100')
const share2 = testUtil.createShare(delegatorAddress, '2', '200')
const share3 = testUtil.createShare(delegatorAddress, '3', '300')
const share4 = testUtil.createShare(delegatorAddress, '4', '400')
const share5 = testUtil.createShare(delegatorAddress, '5', '500')
const share6 = testUtil.createShare(delegatorAddress, '6', '600')
const share7 = testUtil.createShare(delegatorAddress, '7', '700')
const reward1 = utils.unitAmountInTokenUnits('100')
const reward2 = utils.unitAmountInTokenUnits('200')
const reward3 = utils.unitAmountInTokenUnits('300')
const reward4 = utils.unitAmountInTokenUnits('400')
const reward5 = utils.unitAmountInTokenUnits('500')
const reward6 = utils.unitAmountInTokenUnits('600')
const reward7 = utils.unitAmountInTokenUnits('700')
const share1 = testUtil.createShare(delegatorAddress, '1', reward1)
const share2 = testUtil.createShare(delegatorAddress, '2', reward2)
const share3 = testUtil.createShare(delegatorAddress, '3', reward3)
const share4 = testUtil.createShare(delegatorAddress, '4', reward4)
const share5 = testUtil.createShare(delegatorAddress, '5', reward5)
const share6 = testUtil.createShare(delegatorAddress, '6', reward6)
const share7 = testUtil.createShare(delegatorAddress, '7', reward7)
const shares = [share7, share6, share5, share4, share3, share2, share1]
const sharesExpected = shares
const delegator = testUtil.createDelegator(delegatorAddress)
Expand Down Expand Up @@ -191,14 +200,22 @@ describe('## DelegatorsUtils test', () => {
// given
const delegatorAddress = 1
const currentRound = 7
const share1 = testUtil.createShare(delegatorAddress, '1', '100')
const share2 = testUtil.createShare(delegatorAddress, '2', '200')
const share3 = testUtil.createShare(delegatorAddress, '3', '300')
const share4 = testUtil.createShare(delegatorAddress, '4', '400')
const share5 = testUtil.createShare(delegatorAddress, '5', '500')
const share6 = testUtil.createShare(delegatorAddress, '6', '600')
const share7 = testUtil.createShare(delegatorAddress, '7', '700')
const share8 = testUtil.createShare(delegatorAddress, '8', '800')
const reward1 = utils.unitAmountInTokenUnits('100')
const reward2 = utils.unitAmountInTokenUnits('200')
const reward3 = utils.unitAmountInTokenUnits('300')
const reward4 = utils.unitAmountInTokenUnits('400')
const reward5 = utils.unitAmountInTokenUnits('500')
const reward6 = utils.unitAmountInTokenUnits('600')
const reward7 = utils.unitAmountInTokenUnits('700')
const reward8 = utils.unitAmountInTokenUnits('800')
const share1 = testUtil.createShare(delegatorAddress, '1', reward1)
const share2 = testUtil.createShare(delegatorAddress, '2', reward2)
const share3 = testUtil.createShare(delegatorAddress, '3', reward3)
const share4 = testUtil.createShare(delegatorAddress, '4', reward4)
const share5 = testUtil.createShare(delegatorAddress, '5', reward5)
const share6 = testUtil.createShare(delegatorAddress, '6', reward6)
const share7 = testUtil.createShare(delegatorAddress, '7', reward7)
const share8 = testUtil.createShare(delegatorAddress, '8', reward8)
const shares = [share8, share7, share6, share5, share4, share3, share2, share1]
const sharesExpected = [share7, share6, share5, share4, share3, share2, share1]
const delegator = testUtil.createDelegator(delegatorAddress)
Expand Down

0 comments on commit 129a04d

Please sign in to comment.