From 7a9c5074de2898a7bfd3f25f0ac2b0f9a431e907 Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Mon, 15 Jul 2024 10:01:20 -0400 Subject: [PATCH 1/5] add default array to block.transactions --- packages/web3-eth/src/rpc_method_wrappers.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 3a03afe0967..8195eb58caa 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -286,11 +286,21 @@ export async function getBlock( hydrated, ); } - return format( + const res = format( blockSchema, response as unknown as Block, returnFormat ?? web3Context.defaultReturnFormat, ); + + if (!isNullish(res)) { + const result = { + ...res, + transactions: res.transactions ?? [], + } + return result; + } + + return res; } /** From 6c655a3f566688df967c89ce86dd2a992ff10a4f Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Mon, 15 Jul 2024 11:08:26 -0400 Subject: [PATCH 2/5] update --- packages/web3-eth/CHANGELOG.md | 6 +++- .../rpc_method_wrappers/fixtures/get_block.ts | 4 +++ .../rpc_method_wrappers/get_block.test.ts | 29 ++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index f098725ad14..417db4d0837 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -262,4 +262,8 @@ Documentation: - Fixed geth issue when running a new instance, transactions will index when there are no blocks created (#7098) -## [Unreleased] \ No newline at end of file +## [Unreleased] + +### Fixed + +- Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) \ No newline at end of file diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/get_block.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/get_block.ts index 4474bec4972..250f770bd64 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/get_block.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/get_block.ts @@ -70,6 +70,10 @@ export const mockRpcResponseHydrated: Block = { ...mockRpcResponse, transactions: [hydratedTransaction, hydratedTransaction, hydratedTransaction], }; +export const noTransactionBlock: Block = { + ...mockRpcResponse, + transactions: [], +} /** * Array consists of: diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts index 778c6d4d000..6b9338c3c53 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts @@ -28,7 +28,7 @@ import { isBytes, isNullish } from 'web3-validator'; import { ethRpcMethods } from 'web3-rpc-methods'; import { getBlock } from '../../../src/rpc_method_wrappers'; -import { mockRpcResponse, mockRpcResponseHydrated, testData } from './fixtures/get_block'; +import { mockRpcResponse, mockRpcResponseHydrated, testData, noTransactionBlock } from './fixtures/get_block'; import { blockSchema } from '../../../src/schemas'; jest.mock('web3-rpc-methods'); @@ -84,4 +84,31 @@ describe('getBlock', () => { expect(result).toStrictEqual(expectedFormattedResult); }, ); + + it.each(testData)( + `should format the block to include transactions as an empty array if no transactions are present\nTitle: %s\nInput parameters: %s\n`, + async (_, inputParameters) => { + const [inputBlock] = inputParameters; + const expectedReturnFormat = { number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY }; + const expectedMockRpcResponse = noTransactionBlock; + // TODO: Fix format to default have a default in oneOf if no schema is matched + let formattedResult = format( + blockSchema, + expectedMockRpcResponse, + expectedReturnFormat, + ); + const expectedFormattedResult = {...formattedResult, + transactions: [] + }; + const inputBlockIsBytes = isBytes(inputBlock as Bytes); + ( + (inputBlockIsBytes + ? ethRpcMethods.getBlockByHash + : ethRpcMethods.getBlockByNumber) as jest.Mock + ).mockResolvedValueOnce(expectedMockRpcResponse); + + const result = await getBlock(web3Context, ...inputParameters, expectedReturnFormat); + expect(result).toStrictEqual(expectedFormattedResult); + }, + ); }); From 79d250a7e358259121d1525351aeddd41017754d Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Mon, 15 Jul 2024 11:29:11 -0400 Subject: [PATCH 3/5] update --- .../web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts b/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts index 6b9338c3c53..365fb09d9ec 100644 --- a/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts +++ b/packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts @@ -92,7 +92,7 @@ describe('getBlock', () => { const expectedReturnFormat = { number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY }; const expectedMockRpcResponse = noTransactionBlock; // TODO: Fix format to default have a default in oneOf if no schema is matched - let formattedResult = format( + const formattedResult = format( blockSchema, expectedMockRpcResponse, expectedReturnFormat, From 9ebd2ea03c4cc84570505ec559a0cabda82395cf Mon Sep 17 00:00:00 2001 From: luu-alex <98a.lexluu@gmail.com> Date: Wed, 17 Jul 2024 14:42:34 -0400 Subject: [PATCH 4/5] update --- packages/web3-eth/src/rpc_method_wrappers.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 8195eb58caa..02fb9fbb173 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -521,14 +521,13 @@ export async function getTransactionReceipt( } } - return isNullish(response) ? response : (format( transactionReceiptSchema, response as unknown as TransactionReceipt, returnFormat ?? web3Context.defaultReturnFormat, - ) as TransactionReceipt); + )); } /** From 2d03be306622766730f56b2d0dbd35c7e48d2d89 Mon Sep 17 00:00:00 2001 From: CI Date: Thu, 18 Jul 2024 18:45:17 -0400 Subject: [PATCH 5/5] update changelog --- packages/web3-eth/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 417db4d0837..b3b98530d82 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -266,4 +266,5 @@ Documentation: ### Fixed -- Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) \ No newline at end of file +- Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151) +- Change method `getTransactionReceipt` to not be casted as `TransactionReceipt` to give proper return type (#7159) \ No newline at end of file