2020-05-08 03:24:40 -04:00
|
|
|
_section: API Providers @<api-providers>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
There are many services which offer a web API for accessing
|
|
|
|
the Ethereum Blockchain. These Providers allow connecting
|
|
|
|
to them, which simplifies development, since you do not need
|
|
|
|
to run your own instance or cluster of Ethereum nodes.
|
|
|
|
|
|
|
|
However, this reliance on third-party services can reduce
|
2020-11-22 23:03:50 -05:00
|
|
|
resilience, security and increase the amount of required trust.
|
2019-08-21 01:53:47 -04:00
|
|
|
To mitigate these issues, it is recommended you use a
|
2020-05-08 03:24:40 -04:00
|
|
|
[Default Provider](providers-getDefaultProvider).
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2022-02-03 16:31:40 -05:00
|
|
|
_subsection: EtherscanProvider @<EtherscanProvider> @inherit<[[BaseProvider]]> @src<providers:class.EtherscanProvider>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2019-08-23 15:25:13 -04:00
|
|
|
The **EtherscanProvider** is backed by a combination of the various
|
2020-02-02 07:58:29 -05:00
|
|
|
[Etherscan APIs](link-etherscan-api).
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: new ethers.providers.EtherscanProvider([ network = "homestead", [ apiKey ] ])
|
|
|
|
Create a new **EtherscanProvider** connected to //network// with the
|
|
|
|
optional //apiKey//.
|
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
The //network// may be specified as a **string** for a common
|
2020-05-08 03:24:40 -04:00
|
|
|
network name, a **number** for a common chain ID or a
|
|
|
|
[Network Object]provider-(network).
|
|
|
|
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[Etherscan](link-etherscan) for your own API key.
|
|
|
|
|
|
|
|
_note: Note: Default API keys
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[Etherscan](link-etherscan) for your own API key.
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-02-01 03:39:21 -05:00
|
|
|
_definition: **Supported Networks**
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2022-02-03 15:55:50 -05:00
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
|
|
|
- ``ropsten`` - Ropsten (proof-of-work testnet)
|
|
|
|
- ``rinkeby`` - Rinkeby (proof-of-authority testnet)
|
|
|
|
- ``goerli`` - Görli (clique testnet)
|
|
|
|
- ``kovan`` - Kovan (proof-of-authority testnet)
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: Etherscan Examples @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: const EtherscanProvider = ethers.providers.EtherscanProvider;
|
|
|
|
//_hide: const apiKey = "...";
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new EtherscanProvider();
|
|
|
|
|
|
|
|
// Connect to rinkeby testnet (these are equivalent)
|
|
|
|
provider = new EtherscanProvider("rinkeby");
|
|
|
|
provider = new EtherscanProvider(4);
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
network = ethers.providers.getNetwork("rinkeby");
|
|
|
|
//_hide: delete network._defaultProvider;
|
|
|
|
//_log: network
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
provider = new EtherscanProvider(network);
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
// Connect to mainnet (homestead) with an API key
|
|
|
|
provider = new EtherscanProvider(null, apiKey);
|
|
|
|
provider = new EtherscanProvider("homestead", apiKey);
|
|
|
|
|
|
|
|
|
|
|
|
_property: provider.getHistory(address) => Array<History> @src<providers>
|
|
|
|
@TODO... Explain
|
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: InfuraProvider @<InfuraProvider> @INHERIT<[[UrlJsonRpcProvider]]> @src<providers:class.InfuraProvider>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-02-02 07:58:29 -05:00
|
|
|
The **InfuraProvider** is backed by the popular [INFURA](link-infura)
|
2020-02-01 03:39:21 -05:00
|
|
|
Ethereum service.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-06-17 23:38:59 -04:00
|
|
|
_property: new ethers.providers.InfuraProvider([ network = "homestead", [ apiKey ] ]) @SRC<providers>
|
2020-05-08 03:24:40 -04:00
|
|
|
Create a new **InfuraProvider** connected to //network// with
|
|
|
|
the optional //apiKey//.
|
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
The //network// may be specified as a **string** for a common
|
2020-05-08 03:24:40 -04:00
|
|
|
network name, a **number** for a common chain ID or a
|
|
|
|
[Network Object]provider-(network).
|
|
|
|
|
|
|
|
The //apiKey// can be a **string** Project ID or an **object**
|
|
|
|
with the properties ``projectId`` and ``projectSecret`` to
|
|
|
|
specify a [Project Secret](link-infura-secret) which can be used
|
|
|
|
on non-public sources (like on a server) to further secure your
|
|
|
|
API access and quotas.
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
_property: InfuraProvider.getWebSocketProvider([ network [ , apiKey ] ]) => [[WebSocketProvider]] @<InfuraProvider-getWebSocketProvider> @SRC<providers:InfuraProvider.getWebSocketProvider>
|
2020-06-17 23:38:59 -04:00
|
|
|
Create a new [[WebSocketProvider]] using the INFURA web-socket endpoint
|
|
|
|
to connect to //network// with the optional //apiKey//.
|
|
|
|
|
|
|
|
The //network// and //apiKey// are specified the same as [the constructor](InfuraProvider).
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_note: Note: Default API keys
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[INFURA](link-infura) for your own API key.
|
|
|
|
|
2020-02-01 03:39:21 -05:00
|
|
|
_definition: **Supported Networks**
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2022-02-03 15:55:50 -05:00
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
|
|
|
- ``ropsten`` - Ropsten (proof-of-work testnet)
|
|
|
|
- ``rinkeby`` - Rinkeby (proof-of-authority testnet)
|
|
|
|
- ``goerli`` - Görli (clique testnet)
|
|
|
|
- ``kovan`` - Kovan (proof-of-authority testnet)
|
|
|
|
- ``matic`` - Polygon
|
|
|
|
- ``maticmum`` - Polygon Mumbai Testnet
|
|
|
|
- ``optimism`` - Optimism (L2; optimistic roll-up)
|
|
|
|
- ``optimism-kovan`` - Optimism Testnet (L2; optimistic roll-up testnet)
|
|
|
|
- ``arbitrum`` - Arbitrum (L2; optimistic roll-up)
|
|
|
|
- ``arbitrum-rinkeby`` - Arbitrum Testnet (L2; optimistic roll-up testnet)
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: INFURA Examples @lang<javascript>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: const InfuraProvider = ethers.providers.InfuraProvider;
|
|
|
|
//_hide: const projectId = "...";
|
|
|
|
//_hide: const projectSecret = "...";
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new InfuraProvider();
|
|
|
|
|
|
|
|
// Connect to the ropsten testnet
|
|
|
|
// (see EtherscanProvider above for other network examples)
|
|
|
|
provider = new InfuraProvider("ropsten");
|
|
|
|
|
|
|
|
// Connect to mainnet with a Project ID (these are equivalent)
|
|
|
|
provider = new InfuraProvider(null, projectId);
|
|
|
|
provider = new InfuraProvider("homestead", projectId);
|
|
|
|
|
|
|
|
// Connect to mainnet with a Project ID and Project Secret
|
|
|
|
provider = new InfuraProvider("homestead", {
|
|
|
|
projectId: projectId,
|
|
|
|
projectSecret: projectSecret
|
|
|
|
});
|
|
|
|
|
2020-06-17 23:38:59 -04:00
|
|
|
// Connect to the INFURA WebSocket endpoints with a WebSocketProvider
|
|
|
|
provider = InfuraProvider.getWebSocketProvider()
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: await provider.destroy();
|
2020-06-17 23:38:59 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: AlchemyProvider @<AlchemyProvider> @inherit<[[UrlJsonRpcProvider]]> @src<providers:class.AlchemyProvider>
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
The **AlchemyProvider** is backed by [Alchemy](link-alchemy).
|
|
|
|
|
|
|
|
_property: new ethers.providers.AlchemyProvider([ network = "homestead", [ apiKey ] ])
|
|
|
|
Create a new **AlchemyProvider** connected to //network// with
|
|
|
|
the optional //apiKey//.
|
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
The //network// may be specified as a **string** for a common
|
2020-05-08 03:24:40 -04:00
|
|
|
network name, a **number** for a common chain ID or a
|
|
|
|
[Network Object](providers-Network).
|
|
|
|
|
|
|
|
_note: Note: Default API keys
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[Alchemy](link-alchemy) for your own API key.
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-02-01 03:39:21 -05:00
|
|
|
_definition: **Supported Networks**
|
|
|
|
|
2022-02-03 15:55:50 -05:00
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
|
|
|
- ``ropsten`` - Ropsten (proof-of-work testnet)
|
|
|
|
- ``rinkeby`` - Rinkeby (proof-of-authority testnet)
|
|
|
|
- ``goerli`` - Görli (clique testnet)
|
|
|
|
- ``kovan`` - Kovan (proof-of-authority testnet)
|
|
|
|
- ``matic`` - Polygon
|
|
|
|
- ``maticmum`` - Polygon Mumbai Testnet
|
|
|
|
- ``optimism`` - Optimism (L2; optimistic roll-up)
|
|
|
|
- ``optimism-kovan`` - Optimism Testnet (L2; optimistic roll-up testnet)
|
|
|
|
- ``arbitrum`` - Arbitrum (L2; optimistic roll-up)
|
|
|
|
- ``arbitrum-rinkeby`` - Arbitrum Testnet (L2; optimistic roll-up testnet)
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: Alchemy Examples @lang<javascript>
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: const AlchemyProvider = ethers.providers.AlchemyProvider;
|
|
|
|
//_hide: const apiKey = "...";
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new AlchemyProvider();
|
|
|
|
|
|
|
|
// Connect to the ropsten testnet
|
|
|
|
// (see EtherscanProvider above for other network examples)
|
|
|
|
provider = new AlchemyProvider("ropsten");
|
|
|
|
|
|
|
|
// Connect to mainnet with an API key (these are equivalent)
|
|
|
|
provider = new AlchemyProvider(null, apiKey);
|
|
|
|
provider = new AlchemyProvider("homestead", apiKey);
|
|
|
|
|
2020-07-14 02:11:57 -04:00
|
|
|
// Connect to the Alchemy WebSocket endpoints with a WebSocketProvider
|
|
|
|
provider = AlchemyProvider.getWebSocketProvider()
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: provider.destroy();
|
2020-07-14 02:11:57 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: CloudflareProvider @<CloudflareProvider> @inherit<[[UrlJsonRpcProvider]]> @src<providers:class.CloudflareProvider>
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-04-16 22:25:05 -04:00
|
|
|
The CloudflareProvider is backed by the [Cloudflare Ethereum Gateway](link-cloudflare).
|
2019-08-23 15:25:13 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: new ethers.providers.CloudflareProvider()
|
|
|
|
Create a new **CloudflareProvider** connected to mainnet (i.e. "homestead").
|
|
|
|
|
2020-02-01 03:39:21 -05:00
|
|
|
_definition: **Supported Networks**
|
|
|
|
|
2022-02-03 15:55:50 -05:00
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_code: Cloudflare Examples @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: const CloudflareProvider = ethers.providers.CloudflareProvider;
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new CloudflareProvider();
|
2022-03-19 02:48:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
_subsection: PocketProvider @<PocketProvider> @inherit<[[UrlJsonRpcProvider]]> @src<providers:class.PocketProvider>
|
|
|
|
|
|
|
|
The **PocketProvider** is backed by [Pocket](link-pocket).
|
|
|
|
|
|
|
|
_property: new ethers.providers.PocketProvider([ network = "homestead", [ apiKey ] ])
|
|
|
|
Create a new **PocketProvider** connected to //network// with
|
|
|
|
the optional //apiKey//.
|
|
|
|
|
|
|
|
The //network// may be specified as a **string** for a common
|
|
|
|
network name, a **number** for a common chain ID or a
|
|
|
|
[Network Object](providers-Network).
|
|
|
|
|
|
|
|
_note: Note: Default API keys
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[Pocket](link-pocket) for your own API key.
|
|
|
|
|
|
|
|
_definition: **Supported Networks**
|
|
|
|
|
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
|
|
|
- ``ropsten`` - Ropsten (proof-of-work testnet)
|
|
|
|
- ``rinkeby`` - Rinkeby (proof-of-authority testnet)
|
|
|
|
- ``goerli`` - Görli (clique testnet)
|
|
|
|
|
|
|
|
_code: Pocket Examples @lang<javascript>
|
|
|
|
|
|
|
|
//_hide: const PocketProvider = ethers.providers.PocketProvider;
|
|
|
|
//_hide: const applicationId = "...";
|
|
|
|
//_hide: const applicationSecretKey = "...";
|
|
|
|
//_hide: const loadBalancer = true;
|
|
|
|
|
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new PocketProvider();
|
|
|
|
|
|
|
|
// Connect to the ropsten testnet
|
|
|
|
// (see EtherscanProvider above for other network examples)
|
|
|
|
provider = new PocketProvider("ropsten");
|
|
|
|
|
|
|
|
// Connect to mainnet with an Application ID (these are equivalent)
|
|
|
|
provider = new PocketProvider(null, applicationId);
|
|
|
|
provider = new PocketProvider("homestead", applicationId);
|
|
|
|
|
|
|
|
// Connect to mainnet with an application ID, application secret
|
|
|
|
// and specify whether to use the load balances
|
|
|
|
provider = new PocketProvider("homestead", {
|
|
|
|
applicationId: applicationId,
|
|
|
|
applicationSecretKey: applicationSecretKey,
|
|
|
|
loadBalancer: loadBalancer // true or false
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_subsection: AnkrProvider @<AnkrProvider> @inherit<[[UrlJsonRpcProvider]]> @src<providers:class.AnkrProvider>
|
|
|
|
|
|
|
|
The **AnkrProvider** is backed by [Ankr](link-ankr).
|
|
|
|
|
|
|
|
_property: new ethers.providers.AnkrProvider([ network = "homestead", [ apiKey ] ])
|
|
|
|
Create a new **AnkrProvider** connected to //network// with
|
|
|
|
the optional //apiKey//.
|
|
|
|
|
|
|
|
The //network// may be specified as a **string** for a common
|
|
|
|
network name, a **number** for a common chain ID or a
|
|
|
|
[Network Object](providers-Network).
|
|
|
|
|
|
|
|
_note: Note: Default API keys
|
|
|
|
If no //apiKey// is provided, a shared API key will be used,
|
|
|
|
which may result in reduced performance and throttled requests.
|
|
|
|
|
|
|
|
It is highly recommended for production, you register with
|
|
|
|
[Ankr](link-ankr) for your own API key.
|
|
|
|
|
|
|
|
_definition: **Supported Networks**
|
|
|
|
|
|
|
|
- ``homestead`` - Homestead (Mainnet)
|
|
|
|
- ``matic`` - Polygon
|
|
|
|
- ``arbitrum`` - Arbitrum (L2; optimistic roll-up)
|
|
|
|
|
|
|
|
_code: Ankr Examples @lang<javascript>
|
|
|
|
|
|
|
|
//_hide: const AnkrProvider = ethers.providers.AnkrProvider;
|
|
|
|
//_hide: const apiKey = "...";
|
|
|
|
|
|
|
|
// Connect to mainnet (homestead)
|
|
|
|
provider = new AnkrProvider();
|
|
|
|
|
|
|
|
// Connect to polygont
|
|
|
|
// (see EtherscanProvider above for other network examples)
|
|
|
|
provider = new AnkrProvider("matic");
|
|
|
|
|
|
|
|
// Connect to mainnet with an API Key (these are equivalent)
|
|
|
|
provider = new AnkrProvider(null, apiKey);
|
|
|
|
provider = new AnkrProvider("homestead", apiKey);
|