hydra
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!
Handers receive a signle argument of type defined in @dzlzv/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 mapping generated by the scaffolder
1
import {
2
Transfer,
3
} from '../generated/graphql-server/model'
4
5
// run 'NODE_URL=<RPC_ENDPOINT> EVENTS=<comma separated list of events> yarn codegen:mappings-types'
6
// to genenerate typescript classes for events, such as Balances.TransferEvent
7
import { Balances } from './generated/types'
8
import BN from 'bn.js'
9
import {
10
ExtrinsicContext,
11
EventContext,
12
BlockContext,
13
StoreContext,
14
} from '@dzlzv/hydra-common'
15
16
export async function balancesTransfer({
17
store,
18
event,
19
block,
20
extrinsic,
21
}: EventContext & StoreContext) {
22
const transfer = new Transfer()
23
const [from, to, value] = new Balances.TransferEvent(event).params
24
transfer.from = from.toHuman()
25
transfer.to = to.toHuman()
26
transfer.value = value.toBn()
27
transfer.tip = extrinsic ? new BN(extrinsic.tip.toString(10)) : new BN(0)
28
transfer.insertedAt = new Date(block.timestamp)
29
30
transfer.block = block.height
31
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
32
transfer.timestamp = new BN(block.timestamp)
33
console.log(`Saving transfer: ${JSON.stringify(transfer, null, 2)}`)
34
await store.save<Transfer>(transfer)
35
}
36
Copied!
Note that the required entity classes are exported from
1
../generated/graphql-server/model
Copied!
and this is where all the auto-generated classes live by default.
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 and can be imported from ./generated/types. 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: ./mappings/generated/types
Copied!
Last modified 6mo ago
Copy link