Skip to content

Commit

Permalink
Specify SignTransaction's argument types (#5993)
Browse files Browse the repository at this point in the history
* move some data-format and contract types to web3-types

* at `signTransaction` use `Transaction` instead of `Record<string, unknown>`

* update CHANGELOG.md files
  • Loading branch information
Muhammad-Altabba authored and spacesailor24 committed Apr 14, 2023
1 parent 5c87bee commit a9edc91
Show file tree
Hide file tree
Showing 110 changed files with 532 additions and 340 deletions.
4 changes: 2 additions & 2 deletions docs/docs/guides/web3_plugin_guide/plugin_authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ A workaround for this issue is available, below is an example of it:
import { Web3Context, Web3PluginBase } from 'web3-core';
import { ContractAbi } from 'web3-eth-abi';
import Contract from 'web3-eth-contract';
import { Address } from 'web3-types';
import { DataFormat, DEFAULT_RETURN_FORMAT, format } from 'web3-utils';
import { Address, DataFormat, DEFAULT_RETURN_FORMAT } from 'web3-types';
import { format } from 'web3-utils';

import { ERC20TokenAbi } from './ERC20Token';

Expand Down
1 change: 1 addition & 0 deletions packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- If a transaction object with a `data` property is passed to `txInputOptionsFormatter`, it will now be replaced with `input` (#5915)
- The types `TransactionTypeParser` and `TransactionBuilder` are now utilizing the type `Transaction` for the transaction object. (#5993)
- no need for polyfilling nodejs `net` and `fs` modules

### Removed
Expand Down
5 changes: 2 additions & 3 deletions packages/web3-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 { HexString } from 'web3-types';
import { HexString, Transaction } from 'web3-types';

// TODO: When we have `web3-types` package we can share TransactionType
export type TransactionTypeParser = (transaction: Record<string, unknown>) => HexString | undefined;
export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined;
6 changes: 3 additions & 3 deletions packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
HexString,
EthExecutionAPI,
Web3BaseProvider,
Transaction,
} from 'web3-types';
import { isNullish } from 'web3-utils';
import { ExistingPluginNamespaceError } from 'web3-errors';
Expand Down Expand Up @@ -367,11 +368,10 @@ export class Web3Context<
}

// To avoid cycle dependency declare this type in this file
// TODO: When we have `web3-types` package we can share TransactionType
export type TransactionBuilder<API extends Web3APISpec = unknown> = <
ReturnType = Record<string, unknown>,
ReturnType = Transaction,
>(options: {
transaction: Record<string, unknown>;
transaction: Transaction;
web3Context: Web3Context<API>;
privateKey?: HexString | Buffer;
}) => Promise<ReturnType>;
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core/src/web3_subscription_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ 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 { Web3APISpec } from 'web3-types';
import { DataFormat, DEFAULT_RETURN_FORMAT, Web3APISpec } from 'web3-types';
import { ProviderError, SubscriptionError } from 'web3-errors';
import { isNullish, DataFormat, DEFAULT_RETURN_FORMAT } from 'web3-utils';
import { isNullish } from 'web3-utils';
import { isSupportSubscriptions } from './utils';
import { Web3RequestManager, Web3RequestManagerEvent } from './web3_request_manager';
import { Web3SubscriptionConstructor } from './web3_subscriptions';
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-core/src/web3_subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

