Skip to content

Commit

Permalink
Result of contract method .send is hard to type (#6883)
Browse files Browse the repository at this point in the history
* fix: Add videos to Plugin Section

* add deploy and send types

* lint fix

* add types to web3 packcage

* fix unit tests

* test

* test

* test

* revert

* changelog
  • Loading branch information
avkos authored Mar 15, 2024
1 parent 16a2c27 commit 2373e94
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2383,4 +2383,14 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

- replaced our eventEmitter to EventEmitter3 to support react native builds (#6253)

## [Unreleased]
## [Unreleased]
### Changed

#### web3

- Types `ContractDeploySend`, `ContractMethodSend`, `Web3PromiEvent` was exported (#6883)

### Added

#### web3-eth-contract

4 changes: 4 additions & 0 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,7 @@ Documentation:
- Fixed: The Contract is not using the context wallet passed if context was passed at constructor. (#6661)

## [Unreleased]

### Added

- Types `ContractDeploySend`, `ContractMethodSend` was added (#6883)
31 changes: 22 additions & 9 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,16 @@ import {
EventLog,
ContractAbiWithSignature,
ContractOptions,
TransactionReceipt,
FormatType,
} from 'web3-types';
import { format, isDataFormat, keccak256, toChecksumAddress , isContractInitOptions } from 'web3-utils';
import {
format,
isDataFormat,
keccak256,
toChecksumAddress,
isContractInitOptions,
} from 'web3-utils';
import {
isNullish,
validator,
Expand Down Expand Up @@ -113,7 +121,7 @@ type ContractBoundMethod<
Abi extends AbiFunctionFragment,
Method extends ContractMethod<Abi> = ContractMethod<Abi>,
> = (
...args: Method['Inputs'] extends undefined|unknown ? any[] : Method['Inputs']
...args: Method['Inputs'] extends undefined | unknown ? any[] : Method['Inputs']
) => Method['Abi']['stateMutability'] extends 'payable' | 'pure'
? PayableMethodObject<Method['Inputs'], Method['Outputs']>
: NonPayableMethodObject<Method['Inputs'], Method['Outputs']>;
Expand Down Expand Up @@ -148,6 +156,16 @@ export type ContractMethodsInterface<Abi extends ContractAbi> = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} & { [key: string]: ContractBoundMethod<any> };

export type ContractMethodSend = Web3PromiEvent<
FormatType<TransactionReceipt, typeof DEFAULT_RETURN_FORMAT>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;
export type ContractDeploySend<Abi extends ContractAbi> = Web3PromiEvent<
// eslint-disable-next-line no-use-before-define
Contract<Abi>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;

/**
* @hidden
* The event object can be accessed from `myContract.events.myEvent`.
Expand Down Expand Up @@ -768,12 +786,7 @@ export class Contract<Abi extends ContractAbi>
const deployData = _input ?? _data;
return {
arguments: args,
send: (
options?: PayableTxOptions,
): Web3PromiEvent<
Contract<Abi>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
> => {
send: (options?: PayableTxOptions): ContractDeploySend<Abi> => {
const modifiedOptions = { ...options };

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand Down Expand Up @@ -1101,7 +1114,7 @@ export class Contract<Abi extends ContractAbi>
block,
),

send: (options?: PayableTxOptions | NonPayableTxOptions) =>
send: (options?: PayableTxOptions | NonPayableTxOptions): ContractMethodSend =>
this._contractMethodSend(methodAbi, abiParams, internalErrorsAbis, options),

estimateGas: async <ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
Expand Down
6 changes: 5 additions & 1 deletion packages/web3/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,8 @@ Documentation:

- Added EIP-6963 utility function `requestEIP6963Providers` for multi provider discovery ( other details are in root changelog )

## [Unreleased]
## [Unreleased]

### Changed

- Types `ContractDeploySend`, `ContractMethodSend`, `Web3PromiEvent` was exported (#6883)
4 changes: 2 additions & 2 deletions packages/web3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ export default Web3;
* Named exports for all objects which are the default-exported-object in their packages
*/
export { Web3 };
export { Web3Context, Web3PluginBase, Web3EthPluginBase } from 'web3-core';
export { Web3Context, Web3PluginBase, Web3EthPluginBase, Web3PromiEvent } from 'web3-core';
export { Web3Eth } from 'web3-eth';
export { Contract } from 'web3-eth-contract';
export { Contract, ContractDeploySend, ContractMethodSend } from 'web3-eth-contract';
export { Iban } from 'web3-eth-iban';
export { Personal } from 'web3-eth-personal';
export { Net } from 'web3-net';
Expand Down

1 comment on commit 2373e94

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2373e94 Previous: 6c075db Ratio
processingTx 9434 ops/sec (±4.95%) 9301 ops/sec (±4.81%) 0.99
processingContractDeploy 41543 ops/sec (±6.88%) 39129 ops/sec (±7.62%) 0.94
processingContractMethodSend 20522 ops/sec (±5.92%) 19443 ops/sec (±5.19%) 0.95
processingContractMethodCall 39893 ops/sec (±4.91%) 38971 ops/sec (±6.34%) 0.98
abiEncode 46074 ops/sec (±6.65%) 44252 ops/sec (±6.92%) 0.96
abiDecode 32141 ops/sec (±6.84%) 30419 ops/sec (±8.89%) 0.95
sign 1688 ops/sec (±0.86%) 1656 ops/sec (±4.08%) 0.98
verify 375 ops/sec (±0.55%) 373 ops/sec (±0.78%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.