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

Commit 8d2d49c

Browse files
authored
'0x' strings returned by contract are incorrectly interpreted as nulls (#4730)
* 🐛 Fix decoding bug for the "0x" string * 🎨 Update the validation check * 🎨 Improve the condition to be more readable * 📝 Update change log doc
1 parent 115c3a9 commit 8d2d49c

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ Released with 1.0.0-beta.37 code base.
507507
- Updated README to include Webpack 5 create-react-app support instructions (#4173)
508508
- Update the documentation for `methods.myMethod.estimateGas` (#4702)
509509
- Fix typos in REVIEW.md and TESTING.md (#4691)
510+
- Fix encoding for "0x" string values (#4512)
510511

511512

512513
### Changed

packages/web3-eth-abi/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) {
362362

363363
outputs.forEach(function (output, i) {
364364
var decodedValue = res[returnValue.__length__];
365-
decodedValue = (decodedValue === '0x') ? null : decodedValue;
365+
366+
const isStringObject = typeof output === 'object' && output.type && output.type === 'string';
367+
const isStringType = typeof output === 'string' && output === 'string';
368+
369+
// only convert `0x` to null if it's not string value
370+
decodedValue = (decodedValue === '0x' && !isStringObject && !isStringType) ? null : decodedValue;
366371

367372
returnValue[i] = decodedValue;
368373

packages/web3-eth-abi/types/tests/abi-coder-test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ abiCoder.decodeParameter(
327327
0000000000000000000000004e`
328328
);
329329

330+
// $ExpectType { [key: string]: any; }
331+
abiCoder.decodeParameter(
332+
'string',
333+
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000'
334+
);
335+
330336
// $ExpectError
331337
abiCoder.decodeParameter('uint256', [345]);
332338
// $ExpectError

test/abi.decodeParameter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ describe('lib/solidity/coder', function () {
635635
'0000000000000000000000000000000000000000000000000000000000000060' +
636636
'0000000000000000000000000000000000000000000000000000000000000006' +
637637
'737472696e670000000000000000000000000000000000000000000000000000'})
638+
639+
test( { types: ['string'], expected: ["0x"],
640+
values: '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000'})
638641
});
639642
});
640643

0 commit comments

Comments
 (0)