avatarAlex Roan

Summary

The article outlines the top five Web3 JavaScript functions essential for Ethereum DApp development, including getAccounts, sendTransaction, estimateGas, new web3.eth.Contract, and toWei.

Abstract

The "undefined" website provides a concise guide to the most crucial Web3 JavaScript functions for developers working on Ethereum DApps. It emphasizes the importance of Web3JS for enabling user interactions with DApps through web browsers. The article details the usage of web3.eth.getAccounts() for retrieving account addresses, web3.eth.sendTransaction() for transferring Ether and handling transaction events, web3.eth.estimateGas() for estimating gas costs for contract transactions, new web3.eth.Contract() for loading and interacting with deployed contracts, and web3.utils.toWei() for converting Ether to Wei. The functions are presented with code examples and explanations, highlighting their practical application in DApp development.

Opinions

  • The author suggests that understanding Web3JS is essential for front-end Ethereum DApp development.
  • The article implies that getAccounts is a fundamental function for initiating interactions with a DApp.
  • The use of sendTransaction is presented as a versatile tool for executing various types of transactions, with the ability to handle events during blockchain submission.
  • Estimating gas with estimateGas is considered important for transactions involving smart contracts, ensuring that sufficient gas is provided.
  • Loading a deployed contract with new web3.eth.Contract is seen as a critical step for DApps to interact with smart contracts on the Ethereum blockchain.
  • Conversion of Ether to Wei using toWei is highlighted as a necessary utility function for working with smart contracts, which operate in Wei.
  • The article encourages further learning and exploration in blockchain development, offering additional resources for those interested in expanding their knowledge and skills.

The Top 5 Web3 JavaScript Functions for Ethereum DApps

getAccounts, sendTransaction, estimateGas, and more

Photo by Paul Esch-Laurent on Unsplash.

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 one 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 that occur during submission to the blockchain. These events are transactionHash, receipt, confirmation, and error.

Usage:

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:

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

If you’re interested in blockchain development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio.

Check out some of these resources:

Programming
JavaScript
Blockchain
Ethereum
Dapps
Recommended from ReadMedium