Skip to content

Commit

Permalink
Merge db33642 into 59edaa1
Browse files Browse the repository at this point in the history
  • Loading branch information
Agupane committed Aug 1, 2019
2 parents 59edaa1 + db33642 commit 387d780
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 103 deletions.
13 changes: 12 additions & 1 deletion config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const DAILY_FREQUENCY = 'daily'
const WEEKLY_FREQUENCY = 'weekly'
const VALID_SUBSCRIPTION_FREQUENCIES = [WEEKLY_FREQUENCY, DAILY_FREQUENCY]
const TO_FIXED_VALUES_DECIMALS = 4
const LIVEPEER_DEFAULT_CONSTANTS = {
DELEGATOR_STATUS: {
Bonded: 'Bonded',
Pending: 'Pending',
Unbonded: 'Unbonded',
Unbonding: 'Unbonding'
},
ROLE: { DELEGATOR: 'Delegator', TRANSCODER: 'Transcoder' }
}

module.exports = {
PROTOCOL_DIVISION_BASE,
TOKEN_DECIMALS_MULTIPLIER,
Expand All @@ -14,5 +24,6 @@ module.exports = {
VALID_SUBSCRIPTION_FREQUENCIES,
DAILY_FREQUENCY,
WEEKLY_FREQUENCY,
TO_FIXED_VALUES_DECIMALS
TO_FIXED_VALUES_DECIMALS,
LIVEPEER_DEFAULT_CONSTANTS
}
140 changes: 75 additions & 65 deletions server/helpers/notification/notificateDelegatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,46 +116,51 @@ const sendEmailAfterBondingPeriodHasEndedNotificationToDelegators = async curren
const currentRoundId = currentRoundInfo.id

for (const subscriberItem of subscribers) {
const { subscriber } = subscriberItem
try {
const { subscriber } = subscriberItem

if (!subscriber.lastPendingToBondingPeriodEmailSent) {
subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId
await subscriber.save()
continue
}
if (!subscriber.lastPendingToBondingPeriodEmailSent) {
subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId
await subscriber.save()
continue
}

// Check if notification was already sent, backward to 1 or 2 rounds
const differenceAlreadySended =
+currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0)
const isNotificationAlreadySended =
differenceAlreadySended === 0 ||
differenceAlreadySended === 1 ||
differenceAlreadySended === 2
// Check if notification was already sent, backward to 1 or 2 rounds
const differenceAlreadySended =
+currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0)
const isNotificationAlreadySended =
differenceAlreadySended === 0 ||
differenceAlreadySended === 1 ||
differenceAlreadySended === 2

if (isNotificationAlreadySended) {
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round`
)
continue
}
if (isNotificationAlreadySended) {
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round`
)
continue
}

const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)

// Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status
const difference = +currentRoundId - +delegator.startRound
const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2
// Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status
const difference = +currentRoundId - +delegator.startRound
const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2

