subsquid
Search…
Mappings
Mappings describe how Substate events and transactions should be handled by Hydra processor
A mapping file is a standalone typescript module defining how the Substrate events of interest should be handled by the Hydra indexer.
The event handlers (aka mappings) should be defined in the manifest file:
1
mappings:
2
eventHandlers:
3
- event: balances.Transfer
4
handler: balancesTransfer
5
extrinsicHandlers:
6
- extrinsic: timestamp.set
7
handler: timestampCall
Copied!
Handlers receive a single argument of type defined in @subsquid/hydra-common
    eventHanlder receives a single argument of type EventContext & StoreContext
    extrinsicHandlerreceives a single argument of type ExtrinsicContext & StoreContext
    pre/postBlockHooksreceive a single argument of type BlockContext & StoreContext
Let us look at the sample mappings/index.ts module generated by the scaffolder
1
import { Transfer } from '../generated/model'
2
import { Balances } from '../chain'
3
import BN from 'bn.js'
4
import {
5
ExtrinsicContext,
6
EventContext,
7
BlockContext,
8
StoreContext,
9
} from '@subsquid/hydra-common'
10
11
export async function balancesTransfer({
12
store,
13
event,
14
block,
15
extrinsic,
16
}: EventContext & StoreContext) {
17
const transfer = new Transfer()
18
const [from, to, value] = new Balances.TransferEvent(event).params
19
transfer.from = from.toHuman()
20
transfer.to = to.toHuman()
21
transfer.value = value.toBn()
22
transfer.tip = extrinsic ? new BN(extrinsic.tip.toString(10)) : new BN(0)
23
transfer.insertedAt = new Date(block.timestamp)
24
25
transfer.block = block.height
26
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
27
transfer.timestamp = new BN(block.timestamp)
28
console.log(`Saving transfer: ${JSON.stringify(transfer, null, 2)}`)
29
await store.save<Transfer>(transfer)
30
}
Copied!
Note that the required entity classes are imported from
1
../generated/model
Copied!
and this is where all the auto-generated classes live.
Note how the handlers use the type-safe event classes Balances.TransferEvent provided by typegen, and so native methods like toHuman() become available
All the events and extrinsics for which we need the generate the types should be defined in the typegen section like below. See also Hydra Typegen.
1
typegen:
2
metadata:
3
source: wss://rpc.polkadot.io
4
blockHash: '0xab5c9230a7dde8bb90a6728ba4a0165423294dac14336b1443f865b796ff682c'
5
events:
6
- balances.Transfer
7
calls:
8
- timestamp.set
9
outDir: chain
Copied!
Last modified 7d ago
Copy link