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

fix: remove duplicate CRT notifications creation #343

Merged
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 4.0.4

## Bug Fixes:
- Fixed: avoiding IDs override/conflict while creating concurrent runtime notifications by reading/updating the `nextEntityId` from the `overlay` instead of DB - [#342](https://github.com/Joystream/orion/pull/342)
- Fixed: removed possibility of creating duplicate CRT notifications in `processAmmActivatedEvent` and `processTokenSaleInitializedEvent` mappings if the account being notified is both channel follower as well as token holder - [#343](https://github.com/Joystream/orion/pull/343)

# 4.0.4

## Bug Fixes:
- Fixed: improve the accuracy of `Video.orionLanguage` field by reworking the `predictVideoLanguage` function in `src/utils/language.ts`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orion",
"version": "4.0.4",
"version": "4.0.5",
"engines": {
"node": ">=16"
},
Expand Down
19 changes: 15 additions & 4 deletions src/mappings/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
createAccount,
getTokenAccountByMemberByToken,
getTokenAccountByMemberByTokenOrFail,
notifyChannelFollowersAndTokenHolders,
notifyTokenHolders,
parseCreatorTokenSymbol,
processTokenMetadata,
Expand All @@ -77,7 +78,7 @@
},
}: EventHandlerContext<'ProjectToken.TokenIssued'>) {
// create token
const totalSupply = initialAllocation.reduce((acc, [_, allocation]) => {

Check warning on line 81 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 @@ -248,8 +249,13 @@
channelTitle: parseChannelTitle(channel),
})

await notifyTokenHolders(overlay, tokenId.toString(), notificationData, eventEntity)
await notifyChannelFollowers(overlay, channel.id, notificationData, eventEntity)
await notifyChannelFollowersAndTokenHolders(
overlay,
channel.id,
tokenId.toString(),
notificationData,
eventEntity
)
}

export async function processTokenSaleInitializedEvent({
Expand Down Expand Up @@ -333,8 +339,13 @@
channelTitle: parseChannelTitle(channel),
})

await notifyTokenHolders(overlay, tokenId.toString(), notificationData, eventEntity)
await notifyChannelFollowers(overlay, channel.id, notificationData, eventEntity)
await notifyChannelFollowersAndTokenHolders(
overlay,
channel.id,
tokenId.toString(),
notificationData,
eventEntity
)
}

export async function processPatronageRateDecreasedToEvent({
Expand Down Expand Up @@ -387,7 +398,7 @@
buyerAccount.totalAmount += crtMinted
}

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

Check warning on line 401 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 @@ -447,7 +458,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 461 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 @@ -520,7 +531,7 @@
}

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

Check warning on line 534 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 @@ -580,7 +591,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 594 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 @@ -699,13 +710,13 @@
export async function processAmmDeactivatedEvent({
overlay,
event: {
asV2002: [tokenId, , burnedAmount],

Check warning on line 713 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 719 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 @@ -744,12 +755,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 758 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 763 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 @@ -785,7 +796,7 @@
const token = await overlay.getRepository(CreatorToken).getByIdOrFail(tokenId.toString())
const revenueShare = await overlay
.getRepository(RevenueShare)
.getByIdOrFail(token.currentRevenueShareId!)

Check warning on line 799 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
}
Expand Down
38 changes: 38 additions & 0 deletions src/mappings/token/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { uniqueId } from '../../utils/crypto'
import { criticalError } from '../../utils/misc'
import { addNotification } from '../../utils/notification'
import { EntityManagerOverlay, Flat } from '../../utils/overlay'
import { getFollowersAccountsForChannel } from '../content/utils'

export const FALLBACK_TOKEN_SYMBOL = '??'

Expand Down Expand Up @@ -366,3 +367,40 @@ export async function notifyTokenHolders(
)
)
}

export async function notifyChannelFollowersAndTokenHolders(
overlay: EntityManagerOverlay,
channelId: string,
tokenId: string,
notificationType: NotificationType,
event?: Event,
dispatchBlock?: number
) {
const [followersAccounts, holderAccounts] = await Promise.all([
getFollowersAccountsForChannel(overlay, channelId),
getHolderAccountsForToken(overlay, tokenId),
])

// Combine followers and holders, removing duplicates
const allAccounts = [...followersAccounts, ...holderAccounts]
const accounts = Array.from(new Set(allAccounts.map((a) => a.id)))
.map((id) => allAccounts.find((account) => account.id === id))
.filter((account): account is Account => account !== undefined)

const limit = pLimit(10) // Limit to 10 concurrent promises

await Promise.all(
accounts.map((account) =>
limit(() =>
addNotification(
overlay,
account,
new MemberRecipient({ membership: account.membershipId }),
notificationType,
event,
dispatchBlock
)
)
)
)
}
Loading