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

176 lines
17 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Migration: From Web3.js</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
<meta property="og:title" content="Migration: From Web3.js"/>
<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 show link depth-1"><a href="/v5/api/">Application Programming Interface</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/signer/">Signers</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/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 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 ancestor show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="myself ancestor ancestor show link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="link show child depth-3"><a href="#migration-from-web3-js--providers">Providers</a></div><div class="link show child depth-3"><a href="#migration-from-web3-js--signers">Signers</a></div><div class="link show child depth-3"><a href="#migration-from-web3-js--contracts">Contracts</a></div><div class="link show child depth-3"><a href="#migration-from-web3-js--numbers">Numbers</a></div><div class="link show child depth-3"><a href="#migration-from-web3-js--utilities">Utilities</a></div><div class="show 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/migration/">Migration Guide</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Migration: From Web3.js</span></div>
<a name="migration-from-web3-js"></a><h1 class="show-anchors"><div>Migration: From Web3.js<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js"></a></div></div></h1><p>This migration guide focuses on migrating web3.js version 1.2.9 to ethers.js v5.</p>
<a name="migration-from-web3-js--providers"></a><h2 class="show-anchors"><div>Providers<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--providers"></a></div></div></h2><p>In ethers, a provider provides an abstraction for a connection to the Ethereum Network. It can be used to issue read only queries and send signed state changing transactions to the Ethereum Network.</p>
<a name="migration-from-web3-js--providers--connecting-to-ethereum"></a><h3 class="show-anchors"><div>Connecting to Ethereum<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--providers--connecting-to-ethereum"></a></div></div></h3>
<div class="code"><span class="comment">// web3
</span>var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
<span class="comment">// ethers
</span>var ethers = require('ethers');
const url = "http://127.0.0.1:8545";
const provider = new ethers.providers.JsonRpcProvider(url);
</div><a name="migration-from-web3-js--providers--connecting-to-ethereum-metamask"></a><h3 class="show-anchors"><div>Connecting to Ethereum: Metamask<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--providers--connecting-to-ethereum-metamask"></a></div></div></h3>
<div class="code"><span class="comment">// web3
</span>const web3 = new Web3(Web3.givenProvider);
<span class="comment">// ethers
</span>const provider = new ethers.providers.Web3Provider(window.ethereum);
</div><a name="migration-from-web3-js--signers"></a><h2 class="show-anchors"><div>Signers<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--signers"></a></div></div></h2><p>In ethers, a <b>signer</b> is an abstraction of an Ethereum Account. It can be used to sign messages and transactions and send signed transactions to the Ethereum Network.</p>
<p>In web3, an account can be used to sign messages and transactions.</p>
<a name="migration-from-web3-js--signers--creating-signer"></a><h3 class="show-anchors"><div>Creating signer<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--signers--creating-signer"></a></div></div></h3>
<div class="code"><span class="comment">// web3
</span>const account = web3.eth.accounts.create();
<span class="comment">// ethers (create random new account)
</span>const signer = ethers.Wallet.createRandom();
<span class="comment">// ethers (connect to JSON-RPC accounts)
</span>const signer = provider.getSigner();
</div><a name="migration-from-web3-js--signers--signing-a-message"></a><h3 class="show-anchors"><div>Signing a message<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--signers--signing-a-message"></a></div></div></h3>
<div class="code"><span class="comment">// web3 (using a private key)
</span>signature = web3.eth.accounts.sign('Some data', privateKey)
<span class="comment">// web3 (using a JSON-RPC account)
</span><span class="comment">// @TODO
</span>
<span class="comment">// ethers
</span>signature = await signer.signMessage('Some data')
</div><a name="migration-from-web3-js--contracts"></a><h2 class="show-anchors"><div>Contracts<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--contracts"></a></div></div></h2><p>A contract object is an abstraction of a smart contract on the Ethereum Network. It allows for easy interaction with the smart contact.</p>
<a name="migration-from-web3-js--contracts--deploying-a-contract"></a><h3 class="show-anchors"><div>Deploying a Contract<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--contracts--deploying-a-contract"></a></div></div></h3>
<div class="code"><span class="comment">// web3
</span>const contract = new web3.eth.Contract(abi);
contract.deploy({
data: bytecode,
arguments: ["my string"]
})
.send({
from: "0x12598d2Fd88B420ED571beFDA8dD112624B5E730",
gas: 150000,
gasPrice: "30000000000000"
}), function(error, transactionHash){ ... })
.then(function(newContract){
console.log('new contract', newContract.options.address)
});
<span class="comment">// ethers
</span>const signer = provider.getSigner();
const factory = new ethers.ContractFactory(abi, bytecode, signer);
const contract = await factory.deploy("hello world");
console.log('contract address', contract.address);
<span class="comment">// wait for contract creation transaction to be mined
</span>await contract.deployTransaction.wait();
</div><a name="migration-from-web3-js--contracts--interacting-with-a-contract"></a><h3 class="show-anchors"><div>Interacting with a Contract<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--contracts--interacting-with-a-contract"></a></div></div></h3>
<div class="code"><span class="comment">// web3
</span>const contract = new web3.eth.Contract(abi, contractAddress);
<span class="comment">// read only query
</span>contract.methods.getValue().call();
<span class="comment">// state changing operation
</span>contract.methods.changeValue(42).send({from: ....})
.on('receipt', function(){
...
});
<span class="comment">// ethers
</span><span class="comment">// pass a provider when initiating a contract for read only queries
</span>const contract = new ethers.Contract(contractAddress, abi, provider);
const value = await contract.getValue();
<span class="comment">// pass a signer to create a contract instance for state changing operations
</span>const contract = new ethers.Contract(contractAddress, abi, signer);
const tx = await contract.changeValue(33);
<span class="comment">// wait for the transaction to be mined
</span>const receipt = await tx.wait();
</div><a name="migration-from-web3-js--contracts--overloaded-functions"></a><h3 class="show-anchors"><div>Overloaded Functions<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--contracts--overloaded-functions"></a></div></div></h3><p>Overloaded functions are functions that have the same name but different parameter types.</p>
<p>In ethers, the syntax to call an overloaded contract function is different from the non-overloaded function. This section shows the differences between web3 and ethers when calling overloaded functions.</p>
<p>See <a href="https://github.com/ethers-io/ethers.js/issues/407">issue #407</a> for more details.</p>
<div class="code"><span class="comment">// web3
</span>message = await contract.methods.getMessage('nice').call();
<span class="comment">// ethers
</span>const abi = [
"function getMessage(string) public view returns (string)",
"function getMessage() public view returns (string)"
]
const contract = new ethers.Contract(address, abi, signer);
<span class="comment">// for ambiguous functions (two functions with the same
</span><span class="comment">// name), the signature must also be specified
</span>message = await contract['getMessage(string)']('nice');
</div><a name="migration-from-web3-js--numbers"></a><h2 class="show-anchors"><div>Numbers<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--numbers"></a></div></div></h2>
<a name="migration-from-web3-js--numbers--bignumber"></a><h3 class="show-anchors"><div>BigNumber<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--numbers--bignumber"></a></div></div></h3><p>Convert to BigNumber:</p>
<div class="code"><span class="comment">// web3
</span>web3.utils.toBN('123456');
<span class="comment">// ethers (from a number; must be within safe range)
</span>ethers.BigNumber.from(123456)
<span class="comment">// ethers (from base-10 string)
</span>ethers.BigNumber.from("123456")
<span class="comment">// ethers (from hex string)
</span>ethers.BigNumber.from("0x1e240")
</div><a name="migration-from-web3-js--utilities"></a><h2 class="show-anchors"><div>Utilities<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--utilities"></a></div></div></h2>
<a name="migration-from-web3-js--utilities--hash"></a><h3 class="show-anchors"><div>Hash<div class="anchors"><a class="self" href="/v5/migration/web3/#migration-from-web3-js--utilities--hash"></a></div></div></h3><p>Computing Keccak256 hash of a UTF-8 string in web3 and ethers:</p>
<div class="code"><span class="comment">// web3
</span>web3.utils.sha3('hello world');
web3.utils.keccak256('hello world');
<span class="comment">// ethers (hash of a string)
</span>ethers.utils.id('hello world')
<span class="comment">// ethers (hash of binary data)
</span>ethers.utils.keccak256('0x4242')
</div>
<div class="footer">
<div class="nav previous"><a href="/v5/migration/"><span class="arrow">&larr;</span>Migration Guide</a></div>
<div class="nav next"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4<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>