Skip to content

Commit

Permalink
Merge b9da76a into fa71ebd
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano-aguero authored Jul 26, 2019
2 parents fa71ebd + b9da76a commit 0d50bf8
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ APOLLO_API_URL=https://api.thegraph.com/subgraphs/name/graphprotocol/livepeer
MINUTES_TO_WAIT_AFTER_LAST_SENT_EMAIL=500
MINUTES_TO_WAIT_AFTER_LAST_SENT_TELEGRAM=500
EARNING_DECIMALS=6
SENDGRID_TEMPLATE_ID_NOTIFICATION_DELEGATOR_BONDING_PERIOD_HAS_ENDED=d-1e5c12f1d30e476d859ea12857b35160
7 changes: 7 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const envVarsSchema = Joi.object({
SENDGRID_TEMPLATE_ID_NOTIFICATION_DELEGATE_CHANGE_RULES: Joi.string()
.required()
.description('Sendgrid template id for notification related to delegate change of rule'),
SENDGRID_TEMPLATE_ID_NOTIFICATION_DELEGATOR_BONDING_PERIOD_HAS_ENDED: Joi.string()
.required()
.description(
'Sendgrid template id for notification related to delegator bonding period has ended'
),
SENDGRID_TEMPLATE_ID_DELEGATOR_WEEKLY_SUMMARY: Joi.string()
.required()
.description('Sendgrid template id for the summary week of the delegators'),
Expand Down Expand Up @@ -133,6 +138,8 @@ const config = {
envVars.SENDGRID_TEMPLATE_ID_CLAIM_REWARD_UNBONDED_STATE,
sendgridTemplateIdNotificationDelegateChangeRules:
envVars.SENDGRID_TEMPLATE_ID_NOTIFICATION_DELEGATE_CHANGE_RULES,
sendgridTemplateIdNotificationDelegatorBondingPeriodHasEnded:
envVars.SENDGRID_TEMPLATE_ID_NOTIFICATION_DELEGATOR_BONDING_PERIOD_HAS_ENDED,
sendgrindTemplateIdDelegatorWeeklySummary: envVars.SENDGRID_TEMPLATE_ID_DELEGATOR_WEEKLY_SUMMARY,
mainnetControllerAddress: envVars.MAINNET_CONTROLLER_ADDRESS,
rinkebyControllerAddress: envVars.RINKEBY_CONTROLLER_ADDRESS,
Expand Down
68 changes: 67 additions & 1 deletion server/helpers/notification/notificateDelegatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,71 @@ const sendEmailRewardCallNotificationToDelegators = async currentRoundInfo => {
return await Promise.all(emailsToSend)
}

const sendEmailAfterBondingPeriodHasEndedNotificationToDelegators = async currentRoundInfo => {
if (!currentRoundInfo) {
throw new Error(
'No currentRoundInfo provided on sendEmailAfterBondingPeriodHasEndedNotificationToDelegators()'
)
}
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Start sending email notification to delegators`
)

const subscribers = await subscriberUtils.getEmailSubscribersDelegators()
const subscribersToSendEmails = []
const currentRoundId = currentRoundInfo.id

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

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

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)

// 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
)
)
}
}
console.log(
`[Notificate-After-Bonding-Period-Has-Ended] - Emails subscribers to notify ${subscribersToSendEmails.length}`
)
await Promise.all(subscribersToSendEmails)

return subscribers
}

const sendTelegramRewardCallNotificationToDelegators = async currentRoundInfo => {
if (!currentRoundInfo) {
throw new Error(
Expand Down Expand Up @@ -134,7 +199,8 @@ const sendNotificationDelegateChangeRuleToDelegators = async subscribers => {
const notificateDelegatorService = {
sendEmailRewardCallNotificationToDelegators,
sendTelegramRewardCallNotificationToDelegators,
sendNotificationDelegateChangeRuleToDelegators
sendNotificationDelegateChangeRuleToDelegators,
sendEmailAfterBondingPeriodHasEndedNotificationToDelegators
}

module.exports = notificateDelegatorService
8 changes: 7 additions & 1 deletion server/helpers/notification/notificationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ const sendRoundNotifications = async (roundProgress, round, thresholdSendNotific
// Send email notifications for delegate and delegators
try {
console.log(`[Check-Round-Change] - Get all subscribers with email`)

// TODO -- Refactor as promise All
console.log(`[Check-Round-Change] - Sending email round notifications to delegators`)
await notificateDelegatorUtil.sendEmailRewardCallNotificationToDelegators(round)
console.log(`[Check-Round-Change] - Sending email round notifications to delegates`)
await notificateDelegateUtil.sendEmailRewardCallNotificationToDelegates(round)
// Send telegram notifications for delegates
console.log(
`[Check-Round-Change] - Sending bonding period has ended email notifications to delegators`
)
await notificateDelegatorUtil.sendEmailAfterBondingPeriodHasEndedNotificationToDelegators(round)

// Send telegram notifications
console.log(`[Check-Round-Change] - Sending telegram round notifications to delegators`)
await notificateDelegatorUtil.sendTelegramRewardCallNotificationToDelegators(round)
console.log(`[Check-Round-Change] - Sending telegram round notifications to delegates`)
Expand Down
34 changes: 32 additions & 2 deletions server/helpers/sendDelegatorEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
sendgridTemplateIdClaimRewardUnbondingState,
sendgridTemplateIdNotificationDelegateChangeRules,
sendgrindTemplateIdDelegatorWeeklySummary,
sendgridTemplateIdNotificationDelegatorBondingPeriodHasEnded,
earningDecimals
} = config

Expand Down Expand Up @@ -289,7 +290,7 @@ const sendDelegatorNotificationDelegateChangeRulesEmail = async (
const { email } = subscriber
try {
if (!email) {
return
throw new Error('The subscriber has no email')
}

const body = getRulesChangedTemplate(subscriber.address, delegateAddress, propertiesChanged)
Expand All @@ -304,9 +305,38 @@ const sendDelegatorNotificationDelegateChangeRulesEmail = async (
return
}

const sendDelegatorNotificationBondingPeriodHasEnded = async (
subscriber,
delegateAddress,
currentRoundId
) => {
try {
if (!subscriber.email) {
throw new Error('The subscriber has no email')
}

let body = {
transcoderAddress: truncateStringInTheMiddle(delegateAddress),
delegatingStatusUrl: `https://explorer.livepeer.org/accounts/${subscriber.address}/delegating`,
delegateAddress: delegateAddress,
templateId: sendgridTemplateIdNotificationDelegatorBondingPeriodHasEnded,
email: subscriber.email
}

await sendEmail(body)

subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId
await subscriber.save({ validateBeforeSave: false })
} catch (e) {
console.error(e)
}
return
}

const delegatorEmailUtils = {
sendDelegatorNotificationEmail,
sendDelegatorNotificationDelegateChangeRulesEmail
sendDelegatorNotificationDelegateChangeRulesEmail,
sendDelegatorNotificationBondingPeriodHasEnded
}

module.exports = delegatorEmailUtils
5 changes: 5 additions & 0 deletions server/subscriber/subscriber.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ let SubscriberSchema = new mongoose.Schema({
type: String,
default: null
},
// References the roundID in which the last pending to bonding period email was sent
lastPendingToBondingPeriodEmailSent: {
type: String,
default: null
},
createdAt: {
type: Date,
default: Date.now
Expand Down
Loading

0 comments on commit 0d50bf8

Please sign in to comment.