Skip to content

Commit

Permalink
abi support json-string
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Jun 10, 2019
1 parent 4677ca0 commit 487362d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/abi/coder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function encodeParameter(typeStr, params) {
}

export function encodeParameters(types, params) {
if (typeof params === 'string') {
params = JSON.parse(params);
}

if (!isArray(types) || !isArray(params) || types.length !== params.length) {
throw new Error('[Error] Illegal types or params. Types and params should be array.');
}
Expand Down
11 changes: 10 additions & 1 deletion test/packages/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ describe('decodeParameter', function () {
});

describe('encodeParameters', function () {
it('1 abi constructor', function () {
it('1 abi constructor array', function () {
const encodeParametersResult1 = abi.encodeParameters({
'type': 'constructor',
'inputs': [
Expand All @@ -506,6 +506,15 @@ describe('encodeParameters', function () {
}, [[ '34', '43' ], '324567ff' ]);
assert.equal('000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002b0000000000000000000000000000000000000000000000000000000000000004324567ff00000000000000000000000000000000000000000000000000000000', encodeParametersResult1);
});
it('1 abi constructor string', function () {
const encodeParametersResult1 = abi.encodeParameters({
'type': 'constructor',
'inputs': [
{ 'type': 'uint8[]' }, { 'type': 'bytes' }
]
}, JSON.stringify([[ '34', '43' ], '324567ff' ]));
assert.equal('000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002b0000000000000000000000000000000000000000000000000000000000000004324567ff00000000000000000000000000000000000000000000000000000000', encodeParametersResult1);
});
it('multi abi constructor', function () {
const encodeParametersResult12 = abi.encodeParameters([
{ 'type': 'constructor', 'name': 'myMethods', 'inputs': [ { 'type': 'uint8[]' }, { 'type': 'bytes' } ] },
Expand Down

0 comments on commit 487362d

Please sign in to comment.