-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IDXR-25] Adds CollectionTransferred event (#1017)
* Adds CollectionTransferred event * Fixes
- Loading branch information
1 parent
31e6cfe
commit 5c6c224
Showing
8 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters