Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
justraman committed Apr 20, 2024
1 parent dc09123 commit 2734785
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/mappings/identity/events/subIdentityAdded.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,40 @@
import { SubstrateBlock } from '@subsquid/substrate-processor'
import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection'
import { u8aToHex, u8aToString } from '@polkadot/util'
import { hexToString } from '@polkadot/util'
import { CallNotDefinedError, UnknownVersionError } from '../../../common/errors'
import { IdentitySubIdentityAddedEvent } from '../../../types/generated/events'
import { events, calls } from '../../../types/generated'
import { Event as EventModel, Identity, Registration } from '../../../model'
import { Call, Event } from '../../../types/generated/support'
import { CommonContext } from '../../types/contexts'
import { CommonContext, CallItem, BlockHeader, EventItem } from '../../types/contexts'
import { getOrCreateAccount } from '../../util/entities'
import { IdentityAddSubCall } from '../../../types/generated/calls'

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

if (data.isMatrixEnjinV1000) {
return data.asMatrixEnjinV1000
function getEventData(event: EventItem) {
if (events.identity.subIdentityAdded.matrixEnjinV1000.is(event)) {
return events.identity.subIdentityAdded.matrixEnjinV1000.decode(event)
}

throw new UnknownVersionError(data.constructor.name)
throw new UnknownVersionError(events.identity.subIdentityAdded.name)
}

function getCallData(ctx: CommonContext, call: Call) {
const data = new IdentityAddSubCall(ctx, call)

if (data.isMatrixEnjinV1000) {
return data.asMatrixEnjinV1000
function getCallData(call: CallItem) {
if (calls.identity.addSub.matrixEnjinV1000.is(call)) {
return calls.identity.addSub.matrixEnjinV1000.decode(call)
}

throw new UnknownVersionError(data.constructor.name)
throw new UnknownVersionError(calls.identity.addSub.name)
}

export async function subIdentityAdded(
ctx: CommonContext,
block: SubstrateBlock,
item: EventItem<'Identity.SubIdentityAdded', { event: { args: true; call: true; extrinsic: true } }>
): Promise<EventModel | undefined> {
if (!item.event.call) throw new CallNotDefinedError()
export async function subIdentityAdded(ctx: CommonContext, block: BlockHeader, item: EventItem): Promise<EventModel | undefined> {
if (!item.call) throw new CallNotDefinedError()

const eventData = getEventData(ctx, item.event)
const callData = getCallData(ctx, item.event.call)
const eventData = getEventData(item)
const callData = getCallData(item.call)

const account = await getOrCreateAccount(ctx, eventData.sub)
const main = u8aToHex(eventData.main)
const { main } = eventData

const existing = await ctx.store.findOneBy(Identity, { id: account.id })

if (existing) {
existing.super = new Identity({ id: main })
existing.name = callData.data.__kind !== 'None' ? u8aToString(callData.data.value) : null
existing.name = callData.data.__kind !== 'None' ? hexToString(callData.data.value) : null
await ctx.store.save(existing)

return undefined
Expand All @@ -55,11 +43,11 @@ export async function subIdentityAdded(
const identity = new Identity({
id: account.id,
account,
name: callData.data.__kind !== 'None' ? u8aToString(callData.data.value) : null,
name: callData.data.__kind !== 'None' ? hexToString(callData.data.value) : null,
super: new Identity({ id: main }),
isSub: true,
info: new Registration({ id: main }),
createdAt: new Date(block.timestamp),
createdAt: new Date(block.timestamp ?? 0),
})

await ctx.store.save(identity)
Expand Down

0 comments on commit 2734785

Please sign in to comment.