|
| 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 { Contract } from '../../src'; |
| 19 | + |
| 20 | +describe('Contract method types', () => { |
| 21 | + it('contract method params types test', () => { |
| 22 | + const abiAsConst = [ |
| 23 | + { |
| 24 | + inputs: [ |
| 25 | + { internalType: 'uint256', name: 'testArg1', type: 'uint256' }, |
| 26 | + { internalType: 'uint256', name: 'testArg2', type: 'uint256' }, |
| 27 | + ], |
| 28 | + name: 'testWithParams', |
| 29 | + outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], |
| 30 | + stateMutability: 'nonpayable', |
| 31 | + type: 'function', |
| 32 | + }, |
| 33 | + { |
| 34 | + inputs: [], |
| 35 | + name: 'testWithoutParams', |
| 36 | + outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], |
| 37 | + stateMutability: 'nonpayable', |
| 38 | + type: 'function', |
| 39 | + }, |
| 40 | + ] as const; |
| 41 | + |
| 42 | + const abiAsArray = [ |
| 43 | + { |
| 44 | + inputs: [ |
| 45 | + { internalType: 'uint256', name: 'testArg1', type: 'uint256' }, |
| 46 | + { internalType: 'uint256', name: 'testArg2', type: 'uint256' }, |
| 47 | + ], |
| 48 | + name: 'testWithParams', |
| 49 | + outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], |
| 50 | + stateMutability: 'nonpayable', |
| 51 | + type: 'function', |
| 52 | + }, |
| 53 | + { |
| 54 | + inputs: [], |
| 55 | + name: 'testWithoutParams', |
| 56 | + outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], |
| 57 | + stateMutability: 'nonpayable', |
| 58 | + type: 'function', |
| 59 | + }, |
| 60 | + ]; |
| 61 | + |
| 62 | + // abi as const |
| 63 | + const contract = new Contract<typeof abiAsConst>(abiAsConst); |
| 64 | + contract.methods.testWithParams(1, 2); // no ts error - works as expected |
| 65 | + // @ts-expect-error ts compiler error |
| 66 | + expect(() => contract.methods.testWithParams()).toThrow(); // ts error - works as expected |
| 67 | + // @ts-expect-error ts compiler error |
| 68 | + contract.methods.testWithoutParams(1, 2); // ts error - works as expected |
| 69 | + contract.methods.testWithoutParams(); // no ts error - works as expected |
| 70 | + |
| 71 | + // abi as usual array type |
| 72 | + const contract2 = new Contract<typeof abiAsArray>(abiAsArray); |
| 73 | + // because we do not know exact type without const or provided type |
| 74 | + contract2.methods.testWithParams(1, 2); // no ts error - works as expected |
| 75 | + contract2.methods.testWithoutParams(); // no ts error - works as expected |
| 76 | + contract2.methods.testWithoutParams(1, 2); // no ts error - works as expected |
| 77 | + expect(() => contract2.methods.testWithParams()).toThrow(); // no ts error - works as expected |
| 78 | + }); |
| 79 | +}); |
0 commit comments