From d743c4f08a19a73a343827976718cec90096530a Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:06:03 +0200 Subject: [PATCH 1/8] DRPCProvider --- .../src/web3_provider_drpc.ts | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 packages/web3-rpc-providers/src/web3_provider_drpc.ts 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..8fe7d81f65b --- /dev/null +++ b/packages/web3-rpc-providers/src/web3_provider_drpc.ts @@ -0,0 +1,92 @@ +/* +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'; + +export class DRPCProvider extends Web3ExternalProvider { + // eslint-disable-next-line default-param-last + public constructor( + network: Network = Network.ETH_MAINNET, + transport: Transport = Transport.HTTPS, + token = 'TEST_TOKEN', + host = 'lb.drpc.org', + providerConfigOptions?: HttpProviderOptions | SocketOptions, + ) { + super(network, transport, token, host, providerConfigOptions); + } + + // eslint-disable-next-line class-methods-use-this + public getRPCURL(network: Network, transport: Transport, key: string, host: string) { + let networkString = ''; + + switch (network) { + case Network.ETH_MAINNET: + networkString = 'ethereum'; + break; + case Network.ETH_SEPOLIA: + networkString = 'sepolia'; + break; + case Network.ETH_HOLESKY: + networkString = 'holesky'; + break; + + case Network.ARBITRUM_MAINNET: + networkString = 'arbitrum'; + break; + case Network.ARBITRUM_SEPOLIA: + networkString = 'arbitrum-sepolia'; + break; + + case Network.BNB_MAINNET: + networkString = 'bsc'; + break; + case Network.BNB_TESTNET: + networkString = 'bsc-testnet'; + break; + + case Network.POLYGON_MAINNET: + networkString = 'polygon'; + break; + case Network.POLYGON_AMONY: + networkString = 'polygon-amoy'; + break; + + case Network.ZKERA_MAINNET: + networkString = 'zksync'; + break; + + case Network.ZKERA_SEPOLIA: + networkString = 'zksync-sepolia'; + break; + + default: + throw new Error('Network info not available.'); + } + + let protocol = ''; + + if (transport === Transport.HTTPS) { + protocol = 'rpc'; + } else if (transport === Transport.WebSocket) { + protocol = 'ws'; + } + + return `${transport}://${host}/og${protocol}?network=${networkString}&dkey=${key}`; + } +} From 4aad732f7f07fb17d9615a9462e8cde9aeec5301 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:06:34 +0200 Subject: [PATCH 2/8] default provider --- packages/web3-rpc-providers/src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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(); From 39f9648ffe43f9cc24d63cdfec776b84a1135584 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:07:08 +0200 Subject: [PATCH 3/8] ZK network --- packages/web3-rpc-providers/src/types.ts | 3 +++ 1 file changed, 3 insertions(+) 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 From 665867bead421bccb726e3cdf500dd60e69b0b28 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:07:27 +0200 Subject: [PATCH 4/8] test and update --- .../src/web3_provider_quicknode.ts | 2 +- .../test/integration/web3RPCProviders.test.ts | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) 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..d6ab51ca0cf 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,29 @@ 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.skip(`dRPC Provider should work with ${transport} transport and ${network} network`, async () => { + const provider = new DRPCProvider(network, transport); const web3 = new Web3(provider); const result = await web3.eth.getBlockNumber(); From b0100fa4e54df0cc95d749bb83aa17fdf67775d4 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:14:33 +0200 Subject: [PATCH 5/8] test updates in draft PR --- packages/web3/test/integration/web3RPCProviders.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/web3/test/integration/web3RPCProviders.test.ts b/packages/web3/test/integration/web3RPCProviders.test.ts index d6ab51ca0cf..4d8ec7fc9a8 100644 --- a/packages/web3/test/integration/web3RPCProviders.test.ts +++ b/packages/web3/test/integration/web3RPCProviders.test.ts @@ -65,14 +65,14 @@ describe('Web3 RPC Provider Integration tests', () => { }); }); }); - it(`should work with mainnet provider`, async () => { + it.skip(`should work with mainnet provider`, async () => { const web3 = new Web3(mainnet); const result = await web3.eth.getBlockNumber(); expect(typeof result).toBe('bigint'); expect(result > 0).toBe(true); }); - it(`should work with default provider`, async () => { + it.skip(`should work with default provider`, async () => { const web3 = new Web3(); const result = await web3.eth.getBlockNumber(); expect(typeof result).toBe('bigint'); From 276092f0ed9b9bbe43226ec364da6ceaf0ade508 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 12 Sep 2024 17:17:41 +0200 Subject: [PATCH 6/8] added test --- .../web3/test/integration/web3RPCProviders.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/web3/test/integration/web3RPCProviders.test.ts b/packages/web3/test/integration/web3RPCProviders.test.ts index 4d8ec7fc9a8..3f7b9b3c5f0 100644 --- a/packages/web3/test/integration/web3RPCProviders.test.ts +++ b/packages/web3/test/integration/web3RPCProviders.test.ts @@ -78,4 +78,15 @@ describe('Web3 RPC Provider Integration tests', () => { expect(typeof result).toBe('bigint'); expect(result > 0).toBe(true); }); + + it.skip(`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)); + }); }); From f2f2974ca6ca74154242fa6e3a78d9865589e524 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Tue, 17 Sep 2024 15:48:31 +0200 Subject: [PATCH 7/8] update getRPCURL --- .../src/web3_provider_drpc.ts | 62 ++++++------------- 1 file changed, 19 insertions(+), 43 deletions(-) diff --git a/packages/web3-rpc-providers/src/web3_provider_drpc.ts b/packages/web3-rpc-providers/src/web3_provider_drpc.ts index 8fe7d81f65b..08afa5dc547 100644 --- a/packages/web3-rpc-providers/src/web3_provider_drpc.ts +++ b/packages/web3-rpc-providers/src/web3_provider_drpc.ts @@ -24,63 +24,39 @@ export class DRPCProvider extends Web3ExternalProvider { public constructor( network: Network = Network.ETH_MAINNET, transport: Transport = Transport.HTTPS, - token = 'TEST_TOKEN', + token = 'TOKEN_HOLDER', host = 'lb.drpc.org', providerConfigOptions?: HttpProviderOptions | SocketOptions, ) { super(network, transport, token, host, providerConfigOptions); } - // eslint-disable-next-line class-methods-use-this - public getRPCURL(network: Network, transport: Transport, key: string, host: string) { - let networkString = ''; - - switch (network) { - case Network.ETH_MAINNET: - networkString = 'ethereum'; - break; - case Network.ETH_SEPOLIA: - networkString = 'sepolia'; - break; - case Network.ETH_HOLESKY: - networkString = 'holesky'; - break; - - case Network.ARBITRUM_MAINNET: - networkString = 'arbitrum'; - break; - case Network.ARBITRUM_SEPOLIA: - networkString = 'arbitrum-sepolia'; - break; + public static readonly networkStringMap: { [key: string]: string } = { + [Network.ETH_MAINNET]: 'ethereum', + [Network.ETH_SEPOLIA]: 'sepolia', + [Network.ETH_HOLESKY]: 'holesky', - case Network.BNB_MAINNET: - networkString = 'bsc'; - break; - case Network.BNB_TESTNET: - networkString = 'bsc-testnet'; - break; + [Network.ARBITRUM_MAINNET]: 'arbitrum', + [Network.ARBITRUM_SEPOLIA]: 'arbitrum-sepolia', - case Network.POLYGON_MAINNET: - networkString = 'polygon'; - break; - case Network.POLYGON_AMONY: - networkString = 'polygon-amoy'; - break; + [Network.BNB_MAINNET]: 'bsc', + [Network.BNB_TESTNET]: 'bsc-testnet', - case Network.ZKERA_MAINNET: - networkString = 'zksync'; - break; + [Network.POLYGON_MAINNET]: 'polygon', + [Network.POLYGON_AMONY]: 'polygon-amoy', - case Network.ZKERA_SEPOLIA: - networkString = 'zksync-sepolia'; - break; + [Network.ZKERA_MAINNET]: 'zksync', + [Network.ZKERA_SEPOLIA]: 'zksync-sepolia', + }; - default: - throw new Error('Network info not available.'); + // 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) { From c1c78ff9d2bb023354bdc166e7bc209917787b1c Mon Sep 17 00:00:00 2001 From: Dan Forbes Date: Thu, 7 Nov 2024 08:28:13 -0800 Subject: [PATCH 8/8] Update dRPC Provider with API Key (#7352) * Update with API Key * Update CHANGELOG --- CHANGELOG.md | 6 ++++++ packages/web3-rpc-methods/CHANGELOG.md | 6 ++++++ packages/web3-rpc-providers/src/web3_provider_drpc.ts | 11 ++++++++--- .../web3/test/integration/web3RPCProviders.test.ts | 9 +++++---- 4 files changed, 25 insertions(+), 7 deletions(-) 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/web3_provider_drpc.ts b/packages/web3-rpc-providers/src/web3_provider_drpc.ts index 08afa5dc547..66e6e9a434f 100644 --- a/packages/web3-rpc-providers/src/web3_provider_drpc.ts +++ b/packages/web3-rpc-providers/src/web3_provider_drpc.ts @@ -19,12 +19,14 @@ 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 = 'TOKEN_HOLDER', + token = 'Avj1DYpxRUOer0sLDhdUK_YiF4nikX4R77x3TgFkVp5j', host = 'lb.drpc.org', providerConfigOptions?: HttpProviderOptions | SocketOptions, ) { @@ -50,7 +52,7 @@ export class DRPCProvider extends Web3ExternalProvider { }; // eslint-disable-next-line class-methods-use-this - public getRPCURL(network: Network, transport: Transport, key: string, host: string) { + public getRPCURL(network: Network, transport: Transport, _key: string, _host: string) { const networkString = DRPCProvider.networkStringMap[network] || ''; if (!networkString) { throw new Error('Network info not available.'); @@ -63,6 +65,9 @@ export class DRPCProvider extends Web3ExternalProvider { protocol = 'ws'; } - return `${transport}://${host}/og${protocol}?network=${networkString}&dkey=${key}`; + 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/test/integration/web3RPCProviders.test.ts b/packages/web3/test/integration/web3RPCProviders.test.ts index 3f7b9b3c5f0..d8d1b8e9136 100644 --- a/packages/web3/test/integration/web3RPCProviders.test.ts +++ b/packages/web3/test/integration/web3RPCProviders.test.ts @@ -51,7 +51,8 @@ describe('Web3 RPC Provider Integration tests', () => { }); } - it.skip(`dRPC Provider should work with ${transport} transport and ${network} network`, async () => { + 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(); @@ -65,21 +66,21 @@ describe('Web3 RPC Provider Integration tests', () => { }); }); }); - it.skip(`should work with mainnet provider`, async () => { + it(`should work with mainnet provider`, async () => { const web3 = new Web3(mainnet); const result = await web3.eth.getBlockNumber(); expect(typeof result).toBe('bigint'); expect(result > 0).toBe(true); }); - it.skip(`should work with default provider`, async () => { + it(`should work with default provider`, async () => { const web3 = new Web3(); const result = await web3.eth.getBlockNumber(); expect(typeof result).toBe('bigint'); expect(result > 0).toBe(true); }); - it.skip(`Web3 default provider should always be connected with Eth mainnet`, async () => { + 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');