Concordium Mapping
Concordium Mapping
Mapping functions define how chain data is transformed into the optimised GraphQL entities that we have previously defined in the schema.graphql
file.
- Mappings are defined in the
src/mappings
directory and are exported as a function. - These mappings are also exported in
src/index.ts
. - The mappings files are reference in
project.ts
under the mapping handlers. - The mappings are run from within a Sandbox
There are different classes of mappings functions for Concordium: Block handlers, Transaction Handlers, Transaction Event Handlers and Special Event Handlers.
Block Handler
You can use block handlers to capture information each time a new block is attached to the chain, e.g. block number. To achieve this, a defined BlockHandler will be called once for every block.
Using block handlers slows your project down as they can be executed with each and every block - only use if you need to.
import { ConcordiumBlock } from "@subql/types-concordium";
export async function handleBlock(block: ConcordiumBlock): Promise<void> {
const record = BlockEntity.create({
id: block.blockHash,
field1: block.blockHeight,
field2: "block",
});
await record.save();
}
Transaction Handler
You can use transaction handlers to capture information about each of the transactions in a block. To achieve this, a defined TransactionHandler will be called once for every transaction. You should use Mapping Filters in your manifest to filter transactions to reduce the time it takes to index data and improve mapping performance.
import { ConcordiumTransaction } from "@subql/types-concordium";
export async function handleTransaction(
tx: ConcordiumTransaction,
): Promise<void> {
logger.info(
`Handling transaction at block ${tx.block.blockHeight.toString()}`,
);
const record = TransactionEntity.create({
id: tx.hash,
field1: tx.type,
field2: "tx",
});
await record.save();
}
Transaction Event Handler
You can use transaction event handlers to capture information about each of the transaction events in a block. To achieve this, a defined TransactionEventHandler will be called once for every transaction event. You should use Mapping Filters in your manifest to filter transactions events to reduce the time it takes to index data and improve mapping performance.
import { ConcordiumTransactionEvent } from "@subql/types-concordium";
export async function handleTransactionEvent(
txEvent: ConcordiumTransactionEvent,
): Promise<void> {
logger.info(
`Handling event at block ${txEvent.block.blockHeight.toString()}`,
);
const record = TransactionEventEntity.create({
id: txEvent.id,
field1: txEvent.tag,
field2: "txEvent",
});
await record.save();
}
Special Event Handler
You can use special event handlers to capture information about each of the special events in a block. To achieve this, a defined SpecialEventHandler will be called once for every special event. You should use Mapping Filters in your manifest to filter special events to reduce the time it takes to index data and improve mapping performance.
import { ConcordiumSpecialEvent } from "@subql/types-concordium";
export async function handleSpecialEvent(
specialEvent: ConcordiumSpecialEvent,
): Promise<void> {
logger.info(
`Handling special event at block ${specialEvent.block.blockHeight.toString()}`,
);
const record = SpecialEventEntity.create({
id: specialEvent.id,
field1: specialEvent.tag,
field2: "specialEvent",
});
await record.save();
}