diff --git a/packages/web3-rpc-providers/src/index.ts b/packages/web3-rpc-providers/src/index.ts index 577d8e6b960..b48e8cd0527 100644 --- a/packages/web3-rpc-providers/src/index.ts +++ b/packages/web3-rpc-providers/src/index.ts @@ -21,6 +21,7 @@ export * from './types.js'; export * from './web3_provider_quicknode.js'; export * from './web3_provider.js'; export * from './errors.js'; +export * from './web3_provider_ankr.js'; // default providers export const mainnet = new QuickNodeProvider(); diff --git a/packages/web3-rpc-providers/src/types.ts b/packages/web3-rpc-providers/src/types.ts index 9fda306ae0d..04b31bd8aaf 100644 --- a/packages/web3-rpc-providers/src/types.ts +++ b/packages/web3-rpc-providers/src/types.ts @@ -31,7 +31,7 @@ export enum Network { POLYGON_MAINNET = 'polygon_mainnet', POLYGON_MUMBAI = 'polygon_mumbai', - POLYGON_AMONY = 'polygon_amony', + POLYGON_AMOY = 'polygon_amoy', ARBITRUM_MAINNET = 'arbitrum_mainnet', ARBITRUM_SEPOLIA = 'arbitrum_sepolia', diff --git a/packages/web3-rpc-providers/src/web3_provider_ankr.ts b/packages/web3-rpc-providers/src/web3_provider_ankr.ts new file mode 100644 index 00000000000..d7e680af38f --- /dev/null +++ b/packages/web3-rpc-providers/src/web3_provider_ankr.ts @@ -0,0 +1,58 @@ +/* +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 AnkrProvider extends Web3ExternalProvider { + // eslint-disable-next-line default-param-last + public constructor( + network: Network = Network.ETH_MAINNET, + transport: Transport = Transport.HTTPS, + key = 'ADD_KEY_HERE', + host = 'rpc.ankr.com', + providerConfigOptions?: HttpProviderOptions | SocketOptions, + ) { + super(network, transport, key, host, providerConfigOptions); + + if (transport === Transport.WebSocket) + throw new Error('Websocket is not avalible for Ankr free account.'); + } + + public static readonly networkStringMap: { [key: string]: string } = { + [Network.ETH_MAINNET]: 'eth', + [Network.ETH_SEPOLIA]: Network.ETH_SEPOLIA, + [Network.ETH_HOLESKY]: Network.ETH_HOLESKY, + [Network.ARBITRUM_MAINNET]: 'arbitrum', + [Network.ARBITRUM_SEPOLIA]: Network.ARBITRUM_SEPOLIA, + [Network.BNB_MAINNET]: 'bsc', + [Network.BNB_TESTNET]: 'bsc_testnet_chapel', + [Network.POLYGON_MAINNET]: 'polygon', + [Network.POLYGON_AMOY]: Network.POLYGON_AMOY, + }; + + // eslint-disable-next-line class-methods-use-this + public getRPCURL(network: Network, transport: Transport, key: string, host: string) { + const networkString = AnkrProvider.networkStringMap[network] || ''; + if (!networkString) { + throw new Error('Network info not available.'); + } + + return `${transport}://${host}/${networkString}/${key}`; + } +} diff --git a/packages/web3-rpc-providers/src/web3_provider_quicknode.ts b/packages/web3-rpc-providers/src/web3_provider_quicknode.ts index ae74fa49860..4b6e5ae73e6 100644 --- a/packages/web3-rpc-providers/src/web3_provider_quicknode.ts +++ b/packages/web3-rpc-providers/src/web3_provider_quicknode.ts @@ -107,7 +107,7 @@ export class QuickNodeProvider< host = isValid(_host) ? _host : 'small-chaotic-moon.matic.quiknode.pro'; token = isValid(_token) ? _token : '847569f8a017e84d985e10d0f44365d965a951f1'; break; - case Network.POLYGON_AMONY: + case Network.POLYGON_AMOY: host = isValid(_host) ? _host : 'prettiest-side-shape.matic-amoy.quiknode.pro'; token = isValid(_token) ? _token : '79a9476eea661d4f82de614db1d8a895b14b881c'; break; diff --git a/packages/web3/test/integration/web3RPCProviders.test.ts b/packages/web3/test/integration/web3RPCProviders.test.ts index 1faeffad452..0f1f6ef4786 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, AnkrProvider } from 'web3-rpc-providers'; import { Web3 } from '../../src/index'; describe('Web3 RPC Provider Integration tests', () => { @@ -29,7 +29,7 @@ describe('Web3 RPC Provider Integration tests', () => { Network.BNB_MAINNET, Network.BNB_TESTNET, Network.POLYGON_MAINNET, - Network.POLYGON_AMONY, + Network.POLYGON_AMOY, ]; transports.forEach(transport => { @@ -46,8 +46,20 @@ describe('Web3 RPC Provider Integration tests', () => { web3.provider?.disconnect(); } }); + + if (transport !== Transport.WebSocket) { + it.skip(`AnkrProvider should work with ${transport} transport and ${network} network`, async () => { + const provider = new AnkrProvider(network, transport); + const web3 = new Web3(provider); + const result = await web3.eth.getBlockNumber(); + + expect(typeof result).toBe('bigint'); + expect(result > 0).toBe(true); + }); + } }); }); + it(`should work with mainnet provider`, async () => { const web3 = new Web3(mainnet); const result = await web3.eth.getBlockNumber();