diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edd15446e5..f01259ff370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2702,3 +2702,9 @@ If there are any bugs, improvements, optimizations or any new feature proposal f - The callback function provided to the static `Web3.onNewProviderDiscovered` function expects a parameter of type `EIP6963ProvidersMapUpdateEvent` as opposed to `EIP6963AnnounceProviderEvent`. (#7242) ## [Unreleased] + +### Added + +#### web3-rpc-providers + +- DRPCProvider was added (#7252) diff --git a/packages/web3-rpc-methods/CHANGELOG.md b/packages/web3-rpc-methods/CHANGELOG.md index 5cb444b1a83..d54c5745747 100644 --- a/packages/web3-rpc-methods/CHANGELOG.md +++ b/packages/web3-rpc-methods/CHANGELOG.md @@ -145,3 +145,9 @@ Documentation: - Change `estimateGas` method to add possibility pass Transaction type (#7000) ## [Unreleased] + +### Added + +#### web3-rpc-providers + +- DRPCProvider was added (#7252) diff --git a/packages/web3-rpc-providers/src/index.ts b/packages/web3-rpc-providers/src/index.ts index 577d8e6b960..24ef0f9749a 100644 --- a/packages/web3-rpc-providers/src/index.ts +++ b/packages/web3-rpc-providers/src/index.ts @@ -15,12 +15,13 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { QuickNodeProvider } from './web3_provider_quicknode.js'; +import { DRPCProvider } from './web3_provider_drpc.js'; export * from './types.js'; export * from './web3_provider_quicknode.js'; export * from './web3_provider.js'; export * from './errors.js'; +export * from './web3_provider_drpc.js'; -// default providers -export const mainnet = new QuickNodeProvider(); +// default provider +export const mainnet = new DRPCProvider(); diff --git a/packages/web3-rpc-providers/src/types.ts b/packages/web3-rpc-providers/src/types.ts index 9fda306ae0d..65f37afdc32 100644 --- a/packages/web3-rpc-providers/src/types.ts +++ b/packages/web3-rpc-providers/src/types.ts @@ -44,6 +44,9 @@ export enum Network { BNB_MAINNET = 'bnb_mainnet', BNB_TESTNET = 'bnb_testnet', + + ZKERA_MAINNET = 'zkera_mainnet', + ZKERA_SEPOLIA = 'zkera_sepolia', } // Combining the ws types diff --git a/packages/web3-rpc-providers/src/web3_provider_drpc.ts b/packages/web3-rpc-providers/src/web3_provider_drpc.ts new file mode 100644 index 00000000000..66e6e9a434f --- /dev/null +++ b/packages/web3-rpc-providers/src/web3_provider_drpc.ts @@ -0,0 +1,73 @@ +/* +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 { HttpProviderOptions } from 'web3-providers-http'; +import { Transport, Network, SocketOptions } from './types.js'; +import { Web3ExternalProvider } from './web3_provider.js'; + +const isValid = (str: string) => str !== undefined && str.trim().length > 0; + +export class DRPCProvider extends Web3ExternalProvider { + // eslint-disable-next-line default-param-last + public constructor( + network: Network = Network.ETH_MAINNET, + transport: Transport = Transport.HTTPS, + token = 'Avj1DYpxRUOer0sLDhdUK_YiF4nikX4R77x3TgFkVp5j', + host = 'lb.drpc.org', + providerConfigOptions?: HttpProviderOptions | SocketOptions, + ) { + super(network, transport, token, host, providerConfigOptions); + } + + public static readonly networkStringMap: { [key: string]: string } = { + [Network.ETH_MAINNET]: 'ethereum', + [Network.ETH_SEPOLIA]: 'sepolia', + [Network.ETH_HOLESKY]: 'holesky', + + [Network.ARBITRUM_MAINNET]: 'arbitrum', + [Network.ARBITRUM_SEPOLIA]: 'arbitrum-sepolia', + + [Network.BNB_MAINNET]: 'bsc', + [Network.BNB_TESTNET]: 'bsc-testnet', + + [Network.POLYGON_MAINNET]: 'polygon', + [Network.POLYGON_AMONY]: 'polygon-amoy', + + [Network.ZKERA_MAINNET]: 'zksync', + [Network.ZKERA_SEPOLIA]: 'zksync-sepolia', + }; + + // eslint-disable-next-line class-methods-use-this + public getRPCURL(network: Network, transport: Transport, _key: string, _host: string) { + const networkString = DRPCProvider.networkStringMap[network] || ''; + if (!networkString) { + throw new Error('Network info not available.'); + } + + let protocol = ''; + if (transport === Transport.HTTPS) { + protocol = 'rpc'; + } else if (transport === Transport.WebSocket) { + protocol = 'ws'; + } + + const host = isValid(_host) ? _host : 'lb.drpc.org'; + const key = isValid(_key) ? _key : 'Avj1DYpxRUOer0sLDhdUK_YiF4nikX4R77x3TgFkVp5j'; + + return `${transport}://${host}/og${protocol}?network=${networkString}&dkey=${key}&tag=web3js`; + } +} diff --git a/packages/web3-rpc-providers/src/web3_provider_quicknode.ts b/packages/web3-rpc-providers/src/web3_provider_quicknode.ts index ae74fa49860..633f9293402 100644 --- a/packages/web3-rpc-providers/src/web3_provider_quicknode.ts +++ b/packages/web3-rpc-providers/src/web3_provider_quicknode.ts @@ -112,7 +112,7 @@ export class QuickNodeProvider< token = isValid(_token) ? _token : '79a9476eea661d4f82de614db1d8a895b14b881c'; break; default: - throw new Error('Network info not avalible.'); + throw new Error('Network info not available.'); } return `${transport}://${host}/${token}`; diff --git a/packages/web3/test/integration/web3RPCProviders.test.ts b/packages/web3/test/integration/web3RPCProviders.test.ts index 1faeffad452..d8d1b8e9136 100644 --- a/packages/web3/test/integration/web3RPCProviders.test.ts +++ b/packages/web3/test/integration/web3RPCProviders.test.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 { mainnet, Network, QuickNodeProvider, Transport } from 'web3-rpc-providers'; +import { mainnet, Network, QuickNodeProvider, Transport, DRPCProvider } from 'web3-rpc-providers'; import { Web3 } from '../../src/index'; describe('Web3 RPC Provider Integration tests', () => { @@ -30,12 +30,30 @@ describe('Web3 RPC Provider Integration tests', () => { Network.BNB_TESTNET, Network.POLYGON_MAINNET, Network.POLYGON_AMONY, + Network.ZKERA_MAINNET, + Network.ZKERA_SEPOLIA, ]; transports.forEach(transport => { networks.forEach(network => { - it(`QuickNodeProvider should work with ${transport} transport and ${network} network`, async () => { - const provider = new QuickNodeProvider(network, transport); + if (network !== Network.ZKERA_MAINNET && network !== Network.ZKERA_SEPOLIA) { + it(`QuickNodeProvider should work with ${transport} transport and ${network} network`, async () => { + const provider = new QuickNodeProvider(network, transport); + const web3 = new Web3(provider); + const result = await web3.eth.getBlockNumber(); + + expect(typeof result).toBe('bigint'); + expect(result > 0).toBe(true); + + if (transport === Transport.WebSocket) { + web3.provider?.disconnect(); + } + }); + } + + it(`dRPC Provider should work with ${transport} transport and ${network} network`, async () => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + const provider = new DRPCProvider(network, transport); const web3 = new Web3(provider); const result = await web3.eth.getBlockNumber(); @@ -61,4 +79,15 @@ describe('Web3 RPC Provider Integration tests', () => { expect(typeof result).toBe('bigint'); expect(result > 0).toBe(true); }); + + it(`Web3 default provider should always be connected with Eth mainnet`, async () => { + const web3 = new Web3(); + const chainID = await web3.eth.getChainId(); + expect(typeof chainID).toBe('bigint'); + expect(chainID).toBe(BigInt(1)); + + const NWID = await web3.eth.net.getId(); + expect(typeof NWID).toBe('bigint'); + expect(NWID).toBe(BigInt(1)); + }); });