Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 9afaa61

Browse files
authored
Fix getblock.transactions (#7151)
* add default array to block.transactions * update * update
1 parent 6b80cf0 commit 9afaa61

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

packages/web3-eth/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,8 @@ Documentation:
262262

263263
- Fixed geth issue when running a new instance, transactions will index when there are no blocks created (#7098)
264264

265-
## [Unreleased]
265+
## [Unreleased]
266+
267+
### Fixed
268+
269+
- Adds transaction property to be an empty list rather than undefined when no transactions are included in the block (#7151)

packages/web3-eth/src/rpc_method_wrappers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,21 @@ export async function getBlock<ReturnFormat extends DataFormat>(
286286
hydrated,
287287
);
288288
}
289-
return format(
289+
const res = format(
290290
blockSchema,
291291
response as unknown as Block,
292292
returnFormat ?? web3Context.defaultReturnFormat,
293293
);
294+
295+
if (!isNullish(res)) {
296+
const result = {
297+
...res,
298+
transactions: res.transactions ?? [],
299+
}
300+
return result;
301+
}
302+
303+
return res;
294304
}
295305

296306
/**

packages/web3-eth/test/unit/rpc_method_wrappers/fixtures/get_block.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export const mockRpcResponseHydrated: Block = {
7070
...mockRpcResponse,
7171
transactions: [hydratedTransaction, hydratedTransaction, hydratedTransaction],
7272
};
73+
export const noTransactionBlock: Block = {
74+
...mockRpcResponse,
75+
transactions: [],
76+
}
7377

7478
/**
7579
* Array consists of:

packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { isBytes, isNullish } from 'web3-validator';
2828
import { ethRpcMethods } from 'web3-rpc-methods';
2929

3030
import { getBlock } from '../../../src/rpc_method_wrappers';
31-
import { mockRpcResponse, mockRpcResponseHydrated, testData } from './fixtures/get_block';
31+
import { mockRpcResponse, mockRpcResponseHydrated, testData, noTransactionBlock } from './fixtures/get_block';
3232
import { blockSchema } from '../../../src/schemas';
3333

3434
jest.mock('web3-rpc-methods');
@@ -84,4 +84,31 @@ describe('getBlock', () => {
8484
expect(result).toStrictEqual(expectedFormattedResult);
8585
},
8686
);
87+
88+
it.each(testData)(
89+
`should format the block to include transactions as an empty array if no transactions are present\nTitle: %s\nInput parameters: %s\n`,
90+
async (_, inputParameters) => {
91+
const [inputBlock] = inputParameters;
92+
const expectedReturnFormat = { number: FMT_NUMBER.STR, bytes: FMT_BYTES.UINT8ARRAY };
93+
const expectedMockRpcResponse = noTransactionBlock;
94+
// TODO: Fix format to default have a default in oneOf if no schema is matched
95+
const formattedResult = format(
96+
blockSchema,
97+
expectedMockRpcResponse,
98+
expectedReturnFormat,
99+
);
100+
const expectedFormattedResult = {...formattedResult,
101+
transactions: []
102+
};
103+
const inputBlockIsBytes = isBytes(inputBlock as Bytes);
104+
(
105+
(inputBlockIsBytes
106+
? ethRpcMethods.getBlockByHash
107+
: ethRpcMethods.getBlockByNumber) as jest.Mock
108+
).mockResolvedValueOnce(expectedMockRpcResponse);
109+
110+
const result = await getBlock(web3Context, ...inputParameters, expectedReturnFormat);
111+
expect(result).toStrictEqual(expectedFormattedResult);
112+
},
113+
);
87114
});

0 commit comments

Comments
 (0)