Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions packages/web3-errors/src/error_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,39 @@ export const ERR_TX_OUT_OF_GAS = 406;
export const ERR_RAW_TX_UNDEFINED = 407;

export const ERR_TX_INVALID_SENDER = 408;
export const ERR_TX_INVALID_CALL = 409;
export const ERR_TX_MISSING_CUSTOM_CHAIN = 410;
export const ERR_TX_MISSING_CUSTOM_CHAIN_ID = 411;
export const ERR_TX_CHAIN_ID_MISMATCH = 412;
export const ERR_TX_INVALID_CHAIN_INFO = 413;
export const ERR_TX_MISSING_CHAIN_INFO = 414;
export const ERR_TX_MISSING_GAS = 415;
export const ERR_TX_INVALID_LEGACY_GAS = 416;
export const ERR_TX_INVALID_FEE_MARKET_GAS = 417;
export const ERR_TX_INVALID_FEE_MARKET_GAS_PRICE = 418;
export const ERR_TX_INVALID_LEGACY_FEE_MARKET = 419;
export const ERR_TX_INVALID_OBJECT = 420;
export const ERR_TX_INVALID_NONCE_OR_CHAIN_ID = 421;
export const ERR_TX_UNABLE_TO_POPULATE_NONCE = 422;
export const ERR_TX_UNSUPPORTED_EIP_1559 = 423;
export const ERR_TX_UNSUPPORTED_TYPE = 424;
export const ERR_TX_DATA_AND_INPUT = 425;
export const ERR_TX_POLLING_TIMEOUT = 426;
export const ERR_TX_RECEIPT_MISSING_OR_BLOCKHASH_NULL = 427;
export const ERR_TX_RECEIPT_MISSING_BLOCK_NUMBER = 428;

export const ERR_TX_LOCAL_WALLET_NOT_AVAILABLE = 429;

export const ERR_TX_NOT_FOUND = 430;
export const ERR_TX_SEND_TIMEOUT = 431;
export const ERR_TX_BLOCK_TIMEOUT = 432;

export const ERR_TX_SIGNING = 433;
export const ERR_TX_GAS_MISMATCH = 434;

export const ERR_TX_CHAIN_MISMATCH = 435;
export const ERR_TX_HARDFORK_MISMATCH = 436;
export const ERR_TX_INVALID_RECEIVER = 409;
export const ERR_TX_INVALID_CALL = 410;
export const ERR_TX_MISSING_CUSTOM_CHAIN = 411;
export const ERR_TX_MISSING_CUSTOM_CHAIN_ID = 412;
export const ERR_TX_CHAIN_ID_MISMATCH = 413;
export const ERR_TX_INVALID_CHAIN_INFO = 414;
export const ERR_TX_MISSING_CHAIN_INFO = 415;
export const ERR_TX_MISSING_GAS = 416;
export const ERR_TX_INVALID_LEGACY_GAS = 417;
export const ERR_TX_INVALID_FEE_MARKET_GAS = 418;
export const ERR_TX_INVALID_FEE_MARKET_GAS_PRICE = 419;
export const ERR_TX_INVALID_LEGACY_FEE_MARKET = 420;
export const ERR_TX_INVALID_OBJECT = 421;
export const ERR_TX_INVALID_NONCE_OR_CHAIN_ID = 422;
export const ERR_TX_UNABLE_TO_POPULATE_NONCE = 423;
export const ERR_TX_UNSUPPORTED_EIP_1559 = 424;
export const ERR_TX_UNSUPPORTED_TYPE = 425;
export const ERR_TX_DATA_AND_INPUT = 426;
export const ERR_TX_POLLING_TIMEOUT = 427;
export const ERR_TX_RECEIPT_MISSING_OR_BLOCKHASH_NULL = 428;
export const ERR_TX_RECEIPT_MISSING_BLOCK_NUMBER = 429;

export const ERR_TX_LOCAL_WALLET_NOT_AVAILABLE = 430;

export const ERR_TX_NOT_FOUND = 431;
export const ERR_TX_SEND_TIMEOUT = 432;
export const ERR_TX_BLOCK_TIMEOUT = 433;

export const ERR_TX_SIGNING = 434;
export const ERR_TX_GAS_MISMATCH = 435;

export const ERR_TX_CHAIN_MISMATCH = 436;
export const ERR_TX_HARDFORK_MISMATCH = 437;

