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

Commit d12dc7e

Browse files
author
Alex
authoredJun 27, 2023
allow getpastlogs to accept bn and numbers (#6219)
* allow getpastlogs to accept bn and numbers * update rpc tests * update numbertohex bigint conditional * update conditional * update conditional * update changelog * getpastLogs accept blockhash as a parameter * add coverage and update changelogs * update conditional * address feedback * update web3 eth methods * update unit tests for getpastlogs * update fixtures for getpastlogs
1 parent a26a888 commit d12dc7e

File tree

12 files changed

+155
-8
lines changed

12 files changed

+155
-8
lines changed
 

‎CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1698,3 +1698,12 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
16981698
- Dependencies updated
16991699

17001700
## [Unreleased]
1701+
1702+
1703+
#### web3-utils
1704+
1705+
- BigInts pass validation within the method `numberToHex` (#6206)
1706+
1707+
#### web3-rpc-methods
1708+
1709+
- Rpc method `getPastLogs` accept blockHash as a parameter https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs (#6181)

‎packages/web3-eth/src/rpc_method_wrappers.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
AccessListResult,
5050
} from 'web3-types';
5151
import { Web3Context, Web3PromiEvent } from 'web3-core';
52-
import { format, hexToBytes, bytesToUint8Array } from 'web3-utils';
52+
import { format, hexToBytes, bytesToUint8Array, numberToHex } from 'web3-utils';
5353
import { TransactionFactory } from 'web3-eth-accounts';
5454
import { isBlockTag, isBytes, isNullish, isString } from 'web3-validator';
5555
import {
@@ -988,7 +988,22 @@ export async function getLogs<ReturnFormat extends DataFormat>(
988988
filter: Filter,
989989
returnFormat: ReturnFormat,
990990
) {
991-
const response = await ethRpcMethods.getLogs(web3Context.requestManager, filter);
991+
// format type bigint or number toBlock and fromBlock to hexstring.
992+
let { toBlock, fromBlock } = filter;
993+
if (!isNullish(toBlock)) {
994+
if (typeof toBlock === 'number' || typeof toBlock === 'bigint') {
995+
toBlock = numberToHex(toBlock);
996+
}
997+
}
998+
if (!isNullish(fromBlock)) {
999+
if (typeof fromBlock === 'number' || typeof fromBlock === 'bigint') {
1000+
fromBlock = numberToHex(fromBlock);
1001+
}
1002+
}
1003+
1004+
const formattedFilter = { ...filter, fromBlock, toBlock };
1005+
1006+
const response = await ethRpcMethods.getLogs(web3Context.requestManager, formattedFilter);
9921007

9931008
const result = response.map(res => {
9941009
if (typeof res === 'string') {

‎packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,15 @@ export const signValidData: [
17691769
],
17701770
];
17711771

1772+
/**
1773+
* Array tests getPastLogs and formats valid fromBlock and toBlock
1774+
*/
1775+
export const getPastLogsValidFormatData: [Filter][] = [
1776+
[{ fromBlock: 0, toBlock: 100 }],
1777+
[{ fromBlock: BigInt(0), toBlock: BigInt(100) }],
1778+
[{ fromBlock: '0x0', toBlock: '0x10' }],
1779+
[{ blockHash: '0x19e54b41ac6ed5eb5045aab59967489bbab1852e742857b1987f62290a4b89af' }],
1780+
];
17721781
/**
17731782
* Array consists of:
17741783
* - array of inputs
@@ -1779,6 +1788,16 @@ export const getPastLogsValidData: [[Filter, DataFormat | undefined], [Filter, D
17791788
[{}, undefined],
17801789
[{}, DEFAULT_RETURN_FORMAT],
17811790
],
1791+
[
1792+
[
1793+
{ blockHash: '0x19e54b41ac6ed5eb5045aab59967489bbab1852e742857b1987f62290a4b89af' },
1794+
undefined,
1795+
],
1796+
[
1797+
{ blockHash: '0x19e54b41ac6ed5eb5045aab59967489bbab1852e742857b1987f62290a4b89af' },
1798+
DEFAULT_RETURN_FORMAT,
1799+
],
1800+
],
17821801
[
17831802
[
17841803
{

‎packages/web3-eth/test/integration/rpc.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,45 @@ describe('rpc', () => {
345345
// eslint-disable-next-line no-await-in-loop
346346
resTx.push(await contractDeployed.methods?.firesStringEvent(l).send(sendOptions));
347347
}
348+
349+
// test type hexstring
348350
const res: Array<any> = await web3Eth.getPastLogs({
349351
address: contractDeployed.options.address as string,
350352
fromBlock: numberToHex(Math.min(...resTx.map(d => Number(d.blockNumber)))),
353+
toBlock: numberToHex(1000),
351354
});
352355
const results = res.map(
353356
r =>
354357
decodeEventABI(eventAbi as AbiEventFragment & { signature: string }, r, [])
355358
.returnValues[0],
356359
);
360+
361+
// test type number
362+
const res2: Array<any> = await web3Eth.getPastLogs({
363+
address: contractDeployed.options.address as string,
364+
fromBlock: Math.min(...resTx.map(d => Number(d.blockNumber))),
365+
toBlock: 1000,
366+
});
367+
const results2 = res2.map(
368+
r =>
369+
decodeEventABI(eventAbi as AbiEventFragment & { signature: string }, r, [])
370+
.returnValues[0],
371+
);
372+
// test type BigInt
373+
const res3: Array<any> = await web3Eth.getPastLogs({
374+
address: contractDeployed.options.address as string,
375+
fromBlock: BigInt(Math.min(...resTx.map(d => Number(d.blockNumber)))),
376+
toBlock: BigInt(1000),
377+
});
378+
const results3 = res3.map(
379+
r =>
380+
decodeEventABI(eventAbi as AbiEventFragment & { signature: string }, r, [])
381+
.returnValues[0],
382+
);
357383
for (const l of listOfStrings) {
358384
expect(results).toContain(l);
385+
expect(results2).toContain(l);
386+
expect(results3).toContain(l);
359387
}
360388
});
361389
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
import { ethRpcMethods } from 'web3-rpc-methods';
19+
import { DEFAULT_RETURN_FORMAT } from 'web3-types';
20+
import { numberToHex, isNullish } from 'web3-utils';
21+
import * as rpcMethodWrappers from '../../src/rpc_method_wrappers';
22+
import { getPastLogsValidFormatData } from '../fixtures/web3_eth_methods_with_parameters';
23+
import Web3Eth from '../../src/index';
24+
25+
jest.mock('web3-rpc-methods');
26+
describe('web3_eth_methods formatting', () => {
27+
let web3Eth: Web3Eth;
28+
29+
beforeAll(() => {
30+
web3Eth = new Web3Eth('http://127.0.0.1:8545');
31+
});
32+
33+
describe('getPastLogs makes sure data is prepared properly', () => {
34+
it.each(getPastLogsValidFormatData)('input: %s\nrpcMethodParameters: %s', async filter => {
35+
jest.spyOn(ethRpcMethods, 'getLogs').mockResolvedValue(['']);
36+
await rpcMethodWrappers.getLogs(web3Eth, filter, DEFAULT_RETURN_FORMAT);
37+
let { fromBlock, toBlock } = filter;
38+
if (
39+
!isNullish(filter.fromBlock) &&
40+
(typeof filter.fromBlock === 'bigint' || typeof filter.fromBlock === 'number')
41+
)
42+
fromBlock = numberToHex(filter.fromBlock);
43+
if (
44+
!isNullish(filter.toBlock) &&
45+
(typeof filter.toBlock === 'bigint' || typeof filter.toBlock === 'number')
46+
)
47+
toBlock = numberToHex(filter.toBlock);
48+
expect(ethRpcMethods.getLogs).toHaveBeenCalledWith(web3Eth.requestManager, {
49+
...filter,
50+
toBlock,
51+
fromBlock,
52+
});
53+
});
54+
});
55+
});

‎packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ import {
2626
} from '../fixtures/rpc_methods_wrappers';
2727
import {
2828
estimateGasValidData,
29-
// estimateGasValidData,
3029
getBalanceValidData,
3130
getBlockTransactionCountValidData,
3231
getBlockUncleCountValidData,
3332
getBlockValidData,
3433
getCodeValidData,
3534
getFeeHistoryValidData,
36-
// getCodeValidData,
37-
// getFeeHistoryValidData,
3835
getPastLogsValidData,
3936
getProofValidData,
4037
getStorageAtValidData,
@@ -341,6 +338,18 @@ describe('web3_eth_methods_with_parameters', () => {
341338
},
342339
);
343340
});
341+
describe('getPastLogs called with rpcMethodWrappers', () => {
342+
it.each(getPastLogsValidData)(
343+
'input: %s\nrpcMethodParameters: %s',
344+
async (_, rpcMethodParameters) => {
345+
await rpcMethodWrappers.getLogs(web3Eth, ...rpcMethodParameters);
346+
expect(rpcMethodWrappers.getLogs).toHaveBeenCalledWith(
347+
web3Eth,
348+
...rpcMethodParameters,
349+
);
350+
},
351+
);
352+
});
344353

345354
describe('createAccessList', () => {
346355
it.each(createAccessListTestData)(

‎packages/web3-rpc-methods/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ Documentation:
9797
- Dependencies updated
9898

9999
## [Unreleased]
100+
101+
### Fixed
102+
103+
- Rpc method `getPastLogs` accept blockHash as a parameter https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs (#6181)

‎packages/web3-types/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ Documentation:
135135
- Fixed bug #6185, now web3.js compiles on typescript v5 (#6195)
136136

137137
## [Unreleased]
138+
139+
### Fixed
140+
141+
- type `Filter` includes `blockHash` (#6206)

‎packages/web3-types/src/eth_types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export interface Filter {
226226
readonly fromBlock?: BlockNumberOrTag;
227227
readonly toBlock?: BlockNumberOrTag;
228228
readonly address?: Address | Address[];
229-
229+
readonly blockHash?: Address;
230230
// Using "null" type intentionally to match specifications
231231
// eslint-disable-next-line @typescript-eslint/ban-types
232232
readonly topics?: (null | Topic | Topic[])[];

‎packages/web3-utils/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ Documentation:
135135
- Dependencies updated
136136

137137
## [Unreleased]
138+
139+
### Fixed
140+
141+
- BigInts pass validation within the method `numberToHex` (#6206)

‎packages/web3-utils/src/converters.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ export const toDecimal = hexToNumber;
175175
* ```
176176
*/
177177
export const numberToHex = (value: Numbers, hexstrict?: boolean): HexString => {
178-
validator.validate(['int'], [value]);
178+
if (typeof value !== 'bigint') validator.validate(['int'], [value]);
179179
// To avoid duplicate code and circular dependency we will
180180
// use `numberToHex` implementation from `web3-validator`
181181
let updatedValue = validatorUtils.numberToHex(value);
182-
// return validatorUtils.numberToHex(value);
183182
if (hexstrict) {
184183
if (!updatedValue.startsWith('-') && updatedValue.length % 2 === 1) {
185184
// To avoid duplicate a circular dependancy we will not be using the padLeft method

‎packages/web3-validator/src/validation/filter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const isFilterObject = (value: Filter) => {
3333
'toBlock',
3434
'address',
3535
'topics',
36+
'blockHash',
3637
];
3738
if (isNullish(value) || typeof value !== 'object') return false;
3839

0 commit comments

Comments
 (0)
This repository has been archived.