This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
make decodeFunctionCall and decodeFunctionReturn available at web3-eth-abi
#7345
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
60acf7d
move `decodeMethodParams` and `decodeMethodReturn` to web3-eth-abi
Muhammad-Altabba f1ee17d
add unit tests
Muhammad-Altabba 247f00c
renaming, enhancing and documenting
Muhammad-Altabba cc7a727
update CHANGELOG.md
Muhammad-Altabba 501ee4d
add 2 tests
Muhammad-Altabba b0dd7c5
add a test
Muhammad-Altabba 27efc5a
add 2 tests
Muhammad-Altabba 4a0e890
add examples in functions docs
Muhammad-Altabba dee0899
Merge branch '4.x' into 4917-add-decode-function-parameters
Muhammad-Altabba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.