-
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
7203 - Alchemy rpc provider #7296
Open
MRLab12
wants to merge
12
commits into
web3:4.x
Choose a base branch
from
MRLab12:alchemy-rpc-provider
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd25b34
Add alchemy rpc provider
MRLab12 66d3592
Add Polygon AMOY network (replaces MUMBAI)
MRLab12 40af4b7
Keep this for now, check what is using this property
MRLab12 9ec6145
Testing
MRLab12 ba693ba
Remove request method
MRLab12 bed9651
Updating imports and unused API extension / Use map instead of switch
MRLab12 f3b4df7
Fix import in rpc tests
MRLab12 2f514c0
Include more Alchemy networks
MRLab12 2982118
Make a separate test loop for Alchemy networks and use a specific net…
MRLab12 3961e2a
Merge branch '4.x' into alchemy-rpc-provider
MRLab12 45c1b8a
ETH Goerli has been removed
MRLab12 809cb3a
Merge branch '4.x' into alchemy-rpc-provider
Muhammad-Altabba 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
107 changes: 107 additions & 0 deletions
107
packages/web3-rpc-providers/src/web3_provider_alchemy.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,107 @@ | ||
/* | ||
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 { HttpProviderOptions } from 'web3-providers-http'; | ||
import { Web3ExternalProvider } from './web3_provider.js'; | ||
import { Network, Transport, SocketOptions } from './types.js'; | ||
|
||
function isValid(value: string): boolean { | ||
return !!(value && value.trim().length > 0); | ||
} | ||
|
||
export class AlchemyProvider extends Web3ExternalProvider { | ||
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 networkStringMap: { [key: string]: string } = { | ||
[Network.ETH_MAINNET]: 'eth-mainnet.g.alchemy.com', | ||
[Network.ETH_SEPOLIA]: 'eth-sepolia.g.alchemy.com', | ||
[Network.ETH_HOLESKY]: 'eth-holesky.g.alchemy.com', | ||
[Network.ARBITRUM_MAINNET]: 'arb-mainnet.g.alchemy.com', | ||
[Network.ARBITRUM_SEPOLIA]: 'arb-sepolia.g.alchemy.com', | ||
[Network.ARBITRUM_NOVA_MAINNET]: 'arb-nova.g.alchemy.com', | ||
[Network.BASE_MAINNET]: 'base-mainnet.g.alchemy.com', | ||
[Network.BASE_SEPOLIA]: 'base-sepolia.g.alchemy.com', | ||
[Network.POLYGON_MAINNET]: 'polygon-mainnet.g.alchemy.com', | ||
[Network.POLYGON_MUMBAI]: 'polygon-mumbai.g.alchemy.com', | ||
[Network.POLYGON_AMOY]: 'polygon-amoy.g.alchemy.com', | ||
[Network.POLYGON_POS_MAINNET]: 'polygon-mainnet.g.alchemy.com', | ||
[Network.POLYGON_ZKEVM_MAINNET]: 'polygonzkevm-mainnet.g.alchemy.com', | ||
[Network.POLYGON_ZKEVM_CARDONA]: 'polygonzkevm-cardona.g.alchemy.com', | ||
[Network.OPTIMISM_MAINNET]: 'opt-mainnet.g.alchemy.com', | ||
[Network.OPTIMISM_SEPOLIA]: 'opt-sepolia.g.alchemy.com', | ||
[Network.ASTAR_MAINNET]: 'astar-mainnet.g.alchemy.com', | ||
[Network.WORLD_CHAIN_MAINNET]: 'worldchain-mainnet.g.alchemy.com', | ||
[Network.WORLD_CHAIN_SEPOLIA]: 'worldchain-sepolia.g.alchemy.com', | ||
[Network.SHAPE_MAINNET]: 'shape-mainnet.g.alchemy.com', | ||
[Network.SHAPE_SEPOLIA]: 'shape-sepolia.g.alchemy.com', | ||
[Network.ZKSYNC_MAINNET]: 'zksync-mainnet.g.alchemy.com', | ||
[Network.ZKSYNC_SEPOLIA]: 'zksync-sepolia.g.alchemy.com', | ||
[Network.STARKNET_MAINNET]: 'starknet-mainnet.g.alchemy.com', | ||
[Network.STARKNET_SEPOLIA]: 'starknet-sepolia.g.alchemy.com', | ||
[Network.ZETACHAIN_MAINNET]: 'zetachain-mainnet.g.alchemy.com', | ||
[Network.ZETACHAIN_TESTNET]: 'zetachain-testnet.g.alchemy.com', | ||
[Network.FANTOM_OPERA_MAINNET]: 'fantom-mainnet.g.alchemy.com', | ||
[Network.FANTOM_OPERA_TESTNET]: 'fantom-testnet.g.alchemy.com', | ||
[Network.MANTLE_MAINNET]: 'mantle-mainnet.g.alchemy.com', | ||
[Network.BERACHAIN_ARTIO]: 'berachain-artio.g.alchemy.com', | ||
[Network.BLAST_MAINNET]: 'blast-mainnet.g.alchemy.com', | ||
[Network.BLAST_SEPOLIA]: 'blast-sepolia.g.alchemy.com', | ||
[Network.LINEA_MAINNET]: 'linea-mainnet.g.alchemy.com', | ||
[Network.LINEA_SEPOLIA]: 'linea-sepolia.g.alchemy.com', | ||
[Network.ZORA_MAINNET]: 'zora-mainnet.g.alchemy.com', | ||
[Network.ZORA_SEPOLIA]: 'zora-sepolia.g.alchemy.com', | ||
[Network.POLYNOMIAL_MAINNET]: 'polynomial-mainnet.g.alchemy.com', | ||
[Network.POLYNOMIAL_SEPOLIA]: 'polynomial-sepolia.g.alchemy.com', | ||
[Network.SCROLL_MAINNET]: 'scroll-mainnet.g.alchemy.com', | ||
[Network.SCROLL_SEPOLIA]: 'scroll-sepolia.g.alchemy.com', | ||
[Network.FRAX_MAINNET]: 'frax-mainnet.g.alchemy.com', | ||
[Network.FRAX_SEPOLIA]: 'frax-sepolia.g.alchemy.com', | ||
[Network.SOLANA_MAINNET]: 'solana-mainnet.g.alchemy.com', | ||
[Network.SOLANA_DEVNET]: 'solana-devnet.g.alchemy.com', | ||
[Network.CROSSFI_TESTNET]: 'crossfi-testnet.g.alchemy.com', | ||
[Network.FLOW_EVM_TESTNET]: 'flow-testnet.g.alchemy.com', | ||
[Network.SONEIUM_MINATO]: 'soneium-minato.g.alchemy.com', | ||
[Network.GEIST_POLTER]: 'geist-polter.g.alchemy.com', | ||
[Network.ROOTSTOCK_MAINNET]: 'rootstock-mainnet.g.alchemy.com', | ||
[Network.ROOTSTOCK_TESTNET]: 'rootstock-testnet.g.alchemy.com', | ||
[Network.UNICHAIN_SEPOLIA]: 'unichain-sepolia.g.alchemy.com', | ||
[Network.GNOSIS_MAINNET]: 'gnosis-mainnet.g.alchemy.com', | ||
[Network.BNB_MAINNET]: 'bnb-mainnet.g.alchemy.com', | ||
[Network.BNB_TESTNET]: 'bnb-testnet.g.alchemy.com', | ||
[Network.OPBNB_MAINNET]: 'opbnb-mainnet.g.alchemy.com', | ||
[Network.OPBNB_TESTNET]: 'opbnb-testnet.g.alchemy.com', | ||
}; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public getRPCURL(network: Network, transport: Transport, _token: string, _host: string) { | ||
const host = AlchemyProvider.networkStringMap[network] || ''; | ||
const token = isValid(_token) ? _token : `alchemy-${network.toLowerCase()}-token`; | ||
|
||
if (!host) { | ||
throw new Error('Network info not available.'); | ||
} | ||
|
||
return `${transport}://${host}/v2/${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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,13 @@ import { | |
QuickNodeProvider, | ||
Transport, | ||
PublicNodeProvider, | ||
AlchemyProvider, | ||
} from 'web3-rpc-providers'; | ||
import { Web3 } from '../../src/index'; | ||
|
||
describe('Web3 RPC Provider Integration tests', () => { | ||
const transports = Object.values(Transport); | ||
const networks = [ | ||
const quickNodeNetworks = [ | ||
Network.ETH_MAINNET, | ||
Network.ETH_HOLESKY, | ||
Network.ETH_SEPOLIA, | ||
|
@@ -39,7 +40,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); | ||
|
@@ -53,6 +54,81 @@ describe('Web3 RPC Provider Integration tests', () => { | |
} | ||
}); | ||
}); | ||
|
||
const alchemyNetworks = [ | ||
Network.ETH_MAINNET, | ||
Network.ETH_SEPOLIA, | ||
Network.ETH_HOLESKY, | ||
Network.ARBITRUM_MAINNET, | ||
Network.ARBITRUM_SEPOLIA, | ||
Network.ARBITRUM_NOVA_MAINNET, | ||
Network.BASE_MAINNET, | ||
Network.BASE_SEPOLIA, | ||
Network.POLYGON_MAINNET, | ||
Network.POLYGON_MUMBAI, | ||
Network.POLYGON_AMOY, | ||
Network.POLYGON_POS_MAINNET, | ||
Network.POLYGON_ZKEVM_MAINNET, | ||
Network.POLYGON_ZKEVM_CARDONA, | ||
Network.OPTIMISM_MAINNET, | ||
Network.OPTIMISM_SEPOLIA, | ||
Network.ASTAR_MAINNET, | ||
Network.WORLD_CHAIN_MAINNET, | ||
Network.WORLD_CHAIN_SEPOLIA, | ||
Network.SHAPE_MAINNET, | ||
Network.SHAPE_SEPOLIA, | ||
Network.ZKSYNC_MAINNET, | ||
Network.ZKSYNC_SEPOLIA, | ||
Network.STARKNET_MAINNET, | ||
Network.STARKNET_SEPOLIA, | ||
Network.ZETACHAIN_MAINNET, | ||
Network.ZETACHAIN_TESTNET, | ||
Network.FANTOM_OPERA_MAINNET, | ||
Network.FANTOM_OPERA_TESTNET, | ||
Network.MANTLE_MAINNET, | ||
Network.BERACHAIN_ARTIO, | ||
Network.BLAST_MAINNET, | ||
Network.BLAST_SEPOLIA, | ||
Network.LINEA_MAINNET, | ||
Network.LINEA_SEPOLIA, | ||
Network.ZORA_MAINNET, | ||
Network.ZORA_SEPOLIA, | ||
Network.POLYNOMIAL_MAINNET, | ||
Network.POLYNOMIAL_SEPOLIA, | ||
Network.SCROLL_MAINNET, | ||
Network.SCROLL_SEPOLIA, | ||
Network.FRAX_MAINNET, | ||
Network.FRAX_SEPOLIA, | ||
Network.SOLANA_MAINNET, | ||
Network.SOLANA_DEVNET, | ||
Network.CROSSFI_TESTNET, | ||
Network.FLOW_EVM_TESTNET, | ||
Network.SONEIUM_MINATO, | ||
Network.GEIST_POLTER, | ||
Network.ROOTSTOCK_MAINNET, | ||
Network.ROOTSTOCK_TESTNET, | ||
Network.UNICHAIN_SEPOLIA, | ||
Network.GNOSIS_MAINNET, | ||
Network.BNB_MAINNET, | ||
Network.BNB_TESTNET, | ||
Network.OPBNB_MAINNET, | ||
Network.OPBNB_TESTNET, | ||
]; | ||
|
||
alchemyNetworks.forEach(network => { | ||
it(`AlchemyProvider should work with ${transport} transport and ${network} network`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After adding all platforms it will be a different array of networks. so it will be better to have separate const |
||
const provider = new AlchemyProvider(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(); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
const publicNodeNetworks = [ | ||
|
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.
It would be great to add all Alchemy platforms if they are compatible with web3js.
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.
Is this screenshot from web.js? Wondering if this is an accurate list of the chains to support with Alchemy.
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.
@avkos Made some changes, not sure if all the chains I added are to be supported but lmk.