|
| 1 | +/* |
| 2 | +This file is part of web3.js. |
| 3 | +
|
| 4 | +web3.js is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU Lesser General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +web3.js is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Lesser General Public License |
| 15 | +along with web3.js. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +import { AbiFunctionFragment } from 'web3-types'; |
| 19 | +import { decodeFunctionCall, decodeFunctionReturn } from '../../src'; |
| 20 | + |
| 21 | +describe('decodeFunctionCall and decodeFunctionReturn tests should pass', () => { |
| 22 | + it('decodeFunctionCall should decode single-value data of a method', async () => { |
| 23 | + const data = |
| 24 | + '0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; |
| 25 | + |
| 26 | + const params = decodeFunctionCall( |
| 27 | + { |
| 28 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 29 | + name: 'setGreeting', |
| 30 | + outputs: [ |
| 31 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 32 | + { internalType: 'string', name: '', type: 'string' }, |
| 33 | + ], |
| 34 | + stateMutability: 'nonpayable', |
| 35 | + type: 'function', |
| 36 | + }, |
| 37 | + data, |
| 38 | + ); |
| 39 | + |
| 40 | + expect(params).toMatchObject({ |
| 41 | + __method__: 'setGreeting(string)', |
| 42 | + __length__: 1, |
| 43 | + '0': 'Hello', |
| 44 | + _greeting: 'Hello', |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('decodeFunctionCall should decode data of a method without removing the method signature (if intended)', async () => { |
| 49 | + const data = |
| 50 | + '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; |
| 51 | + |
| 52 | + const params = decodeFunctionCall( |
| 53 | + { |
| 54 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 55 | + name: 'setGreeting', |
| 56 | + outputs: [ |
| 57 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 58 | + { internalType: 'string', name: '', type: 'string' }, |
| 59 | + ], |
| 60 | + stateMutability: 'nonpayable', |
| 61 | + type: 'function', |
| 62 | + }, |
| 63 | + data, |
| 64 | + false, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(params).toMatchObject({ |
| 68 | + __method__: 'setGreeting(string)', |
| 69 | + __length__: 1, |
| 70 | + '0': 'Hello', |
| 71 | + _greeting: 'Hello', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('decodeFunctionCall should throw if no inputs at the ABI', async () => { |
| 76 | + const data = |
| 77 | + '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; |
| 78 | + |
| 79 | + expect(() => |
| 80 | + decodeFunctionCall( |
| 81 | + { |
| 82 | + name: 'setGreeting', |
| 83 | + // no `inputs` provided! |
| 84 | + outputs: [ |
| 85 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 86 | + { internalType: 'string', name: '', type: 'string' }, |
| 87 | + ], |
| 88 | + stateMutability: 'nonpayable', |
| 89 | + type: 'function', |
| 90 | + }, |
| 91 | + data, |
| 92 | + false, |
| 93 | + ), |
| 94 | + ).toThrow('No inputs found in the ABI'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('decodeFunctionCall should decode multi-value data of a method', async () => { |
| 98 | + const data = |
| 99 | + '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; |
| 100 | + |
| 101 | + const params = decodeFunctionCall( |
| 102 | + { |
| 103 | + inputs: [ |
| 104 | + { internalType: 'string', name: '_greeting', type: 'string' }, |
| 105 | + { internalType: 'string', name: '_second_greeting', type: 'string' }, |
| 106 | + ], |
| 107 | + name: 'setGreeting', |
| 108 | + outputs: [ |
| 109 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 110 | + { internalType: 'string', name: '', type: 'string' }, |
| 111 | + ], |
| 112 | + stateMutability: 'nonpayable', |
| 113 | + type: 'function', |
| 114 | + }, |
| 115 | + data, |
| 116 | + ); |
| 117 | + |
| 118 | + expect(params).toEqual({ |
| 119 | + '0': 'Hello', |
| 120 | + '1': 'Another Greeting', |
| 121 | + __length__: 2, |
| 122 | + __method__: 'setGreeting(string,string)', |
| 123 | + _greeting: 'Hello', |
| 124 | + _second_greeting: 'Another Greeting', |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('decodeFunctionReturn should decode single-value data of a method', async () => { |
| 129 | + const data = |
| 130 | + '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; |
| 131 | + |
| 132 | + const decodedResult = decodeFunctionReturn( |
| 133 | + { |
| 134 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 135 | + name: 'setGreeting', |
| 136 | + outputs: [{ internalType: 'string', name: '', type: 'string' }], |
| 137 | + stateMutability: 'nonpayable', |
| 138 | + type: 'function', |
| 139 | + }, |
| 140 | + data, |
| 141 | + ); |
| 142 | + |
| 143 | + expect(decodedResult).toBe('Hello'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('decodeFunctionReturn should decode multi-value data of a method', async () => { |
| 147 | + const data = |
| 148 | + '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; |
| 149 | + |
| 150 | + const decodedResult = decodeFunctionReturn( |
| 151 | + { |
| 152 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 153 | + name: 'setGreeting', |
| 154 | + outputs: [ |
| 155 | + { internalType: 'string', name: '', type: 'string' }, |
| 156 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 157 | + ], |
| 158 | + stateMutability: 'nonpayable', |
| 159 | + type: 'function', |
| 160 | + }, |
| 161 | + data, |
| 162 | + ); |
| 163 | + |
| 164 | + expect(decodedResult).toEqual({ '0': 'Hello', '1': true, __length__: 2 }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('decodeFunctionReturn should decode nothing if it is called on a constructor', async () => { |
| 168 | + const data = 'anything passed should be returned as-is'; |
| 169 | + |
| 170 | + const decodedResult = decodeFunctionReturn( |
| 171 | + { |
| 172 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 173 | + stateMutability: 'nonpayable', |
| 174 | + type: 'constructor', |
| 175 | + } as unknown as AbiFunctionFragment, |
| 176 | + data, |
| 177 | + ); |
| 178 | + |
| 179 | + expect(decodedResult).toEqual(data); |
| 180 | + }); |
| 181 | + |
| 182 | + it('decodeFunctionReturn should return `null` if no values passed', async () => { |
| 183 | + const data = ''; |
| 184 | + |
| 185 | + const decodedResult = decodeFunctionReturn( |
| 186 | + { |
| 187 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 188 | + name: 'setGreeting', |
| 189 | + outputs: [ |
| 190 | + { internalType: 'string', name: '', type: 'string' }, |
| 191 | + { internalType: 'bool', name: '', type: 'bool' }, |
| 192 | + ], |
| 193 | + stateMutability: 'nonpayable', |
| 194 | + type: 'function', |
| 195 | + }, |
| 196 | + data, |
| 197 | + ); |
| 198 | + |
| 199 | + expect(decodedResult).toBeNull(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('decodeFunctionReturn should return `null` if no function output provided', async () => { |
| 203 | + const data = '0x000000'; |
| 204 | + |
| 205 | + const decodedResult = decodeFunctionReturn( |
| 206 | + { |
| 207 | + inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }], |
| 208 | + name: 'setGreeting', |
| 209 | + stateMutability: 'nonpayable', |
| 210 | + type: 'function', |
| 211 | + }, |
| 212 | + data, |
| 213 | + ); |
| 214 | + |
| 215 | + expect(decodedResult).toBeNull(); |
| 216 | + }); |
| 217 | +}); |
0 commit comments