🧰 Web3 [Serie Part 0/10] - Introduction
Series Plan
In this Web3.js serie, we will go through the following parts:
- Setup Environment
- Connect Web3 to Ganache (local Ethereum Blockchain)
- Create Web3 instance
- Call Smart Contract function (call API)
- Send Transactions to Smart Contract (sendTransactions API)
- Send Ether to a Smart Contract
- Listen to Smart Contract's Events
- Deploy & Interact with Smart Contract using Infura
- Integrate Web3 with Metamask
- Overview Web3 Utility Functions
Introduction: What is Web3, and Why do we need it?
Web3 is a Javascript library easing interactions between Smart Contract(s) deployed on the Ethereum Blockchain, and a Web App.
Web3 is an “isomorphic library”, i.e. it can be used either in the back-end, and/or in the front-end.
The library was developed to ease communication with the Ethereum API. Natively, direct interactions with the Ethereum API remains complicated.
Ethereum Blockchain Environment Overview, without Web3
Ethereum Blockchain Environment Overview, including Web3
Smart Contract Interactions Overview
There are 2 main ways to interact with a Smart Contract: read and/or write/edit.
Interacting with these APIs requires a couple of information:
Smart Contract Address
What contract do we want to interact with? As you may imagine, there are plenty of contracts stored on the blockchain. The Smart Contract address is like the street address, where we can find the Smart Contract.
Smart Contract Function(s)
Such as a standard script, Smart Contracts may count several functions. Thus, we need to specify what function(s) to interact with.
Function Arguments (Optional)
If the function requires specific parameters, we need to include them in our API interaction.
Ether Value (Transaction API only)
In the case of writing or editing functions, we need to define the Ether value. Why? Since we are changing the Smart Contract, thus, the Blockchain, the changes must be mined. By consequence, we need to pay the interaction.
Other Parameters
Other parameters can be defined such as gas, gasLimit, etc. In the case of transactions, we are impacting the blockchain. Thus, it must be mined in a block. By consequence it will cost a bit of money, called gas (~money required by the miner to write the transaction in the blockchain).
In order to interact with Smart Contracts, Web3 needs access to an Ethereum node (i.e. a server running the Ethereum Blockchain software).
There are 2 main ways to connect Web3 library to the Ethereum Mainnet or any other Public Testnets (e.g. Ropsten, or Kovan):
- You own Ethereum Node (not covered in this tutorial). See this link for more information about running your own node.
- Infura: a third party service exposing an API to interact with Ethereum node (free plans available).
In this tutorial, we will go through the main features of Web3 library allowing you to interact with a Smart Contract.
In the next lectures, we will cover the following steps:
- Setup Environment
- Connect Web3 to Ganache (local Ethereum Blockchain)
- Create Web3 instance
- Call Smart Contract function (call API)
- Send Transactions to Smart Contract (sendTransactions API)
- Send Ether to a Smart Contract
- Listen to Smart Contract's Events
- Deploy & Interact with Smart Contract using Infura
- Integrate Web3 with Metamask
- Overview Web3 Utility Functions
Ready? Let's move on to the first lecture: Setup Environment.