ethers.js/docs/quick-start/index.html
2020-06-09 23:56:58 -04:00

143 lines
12 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Quick Start</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0-beta</div></a></div>
</div>
<div class="toc"><div>
<div class="link title"><a href="/">Documentation</a></div><div class="base show link depth-1"><a href="/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/concepts/security/">Security</a></div><div class="base show link depth-1"><a href="/api/">Application Programming Interface</a></div><div class="hide link depth-2"><a href="/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/api/contract/example/">Example: ERC-20 Contract</a></div><div class="hide link depth-2"><a href="/api/signer/">Signers</a></div><div class="hide link depth-2"><a href="/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/api/providers/types/">Types</a></div><div class="hide link depth-2"><a href="/api/utils/">Utilities</a></div><div class="hide link depth-3"><a href="/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/api/utils/abi/interface/">Interface</a></div><div class="hide link depth-4"><a href="/api/utils/abi/fragments/">Fragments</a></div><div class="hide link depth-3"><a href="/api/utils/address/">Addresses</a></div><div class="hide link depth-3"><a href="/api/utils/bignumber/">BigNumber</a></div><div class="hide link depth-3"><a href="/api/utils/bytes/">Byte Manipulation</a></div><div class="hide link depth-3"><a href="/api/utils/constants/">Constants</a></div><div class="hide link depth-3"><a href="/api/utils/display-logic/">Display Logic and Input</a></div><div class="hide link depth-3"><a href="/api/utils/encoding/">Encoding Utilities</a></div><div class="hide link depth-3"><a href="/api/utils/fixednumber/">FixedNumber</a></div><div class="hide link depth-3"><a href="/api/utils/hashing/">Hashing Algorithms</a></div><div class="hide link depth-3"><a href="/api/utils/hdnode/">HD Wallet</a></div><div class="hide link depth-3"><a href="/api/utils/logger/">Logging</a></div><div class="hide link depth-3"><a href="/api/utils/properties/">Property Utilities</a></div><div class="hide link depth-3"><a href="/api/utils/signing-key/">Signing Key</a></div><div class="hide link depth-3"><a href="/api/utils/strings/">Strings</a></div><div class="hide link depth-3"><a href="/api/utils/transactions/">Transactions</a></div><div class="hide link depth-3"><a href="/api/utils/web/">Web Utilities</a></div><div class="hide link depth-3"><a href="/api/utils/wordlists/">Wordlists</a></div><div class="hide link depth-2"><a href="/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/api/other/hardware/">Hardware Wallets</a></div><div class="hide link depth-2"><a href="/api/experimental/">Experimental</a></div><div class="base show link depth-1"><a href="/cli/">Command Line Interfaces</a></div><div class="hide link depth-2"><a href="/cli/ethers/">Sandbox Utility</a></div><div class="hide link depth-2"><a href="/cli/asm/">Assembler</a></div><div class="hide link depth-2"><a href="/cli/ens/">Ethereum Naming Service</a></div><div class="hide link depth-2"><a href="/cli/typescript/">TypeScript</a></div><div class="hide link depth-2"><a href="/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/cookbook/">Cookbook</a></div><div class="base show link depth-1"><a href="/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/testing/">Testing</a></div><div class="base show link depth-1"><a href="/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/license/">License and Copyright</a></div>
</div></div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Quick Start</span></div>
<a name="quick-start"></a><h1 class="show-anchors"><div>Quick Start<div class="anchors"><a class="self" href="/quick-start/#quick-start"></a></div></div></h1>
<a name="quick-start--connecting-to-ethereum-metamask"></a><h2 class="show-anchors"><div>Connecting to Ethereum: Metamask<div class="anchors"><a class="self" href="/quick-start/#quick-start--connecting-to-ethereum-metamask"></a></div></div></h2><p>The quickest and easiest way to experiment and begin developing on Ethereum is to use <a href="https://metamask.io/">Metamask</a>, which is a browser extension that provider:</p>
<p><ul><li>A connection to the Ethereum network </li><li>Holds your private key and can sign thing </li></ul></p>
<div class="code-title"><div>Connecting to Metamask</div></div><div class="code">// A Web3Provider wraps a standard Web3 provider, which is
// what Metamask injects into every page you visit as window.ethereum
const provider = new ethers.providers.Web3Provider(window.ethereum)
// The Metamask plugin also allows signing transactions to send ether
// and pay to change state within the blockchain. For this, we need
// the account signer...
const signer = provider.getSigner()</div><a name="quick-start--connecting-to-ethereum-metamask--querying-the-blockchain"></a><h3 class="show-anchors"><div>Querying the Blockchain<div class="anchors"><a class="self" href="/quick-start/#quick-start--connecting-to-ethereum-metamask--querying-the-blockchain"></a></div></div></h3><p>Once you have a <a href="/api/providers/provider/">Provider</a>, you have a read-only connection to the blockchain, which can be used to query the current state, fetch historic logs, look up deployed code and so on.</p>
<div class="code-title"><div>Basic Queries</div></div><div class="code">// &lt;hide&gt;
const provider = ethers.getDefaultProvider();
// &lt;/hide&gt;
// Look up the current block number
provider.getBlockNumber()
// Get the balance of an account (by address or ENS name)
provider.getBalance("ethers.eth")</div><a name="quick-start--connecting-to-ethereum-metamask--writing-to-the-blockchain"></a><h3 class="show-anchors"><div>Writing to the Blockchain<div class="anchors"><a class="self" href="/quick-start/#quick-start--connecting-to-ethereum-metamask--writing-to-the-blockchain"></a></div></div></h3><p>Every write costs ... etc</p>
<div class="code-title"><div>Sending Ether</div></div><div class="code">// Send 1 ether to an ens name.
const tx = signer.sendTransaction({
to: "ricmoo.firefly.eth",
value: ethers.utils.parseEther("1.0")
});</div><a name="quick-start--contracts"></a><h2 class="show-anchors"><div>Contracts<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts"></a></div></div></h2>
<a name="quick-start--contracts--connecting-to-a-contract"></a><h3 class="show-anchors"><div>Connecting to a Contract<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts--connecting-to-a-contract"></a></div></div></h3><p>To connect to an contract...</p>
<p>Explain ABI</p>
<p>Explain meta-class and the aBI</p>
<div class="code-title"><div>Connecting to a Contract</div></div><div class="code">// The ERC-20 Contract ABI, which is a common contract interface
// for tokens.
const abi = [
// Some simple details about the token
"function namd() view returns (string)",
"function symbol() view returns (string)",
// Get the account balance
"function balanceOf(address) view returns (uint)",
// Send some of your tokens to someone else
"function transfer(address to, uint amount")",
// An event triggered whenever anyone transfers to someone else
"event Transfer(address indexed from, address indexed to, uint amount)"
];
const contract = new ethers.Contract(address, abi, provider);
const contract = new ethers.Contract(address, abi, provider);</div><a name="quick-start--contracts--read-only-methods"></a><h3 class="show-anchors"><div>Read-Only Methods<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts--read-only-methods"></a></div></div></h3>
<div class="code-title"><div>Querying the DAI Contract</div></div><div class="code">// &lt;hide&gt;
// &lt;/hide&gt;
const contract = new Contract("dai.tokens.ethers.eth", abi, provider);
contract.name()
//!
contract.symbol()
//!
contract.balanceOf("ricmoo.firefly.eth")
//!</div><a name="quick-start--contracts--state-changing-methods"></a><h3 class="show-anchors"><div>State Changing Methods<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts--state-changing-methods"></a></div></div></h3>
<a name="quick-start--contracts--listening-to-events"></a><h3 class="show-anchors"><div>Listening to Events<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts--listening-to-events"></a></div></div></h3>
<div class="code-title"><div>Listening to Events</div></div><div class="code">// &lt;hide&gt;
const contract = ...
// &lt;/hide&gt;
// Receive an event when ANY transfer occurs
contract.on("Transfer", (from, to, amount, event) =&gt; {
console.log(`${ from } sent ${ formatEther(amount) } to ${ to}`);
});
// Receive an event when a specific address receives a token
const filter = contract.filters.Transfer(null, address)
contract.on(filter, (from, to, amount, event) =&gt; {
// The to will always be "address"
console.log(`I got ${ formatEther(amount) } from ${ from }.`);
});</div><a name="quick-start--contracts--listing-historic-events"></a><h3 class="show-anchors"><div>Listing Historic Events<div class="anchors"><a class="self" href="/quick-start/#quick-start--contracts--listing-historic-events"></a></div></div></h3>
<div class="code-title"><div>Historic Events by Name</div></div><div class="code">// List all transfers from anyone to anyone in the first XXX
contract.queryFilter("Transfer", 0, XXX)
//!</div><div class="code-title"><div>Filtering Historic Events</div></div><div class="code">// &lt;hide&gt;
const contract = ...
// &lt;/hide&gt;
const address = signer.getAddress()
// Filter for all token transfers we've sent
const filterFrom = contract.filter.Transfer(address, null);
// &lt;hide&gt;
filterFrom
// &lt;/hide&gt;
//!
// Filter for all token transfers we've received
const filterTo = contract.filter.Transfer(null, address);
// &lt;hide&gt;
filterTo
// &lt;/hide&gt;
//!
// List all transfers we've sent between ...
contract.queryFilter(filterFrom, 0, 100000)
//!
// List all transfers we've received in blockhash XXX
contract.queryFilter(filterTo, XXX)
//!</div><a name="quick-start--signing-messages"></a><h2 class="show-anchors"><div>Signing Messages<div class="anchors"><a class="self" href="/quick-start/#quick-start--signing-messages"></a></div></div></h2>
<div class="footer">
<div class="nav previous"><!--PREV_LINK--></div>
<div class="nav next"><!--NEXT_LINK--></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 May 14, 2020, 12:50am.</div>
</div>
<script src="/static/script.js" type="text/javascript"></script>
</body>
</html>