if (
isDifferenceBetweenRoundsEqualTo &&
delegator.status === constants.DELEGATOR_STATUS.Bonded
) {
subscribersToSendEmails.push(
delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded(
subscriber,
delegator.delegateAddress,
currentRoundId
if (
isDifferenceBetweenRoundsEqualTo &&
delegator.status === constants.DELEGATOR_STATUS.Bonded
) {
subscribersToSendEmails.push(
delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded(
subscriber,
delegator.delegateAddress,
currentRoundId
)
)
)
}
} catch (err) {
console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`)
continue
}
}
console.log(
Expand All @@ -181,45 +186,50 @@ const sendTelegramAfterBondingPeriodHasEndedNotificationToDelegators = async cur
const currentRoundId = currentRoundInfo.id

for (const subscriberItem of subscribers) {
const { subscriber } = subscriberItem
try {
const { subscriber } = subscriberItem

if (!subscriber.lastPendingToBondingPeriodTelegramSent) {
subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId
await subscriber.save()
continue
}
if (!subscriber.lastPendingToBondingPeriodTelegramSent) {
subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId
await subscriber.save()
continue
}

// Check if notification was already sent, backward to 1 or 2 rounds
const differenceAlreadySended =
+currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0)
const isNotificationAlreadySended =
differenceAlreadySended === 0 ||
differenceAlreadySended === 1 ||
differenceAlreadySended === 2
// Check if notification was already sent, backward to 1 or 2 rounds
const differenceAlreadySended =
+currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0)
const isNotificationAlreadySended =
differenceAlreadySended === 0 ||
differenceAlreadySended === 1 ||
differenceAlreadySended === 2

if (isNotificationAlreadySended) {
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round`
)
continue
}
if (isNotificationAlreadySended) {
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round`
)
continue
}

const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)

// Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status
const difference = +currentRoundId - +delegator.startRound
const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2
// Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status
const difference = +currentRoundId - +delegator.startRound
const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2

if (
isDifferenceBetweenRoundsEqualTo &&
delegator.status === constants.DELEGATOR_STATUS.Bonded
) {
subscribersToSendTelegrams.push(
delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded(
subscriber,
currentRoundId
if (
isDifferenceBetweenRoundsEqualTo &&
delegator.status === constants.DELEGATOR_STATUS.Bonded
) {
subscribersToSendTelegrams.push(
delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded(
subscriber,
currentRoundId
)
)
)
}
} catch (err) {
console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`)
continue
}
}
console.log(
Expand Down
2 changes: 1 addition & 1 deletion server/helpers/sendDelegatorEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ const sendDelegatorNotificationBondingPeriodHasEnded = async (
}

let body = {
transcoderAddress: truncateStringInTheMiddle(delegateAddress),
transcoderAddress: utils.truncateStringInTheMiddle(delegateAddress),
delegatingStatusUrl: `https://explorer.livepeer.org/accounts/${subscriber.address}/delegating`,
delegateAddress: delegateAddress,
templateId: sendgridTemplateIdNotificationDelegatorBondingPeriodHasEnded,
Expand Down
9 changes: 6 additions & 3 deletions server/helpers/services/protocolService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const promiseRetry = require('promise-retry')

const { calculateNextRoundInflationRatio, tokenAmountInUnits, MathBN } = require('../utils')

const { CACHE_UPDATE_INTERVAL, PROTOCOL_DIVISION_BASE } = require('../../../config/constants')
const {
CACHE_UPDATE_INTERVAL,
PROTOCOL_DIVISION_BASE,
LIVEPEER_DEFAULT_CONSTANTS
} = require('../../../config/constants')

const defaultProtocolSource = require('../sdk/protocol')
let protocolServiceInstance
Expand All @@ -19,7 +23,7 @@ class ProtocolService {
// The source of the functions that fetch data of the protocol, currently we are only supporting SDK
this.source = source
this.currentRound = null
this.defaultConstants = null
this.defaultConstants = LIVEPEER_DEFAULT_CONSTANTS
this.totalBonded = null
this.targetBondingRate = null
this.inflationChange = null
Expand All @@ -33,7 +37,6 @@ class ProtocolService {
// Sets an interval that will reset the cached values periodically
setInterval(() => {
this.currentRound = null
this.defaultConstants = null
this.totalBonded = null
this.targetBondingRate = null
this.inflationChange = null
Expand Down
78 changes: 45 additions & 33 deletions server/helpers/subscriberUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,32 +154,36 @@ const findTelegramSubscription = async (address, chatId) => {
}

const getSubscriptorRole = async subscriptor => {
console.log(`[Subscribers-utils] - getSubscriptorRole()`)
const { getProtocolService } = require('./services/protocolService')
const { getDelegatorService } = require('./services/delegatorService')
const protocolService = getProtocolService()
const delegatorService = getDelegatorService()

let [constants, delegator] = await promiseRetry(retry => {
return Promise.all([
protocolService.getLivepeerDefaultConstants(),
delegatorService.getDelegatorAccount(subscriptor.address)
]).catch(err => retry())
})

const { status, address, delegateAddress } = delegator

// Detect role
const role =
delegator &&
status === constants.DELEGATOR_STATUS.Bonded &&
delegateAddress &&
address.toLowerCase() === delegateAddress.toLowerCase()
? constants.ROLE.TRANSCODER
: constants.ROLE.DELEGATOR
return {
role,
constants,
delegator
try {
const constants = await protocolService.getLivepeerDefaultConstants()
console.log(
`[Subscribers-utils] - getDelegatorAccount() for subscriptor: ${subscriptor.address}`
)
const delegator = await delegatorService.getDelegatorAccount(subscriptor.address)
console.log(`[Subscribers-utils] - getDelegatorAccount() finished, detecting role`)
const { status, address, delegateAddress } = delegator

// Detect role
const role =
delegator &&
status === constants.DELEGATOR_STATUS.Bonded &&
delegateAddress &&
address.toLowerCase() === delegateAddress.toLowerCase()
? constants.ROLE.TRANSCODER
: constants.ROLE.DELEGATOR
return {
role,
constants,
delegator
}
} catch (err) {
console.error(`[Subscribers-utils] - error on getSubscriptorRole(): ${err}`)
throw err
}
}

Expand Down Expand Up @@ -287,12 +291,16 @@ const filterSubscribersByDelegatorRole = async allSubscribers => {
throw new Error('No allSubscribersList received on filterSubscribersByDelegatorRole()')
}
for (let subscriber of allSubscribers) {
const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
if (role === constants.ROLE.DELEGATOR) {
subscribersList.push({
subscriber,
delegator
})
try {
const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
if (role === constants.ROLE.DELEGATOR) {
subscribersList.push({
subscriber,
delegator
})
}
} catch (err) {
continue
}
}
return subscribersList
Expand All @@ -304,11 +312,15 @@ const filterSubscribersByDelegateRole = async allSubscribers => {
throw new Error('No allSubscribers list received on filterSubscribersByDelegateRole()')
}
for (let subscriber of allSubscribers) {
const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
if (role === constants.ROLE.TRANSCODER) {
subscribersList.push({
subscriber
})
try {
const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber)
if (role === constants.ROLE.TRANSCODER) {
subscribersList.push({
subscriber
})
}
} catch (e) {
continue
}
}
return subscribersList
Expand Down

0 comments on commit 387d780

Please sign in to comment.