diff --git a/CHANGELOG.md b/CHANGELOG.md index ff0bf758f8c..b858ceaea56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1138,3 +1138,13 @@ should use 4.0.1-alpha.0 for testing. ## [Unreleased] - Added rpc exception codes following eip-1474 as an experimental feature (if `useRpcCallSpecification` at `enableExperimentalFeatures` is `true`) (#5525) + +#### web3 + +##### Removed + +- Private static `_contracts:Contract[]` and static `setProvider` function was removed (#5792) + +##### Added + +- `registeredSubscriptions` was added by default in web3 constructor (#5792) diff --git a/packages/web3-eth/src/web3_eth.ts b/packages/web3-eth/src/web3_eth.ts index 9efbe426efa..37bdf9ca652 100644 --- a/packages/web3-eth/src/web3_eth.ts +++ b/packages/web3-eth/src/web3_eth.ts @@ -58,7 +58,7 @@ type RegisteredSubscription = { syncing: typeof SyncingSubscription; }; -const registeredSubscriptions = { +export const registeredSubscriptions = { logs: LogsSubscription, newPendingTransactions: NewPendingTransactionsSubscription, newHeads: NewHeadsSubscription, diff --git a/packages/web3/CHANGELOG.md b/packages/web3/CHANGELOG.md index a08f5baf67f..c4f4d81b494 100644 --- a/packages/web3/CHANGELOG.md +++ b/packages/web3/CHANGELOG.md @@ -66,3 +66,11 @@ web3.currentProvider.disconnect(); - `build` entry from `package.json` (#5755) ## [Unreleased] + +### Removed + +- Private static `_contracts:Contract[]` and static `setProvider` function was removed (#5792) + +### Added + +- `registeredSubscriptions` was added by default in web3 constructor (#5792) diff --git a/packages/web3/src/types.ts b/packages/web3/src/types.ts index 8a15b7252c3..7a2f7857e62 100644 --- a/packages/web3/src/types.ts +++ b/packages/web3/src/types.ts @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { EthExecutionAPI, SupportedProviders, Address, Bytes, Transaction } from 'web3-types'; +import { Address, Bytes, Transaction } from 'web3-types'; import Eth from 'web3-eth'; import { ContractAbi, @@ -42,10 +42,8 @@ import { ENS } from 'web3-eth-ens'; import Net from 'web3-net'; import { Iban } from 'web3-eth-iban'; -export type Web3ContractConstructor = Omit & { - new (jsonInterface: Abi, address?: Address, options?: ContractInitOptions): Contract; - setProvider: (provider: SupportedProviders) => void; -}; +export type Web3ContractConstructor = Omit & + (new (jsonInterface: Abi, address?: Address, options?: ContractInitOptions) => Contract); /** * The Ethereum interface for main web3 object. It provides extra methods in addition to `web3-eth` interface. diff --git a/packages/web3/src/web3.ts b/packages/web3/src/web3.ts index 7d0e6a8fc56..3f49043ca68 100644 --- a/packages/web3/src/web3.ts +++ b/packages/web3/src/web3.ts @@ -16,7 +16,7 @@ along with web3.js. If not, see . */ // eslint-disable-next-line max-classes-per-file import { Web3Context } from 'web3-core'; -import Web3Eth from 'web3-eth'; +import Web3Eth, { registeredSubscriptions } from 'web3-eth'; import { ContractAbi } from 'web3-eth-abi'; import Contract, { ContractInitOptions } from 'web3-eth-contract'; import { ENS, registryAddresses } from 'web3-eth-ens'; @@ -47,7 +47,7 @@ export class Web3 extends Web3Context { public eth: Web3EthInterface; public constructor(provider?: SupportedProviders | string) { - super({ provider }); + super({ provider, registeredSubscriptions }); if (isNullish(provider) || (typeof provider === 'string' && provider.trim() === '')) { console.warn( @@ -68,9 +68,6 @@ export class Web3 extends Web3Context { const self = this; class ContractBuilder extends Contract { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - private static readonly _contracts: Contract[] = []; - public constructor(jsonInterface: Abi); public constructor(jsonInterface: Abi, address: Address); public constructor(jsonInterface: Abi, options: ContractInitOptions); @@ -91,16 +88,6 @@ export class Web3 extends Web3Context { } else { super(jsonInterface, self.getContextObject()); } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ContractBuilder._contracts.push(this as Contract); - } - - public static setProvider(_provider: SupportedProviders): boolean { - for (const contract of ContractBuilder._contracts) { - contract.provider = _provider; - } - return true; } } diff --git a/packages/web3/test/integration/web3.setProvider.test.ts b/packages/web3/test/integration/web3.setProvider.test.ts new file mode 100644 index 00000000000..e3b4d92386f --- /dev/null +++ b/packages/web3/test/integration/web3.setProvider.test.ts @@ -0,0 +1,80 @@ +/* +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 . +*/ + +import { + getSystemTestProvider, + waitForOpenConnection, + closeOpenConnection, + isWs, + isHttp, + describeIf, +} from '../shared_fixtures/system_tests_utils'; +import Web3 from '../../src/index'; + +describeIf(isWs || isHttp)('web3.contract.setProvider', () => { + let clientUrl: string; + let secontUrl: string; + let web3: Web3; + + beforeAll(async () => { + clientUrl = getSystemTestProvider(); + secontUrl = clientUrl.startsWith('http') + ? clientUrl.replace('http', 'ws') + : clientUrl.replace('ws', 'http'); + web3 = new Web3(clientUrl); + + await waitForOpenConnection(web3); + }); + + afterAll(async () => { + await closeOpenConnection(web3); + }); + + test('create few contracts and check providers', () => { + const c1 = new web3.eth.Contract([]); + const c2 = new web3.eth.Contract([]); + + expect(c1.provider).toBe(web3.provider); + expect(c2.provider).toBe(web3.provider); + }); + + test('create few contracts and check providers. set different provider', () => { + const c1 = new web3.eth.Contract([]); + const c2 = new web3.eth.Contract([]); + + expect(c1.provider).toBe(web3.provider); + expect(c2.provider).toBe(web3.provider); + + web3.setProvider(secontUrl); + + expect(c1.provider).toBe(web3.provider); + expect(c2.provider).toBe(web3.provider); + }); + + test('create few contracts, set different provider to contract and check other contract', () => { + const c1 = new web3.eth.Contract([]); + const c2 = new web3.eth.Contract([]); + + expect(c1.provider).toBe(web3.provider); + expect(c2.provider).toBe(web3.provider); + + c1.setProvider(secontUrl); + + expect(c1.provider).toBe(web3.provider); + expect(c2.provider).toBe(web3.provider); + }); +}); diff --git a/scripts/system_tests_utils.ts b/scripts/system_tests_utils.ts index b696366c072..1a533dc0915 100644 --- a/scripts/system_tests_utils.ts +++ b/scripts/system_tests_utils.ts @@ -53,6 +53,7 @@ import Web3 from 'web3'; // eslint-disable-next-line import/no-extraneous-dependencies import { NonPayableMethodObject } from 'web3-eth-contract'; // eslint-disable-next-line import/no-extraneous-dependencies +import HttpProvider from 'web3-providers-http'; import accountsString from './accounts.json'; /** @@ -125,7 +126,7 @@ export const waitForOpenConnection = async ( }); export const closeOpenConnection = async (web3Context: Web3Context) => { - if (!isSocket) { + if (!isSocket || web3Context?.provider instanceof HttpProvider) { return; }