2020-06-12 03:38:55 -04:00
|
|
|
_section: Providers @<providers>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
A **Provider** is an abstraction of a connection to the
|
|
|
|
Ethereum network, providing a concise, consistent interface
|
|
|
|
to standard Ethereum node functionality.
|
|
|
|
|
|
|
|
The ethers.js library provides several options which should
|
|
|
|
cover the vast majority of use-cases, but also includes the
|
|
|
|
necessary functions and classes for sub-classing if a more
|
|
|
|
custom configuration is necessary.
|
|
|
|
|
2020-07-14 13:13:52 +02:00
|
|
|
Most users should use the [[providers-getDefaultProvider]].
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_subsection: Default Provider @<providers-getDefaultProvider>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
The default provider is the safest, easiest way to begin
|
|
|
|
developing on //Ethereum//, and it is also robust enough
|
|
|
|
for use in production.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
It creates a [[FallbackProvider]] connected to as many backend
|
2019-08-21 01:53:47 -04:00
|
|
|
services as possible. When a request is made, it is sent to
|
2020-11-22 23:03:50 -05:00
|
|
|
multiple backends simultaneously. As responses from each backend
|
2019-08-21 01:53:47 -04:00
|
|
|
are returned, they are checked that they agree. Once a quorum
|
|
|
|
has been reached (i.e. enough of the backends agree), the response
|
|
|
|
is provided to your application.
|
|
|
|
|
|
|
|
This ensures that if a backend has become out-of-sync, or if it
|
|
|
|
has been compromised that its responses are dropped in favor of
|
|
|
|
responses that match the majority.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.getDefaultProvider([ network, [ options ] ]) => [[Provider]]
|
2019-08-21 01:53:47 -04:00
|
|
|
Returns a new Provider, backed by multiple services, connected
|
2020-11-22 23:03:50 -05:00
|
|
|
to //network//. If no //network// is provided, **homestead**
|
2019-08-21 01:53:47 -04:00
|
|
|
(i.e. mainnet) is used.
|
|
|
|
|
2020-09-08 01:10:08 -04:00
|
|
|
The //network// may also be a URL to connect to, such as ``http:/\/localhost:8545``
|
|
|
|
or ``wss:/\/example.com``.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
The //options// is an object, with the following properties:
|
|
|
|
|
|
|
|
_table: Option Properties
|
|
|
|
|
|
|
|
$Alchemy: [[link-alchemy]] API Token
|
|
|
|
$Etherscan: [[link-etherscan]] API Token
|
2020-11-22 23:03:50 -05:00
|
|
|
$Infura: [[link-infura]] Project ID or ``{ projectId, projectSecret }``
|
|
|
|
$Pocket: [[link-pocket]] Application ID or ``{ applicationId, applicationSecretKey }``
|
2020-06-12 03:38:55 -04:00
|
|
|
$Quorum: The number of backends that must agree
|
2020-05-08 03:24:40 -04:00
|
|
|
//(default: 2 for mainnet, 1 for testnets)//
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
| **Property** | **Description** |
|
|
|
|
| //alchemy// | $Alchemy |
|
|
|
|
| //etherscan// | $Etherscan |
|
|
|
|
| //infura// | $Infura |
|
2020-11-22 23:03:50 -05:00
|
|
|
| //pocket// | $Pocket |
|
2020-06-12 03:38:55 -04:00
|
|
|
| //quorum// | $Quorum |
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_note: Note: API Keys
|
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
It is highly recommended for production services to acquire
|
|
|
|
and specify an API Key for each service.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
The default API Keys used by ethers are shared across all users,
|
2020-05-08 03:24:40 -04:00
|
|
|
so services may throttle all services that are using the default
|
|
|
|
API Keys during periods of load without realizing it.
|
|
|
|
|
|
|
|
Many services also have monitoring and usage metrics, which are
|
2020-07-03 01:44:17 -04:00
|
|
|
only available if an API Key is specified. This allows tracking
|
2020-05-08 03:24:40 -04:00
|
|
|
how many requests are being sent and which methods are being
|
|
|
|
used the most.
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
Some services also provide additional paid features, which are only
|
2020-05-08 03:24:40 -04:00
|
|
|
available when specifying an API Key.
|
|
|
|
|
2020-09-08 01:10:08 -04:00
|
|
|
_subsection: Networks
|
|
|
|
|
|
|
|
There are several official common Ethereum networks as well as custom
|
|
|
|
networks and other compatible projects.
|
|
|
|
|
2021-06-10 17:38:38 -04:00
|
|
|
Any API that accept a [[providers-Networkish]] can be passed a common
|
|
|
|
name (such as ``"mainnet"`` or ``"ropsten"``) or chain ID to use that
|
|
|
|
network definition or may specify custom parameters.
|
|
|
|
|
|
|
|
_property: ethers.providers.getNetwork(aNetworkish) => [[providers-Network]]
|
|
|
|
|
|
|
|
Returns the full [[providers-Network]] for the given standard
|
|
|
|
//aNetworkish// [[providers-Networkish]].
|
|
|
|
|
|
|
|
This is useful for functions and classes which wish to accept [[providers-Networkish]]
|
|
|
|
as an input parameter.
|
|
|
|
|
|
|
|
_code: @lang<javascript>
|
|
|
|
|
|
|
|
//_hide: const getNetwork = ethers.providers.getNetwork;
|
|
|
|
|
|
|
|
// By Chain Name
|
|
|
|
//_result:
|
|
|
|
getNetwork("homestead");
|
|
|
|
//_hide: delete _._defaultProvider;
|
|
|
|
//_log:
|
|
|
|
|
|
|
|
// By Chain ID
|
|
|
|
//_result:
|
|
|
|
getNetwork(1);
|
|
|
|
//_hide: delete _._defaultProvider;
|
|
|
|
//_log:
|
2020-09-08 01:10:08 -04:00
|
|
|
|
|
|
|
_heading: Custom ENS Contract
|
|
|
|
|
|
|
|
One common reason to specify custom properties for a [[providers-Network]] is to override
|
|
|
|
the address of the root ENS registry, either on a common network to intercept
|
|
|
|
the [ENS Methods](Provider--ens-methods) or to specify the ENS registry on a
|
|
|
|
dev network (on most dev networks you must deploy the ENS contracts manually).
|
|
|
|
|
|
|
|
_code: Example: Override the ENS registry @lang<script>
|
|
|
|
|
|
|
|
const network = {
|
|
|
|
name: "dev",
|
2021-10-04 10:41:54 -04:00
|
|
|
chainId: 1337,
|
2020-09-08 01:10:08 -04:00
|
|
|
ensAddress: customEnsAddress
|
|
|
|
};
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-10 17:38:38 -04:00
|
|
|
|
2019-08-21 01:53:47 -04:00
|
|
|
_subsection: Provider Documentation
|
|
|
|
|
|
|
|
_toc:
|
|
|
|
provider
|
|
|
|
jsonrpc-provider
|
|
|
|
api-providers
|
|
|
|
other
|
|
|
|
types
|