Skip to content

Commit

Permalink
Add types for user input in send/call (#5960)
Browse files Browse the repository at this point in the history
* Add types for user input in send/call

* Revert types

* remove chainId, to, data & input from NonPayableCallOptions

* Remove input from deployOptions

* Fix createAccessListType

* Fix createAccessListType payable and estimateGas

* Fix send and call

* Modify test

* Remove unused types from NonPayableCallOptions

* Allow type in estimateGas

* Revert deploy test

* Fix contract test

* Apply changes to docs

* Delete properties again after merge

* Update changelog

* Address feedback for types

* Undo delete to property

* Allow input for contract deployment

* Remove delete to

* Fix types

* Address feedback

* Remove unecessary file

---------

Co-authored-by: Wyatt Barnes <me@wyatt.email>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>
  • Loading branch information
5 people authored Apr 25, 2023
1 parent 547f725 commit 5153961
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
9 changes: 1 addition & 8 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export class Contract<Abi extends ContractAbi>
*
* ```ts
* myContract.deploy({
* input: '0x12345...',
* input: '0x12345...', // data keyword can be used, too. If input is used, data will be ignored.
* arguments: [123, 'My String']
* })
* .send({
Expand Down Expand Up @@ -604,10 +604,6 @@ export class Contract<Abi extends ContractAbi>
> => {
const modifiedOptions = { ...options };

// Remove to address
// modifiedOptions.to = '0x0000000000000000000000000000000000000000';
delete modifiedOptions.to;

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._contractMethodDeploySend(
abi as AbiFunctionFragment,
Expand All @@ -622,9 +618,6 @@ export class Contract<Abi extends ContractAbi>
) => {
const modifiedOptions = { ...options };

// Remove to address
delete modifiedOptions.to;

return this._contractMethodEstimateGas({
abi: abi as AbiFunctionFragment,
params: args as unknown[],
Expand Down
13 changes: 5 additions & 8 deletions packages/web3-eth-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export interface NonPayableMethodObject<Inputs = unknown[], Outputs = unknown[]>
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns The returned data of the createAccessList, e.g. The generated access list for transaction.
*
* ```ts
* ```ts
* const result = await MyContract.methods.myFunction().createAccessList();
* console.log(result);
*
Expand All @@ -319,7 +319,7 @@ export interface NonPayableMethodObject<Inputs = unknown[], Outputs = unknown[]>
* ],
* "gasUsed": "0x7671"
* }
* ```
* ```
*/
createAccessList(
tx?: NonPayableCallOptions,
Expand Down Expand Up @@ -493,7 +493,7 @@ export interface PayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns The returned data of the createAccessList, e.g. The generated access list for transaction.
*
* ```ts
* ```ts
* const result = await MyContract.methods.myFunction().createAccessList();
* console.log(result);
*
Expand All @@ -508,12 +508,9 @@ export interface PayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
* ],
* "gasUsed": "0x7671"
* }
* ```
*```
*/
createAccessList(
tx?: NonPayableCallOptions,
block?: BlockNumberOrTag,
): Promise<AccessListResult>;
createAccessList(tx?: PayableCallOptions, block?: BlockNumberOrTag): Promise<AccessListResult>;
}

export type Web3ContractContext = Partial<
Expand Down
11 changes: 8 additions & 3 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TransactionWithSenderAPI,
TransactionCall,
HexString,
Address,
NonPayableCallOptions,
PayableCallOptions,
ContractInitOptions,
Expand All @@ -38,7 +39,11 @@ export const getSendTxParams = ({
}: {
abi: AbiFunctionFragment;
params: unknown[];
options?: PayableCallOptions | NonPayableCallOptions;
options?: (PayableCallOptions | NonPayableCallOptions) & {
input?: HexString;
data?: HexString;
to?: Address;
};
contractOptions: ContractOptions;
}): TransactionCall => {
const deploymentCall = options?.input ?? options?.data ?? contractOptions.input;
Expand Down Expand Up @@ -80,7 +85,7 @@ export const getEthTxCallParams = ({
}: {
abi: AbiFunctionFragment;
params: unknown[];
options?: PayableCallOptions | NonPayableCallOptions;
options?: (PayableCallOptions | NonPayableCallOptions) & { to?: Address };
contractOptions: ContractOptions;
}): TransactionCall => {
if (!options?.to && !contractOptions.address) {
Expand Down Expand Up @@ -162,7 +167,7 @@ export const getCreateAccessListParams = ({
}: {
abi: AbiFunctionFragment;
params: unknown[];
options?: PayableCallOptions | NonPayableCallOptions;
options?: (PayableCallOptions | NonPayableCallOptions) & { to?: Address };
contractOptions: ContractOptions;
}): TransactionForAccessList => {
if (!options?.to && !contractOptions.address) {
Expand Down
5 changes: 5 additions & 0 deletions packages/web3-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The types `FMT_NUMBER`, `NumberTypes`, `FMT_BYTES`, `ByteTypes`, `DataFormat`, `DEFAULT_RETURN_FORMAT`, `ETH_DATA_FORMAT` and `FormatType` moved from `web3-utils`. (#5993)
- The types `ContractInitOptions`, `NonPayableCallOptions` and `PayableCallOptions` are moved from `web3-eth-contract`. (#5993)


## [Unreleased]

### Changed

- Removed chainId, to, data & input properties from NonPayableCallOptions

### Added

- Added `filters` param to the `Filter` type (#6010)
4 changes: 0 additions & 4 deletions packages/web3-types/src/eth_contract_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,11 @@ export interface ContractInitOptions {

export interface NonPayableCallOptions {
nonce?: HexString;
chainId?: HexString;
/**
* The address the call `transaction` should be made from. For calls the `from` property is optional however it is
* highly recommended to explicitly set it or it may default to address(0) depending on your node or provider.
*/
from?: Address;
to?: Address;
data?: HexString;
input?: HexString;
/**
* The maximum gas provided for this call “transaction” (gas limit)
*/
Expand Down

0 comments on commit 5153961

Please sign in to comment.