Lesson 9 of 14
In Progress

Reading Data from the Blockchain with Web3.js

Web3.js is a powerful tool for interacting with the Ethereum blockchain. In addition to sending transactions, you can also use Web3.js to read data from the blockchain. In this article, we will look at how to use Web3.js to read data from the Ethereum blockchain.

Reading Data from Smart Contracts

One of the most common ways to read data from the Ethereum blockchain is to read it from a smart contract. 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 read data from a smart contract with Web3.js, 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 read data from a smart contract with 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).call((error, result) => {
  console.log(result);
});

This code calls the functionName() function of the contract smart contract, with the specified functionArguments. The result of the function will be output to the console.

Reading Data from the Ethereum Blockchain

In addition to reading data from smart contracts, you can also read data directly from the Ethereum blockchain. This can be useful for reading data such as the balance of an Ethereum account, the current block number, or the transaction history of an account.

To read data from the Ethereum blockchain with Web3.js, you can use the various methods provided by the web3.eth object. For example, you can use the web3.eth.getBalance() method to get the balance of an Ethereum account, and the web3.eth.getBlockNumber() method to get the current block number.

Here is an example of how to read the balance of an Ethereum account with Web3.js:

const Web3 = require('web3');

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

web3.eth.getBalance('ACCOUNT-ADDRESS', (error, balance) => {
  console.log(balance);
});

This code reads the balance of the Ethereum account with the specified ACCOUNT-ADDRESS and outputs it to the console. The balance is returned in Wei, which is the smallest unit of Ether (ETH). You can use the web3.utils.fromWei() method to convert the balance to Ether (ETH).

Conclusion

Reading data from the Ethereum blockchain is an important function of any decentralized application (DApp) built on Ethereum. By using the Web3.js library, you can easily read data from smart contracts or the Ethereum blockchain, and you can build powerful DApps that are fast, secure, and transparent. Whether you are a seasoned blockchain developer or a beginner, Web3.js is a powerful tool for building on Ethereum.

Exercises

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

How do you read data from a smart contract with Web3.js?

To read data from a smart contract with 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.

Write a function that takes an Ethereum address as an argument and returns the balance of that address in Ether (ETH).

const Web3 = require('web3');

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

const getBalance = (address) => {
  web3.eth.getBalance(address, (error, balance) => {
    const balanceInEth = web3.utils.fromWei(balance, 'ether');
    console.log(balanceInEth);
  });
};

getBalance('ACCOUNT-ADDRESS'); // Outputs the balance in Ether (ETH)

Write a function that takes a smart contract address and a function name as arguments, and returns the result of calling that function on the smart contract.

const Web3 = require('web3');

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

const callSmartContractFunction = (contractAddress, functionName) => {
  const contract = new web3.eth.Contract(ABI, contractAddress);
  contract.methods[functionName]().call((error, result) => {
    console.log(result);
  });
};

callSmartContractFunction('CONTRACT-ADDRESS', 'functionName');

Write a function that takes a smart contract address and a function name as arguments, and returns the transaction history of that function on the smart contract.

const Web3 = require('web3');

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

const getSmartContractFunctionTransactionHistory = (contractAddress, functionName) => {
  const contract = new web3.eth.Contract(ABI, contractAddress);
  web3.eth.getPastLogs({
    address: contractAddress,
    topics: [web3.utils.sha3(functionName)]
  }, (error, logs) => {
    console.log(logs);
  });
};

getSmartContractFunctionTransactionHistory('CONTRACT-ADDRESS', 'functionName');

Write a function that takes a smart contract address, a function name, and function arguments as arguments, and returns the result of calling that function on the smart contract with the specified arguments.

const Web3 = require('web3');

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

const callSmartContractFunctionWithArguments = (contractAddress, functionName, functionArguments) => {
  const contract = new web3.eth.Contract(ABI, contractAddress);
  contract.methods[functionName](...functionArguments).call((error, result) => {
    console.log(result);
  });
};