Skip to content

Commit

Permalink
Fix 7055 one of with scalar value and string (#7173)
Browse files Browse the repository at this point in the history
* code work

* add test

* add more test

* update change log

* refine change log

* refine test

---------

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
  • Loading branch information
mmyyrroonn and avkos authored Aug 8, 2024
1 parent 8b435c1 commit 0db2b18
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,12 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

## [Unreleased]

### Fixed

#### web3-utils

- Fixed format schema with `oneOf` doesn't work correctly (#7055)

### Added

#### web3-eth-accounts
Expand Down
21 changes: 20 additions & 1 deletion packages/web3-utils/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export const convertScalarValue = (value: unknown, ethType: string, format: Data
throw new FormatterError(`Invalid format: ${String(format.bytes)}`);
}
}

if (baseType === 'string') {
return String(value);
}

} catch (error) {
// If someone didn't use `eth` keyword we can return original value
// as the scope of this code is formatting not validation
Expand Down Expand Up @@ -289,7 +294,7 @@ export const convert = (
} else {
for (const [key, value] of Object.entries(object)) {
dataPath.push(key);
const schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath);
let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath);

// If value is a scaler value
if (isNullish(schemaProp)) {
Expand Down Expand Up @@ -322,6 +327,20 @@ export const convert = (
continue;
}

// The following code is basically saying:
// if the schema specifies oneOf, then we are to loop
// over each possible schema and check if they type of the schema specifies format
// and if so we use the oneOfSchemaProp as the schema for formatting
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
if ((schemaProp?.format === undefined) && (schemaProp?.oneOf !== undefined)) {
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) {
if ((oneOfSchemaProp?.format !== undefined)) {
schemaProp = oneOfSchemaProp;
break;
}
};
}

object[key] = convertScalarValue(value, schemaProp.format as string, format);

dataPath.pop();
Expand Down
61 changes: 61 additions & 0 deletions packages/web3-utils/test/unit/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,17 @@ describe('formatter', () => {
).toEqual(new Uint8Array([16, 11, 202]));
});
});

describe('string', () => {
it('should format string for 123', () => {
expect(
format({ format: 'string' }, 123, {
number: FMT_NUMBER.STR,
bytes: FMT_BYTES.HEX,
}),
).toBe('123');
});
});
});

describe('array values', () => {
Expand Down Expand Up @@ -827,6 +838,56 @@ describe('formatter', () => {

expect(result).toEqual(expected);
});

it('should format object with oneOf', () => {
const schema = {
type: 'object',
properties: {
from: {
format: 'address',
},
to: {
oneOf: [{ format: 'string' }, { type: 'null' }],
},
},
};

const data ={
from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401',
to: 123,
}
;

const result = { from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401', to: '123' };

expect(
format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }),
).toEqual(result);
});

it('should format object with oneOf when property is undefined', () => {
const schema = {
type: 'object',
properties: {
from: {
format: 'address',
},
to: {
oneOf: [{ format: 'string' }, { type: 'null' }],
},
},
};

const data ={
from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401'
};

const result = { from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401'};

expect(
format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }),
).toEqual(result);
});
});
describe('isDataFormat', () => {
describe('valid cases', () => {
Expand Down

1 comment on commit 0db2b18

@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: 0db2b18 Previous: 8b435c1 Ratio
processingTx 21418 ops/sec (±7.87%) 9078 ops/sec (±4.67%) 0.42
processingContractDeploy 40091 ops/sec (±5.61%) 39858 ops/sec (±6.95%) 0.99
processingContractMethodSend 15185 ops/sec (±10.09%) 16111 ops/sec (±6.62%) 1.06
processingContractMethodCall 27867 ops/sec (±6.56%) 27631 ops/sec (±7.57%) 0.99
abiEncode 41653 ops/sec (±7.57%) 44291 ops/sec (±7.06%) 1.06
abiDecode 28976 ops/sec (±7.90%) 31565 ops/sec (±7.32%) 1.09
sign 1503 ops/sec (±0.71%) 1575 ops/sec (±1.11%) 1.05
verify 351 ops/sec (±2.70%) 365 ops/sec (±2.63%) 1.04

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

Please sign in to comment.