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

Commit 0079ef4

Browse files
defaultTransactionBuilder (chainId and networkId) - #4800 (#4842)
* Move Web3Net to web3_net.ts * Refactor index.ts exports * Add web3-net dependency to web3-eth * Add chainId and networkId to defaultTransactionBuilder * yarn format * Add rpcMethods export to web3-net * Add tests for populating chainId and networkId * Update prepareTransactionForSigning tests to account for added networkId population
1 parent f71d2fe commit 0079ef4

File tree

7 files changed

+80
-41
lines changed

7 files changed

+80
-41
lines changed

packages/web3-eth/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"web3-core": "4.0.0-alpha.0",
4747
"web3-eth-accounts": "4.0.0-alpha.0",
4848
"web3-utils": "4.0.0-alpha.1",
49-
"web3-validator": "^0.1.0-alpha.0"
49+
"web3-validator": "^0.1.0-alpha.0",
50+
"web3-net": "4.0.0-alpha.0"
5051
}
5152
}

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { EthExecutionAPI } from 'web3-common';
22
import { Web3Context } from 'web3-core';
33
import { privateKeyToAddress } from 'web3-eth-accounts';
44
import { Address, convertToValidType, HexString, ValidTypes } from 'web3-utils';
5+
import { getId, Web3NetAPI } from 'web3-net';
6+
57
import { TransactionDataAndInputError, UnableToPopulateNonceError } from '../errors';
68
// eslint-disable-next-line import/no-cycle
7-
import { getTransactionCount } from '../rpc_method_wrappers';
9+
import { getChainId, getTransactionCount } from '../rpc_method_wrappers';
810
import { chain, hardfork, Transaction } from '../types';
911
import { detectTransactionType } from './detect_transaction_type';
1012
// eslint-disable-next-line import/no-cycle
@@ -55,7 +57,7 @@ export const getTransactionType = (
5557
// as some of the properties are dependent on others
5658
export async function defaultTransactionBuilder<ReturnType = Record<string, unknown>>(options: {
5759
transaction: Record<string, unknown>;
58-
web3Context: Web3Context<EthExecutionAPI>;
60+
web3Context: Web3Context<EthExecutionAPI & Web3NetAPI>;
5961
privateKey?: HexString | Buffer;
6062
}): Promise<ReturnType> {
6163
let populatedTransaction = { ...options.transaction } as unknown as Transaction;
@@ -97,15 +99,19 @@ export async function defaultTransactionBuilder<ReturnType = Record<string, unkn
9799
populatedTransaction.hardfork = options.web3Context.defaultHardfork as hardfork;
98100
}
99101

100-
// if (populatedTransaction.chainId === undefined && populatedTransaction.common?.customChain.chainId === undefined)
101-
// TODO - web3Eth.getChainId not implemented
102-
// populatedTransaction.chainId = await web3Eth.getChainId();
102+
if (
103+
populatedTransaction.chainId === undefined &&
104+
populatedTransaction.common?.customChain.chainId === undefined
105+
)
106+
populatedTransaction.chainId = await getChainId(
107+
options.web3Context,
108+
options.web3Context.defaultReturnType,
109+
);
103110

104-
// if (populatedTransaction.networkId === undefined) {
105-
// populatedTransaction.networkId = web3Context.defaultNetworkId ?? undefined;
106-
// TODO - getNetworkId (net_version) not implemented
107-
// populatedTransaction.networkId = await getNetworkId();
108-
// }
111+
if (populatedTransaction.networkId === undefined)
112+
populatedTransaction.networkId =
113+
options.web3Context.defaultNetworkId ??
114+
(await getId(options.web3Context, options.web3Context.defaultReturnType));
109115

110116
if (populatedTransaction.gasLimit === undefined && populatedTransaction.gas !== undefined)
111117
populatedTransaction.gasLimit = populatedTransaction.gas;

packages/web3-eth/test/unit/default_transaction_builder.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import {
1616

1717
jest.mock('../../src/rpc_methods');
1818

19+
const expectedNetworkId = '0x4';
20+
jest.mock('web3-net', () => ({
21+
getId: jest.fn().mockImplementation(() => expectedNetworkId),
22+
}));
23+
1924
describe('defaultTransactionBuilder', () => {
2025
const expectedFrom = '0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01';
2126
const expectedNonce = '0x42';
@@ -25,6 +30,7 @@ describe('defaultTransactionBuilder', () => {
2530
const expectedBaseFeePerGas = '0x13afe8b904';
2631
const expectedMaxPriorityFeePerGas = '0x9502f900';
2732
const expectedMaxFeePerGas = '0x27f4d46b08';
33+
const expectedChainId = '0x1';
2834
const defaultTransactionType = '0x0';
2935
const transaction: Transaction = {
3036
from: expectedFrom,
@@ -40,12 +46,13 @@ describe('defaultTransactionBuilder', () => {
4046
nonce: expectedNonce,
4147
chain: 'mainnet',
4248
hardfork: 'berlin',
43-
chainId: '0x1',
49+
chainId: expectedChainId,
50+
networkId: expectedNetworkId,
4451
common: {
4552
customChain: {
4653
name: 'foo',
47-
networkId: '0x4',
48-
chainId: '0x42',
54+
networkId: expectedNetworkId,
55+
chainId: expectedChainId,
4956
},
5057
baseChain: 'mainnet',
5158
hardfork: 'berlin',
@@ -98,6 +105,9 @@ describe('defaultTransactionBuilder', () => {
98105
// @ts-expect-error - Mocked implementation doesn't have correct method signature
99106
// (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test
100107
jest.spyOn(rpcMethods, 'getGasPrice').mockImplementation(() => expectedGasPrice);
108+
// @ts-expect-error - Mocked implementation doesn't have correct method signature
109+
// (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test
110+
jest.spyOn(rpcMethods, 'getChainId').mockImplementation(() => expectedChainId);
101111

102112
web3Context = new Web3Context<EthExecutionAPI>(new HttpProvider('http://127.0.0.1'));
103113
});
@@ -258,16 +268,29 @@ describe('defaultTransactionBuilder', () => {
258268
});
259269

260270
describe('should populate chainId', () => {
261-
// TODO - web3Eth.getChainId not implemented
262-
it.skip('should populate with web3Eth.getChainId', async () => {
271+
it('should populate with web3Eth.getChainId', async () => {
263272
const input = { ...transaction };
264273
delete input.chainId;
274+
delete input.common;
275+
276+
const result = await defaultTransactionBuilder({
277+
transaction: input,
278+
web3Context,
279+
});
280+
expect(result.chainId).toBe(expectedChainId);
281+
});
282+
});
283+
284+
describe('should populate networkId', () => {
285+
it('should populate with web3Net.getId', async () => {
286+
const input = { ...transaction };
287+
delete input.networkId;
265288

266289
const result = await defaultTransactionBuilder({
267290
transaction: input,
268291
web3Context,
269292
});
270-
expect(result.chainId).toBe('0x1');
293+
expect(result.networkId).toBe(expectedNetworkId);
271294
});
272295
});
273296

packages/web3-eth/test/unit/prepare_transaction_for_signing.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { prepareTransactionForSigning } from '../../src/utils/prepare_transactio
1212
import { validTransactions } from '../fixtures/prepare_transaction_for_signing';
1313

1414
describe('prepareTransactionForSigning', () => {
15-
const web3Context = new Web3Context<EthExecutionAPI>(new HttpProvider('http://127.0.0.1'));
15+
const web3Context = new Web3Context<EthExecutionAPI>({
16+
provider: new HttpProvider('http://127.0.0.1'),
17+
config: { defaultNetworkId: '0x1' },
18+
});
1619

1720
describe('should return an @ethereumjs/tx instance with expected properties', () => {
1821
it.each(validTransactions)(

packages/web3-net/src/index.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
import { Web3Context } from 'web3-core';
2-
import { ValidTypes } from 'web3-utils';
3-
import * as rpcMethodsWrappers from './rpc_method_wrappers';
4-
import { Web3NetAPI } from './web3_net_api';
1+
import { Web3Net } from './web3_net';
52

6-
export class Web3Net extends Web3Context<Web3NetAPI> {
7-
public async getId<ReturnType extends ValidTypes = ValidTypes.HexString>(
8-
returnType?: ReturnType,
9-
) {
10-
return rpcMethodsWrappers.getId(this, returnType);
11-
}
12-
13-
public async getPeerCount<ReturnType extends ValidTypes = ValidTypes.HexString>(
14-
returnType?: ReturnType,
15-
) {
16-
return rpcMethodsWrappers.getPeerCount(this, returnType);
17-
}
18-
19-
public async isListening() {
20-
return rpcMethodsWrappers.isListening(this);
21-
}
22-
}
23-
export { Web3NetAPI };
3+
export * from './web3_net';
4+
export * as rpcMethods from './rpc_methods';
245
export * from './rpc_method_wrappers';
6+
export * from './web3_net_api';
7+
8+
export default Web3Net;

packages/web3-net/src/web3_net.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Web3Context } from 'web3-core';
2+
3+
import { ValidTypes } from 'web3-utils';
4+
import * as rpcMethodsWrappers from './rpc_method_wrappers';
5+
import { Web3NetAPI } from './web3_net_api';
6+
7+
export class Web3Net extends Web3Context<Web3NetAPI> {
8+
public async getId<ReturnType extends ValidTypes = ValidTypes.HexString>(
9+
returnType?: ReturnType,
10+
) {
11+
return rpcMethodsWrappers.getId(this, returnType);
12+
}
13+
14+
public async getPeerCount<ReturnType extends ValidTypes = ValidTypes.HexString>(
15+
returnType?: ReturnType,
16+
) {
17+
return rpcMethodsWrappers.getPeerCount(this, returnType);
18+
}
19+
20+
public async isListening() {
21+
return rpcMethodsWrappers.isListening(this);
22+
}
23+
}

packages/web3-utils/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,3 @@ export interface Filter {
125125
readonly address?: Address | Address[];
126126
readonly topics?: (Topic | Topic[] | null)[];
127127
}
128-

0 commit comments

Comments
 (0)