Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐰 Add revenue share reminder notification #347

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions schema/auth.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type AccountNotificationPreferences {
crtRevenueShareStarted: NotificationPreference!
crtRevenueSharePlanned: NotificationPreference!
crtRevenueShareEnded: NotificationPreference!
crtRevenueShareEndedReminder: NotificationPreference!
}

type NotificationPreference {
Expand Down
18 changes: 18 additions & 0 deletions schema/notifications.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ union NotificationType =
| CreatorTokenRevenueShareStarted
| CreatorTokenRevenueSharePlanned
| CreatorTokenRevenueShareEnded
| CreatorTokenRevenueShareEndedReminder

type ChannelSuspended @variant {
phantom: Int
Expand Down Expand Up @@ -587,3 +588,20 @@ type CreatorTokenRevenueShareEnded @variant {
"id of token"
tokenId: String!
}

type CreatorTokenRevenueShareEndedReminder @variant {
"channel title for notification text"
channelTitle: String!

"channel title for notification avatar"
channelId: String!

"symbol of the token"
tokenSymbol: String!

"id of created revenue share to verify its' viability in future"
revenueShareId: String!

"id of token"
tokenId: String!
}
27 changes: 27 additions & 0 deletions src/mappings/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CreatorTokenMarketStarted,
CreatorTokenMarketStartedEventData,
CreatorTokenRevenueShareEnded,
CreatorTokenRevenueShareEndedReminder,
CreatorTokenRevenueSharePlanned,
CreatorTokenRevenueShareStarted,
CreatorTokenRevenueSplitIssuedEventData,
Expand All @@ -41,10 +42,12 @@
VestedSale,
VestingSchedule,
} from '../../model'
import { BLOCKS_PER_DAY } from '../../server-extension/resolvers/CreatorToken'
import { getCurrentBlockHeight } from '../../utils/blockHeight'
import { EventHandlerContext } from '../../utils/events'
import { criticalError } from '../../utils/misc'
import { addNotification } from '../../utils/notification'
import { removeFutureNotification } from '../../utils/notification/helpers'
import { getChannelOwnerAccount, notifyChannelFollowers, parseChannelTitle } from '../content/utils'
import { deserializeMetadata, genericEventFields } from '../utils'
import {
Expand Down Expand Up @@ -78,7 +81,7 @@
},
}: EventHandlerContext<'ProjectToken.TokenIssued'>) {
// create token
const totalSupply = initialAllocation.reduce((acc, [_, allocation]) => {

Check warning on line 84 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

'_' is defined but never used
return acc + allocation.amount
}, BigInt(0))

Expand Down Expand Up @@ -398,7 +401,7 @@
buyerAccount.totalAmount += crtMinted
}

const activeAmm = await overlay.getRepository(AmmCurve).getByIdOrFail(token.currentAmmSaleId!)

Check warning on line 404 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion

activeAmm.mintedByAmm += crtMinted
const tx = overlay.getRepository(AmmTransaction).new({
Expand Down Expand Up @@ -458,7 +461,7 @@
.getOneByRelationOrFail('tokenId', tokenId.toString())
const channel = await overlay.getRepository(Channel).getByIdOrFail(tokenChannel.channelId)
token.totalSupply -= crtBurned
const activeAmm = await overlay.getRepository(AmmCurve).getByIdOrFail(token.currentAmmSaleId!)

Check warning on line 464 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
const ammId = activeAmm.id

const sellerAccount = await getTokenAccountByMemberByTokenOrFail(overlay, memberId, tokenId)
Expand Down Expand Up @@ -531,7 +534,7 @@
}

const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
const sale = await overlay.getRepository(Sale).getByIdOrFail(token.currentSaleId!)

Check warning on line 537 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
sale.tokensSold += amountPurchased

const tx = overlay.getRepository(SaleTransaction).new({
Expand Down Expand Up @@ -591,7 +594,7 @@
},
}: EventHandlerContext<'ProjectToken.UpcomingTokenSaleUpdated'>) {
const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
const sale = await overlay.getRepository(Sale).getByIdOrFail(token.currentSaleId!)

Check warning on line 597 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion

if (newStart) {
sale.startBlock = newStart
Expand Down Expand Up @@ -695,6 +698,26 @@
event,
endsAt // schedule for end block
)

// This event should have dispatch block of ending block plus about 3 days
// If user closes the share before its execution, this notification will be removing during share closing mapping
const revenueSharedEndedReminderNotification = new CreatorTokenRevenueShareEndedReminder({
revenueShareId: revenueShare.id,
channelTitle: parseChannelTitle(channel),
channelId: channel.id,
tokenSymbol: parseCreatorTokenSymbol(token),
tokenId: tokenId.toString(),
})

const channelOwnerAccount = await getChannelOwnerAccount(overlay, channel)
await addNotification(
overlay,
channelOwnerAccount,
new ChannelRecipient({ channel: channel.id }),
revenueSharedEndedReminderNotification,
undefined,
endsAt + BLOCKS_PER_DAY * 3 // schedule for end block
)
}

