avatarAlex Roan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2008

Abstract

atch certain events which occur during submission to the Blockchain. These events are <i>transactionHash</i>, <i>receipt</i>, <i>confirmation</i> and <i>error</i>.</p><p id="b627">Usage:</p><div id="54d2"><pre>web3.eth.sendTransaction({ from: account1, to: account2, value: <span class="hljs-number">1000000000</span> }) .<span class="hljs-literal">on</span>(<span class="hljs-string">'transactionHash'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =></span> { ... }) .<span class="hljs-literal">on</span>(<span class="hljs-string">'receipt'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =></span> { ... }) .<span class="hljs-literal">on</span>(<span class="hljs-string">'confirmation'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =></span> { ... }) .<span class="hljs-literal">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =></span> { ... })</pre></div><h1 id="931b">web3.eth.estimateGas()</h1><p id="bfb2">If you’re sending a transaction to a contract, you might have to estimate the gas. Pass the same parameters as <code>web3.eth.sendTransaction()</code> to receive the gas estimate. You can then add the result to the parameters in your <code>web3.eth.sendTransaction()</code> call.</p><p id="3119">Usage:</p><div id="a80b"><pre><span class="hljs-keyword">let</span> gasEstimate = <span class="hljs-keyword">await</span> web3.eth.estimateGas({ <span class="hljs-keyword">from</span>: account1, to: account2, <span class="hljs-keyword">value</span>: <span class="hljs-number">1000000000</span> });</pre></div><div id="2f75"><pre>web3.eth.sendTransaction({ from: account1, to: account2, value: <span class="hljs-number">1000000000</span>, gas: gasEstimate }) .<span class="hljs-literal">on</span>(<span class="hljs-string">'transactionHash'</span>, <span class="hljs-function"><span

Options

class="hljs-params">()</span> =></span> { ...</pre></div><h1 id="e5ba">new web3.eth.Contract()</h1><p id="4a11">Use this function to load your deployed contract into your DApp so you can start interacting with it. Parameters include the <i>ABI</i> and the <i>address</i> the contract was deployed to.</p><p id="94f4">Usage:</p><div id="2849"><pre><span class="hljs-keyword">let</span> contractInstance = <span class="hljs-keyword">new</span> web3.eth.Contract( MyContract.abi, deployedAddress ); <span class="hljs-keyword">await</span> contractInstance.doSomething();</pre></div><h1 id="f1d2">web3.utils.toWei()</h1><p id="ed19">Use this function to convert Ether values into Wei, the unit of value used by Smart Contracts.</p><p id="64bb">Usage:</p><div id="46b2"><pre><span class="hljs-keyword">let</span> weiValue = web3.utils.toWei(<span class="hljs-string">"1"</span>, <span class="hljs-string">"ether"</span>); ...</pre></div><h1 id="1b59">Further Reading</h1><p id="c58b">Dive into the <a href="https://web3js.readthedocs.io/en/v1.2.6/">Web3JS documentation</a> to learn about these, and more.</p><p id="30ba">Browse a <a href="https://readmedium.com/blockchain-development-resources-b44b752f3248">collection of tutorials, walkthroughs, explanations, and cheat sheets</a> to build your experience with blockchain development.</p><div id="71db" class="link-block"> <a href="https://readmedium.com/blockchain-development-resources-b44b752f3248"> <div> <div> <h2>Blockchain Development Resources To Follow Right Now</h2> <div><h3>A list of resources to learn Blockchain, Ethereum, and DApp development</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*GZlVe27SOc44fcIYztY5Qw.jpeg)"></div> </div> </div> </a> </div></article></body>

Getting Started With Web3 JS

An overview of the most useful and commonly used functions

Photo by Joshua Aragon on Unsplash

Introduction

Web3 bridges the gap between the traditional internet and the Ethereum Blockchain. It enables users to interact with your DApp through a browser. When using Javascript for your front end, knowing the ins and outs of Web3JS is essential.

Here is a list of some of the most useful and commonly used functions in Web3JS.

web3.eth.getAccounts()

Use this function to get all of the available account addresses.

Usage:

let accounts = await web3.eth.getAccounts();
console.log(accounts[0]);

or

web3.eth.getAccounts().then(console.log);

web3.eth.sendTransaction()

Use this to send Ether from an account to another, or a Smart Contract address. It requires a few parameters depending on the transaction. Possible parameters include: from, to, value, gas, and more.

If you’re using this method as part of your front end, you can catch certain events which occur during submission to the Blockchain. These events are transactionHash, receipt, confirmation and error.

Usage:

web3.eth.sendTransaction({
    from: account1, 
    to: account2, 
    value: 1000000000
})
.on('transactionHash', () => {
    ...
})
.on('receipt', () => {
    ...
})
.on('confirmation', () => {
    ...
})
.on('error', () => {
    ...
})

web3.eth.estimateGas()

If you’re sending a transaction to a contract, you might have to estimate the gas. Pass the same parameters as web3.eth.sendTransaction() to receive the gas estimate. You can then add the result to the parameters in your web3.eth.sendTransaction() call.

Usage:

let gasEstimate = await web3.eth.estimateGas({
    from: account1, 
    to: account2, 
    value: 1000000000
});
web3.eth.sendTransaction({
    from: account1, 
    to: account2, 
    value: 1000000000,
    gas: gasEstimate
})
.on('transactionHash', () => { 
    ...

new web3.eth.Contract()

Use this function to load your deployed contract into your DApp so you can start interacting with it. Parameters include the ABI and the address the contract was deployed to.

Usage:

let contractInstance = new web3.eth.Contract(
    MyContract.abi,
    deployedAddress
);
await contractInstance.doSomething();

web3.utils.toWei()

Use this function to convert Ether values into Wei, the unit of value used by Smart Contracts.

Usage:

let weiValue = web3.utils.toWei("1", "ether");
...

Further Reading

Dive into the Web3JS documentation to learn about these, and more.

Browse a collection of tutorials, walkthroughs, explanations, and cheat sheets to build your experience with blockchain development.

Programming
JavaScript
Blockchain
Cryptocurrency
Ethereum
Recommended from ReadMedium