diff --git a/packages/web3-eth-abi/CHANGELOG.md b/packages/web3-eth-abi/CHANGELOG.md index 0b7990b7034..cc8ab72306d 100644 --- a/packages/web3-eth-abi/CHANGELOG.md +++ b/packages/web3-eth-abi/CHANGELOG.md @@ -189,3 +189,7 @@ Documentation: - 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) ## [Unreleased] + +### Fixed + +- `decodeLog` , `decodeParametersWith` , `decodeParameters` and `decodeParameters` now accepts first immutable param as well (#7288) diff --git a/packages/web3-eth-abi/src/api/logs_api.ts b/packages/web3-eth-abi/src/api/logs_api.ts index 678ec6d7129..0f936ca6931 100644 --- a/packages/web3-eth-abi/src/api/logs_api.ts +++ b/packages/web3-eth-abi/src/api/logs_api.ts @@ -72,7 +72,7 @@ const _decodeParameter = (inputType: string, clonedTopic: string) => * ``` */ export const decodeLog = ( - inputs: Array, + inputs: Array | ReadonlyArray, data: HexString, topics: string | string[], ) => { diff --git a/packages/web3-eth-abi/src/api/parameters_api.ts b/packages/web3-eth-abi/src/api/parameters_api.ts index f81a32e9db3..3bfef281580 100644 --- a/packages/web3-eth-abi/src/api/parameters_api.ts +++ b/packages/web3-eth-abi/src/api/parameters_api.ts @@ -89,7 +89,7 @@ export const encodeParameter = (abi: AbiInput, param: unknown): string => * Should be used to decode list of params */ export const decodeParametersWith = ( - abis: AbiInput[], + abis: AbiInput[] | ReadonlyArray, bytes: HexString, loose: boolean, ): { [key: string]: unknown; __length__: number } => { @@ -216,7 +216,7 @@ export const decodeParametersWith = ( * ``` */ export const decodeParameters = ( - abi: AbiInput[], + abi: AbiInput[] | ReadonlyArray, bytes: HexString, ): { [key: string]: unknown; __length__: number } => decodeParametersWith(abi, bytes, false); diff --git a/packages/web3-eth-abi/src/coders/decode.ts b/packages/web3-eth-abi/src/coders/decode.ts index adc84ad93bb..9a67d89e810 100644 --- a/packages/web3-eth-abi/src/coders/decode.ts +++ b/packages/web3-eth-abi/src/coders/decode.ts @@ -21,7 +21,7 @@ import { decodeTuple } from './base/tuple.js'; import { toAbiParams } from './utils.js'; export function decodeParameters( - abis: AbiInput[], + abis: AbiInput[] | ReadonlyArray, bytes: HexString, _loose: boolean, ): { [key: string]: unknown; __length__: number } { diff --git a/packages/web3-eth-abi/test/unit/api/logs_api.test.ts b/packages/web3-eth-abi/test/unit/api/logs_api.test.ts index 972cec3fd3b..357ddea37c2 100644 --- a/packages/web3-eth-abi/test/unit/api/logs_api.test.ts +++ b/packages/web3-eth-abi/test/unit/api/logs_api.test.ts @@ -29,5 +29,45 @@ describe('logs_api', () => { }, ); }); + + it('decodeLog with first immutable param', () => { + const abi = [ + { + type: 'string', + name: 'myString', + }, + { + type: 'uint256', + name: 'myNumber', + indexed: true, + }, + { + type: 'uint8', + name: 'mySmallNumber', + indexed: true, + }, + ] as const; + + const data = + '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000'; + + const topics = [ + '0x000000000000000000000000000000000000000000000000000000000000f310', + '0x0000000000000000000000000000000000000000000000000000000000000010', + ]; + + const result = { + '0': 'Hello%!', + '1': '62224', + '2': '16', + __length__: 3, + myString: 'Hello%!', + myNumber: '62224', + mySmallNumber: '16', + }; + + const expected = decodeLog(abi, data, topics); + expect(JSON.parse(JSON.stringify(expected))).toEqual(result); + }); }); }); diff --git a/packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts b/packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts index 184ed0fcf4a..c36b1594a47 100644 --- a/packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts +++ b/packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts @@ -47,4 +47,23 @@ describe('encodeParameters decodeParameters tests should pass', () => { expect(deepEqualTolerateBigInt(decodedResult[0], decoderTestObj.value)).toBeTruthy(); }); + + it('unit test of decodeParameters with first immutable param', () => { + const bytes = + '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000015a828c295b2bea094b70a05e96ae19c876417adf3a9083500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040729a97ba5090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000634d6f6f20c3a9f09f9a80c3a96f4d6f206f20c3a9204d4dc3a9f09f9a802020c3a96f4df09f9a806ff09f9a804df09f9a806fc3a920f09f9a80c3a94df09f9a80f09f9a8020c3a920f09f9a8020c3a9c3a96f4d6fc3a96fc3a94dc3a94dc3a96f6f4d6f0000000000000000000000000000000000000000000000000000000000'; + + const result = [ + '531024955072740163537488200975830992725163050550575040565', + [ + 'Moo é🚀éoMo o é MMé🚀 éoM🚀o🚀M🚀oé 🚀éM🚀🚀 é 🚀 ééoMoéoéMéMéooMo', + '0x729a97ba5090', + ], + ]; + const readonlyArray = ['(uint192,(string,bytes6))'] as const; // should allow immutable array as first param + + const decodedResult = decodeParameters(readonlyArray, bytes); + + removeKey(decodedResult[0], '__length__'); + expect(deepEqualTolerateBigInt(decodedResult[0], result)).toBeTruthy(); + }); });