ethers.js/docs/migration/ethers-v4/index.html

45 lines
25 KiB
HTML
Raw Normal View History

2020-02-18 01:56:50 +03:00
<html><head><title>ethers v4 to v5</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="hide link depth-2"><a href="/getting-started/#installing">Installing</a></div><div class="hide link depth-2"><a href="/getting-started/#importing">Importing</a></div><div class="base show link depth-1"><a href="/concepts/">Concepts</a></div><div class="hide link depth-2"><a href="/concepts/events/">Events</a></div><div class="hide link depth-3"><a href="/concepts/events/#solidity-topics">Solidity Topics</a></div><div class="hide link depth-2"><a href="/concepts/gas/">Gas</a></div><div class="hide link depth-3"><a href="/concepts/gas/#gas-price">Gas Price</a></div><div class="hide link depth-3"><a href="/concepts/gas/#gas-limit">Gas Limit</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/">Contracts</a></div><div class="hide link depth-3"><a href="/api/contract/#buckets">Buckets</a></div><div class="hide link depth-2"><a href="/api/signer/">Signers</a></div><div class="hide link depth-3"><a href="/api/signer/#signer">Signer</a></div><div class="hide link depth-3"><a href="/api/signer/#wallet">Wallet</a></div><div class="hide link depth-3"><a href="/api/signer/#voidsigner">VoidSigner</a></div><div class="hide link depth-3"><a href="/api/signer/#externallyownedaccount">ExternallyOwnedAccount</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-4"><a href="/api/providers/provider/#accounts-methods">Accounts Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#blocks-methods">Blocks Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#ethereum-naming-service-(ens)-methods">Ethereum Naming Service (ENS) Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#logs-methods">Logs Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#network-status-methods">Network Status Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#transactions-methods">Transactions Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#event-emitter-methods">Event Emitter Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#inspection-methods">Inspection Methods</a></div><div class="hide link depth-3"><a href="/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-4"><a href="/api/providers/jsonrpc-provider/#jsonrpcsigner">JsonRpcSigner</a></div><div class="hide link depth-4"><a href="/api/providers/jsonrpc-provider/#jsonrpcuncheckedsigner">JsonRpcUncheckedSigner</a></div><div class="hide link depth-3"><a href="/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#etherscanprovider">EtherscanProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#infuraprovider">InfuraProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#alchemyprovider">AlchemyProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#cloudfrontprovider">CloudfrontProvider</a></div><div class="hide link depth-3"><a href="/api/providers/other/">Other Providers</a></div><div class="hide link depth-4"><a href="/api/providers/other/#fallbackprovider">FallbackProvider</a></div><div class="hide link depth-4"><a href="/api/providers/other/#ipcprovider">IpcProvider</a></div><div class="hide
<a name="migration:-from-ethers-v4"></a><h1 class="show-anchors"><div>Migration: From Ethers v4<div class="anchors"><a class="self" href="#migration:-from-ethers-v4"></a></div></div></h1>
<a name="bignumber"></a><h2 class="show-anchors"><div>BigNumber<div class="anchors"><a class="self" href="#bignumber"></a></div></div></h2>
<a name="namespace"></a><h3 class="show-anchors"><div>Namespace<div class="anchors"><a class="self" href="#namespace"></a></div></div></h3>
<p>Since <a href="/api/utils/bignumber/">BigNumber</a> is used quite frequently, it has been moved to the top level of the umbrella package.</p>
<div class="code">//&nbsp;v4<br>ethers.utils.BigNumber<br>ethers.utils.BigNumberish<br><br>//&nbsp;v5<br>ethers.BigNumber<br>ethers.BigNumberish</div>
<a name="creating-instances"></a><h3 class="show-anchors"><div>Creating Instances<div class="anchors"><a class="self" href="#creating-instances"></a></div></div></h3>
<p>The <code class="inline">bigNumberify</code> method was always preferred over the constructor since it could short-circuit an object instantiation for [[bignumber] objects (since they are immutable). This has been moved to a static <code class="inline">from</code> class method.</p>
<div class="code">//&nbsp;v4<br>new&nbsp;ethers.utils.BigNumber(someValue)<br>ethers.utils.bigNumberify(someValue);<br><br>//&nbsp;v5<br>//&nbsp;-&nbsp;Constructor&nbsp;is&nbsp;private<br>//&nbsp;-&nbsp;Removed&nbsp;`bigNumberify`<br>ethers.BigNumber.from(someValue)</div>
<a name="contracts"></a><h2 class="show-anchors"><div>Contracts<div class="anchors"><a class="self" href="#contracts"></a></div></div></h2>
<div class="code">TODO</div>
<a name="errors"></a><h2 class="show-anchors"><div>Errors<div class="anchors"><a class="self" href="#errors"></a></div></div></h2>
<a name="namespace"></a><h3 class="show-anchors"><div>Namespace<div class="anchors"><a class="self" href="#namespace"></a></div></div></h3>
<p>All errors now belong to the <a href="/api/utils/logger/">Logger</a> class and the related functions have been moved to <a href="/api/utils/logger/">Logger</a> instances, which can include a per-package version string.</p><p>Global error fucntions have been moved <a href="/api/utils/logger/">Logger</a> class methods.</p>
<div class="code">//&nbsp;v4<br>ethers.errors.UNKNOWN_ERROR<br>ethers.errors.*<br><br>errors.setCensorship(censorship,&nbsp;permanent)<br>errors.setLogLevel(logLevel)<br><br>errors.checkArgumentCount(count,&nbsp;expectedCount,&nbsp;suffix)<br>errors.checkNew(self,&nbsp;kind)<br>errors.checkNormalize()<br>errors.throwError(message,&nbsp;code,&nbsp;params)<br>errors.warn(...)<br>errors.info(...)<br><br>//&nbsp;v5<br>ethers.utils.Logger.errors.UNKNOWN_ERROR<br>ethers.utils.Logger.errors.*<br><br>Logger.setCensorship(censorship,&nbsp;permanent)<br>Logger.setLogLevel(logLevel)<br><br>const&nbsp;logger&nbsp;=&nbsp;new&nbsp;ethers.utils.Logger(version);<br>logger.checkArgumentCount(count,&nbsp;expectedCount,&nbsp;suffix)<br>logger.checkNew(self,&nbsp;kind)<br>logger.checkNormalize()<br>logger.throwError(message,&nbsp;code,&nbsp;params)<br>logger.warn(...)<br>logger.info(...)</div>
<a name="interface"></a><h2 class="show-anchors"><div>Interface<div class="anchors"><a class="self" href="#interface"></a></div></div></h2>
<p>The <a href="/api/utils/abi/#abi-interface">Interface</a> object has undergone the most dramatic changes.</p><p>It is no longer a meta-class and now has methods that simplify handling contract interface operations without the need for object inspection and special edge cases.</p>
<a name="functions"></a><h3 class="show-anchors"><div>Functions<div class="anchors"><a class="self" href="#functions"></a></div></div></h3>
<div class="code">//&nbsp;v4&nbsp;(example:&nbsp;"transfer(address&nbsp;to,&nbsp;uint&nbsp;amount)")<br>interface.functions.transfer.encode(to,&nbsp;amount)<br>interface.functions.transfer.decode(callData)<br><br>//&nbsp;v5<br>interface.encodeData("transfer",&nbsp;[&nbsp;to,&nbsp;amount&nbsp;])<br>interface.decodeResult("transfer",&nbsp;data)<br><br>//&nbsp;Or&nbsp;you&nbsp;can&nbsp;use&nbsp;any&nbsp;compatible&nbsp;signature&nbsp;or&nbsp;Fragment&nbsp;objects.<br>//&nbsp;Notice&nbsp;that&nbsp;signature&nbsp;normalization&nbsp;is&nbsp;performed&nbsp;for&nbsp;you,<br>//&nbsp;e.g.&nbsp;"uint"&nbsp;and&nbsp;"uint256"&nbsp;will&nbsp;be&nbsp;automatically&nbsp;converted<br>interface.encodeData("transfer(address,uint)",&nbsp;[&nbsp;to,&nbsp;amount&nbsp;])<br>interface.decodeResult("transfer(address&nbsp;to,&nbsp;uint256&nbsp;amount)",&nbsp;data)</div>
<a name="events"></a><h3 class="show-anchors"><div>Events<div class="anchors"><a class="self" href="#events"></a></div></div></h3>
<div class="code">//&nbsp;v4&nbsp;(example:&nbsp;Transfer(address&nbsp;indexed,&nbsp;address&nbsp;indexed,&nbsp;uint256)<br>interface.events.Transfer.encodeTopics(values)<br>interface.events.Transfer.decode(data,&nbsp;topics)<br><br>//&nbsp;v5<br>interface.encodeFilterTopics("Transfer",&nbsp;values)<br>interface.encodeEventLog("Transfer",&nbsp;data,&nbsp;topics)</div>
<a name="inspection"></a><h3 class="show-anchors"><div>Inspection<div class="anchors"><a class="self" href="#inspection"></a></div></div></h3>
<p>Interrogating properties about a function or event can now (mostly) be done directly on the <a href="/api/utils/abi/#abi-fragment">Fragment</a> object.</p>
<div class="code">//&nbsp;v4<br>interface.functions.transfer.name<br>interface.functions.transfer.inputs<br>interface.functions.transfer.outputs<br>interface.functions.transfer.payable<br>interface.functions.transfer.gas<br><br>//&nbsp;v5<br>const&nbsp;functionFragment&nbsp;=&nbsp;interface.getFunction("transfer")<br>functionFragment.name<br>functionFragment.inputs<br>functionFragment.outputs<br>functionFragment.payable<br>functionFragment.gas<br><br><br>//&nbsp;v4;&nbsp;type&nbsp;is&nbsp;"call"&nbsp;or&nbsp;"transaction"<br>interface.functions.transfer.type<br><br>//&nbsp;v5;&nbsp;constant&nbsp;is&nbsp;true&nbsp;(i.e.&nbsp;"call")&nbsp;or&nbsp;false&nbsp;(i.e.&nbsp;"transaction")<br>functionFragment.constant<br><br><br>//&nbsp;v4<br>interface.events.Transfer.anonymous<br>interface.events.Transfer.inputs<br>interface.events.Transfer.name<br><br>//&nbsp;v5<br>const&nbsp;eventFragment&nbsp;=&nbsp;interface.getEvent("Transfer");<br>eventFragment.anonymous<br>eventFragment.inputs<br>eventFragment.name<br><br><br>//&nbsp;v4<br>const&nbsp;functionSig&nbsp;=&nbsp;interface.functions.transfer.signature<br>const&nbsp;sighash&nbsp;=&nbsp;interface.functions.transfer.sighash<br><br>const&nbsp;eventSig&nbsp;=&nbsp;interface.events.Transfer.signature<br>const&nbsp;topic&nbsp;=&nbsp;interface.events.Transfer.topic<br><br>//&nbsp;v5<br>const&nbsp;functionSig&nbsp;=&nbsp;functionFragment.format()<br>const&nbsp;sighash&nbsp;=&nbsp;interface.getSighash(functionFragment)<br><br>const&nbsp;eventSig&nbsp;=&nbsp;eventFragment.format()<br>const&nbsp;topic&nbsp;=&nbsp;interface.getTopic(eventFragment)</div>
<a name="utilities"></a><h2 class="show-anchors"><div>Utilities<div class="anchors"><a class="self" href="#utilities"></a></div></div></h2>
<a name="renaming"></a><h3 class="show-anchors"><div>Renaming<div class="anchors"><a class="self" href="#renaming"></a></div></div></h3>
<div class="code">TODO</div>
<a name="wallet"></a><h2 class="show-anchors"><div>Wallet<div class="anchors"><a class="self" href="#wallet"></a></div></div></h2>
<a name="mnemonic-phrases"></a><h3 class="show-anchors"><div>Mnemonic Phrases<div class="anchors"><a class="self" href="#mnemonic-phrases"></a></div></div></h3>
<p>The <b>mnemonic</b> phrase and related properties have been merged into a single <code class="inline">mnemonic</code> object, which also now includes the <code class="inline">locale</code>.</p>
<div class="code">//&nbsp;v4<br>wallet.mnemonic<br>wallet.path<br><br>//&nbsp;v5<br>//&nbsp;-&nbsp;Mnemonic&nbsp;phrase&nbsp;and&nbsp;path&nbsp;are&nbsp;a&nbsp;Mnemonic&nbsp;object<br>//&nbsp;-&nbsp;Note:&nbsp;wallet.mnemonic&nbsp;is&nbsp;null&nbsp;if&nbsp;there&nbsp;is&nbsp;no&nbsp;mnemonic<br>wallet.mnemonic.phrase<br>wallet.mnemonic.path</div>
<div class="footer"><div class="nav previous"><a href="/migration/web3/"><span class="arrow">&larr;</span>Migration: From Web3.js</a></div> <div class="nav next"><a href="/testing/">Testing<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 Attribution 4.0 International License</a>.</div></div><script src="/script.js" type="text/javascript"></script></body></html><!-- ContentHash:b23835fe0ae8cb86b0b5436e141e68656882e8754a899c46078964b042ed6ef3 -->