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 all 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: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,21 @@ should use 4.0.1-alpha.0 for testing.
- Parameters decoding error for nested components (#5714)

## [Unreleased]

### Changed

#### web3-providers-ipc

- Refactor to use common SocketProvider class (#5683)

#### web3-providers-ws

- Refactor to use common SocketProvider class (#5683)

#### web3-utils

- Add SocketProvider class and Eip1193Provider abstract class (#5683)

#### web3-types

- These types were added: ProviderRpcError, EthSubscription, ProviderMessage, ProviderConnectInfo (#5683)
6 changes: 4 additions & 2 deletions packages/web3-eth-ens/test/integration/ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isWs,
isIpc,
closeOpenConnection,
isSocket,
} from '../fixtures/system_tests_utils';

import { ENSRegistryAbi } from '../../src/abi/ens/ENSRegistry';
Expand Down Expand Up @@ -132,9 +133,10 @@ describe('ens', () => {
});

afterAll(async () => {
if (isWs || isIpc) {
if (isSocket) {
await closeOpenConnection(ens);
await closeOpenConnection(ens['_registry']['contract']);
// @ts-expect-error @typescript-eslint/ban-ts-comment
await closeOpenConnection(ens?._registry?.contract);
await closeOpenConnection(getEnsResolver);
await closeOpenConnection(setEnsResolver);
await closeOpenConnection(registry);
Expand Down
6 changes: 4 additions & 2 deletions packages/web3-eth-ens/test/integration/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isWs,
isIpc,
closeOpenConnection,
isSocket,
} from '../fixtures/system_tests_utils';

import { ENSRegistryAbi } from '../../src/abi/ens/ENSRegistry';
Expand Down Expand Up @@ -130,9 +131,10 @@ describe('ens', () => {
});

afterAll(async () => {
if (isWs || isIpc) {
if (isSocket) {
await closeOpenConnection(ens);
await closeOpenConnection(ens['_registry']['contract']);
// @ts-expect-error @typescript-eslint/ban-ts-comment
await closeOpenConnection(ens?._registry?.contract);
await closeOpenConnection(registry);
await closeOpenConnection(resolver);
await closeOpenConnection(nameWrapper);
Expand Down
18 changes: 4 additions & 14 deletions packages/web3-eth/test/integration/batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 WebSocketProvider from 'web3-providers-ws';
// eslint-disable-next-line import/no-extraneous-dependencies
import { hexToNumber } from 'web3-utils';
import { Web3Eth } from '../../src';
Expand All @@ -23,7 +22,7 @@ import {
closeOpenConnection,
createTempAccount,
getSystemTestProvider,
isWs,
waitForOpenConnection,
} from '../fixtures/system_test_utils';

describe('eth', () => {
Expand All @@ -33,18 +32,8 @@ describe('eth', () => {

beforeAll(async () => {
clientUrl = getSystemTestProvider();

if (isWs) {
web3Eth = new Web3Eth(
new WebSocketProvider(
clientUrl,
{},
{ delay: 1, autoReconnect: false, maxAttempts: 1 },
),
);
} else {
web3Eth = new Web3Eth(clientUrl);
}
web3Eth = new Web3Eth(clientUrl);
await waitForOpenConnection(web3Eth);
});
afterAll(async () => {
await closeOpenConnection(web3Eth);
Expand All @@ -54,6 +43,7 @@ describe('eth', () => {
it('BatchRequest', async () => {
const acc1 = await createTempAccount();
const acc2 = await createTempAccount();

const batch = new web3Eth.BatchRequest();
const request1 = {
id: 10,
Expand Down
130 changes: 130 additions & 0 deletions packages/web3-eth/test/integration/block/rpc.getBlock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 { FMT_BYTES, FMT_NUMBER } from 'web3-utils';
import { TransactionReceipt, Transaction } from 'web3-types';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Contract } from 'web3-eth-contract';
import { validator } from 'web3-validator';
import { Web3Eth } from '../../../src';
import {
getSystemTestProvider,
createTempAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { BasicAbi, BasicBytecode } from '../../shared_fixtures/build/Basic';
import { toAllVariants } from '../../shared_fixtures/utils';
import { sendFewTxes } from '../helper';
import { blockSchema } from '../../../src/schemas';

describe('rpc with block', () => {
let web3Eth: Web3Eth;
let clientUrl: string;

let contract: Contract<typeof BasicAbi>;
let deployOptions: Record<string, unknown>;
let sendOptions: Record<string, unknown>;

let blockData: {
earliest: 'earliest';
latest: 'latest';
pending: 'pending';
blockNumber: number | bigint;
blockHash: string;
transactionHash: string;
transactionIndex: number | bigint;
};
let tempAcc: { address: string; privateKey: string };
let tempAcc2: { address: string; privateKey: string };

beforeAll(() => {
clientUrl = getSystemTestProvider();
web3Eth = new Web3Eth({
provider: clientUrl,
config: {
transactionPollingTimeout: 2000,
},
});

contract = new Contract(BasicAbi, undefined, {
provider: clientUrl,
});

deployOptions = {
data: BasicBytecode,
arguments: [10, 'string init value'],
};
});
beforeAll(async () => {
tempAcc = await createTempAccount();
tempAcc2 = await createTempAccount();
sendOptions = { from: tempAcc.address, gas: '1000000' };

await contract.deploy(deployOptions).send(sendOptions);
const [receipt]: TransactionReceipt[] = await sendFewTxes({
web3Eth,
from: tempAcc.address,
to: tempAcc2.address,
value: '0x1',
times: 1,
});
blockData = {
pending: 'pending',
latest: 'latest',
earliest: 'earliest',
blockNumber: Number(receipt.blockNumber),
blockHash: String(receipt.blockHash),
transactionHash: String(receipt.transactionHash),
transactionIndex: Number(receipt.transactionIndex),
};
});
afterAll(async () => {
await closeOpenConnection(web3Eth);
await closeOpenConnection(contract);
});

describe('methods', () => {
it.each(
toAllVariants<{
block: 'earliest' | 'latest' | 'pending' | 'blockHash' | 'blockNumber';
hydrated: boolean;
format: string;
}>({
block: ['earliest', 'latest', 'blockHash', 'blockNumber'],
hydrated: [true, false],
format: Object.values(FMT_NUMBER),
}),
)('getBlock', async ({ hydrated, block, format }) => {
const b = {
...(await web3Eth.getBlock(blockData[block], hydrated, {
number: format as FMT_NUMBER,
bytes: FMT_BYTES.HEX,
})),
};
if (blockData[block] === 'pending') {
b.nonce = '0x0';
b.miner = '0x0000000000000000000000000000000000000000';
b.totalDifficulty = '0x0';
}
expect(validator.validateJSONSchema(blockSchema, b)).toBeUndefined();

if (hydrated && b.transactions?.length > 0) {
// eslint-disable-next-line jest/no-conditional-expect
expect(b.transactions).toBeInstanceOf(Array<Transaction>);
}
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 { TransactionReceipt } from 'web3-types';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Contract } from 'web3-eth-contract';
import { Web3Eth } from '../../../src';
import {
getSystemTestBackend,
getSystemTestProvider,
createTempAccount,
closeOpenConnection,
} from '../../fixtures/system_test_utils';
import { BasicAbi, BasicBytecode } from '../../shared_fixtures/build/Basic';
import { toAllVariants } from '../../shared_fixtures/utils';
import { sendFewTxes } from '../helper';

describe('rpc with block', () => {
let web3Eth: Web3Eth;
let clientUrl: string;

let contract: Contract<typeof BasicAbi>;
let deployOptions: Record<string, unknown>;
let sendOptions: Record<string, unknown>;

let blockData: {
earliest: 'earliest';
latest: 'latest';
pending: 'pending';
blockNumber: number | bigint;
blockHash: string;
transactionHash: string;
transactionIndex: number | bigint;
};
let tempAcc: { address: string; privateKey: string };
let tempAcc2: { address: string; privateKey: string };

beforeAll(() => {
clientUrl = getSystemTestProvider();
web3Eth = new Web3Eth({
provider: clientUrl,
config: {
transactionPollingTimeout: 2000,
},
});

contract = new Contract(BasicAbi, undefined, {
provider: clientUrl,
});

deployOptions = {
data: BasicBytecode,
arguments: [10, 'string init value'],
};
});
beforeAll(async () => {
tempAcc = await createTempAccount();
tempAcc2 = await createTempAccount();
sendOptions = { from: tempAcc.address, gas: '1000000' };

await contract.deploy(deployOptions).send(sendOptions);
const [receipt]: TransactionReceipt[] = await sendFewTxes({
web3Eth,
from: tempAcc.address,
to: tempAcc2.address,
value: '0x1',
times: 1,
});
blockData = {
pending: 'pending',
latest: 'latest',
earliest: 'earliest',
blockNumber: Number(receipt.blockNumber),
blockHash: String(receipt.blockHash),
transactionHash: String(receipt.transactionHash),
transactionIndex: Number(receipt.transactionIndex),
};
});
afterAll(async () => {
await closeOpenConnection(web3Eth);
await closeOpenConnection(contract);
});

describe('methods', () => {
it.each(
toAllVariants<{
block: 'earliest' | 'latest' | 'pending' | 'blockHash' | 'blockNumber';
}>({
block: ['earliest', 'latest', 'pending', 'blockHash', 'blockNumber'],
}),
)('getBlockTransactionCount', async ({ block }) => {
const res = await web3Eth.getBlockTransactionCount(blockData[block]);
let shouldBe: number;
if (getSystemTestBackend() === 'ganache') {
shouldBe = blockData[block] === 'earliest' ? 0 : 1;
} else {
shouldBe = ['earliest', 'pending'].includes(String(blockData[block])) ? 0 : 1;
}
expect(Number(res)).toBe(shouldBe);
});
});
});
Loading