Skip to content

Commit

Permalink
Fix static tuple encoding (#4673) (#4884)
Browse files Browse the repository at this point in the history
* Fix static tuple encoding (#4673)

* updated change log

* Trigger build

* Trigger build 2

* Update CHANGELOG.md

Co-authored-by: Biswajit <patrabiswajit133@gmail.com>
Co-authored-by: Wyatt Barnes <me@wyatt.email>
  • Loading branch information
3 people authored Apr 12, 2022
1 parent fab97f7 commit 5a437ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,4 @@ Released with 1.0.0-beta.37 code base.
### Fixed
- Fix dead link in web3-eth.rst (#4916)
- Fix web3-core-method throws on `f.call = this.call` when intrinsic is frozen (#4918) (#4938)
- Fix static tuple encoding (#4673) (#4884)
14 changes: 8 additions & 6 deletions packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ ABICoder.prototype.encodeParameters = function (types, params) {
const coder = ethersAbiCoder._getCoder(ParamType.from(type));
const modifyParams = (coder, param) => {
if (coder.name === 'array') {
return param.map(p =>
modifyParams(
ethersAbiCoder._getCoder(ParamType.from(coder.type.replace('[]', ''))),
p
)
);
if (!coder.type.match(/\[(\d+)\]/)) {
return param.map(p => modifyParams(ethersAbiCoder._getCoder(ParamType.from(coder.type.replace('[]', ''))), p));
}
const arrayLength = parseInt(coder.type.match(/\[(\d+)\]/)[1]);
if (param.length !== arrayLength) {
throw new Error('Array length does not matches with the given input');
}
return param.map(p => modifyParams(ethersAbiCoder._getCoder(ParamType.from(coder.type.replace(/\[\d+\]/, ''))), p));
}
coder.coders.forEach((c, i) => {
if (c.name === 'tuple') {
Expand Down
9 changes: 7 additions & 2 deletions test/abi.encodeParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var coder = require('../packages/web3-eth-abi');
describe('lib/solidity/coder', function () {
describe('encodeParameter', function () {
var test = function (t) {
it('should turn ' + t.value + ' to ' + t.expected + ' for '+ t.type, function () {
assert.equal(coder.encodeParameter(t.type, t.value).replace('0x',''), t.expected);
it('should turn ' + (typeof t.value === 'object' ? JSON.stringify(t.value) : t.value) + ' to ' + t.expected + ' for ' + (typeof t.type === 'object' ? JSON.stringify(t.type) : t.type), function () {
assert.equal(coder.encodeParameter(t.type, t.value).replace('0x', ''), t.expected);
});
};

Expand Down Expand Up @@ -363,6 +363,11 @@ describe('lib/solidity/coder', function () {
'000000000000000000000000000000000000000000000000000000000000002a' +
'000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: { components: [{ name: "a", type: "uint256" }], type: "tuple[1]" },
value: [{ a: 2 }],
expected: '0000000000000000000000000000000000000000000000000000000000000002'
})
});
});

Expand Down

0 comments on commit 5a437ce

Please sign in to comment.