Skip to content

Commit

Permalink
✨ Parse logs types and final touches on getTransactionReceipt function
Browse files Browse the repository at this point in the history
  • Loading branch information
arimgibson committed Apr 29, 2022
1 parent 969f6a4 commit b09569a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
21 changes: 21 additions & 0 deletions src/classes/utils/clean-transaction-receipt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { tinyBig, toChecksumAddress } from '../..';
import {
RPCLog,
RPCTransactionReceipt,
TransactionReceipt,
} from '../../types/Transaction.types';
Expand Down Expand Up @@ -41,6 +42,26 @@ export function cleanTransactionReceipt(
);
break;
case 'logs':
transactionReceipt[key].forEach((log: RPCLog, index: number) => {
(Object.keys(log) as Array<keyof RPCLog>).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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,37 @@ 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 = [
'gasUsed',
'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';
Expand All @@ -59,7 +49,7 @@ describe('provider.getTransactionReceipt', () => {
essentialEthProvider.getTransactionReceipt(transactionHash),
]);

testTransactionEquality(
testTransactionReceiptEquality(
ethersTransactionReceipt,
essentialEthTransactionReceipt,
);
Expand Down
7 changes: 4 additions & 3 deletions src/types/Transaction.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type TransactionReceipt = Modify<
cumulativeGasUsed: TinyBig;
effectiveGasPrice: TinyBig;
gasUsed: TinyBig;
logs: Array<Log>;
status: number;
transactionIndex: number;
type: number;
Expand All @@ -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<RPCLog, 'removed'>,
{
blockNumber: number;
logNumber: number;
logIndex: number;
transactionIndex: number;
}
>;
Expand Down Expand Up @@ -108,7 +109,7 @@ export interface RPCLog {
blockNumber: string;
data: string;
logIndex: string;
removed: boolean;
removed?: boolean;
topics: Array<string>;
transactionHash: string;
transactionIndex: string;
Expand Down

0 comments on commit b09569a

Please sign in to comment.