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/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; } /** 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..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 @@ -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 + const 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); + }, + ); });