Skip to content

Commit

Permalink
make default transaction 0x2 (#6426)
Browse files Browse the repository at this point in the history
* make default transaction 0x2

* fix tests

* fix tests

* fix lint

* update

* fix linting

* add more test cases

* fix tests

* fix tests

* update test

* address feedback

* remove eslint comments

* address feedback

* update changelog wording
  • Loading branch information
Alex authored Sep 23, 2023
1 parent d036166 commit 80adabe
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 122 deletions.
6 changes: 5 additions & 1 deletion packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@ Documentation:

- Added to `Web3Config` property `contractDataInputFill` allowing users to have the choice using property `data`, `input` or `both` for contract methods to be sent to the RPC provider when creating contracts. (#6377) (#6400)

## [Unreleased]
## [Unreleased]

### Changed

- defaultTransactionType is now type 0x2 instead of 0x0 (#6282)
4 changes: 3 additions & 1 deletion packages/web3-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { HexString, Transaction } from 'web3-types';

export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined;
export type TransactionTypeParser = (
transaction: Transaction,
) => HexString | undefined;

export interface Method {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export abstract class Web3Config
defaultHardfork: 'london',
// TODO - Check if there is a default Common
defaultCommon: undefined,
defaultTransactionType: '0x0',
defaultTransactionType: '0x2',
defaultMaxPriorityFeePerGas: toHex(2500000000),
enableExperimentalFeatures: {
useSubscriptionWhenCheckingBlockTimeout: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Object {
"defaultHardfork": "london",
"defaultMaxPriorityFeePerGas": "0x9502f900",
"defaultNetworkId": undefined,
"defaultTransactionType": "0x0",
"defaultTransactionType": "0x2",
"enableExperimentalFeatures": Object {
"useRpcCallSpecification": false,
"useSubscriptionWhenCheckingBlockTimeout": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/test/unit/web3_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const defaultConfig = {
transactionReceiptPollingInterval: undefined,
transactionSendTimeout: 750 * 1000,
transactionConfirmationPollingInterval: undefined,
defaultTransactionType: '0x0',
defaultTransactionType: '0x2',
defaultMaxPriorityFeePerGas: toHex(2500000000),
};
const setValue = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('contract', () => {
status: BigInt(0),
to: contractDeployed.options.address?.toLowerCase(),
transactionIndex: BigInt(0),
type: BigInt(0),
type: BigInt(2),
},
});
});
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Documentation:

## [Unreleased]

### Changed

- Transactions will now default to type 2 transactions instead of type 0, similar to 1.x version. (#6282)

### Fixed

- Ensure provider.supportsSubscriptions exists before watching by subscription (#6440)
Expand Down
43 changes: 18 additions & 25 deletions packages/web3-eth/src/utils/detect_transaction_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ const validateTxTypeAndHandleErrors = (
}
};

export const defaultTransactionTypeParser: TransactionTypeParser = transaction => {
export const defaultTransactionTypeParser: TransactionTypeParser = (
transaction
) => {
const tx = transaction as unknown as Transaction;

if (!isNullish(tx.type)) {
let txSchema;
switch (tx.type) {
Expand Down Expand Up @@ -110,34 +111,26 @@ export const defaultTransactionTypeParser: TransactionTypeParser = transaction =
return '0x1';
}

// We don't return 0x0 here, because if gasPrice is not
// undefined, we still don't know if the network
// supports EIP-2718 (https://eips.ethereum.org/EIPS/eip-2718)
// and whether we should return undefined for legacy txs,
// or type 0x0 for legacy txs post EIP-2718
if (!isNullish(tx.gasPrice)) {
validateTxTypeAndHandleErrors(transactionType0x0Schema, tx, '0x0');
}

const givenHardfork = tx.hardfork ?? tx.common?.hardfork;
// If we don't have a hardfork, then we can't be sure we're post
// EIP-2718 where transaction types are available
if (givenHardfork === undefined) return undefined;

const hardforkIndex = Object.keys(HardforksOrdered).indexOf(givenHardfork);
if (!isNullish(givenHardfork)) {
const hardforkIndex = Object.keys(HardforksOrdered).indexOf(givenHardfork);

// Unknown hardfork
if (hardforkIndex === undefined) return undefined;
// givenHardfork is London or later, so EIP-2718 is supported
if (hardforkIndex >= Object.keys(HardforksOrdered).indexOf('london'))
return !isNullish(tx.gasPrice) ? '0x0' : '0x2';

// givenHardfork is London or later, so EIP-2718 is supported
if (hardforkIndex >= Object.keys(HardforksOrdered).indexOf('london'))
return !isNullish(tx.gasPrice) ? '0x0' : '0x2';
// givenHardfork is Berlin, tx.accessList is undefined, assume type is 0x0
if (hardforkIndex === Object.keys(HardforksOrdered).indexOf('berlin')) return '0x0';
}

// givenHardfork is Berlin, tx.accessList is undefined, assume type is 0x0
if (hardforkIndex === Object.keys(HardforksOrdered).indexOf('berlin')) return '0x0';
// gasprice is defined
if (!isNullish(tx.gasPrice)) {
validateTxTypeAndHandleErrors(transactionType0x0Schema, tx, '0x0');
return '0x0';
}

// For all pre-Berlin hardforks, return undefined since EIP-2718
// isn't supported
// no transaction type can be inferred from properties, use default transaction type
return undefined;
};

Expand All @@ -146,7 +139,7 @@ export const detectTransactionType = (
web3Context?: Web3Context<EthExecutionAPI>,
) =>
(web3Context?.transactionTypeParser ?? defaultTransactionTypeParser)(
transaction as unknown as Record<string, unknown>,
transaction as unknown as Record<string, unknown>
);

export const detectRawTransactionType = (transaction: Uint8Array) =>
Expand Down
1 change: 0 additions & 1 deletion packages/web3-eth/src/utils/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export const getTransactionType = (
web3Context: Web3Context<EthExecutionAPI>,
) => {
const inferredType = detectTransactionType(transaction, web3Context);

if (!isNullish(inferredType)) return inferredType;
if (!isNullish(web3Context.defaultTransactionType))
return format({ format: 'uint' }, web3Context.defaultTransactionType, ETH_DATA_FORMAT);
Expand Down
32 changes: 19 additions & 13 deletions packages/web3-eth/test/fixtures/detect_transaction_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const transactionType0x0: FormatType<Transaction, typeof ETH_DATA_FORMAT>
chainId: '0x1',
gasLimit: '0x5208',
},
{
from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
gas: '0x5208',
gasPrice: '0x4a817c800',
data: '0x',
nonce: '0x4',
chainId: '0x1',
gasLimit: '0x5208',
},
{
to: '0x8f3e9c1Bd65EB267d19B176A73217524DC21A5ca',
nonce: '0x3B7',
Expand Down Expand Up @@ -199,17 +210,6 @@ export const transactionType0x2: FormatType<Transaction, typeof ETH_DATA_FORMAT>
];

export const transactionTypeUndefined: FormatType<Transaction, typeof ETH_DATA_FORMAT>[] = [
{
from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
gas: '0x5208',
gasPrice: '0x4a817c800',
data: '0x',
nonce: '0x4',
chainId: '0x1',
gasLimit: '0x5208',
},
{
from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
to: '0x3535353535353535353535353535353535353535',
Expand All @@ -225,7 +225,6 @@ export const transactionTypeUndefined: FormatType<Transaction, typeof ETH_DATA_F
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
gas: '0x5208',
gasPrice: '0x4a817c800',
data: '0x',
nonce: '0x4',
chainId: '0x1',
Expand All @@ -238,7 +237,6 @@ export const transactionTypeUndefined: FormatType<Transaction, typeof ETH_DATA_F
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
gas: '0x5208',
gasPrice: '0x4a817c800',
data: '0x',
nonce: '0x4',
chainId: '0x1',
Expand All @@ -252,6 +250,14 @@ export const transactionTypeUndefined: FormatType<Transaction, typeof ETH_DATA_F
hardfork: 'nonExistent',
},
},
{
from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
to: '0x3535353535353535353535353535353535353535',
value: '0x174876e800',
data: '0x0',
nonce: '0x4',
chainId: '0x1',
},
];

export const transactionTypeValidationError: FormatType<Transaction, typeof ETH_DATA_FORMAT>[] = [
Expand Down
8 changes: 6 additions & 2 deletions packages/web3-eth/test/integration/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,11 @@ describe('defaults', () => {
provider: web3Eth.provider,
config: {
defaultHardfork: 'istanbul',
defaultTransactionType: '0x0',
},
});
expect(eth2.defaultHardfork).toBe('istanbul');
expect(eth2.defaultTransactionType).toBe('0x0');

const res = await prepareTransactionForSigning(
{
Expand Down Expand Up @@ -759,9 +761,9 @@ describe('defaults', () => {
});
expect(eth2.defaultCommon).toBe(common);
});
it('defaultTransactionType', () => {
it('defaultTransactionType', async () => {
// default
expect(web3Eth.defaultTransactionType).toBe('0x0');
expect(web3Eth.defaultTransactionType).toBe('0x2');
// after set
web3Eth.setConfig({
defaultTransactionType: '0x3',
Expand All @@ -770,10 +772,12 @@ describe('defaults', () => {

// set by create new instance
eth2 = new Web3Eth({
provider: clientUrl,
config: {
defaultTransactionType: '0x4444',
},
});

expect(eth2.defaultTransactionType).toBe('0x4444');

const res = getTransactionType(
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/test/integration/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ExpectOptions = {
};
export const validateTransaction = (
tx: TransactionInfo,
expectOptions: ExpectOptions = { type: 0 },
expectOptions: ExpectOptions = { type: 2 },
) => {
expect(tx.nonce).toBeDefined();
expect(tx.hash).toMatch(regexHex32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('Web3Eth.sendTransaction', () => {
to: '0x0000000000000000000000000000000000000000',
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
value: BigInt(1),
type: BigInt(0),
};
const response = await web3Eth.sendTransaction(transaction, DEFAULT_RETURN_FORMAT);
expect(response.type).toBe(BigInt(0));
Expand All @@ -307,6 +308,36 @@ describe('Web3Eth.sendTransaction', () => {
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
});

it('should send a successful autodetected type 0x0 gasprice as data', async () => {
const transaction: Transaction = {
from: tempAcc.address,
to: '0x0000000000000000000000000000000000000000',
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
value: BigInt(1),
gasPrice: BigInt(2500000008),
};
const response = await web3Eth.sendTransaction(transaction, DEFAULT_RETURN_FORMAT);
expect(response.type).toBe(BigInt(0));
expect(response.status).toBe(BigInt(1));
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
});

it('should send a successful default type 0x2 transaction with data', async () => {
const transaction: Transaction = {
from: tempAcc.address,
to: '0x0000000000000000000000000000000000000000',
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
value: BigInt(1),
gas: BigInt(30000),
};
const response = await web3Eth.sendTransaction(transaction, DEFAULT_RETURN_FORMAT);
expect(response.type).toBe(BigInt(2));
expect(response.status).toBe(BigInt(1));
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
expect(minedTransactionData).toMatchObject(transaction);
});
});
it('should autofill a successful type 0x2 transaction with only maxFeePerGas passed', async () => {
const transaction: Transaction = {
Expand Down Expand Up @@ -392,7 +423,7 @@ describe('Web3Eth.sendTransaction', () => {
expect(typeof data.gasUsed).toBe('bigint');
expect(typeof data.transactionIndex).toBe('bigint');
expect(data.status).toBe(BigInt(1));
expect(data.type).toBe(BigInt(0));
expect(data.type).toBe(BigInt(2));
expect(data.events).toBeUndefined();
});
expect.assertions(9);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { Web3Context } from 'web3-core';
import HttpProvider from 'web3-providers-http';
import { isNullish } from 'web3-validator';
import { ethRpcMethods } from 'web3-rpc-methods';

import {
Eip1559NotSupportedError,
TransactionDataAndInputError,
Expand All @@ -36,7 +35,6 @@ import {
import { defaultTransactionBuilder } from '../../src/utils/transaction_builder';

jest.mock('web3-rpc-methods');

const expectedNetworkId = '0x4';
jest.mock('web3-net', () => ({
getId: jest.fn().mockImplementation(() => expectedNetworkId),
Expand Down
Loading

0 comments on commit 80adabe

Please sign in to comment.