export async function processMemberJoinedWhitelistEvent({
Expand All @@ -710,13 +733,13 @@
export async function processAmmDeactivatedEvent({
overlay,
event: {
asV2002: [tokenId, , burnedAmount],

Check warning on line 736 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

'burnedAmount' is defined but never used
},
}: EventHandlerContext<'ProjectToken.AmmDeactivated'>) {
const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
token.status = TokenStatus.IDLE

const activeAmm = await overlay.getRepository(AmmCurve).getByIdOrFail(token.currentAmmSaleId!)

Check warning on line 742 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
activeAmm.finalized = true

token.currentAmmSaleId = null
Expand Down Expand Up @@ -755,12 +778,12 @@
},
}: EventHandlerContext<'ProjectToken.TokenSaleFinalized'>) {
const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
const sale = await overlay.getRepository(Sale).getByIdOrFail(token.currentSaleId!)

Check warning on line 781 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
sale.finalized = true

const sourceAccount = await overlay
.getRepository(TokenAccount)
.getByIdOrFail(sale.fundsSourceAccountId!)

Check warning on line 786 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
sourceAccount.totalAmount += quantityLeft

token.status = TokenStatus.IDLE
Expand Down Expand Up @@ -796,9 +819,13 @@
const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
const revenueShare = await overlay
.getRepository(RevenueShare)
.getByIdOrFail(token.currentRevenueShareId!)

Check warning on line 822 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

Forbidden non-null assertion
revenueShare.finalized = true
token.currentRevenueShareId = null

await removeFutureNotification(overlay.getEm(), 'CreatorTokenRevenueShareEndedReminder', 1, {
tokenId: token.id,
})
}

export async function processUserParticipatedInSplitEvent({
Expand Down
45 changes: 44 additions & 1 deletion src/utils/notification/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flat } from 'lodash'
import { EntityManager } from 'typeorm'
import { EntityManager, FindOptionsWhere } from 'typeorm'
import {
Account,
AccountNotificationPreferences,
Expand Down Expand Up @@ -72,6 +72,7 @@ export function defaultNotificationPreferences(): AccountNotificationPreferences
crtRevenueShareStarted: notificationPrefAllTrue(),
crtRevenueSharePlanned: notificationPrefAllTrue(),
crtRevenueShareEnded: notificationPrefAllTrue(),
crtRevenueShareEndedReminder: notificationPrefAllTrue(),
})
}

Expand Down Expand Up @@ -144,6 +145,9 @@ export function preferencesForNotification(
return preferences.crtRevenueSharePlanned
case 'CreatorTokenRevenueShareEnded':
return preferences.crtRevenueShareEnded
case 'CreatorTokenRevenueShareEndedReminder':
return preferences.crtRevenueShareEnded

default: // all the remaining notifications (v2 scope) are not enabled by default
return new NotificationPreference({ inAppEnabled: false, emailEnabled: false })
}
Expand Down Expand Up @@ -299,6 +303,45 @@ export const addNotification = async (
}
}

export async function removeFutureNotification(
em: EntityManager,
notificationType: Notification['notificationType']['isTypeOf'],
currentBlockHeight: number,
filters: Record<string, string | number>
) {
const result: { id: string }[] = await em.query(
`
SELECT
id
FROM
public.notification
WHERE
notification_type ->> 'isTypeOf'=$1
AND
dispatch_block > $2
${Object.keys(filters).map((key, idx) => {
return `
AND
notification_type ->> '${key}'=$${idx + 3}
`
})}
`,
[notificationType, currentBlockHeight, ...Object.values(filters)]
)
const notificationToRemove = result[0]

if (!notificationToRemove) {
return
}

await em.getRepository(Notification).delete({
id: notificationToRemove.id,
})
await em.getRepository(NotificationEmailDelivery).delete({
notificationId: notificationToRemove.id,
})
}

async function saveNextNotificationId(
em: EntityManager,
nextNotificationId: number,
Expand Down
10 changes: 10 additions & 0 deletions src/utils/notification/notificationsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,15 @@ export const getNotificationData = async (
subject: `🔓 ${channelTitle} ended revenue share for $${tokenSymbol} token. Unlock your locked tokens!`,
}
}
case 'CreatorTokenRevenueShareEndedReminder': {
const { tokenSymbol, channelId } = notificationType
return {
icon: await getNotificationIcon(em, 'payout'),
link: await getNotificationLink(em, 'portfolio'),
avatar: await getNotificationAvatar(em, 'channelId', channelId),
text: `🔓 Your $${tokenSymbol} revenue share is waiting to be closed. Unlock locked tokens and open your market!`,
subject: `🔓 Your $${tokenSymbol} revenue share is waiting to be closed. Unlock locked tokens and open your market!`,
}
}
}
}
Loading