Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7136 decodeparams update #7288

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/src/api/logs_api.ts
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ const _decodeParameter = (inputType: string, clonedTopic: string) =>
* ```
*/
export const decodeLog = <ReturnType extends DecodedParams>(
inputs: Array<AbiParameter>,
inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
data: HexString,
topics: string | string[],
) => {
4 changes: 2 additions & 2 deletions packages/web3-eth-abi/src/api/parameters_api.ts
Original file line number Diff line number Diff line change
@@ -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<AbiInput>,
bytes: HexString,
loose: boolean,
): { [key: string]: unknown; __length__: number } => {
@@ -216,7 +216,7 @@ export const decodeParametersWith = (
* ```
*/
export const decodeParameters = (
abi: AbiInput[],
abi: AbiInput[] | ReadonlyArray<AbiInput>,
bytes: HexString,
): { [key: string]: unknown; __length__: number } => decodeParametersWith(abi, bytes, false);

2 changes: 1 addition & 1 deletion packages/web3-eth-abi/src/coders/decode.ts
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ import { decodeTuple } from './base/tuple.js';
import { toAbiParams } from './utils.js';

export function decodeParameters(
abis: AbiInput[],
abis: AbiInput[] | ReadonlyArray<AbiInput>,
bytes: HexString,
_loose: boolean,
): { [key: string]: unknown; __length__: number } {
40 changes: 40 additions & 0 deletions packages/web3-eth-abi/test/unit/api/logs_api.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
19 changes: 19 additions & 0 deletions packages/web3-eth-abi/test/unit/encodeDecodeParams.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});