-
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.
Fix Contract methods input param type any[] (#7340)
* fix types * fix * Update type.test.ts * fix tests * fix test
- Loading branch information
Showing
6 changed files
with
96 additions
and
6 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
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
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,79 @@ | ||
/* | ||
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 { Contract } from '../../src'; | ||
|
||
describe('Contract method types', () => { | ||
it('contract method params types test', () => { | ||
const abiAsConst = [ | ||
{ | ||
inputs: [ | ||
{ internalType: 'uint256', name: 'testArg1', type: 'uint256' }, | ||
{ internalType: 'uint256', name: 'testArg2', type: 'uint256' }, | ||
], | ||
name: 'testWithParams', | ||
outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [], | ||
name: 'testWithoutParams', | ||
outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const; | ||
|
||
const abiAsArray = [ | ||
{ | ||
inputs: [ | ||
{ internalType: 'uint256', name: 'testArg1', type: 'uint256' }, | ||
{ internalType: 'uint256', name: 'testArg2', type: 'uint256' }, | ||
], | ||
name: 'testWithParams', | ||
outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
{ | ||
inputs: [], | ||
name: 'testWithoutParams', | ||
outputs: [{ internalType: 'uint256', name: 'testRes1', type: 'uint256' }], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
]; | ||
|
||
// abi as const | ||
const contract = new Contract<typeof abiAsConst>(abiAsConst); | ||
contract.methods.testWithParams(1, 2); // no ts error - works as expected | ||
// @ts-expect-error ts compiler error | ||
expect(() => contract.methods.testWithParams()).toThrow(); // ts error - works as expected | ||
// @ts-expect-error ts compiler error | ||
contract.methods.testWithoutParams(1, 2); // ts error - works as expected | ||
contract.methods.testWithoutParams(); // no ts error - works as expected | ||
|
||
// abi as usual array type | ||
const contract2 = new Contract<typeof abiAsArray>(abiAsArray); | ||
// because we do not know exact type without const or provided type | ||
contract2.methods.testWithParams(1, 2); // no ts error - works as expected | ||
contract2.methods.testWithoutParams(); // no ts error - works as expected | ||
contract2.methods.testWithoutParams(1, 2); // no ts error - works as expected | ||
expect(() => contract2.methods.testWithParams()).toThrow(); // no ts error - works as expected | ||
}); | ||
}); |
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
d3baae6
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
22817
ops/sec (±8.03%
)22704
ops/sec (±8.84%
)1.00
processingContractDeploy
39756
ops/sec (±7.53%
)40462
ops/sec (±7.31%
)1.02
processingContractMethodSend
16008
ops/sec (±6.83%
)16743
ops/sec (±7.19%
)1.05
processingContractMethodCall
27788
ops/sec (±6.35%
)27222
ops/sec (±7.83%
)0.98
abiEncode
44379
ops/sec (±7.28%
)45273
ops/sec (±6.86%
)1.02
abiDecode
30710
ops/sec (±7.66%
)30688
ops/sec (±6.77%
)1.00
sign
1530
ops/sec (±4.46%
)1587
ops/sec (±0.68%
)1.04
verify
370
ops/sec (±0.42%
)366
ops/sec (±0.65%
)0.99
This comment was automatically generated by workflow using github-action-benchmark.