Skip to content

Commit

Permalink
'0x' strings returned by contract are incorrectly interpreted as nulls (
Browse files Browse the repository at this point in the history
#4730)

* 🐛 Fix decoding bug for the "0x" string

* 🎨 Update the validation check

* 🎨 Improve the condition to be more readable

* 📝 Update change log doc
  • Loading branch information
nazarhussain committed Jan 27, 2022
1 parent 115c3a9 commit 8d2d49c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ Released with 1.0.0-beta.37 code base.
- Updated README to include Webpack 5 create-react-app support instructions (#4173)
- Update the documentation for `methods.myMethod.estimateGas` (#4702)
- Fix typos in REVIEW.md and TESTING.md (#4691)
- Fix encoding for "0x" string values (#4512)


### Changed
Expand Down
7 changes: 6 additions & 1 deletion packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) {

outputs.forEach(function (output, i) {
var decodedValue = res[returnValue.__length__];
decodedValue = (decodedValue === '0x') ? null : decodedValue;

const isStringObject = typeof output === 'object' && output.type && output.type === 'string';
const isStringType = typeof output === 'string' && output === 'string';

// only convert `0x` to null if it's not string value
decodedValue = (decodedValue === '0x' && !isStringObject && !isStringType) ? null : decodedValue;

returnValue[i] = decodedValue;

Expand Down
6 changes: 6 additions & 0 deletions packages/web3-eth-abi/types/tests/abi-coder-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ abiCoder.decodeParameter(
0000000000000000000000004e`
);

// $ExpectType { [key: string]: any; }
abiCoder.decodeParameter(
'string',
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000'
);

// $ExpectError
abiCoder.decodeParameter('uint256', [345]);
// $ExpectError
Expand Down
3 changes: 3 additions & 0 deletions test/abi.decodeParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000060' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'737472696e670000000000000000000000000000000000000000000000000000'})

test( { types: ['string'], expected: ["0x"],
values: '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000'})
});
});

Expand Down

0 comments on commit 8d2d49c

Please sign in to comment.