// eslint-disable-next-line max-classes-per-file
import { DataFormat, DEFAULT_RETURN_FORMAT } from 'web3-utils';
import {
HexString,
BlockOutput,
Expand All @@ -27,6 +26,8 @@ import {
Log,
JsonRpcNotification,
JsonRpcSubscriptionResult,
DataFormat,
DEFAULT_RETURN_FORMAT,
} from 'web3-types';
import { jsonRpc } from 'web3-utils';
import { Web3EventEmitter, Web3EventMap } from './web3_event_emitter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 { DEFAULT_RETURN_FORMAT } from 'web3-utils';
import { DEFAULT_RETURN_FORMAT } from 'web3-types';
import { Web3RequestManagerEvent } from '../../src/web3_request_manager';
import { Web3SubscriptionManager } from '../../src/web3_subscription_manager';
import { ExampleSubscription } from './fixtures/example_subscription';
Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Moved @ethereumjs/tx, @ethereumjs/common code to our source code (#5963)
- The method `signTransaction` returned by `privateKeyToAccount` is now accepting the type `Transaction` for its argument. (#5993)
3 changes: 2 additions & 1 deletion packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
PBKDF2SHA256Params,
ScryptParams,
KeyStore,
Transaction,
} from 'web3-types';
import {
bytesToBuffer,
Expand Down Expand Up @@ -594,7 +595,7 @@ export const privateKeyToAccount = (privateKey: Bytes, ignoreLength?: boolean):
address: privateKeyToAddress(privateKeyBuffer),
privateKey: bytesToHex(privateKeyBuffer),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
signTransaction: (_tx: Record<string, unknown>) => {
signTransaction: (_tx: Transaction) => {
throw new TransactionSigningError('Do not have network access to sign the transaction');
},
sign: (data: Record<string, unknown> | string) =>
Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ const transactionHash = receipt.transactionHash;

- `getSendTxParams` will now return `input` instead of `data` in returned transaction parameters object (#5915)
- `Contract` constructor will now thrown new `ContractTransactionDataAndInputError` if both `data` and `input` are passed in `ContractInitOptions` for `Contract` constructor (#5915)
- The types `ContractInitOptions`, `NonPayableCallOptions` and `PayableCallOptions` are moved to `web3-types`. (#5993)

### Removed

Expand Down
14 changes: 5 additions & 9 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ import {
HexString,
LogsInput,
Mutable,
} from 'web3-types';
import {
ContractInitOptions,
NonPayableCallOptions,
PayableCallOptions,
DataFormat,
DEFAULT_RETURN_FORMAT,
format,
isDataFormat,
toChecksumAddress,
} from 'web3-utils';
} from 'web3-types';
import { format, isDataFormat, toChecksumAddress } from 'web3-utils';
import {
isNullish,
validator,
Expand All @@ -85,13 +84,10 @@ import { LogsSubscription } from './log_subscription';
import {
ContractAbiWithSignature,
ContractEventOptions,
ContractInitOptions,
ContractOptions,
EventLog,
NonPayableCallOptions,
NonPayableMethodObject,
NonPayableTxOptions,
PayableCallOptions,
PayableMethodObject,
PayableTxOptions,
Web3ContractContext,
Expand Down
16 changes: 7 additions & 9 deletions packages/web3-eth-contract/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ 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 {
DataFormat,
DEFAULT_RETURN_FORMAT,
FMT_BYTES,
FMT_NUMBER,
format,
isNullish,
} from 'web3-utils';
import { format, isNullish } from 'web3-utils';

import {
AbiConstructorFragment,
Expand All @@ -34,6 +27,10 @@ import {
HexString,
Topic,
Numbers,
FMT_NUMBER,
FMT_BYTES,
DataFormat,
DEFAULT_RETURN_FORMAT,
} from 'web3-types';

import {
Expand All @@ -50,8 +47,9 @@ import {
import { blockSchema, logSchema } from 'web3-eth';

import { Web3ContractError } from 'web3-errors';

// eslint-disable-next-line import/no-cycle
import { ContractAbiWithSignature, ContractOptions, EventLog } from './types';
import { ContractOptions, ContractAbiWithSignature, EventLog } from './types';

export const encodeEventABI = (
{ address }: ContractOptions,
Expand Down
6 changes: 2 additions & 4 deletions packages/web3-eth-contract/src/log_subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ 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 { AbiEventFragment, LogsInput, HexString, Topic } from 'web3-types';
import { AbiEventFragment, LogsInput, HexString, Topic, DataFormat } from 'web3-types';
import { Web3RequestManager, Web3Subscription } from 'web3-core';
// eslint-disable-next-line import/no-cycle
import { DataFormat } from 'web3-utils';
// eslint-disable-next-line import/no-cycle
import { decodeEventABI } from './encoding';
// eslint-disable-next-line import/no-cycle
import { ContractAbiWithSignature, EventLog } from './types';
import { EventLog, ContractAbiWithSignature } from './types';

/**
* LogSubscription to be used to subscribe to events logs.
Expand Down
82 changes: 13 additions & 69 deletions packages/web3-eth-contract/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ import { Web3ContextInitOptions, Web3PromiEvent } from 'web3-core';
import { NewHeadsSubscription, SendTransactionEvents } from 'web3-eth';
import {
AccessListResult,
AbiFragment,
Address,
BlockNumberOrTag,
Bytes,
ContractAbi,
EthExecutionAPI,
HexString,
HexString32Bytes,
Numbers,
SupportedProviders,
TransactionReceipt,
NonPayableCallOptions,
PayableCallOptions,
DataFormat,
DEFAULT_RETURN_FORMAT,
FormatType,
AbiFragment,
Address,
Bytes,
ContractAbi,
HexString32Bytes,
Uint,
} from 'web3-types';
import { DataFormat, DEFAULT_RETURN_FORMAT, FormatType } from 'web3-utils';
// eslint-disable-next-line import/no-cycle
import { LogsSubscription } from './log_subscription';

export type { TransactionReceipt } from 'web3-types';
export type NonPayableTxOptions = NonPayableCallOptions;
export type PayableTxOptions = PayableCallOptions;

export type ContractAbiWithSignature = ReadonlyArray<AbiFragment & { signature: HexString }>;

export interface EventLog {
Expand Down Expand Up @@ -132,67 +137,6 @@ export interface ContractOptions {
address?: Address; // All transactions generated by web3.js from this contract will contain this address as the "to".
}

export interface ContractInitOptions {
/**
* The maximum gas provided for a transaction (gas limit).
*/
readonly gas?: Uint;
readonly gasLimit?: Uint;
/**
* The gas price in wei to use for transactions.
*/
readonly gasPrice?: Uint;
/**
* The address transactions should be made from
*/
readonly from?: Address;
/**
* The byte code of the contract. Used when the contract gets {@link Contract.deploy | deployed}
*/
readonly data?: Bytes;
readonly input?: Bytes;

readonly provider?: SupportedProviders<EthExecutionAPI> | string;
/**
* If `true`, the defaults of the contract instance will be updated automatically based on the changes of the context used to instantiate the contract.
*/
readonly syncWithContext?: boolean;
}

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)
*/
gas?: string;
maxPriorityFeePerGas?: HexString;
maxFeePerGas?: HexString;
/**
* The gas price in wei to use for this call `transaction`.
*/
gasPrice?: string;
type?: string | number;
}

export interface PayableCallOptions extends NonPayableCallOptions {
/**
*
*/
value?: string;
}

export type NonPayableTxOptions = NonPayableCallOptions;
export type PayableTxOptions = PayableCallOptions;

export interface NonPayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
arguments: Inputs;
/**
Expand Down
11 changes: 4 additions & 7 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ import {
TransactionWithSenderAPI,
TransactionCall,
HexString,
} from 'web3-types';
import { isNullish, mergeDeep, toHex } from 'web3-utils';
import { encodeMethodABI } from './encoding';
import {
NonPayableCallOptions,
PayableCallOptions,
ContractOptions,
Web3ContractContext,
ContractInitOptions,
} from './types';
} from 'web3-types';
import { isNullish, mergeDeep, toHex } from 'web3-utils';
import { encodeMethodABI } from './encoding';
import { ContractOptions, Web3ContractContext } from './types';

export const getSendTxParams = ({
abi,
Expand Down
8 changes: 6 additions & 2 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ import {
EthExecutionAPI,
TransactionReceipt,
Web3NetAPI,
NonPayableCallOptions,
DEFAULT_RETURN_FORMAT,
FormatType,
FMT_NUMBER,
DataFormat,
} from 'web3-types';
import { DEFAULT_RETURN_FORMAT, FormatType, FMT_NUMBER, DataFormat } from 'web3-utils';
import { NonPayableCallOptions, Contract } from 'web3-eth-contract';
import { Contract } from 'web3-eth-contract';

import { PublicResolverAbi } from './abi/ens/PublicResolver';
import { Registry } from './registry';
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-ens/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ 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, NonPayableCallOptions } from 'web3-eth-contract';
import { DataFormat, DEFAULT_RETURN_FORMAT, format, sha3Raw } from 'web3-utils';
import { Contract } from 'web3-eth-contract';
import { format, sha3Raw } from 'web3-utils';
import { isHexStrict } from 'web3-validator';
import { Address } from 'web3-types';
import { Address, DataFormat, DEFAULT_RETURN_FORMAT, NonPayableCallOptions } from 'web3-types';
import { Web3ContextObject } from 'web3-core';
import { ENSRegistryAbi } from './abi/ens/ENSRegistry';
import { PublicResolverAbi } from './abi/ens/PublicResolver';
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ 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 { sha3, isNullish, format, DEFAULT_RETURN_FORMAT, DataFormat } from 'web3-utils';
import { Contract, NonPayableCallOptions } from 'web3-eth-contract';
import { sha3, isNullish, format } from 'web3-utils';
import { Contract } from 'web3-eth-contract';
import { ResolverMethodMissingError } from 'web3-errors';
import { isHexStrict } from 'web3-validator';
import { Address } from 'web3-types';
import { Address, DataFormat, DEFAULT_RETURN_FORMAT, NonPayableCallOptions } from 'web3-types';
import { interfaceIds, methodsInInterface } from './config';
import { Registry } from './registry';
import { PublicResolverAbi } from './abi/ens/PublicResolver';
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-eth-ens/test/integration/ens.events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

/* eslint-disable @typescript-eslint/no-unused-vars */
import { Contract, PayableTxOptions } from 'web3-eth-contract';
import { sha3, DEFAULT_RETURN_FORMAT } from 'web3-utils';
import { sha3 } from 'web3-utils';
import { getBlock } from 'web3-eth';

import { Address, Bytes } from 'web3-types';
import { Address, Bytes, DEFAULT_RETURN_FORMAT } from 'web3-types';
// eslint-disable-next-line import/no-extraneous-dependencies
import IpcProvider from 'web3-providers-ipc';
import { ENS } from '../../src';
Expand Down
Loading

0 comments on commit a9edc91

Please sign in to comment.