Skip to content

Commit

Permalink
Modified the logic of replacement type string for fixed length tuple …
Browse files Browse the repository at this point in the history
…array
  • Loading branch information
jimni1222 committed Apr 19, 2021
1 parent a1fd078 commit ec96189
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/caver-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ ABICoder.prototype.encodeParameters = function(types, params) {
// eslint-disable-next-line no-shadow
const modifyParams = (coder, param) => {
if (coder.name === 'array') {
return param.map(p => modifyParams(ethersAbiCoder._getCoder(ParamType.from(coder.type.replace('[]', ''))), p))
return param.map(p => {
// `coder.type.replace('[]','')` can handle'tuple(string,string)[]', but cannot handle `tuple(string,string)[3]'.
// Therefore, in order to handle tuple arrays of fixed length, the logic is changed to handle strings using regular expression expressions.
const replacedType = coder.type.replace(/\[[1-9]*\]/g, '')
const parameterType = ParamType.from(replacedType)
const gotCoder = ethersAbiCoder._getCoder(parameterType)
modifyParams(gotCoder, p)
})
}
coder.coders.forEach((c, i) => {
if (c.name === 'tuple') {
Expand Down
12 changes: 12 additions & 0 deletions test/abi.encodeParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ describe('caver.abi.encodeParameter', function() {
expected:
'0000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000',
})
test({
type: 'tuple(string,string)[]',
value: [['str1', 'str2'], ['str3', 'str4']],
expected:
'0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004737472310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047374723200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004737472330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047374723400000000000000000000000000000000000000000000000000000000',
})
test({
type: 'tuple(string,string)[2]',
value: [['str1', 'str2'], ['str3', 'str4']],
expected:
'000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004737472310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047374723200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004737472330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047374723400000000000000000000000000000000000000000000000000000000',
})
})
})

Expand Down

0 comments on commit ec96189

Please sign in to comment.