ethers.js/docs/v5/api/providers/api-providers/index.html
2021-02-08 15:26:10 -05:00

149 lines
22 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>API Providers</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
<meta property="og:title" content="API Providers"/>
<meta property="og:description" content="Documentation for ethers, a complete, tiny and simple Ethereum library."/>
<meta property="og:image" content="/v5/static/social.jpg"/>
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0</div></a></div>
<div class="search"><form action="/v5/search/" method="GET"><input name="search" id="search" /></form><span class="search-icon">&#9906;</span></div>
</div>
<div class="toc"><div>
<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="hide link depth-2"><a href="/v5/concepts/best-practices/">Best Practices</a></div><div class="base show link depth-1"><a href="/v5/api-keys/">Provider API Keys</a></div><div class="base ancestor show link depth-1"><a href="/v5/api/">Application Programming Interface</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="link show child depth-4"><a href="#EtherscanProvider">EtherscanProvider</a></div><div class="link show child depth-4"><a href="#InfuraProvider">InfuraProvider</a></div><div class="link show child depth-4"><a href="#AlchemyProvider">AlchemyProvider</a></div><div class="link show child depth-4"><a href="#CloudflareProvider">CloudflareProvider</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/signer/">Signers</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/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="show link depth-2"><a href="/v5/api/experimental/">Experimental</a></div><div class="base show link depth-1"><a href="/v5/cli/">Command Line Interfaces</a></div><div class="hide link depth-2"><a href="/v5/cli/ethers/">Sandbox Utility</a></div><div class="hide link depth-2"><a href="/v5/cli/asm/">Assembler</a></div><div class="hide link depth-2"><a href="/v5/cli/ens/">Ethereum Naming Service</a></div><div class="hide link depth-2"><a href="/v5/cli/typescript/">TypeScript</a></div><div class="hide link depth-2"><a href="/v5/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/v5/cookbook/">Cookbook</a></div><div class="hide link depth-2"><a href="/v5/cookbook/react-native/">React Native (and ilk)</a></div><div class="base show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/v5/testing/">Testing</a></div><div class="base show link depth-1"><a href="/v5/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/v5/other-resources/">Other Resources</a></div><div class="base show link depth-1"><a href="/v5/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/v5/license/">License and Copyright</a></div>
</div></div>
<div class="footer">
<a href="/v5/single-page/">Single Page</a>
</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 resilience, 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>
<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#L142">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>
<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 a <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>
</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();
<span class="comment">// Connect to rinkeby testnet (these are equivalent)
</span>provider = new EtherscanProvider("rinkeby");
provider = new EtherscanProvider(4);
const network = ethers.providers.getNetwork("rinkeby");
<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>
provider = new EtherscanProvider(network);
<span class="comment">// Connect to mainnet (homestead) with an API key
</span>provider = new EtherscanProvider(null, apiKey);
provider = new EtherscanProvider("homestead", apiKey);
</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#L392">source</a></div></div><div class="body"><p>@TODO... Explain</p>
</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#L46">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>
<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#L46">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>
<p>The <i>network</i> may be specified as a <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>
</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#L50">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>
</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>
</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();
<span class="comment">// Connect to the ropsten testnet
</span><span class="comment">// (see EtherscanProvider above for other network examples)
</span>provider = new InfuraProvider("ropsten");
<span class="comment">// Connect to mainnet with a Project ID (these are equivalent)
</span>provider = new InfuraProvider(null, projectId);
provider = new InfuraProvider("homestead", projectId);
<span class="comment">// Connect to mainnet with a Project ID and Project Secret
</span>provider = new InfuraProvider("homestead", {
projectId: projectId,
projectSecret: projectSecret
});
<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#L41">source</a></div></div></h2><p>The <b>AlchemyProvider</b> is backed by <a href="https://alchemyapi.io">Alchemy</a>.</p>
<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 a <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>
</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();
<span class="comment">// Connect to the ropsten testnet
</span><span class="comment">// (see EtherscanProvider above for other network examples)
</span>provider = new AlchemyProvider("ropsten");
<span class="comment">// Connect to mainnet with an API key (these are equivalent)
</span>provider = new AlchemyProvider(null, apiKey);
provider = new AlchemyProvider("homestead", apiKey);
<span class="comment">// Connect to the Alchemy WebSocket endpoints with a WebSocketProvider
</span>provider = AlchemyProvider.getWebSocketProvider()
</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>
<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>
</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>
<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>
<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 February 8, 2021, 3:25pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
<!--EXTRASCRIPT-->
</body>
</html>