Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![](https://media.tenor.com/Eu0LNbU4hQMAAAAC/jeanne-darc-vanitas-no-carte.gif)

[Squid](https://docs.subsquid.io) based data used to index, process, and query on top of AssetHub for [KodaDot](https://kodadot.xyz) NFT Marketplace.
[Squid](https://docs.subsquid.io) based data used to index, process, and query on top of AssetHub for Polkadot & Kusama NFT Marketplaces.

## Hosted Squids

Expand Down
25 changes: 21 additions & 4 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Asset, NonFungible, NonFungibleCall, NewNonFungible, Unique } from '../
import * as a from './assets'
import * as n from './nfts'
import * as u from './uniques'
import { BatchContext, Context, SelectedEvent } from './utils/types'
import { BatchContext, Block, Context, SelectedEvent } from './utils/types'
import { ParachainSystemCall } from '../processable'
import { updateSwapsCache } from './utils/cache'

type HandlerFunction = <T extends SelectedEvent>(item: T, ctx: Context) => Promise<void>
Expand Down Expand Up @@ -199,6 +200,18 @@ const globalHandler: Record<string, HandlerFunction> = {
Assets: assets,
}

function getRelayParentNumber(block: Block): number | undefined {
let relayParentNumber: number | undefined

const validationCall = block.calls?.find((c) => c.name === ParachainSystemCall.setValidationData)

if (validationCall) {
relayParentNumber = validationCall.args?.data?.validationData?.relayParentNumber as number | undefined
}

return relayParentNumber
}

/**
* mainFrame is the main entry point for processing a batch of blocks
**/
Expand All @@ -215,6 +228,8 @@ export async function mainFrame(ctx: BatchContext<Store>): Promise<void> {
)

for (const block of ctx.blocks) {
const relayParentNumber = getRelayParentNumber(block)

for (let event of block.events) {
logger.debug(`Processing ${event.name}`)
const [pallet] = event.name.split('.')
Expand All @@ -228,16 +243,18 @@ export async function mainFrame(ctx: BatchContext<Store>): Promise<void> {
store: ctx.store,
extrinsic: event.extrinsic,
call: event.call,
relayParentNumber,
})
// const item = event
}
}

if (ctx.isHead) {
const lastBlock = ctx.blocks[ctx.blocks.length - 1].header
const lastDate = new Date(lastBlock.timestamp || Date.now())
const lastBlock = ctx.blocks[ctx.blocks.length - 1]
const relayParentNumber = getRelayParentNumber(lastBlock)
const lastDate = new Date(lastBlock.header.timestamp || Date.now())
logger.info(`Found head block, updating cache`)
await updateSwapsCache(lastDate, lastBlock.height, ctx.store)
await updateSwapsCache(lastDate, (relayParentNumber || lastBlock.header.height), ctx.store)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mappings/nfts/createSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function handleCreateSwap(context: Context): Promise<void> {
if (!offer) {
(final as Swap).surcharge = event.surcharge || null
}
final.status = final.blockNumber >= deadline ? TradeStatus.EXPIRED : TradeStatus.ACTIVE
final.status = (context.relayParentNumber || final.blockNumber) >= deadline ? TradeStatus.EXPIRED : TradeStatus.ACTIVE
final.updatedAt = event.timestamp

await context.store.save(final)
Expand Down
2 changes: 2 additions & 0 deletions src/mappings/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export type Context<S = Store> = {
event: SelectedEvent
extrinsic: SelectedExtrinsic | undefined
call: SelectedCall | undefined
/** Relay chain block number */
relayParentNumber: number | undefined
// log: Logger
}

Expand Down
8 changes: 8 additions & 0 deletions src/processable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ export enum NonFungibleCall {
claimSwap = 'Nfts.claim_swap',
}

/**
* Parachain System Calls
* Used to extract relay chain context (e.g., relay parent block number)
*/
export enum ParachainSystemCall {
setValidationData = 'ParachainSystem.set_validation_data',
}

/**
* Assets Pallet Events
* @enum {string}
Expand Down
6 changes: 4 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
} from '@subsquid/substrate-processor'
import { TypeormDatabase as Database } from '@subsquid/typeorm-store'
import logger from './mappings/utils/logger'
import { Asset, NewNonFungible, NonFungible, NonFungibleCall, Unique } from './processable'
import { Asset, NewNonFungible, NonFungible, NonFungibleCall, ParachainSystemCall, Unique } from './processable'

import { CHAIN, getArchiveUrl, getNodeUrl, UNIQUES_ENABLED } from './environment'
import { mainFrame } from './mappings'
Expand Down Expand Up @@ -93,6 +93,9 @@ processor.addEvent({ name: [NewNonFungible.claimSwap], call: true, extrinsic: tr
// IMPORTANT: THIS IS CALL NOT EVENT!
processor.addCall({ name: [NonFungibleCall.updateMintSettings], extrinsic: true })

// Capture relay chain context from inherent call
processor.addCall({ name: [ParachainSystemCall.setValidationData] })

processor.setFields(fieldSelection)

/**
Expand All @@ -109,4 +112,3 @@ logger.info(`PROCESSING ~~ ${CHAIN.toUpperCase()} ~~ EVENTS`)

// mainFrame function is called when the processor is ready to process the data
processor.run(database, mainFrame)