Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 80a4d7f

Browse files
authored
Init TransactionWithFromLocalWalletIndex, TransactionWithToLocalWalle… (#5748)
* Init TransactionWithFromLocalWalletIndex, TransactionWithToLocalWalletIndex, TransactionWithToAndFromLocalWalletIndex types * Add null to Transaction.to * Rename TransactionWithToAndFromLocalWalletIndex to TransactionWithFromAndToLocalWalletIndex
1 parent 1e2178a commit 80a4d7f

5 files changed

Lines changed: 49 additions & 19 deletions

File tree

packages/web3-eth/src/rpc_method_wrappers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ import {
3838
TransactionReceipt,
3939
Transaction,
4040
TransactionCall,
41-
TransactionWithLocalWalletIndex,
4241
Web3EthExecutionAPI,
42+
TransactionWithFromLocalWalletIndex,
43+
TransactionWithToLocalWalletIndex,
44+
TransactionWithFromAndToLocalWalletIndex,
4345
} from 'web3-types';
4446
import { Web3Context, Web3PromiEvent } from 'web3-core';
4547
import { ETH_DATA_FORMAT, FormatType, DataFormat, DEFAULT_RETURN_FORMAT, format } from 'web3-utils';
@@ -940,7 +942,7 @@ export async function getTransactionCount<ReturnFormat extends DataFormat>(
940942

941943
/**
942944
* @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
943-
* @param transaction The {@link Transaction} or {@link TransactionWithLocalWalletIndex} to send.
945+
* @param transaction The {@link Transaction}, {@link TransactionWithFromLocalWalletIndex}, {@link TransactionWithToLocalWalletIndex}, or {@link TransactionWithFromAndToLocalWalletIndex} to send.
944946
* @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted.
945947
* @param options A configuration object used to change the behavior of the `sendTransaction` method.
946948
* @returns If `await`ed or `.then`d (i.e. the promise resolves), the transaction hash is returned.
@@ -1050,7 +1052,11 @@ export function sendTransaction<
10501052
ResolveType = FormatType<TransactionReceipt, ReturnFormat>,
10511053
>(
10521054
web3Context: Web3Context<EthExecutionAPI>,
1053-
transaction: Transaction | TransactionWithLocalWalletIndex,
1055+
transaction:
1056+
| Transaction
1057+
| TransactionWithFromLocalWalletIndex
1058+
| TransactionWithToLocalWalletIndex
1059+
| TransactionWithFromAndToLocalWalletIndex,
10541060
returnFormat: ReturnFormat,
10551061
options?: SendTransactionOptions<ResolveType>,
10561062
): Web3PromiEvent<ResolveType, SendTransactionEvents<ReturnFormat>> {

packages/web3-eth/src/utils/transaction_builder.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import {
3030
ValidChains,
3131
Hardfork,
3232
Transaction,
33-
TransactionWithLocalWalletIndex,
33+
TransactionWithFromLocalWalletIndex,
34+
TransactionWithToLocalWalletIndex,
35+
TransactionWithFromAndToLocalWalletIndex,
3436
Common,
3537
Web3NetAPI,
3638
Numbers,
@@ -60,10 +62,14 @@ const isFromAttr = (attr: 'from' | 'to'): attr is 'from' => attr === 'from';
6062
const getTransactionFromOrToAttr = (
6163
attr: 'from' | 'to',
6264
web3Context: Web3Context<EthExecutionAPI>,
63-
transaction?: Transaction | TransactionWithLocalWalletIndex,
65+
transaction?:
66+
| Transaction
67+
| TransactionWithFromLocalWalletIndex
68+
| TransactionWithToLocalWalletIndex
69+
| TransactionWithFromAndToLocalWalletIndex,
6470
privateKey?: HexString | Buffer,
6571
): Address | undefined => {
66-
if (transaction?.[attr] !== undefined) {
72+
if (transaction !== undefined && attr in transaction && transaction[attr] !== undefined) {
6773
if (typeof transaction[attr] === 'string' && isAddress(transaction[attr] as string)) {
6874
return transaction[attr] as Address;
6975
}

packages/web3-eth/src/web3_eth.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ import {
3030
LogsOutput,
3131
Transaction,
3232
TransactionCall,
33-
TransactionWithLocalWalletIndex,
3433
Web3EthExecutionAPI,
34+
TransactionWithFromLocalWalletIndex,
35+
TransactionWithToLocalWalletIndex,
36+
TransactionWithFromAndToLocalWalletIndex,
3537
} from 'web3-types';
3638
import { isSupportedProvider, Web3Context, Web3ContextInitOptions } from 'web3-core';
3739
import { TransactionNotFound } from 'web3-errors';
@@ -822,7 +824,7 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
822824
}
823825

824826
/**
825-
* @param transaction The {@link Transaction} or {@link TransactionWithLocalWalletIndex} to send.
827+
* @param transaction The {@link Transaction}, {@link TransactionWithFromLocalWalletIndex}, {@link TransactionWithToLocalWalletIndex} or {@link TransactionWithFromAndToLocalWalletIndex} to send.
826828
* @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted.
827829
* @param options A configuration object used to change the behavior of the `sendTransaction` method.
828830
* @returns If `await`ed or `.then`d (i.e. the promise resolves), the transaction hash is returned.
@@ -928,7 +930,11 @@ export class Web3Eth extends Web3Context<Web3EthExecutionAPI, RegisteredSubscrip
928930
* ```
929931
*/
930932
public sendTransaction<ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
931-
transaction: Transaction | TransactionWithLocalWalletIndex,
933+
transaction:
934+
| Transaction
935+
| TransactionWithFromLocalWalletIndex
936+
| TransactionWithToLocalWalletIndex
937+
| TransactionWithFromAndToLocalWalletIndex,
932938
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
933939
options?: SendTransactionOptions,
934940
) {

packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { Transaction, TransactionWithLocalWalletIndex } from 'web3-types';
18+
import {
19+
Transaction,
20+
TransactionWithFromLocalWalletIndex,
21+
TransactionWithToLocalWalletIndex,
22+
TransactionWithFromAndToLocalWalletIndex,
23+
} from 'web3-types';
1924
import { Wallet } from 'web3-eth-accounts';
2025
import { isHexStrict } from 'web3-validator';
2126

@@ -63,7 +68,7 @@ describe('Web3Eth.sendTransaction', () => {
6368

6469
web3EthWithWallet.wallet?.add(tempAcc.privateKey);
6570

66-
const transaction: TransactionWithLocalWalletIndex = {
71+
const transaction: TransactionWithFromLocalWalletIndex = {
6772
from: 0,
6873
to: '0x0000000000000000000000000000000000000000',
6974
gas: 21000,
@@ -93,7 +98,7 @@ describe('Web3Eth.sendTransaction', () => {
9398

9499
web3EthWithWallet.wallet?.add(tempAcc.privateKey);
95100

96-
const transaction: TransactionWithLocalWalletIndex = {
101+
const transaction: TransactionWithToLocalWalletIndex = {
97102
from: tempAcc.address,
98103
to: 0,
99104
gas: 21000,
@@ -127,7 +132,7 @@ describe('Web3Eth.sendTransaction', () => {
127132

128133
web3EthWithWallet.wallet?.add(tempAcc2.privateKey);
129134

130-
const transaction: TransactionWithLocalWalletIndex = {
135+
const transaction: TransactionWithFromAndToLocalWalletIndex = {
131136
from: 0,
132137
to: 1,
133138
gas: 21000,

packages/web3-types/src/eth_types.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ interface TransactionBase {
338338
value?: Numbers;
339339
accessList?: AccessList;
340340
common?: Common;
341-
// eslint-disable-next-line @typescript-eslint/ban-types
342-
to?: Address | null;
343341
gas?: Numbers;
344342
gasPrice?: Numbers;
345343
type?: Numbers;
@@ -361,16 +359,25 @@ interface TransactionBase {
361359

362360
export interface Transaction extends TransactionBase {
363361
from?: Address;
362+
// eslint-disable-next-line @typescript-eslint/ban-types
363+
to?: Address | null;
364364
}
365365

366366
export interface TransactionCall extends Transaction {
367367
to: Address;
368368
}
369369

370-
export interface TransactionWithLocalWalletIndex extends Omit<TransactionBase, 'to'> {
371-
from?: Numbers;
372-
// eslint-disable-next-line @typescript-eslint/ban-types
373-
to?: Address | Numbers | null;
370+
export interface TransactionWithFromLocalWalletIndex extends Omit<Transaction, 'from'> {
371+
from: Numbers;
372+
}
373+
374+
export interface TransactionWithToLocalWalletIndex extends Omit<Transaction, 'to'> {
375+
to: Numbers;
376+
}
377+
378+
export interface TransactionWithFromAndToLocalWalletIndex extends Omit<Transaction, 'from' | 'to'> {
379+
from: Numbers;
380+
to: Numbers;
374381
}
375382

376383
export interface TransactionInfo extends Transaction {

0 commit comments

Comments
 (0)