ethers.js/docs/v5/api/providers/api-providers/index.html

136 lines
20 KiB
HTML
Raw Permalink Normal View History

2020-06-09 23:56:58 -04:00
<!DOCTYPE html>
<html class="paged">
<head>
<title>API Providers</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
</head>
<body>
<div class="sidebar">
<div class="header">
2020-07-03 01:54:56 -04:00
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0</div></a></div>
2020-06-09 23:56:58 -04:00
</div>
<div class="toc"><div>
2020-06-12 03:38:55 -04:00
<div class="link title"><a href="/v5/">Documentation</a></div><div class="base show link depth-1"><a href="/v5/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/v5/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/v5/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/v5/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/v5/concepts/security/">Security</a></div><div class="base ancestor show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="show link depth-2"><a href="/v5/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><div class="show link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="ancestor show link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="show link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="show link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="myself ancestor ancestor show link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="show link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="show link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="show link depth-2"><a href="/v5/api/utils/">Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/coder/">AbiCoder</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/formats/">ABI Formats</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/interface/">Interface</a></div><div class="hide link depth-3"><a href="/v5/api/utils/address/">Addresses</a></div><div class="hide link depth-3"><a href="/v5/api/utils/bignumber/">BigNumber</a></div><div class="hide link depth-3"><a href="/v5/api/utils/bytes/">Byte Manipulation</a></div><div class="hide link depth-3"><a href="/v5/api/utils/constants/">Constants</a></div><div class="hide link depth-3"><a href="/v5/api/utils/display-logic/">Display Logic and Input</a></div><div class="hide link depth-3"><a href="/v5/api/utils/encoding/">Encoding Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/fixednumber/">FixedNumber</a></div><div class="hide link depth-3"><a href="/v5/api/utils/hashing/">Hashing Algorithms</a></div><div class="hide link depth-3"><a href="/v5/api/utils/hdnode/">HD Wallet</a></div><div class="hide link depth-3"><a href="/v5/api/utils/logger/">Logging</a></div><div class="hide link depth-3"><a href="/v5/api/utils/properties/">Property Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/signing-key/">Signing Key</a></div><div class="hide link depth-3"><a href="/v5/api/utils/strings/">Strings</a></div><div class="hide link depth-3"><a href="/v5/api/utils/transactions/">Transactions</a></div><div class="hide link depth-3"><a href="/v5/api/utils/web/">Web Utilities</a></div><div class="hide link depth-3"><a href="/v5/api/utils/wordlists/">Wordlists</a></div><div class="show link depth-2"><a href="/v5/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/v5/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/v5/api/other/hardware/">Hardware Wallets</a></div><div class="sho
2020-06-09 23:56:58 -04:00
</div></div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/">API</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/providers/">Providers</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">API Providers</span></div>
<a name="api-providers"></a><a name="api-providers"></a><h1 class="show-anchors"><div>API Providers<div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#api-providers"></a></div></div></h1><p>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.</p>
<p>However, this reliance on third-party services can reduce resiliance, security and increase the amount of required trust. To mitigate these issues, it is recommended you use a <a href="/v5/api/providers/#providers-getDefaultProvider">Default Provider</a>.</p>
2020-07-03 01:54:56 -04:00
<a name="EtherscanProvider"></a><a name="api-providers--EtherscanProvider"></a><h2 class="show-anchors"><div>EtherscanProvider<span class="inherits"> inherits <a href="/v5/api/providers/provider/">Provider</a></span><div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#EtherscanProvider"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/etherscan-provider.ts#L76">source</a></div></div></h2><p>The <b>EtherscanProvider</b> is backed by a combination of the various <a href="https://etherscan.io/apis">Etherscan APIs</a>.</p>
2020-06-09 23:56:58 -04:00
<div class="property show-anchors"><div class="signature"><span class="modifier">new </span><span class="path">ethers</span><span class="symbol">.</span><span class="path">providers</span><span class="symbol">.</span><span class="method">EtherscanProvider</span><span class="symbol">(</span> <span class="symbol">[</span> <span class="param">network</span> = "<span class="param">homestead</span>" <span class="symbol">,</span> <span class="symbol">[</span> <span class="param">apiKey</span> <span class="symbol">]</span> <span class="symbol">]</span> <span class="symbol">)</span><div class="anchors"></div></div><div class="body"><p>Create a new <b>EtherscanProvider</b> connected to <i>network</i> with the optional <i>apiKey</i>.</p>
<p>The <i>network</i> may be specified as <b>string</b> for a common network name, a <b>number</b> for a common chain ID or a [Network Object]provider-(network).</p>
<p>If no <i>apiKey</i> 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 <a href="https://etherscan.io">Etherscan</a> for your own API key.</p>
</div></div><div class="definition container-box note"><div class="term">Note: Default API keys</div><div class="body"><p>If no <i>apiKey</i> is provided, a shared API key will be used, which may result in reduced performance and throttled requests.</p>
<p>It is highly recommended for production, you register with <a href="https://etherscan.io">Etherscan</a> for your own API key.</p>
</div></div><div class="definition"><div class="term"><b>Supported Networks</b></div><div class="body"><p><ul><li>Homestead (Mainnet) </li><li>Ropsten (proof-of-work testnet) </li><li>Rinkeby (proof-of-authority testnet) </li><li>G&ouml;rli (clique testnet) </li><li>Kovan (proof-of-authority testnet) </li></ul></p>
2020-06-12 19:21:37 -04:00
</div></div><div class="code-title"><div>Etherscan Examples</div></div><div class="code"><span class="comment">// Connect to mainnet (homestead)
</span>provider = new EtherscanProvider();
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to rinkeby testnet (these are equivalent)
</span>provider = new EtherscanProvider("rinkeby");
2020-06-09 23:56:58 -04:00
provider = new EtherscanProvider(4);
const network = ethers.providers.getNetwork("rinkeby");
2020-06-12 19:21:37 -04:00
<span class="result ok">// {
</span><span class="result ok">// chainId: 4,
</span><span class="result ok">// ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
</span><span class="result ok">// name: 'rinkeby'
</span><span class="result ok">// }
</span>
2020-06-09 23:56:58 -04:00
provider = new EtherscanProvider(network);
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to mainnet (homestead) with an API key
</span>provider = new EtherscanProvider(null, apiKey);
provider = new EtherscanProvider("homestead", apiKey);
2020-07-03 01:54:56 -04:00
</div><div class="property show-anchors"><div class="signature"><span class="path">provider</span><span class="symbol">.</span><span class="method">getHistory</span><span class="symbol">(</span> <span class="param">address</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Array&lt; History &gt;</span><div class="anchors"><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/etherscan-provider.ts#L306">source</a></div></div><div class="body"><p>@TODO... Explain</p>
2020-06-09 23:56:58 -04:00
2020-07-03 01:54:56 -04:00
</div></div><a name="InfuraProvider"></a><a name="api-providers--InfuraProvider"></a><h2 class="show-anchors"><div>InfuraProvider<span class="inherits"> inherits <a href="/v5/api/providers/other/#UrlJsonRpcProvider">UrlJsonRpcProvider</a></span><div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#InfuraProvider"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/infura-provider.ts#L17">source</a></div></div></h2><p>The <b>InfuraProvider</b> is backed by the popular <a href="https://infura.io">INFURA</a> Ethereum service.</p>
2020-06-09 23:56:58 -04:00
2020-07-03 01:54:56 -04:00
<div class="property show-anchors"><div class="signature"><span class="modifier">new </span><span class="path">ethers</span><span class="symbol">.</span><span class="path">providers</span><span class="symbol">.</span><span class="method">InfuraProvider</span><span class="symbol">(</span> <span class="symbol">[</span> <span class="param">network</span> = "<span class="param">homestead</span>" <span class="symbol">,</span> <span class="symbol">[</span> <span class="param">apiKey</span> <span class="symbol">]</span> <span class="symbol">]</span> <span class="symbol">)</span><div class="anchors"><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/infura-provider.ts#L17">source</a></div></div><div class="body"><p>Create a new <b>InfuraProvider</b> connected to <i>network</i> with the optional <i>apiKey</i>.</p>
2020-06-09 23:56:58 -04:00
<p>The <i>network</i> may be specified as <b>string</b> for a common network name, a <b>number</b> for a common chain ID or a [Network Object]provider-(network).</p>
<p>The <i>apiKey</i> can be a <b>string</b> Project ID or an <b>object</b> with the properties <code class="inline">projectId</code> and <code class="inline">projectSecret</code> to specify a <a href="https://infura.io/docs/gettingStarted/authentication">Project Secret</a> which can be used on non-public sources (like on a server) to further secure your API access and quotas.</p>
2020-07-03 01:54:56 -04:00
</div></div><a name="InfuraProvider-getWebSocketProvider"></a><div class="property show-anchors"><div class="signature"><span class="path">InfuraProvider</span><span class="symbol">.</span><span class="method">getWebSocketProvider</span><span class="symbol">(</span> <span class="symbol">[</span> <span class="param">network</span> <span class="symbol">[</span> <span class="symbol">,</span> <span class="param">apiKey</span> <span class="symbol">]</span> <span class="symbol">]</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns"><a href="/v5/api/providers/other/#WebSocketProvider">WebSocketProvider</a></span><div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#InfuraProvider-getWebSocketProvider"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/infura-provider.ts#L21">source</a></div></div><div class="body"><p>Create a new <a href="/v5/api/providers/other/#WebSocketProvider">WebSocketProvider</a> using the INFURA web-socket endpoint to connect to <i>network</i> with the optional <i>apiKey</i>.</p>
<p>The <i>network</i> and <i>apiKey</i> are specified the same as <a href="/v5/api/providers/api-providers/#InfuraProvider">the constructor</a>.</p>
2020-06-09 23:56:58 -04:00
</div></div><div class="definition container-box note"><div class="term">Note: Default API keys</div><div class="body"><p>If no <i>apiKey</i> is provided, a shared API key will be used, which may result in reduced performance and throttled requests.</p>
<p>It is highly recommended for production, you register with <a href="https://infura.io">INFURA</a> for your own API key.</p>
</div></div><div class="definition"><div class="term"><b>Supported Networks</b></div><div class="body"><p><ul><li>Homestead (Mainnet) </li><li>Ropsten (proof-of-work testnet) </li><li>Rinkeby (proof-of-authority testnet) </li><li>G&ouml;rli (clique testnet) </li><li>Kovan (proof-of-authority testnet) </li></ul></p>
2020-06-12 19:21:37 -04:00
</div></div><div class="code-title"><div>INFURA Examples</div></div><div class="code"><span class="comment">// Connect to mainnet (homestead)
</span>provider = new InfuraProvider();
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to the ropsten testnet
</span><span class="comment">// (see EtherscanProvider above for other network examples)
</span>provider = new InfuraProvider("ropsten");
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to mainnet with a Project ID (these are equivalent)
</span>provider = new InfuraProvider(null, projectId);
2020-06-09 23:56:58 -04:00
provider = new InfuraProvider("homestead", projectId);
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to mainnet with a Project ID and Project Secret
</span>provider = new InfuraProvider("homestead", {
2020-06-09 23:56:58 -04:00
projectId: projectId,
projectSecret: projectSecret
2020-06-12 19:21:37 -04:00
});
2020-07-03 01:54:56 -04:00
<span class="comment">// Connect to the INFURA WebSocket endpoints with a WebSocketProvider
</span>provider = InfuraProvider.getWebSocketProvider()
</div><a name="AlchemyProvider"></a><a name="api-providers--AlchemyProvider"></a><h2 class="show-anchors"><div>AlchemyProvider<span class="inherits"> inherits <a href="/v5/api/providers/other/#UrlJsonRpcProvider">UrlJsonRpcProvider</a></span><div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#AlchemyProvider"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/alchemy-provider.ts#L20">source</a></div></div></h2><p>The <b>AlchemyProvider</b> is backed by <a href="https://alchemyapi.io">Alchemy</a>.</p>
2020-06-09 23:56:58 -04:00
<div class="property show-anchors"><div class="signature"><span class="modifier">new </span><span class="path">ethers</span><span class="symbol">.</span><span class="path">providers</span><span class="symbol">.</span><span class="method">AlchemyProvider</span><span class="symbol">(</span> <span class="symbol">[</span> <span class="param">network</span> = "<span class="param">homestead</span>" <span class="symbol">,</span> <span class="symbol">[</span> <span class="param">apiKey</span> <span class="symbol">]</span> <span class="symbol">]</span> <span class="symbol">)</span><div class="anchors"></div></div><div class="body"><p>Create a new <b>AlchemyProvider</b> connected to <i>network</i> with the optional <i>apiKey</i>.</p>
<p>The <i>network</i> may be specified as <b>string</b> for a common network name, a <b>number</b> for a common chain ID or a <a href="/v5/api/providers/types/#providers-Network">Network Object</a>.</p>
</div></div><div class="definition container-box note"><div class="term">Note: Default API keys</div><div class="body"><p>If no <i>apiKey</i> is provided, a shared API key will be used, which may result in reduced performance and throttled requests.</p>
<p>It is highly recommended for production, you register with <a href="https://alchemyapi.io">Alchemy</a> for your own API key.</p>
</div></div><div class="definition"><div class="term"><b>Supported Networks</b></div><div class="body"><p><ul><li>Homestead (Mainnet) </li><li>Ropsten (proof-of-work testnet) </li><li>Rinkeby (proof-of-authority testnet) </li><li>G&ouml;rli (clique testnet) </li><li>Kovan (proof-of-authority testnet) </li></ul></p>
2020-06-12 19:21:37 -04:00
</div></div><div class="code-title"><div>Alchemy Examples</div></div><div class="code"><span class="comment">// Connect to mainnet (homestead)
</span>provider = new AlchemyProvider();
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to the ropsten testnet
</span><span class="comment">// (see EtherscanProvider above for other network examples)
</span>provider = new AlchemyProvider("ropsten");
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// Connect to mainnet with an API key (these are equivalent)
</span>provider = new AlchemyProvider(null, apiKey);
provider = new AlchemyProvider("homestead", apiKey);
2020-07-03 01:54:56 -04:00
</div><a name="CloudflareProvider"></a><a name="api-providers--CloudflareProvider"></a><h2 class="show-anchors"><div>CloudflareProvider<span class="inherits"> inherits <a href="/v5/api/providers/other/#UrlJsonRpcProvider">UrlJsonRpcProvider</a></span><div class="anchors"><a class="self" href="/v5/api/providers/api-providers/#CloudflareProvider"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/cloudflare-provider.ts#L10">source</a></div></div></h2><p>The CloudflareProvider is backed by the <a href="https://developers.cloudflare.com/distributed-web/ethereum-gateway/">Cloudflare Ethereum Gateway</a>.</p>
2020-06-09 23:56:58 -04:00
<div class="property show-anchors"><div class="signature"><span class="modifier">new </span><span class="path">ethers</span><span class="symbol">.</span><span class="path">providers</span><span class="symbol">.</span><span class="method">CloudflareProvider</span><span class="symbol">(</span> <span class="symbol">)</span><div class="anchors"></div></div><div class="body"><p>Create a new <b>CloudflareProvider</b> connected to mainnet (i.e. "homestead").</p>
</div></div><div class="definition"><div class="term"><b>Supported Networks</b></div><div class="body"><p><ul><li>Homestead (Mainnet) </li></ul></p>
2020-06-12 19:21:37 -04:00
</div></div><div class="code-title"><div>Cloudflare Examples</div></div><div class="code"><span class="comment">// Connect to mainnet (homestead)
</span>provider = new CloudflareProvider();
</div>
2020-06-09 23:56:58 -04:00
<div class="footer">
<div class="nav previous"><a href="/v5/api/providers/jsonrpc-provider/"><span class="arrow">&larr;</span>JsonRpcProvider</a></div>
<div class="nav next"><a href="/v5/api/providers/other/">Other Providers<span class="arrow">&rarr;</span></a></div>
</div>
2020-07-05 00:02:47 -04:00
<div class="copyright">The content of this site is licensed under the <a href="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on July 5, 2020, 12:0am.</div>
2020-06-09 23:56:58 -04:00
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
</body>
</html>