-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make
decodeFunctionCall
and decodeFunctionReturn
available at `we…
…b3-eth-abi` (#7345) * move `decodeFunctionCall` and `decodeFunctionReturn` to web3-eth-abi * add unit tests * update CHANGELOG.md * add functions docs
- Loading branch information
1 parent
331aa9c
commit 3687070
Showing
6 changed files
with
378 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
packages/web3-eth-abi/test/unit/decodeMethodParamsAndReturn.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { AbiFunctionFragment } from 'web3-types'; | ||
import { decodeFunctionCall, decodeFunctionReturn } from '../../src'; | ||
|
||
describe('decodeFunctionCall and decodeFunctionReturn tests should pass', () => { | ||
it('decodeFunctionCall should decode single-value data of a method', async () => { | ||
const data = | ||
'0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
|
||
const params = decodeFunctionCall( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
outputs: [ | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(params).toMatchObject({ | ||
__method__: 'setGreeting(string)', | ||
__length__: 1, | ||
'0': 'Hello', | ||
_greeting: 'Hello', | ||
}); | ||
}); | ||
|
||
it('decodeFunctionCall should decode data of a method without removing the method signature (if intended)', async () => { | ||
const data = | ||
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
|
||
const params = decodeFunctionCall( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
outputs: [ | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
false, | ||
); | ||
|
||
expect(params).toMatchObject({ | ||
__method__: 'setGreeting(string)', | ||
__length__: 1, | ||
'0': 'Hello', | ||
_greeting: 'Hello', | ||
}); | ||
}); | ||
|
||
it('decodeFunctionCall should throw if no inputs at the ABI', async () => { | ||
const data = | ||
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
|
||
expect(() => | ||
decodeFunctionCall( | ||
{ | ||
name: 'setGreeting', | ||
// no `inputs` provided! | ||
outputs: [ | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
false, | ||
), | ||
).toThrow('No inputs found in the ABI'); | ||
}); | ||
|
||
it('decodeFunctionCall should decode multi-value data of a method', async () => { | ||
const data = | ||
'0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
|
||
const params = decodeFunctionCall( | ||
{ | ||
inputs: [ | ||
{ internalType: 'string', name: '_greeting', type: 'string' }, | ||
{ internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
], | ||
name: 'setGreeting', | ||
outputs: [ | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(params).toEqual({ | ||
'0': 'Hello', | ||
'1': 'Another Greeting', | ||
__length__: 2, | ||
__method__: 'setGreeting(string,string)', | ||
_greeting: 'Hello', | ||
_second_greeting: 'Another Greeting', | ||
}); | ||
}); | ||
|
||
it('decodeFunctionReturn should decode single-value data of a method', async () => { | ||
const data = | ||
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
|
||
const decodedResult = decodeFunctionReturn( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(decodedResult).toBe('Hello'); | ||
}); | ||
|
||
it('decodeFunctionReturn should decode multi-value data of a method', async () => { | ||
const data = | ||
'0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
|
||
const decodedResult = decodeFunctionReturn( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
outputs: [ | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(decodedResult).toEqual({ '0': 'Hello', '1': true, __length__: 2 }); | ||
}); | ||
|
||
it('decodeFunctionReturn should decode nothing if it is called on a constructor', async () => { | ||
const data = 'anything passed should be returned as-is'; | ||
|
||
const decodedResult = decodeFunctionReturn( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
stateMutability: 'nonpayable', | ||
type: 'constructor', | ||
} as unknown as AbiFunctionFragment, | ||
data, | ||
); | ||
|
||
expect(decodedResult).toEqual(data); | ||
}); | ||
|
||
it('decodeFunctionReturn should return `null` if no values passed', async () => { | ||
const data = ''; | ||
|
||
const decodedResult = decodeFunctionReturn( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
outputs: [ | ||
{ internalType: 'string', name: '', type: 'string' }, | ||
{ internalType: 'bool', name: '', type: 'bool' }, | ||
], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(decodedResult).toBeNull(); | ||
}); | ||
|
||
it('decodeFunctionReturn should return `null` if no function output provided', async () => { | ||
const data = '0x000000'; | ||
|
||
const decodedResult = decodeFunctionReturn( | ||
{ | ||
inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], | ||
name: 'setGreeting', | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
data, | ||
); | ||
|
||
expect(decodedResult).toBeNull(); | ||
}); | ||
}); |
Oops, something went wrong.
3687070
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
22856
ops/sec (±6.62%
)20042
ops/sec (±7.50%
)0.88
processingContractDeploy
37573
ops/sec (±8.57%
)38574
ops/sec (±5.63%
)1.03
processingContractMethodSend
15453
ops/sec (±7.92%
)14751
ops/sec (±9.38%
)0.95
processingContractMethodCall
27091
ops/sec (±7.83%
)28271
ops/sec (±6.08%
)1.04
abiEncode
42669
ops/sec (±7.08%
)41366
ops/sec (±7.53%
)0.97
abiDecode
30350
ops/sec (±6.23%
)28375
ops/sec (±8.41%
)0.93
sign
1520
ops/sec (±3.42%
)1500
ops/sec (±1.06%
)0.99
verify
362
ops/sec (±0.69%
)356
ops/sec (±0.80%
)0.98
This comment was automatically generated by workflow using github-action-benchmark.