From 1cb399616b8eaff75d931ee0d85d202cc9545bfa Mon Sep 17 00:00:00 2001 From: Torresmorah Date: Fri, 17 Nov 2023 17:23:48 -0600 Subject: [PATCH 1/2] feat(hapi-evm): insert many txs and fix gas used by block --- hapi-evm/src/models/transaction/queries.ts | 28 ++++++++- hapi-evm/src/services/block.service.ts | 72 ++++++++++++---------- 2 files changed, 67 insertions(+), 33 deletions(-) diff --git a/hapi-evm/src/models/transaction/queries.ts b/hapi-evm/src/models/transaction/queries.ts index 56054196..c451cb25 100644 --- a/hapi-evm/src/models/transaction/queries.ts +++ b/hapi-evm/src/models/transaction/queries.ts @@ -96,8 +96,34 @@ export const add_or_modify = async (transaction: CappedTransaction) => { return data } +export const add_or_modify_many = async (transactions: CappedTransaction[]) => { + const mutation = gql` + mutation ($evm_transactions: [evm_transaction_insert_input!]!) { + insert_evm_transaction( + objects: $evm_transactions + on_conflict: { + constraint: transaction_pkey + update_columns: [block_hash, block_number, gas, gas_price, hash] + } + ) { + affected_rows + } + } + ` + const { insert_evm_transaction_one: data } = + await coreUtil.hasura.default.request( + mutation, + { + evm_transactions: transactions + } + ) + + return data +} + export default { exist, get, - add_or_modify + add_or_modify, + add_or_modify_many } diff --git a/hapi-evm/src/services/block.service.ts b/hapi-evm/src/services/block.service.ts index 7766009a..137a2007 100644 --- a/hapi-evm/src/services/block.service.ts +++ b/hapi-evm/src/services/block.service.ts @@ -1,5 +1,10 @@ import { Web3 } from 'web3' -import { Block, TransactionInfo, TransactionHash } from 'web3-types' +import { + Block, + TransactionInfo, + TransactionHash, + TransactionReceipt +} from 'web3-types' import { defaultModel, @@ -45,6 +50,21 @@ const syncFullBlock = async (blockNumber: number | bigint) => { timestamp: blockTimestamp } + if (block.transactions?.length && cappedBlock.gas_used <= 0) { + cappedBlock.gas_used = await cappedBlock.transactions.reduce( + async ( + total: Promise, + trxHash: TransactionHash + ): Promise => { + const transactionReceipt: TransactionReceipt = + await web3.eth.getTransactionReceipt(trxHash.toString()) + + return (await total) + Number(transactionReceipt?.gasUsed) + }, + Promise.resolve(0) + ) + } + try { await blockModel.queries.add_or_modify(cappedBlock) } catch (error: any) { @@ -64,41 +84,29 @@ const syncFullBlock = async (blockNumber: number | bigint) => { return } - await incrementTotalTransactions(block.transactions?.length) - - const transactionsPromises = [ - cappedBlock.transactions.reduce( - async ( - acc: Promise, - trxHash: TransactionHash - ): Promise => { - const transactionExist = await transactionModel.queries.exist(trxHash) - - if (transactionExist) { - return acc - } + if (!block.transactions?.length) return - const trx: TransactionInfo = await web3.eth.getTransaction( - trxHash.toString() - ) - - const customTrx: transactionModel.interfaces.CappedTransaction = { - block_hash: trx.blockHash!.toString(), - block_number: Number(trx.blockNumber), - gas: Number(trx.gas), - gas_price: Number(trx.gasPrice), - hash: trx.hash.toString() - } + await incrementTotalTransactions(block.transactions?.length) - await transactionModel.queries.add_or_modify(customTrx) + const cappedTransactions = await Promise.all( + cappedBlock.transactions.map(async (trxHash: TransactionHash) => { + const trx: TransactionInfo = await web3.eth.getTransaction( + trxHash.toString() + ) + + const customTrx: transactionModel.interfaces.CappedTransaction = { + block_hash: trx.blockHash!.toString(), + block_number: Number(trx.blockNumber), + gas: Number(trx.gas), + gas_price: Number(trx.gasPrice), + hash: trx.hash.toString() + } - return [...(await acc), customTrx] - }, - Promise.resolve([]) - ) - ] + return customTrx + }) + ) - await Promise.all(transactionsPromises) + await transactionModel.queries.add_or_modify_many(cappedTransactions) } const incrementTotalTransactions = async (transactionsCount: number) => { From fe635ecc8e6ef91afada9999d1615a34682ceff2 Mon Sep 17 00:00:00 2001 From: Torresmorah Date: Mon, 20 Nov 2023 09:45:12 -0600 Subject: [PATCH 2/2] chore(hapi-evm): update total of transactions after save them --- hapi-evm/src/services/block.service.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hapi-evm/src/services/block.service.ts b/hapi-evm/src/services/block.service.ts index 137a2007..0bdd129b 100644 --- a/hapi-evm/src/services/block.service.ts +++ b/hapi-evm/src/services/block.service.ts @@ -86,8 +86,6 @@ const syncFullBlock = async (blockNumber: number | bigint) => { if (!block.transactions?.length) return - await incrementTotalTransactions(block.transactions?.length) - const cappedTransactions = await Promise.all( cappedBlock.transactions.map(async (trxHash: TransactionHash) => { const trx: TransactionInfo = await web3.eth.getTransaction( @@ -107,6 +105,7 @@ const syncFullBlock = async (blockNumber: number | bigint) => { ) await transactionModel.queries.add_or_modify_many(cappedTransactions) + await incrementTotalTransactions(block.transactions?.length) } const incrementTotalTransactions = async (transactionsCount: number) => {