Skip to content

Commit

Permalink
7136 decodeparams update (#7288)
Browse files Browse the repository at this point in the history
* decodeParametersWith update

* decodeParameters update

* unit test decodeParameters

* decodeLog update

* decodeLog unit test

* changelog update
  • Loading branch information
jdevcs authored Oct 2, 2024
1 parent cc99825 commit dcd9d6a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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[],
) => {
Expand Down
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
Expand Up @@ -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 } => {
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/src/coders/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } {
Expand Down
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
Expand Up @@ -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
Expand Up @@ -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();
});
});

1 comment on commit dcd9d6a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: dcd9d6a Previous: cc99825 Ratio
processingTx 22379 ops/sec (±6.21%) 21879 ops/sec (±7.68%) 0.98
processingContractDeploy 39186 ops/sec (±7.73%) 38811 ops/sec (±6.95%) 0.99
processingContractMethodSend 14923 ops/sec (±8.63%) 15153 ops/sec (±7.20%) 1.02
processingContractMethodCall 25900 ops/sec (±8.10%) 27291 ops/sec (±6.06%) 1.05
abiEncode 42051 ops/sec (±5.40%) 41796 ops/sec (±7.77%) 0.99
abiDecode 29557 ops/sec (±8.36%) 28813 ops/sec (±7.30%) 0.97
sign 1507 ops/sec (±3.35%) 1512 ops/sec (±0.76%) 1.00
verify 362 ops/sec (±0.53%) 359 ops/sec (±0.56%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.