From b09569a8695db2774874ed422b83f1b4fa10a681 Mon Sep 17 00:00:00 2001 From: Ari Gibson Date: Fri, 29 Apr 2022 02:05:10 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Parse=20logs=20types=20and=20final?= =?UTF-8?q?=20touches=20on=20getTransactionReceipt=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/clean-transaction-receipt.ts | 21 ++++++++++ .../get-transaction-receipt.test.ts | 38 +++++++------------ src/types/Transaction.types.ts | 7 ++-- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/classes/utils/clean-transaction-receipt.ts b/src/classes/utils/clean-transaction-receipt.ts index 6590c546..3a1373db 100644 --- a/src/classes/utils/clean-transaction-receipt.ts +++ b/src/classes/utils/clean-transaction-receipt.ts @@ -1,5 +1,6 @@ import { tinyBig, toChecksumAddress } from '../..'; import { + RPCLog, RPCTransactionReceipt, TransactionReceipt, } from '../../types/Transaction.types'; @@ -41,6 +42,26 @@ export function cleanTransactionReceipt( ); break; case 'logs': + transactionReceipt[key].forEach((log: RPCLog, index: number) => { + (Object.keys(log) as Array).forEach((logKey) => { + switch (logKey) { + case 'address': + cleanedTransactionReceipt[key][index][logKey] = + toChecksumAddress(log[logKey]); + break; + case 'blockNumber': + case 'logIndex': + case 'transactionIndex': + cleanedTransactionReceipt[key][index][logKey] = Number( + hexToDecimal(log[logKey]), + ); + break; + case 'removed': + delete log[logKey]; + break; + } + }); + }); } }); cleanedTransactionReceipt.byzantium = diff --git a/src/providers/test/json-rpc-provider/get-transaction-receipt.test.ts b/src/providers/test/json-rpc-provider/get-transaction-receipt.test.ts index 2ca36726..8d98f313 100644 --- a/src/providers/test/json-rpc-provider/get-transaction-receipt.test.ts +++ b/src/providers/test/json-rpc-provider/get-transaction-receipt.test.ts @@ -7,9 +7,9 @@ import { rpcUrls } from '../rpc-urls'; const rpcUrl = rpcUrls.mainnet; describe('provider.getTransactionReceipt', () => { - function testTransactionEquality( - transaction1: ethers.providers.TransactionReceipt, - transaction2: TransactionReceipt, + function testTransactionReceiptEquality( + transactionReceipt1: ethers.providers.TransactionReceipt, + transactionReceipt2: TransactionReceipt, ) { // requires manually comparing values via bigNum conversion const bignumCheckKeys = [ @@ -17,37 +17,27 @@ describe('provider.getTransactionReceipt', () => { 'cumulativeGasUsed', 'effectiveGasPrice', ]; - const omittedTransaction1 = omit(transaction1, [ - 'logs', // temporary + const omittedTransactionReceipt1 = omit(transactionReceipt1, [ ...bignumCheckKeys, ]); - const omittedTransaction2 = omit(transaction2, [ - 'logs', // temporary + const omittedTransactionReceipt2 = omit(transactionReceipt2, [ ...bignumCheckKeys, ]); - expect(omittedTransaction1).toStrictEqual(omittedTransaction2); + expect(omittedTransactionReceipt1).toStrictEqual( + omittedTransactionReceipt2, + ); expect( - Math.abs(transaction1.confirmations - transaction2.confirmations), + Math.abs( + transactionReceipt1.confirmations - transactionReceipt2.confirmations, + ), ).toBeLessThan(3); bignumCheckKeys.forEach((key) => { const ethersKey = key as keyof ethers.providers.TransactionResponse; - expect((transaction1 as any)[ethersKey].toString()).toBe( - (transaction2 as any)[key].toString(), + expect((transactionReceipt1 as any)[ethersKey].toString()).toBe( + (transactionReceipt2 as any)[key].toString(), ); }); } - // it('should match web3 and essential-eth', async () => { - // const transactionHash = - // '0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789'; - // const web3Provider = new Web3(rpcUrl); - // const essentialEthProvider = new JsonRpcProvider(rpcUrl); - // const [web3Transaction, essentialEthTransaction] = await Promise.all([ - // web3Provider.eth.getTransaction(transactionHash), - // essentialEthProvider.getTransaction(transactionHash), - // ]); - - // testTransactionEquality(web3Transaction as any, essentialEthTransaction); - // }); it('should match ethers and essential-eth', async () => { const transactionHash = '0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789'; @@ -59,7 +49,7 @@ describe('provider.getTransactionReceipt', () => { essentialEthProvider.getTransactionReceipt(transactionHash), ]); - testTransactionEquality( + testTransactionReceiptEquality( ethersTransactionReceipt, essentialEthTransactionReceipt, ); diff --git a/src/types/Transaction.types.ts b/src/types/Transaction.types.ts index e3127224..50b3905d 100644 --- a/src/types/Transaction.types.ts +++ b/src/types/Transaction.types.ts @@ -38,6 +38,7 @@ export type TransactionReceipt = Modify< cumulativeGasUsed: TinyBig; effectiveGasPrice: TinyBig; gasUsed: TinyBig; + logs: Array; status: number; transactionIndex: number; type: number; @@ -52,10 +53,10 @@ export type TransactionReceipt = Modify< * * Similar to [`Type Log on ethers.providers`](https://docs.ethers.io/v5/api/providers/types/#providers-Log) */ export type Log = Modify< - RPCLog, + Omit, { blockNumber: number; - logNumber: number; + logIndex: number; transactionIndex: number; } >; @@ -108,7 +109,7 @@ export interface RPCLog { blockNumber: string; data: string; logIndex: string; - removed: boolean; + removed?: boolean; topics: Array; transactionHash: string; transactionIndex: string;