Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction input and data refactors #6294

Merged
merged 15 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 1 addition & 17 deletions packages/web3-eth/src/utils/format_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import { Transaction, DataFormat, DEFAULT_RETURN_FORMAT, FormatType } from 'web3-types';
import { isNullish, ValidationSchemaInput } from 'web3-validator';
import { TransactionDataAndInputError } from 'web3-errors';
import { mergeDeep, bytesToHex, format } from 'web3-utils';
import { mergeDeep, format } from 'web3-utils';
import { transactionSchema } from '../schemas.js';

export function formatTransaction<
Expand All @@ -40,21 +39,6 @@ export function formatTransaction<

formattedTransaction = format(options.transactionSchema, formattedTransaction, returnFormat);

if (!isNullish(formattedTransaction.data)) {
if (
!isNullish(formattedTransaction.input) &&
formattedTransaction.data !== formattedTransaction.input
)
throw new TransactionDataAndInputError({
data: bytesToHex(formattedTransaction.data),
input: bytesToHex(formattedTransaction.input),
});

formattedTransaction.input = formattedTransaction.data;
} else if (!isNullish(formattedTransaction.input)) {
formattedTransaction.data = formattedTransaction.input;
}

if (!isNullish(formattedTransaction.gasLimit)) {
formattedTransaction.gas = formattedTransaction.gasLimit;
delete formattedTransaction.gasLimit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const getEthereumjsTxDataFromTransaction = (
gasLimit: transaction.gasLimit ?? transaction.gas,
to: transaction.to,
value: transaction.value,
data: transaction.input,
data: transaction.data ?? transaction.input,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The property name is still data because that's what TransactionFactory.fromTxData is expecting

type: transaction.type,
chainId: transaction.chainId,
accessList: (
Expand Down
5 changes: 0 additions & 5 deletions packages/web3-eth/src/utils/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,11 @@ export async function defaultTransactionBuilder<ReturnType = Transaction>(option

if (!populatedTransaction.data.startsWith('0x'))
populatedTransaction.data = `0x${populatedTransaction.data}`;

populatedTransaction.input = populatedTransaction.data;
} else if (!isNullish(populatedTransaction.input)) {
if (!populatedTransaction.input.startsWith('0x'))
populatedTransaction.input = `0x${populatedTransaction.input}`;

populatedTransaction.data = populatedTransaction.input;
} else {
populatedTransaction.input = '0x';
populatedTransaction.data = '0x';
}

if (isNullish(populatedTransaction.common)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Web3Eth } from '../../../src';
import {
closeOpenConnection,
createTempAccount,
getSystemTestBackend,
getSystemTestProvider,
} from '../../fixtures/system_test_utils';

Expand Down Expand Up @@ -47,17 +48,21 @@ describe('Web3Eth.signTransaction', () => {
gasPrice: '0x3b9aca01',
};
const response = await web3Eth.signTransaction(transaction);
expect(response).toMatchObject({
const expectedResponse: { tx: Transaction } = {
tx: {
type: BigInt(0),
nonce: BigInt(nonce),
gasPrice: BigInt(1000000001),
gas: BigInt(21000),
value: BigInt(1),
to: transaction.to,
input: '0x',
},
});
};
if (getSystemTestBackend() === 'geth') expectedResponse.tx.input = '0x';
else if (getSystemTestBackend() === 'ganache') expectedResponse.tx.data = '0x';
Copy link
Contributor

Choose a reason for hiding this comment

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

This indicates a breaking change.

We cannot be sure that no user used to fill input and then read data or vice versa.


expect(response).toMatchObject(expectedResponse);

// Pulling out of toMatchObject to be compatiable with Cypress
expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/);
expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint');
Expand All @@ -77,16 +82,21 @@ describe('Web3Eth.signTransaction', () => {
gasPrice: '0x3b9aca01',
};
const response = await web3Eth.signTransaction(transaction);
// eslint-disable-next-line jest/no-standalone-expect
expect(response).toMatchObject({
const expectedResponse: { tx: Transaction } = {
tx: {
type: BigInt(0),
nonce: BigInt(nonce),
gasPrice: BigInt(1000000001),
gas: BigInt(475320),
input: greeterContractDeploymentData,
},
});
};
if (getSystemTestBackend() === 'geth')
expectedResponse.tx.input = greeterContractDeploymentData;
else if (getSystemTestBackend() === 'ganache')
expectedResponse.tx.data = greeterContractDeploymentData;

// eslint-disable-next-line jest/no-standalone-expect
expect(response).toMatchObject(expectedResponse);
// Pulling out of toMatchObject to be compatiable with Cypress
expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/);
expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint');
Expand Down
24 changes: 19 additions & 5 deletions packages/web3-eth/test/unit/default_transaction_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,8 @@ describe('defaultTransactionBuilder', () => {
});

describe('should populate input/data', () => {
it('should populate with 0x', async () => {
it('should populate input with 0x', async () => {
const input = { ...transaction };
delete input.input;
delete input.maxPriorityFeePerGas;
delete input.maxFeePerGas;
delete input.data;
Expand All @@ -259,22 +258,37 @@ describe('defaultTransactionBuilder', () => {
fillGasPrice: true,
});
expect(result.input).toBe('0x');
expect(result.data).toBe('0x');
expect(result.data).toBeUndefined();
});

it('should prefix with 0x', async () => {
it('should prefix input with 0x', async () => {
const input = { ...transaction };
input.input = '123';
delete input.maxPriorityFeePerGas;
delete input.maxFeePerGas;
input.data = '123';
delete input.data;

const result = await defaultTransactionBuilder({
transaction: input,
web3Context,
fillGasPrice: true,
});
expect(result.input).toBe('0x123');
expect(result.data).toBeUndefined();
});

it('should prefix data with 0x', async () => {
const input = { ...transaction };
delete input.maxPriorityFeePerGas;
delete input.maxFeePerGas;
input.data = '123';

const result = await defaultTransactionBuilder({
transaction: input,
web3Context,
fillGasPrice: true,
});
expect(result.input).toBeUndefined();
expect(result.data).toBe('0x123');
});

Expand Down
17 changes: 1 addition & 16 deletions packages/web3-eth/test/unit/format_transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ GNU Lesser General Public License for more details.
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, FMT_BYTES, FMT_NUMBER, Transaction } from 'web3-types';
import { TransactionDataAndInputError } from 'web3-errors';
import { DEFAULT_RETURN_FORMAT, FMT_BYTES, FMT_NUMBER } from 'web3-types';

import { formatTransaction } from '../../src/utils/format_transaction';
import {
Expand Down Expand Up @@ -94,18 +93,4 @@ describe('formatTransaction', () => {
}
}
});

it('Should throw a TransactionDataAndInputError error', () => {
const transaction: Transaction = {
data: '0x00',
input: '0x01',
};

expect(() => formatTransaction(transaction)).toThrow(
new TransactionDataAndInputError({
data: transaction.data as string,
input: transaction.input as string,
}),
);
});
});
3 changes: 2 additions & 1 deletion packages/web3-types/src/eth_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ export interface PopulatedUnsignedBaseTransaction {
gas?: Numbers;
gasPrice: Numbers;
type: Numbers;
input: Bytes;
input?: Bytes;
data?: Bytes;
Comment on lines +412 to +413
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a breaking change for eth_types, but input won't be apart of this object if the user uses data

nonce: Numbers;
networkId: Numbers;
chain: ValidChains;
Expand Down
Loading