Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RPC provider: Infura #7286

Open
wants to merge 10 commits into
base: 4.x
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions FUNDING.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"drips": {
"ethereum": {
"ownedBy": "0x04E504Acb5f6A9CB387fa37ec5Fc457A9D36D371"
}
}
"drips": {
"ethereum": {
"ownedBy": "0x04E504Acb5f6A9CB387fa37ec5Fc457A9D36D371"
}
}
}
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- InfuraProvider was added (#7286)
- PublicNodeProvider was added (#7322)
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { QuickNodeProvider } from './web3_provider_quicknode.js';

export * from './types.js';
export * from './web3_provider_quicknode.js';
export * from './web3_provider_infura.js';
export * from './web3_provider_publicnode.js';
export * from './web3_provider.js';
export * from './errors.js';
Expand Down
30 changes: 27 additions & 3 deletions packages/web3-rpc-providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,27 @@ export enum Transport {
export enum Network {
ETH_MAINNET = 'eth_mainnet',
ETH_SEPOLIA = 'eth_sepolia',
ETH_GOERLI = 'eth_goerli',
ETH_HOLESKY = 'eth_holesky',

PALM_MAINNET = 'palm_mainnet',
PALM_TESTNET = 'palm_testnet',

BLAST_MAINNET = 'blast_mainnet',
BLAST_SEPOLIA = 'blast_sepolia',

STARKNET_MAINNET = 'starknet_mainnet',
STARKNET_SEPOLIA = 'starknet_sepolia',

ZKSYNC_MAINNET = 'zksync_mainnet',
ZKSYNC_SEPOLIA = 'zksync_sepolia',

CELO_MAINNET = 'celo_mainnet',
CELO_ALFAJORES = 'celo_alfajores',

AVALANCHE_MAINNET = 'avalanche_mainnet',
AVALANCHE_FUJI = 'avalanche_fuji',

POLYGON_MAINNET = 'polygon_mainnet',

POLYGON_AMOY = 'polygon_amoy',
Expand All @@ -37,12 +56,15 @@ export enum Network {

ARBITRUM_MAINNET = 'arbitrum_mainnet',
ARBITRUM_SEPOLIA = 'arbitrum_sepolia',
ARBITRUM_GOERLI = 'arbitrum_goerli',

BASE_MAINNET = 'base_mainnet',
BASE_SEPOLIA = 'base_sepolia',
BASE_GOERLI = 'base_foerli',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
BASE_GOERLI = 'base_foerli',
BASE_GOERLI = 'base_goerli',


OPTIMISM_MAINNET = 'optimism_mainnet',
OPTIMISM_SEPOLIA = 'optimism_sepolia',
OPTIMISM_GOERLI = 'optimism_goerli',

FANTOM_MAINNET = 'fantom_mainnet',
FANTOM_TESTNET = 'fantom_testnet',
Expand All @@ -56,12 +78,16 @@ export enum Network {
BSC_MAINNET = 'bsc_mainnet',
BSC_TESTNET = 'bsc_testnet',

MANTLE_SEPOLIA = 'mantle_sepolia',

LINEA_MAINNET = 'linea_mainnet',
LINEA_SEPOLIA = 'linea_sepolia',
LINEA_GOERLI = 'linea_goerli',
ARBITRUM_ONE = 'arbitrum_one',
ARBITRUM_NOVA = 'arbitrum_nova',
AVALANCHE_FUJI_C = 'avalanche_fuji_c',
AVALANCHE_FUJI_P = 'avalanche_fuji_p',
AVALANCHE_FUJI_X = 'avalanche_fuji_x',
BLAST_MAINNET = 'blast_mainnet',
OPBNB_MAINNET = 'opbnb_mainnet',
OPBNB_TESTNET = 'opbnb_testnet',
GNOSIS_MAINNET = 'gnosis_mainnet',
Expand All @@ -76,8 +102,6 @@ export enum Network {
MOONBEAM_MAINNET = 'moonbeam_mainnet',
TAIKO_MAINNET = 'taiko_mainnet',
TAIKO_HEKLA = 'taiko_hekla',
LINEA_MAINNET = 'linea_mainnet',
LINEA_SEPOLIA = 'linea_sepolia',
BAHAMUT_MAINNET = 'bahamut_mainnet',
SCROLL_MAINNET = 'scroll_mainnet',
SCROLL_SEPOLIA = 'scroll_sepolia',
Expand Down
83 changes: 83 additions & 0 deletions packages/web3-rpc-providers/src/web3_provider_infura.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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 { EthExecutionAPI, Web3APISpec } from 'web3-types';
import { HttpProviderOptions } from 'web3-providers-http';
import { Network, SocketOptions, Transport } from './types.js';
import { Web3ExternalProvider } from './web3_provider.js';

const isValid = (str: string) => str !== undefined && str.trim().length > 0;

export class InfuraProvider<
API extends Web3APISpec = EthExecutionAPI,
> extends Web3ExternalProvider<API> {
// eslint-disable-next-line default-param-last
public constructor(
network: Network = Network.ETH_MAINNET,
transport: Transport = Transport.HTTPS,
token = '',
host = '',
providerConfigOptions?: HttpProviderOptions | SocketOptions,
) {
super(network, transport, token, host, providerConfigOptions);
}
public static readonly networkHostMap: { [key: string]: string } = {
[Network.PALM_MAINNET]: 'palm-mainnet',
[Network.PALM_TESTNET]: 'palm-testnet',
[Network.BLAST_MAINNET]: 'blast-mainnet',
[Network.BLAST_SEPOLIA]: 'blast-sepolia',
[Network.AVALANCHE_MAINNET]: 'avalanche-mainnet',
[Network.AVALANCHE_FUJI]: 'avalanche-fuji',
[Network.STARKNET_MAINNET]: 'starknet-mainnet',
[Network.STARKNET_SEPOLIA]: 'starknet-sepolia',
[Network.ZKSYNC_MAINNET]: 'zksync-mainnet',
[Network.ZKSYNC_SEPOLIA]: 'zksync-sepolia',
[Network.CELO_MAINNET]: 'celo-mainnet',
[Network.CELO_ALFAJORES]: 'celo-alfajores',
[Network.BSC_MAINNET]: 'bsc-mainnet',
[Network.BSC_TESTNET]: 'bsc-testnet',
[Network.MANTLE_MAINNET]: 'mantle-mainnet',
[Network.MANTLE_SEPOLIA]: 'mantle-sepolia',
[Network.ETH_MAINNET]: 'mainnet',
[Network.ETH_HOLESKY]: 'holesky',
[Network.ETH_SEPOLIA]: 'sepolia',
[Network.ARBITRUM_MAINNET]: 'arbitrum-mainnet',
[Network.ARBITRUM_SEPOLIA]: 'arbitrum-sepolia',
[Network.BASE_MAINNET]: 'base-mainnet',
[Network.BASE_SEPOLIA]: 'base-sepolia',
[Network.BNB_MAINNET]: 'opbnb-mainnet',
[Network.BNB_TESTNET]: 'opbnb-testnet',
[Network.LINEA_MAINNET]: 'linea-mainnet',
[Network.LINEA_SEPOLIA]: 'linea-sepolia',
[Network.POLYGON_MAINNET]: 'polygon-mainnet',
[Network.POLYGON_AMOY]: 'polygon-amoy',
[Network.OPTIMISM_MAINNET]: 'optimism-mainnet',
[Network.OPTIMISM_SEPOLIA]: 'optimism-sepolia',
};
// eslint-disable-next-line class-methods-use-this
public getRPCURL(network: Network, transport: Transport, token: string, _host: string) {
if (!InfuraProvider.networkHostMap[network]) {
throw new Error('Network info not avalible.');
}
const defaultHost = `${InfuraProvider.networkHostMap[network]}.infura.io`;
const host = isValid(_host) ? _host : defaultHost;

return `${transport}://${host}/${
transport === Transport.WebSocket ? 'ws/' : ''
}v3/${token}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Web3ExternalProvider } from '../../src/web3_provider';
import { Network, SocketOptions, Transport } from '../../src/types';
import { ProviderConfigOptionsError } from '../../src/errors';

// Mock implementation so ws doesnt have openhandle after test exits as it attempts to connects at start
// Mock implementation so ws doesnt have openhandle after test exits as it attempts to connect at start
jest.mock('isomorphic-ws', () => {
return {
__esModule: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-rpc-providers/test/unit/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Web3ExternalProvider', () => {
expect(result).toEqual({ result: 'mock-result' });
});

it('should throw a rate limiting error when status code is 429', async () => {
it('QuickNodeProvider: should throw a rate limiting error when status code is 429', async () => {
const network: Network = Network.ETH_MAINNET;
const transport: Transport = Transport.HTTPS;
const token = 'your-token';
Expand Down

This file was deleted.

85 changes: 83 additions & 2 deletions packages/web3/test/integration/web3RPCProviders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
Network,
QuickNodeProvider,
Transport,
InfuraProvider,
PublicNodeProvider,
} from 'web3-rpc-providers';
import { Web3 } from '../../src/index';

jest.setTimeout(10000);
describe('Web3 RPC Provider Integration tests', () => {
const transports = Object.values(Transport);
const networks = [
const quickNodeNetworks = [
Network.ETH_MAINNET,
Network.ETH_HOLESKY,
Network.ETH_SEPOLIA,
Expand All @@ -39,7 +41,7 @@ describe('Web3 RPC Provider Integration tests', () => {
];

transports.forEach(transport => {
networks.forEach(network => {
quickNodeNetworks.forEach(network => {
it(`QuickNodeProvider should work with ${transport} transport and ${network} network`, async () => {
const provider = new QuickNodeProvider(network, transport);
const web3 = new Web3(provider);
Expand All @@ -55,6 +57,85 @@ describe('Web3 RPC Provider Integration tests', () => {
});
});

const infuraNetworks = [
Network.PALM_MAINNET,
Network.PALM_TESTNET,
Network.BLAST_MAINNET,
Network.BLAST_SEPOLIA,
Network.AVALANCHE_MAINNET,
Network.AVALANCHE_FUJI,
Network.STARKNET_MAINNET,
Network.STARKNET_SEPOLIA,
Network.ZKSYNC_MAINNET,
Network.ZKSYNC_SEPOLIA,
Network.CELO_MAINNET,
Network.CELO_ALFAJORES,
Network.BSC_MAINNET,
Network.BSC_TESTNET,
Network.MANTLE_MAINNET,
Network.MANTLE_SEPOLIA,
Network.ETH_MAINNET,
Network.ETH_HOLESKY,
Network.ETH_SEPOLIA,
Network.ARBITRUM_MAINNET,
Network.ARBITRUM_SEPOLIA,
Network.BASE_MAINNET,
Network.BASE_SEPOLIA,
Network.BNB_MAINNET,
Network.BNB_TESTNET,
Network.LINEA_MAINNET,
Network.LINEA_SEPOLIA,
Network.POLYGON_MAINNET,
Network.POLYGON_AMOY,
Network.OPTIMISM_MAINNET,
Network.OPTIMISM_SEPOLIA,
];
transports.forEach(transport => {
infuraNetworks.forEach(network => {
// skip not exists endpoints
if (
!(
[
Network.PALM_MAINNET,
Network.PALM_TESTNET,
Network.BLAST_SEPOLIA,
Network.STARKNET_MAINNET,
Network.STARKNET_SEPOLIA,
Network.ZKSYNC_SEPOLIA,
Network.BSC_TESTNET,
Network.MANTLE_SEPOLIA,
Network.BNB_TESTNET,
].includes(network) && transport === Transport.WebSocket
)
) {
it.skip(`InfuraProvider should work with ${transport} transport and ${network} network`, async () => {
avkos marked this conversation as resolved.
Show resolved Hide resolved
const provider = new InfuraProvider(
network,
transport,
process.env.INFURA_PROVIDER_KEY,
);

const web3 = new Web3(provider);
const result =
network === Network.STARKNET_MAINNET || network === Network.STARKNET_SEPOLIA
? BigInt(
await web3.requestManager.send({
method: 'starknet_blockNumber',
params: [],
}),
)
: await web3.eth.getBlockNumber();
expect(typeof result).toBe('bigint');
expect(result > 0).toBe(true);

if (transport === Transport.WebSocket) {
web3.provider?.disconnect();
}
});
}
});
});

const publicNodeNetworks = [
Network.POLYGON_AMOY,
Network.POLYGON_MAINNET,
Expand Down
Loading