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..0bdd129b 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,28 @@ 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 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() - } - - await transactionModel.queries.add_or_modify(customTrx) + 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) + await incrementTotalTransactions(block.transactions?.length) } const incrementTotalTransactions = async (transactionsCount: number) => {