<divclass="link title"><ahref="/v5/">Documentation</a></div><divclass="base show link depth-1"><ahref="/v5/getting-started/">Getting Started</a></div><divclass="base show link depth-1"><ahref="/v5/concepts/">Ethereum Basics</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/events/">Events</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/gas/">Gas</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/security/">Security</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/best-practices/">Best Practices</a></div><divclass="base show link depth-1"><ahref="/v5/api-keys/">Provider API Keys</a></div><divclass="base show link depth-1"><ahref="/v5/api/">Application Programming Interface</a></div><divclass="hide link depth-2"><ahref="/v5/api/providers/">Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/provider/">Provider</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/api-providers/">API Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/other/">Other Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/types/">Types</a></div><divclass="hide link depth-2"><ahref="/v5/api/signer/">Signers</a></div><divclass="hide link depth-2"><ahref="/v5/api/contract/">Contract Interaction</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/contract/">Contract</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/contract-factory/">ContractFactory</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><divclass="hide link depth-2"><ahref="/v5/api/utils/">Utilities</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/abi/">Application Binary Interface</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/coder/">AbiCoder</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/formats/">ABI Formats</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/fragments/">Fragments</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/interface/">Interface</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/address/">Addresses</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/bignumber/">BigNumber</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/bytes/">Byte Manipulation</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/constants/">Constants</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/display-logic/">Display Logic and Input</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/encoding/">Encoding Utilities</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/fixednumber/">FixedNumber</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/hashing/">Hashing Algorithms</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/hdnode/">HD Wallet</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/logger/">Logging</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/properties/">Property Utilities</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/signing-key/">Signing Key</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/strings/">Strings</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/transactions/">Transactions</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/web/">Web Utilities</a></div><divclass="hide link depth-3"><ahref="/v5/api/utils/wordlists/">Wordlists</a></div><divclass="hide link depth-2"><ahref="/v5/api/other/">Other Libraries</a></div><divclass="hide link depth-3"><ahref="/v5/api/other/assembly/">Assembly</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/api/">Utilities</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/ast/
<divclass="breadcrumbs"><ahref="/v5/">Documentation</a> » <ahref="/v5/migration/">Migration Guide</a> » <spanclass="current">Migration: From Web3.js</span></div>
<aname="migration-from-web3-js"></a><h1class="show-anchors"><div>Migration: From Web3.js<divclass="anchors"><aclass="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>
<aname="migration-from-web3-js--providers"></a><h2class="show-anchors"><div>Providers<divclass="anchors"><aclass="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>
<aname="migration-from-web3-js--providers--connecting-to-ethereum"></a><h3class="show-anchors"><div>Connecting to Ethereum<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--providers--connecting-to-ethereum"></a></div></div></h3>
<divclass="code"><spanclass="comment">// web3
</span>var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
<spanclass="comment">// ethers
</span>var ethers = require('ethers');
const url = "http://127.0.0.1:8545";
const provider = new ethers.providers.JsonRpcProvider(url);
</div><aname="migration-from-web3-js--providers--connecting-to-ethereum-metamask"></a><h3class="show-anchors"><div>Connecting to Ethereum: Metamask<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--providers--connecting-to-ethereum-metamask"></a></div></div></h3>
<divclass="code"><spanclass="comment">// web3
</span>const web3 = new Web3(Web3.givenProvider);
<spanclass="comment">// ethers
</span>const provider = new ethers.providers.Web3Provider(window.ethereum);
</div><aname="migration-from-web3-js--signers"></a><h2class="show-anchors"><div>Signers<divclass="anchors"><aclass="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>
<spanclass="comment">// ethers (connect to JSON-RPC accounts)
</span>const signer = provider.getSigner();
</div><aname="migration-from-web3-js--signers--signing-a-message"></a><h3class="show-anchors"><div>Signing a message<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--signers--signing-a-message"></a></div></div></h3>
<divclass="code"><spanclass="comment">// web3 (using a private key)
</div><aname="migration-from-web3-js--contracts"></a><h2class="show-anchors"><div>Contracts<divclass="anchors"><aclass="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>
<aname="migration-from-web3-js--contracts--deploying-a-contract"></a><h3class="show-anchors"><div>Deploying a Contract<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--contracts--deploying-a-contract"></a></div></div></h3>
<divclass="code"><spanclass="comment">// web3
</span>const contract = new web3.eth.Contract(abi);
<spanclass="comment">// wait for contract creation transaction to be mined
</span>await contract.deployTransaction.wait();
</div><aname="migration-from-web3-js--contracts--interacting-with-a-contract"></a><h3class="show-anchors"><div>Interacting with a Contract<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--contracts--interacting-with-a-contract"></a></div></div></h3>
<divclass="code"><spanclass="comment">// web3
</span>const contract = new web3.eth.Contract(abi, contractAddress);
</span><spanclass="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();
<spanclass="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);
<spanclass="comment">// wait for the transaction to be mined
</span>const receipt = await tx.wait();
</div><aname="migration-from-web3-js--contracts--overloaded-functions"></a><h3class="show-anchors"><div>Overloaded Functions<divclass="anchors"><aclass="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 <ahref="https://github.com/ethers-io/ethers.js/issues/407">issue #407</a> for more details.</p>
<aname="migration-from-web3-js--numbers--bignumber"></a><h3class="show-anchors"><div>BigNumber<divclass="anchors"><aclass="self"href="/v5/migration/web3/#migration-from-web3-js--numbers--bignumber"></a></div></div></h3><p>Convert to BigNumber:</p>
<divclass="code"><spanclass="comment">// web3
</span>web3.utils.toBN('123456');
<spanclass="comment">// ethers (from a number; must be within safe range)
<aname="migration-from-web3-js--utilities--hash"></a><h3class="show-anchors"><div>Hash<divclass="anchors"><aclass="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>
<divclass="code"><spanclass="comment">// web3
</span>web3.utils.sha3('hello world');
web3.utils.keccak256('hello world');
<spanclass="comment">// ethers (hash of a string)
</span>ethers.utils.id('hello world')
<spanclass="comment">// ethers (hash of binary data)
<divclass="copyright">The content of this site is licensed under the <ahref="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on February 8, 2021, 3:25pm.</div>