Lesson 8 of 14
In Progress

Sending Transactions with Web3.js

Web3.js is a JavaScript library that allows you to interact with the Ethereum blockchain using a simple and intuitive API. In this article, we will look at how to use Web3.js to send transactions on the Ethereum blockchain.

Sending Ether (ETH)

One of the most common use cases for transactions on the Ethereum blockchain is the transfer of Ether (ETH) between accounts. To send Ether (ETH) with Web3.js, you can use the web3.eth.sendTransaction() method, which sends a transaction from one account to another on the Ethereum blockchain.

Here is an example of how to send Ether (ETH) with Web3.js:

const Web3 = require('web3');

const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-API-KEY');

web3.eth.sendTransaction({
  from: 'YOUR-ACCOUNT-ADDRESS',
  to: 'RECIPIENT-ADDRESS',
  value: '100000000000000000',
  gas: 21000,
  gasPrice: '30000000000'
}, (error, transactionHash) => {
  console.log(transactionHash);
}).on('error', (error) => {
  console.error(error);
});

This code sends a transaction from YOUR-ACCOUNT-ADDRESS to RECIPIENT-ADDRESS with 1 ETH as the value. The gas and gasPrice parameters specify the gas limit and gas price that will be used to execute the transaction.

If the transaction is successful, the transaction hash will be output to the console. You can use the transaction hash to track the status of the transaction on the Ethereum blockchain.

Sending Transactions with a Smart Contract

In addition to sending Ether (ETH) directly, you can also use transactions to execute smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. They are used to facilitate, verify, and enforce the negotiation or performance of a contract, and they are executed on the Ethereum Virtual Machine (EVM).

To send a transaction with a smart contract, you will need to do the following:

  1. Compile your smart contract with a Solidity compiler, such as Remix (https://remix.ethereum.org).
  2. Deploy your smart contract to the Ethereum blockchain with a Web3 provider, such as Infura (https://infura.io).
  3. Interact with your smart contract using the Web3.js API.

Here is an example of how to send a transaction with a smart contract using Web3.js:

const Web3 = require('web3');

const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-API-KEY');

const contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS);

contract.methods.functionName(functionArguments).send({
  from: 'YOUR-ACCOUNT-ADDRESS',
  gas: 1500000,
  gasPrice: '30000000000000'
}, (error, transactionHash) => {
  console.log(transactionHash);
}).on('error', (error) => {
  console.error(error);
}).then((result) => {
  console.log(result);
});

This code calls the functionName() function of the contract smart contract, with the specified functionArguments. The from parameter specifies the address of the account that will be used to send the transaction, and the gas and gasPrice parameters specify the amount of gas and the gas price that will be used to execute the transaction.

If the transaction is successful, the transaction hash and the result of the function will be output to the console. You can use the transaction hash to track the status of the transaction on the Ethereum blockchain, and you can use the result to update the state of your application.

Conclusion

Sending transactions on the Ethereum blockchain is a key function of any decentralized application (DApp) built on Ethereum. By using the Web3.js library, you can easily send transactions with Ether (ETH) or smart contracts, and you can build powerful DApps that are fast, secure, and transparent. Whether you are a seasoned blockchain developer or a beginner, Web3.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What is the purpose of transactions on the Ethereum blockchain?

Transactions on the Ethereum blockchain are used to execute smart contracts, transfer value between accounts, and update the state of the Ethereum blockchain.

How do you send Ether (ETH) with Web3.js?

To send Ether (ETH) with Web3.js, you can use the web3.eth.sendTransaction() method. This method requires you to specify the recipient’s address, the amount of Ether (ETH) to be transferred, the gas limit, and the gas price.

What is a smart contract?

A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. Smart contracts are used to facilitate, verify, and enforce the negotiation or performance of a contract, and they are executed on the Ethereum Virtual Machine (EVM).

How do you send a transaction with a smart contract using Web3.js?

To send a transaction with a smart contract using Web3.js, you will need to compile your smart contract with a Solidity compiler, deploy your smart contract to the Ethereum blockchain with a Web3 provider, and interact with your smart contract using the Web3.js API.

What is the purpose of the from parameter in a Web3.js transaction?

The from parameter in a Web3.js transaction specifies the address of the account that will be used to send the transaction. It is important to use the correct from address in a transaction, as it determines who will be credited or debited in the transaction.