The Top 5 Web3 JavaScript Functions for Ethereum DApps
Photo by Paul Esch-Laurent on Unsplash.

The Top 5 Web3 JavaScript Functions for Ethereum DApps

By alexroan | Blockchain Developer | 15 May 2020


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 fromtovaluegas, 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 transactionHashreceiptconfirmation, 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");
...

Learn More

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 this evolving list of Blockchain Development Resources.

If you enjoyed this post and want to learn more about Blockchain Development or the Blockchain Space in general, I highly recommend signing up to the Blockgeeks platform. They have courses on a wide range of topics in the industry, from Coding to Marketing to Trading. It has proven to be an invaluable tool for my development in the Blockchain space.

 


alexroan
alexroan

Blockchain Developer


Blockchain Developer
Blockchain Developer

Tutorials, walkthrough, hints and tips on Blockchain Development for all levels of expertise.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.