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

Commit dcd9d6a

Browse files
authored
7136 decodeparams update (#7288)
* decodeParametersWith update * decodeParameters update * unit test decodeParameters * decodeLog update * decodeLog unit test * changelog update
1 parent cc99825 commit dcd9d6a

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed

packages/web3-eth-abi/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,7 @@ Documentation:
189189
- Handle common cases for smart contract errors according to EIP 838: `0x4e487b71` which is the ‘selector’ for `Panic(uint256)` and `0x08c379a0` is the ‘selector’ of `Error(string)`. (7155)
190190

191191
## [Unreleased]
192+
193+
### Fixed
194+
195+
- `decodeLog` , `decodeParametersWith` , `decodeParameters` and `decodeParameters` now accepts first immutable param as well (#7288)

packages/web3-eth-abi/src/api/logs_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const _decodeParameter = (inputType: string, clonedTopic: string) =>
7272
* ```
7373
*/
7474
export const decodeLog = <ReturnType extends DecodedParams>(
75-
inputs: Array<AbiParameter>,
75+
inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
7676
data: HexString,
7777
topics: string | string[],
7878
) => {

packages/web3-eth-abi/src/api/parameters_api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const encodeParameter = (abi: AbiInput, param: unknown): string =>
8989
* Should be used to decode list of params
9090
*/
9191
export const decodeParametersWith = (
92-
abis: AbiInput[],
92+
abis: AbiInput[] | ReadonlyArray<AbiInput>,
9393
bytes: HexString,
9494
loose: boolean,
9595
): { [key: string]: unknown; __length__: number } => {
@@ -216,7 +216,7 @@ export const decodeParametersWith = (
216216
* ```
217217
*/
218218
export const decodeParameters = (
219-
abi: AbiInput[],
219+
abi: AbiInput[] | ReadonlyArray<AbiInput>,
220220
bytes: HexString,
221221
): { [key: string]: unknown; __length__: number } => decodeParametersWith(abi, bytes, false);
222222

packages/web3-eth-abi/src/coders/decode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { decodeTuple } from './base/tuple.js';
2121
import { toAbiParams } from './utils.js';
2222

2323
export function decodeParameters(
24-
abis: AbiInput[],
24+
abis: AbiInput[] | ReadonlyArray<AbiInput>,
2525
bytes: HexString,
2626
_loose: boolean,
2727
): { [key: string]: unknown; __length__: number } {

packages/web3-eth-abi/test/unit/api/logs_api.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,45 @@ describe('logs_api', () => {
2929
},
3030
);
3131
});
32+
33+
it('decodeLog with first immutable param', () => {
34+
const abi = [
35+
{
36+
type: 'string',
37+
name: 'myString',
38+
},
39+
{
40+
type: 'uint256',
41+
name: 'myNumber',
42+
indexed: true,
43+
},
44+
{
45+
type: 'uint8',
46+
name: 'mySmallNumber',
47+
indexed: true,
48+
},
49+
] as const;
50+
51+
const data =
52+
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000';
53+
54+
const topics = [
55+
'0x000000000000000000000000000000000000000000000000000000000000f310',
56+
'0x0000000000000000000000000000000000000000000000000000000000000010',
57+
];
58+
59+
const result = {
60+
'0': 'Hello%!',
61+
'1': '62224',
62+
'2': '16',
63+
__length__: 3,
64+
myString: 'Hello%!',
65+
myNumber: '62224',
66+
mySmallNumber: '16',
67+
};
68+
69+
const expected = decodeLog(abi, data, topics);
70+
expect(JSON.parse(JSON.stringify(expected))).toEqual(result);
71+
});
3272
});
3373
});

packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,23 @@ describe('encodeParameters decodeParameters tests should pass', () => {
4747

4848
expect(deepEqualTolerateBigInt(decodedResult[0], decoderTestObj.value)).toBeTruthy();
4949
});
50+
51+
it('unit test of decodeParameters with first immutable param', () => {
52+
const bytes =
53+
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000015a828c295b2bea094b70a05e96ae19c876417adf3a9083500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040729a97ba5090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000634d6f6f20c3a9f09f9a80c3a96f4d6f206f20c3a9204d4dc3a9f09f9a802020c3a96f4df09f9a806ff09f9a804df09f9a806fc3a920f09f9a80c3a94df09f9a80f09f9a8020c3a920f09f9a8020c3a9c3a96f4d6fc3a96fc3a94dc3a94dc3a96f6f4d6f0000000000000000000000000000000000000000000000000000000000';
54+
55+
const result = [
56+
'531024955072740163537488200975830992725163050550575040565',
57+
[
58+
'Moo é🚀éoMo o é MMé🚀 éoM🚀o🚀M🚀oé 🚀éM🚀🚀 é 🚀 ééoMoéoéMéMéooMo',
59+
'0x729a97ba5090',
60+
],
61+
];
62+
const readonlyArray = ['(uint192,(string,bytes6))'] as const; // should allow immutable array as first param
63+
64+
const decodedResult = decodeParameters(readonlyArray, bytes);
65+
66+
removeKey(decodedResult[0], '__length__');
67+
expect(deepEqualTolerateBigInt(decodedResult[0], result)).toBeTruthy();
68+
});
5069
});

0 commit comments

Comments
 (0)