-
Notifications
You must be signed in to change notification settings - Fork 5k
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
avkos
wants to merge
10
commits into
4.x
Choose a base branch
from
ok/6975-Add-RPC-provider-Infura
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add RPC provider: Infura #7286
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ba5fee
Add infura provider
avkos 2cc6f2e
fix infura provider
avkos cda00cc
fix infura test and skip it
avkos 16576db
refactor
avkos 6a2238d
Merge branch '4.x' into ok/6975-Add-RPC-provider-Infura
avkos 92bd039
remove unnecessary test
avkos 0e0d70d
add changelog
avkos 6117711
Merge branch '4.x' into ok/6975-Add-RPC-provider-Infura
avkos 7cfb629
Merge branch '4.x' into ok/6975-Add-RPC-provider-Infura
avkos 8f44a50
Update web3_provider_infura.ts
avkos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
packages/web3-rpc-providers/src/web3_provider_infura.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
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, | ||
JsonRpcResponseWithResult, | ||
Web3APIMethod, | ||
Web3APIPayload, | ||
Web3APIReturnType, | ||
Web3APISpec, | ||
} from 'web3-types'; | ||
import { ResponseError } from 'web3-errors'; | ||
import { HttpProviderOptions } from 'web3-providers-http'; | ||
import { Transport, Network, SocketOptions } from './types.js'; | ||
import { Web3ExternalProvider } from './web3_provider.js'; | ||
import { QuickNodeRateLimitError } from './errors.js'; | ||
|
||
const isValid = (str: string) => str !== undefined && str.trim().length > 0; | ||
|
||
export class InfuraProvider< | ||
API extends Web3APISpec = EthExecutionAPI, | ||
> extends Web3ExternalProvider { | ||
// 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 async request< | ||
Method extends Web3APIMethod<API>, | ||
ResultType = Web3APIReturnType<API, Method>, | ||
>( | ||
payload: Web3APIPayload<EthExecutionAPI, Method>, | ||
requestOptions?: RequestInit, | ||
): Promise<JsonRpcResponseWithResult<ResultType>> { | ||
try { | ||
return await super.request(payload, requestOptions); | ||
} catch (error) { | ||
if (error instanceof ResponseError && error.statusCode === 429) { | ||
throw new QuickNodeRateLimitError(error); | ||
avkos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
throw error; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public getRPCURL(network: Network, transport: Transport, token: string, _host: string) { | ||
let host = ''; | ||
|
||
switch (network) { | ||
avkos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case Network.PALM_MAINNET: | ||
host = isValid(_host) ? _host : 'palm-mainnet.infura.io'; | ||
break; | ||
case Network.PALM_TESTNET: | ||
host = isValid(_host) ? _host : 'palm-testnet.infura.io'; | ||
break; | ||
case Network.BLAST_MAINNET: | ||
host = isValid(_host) ? _host : 'blast-mainnet.infura.io'; | ||
break; | ||
case Network.BLAST_SEPOLIA: | ||
host = isValid(_host) ? _host : 'blast-sepolia.infura.io'; | ||
break; | ||
case Network.AVALANCHE_MAINNET: | ||
host = isValid(_host) ? _host : 'avalanche-mainnet.infura.io'; | ||
break; | ||
case Network.AVALANCHE_FUJI: | ||
host = isValid(_host) ? _host : 'avalanche-fuji.infura.io'; | ||
break; | ||
case Network.STARKNET_MAINNET: | ||
host = isValid(_host) ? _host : 'starknet-mainnet.infura.io'; | ||
break; | ||
case Network.STARKNET_SEPOLIA: | ||
host = isValid(_host) ? _host : 'starknet-sepolia.infura.io'; | ||
break; | ||
case Network.ZKSYNC_MAINNET: | ||
host = isValid(_host) ? _host : 'zksync-mainnet.infura.io'; | ||
break; | ||
case Network.ZKSYNC_SEPOLIA: | ||
host = isValid(_host) ? _host : 'zksync-sepolia.infura.io'; | ||
break; | ||
case Network.CELO_MAINNET: | ||
host = isValid(_host) ? _host : 'celo-mainnet.infura.io'; | ||
break; | ||
case Network.CELO_ALFAJORES: | ||
host = isValid(_host) ? _host : 'celo-alfajores.infura.io'; | ||
break; | ||
case Network.BSC_MAINNET: | ||
host = isValid(_host) ? _host : 'bsc-mainnet.infura.io'; | ||
break; | ||
case Network.BSC_TESTNET: | ||
host = isValid(_host) ? _host : 'bsc-testnet.infura.io'; | ||
break; | ||
case Network.MANTLE_MAINNET: | ||
host = isValid(_host) ? _host : 'mantle-mainnet.infura.io'; | ||
break; | ||
case Network.MANTLE_SEPOLIA: | ||
host = isValid(_host) ? _host : 'mantle-sepolia.infura.io'; | ||
break; | ||
case Network.ETH_MAINNET: | ||
host = isValid(_host) ? _host : 'mainnet.infura.io'; | ||
break; | ||
case Network.ETH_HOLESKY: | ||
host = isValid(_host) ? _host : 'holesky.infura.io'; | ||
break; | ||
case Network.ETH_SEPOLIA: | ||
host = isValid(_host) ? _host : 'sepolia.infura.io'; | ||
break; | ||
case Network.ARBITRUM_MAINNET: | ||
host = isValid(_host) ? _host : 'arbitrum-mainnet.infura.io'; | ||
break; | ||
case Network.ARBITRUM_SEPOLIA: | ||
host = isValid(_host) ? _host : 'arbitrum-sepolia.infura.io'; | ||
break; | ||
case Network.BASE_MAINNET: | ||
host = isValid(_host) ? _host : 'base-mainnet.infura.io'; | ||
break; | ||
case Network.BASE_SEPOLIA: | ||
host = isValid(_host) ? _host : 'base-sepolia.infura.io'; | ||
break; | ||
case Network.BNB_MAINNET: | ||
host = isValid(_host) ? _host : 'opbnb-mainnet.infura.io'; | ||
break; | ||
case Network.BNB_TESTNET: | ||
host = isValid(_host) ? _host : 'opbnb-testnet.infura.io'; | ||
break; | ||
case Network.LINEA_MAINNET: | ||
host = isValid(_host) ? _host : 'linea-mainnet.infura.io'; | ||
break; | ||
case Network.LINEA_SEPOLIA: | ||
host = isValid(_host) ? _host : 'linea-sepolia.infura.io'; | ||
break; | ||
case Network.POLYGON_MAINNET: | ||
host = isValid(_host) ? _host : 'polygon-mainnet.infura.io'; | ||
break; | ||
case Network.POLYGON_AMONY: | ||
host = isValid(_host) ? _host : 'polygon-amoy.infura.io'; | ||
break; | ||
case Network.OPTIMISM_MAINNET: | ||
host = isValid(_host) ? _host : 'optimism-mainnet.infura.io'; | ||
break; | ||
case Network.OPTIMISM_SEPOLIA: | ||
host = isValid(_host) ? _host : 'optimism-sepolia.infura.io'; | ||
break; | ||
default: | ||
throw new Error('Network info not avalible.'); | ||
} | ||
|
||
return `${transport}://${host}/v3/${token}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
packages/web3/test/integration/external-providers/infura-rpc.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.