Skip to content

Commit

Permalink
[IDXR-25] Adds CollectionTransferred event (#1017)
Browse files Browse the repository at this point in the history
* Adds CollectionTransferred event

* Fixes
  • Loading branch information
leonardocustodio authored Apr 17, 2024
1 parent 31e6cfe commit 5c6c224
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 6 deletions.
8 changes: 7 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ union EventData = MultiTokensApproved | MultiTokensAttributeRemoved | MultiToken
| MultiTokensTokenDestroyed | MultiTokensTokenMutated | MultiTokensTransferred | MultiTokensUnapproved
| MarketplaceAuctionFinalized | MarketplaceBidPlaced | MarketplaceListingCancelled | MarketplaceListingCreated
| MarketplaceListingFilled | BalancesTransfer | TeleportBalanceWithdrawn | ClaimsClaimRequested | ClaimsClaimed
| FuelTankCreated | FuelTankDestroyed | MultiTokensClaimedCollections | MultiTokensClaimTokensInitiated | MultiTokensClaimTokensCompleted
| FuelTankCreated | FuelTankDestroyed | MultiTokensClaimedCollections | MultiTokensClaimTokensInitiated
| MultiTokensClaimTokensCompleted | MultiTokensCollectionTransferred

type BalancesTransfer {
from: Account!
Expand Down Expand Up @@ -181,6 +182,11 @@ type MultiTokensCollectionMutated {
extrinsic: Extrinsic
}

type MultiTokensCollectionTransferred {
collectionId: BigInt!
owner: Account!
}

type MultiTokensFrozen {
extrinsic: Extrinsic
}
Expand Down
8 changes: 4 additions & 4 deletions src/mappings/multiTokens/events/collection_mutated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getEventData(ctx: CommonContext, event: Event) {

return {
collectionId,
owner: mutation.owner,
owner: null, // For this chainSpec owner is changed on CollectionTransferred event
royalty: mutation.royalty,
explicitRoyaltyCurrencies: mutation.explicitRoyaltyCurrencies,
}
Expand All @@ -37,7 +37,7 @@ function getEventData(ctx: CommonContext, event: Event) {

return {
collectionId,
owner: mutation.owner,
owner: null, // For this chainSpec owner is changed on CollectionTransferred event
royalty: mutation.royalty,
explicitRoyaltyCurrencies: mutation.explicitRoyaltyCurrencies,
}
Expand All @@ -59,7 +59,7 @@ function getEventData(ctx: CommonContext, event: Event) {

return {
collectionId,
owner: mutation.owner,
owner: null, // For this chainSpec owner is changed on CollectionTransferred event
royalty: mutation.royalty,
explicitRoyaltyCurrencies: mutation.explicitRoyaltyCurrencies,
}
Expand All @@ -70,7 +70,7 @@ function getEventData(ctx: CommonContext, event: Event) {

return {
collectionId,
owner: mutation.owner,
owner: null, // For this chainSpec owner is changed on CollectionTransferred event
royalty: mutation.royalty,
explicitRoyaltyCurrencies: mutation.explicitRoyaltyCurrencies,
}
Expand Down
66 changes: 66 additions & 0 deletions src/mappings/multiTokens/events/collection_transferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable no-restricted-syntax */
import { SubstrateBlock } from '@subsquid/substrate-processor'
import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
import { u8aToHex } from '@polkadot/util'
import { throwError, UnknownVersionError } from '../../../common/errors'
import { MultiTokensCollectionTransferredEvent } from '../../../types/generated/events'
import { Collection, Event as EventModel, Extrinsic, MultiTokensCollectionTransferred } from '../../../model'
import { Event } from '../../../types/generated/support'
import { CommonContext } from '../../types/contexts'
import { getOrCreateAccount } from '../../util/entities'

function getEventData(ctx: CommonContext, event: Event) {
const data = new MultiTokensCollectionTransferredEvent(ctx, event)

if (data.isMatrixEnjinV1004) {
const { collectionId, newOwner } = data.asMatrixEnjinV1004

return {
collectionId,
owner: newOwner,
}
}

throw new UnknownVersionError(data.constructor.name)
}

function getEvent(
item: EventItem<'MultiTokens.CollectionTransferred', { event: { args: true; extrinsic: true } }>,
data: ReturnType<typeof getEventData>
) {
return new EventModel({
id: item.event.id,
extrinsic: item.event.extrinsic?.id ? new Extrinsic({ id: item.event.extrinsic.id }) : null,
data: new MultiTokensCollectionTransferred({
collectionId: data.collectionId,
owner: u8aToHex(data.owner),
}),
})
}

export async function collectionTransferred(
ctx: CommonContext,
block: SubstrateBlock,
item: EventItem<'MultiTokens.CollectionTransferred', { event: { args: true; extrinsic: true } }>,
skipSave: boolean
): Promise<EventModel | undefined> {
const data = getEventData(ctx, item.event)
if (!data) return undefined

if (skipSave) return getEvent(item, data)

const collection = await ctx.store.findOne<Collection>(Collection, {
where: { id: data.collectionId.toString() },
})

if (!collection) {
throwError(`[CollectionTransferred] We have not found collection ${data.collectionId.toString()}`, 'fatal')
return getEvent(item, data)
}

collection.owner = await getOrCreateAccount(ctx, data.owner)

await ctx.store.save(collection)

return getEvent(item, data)
}
1 change: 1 addition & 0 deletions src/mappings/multiTokens/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './approved'
export * from './unapproved'
export * from './transferred'
export * from './collection_mutated'
export * from './collection_transferred'
export * from './token_mutated'
export * from './reserved'
export * from './unreserved'
Expand Down
4 changes: 3 additions & 1 deletion src/model/generated/_eventData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import {FuelTankDestroyed} from "./_fuelTankDestroyed"
import {MultiTokensClaimedCollections} from "./_multiTokensClaimedCollections"
import {MultiTokensClaimTokensInitiated} from "./_multiTokensClaimTokensInitiated"
import {MultiTokensClaimTokensCompleted} from "./_multiTokensClaimTokensCompleted"
import {MultiTokensCollectionTransferred} from "./_multiTokensCollectionTransferred"

export type EventData = MultiTokensApproved | MultiTokensAttributeRemoved | MultiTokensAttributeSet | MultiTokensBurned | MultiTokensCollectionAccountCreated | MultiTokensCollectionAccountDestroyed | MultiTokensCollectionCreated | MultiTokensCollectionDestroyed | MultiTokensCollectionMutated | MultiTokensFrozen | MultiTokensMinted | MultiTokensThawed | MultiTokensTokenAccountCreated | MultiTokensTokenAccountDestroyed | MultiTokensTokenCreated | MultiTokensTokenDestroyed | MultiTokensTokenMutated | MultiTokensTransferred | MultiTokensUnapproved | MarketplaceAuctionFinalized | MarketplaceBidPlaced | MarketplaceListingCancelled | MarketplaceListingCreated | MarketplaceListingFilled | BalancesTransfer | TeleportBalanceWithdrawn | ClaimsClaimRequested | ClaimsClaimed | FuelTankCreated | FuelTankDestroyed | MultiTokensClaimedCollections | MultiTokensClaimTokensInitiated | MultiTokensClaimTokensCompleted
export type EventData = MultiTokensApproved | MultiTokensAttributeRemoved | MultiTokensAttributeSet | MultiTokensBurned | MultiTokensCollectionAccountCreated | MultiTokensCollectionAccountDestroyed | MultiTokensCollectionCreated | MultiTokensCollectionDestroyed | MultiTokensCollectionMutated | MultiTokensFrozen | MultiTokensMinted | MultiTokensThawed | MultiTokensTokenAccountCreated | MultiTokensTokenAccountDestroyed | MultiTokensTokenCreated | MultiTokensTokenDestroyed | MultiTokensTokenMutated | MultiTokensTransferred | MultiTokensUnapproved | MarketplaceAuctionFinalized | MarketplaceBidPlaced | MarketplaceListingCancelled | MarketplaceListingCreated | MarketplaceListingFilled | BalancesTransfer | TeleportBalanceWithdrawn | ClaimsClaimRequested | ClaimsClaimed | FuelTankCreated | FuelTankDestroyed | MultiTokensClaimedCollections | MultiTokensClaimTokensInitiated | MultiTokensClaimTokensCompleted | MultiTokensCollectionTransferred

export function fromJsonEventData(json: any): EventData {
switch(json?.isTypeOf) {
Expand Down Expand Up @@ -69,6 +70,7 @@ export function fromJsonEventData(json: any): EventData {
case 'MultiTokensClaimedCollections': return new MultiTokensClaimedCollections(undefined, json)
case 'MultiTokensClaimTokensInitiated': return new MultiTokensClaimTokensInitiated(undefined, json)
case 'MultiTokensClaimTokensCompleted': return new MultiTokensClaimTokensCompleted(undefined, json)
case 'MultiTokensCollectionTransferred': return new MultiTokensCollectionTransferred(undefined, json)
default: throw new TypeError('Unknown json object passed as EventData')
}
}
43 changes: 43 additions & 0 deletions src/model/generated/_multiTokensCollectionTransferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from "assert"
import * as marshal from "./marshal"
import {Account} from "./account.model"

export class MultiTokensCollectionTransferred {
public readonly isTypeOf = 'MultiTokensCollectionTransferred'
private _collectionId!: bigint
private _owner!: string

constructor(props?: Partial<Omit<MultiTokensCollectionTransferred, 'toJSON'>>, json?: any) {
Object.assign(this, props)
if (json != null) {
this._collectionId = marshal.bigint.fromJSON(json.collectionId)
this._owner = marshal.string.fromJSON(json.owner)
}
}

get collectionId(): bigint {
assert(this._collectionId != null, 'uninitialized access')
return this._collectionId
}

set collectionId(value: bigint) {
this._collectionId = value
}

get owner(): string {
assert(this._owner != null, 'uninitialized access')
return this._owner
}

set owner(value: string) {
this._owner = value
}

toJSON(): object {
return {
isTypeOf: this.isTypeOf,
collectionId: marshal.bigint.toJSON(this.collectionId),
owner: this.owner,
}
}
}
1 change: 1 addition & 0 deletions src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export * from "./_fuelTankDestroyed"
export * from "./_multiTokensClaimedCollections"
export * from "./_multiTokensClaimTokensInitiated"
export * from "./_multiTokensClaimTokensCompleted"
export * from "./_multiTokensCollectionTransferred"
export * from "./accountTokenEvent.model"
export * from "./registration.model"
export * from "./_judgement"
Expand Down
3 changes: 3 additions & 0 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const processor = new SubstrateBatchProcessor()
.addEvent('MultiTokens.CollectionCreated', eventOptionsWithCall)
.addEvent('MultiTokens.CollectionDestroyed', eventOptions)
.addEvent('MultiTokens.CollectionMutated', eventOptions)
.addEvent('MultiTokens.CollectionTransferred', eventOptions)
.addEvent('MultiTokens.CollectionAccountCreated', eventOptions)
.addEvent('MultiTokens.CollectionAccountDestroyed', eventOptions)
.addEvent('MultiTokens.TokenCreated', eventOptionsWithCall)
Expand Down Expand Up @@ -153,6 +154,8 @@ async function handleEvents(
return map.multiTokens.events.collectionDestroyed(ctx, block, item, skipSave)
case 'MultiTokens.CollectionMutated':
return map.multiTokens.events.collectionMutated(ctx, block, item, skipSave)
case 'MultiTokens.CollectionTransferred':
return map.multiTokens.events.collectionTransferred(ctx, block, item, skipSave)
case 'MultiTokens.Frozen':
return map.multiTokens.events.frozen(ctx, block, item, skipSave)
case 'MultiTokens.Minted':
Expand Down

0 comments on commit 5c6c224

Please sign in to comment.