From bf5680049d40c6b6560f433457000713291d6a42 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Fri, 29 Nov 2024 12:02:45 +0800 Subject: [PATCH] Add indexer for evm tx --- .../src/scan/events/evm/tx/index.js | 32 ++++++++++++++++--- .../block-scan/src/scan/events/index.js | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/backend/packages/block-scan/src/scan/events/evm/tx/index.js b/backend/packages/block-scan/src/scan/events/evm/tx/index.js index d3c5265d..e9ed4d37 100644 --- a/backend/packages/block-scan/src/scan/events/evm/tx/index.js +++ b/backend/packages/block-scan/src/scan/events/evm/tx/index.js @@ -7,26 +7,50 @@ const { queryAndGetReceipt } = require("./receipt"); const { env: { currentChain }, } = require("@osn/scan-common"); +const isNil = require("lodash.isnil"); -async function handleOneTransaction(tx) { +function findExtrinsicIndex(txHash, events) { + const targetEvent = events.find((e) => { + const { event } = e; + const { section, method } = event; + if ("ethereum" !== section || "Executed" !== method) { + return false; + } + + return event.data[2].toString() === txHash; + }); + if (!targetEvent) { + return null; + } + + return targetEvent.phase.isNone ? null : targetEvent.phase.value.toNumber(); +} + +async function handleOneTransaction(tx, blockIndexer, blockEvents) { const col = await getEvmTxCol(); const receipt = await queryAndGetReceipt(tx.hash); + const extrinsicIndex = findExtrinsicIndex(tx.hash, blockEvents); + const indexer = { + ...blockIndexer, + ...(isNil(extrinsicIndex) ? {} : { extrinsicIndex }), + }; await col.insertOne({ + indexer, ...normalizeTx(tx), receipt, }); } -async function queryAndSaveEvmTxs(blockHeight) { +async function queryAndSaveEvmTxs(blockIndexer, blockEvents) { if (!["laos"].includes(currentChain())) { return; } const web3 = getEvmWeb3Instance(); - const block = await web3.eth.getBlock(blockHeight, true); + const block = await web3.eth.getBlock(blockIndexer.blockHeight, true); for (const transaction of block.transactions) { - await handleOneTransaction(transaction); + await handleOneTransaction(transaction, blockIndexer, blockEvents); } } diff --git a/backend/packages/block-scan/src/scan/events/index.js b/backend/packages/block-scan/src/scan/events/index.js index 962abe10..68f94910 100644 --- a/backend/packages/block-scan/src/scan/events/index.js +++ b/backend/packages/block-scan/src/scan/events/index.js @@ -30,7 +30,7 @@ async function handleEvents(events = [], blockIndexer, extrinsics = []) { const transfers = getBlockNativeTransfers(blockIndexer.blockHash); await batchInsertTransfers(transfers); - await queryAndSaveEvmTxs(blockIndexer.blockHeight); + await queryAndSaveEvmTxs(blockIndexer, events); clearNativeTransfers(blockIndexer.blockHash); clearEvmBlockMark(blockIndexer.blockHeight);