// Connection error codes
export const ERR_CONN = 500;
Expand Down
7 changes: 7 additions & 0 deletions packages/web3-errors/src/errors/transaction_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
ERR_TX_INVALID_NONCE_OR_CHAIN_ID,
ERR_TX_INVALID_OBJECT,
ERR_TX_INVALID_SENDER,
ERR_TX_INVALID_RECEIVER,
ERR_TX_LOCAL_WALLET_NOT_AVAILABLE,
ERR_TX_MISSING_CHAIN_INFO,
ERR_TX_MISSING_CUSTOM_CHAIN,
Expand Down Expand Up @@ -168,7 +169,13 @@ export class InvalidTransactionWithSender extends InvalidValueError {
super(value, 'invalid transaction with sender');
}
}
export class InvalidTransactionWithReceiver extends InvalidValueError {
public code = ERR_TX_INVALID_RECEIVER;

public constructor(value: unknown) {
super(value, 'invalid transaction with receiver');
}
}
export class InvalidTransactionCall extends InvalidValueError {
public code = ERR_TX_INVALID_CALL;

Expand Down
3 changes: 2 additions & 1 deletion packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
SendTransactionOptions,
} from './types';
// eslint-disable-next-line import/no-cycle
import { getTransactionFromAttr } from './utils/transaction_builder';
import { getTransactionFromAttr, getTransactionToAttr } from './utils/transaction_builder';
import { formatTransaction } from './utils/format_transaction';
// eslint-disable-next-line import/no-cycle
import { getTransactionGasPricing } from './utils/get_transaction_gas_pricing';
Expand Down Expand Up @@ -1063,6 +1063,7 @@ export function sendTransaction<
{
...transaction,
from: getTransactionFromAttr(web3Context, transaction),
to: getTransactionToAttr(web3Context, transaction),
},
ETH_DATA_FORMAT,
);
Expand Down
34 changes: 24 additions & 10 deletions packages/web3-eth/src/utils/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ import {
TransactionWithLocalWalletIndex,
Common,
Web3NetAPI,
Numbers,
} from 'web3-types';
import { Web3Context } from 'web3-core';
import { privateKeyToAddress } from 'web3-eth-accounts';
import { getId } from 'web3-net';
import { isNullish, isNumber } from 'web3-validator';
import {
InvalidTransactionWithSender,
InvalidTransactionWithReceiver,
LocalWalletNotAvailableError,
TransactionDataAndInputError,
UnableToPopulateNonceError,
Expand All @@ -53,19 +55,22 @@ import { getTransactionGasPricing } from './get_transaction_gas_pricing';
import { transactionSchema } from '../schemas';
import { InternalTransaction } from '../types';

export const getTransactionFromAttr = (
const isFromAttr = (attr: 'from' | 'to'): attr is 'from' => attr === 'from';

const getTransactionFromOrToAttr = (
attr: 'from' | 'to',
web3Context: Web3Context<EthExecutionAPI>,
transaction?: Transaction | TransactionWithLocalWalletIndex,
privateKey?: HexString | Buffer,
) => {
if (transaction?.from !== undefined) {
if (typeof transaction.from === 'string' && isAddress(transaction.from)) {
return transaction.from;
): Address | undefined => {
if (transaction?.[attr] !== undefined) {
if (typeof transaction[attr] === 'string' && isAddress(transaction[attr] as string)) {
return transaction[attr] as Address;
}
if (isNumber(transaction.from)) {
if (isNumber(transaction[attr] as Numbers)) {
if (web3Context.wallet) {
const account = web3Context.wallet.get(
format({ eth: 'uint' }, transaction.from, NUMBER_DATA_FORMAT),
format({ eth: 'uint' }, transaction[attr] as Numbers, NUMBER_DATA_FORMAT),
);

if (!isNullish(account)) {
Expand All @@ -76,15 +81,24 @@ export const getTransactionFromAttr = (
}
throw new LocalWalletNotAvailableError();
} else {
throw new InvalidTransactionWithSender(transaction.from);
throw isFromAttr(attr)
? new InvalidTransactionWithSender(transaction.from)
: // eslint-disable-next-line @typescript-eslint/no-unsafe-call
new InvalidTransactionWithReceiver(transaction.to);
}
}
if (!isNullish(privateKey)) return privateKeyToAddress(privateKey);
if (!isNullish(web3Context.defaultAccount)) return web3Context.defaultAccount;
if (isFromAttr(attr)) {
if (!isNullish(privateKey)) return privateKeyToAddress(privateKey);
if (!isNullish(web3Context.defaultAccount)) return web3Context.defaultAccount;
}

return undefined;
};

export const getTransactionFromAttr = getTransactionFromOrToAttr.bind(undefined, 'from');

export const getTransactionToAttr = getTransactionFromOrToAttr.bind(undefined, 'to');

export const getTransactionNonce = async <ReturnFormat extends DataFormat>(
web3Context: Web3Context<EthExecutionAPI>,
address?: Address,
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-types/src/eth_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ export interface TransactionCall extends Transaction {
to: Address;
}

export interface TransactionWithLocalWalletIndex extends TransactionBase {
export interface TransactionWithLocalWalletIndex extends Omit<TransactionBase, 'to'> {
from?: Numbers;
// eslint-disable-next-line @typescript-eslint/ban-types
to?: Address | Numbers | null;
Copy link
Contributor

Choose a reason for hiding this comment

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

to being Address | null is already handled by the type TransactionBase, so it should just be listed as Numbers here. The problem is from could be the local wallet index while to is Address | null, so I see why you did this, but it doesn't work for the inverse where to is an index but from is Address | null

I'm not sure how else to this besides introducing the following types: TransactionWithFromLocalWalletIndex, TransactionWithToLocalWalletIndex, and TransactionWithFromAndToLocalWalletIndex, so definitely open to suggestions. I've opened #5748 to implement this (doesn't update the CHANGELOGs, so that will need to be done if you decided to merge it)

}

export interface TransactionInfo extends Transaction {
Expand Down