ethers.js/docs/v5/getting-started/index.html

289 lines
27 KiB
HTML
Raw Permalink Normal View History

2020-06-09 23:56:58 -04:00
<!DOCTYPE html>
<html class="paged">
<head>
<title>Getting Started</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
</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-beta</div></a></div>
</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 myself ancestor ancestor 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 show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="hide 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="hide link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="hide link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="hide 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="hide 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="hide link depth-2"><a
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;<span class="current">Getting Started</span></div>
2020-06-12 03:38:55 -04:00
<a name="getting-started"></a><a name="getting-started"></a><h1 class="show-anchors"><div>Getting Started<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started"></a></div></div></h1>
<a name="installing"></a><a name="getting-started--installing"></a><h2 class="show-anchors"><div>Installing<div class="anchors"><a class="self" href="/v5/getting-started/#installing"></a></div></div></h2><p>The various Classes and Functions are available to be imported manually from sub-packages under the <a href="https://www.npmjs.com/search?q=%40ethersproject%2F">@ethersproject</a> organization but for most projects, the umbrella package is the easiest way to get started.</p>
2020-06-09 23:56:58 -04:00
2020-06-12 03:38:55 -04:00
<div class="code">/home/ricmoo&gt; npm install --save ethers@next</div><a name="importing"></a><a name="getting-started--importing"></a><h2 class="show-anchors"><div>Importing<div class="anchors"><a class="self" href="/v5/getting-started/#importing"></a></div></div></h2>
2020-06-09 23:56:58 -04:00
<a name="getting-started--importing--node-js"></a><h3 class="show-anchors"><div>Node.js<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--importing--node-js"></a></div></div></h3>
2020-06-12 19:21:37 -04:00
<div class="code-title"><div>node.js require</div></div><div class="code">const { ethers } = require("ethers");
</div><div class="code-title"><div>ES6 or TypeScript</div></div><div class="code">import { ethers } from "ethers";
</div><a name="getting-started--importing--web-browser"></a><h3 class="show-anchors"><div>Web Browser<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--importing--web-browser"></a></div></div></h3><p>It is generally better practice (for security reasons) to copy the <a href="https://cdn.ethers.io/lib/ethers-5.0.esm.min.js">ethers library</a> to your own webserver and serve it yourself.</p>
2020-06-09 23:56:58 -04:00
<p>For quick demos or prototyping though, it can be loaded in your Web Applications from our CDN.</p>
<div class="code-title"><div>ES6 in the Browser</div></div><div class="code">&lt;script src="https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
type="application/javascipt"&gt;&lt;/script&gt;</div><div class="code-title"><div>ES3 (UMD) in the Browser</div></div><div class="code">&lt;script src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"
2020-06-12 03:38:55 -04:00
type="application/javascipt"&gt;&lt;/script&gt;</div><a name="getting-started--glossary"></a><a name="getting-started--getting-started--glossary"></a><h2 class="show-anchors"><div>Common Terminology<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--glossary"></a></div></div></h2><p>This section needs work...</p>
2020-06-09 23:56:58 -04:00
2020-06-12 03:38:55 -04:00
<table class="table minimal"><tr><td align="left"><b>Provider</b></td><td align="left">A Provider (in ethers) is a class which provides an abstraction for a connection to the Ethereum Network. It provides read-only access to the Blockchain and its status.</td><td class="fix">&nbsp;</td></tr><tr><td align="left"><b>Signer</b></td><td align="left">A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages and transactions to authorize the network to charge your account ether to perform operations.</td><td class="fix">&nbsp;</td></tr><tr><td align="left"><b>Contract</b></td><td align="left">A Contract is an abstraction which represents a connection to a specific contract on the Ethereum Network, so that it can be used like a normal JavaScipt object.</td><td class="fix">&nbsp;</td></tr><tr><td class="table-title" colspan="2">Common Terms</td><td class="fix">&nbsp;</td></tr></table><a name="getting-started--connecting"></a><a name="getting-started--getting-started--connecting"></a><h2 class="show-anchors"><div>Connecting to Ethereum: Metamask<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--connecting"></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 provides:</p>
2020-06-09 23:56:58 -04:00
<p><ul><li>A connection to the Ethereum network (a <a href="/v5/api/providers/provider/">Provider</a>) </li><li>Holds your private key and can sign thing (a <a href="/v5/api/signer/#Signer">Signer</a>) </li></ul></p>
2020-06-12 19:21:37 -04:00
<div class="code-title"><div>Connecting to Metamask</div></div><div class="code"><span class="comment">// A Web3Provider wraps a standard Web3 provider, which is
</span><span class="comment">// what Metamask injects as window.ethereum into each page
</span>const provider = new ethers.providers.Web3Provider(window.ethereum)
<span class="comment">// The Metamask plugin also allows signing transactions to
</span><span class="comment">// send ether and pay to change state within the blockchain.
</span><span class="comment">// For this, we need the account signer...
</span>const signer = provider.getSigner()
</div><a name="getting-started--querying"></a><a name="getting-started--getting-started--connecting--getting-started--querying"></a><h3 class="show-anchors"><div>Querying the Blockchain<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--querying"></a></div></div></h3><p>Once you have a <a href="/v5/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"><span class="comment">// Look up the current block number
</span>provider.getBlockNumber()
<span class="result ok">// { Promise: 10253799 }
</span>
<span class="comment">// Get the balance of an account (by address or ENS name)
</span>balance = await provider.getBalance("ethers.eth")
<span class="result ok">// { BigNumber: "2337132817842795605" }
</span>
<span class="comment">// Often you will need to format the output for the user
</span><span class="comment">// which prefer to see values in ether (instead of wei)
</span>ethers.utils.formatEther(balance)
<span class="result ok">// '2.337132817842795605'
</span>
<span class="comment">// Or if a user enters a string in an input field, you may need
</span><span class="comment">// to convert it from ether (as a string) to wei (as a BigNumber)
</span>ethers.utils.parseEther("1.0")
<span class="result ok">// { BigNumber: "1000000000000000000" }
</span></div><a name="getting-started--sending"></a><a name="getting-started--getting-started--connecting--getting-started--sending"></a><h3 class="show-anchors"><div>Writing to the Blockchain<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--sending"></a></div></div></h3>
<div class="code-title"><div>Sending Ether</div></div><div class="code"><span class="comment">// Send 1 ether to an ens name.
</span>const tx = signer.sendTransaction({
2020-06-09 23:56:58 -04:00
to: "ricmoo.firefly.eth",
value: ethers.utils.parseEther("1.0")
2020-06-12 19:21:37 -04:00
});
</div><a name="getting-started--contracts"></a><a name="getting-started--getting-started--contracts"></a><h2 class="show-anchors"><div>Contracts<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--contracts"></a></div></div></h2><p>A Contract is an abstraction of program code which lives on the Ethereum blockchain.</p>
2020-06-09 23:56:58 -04:00
<p>The <a href="/v5/api/contract/contract/">Contract</a> object makes it easier to use an on-chain Contract as a normal JavaScript object, with the method all mapped to encoding and decoding data for you.</p>
<p>If you are familiar with Databases, this is similar to ORM.</p>
<p>In order to communicate with the Contract on-chain, this class needs to know what methods are available and how to encode and decode the data, which is what the <i>Application Binary Interface</i> (API) provides.</p>
<p>This class is a meta-class, which means its methods are constructed at runtime, when you pass in the ABI to the constructor it uses that to determine which methods to add.</p>
<p>While a on-chain Contract may have many methods available, you can safely ignore any methods you don't need or use, providing a smaller subset of the ABI to the contract.</p>
<p>An ABI often comes from the Solidity or Vyper compiler, but may also be placed in the code easily using the Human-Readable ABI, which the following examples use.</p>
2020-06-12 19:21:37 -04:00
<div class="code-title"><div>Connecting to the DAI Contract</div></div><div class="code"><span class="comment">// We can use an ENS name for the contract address
</span>const daiAddress = "dai.tokens.ethers.eth";
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// The ERC-20 Contract ABI, which is a common contract interface
</span><span class="comment">// for tokens (this is the Human-Readable ABI format)
</span>const daiAbi = [
<span class="comment"> // Some simple details about the token
</span> "function name() view returns (string)",
2020-06-09 23:56:58 -04:00
"function symbol() view returns (string)",
2020-06-12 19:21:37 -04:00
<span class="comment"> // Get the account balance
</span> "function balanceOf(address) view returns (uint)",
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment"> // Send some of your tokens to someone else
</span> "function transfer(address to, uint amount)",
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment"> // An event triggered whenever anyone transfers to someone else
</span> "event Transfer(address indexed from, address indexed to, uint amount)"
2020-06-09 23:56:58 -04:00
];
2020-06-12 19:21:37 -04:00
<span class="comment">// The Contract object
</span>const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);
</div><a name="getting-started--reading"></a><a name="getting-started--getting-started--contracts--getting-started--reading"></a><h3 class="show-anchors"><div>Read-Only Methods<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--reading"></a></div></div></h3>
<div class="code-title"><div>Querying the DAI Contract</div></div><div class="code"><span class="comment">// Get the ERC-20 token name
</span>daiContract.name()
<span class="result ok">// { Promise: 'Dai Stablecoin' }
</span>
<span class="comment">// Get the ERC-20 token synbol (for tickers and UIs)
</span>daiContract.symbol()
<span class="result ok">// { Promise: 'DAI' }
</span>
<span class="comment">// Get the balance of an address
</span>balance = await daiContract.balanceOf("ricmoo.firefly.eth")
<span class="result ok">// { BigNumber: "7712595125722568213383" }
</span>
<span class="comment">// Format the DAI for displaying to the user
</span>ethers.utils.formatUnits(balance, 18)
<span class="result ok">// '7712.595125722568213383'
</span></div><a name="getting-started--writing"></a><a name="getting-started--getting-started--contracts--getting-started--writing"></a><h3 class="show-anchors"><div>State Changing Methods<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--writing"></a></div></div></h3>
<div class="code-title"><div>Sending DAI</div></div><div class="code"><span class="comment">// The DAI Contract is currently connected to the Provider,
</span><span class="comment">// which is read-only. We need to connect to a Signer, so
</span><span class="comment">// that we can pay to send state-changing transactions.
</span>const daiWithSigner = contract.connect(signer);
<span class="comment">// Each DAI has 18 decimal places
</span>const dai = ethers.utils.parseUnits("1.0", 18);
<span class="comment">// Send 1 DAI to "ricmoo.firefly.eth"
</span>tx = daiWithSigner.transfer("ricmoo.firefly.eth", dai);
</div><a name="getting-started--events"></a><a name="getting-started--getting-started--contracts--getting-started--events"></a><h3 class="show-anchors"><div>Listening to Events<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--events"></a></div></div></h3>
<div class="code-title"><div>Listening to Events</div></div><div class="code"><span class="comment">// Receive an event when ANY transfer occurs
</span>daiContract.on("Transfer", (from, to, amount, event) =&gt; {
2020-06-09 23:56:58 -04:00
console.log(`${ from } sent ${ formatEther(amount) } to ${ to}`);
2020-06-12 19:21:37 -04:00
<span class="comment"> // The event object contains the verbatim log data, the
</span><span class="comment"> // EventFragment and functions to fetch the block,
</span><span class="comment"> // transaction and receipt and event functions
</span>});
2020-06-09 23:56:58 -04:00
2020-06-12 19:21:37 -04:00
<span class="comment">// A filter for when a specific address receives tokens
</span>myAddress = "0x8ba1f109551bD432803012645Ac136ddd64DBA72";
2020-06-09 23:56:58 -04:00
filter = daiContract.filters.Transfer(null, myAddress)
2020-06-12 19:21:37 -04:00
<span class="result ok">// {
</span><span class="result ok">// address: 'dai.tokens.ethers.eth',
</span><span class="result ok">// topics: [
</span><span class="result ok">// '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
</span><span class="result ok">// null,
</span><span class="result ok">// '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
</span><span class="result ok">// ]
</span><span class="result ok">// }
</span>
<span class="comment">// Receive an event when that filter occurs
</span>daiContract.on(filter, (from, to, amount, event) =&gt; {
<span class="comment"> // The to will always be "address"
</span> console.log(`I got ${ formatEther(amount) } from ${ from }.`);
2020-06-09 23:56:58 -04:00
});
2020-06-12 19:21:37 -04:00
</div><a name="getting-started--history"></a><a name="getting-started--getting-started--contracts--getting-started--history"></a><h3 class="show-anchors"><div>Query Historic Events<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--history"></a></div></div></h3>
<div class="code-title"><div>Filtering Historic Events</div></div><div class="code"><span class="comment">// Get the address of the Signer
</span>myAddress = await signer.getAddress()
<span class="result ok">// '0x8ba1f109551bD432803012645Ac136ddd64DBA72'
</span>
<span class="comment">// Filter for all token transfers to me
</span>filterFrom = daiContract.filters.Transfer(myAddress, null);
<span class="result ok">// {
</span><span class="result ok">// address: 'dai.tokens.ethers.eth',
</span><span class="result ok">// topics: [
</span><span class="result ok">// '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
</span><span class="result ok">// '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
</span><span class="result ok">// ]
</span><span class="result ok">// }
</span>
<span class="comment">// Filter for all token transfers from me
</span>filterTo = daiContract.filters.Transfer(null, myAddress);
<span class="result ok">// {
</span><span class="result ok">// address: 'dai.tokens.ethers.eth',
</span><span class="result ok">// topics: [
</span><span class="result ok">// '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
</span><span class="result ok">// null,
</span><span class="result ok">// '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
</span><span class="result ok">// ]
</span><span class="result ok">// }
</span>
<span class="comment">// List all transfers sent from me a specific block range
</span>daiContract.queryFilter(filterFrom, 9843470, 9843480)
<span class="result ok">// { Promise: [
</span><span class="result ok">// {
</span><span class="result ok">// address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
</span><span class="result ok">// args: [
</span><span class="result ok">// '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
</span><span class="result ok">// '0x8B3765eDA5207fB21690874B722ae276B96260E0',
</span><span class="result ok">// { BigNumber: "4750000000000000000" }
</span><span class="result ok">// ],
</span><span class="result ok">// blockHash: '0x8462eb2fbcef5aa4861266f59ad5f47b9aa6525d767d713920fdbdfb6b0c0b78',
</span><span class="result ok">// blockNumber: 9843476,
</span><span class="result ok">// data: '0x00000000000000000000000000000000000000000000000041eb63d55b1b0000',
</span><span class="result ok">// decode: [Function],
</span><span class="result ok">// event: 'Transfer',
</span><span class="result ok">// eventSignature: 'Transfer(address,address,uint256)',
</span><span class="result ok">// getBlock: [Function],
</span><span class="result ok">// getTransaction: [Function],
</span><span class="result ok">// getTransactionReceipt: [Function],
</span><span class="result ok">// logIndex: 69,
</span><span class="result ok">// removeListener: [Function],
</span><span class="result ok">// removed: false,
</span><span class="result ok">// topics: [
</span><span class="result ok">// '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
</span><span class="result ok">// '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72',
</span><span class="result ok">// '0x0000000000000000000000008b3765eda5207fb21690874b722ae276b96260e0'
</span><span class="result ok">// ],
</span><span class="result ok">// transactionHash: '0x1be23554545030e1ce47391a41098a46ff426382ed740db62d63d7676ff6fcf1',
</span><span class="result ok">// transactionIndex: 81
</span><span class="result ok">// },
</span><span class="result ok">// {
</span><span class="result ok">// address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
</span><span class="result ok">// args: [
</span><span class="result ok">// '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
</span><span class="result ok">// '0x00De4B13153673BCAE2616b67bf822500d325Fc3',
</span><span class="result ok">// { BigNumber: "250000000000000000" }
</span><span class="result ok">// ],
</span><span class="result ok">// blockHash: '0x8462eb2fbcef5aa4861266f59ad5f47b9aa6525d767d713920fdbdfb6b0c0b78',
</span><span class="result ok">// blockNumber: 9843476,
</span><span class="result ok">// data: '0x00000000000000000000000000000000000000000000000003782dace9d90000',
</span><span class="result ok">// decode: [Function],
</span><span class="result ok">// event: 'Transfer',
</span><span class="result ok">// eventSignature: 'Transfer(address,address,uint256)',
</span><span class="result ok">// getBlock: [Function],
</span><span class="result ok">// getTransaction: [Function],
</span><span class="result ok">// getTransactionReceipt: [Function],
</span><span class="result ok">// logIndex: 70,
</span><span class="result ok">// removeListener: [Function],
</span><span class="result ok">// removed: false,
</span><span class="result ok">// topics: [
</span><span class="result ok">// '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
</span><span class="result ok">// '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72',
</span><span class="result ok">// '0x00000000000000000000000000de4b13153673bcae2616b67bf822500d325fc3'
</span><span class="result ok">// ],
</span><span class="result ok">// transactionHash: '0x1be23554545030e1ce47391a41098a46ff426382ed740db62d63d7676ff6fcf1',
</span><span class="result ok">// transactionIndex: 81
</span><span class="result ok">// }
</span><span class="result ok">// ] }
</span>
2020-06-09 23:56:58 -04:00
//
2020-06-12 19:21:37 -04:00
<span class="comment">// The following have had the results omitted due to the
</span><span class="comment">// number of entries; but they provide some useful examples
</span>//
<span class="comment">// List all transfers I sent in the last 10,000 blocks
</span>daiContract.queryFilter(filterFrom, -10000)
<span class="comment">// List all transfers ever sent to me
</span>daiContract.queryFilter(filterTo)
</div><a name="getting-started--signing"></a><a name="getting-started--getting-started--signing"></a><h2 class="show-anchors"><div>Signing Messages<div class="anchors"><a class="self" href="/v5/getting-started/#getting-started--signing"></a></div></div></h2>
<div class="code-title"><div>Signing Messages</div></div><div class="code"><span class="comment">// To sign a simple string, which can often be used for
</span><span class="comment">// logging into a service, such as CryptoKitties simply
</span><span class="comment">// pass the string in.
</span>signature = await signer.signMessage("Hello World");
<span class="result ok">// '0xe4f2a4e1a867a4757bd1ef7d0d5b9bf552c013f4598356e9326106d74d47292412efc81cd3b97a7e298f1b78b973c376d32ae0bdca811514e1258360a00c1bb71c'
</span>
2020-06-09 23:56:58 -04:00
//
2020-06-12 19:21:37 -04:00
<span class="comment">// A common case is also signing a hash, which is 32
</span><span class="comment">// bytes. It is important to note, that to sign binary
</span><span class="comment">// data it MUST be an Array (or TypedArray)
</span>//
<span class="comment">// This string is 66 chacacters long
</span>message = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
<span class="comment">// This array representation is 32 bytes long
</span>messageBytes = ethers.utils.arrayify(message);
<span class="result ok">// Uint8Array [ 221, 242, 82, 173, 27, 226, 200, 155, 105, 194, 176, 104, 252, 55, 141, 170, 149, 43, 167, 241, 99, 196, 161, 22, 40, 245, 90, 77, 245, 35, 179, 239 ]
</span>
<span class="comment">// To sign a hash, you most often want to sign the bytes
</span>signature = await signer.signMessage(messageBytes)
<span class="result ok">// '0x4542871f6c9b776c7ca4f23d0435b4e98eaf9fc25060162bf1c43420d8ce202c2d12a5e1c1dbbbde145e5ee8b1c243c931d3e2d93394f73081800155b2cc480b1c'
</span></div>
2020-06-09 23:56:58 -04:00
<div class="footer">
<div class="nav previous"><a href="/v5/"><span class="arrow">&larr;</span>Documentation</a></div>
<div class="nav next"><a href="/v5/concepts/">Ethereum Basics<span class="arrow">&rarr;</span></a></div>
</div>
2020-06-12 19:21:37 -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>. Last modified on June 12, 2020, 7:17pm.</div>
2020-06-09 23:56:58 -04:00
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
</body>
</html>