diff --git a/Kusama/kusama-starter/project.ts b/Kusama/kusama-starter/project.ts index bcb9762..461c33a 100644 --- a/Kusama/kusama-starter/project.ts +++ b/Kusama/kusama-starter/project.ts @@ -44,7 +44,7 @@ const project: SubstrateProject = { dataSources: [ { kind: SubstrateDatasourceKind.Runtime, - startBlock: 25845986, + startBlock: 1, mapping: { file: "./dist/index.js", handlers: [ @@ -55,21 +55,21 @@ const project: SubstrateProject = { modulo: 100, }, },*/ - { + /*{ kind: SubstrateHandlerKind.Call, handler: "handleCall", - // filter: { - // module: "balances", - // }, + filter: { + module: "balances", + }, + },*/ + { + kind: SubstrateHandlerKind.Event, + handler: "handleEvent", + filter: { + module: "balances", + method: "Transfer", + }, }, - // { - // kind: SubstrateHandlerKind.Event, - // handler: "handleEvent", - // // filter: { - // // module: "balances", - // // method: "Transfer", - // // }, - // }, ], }, }, diff --git a/Kusama/kusama-starter/src/mappings/mappingHandlers.ts b/Kusama/kusama-starter/src/mappings/mappingHandlers.ts index 1c32cd2..4091413 100644 --- a/Kusama/kusama-starter/src/mappings/mappingHandlers.ts +++ b/Kusama/kusama-starter/src/mappings/mappingHandlers.ts @@ -7,56 +7,47 @@ import { Account, Transfer } from "../types"; import { Balance } from "@polkadot/types/interfaces"; import { decodeAddress } from "@polkadot/util-crypto"; -let lastTransfer: Transfer; - export async function handleBlock(block: SubstrateBlock): Promise { // Do something with each block handler here } export async function handleCall(extrinsic: SubstrateExtrinsic): Promise { - logger.info(`Handle extrinsic ${extrinsic.idx}`) // Do something with a call handler here } export async function handleEvent(event: SubstrateEvent): Promise { - logger.info() - // logger.info( - // `New transfer event found at block ${event.block.block.header.number.toString()}`, - // ); + logger.info( + `New transfer event found at block ${event.block.block.header.number.toString()}`, + ); // Get data from the event // The balances.transfer event has the following payload \[from, to, value\] // logger.info(JSON.stringify(event)); - // const { - // event: { - // data: [from, to, amount], - // }, - // } = event; - - // const blockNumber: number = event.block.block.header.number.toNumber(); + const { + event: { + data: [from, to, amount], + }, + } = event; - // const fromAccount = await checkAndGetAccount(from.toString(), blockNumber); - // const toAccount = await checkAndGetAccount(to.toString(), blockNumber); + const blockNumber: number = event.block.block.header.number.toNumber(); - // // Create the new transfer entity - // const transfer = Transfer.create({ - // id: `${event.block.block.header.number.toNumber()}-${event.idx}`, - // blockNumber, - // date: event.block.timestamp, - // fromId: fromAccount.id, - // toId: toAccount.id, - // amount: (amount as Balance).toBigInt(), - // }); + const fromAccount = await checkAndGetAccount(from.toString(), blockNumber); + const toAccount = await checkAndGetAccount(to.toString(), blockNumber); - // fromAccount.lastTransferBlock = blockNumber; - // toAccount.lastTransferBlock = blockNumber; + // Create the new transfer entity + const transfer = Transfer.create({ + id: `${event.block.block.header.number.toNumber()}-${event.idx}`, + blockNumber, + date: event.block.timestamp, + fromId: fromAccount.id, + toId: toAccount.id, + amount: (amount as Balance).toBigInt(), + }); - // if (lastTransfer) { - // await Transfer.remove(lastTransfer.id); - // lastTransfer = transfer - // } + fromAccount.lastTransferBlock = blockNumber; + toAccount.lastTransferBlock = blockNumber; - // await Promise.all([fromAccount.save(), toAccount.save(), transfer.save()]); + await Promise.all([fromAccount.save(), toAccount.save(), transfer.save()]); } async function checkAndGetAccount(