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

168 lines
14 KiB
HTML
Raw Normal View History

2020-06-10 06:56:58 +03:00
<!DOCTYPE html>
<html class="paged">
<head>
<title>Migration: From Ethers v4</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>
<div class="link title"><a href="/">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 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/interface/">Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</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 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/">Sand
</div></div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/migration/">Migration Guide</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Migration: From Ethers v4</span></div>
<a name="migration-v4"></a><a name="migration-v4"></a><h1 class="show-anchors"><div>Migration: From Ethers v4<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4"></a></div></div></h1>
<a name="migration-v4--bignumber"></a><h2 class="show-anchors"><div>BigNumber<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--bignumber"></a></div></div></h2>
<a name="migration-v4--bignumber--namespace"></a><h3 class="show-anchors"><div>Namespace<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--bignumber--namespace"></a></div></div></h3><p>Since <a href="/v5/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">// v4
ethers.utils.BigNumber
ethers.utils.BigNumberish
// v5
ethers.BigNumber
ethers.BigNumberish</div><a name="migration-v4--bignumber--creating-instances"></a><h3 class="show-anchors"><div>Creating Instances<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--bignumber--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">// v4
new ethers.utils.BigNumber(someValue)
ethers.utils.bigNumberify(someValue);
// v5
// - Constructor is private
// - Removed `bigNumberify`
ethers.BigNumber.from(someValue)</div><a name="migration-v4--contracts"></a><h2 class="show-anchors"><div>Contracts<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--contracts"></a></div></div></h2>
<div class="code">// @TODO</div><a name="migration-v4--errors"></a><h2 class="show-anchors"><div>Errors<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--errors"></a></div></div></h2>
<a name="migration-v4--errors--namespace"></a><h3 class="show-anchors"><div>Namespace<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--errors--namespace"></a></div></div></h3><p>All errors now belong to the <a href="/v5/api/utils/logger/#Logger">Logger</a> class and the related functions have been moved to <a href="/v5/api/utils/logger/#Logger">Logger</a> instances, which can include a per-package version string.</p>
<p>Global error fucntions have been moved <a href="/v5/api/utils/logger/#Logger">Logger</a> class methods.</p>
<div class="code">// v4
ethers.errors.UNKNOWN_ERROR
ethers.errors.*
errors.setCensorship(censorship, permanent)
errors.setLogLevel(logLevel)
errors.checkArgumentCount(count, expectedCount, suffix)
errors.checkNew(self, kind)
errors.checkNormalize()
errors.throwError(message, code, params)
errors.warn(...)
errors.info(...)
// v5
ethers.utils.Logger.errors.UNKNOWN_ERROR
ethers.utils.Logger.errors.*
Logger.setCensorship(censorship, permanent)
Logger.setLogLevel(logLevel)
const logger = new ethers.utils.Logger(version);
logger.checkArgumentCount(count, expectedCount, suffix)
logger.checkNew(self, kind)
logger.checkNormalize()
logger.throwError(message, code, params)
logger.warn(...)
logger.info(...)</div><a name="migration-v4--interface"></a><h2 class="show-anchors"><div>Interface<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--interface"></a></div></div></h2><p>The <a href="/v5/api/utils/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="migration-v4--interface--functions"></a><h3 class="show-anchors"><div>Functions<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--interface--functions"></a></div></div></h3>
<div class="code">// v4 (example: "transfer(address to, uint amount)")
interface.functions.transfer.encode(to, amount)
interface.functions.transfer.decode(callData)
// v5
interface.encodeData("transfer", [ to, amount ])
interface.decodeResult("transfer", data)
// Or you can use any compatible signature or Fragment objects.
// Notice that signature normalization is performed for you,
// e.g. "uint" and "uint256" will be automatically converted
interface.encodeData("transfer(address,uint)", [ to, amount ])
interface.decodeResult("transfer(address to, uint256 amount)", data)</div><a name="migration-v4--interface--events"></a><h3 class="show-anchors"><div>Events<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--interface--events"></a></div></div></h3>
<div class="code">// v4 (example: Transfer(address indexed, address indexed, uint256)
interface.events.Transfer.encodeTopics(values)
interface.events.Transfer.decode(data, topics)
// v5
interface.encodeFilterTopics("Transfer", values)
interface.encodeEventLog("Transfer", data, topics)</div><a name="migration-v4--interface--inspection"></a><h3 class="show-anchors"><div>Inspection<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--interface--inspection"></a></div></div></h3><p>Interrogating properties about a function or event can now (mostly) be done directly on the <a href="/v5/api/utils/abi/fragments/#Fragment">Fragment</a> object.</p>
<div class="code">// v4
interface.functions.transfer.name
interface.functions.transfer.inputs
interface.functions.transfer.outputs
interface.functions.transfer.payable
interface.functions.transfer.gas
// v5
const functionFragment = interface.getFunction("transfer")
functionFragment.name
functionFragment.inputs
functionFragment.outputs
functionFragment.payable
functionFragment.gas
// v4; type is "call" or "transaction"
interface.functions.transfer.type
// v5; constant is true (i.e. "call") or false (i.e. "transaction")
functionFragment.constant
// v4
interface.events.Transfer.anonymous
interface.events.Transfer.inputs
interface.events.Transfer.name
// v5
const eventFragment = interface.getEvent("Transfer");
eventFragment.anonymous
eventFragment.inputs
eventFragment.name
// v4
const functionSig = interface.functions.transfer.signature
const sighash = interface.functions.transfer.sighash
const eventSig = interface.events.Transfer.signature
const topic = interface.events.Transfer.topic
// v5
const functionSig = functionFragment.format()
const sighash = interface.getSighash(functionFragment)
const eventSig = eventFragment.format()
const topic = interface.getTopic(eventFragment)</div><a name="migration-v4--utilities"></a><h2 class="show-anchors"><div>Utilities<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--utilities"></a></div></div></h2>
<a name="migration-v4--utilities--renaming"></a><h3 class="show-anchors"><div>Renaming<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--utilities--renaming"></a></div></div></h3>
<div class="code">// @TODO</div><a name="migration-v4--wallet"></a><h2 class="show-anchors"><div>Wallet<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--wallet"></a></div></div></h2>
<a name="migration-v4--wallet--mnemonic-phrases"></a><h3 class="show-anchors"><div>Mnemonic Phrases<div class="anchors"><a class="self" href="/v5/migration/ethers-v4/#migration-v4--wallet--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">// v4
wallet.mnemonic
wallet.path
// v5
// - Mnemonic phrase and path are a Mnemonic object
// - Note: wallet.mnemonic is null if there is no mnemonic
wallet.mnemonic.phrase
wallet.mnemonic.path</div>
<div class="footer">
<div class="nav previous"><a href="/v5/migration/web3/"><span class="arrow">&larr;</span>Migration: From Web3.js</a></div>
<div class="nav next"><a href="/v5/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 License</a>. Generated on June 8, 2020, 10:6pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
</body>
</html>