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: Add approvals to collection accounts in populateBlock #684

Merged
merged 3 commits into from
Oct 21, 2023
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
2 changes: 1 addition & 1 deletion src/mappings/multiTokens/events/collection_created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export async function collectionCreated(
})
.map((rc) => ctx.store.insert(RoyaltyCurrency, rc as any))

Promise.all(royaltyPromises)
await Promise.all(royaltyPromises)

return getEvent(item, eventData)
}
5 changes: 3 additions & 2 deletions src/mappings/multiTokens/events/frozen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../../../model'
import { CommonContext } from '../../types/contexts'
import { Event } from '../../../types/generated/support'
import { isTokenFrozen } from './token_created'

function getEventData(ctx: CommonContext, event: Event) {
const data = new MultiTokensFrozenEvent(ctx, event)
Expand Down Expand Up @@ -116,8 +117,6 @@ export async function frozen(
where: { id: `${data.collectionId}-${data.tokenId}` },
})

token.isFrozen = true

switch (data.freezeState?.__kind) {
case 'Permanent':
token.freezeState = FreezeState.Permanent
Expand All @@ -133,6 +132,8 @@ export async function frozen(
break
}

token.isFrozen = isTokenFrozen(token.freezeState)

ctx.store.save(token)
} else {
const collection = await ctx.store.findOneOrFail<Collection>(Collection, {
Expand Down
6 changes: 5 additions & 1 deletion src/mappings/multiTokens/events/token_created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export function getFreezeState(state: FreezeState_v500): FreezeState | null {
}
}

export function isTokenFrozen(freezeState: FreezeState | null | undefined): boolean {
return freezeState === FreezeState.Permanent || freezeState === FreezeState.Temporary
}

async function getBehavior(
ctx: CommonContext,
behavior: TokenMarketBehavior
Expand Down Expand Up @@ -595,7 +599,7 @@ export async function tokenCreated(
supply: 0n, // Supply is updated on Mint/Burn events
cap: callData.cap,
behavior: callData.behavior,
isFrozen: false,
isFrozen: isTokenFrozen(callData.freezeState),
freezeState: callData.freezeState,
minimumBalance: callData.minimumBalance,
unitPrice: callData.unitPrice,
Expand Down
22 changes: 18 additions & 4 deletions src/populateBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import {
ListingStatus,
CollectionFlags,
CollectionSocials,
CollectionApproval,
} from './model'
import { getCapType, getFreezeState } from './mappings/multiTokens/events'
import { getCapType, getFreezeState, isTokenFrozen } from './mappings/multiTokens/events'
import { isNonFungible } from './mappings/multiTokens/utils/helpers'
import { safeString } from './common/tools'
import { addAccountsToSet, saveAccounts } from './mappings/balances/processor'
Expand Down Expand Up @@ -308,10 +309,21 @@ async function syncCollectionAccounts(ctx: CommonContext, block: SubstrateBlock)

if (!account) throw Errors.accountNotFound()

let approvals = null

if (data.approvals && data.approvals.length > 0) {
approvals = data.approvals.map((approval) => {
return new CollectionApproval({
account: u8aToHex(approval[0]),
expiration: approval[1],
})
})
}

return new CollectionAccount({
id: `${collectionId}-${accountId}`,
isFrozen: data.isFrozen,
approvals: null,
approvals,
accountCount: data.accountCount,
account,
collection: new Collection({ id: collectionId }),
Expand Down Expand Up @@ -363,16 +375,18 @@ async function syncTokens(ctx: CommonContext, block: SubstrateBlock) {
minimumBalance = BigInt(Math.max(1, Number(10n ** 16n / unitPrice)))
}

const freezeState = data.freezeState ? getFreezeState(data.freezeState) : undefined

const token = new Token({
id: `${collectionId}-${tokenId}`,
tokenId,
collection,
attributeCount: data.attributeCount,
supply: data.supply,
isFrozen: false,
isFrozen: isTokenFrozen(freezeState),
cap: data.cap ? getCapType(data.cap) : null,
behavior,
freezeState: data.freezeState ? getFreezeState(data.freezeState) : undefined,
freezeState,
listingForbidden: 'listingForbidden' in data ? data.listingForbidden : false,
minimumBalance,
unitPrice,
Expand Down