For DApp(Decentralized application) main components include smart contract(a piece of code) and cryptocurrency(tokens). To interact with on-chain we need to create and register the transaction into the blockchain. Decentralized app(DApp) needs to use a Web3 SDK such as web3.js for browsers.
On blockchain one of the main js libraries that let you interact with Ethereum nodes is web3js.
How to install web3?
For installing web3js to your project we can use any of these codes below.
Npm: npm install web3
purejs : link the dist/web3.min.js
meteor: meteor add ethereum:web3
After installing we can create a web3 instance and can set the service provider. Ethereum can support browser addon providers like MetaMask, Mist can have an ethereum Provider or web3.currentProvider.
For eg:
// in node.js use:
var Web3 = require('web3');
var web3 = new Web3('ws://localhost:8546');
console.log(web3);
> {
eth: ... ,
shh: ... ,
utils: ...,
...
}
We can set a provider using the web3.setProvider()
web3.setProvider('ws://localhost:8546');
// or
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
Callbacks Promises Events
To integrate into different projects with different standards web3 provides multiple standards of asynchronous functions. Web3.js objects allow as to return promises to the chain function and also allow call back as the last parameter. Ethereum as a strong blockchain that works on smart contracts has different levels and needs to have different and multiple stages of action. To maintain the current requirement we need to have ‘promiEvent’ for function
‘web3.eth.sendTransaction’ , ‘web3.eth.currentProvider’
To act on different stages of action on blockchain we can use ‘promiEvent’ which is a promise combined with an event emitter, like for example doing transactions. It works like a normal promise with added on, once and off functions.
web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
.once('transactionHash', function(hash){ ... })
.on('confirmation', function(confNumber, receipt){ ... })
.then(function(receipt){
//
});
Web3
Main class of the web3.js library web3
var Web3 = require('web3');
> Web3.utils
> Web3.givenProvider
> Web3.modules
> Web3.providers
> Web3.version
Web3.modules
Usage: web3.modules
This will return an object of all the major sub-modules.
Objects contain the Constructors Module list.
web3.eth- For interacting with the ethereum network this Eth - constructor is used
web.eth.net- Interacting with network properties this net module constructor is used.
web3.ssh- For interacting with the whisper protocol ssh module constructor is used.
web3.eth.personal - It is used for interacting with the Ethereum accounts personal constructor is used.
web3.bzz- For interacting with swarm network bzz constructor is used.
For example
Web3.modules
> {
Eth: Eth(provider),
Net: Net(provider),
Shh: Shh(provider),
Personal: Personal(provider),
Bzz: Bzz(provider),
}
Web3 Instance
Inroder to interact with eatherum and all modules related to the ethereum, Web3 class will help us.
For finding the version of the current package of web3.js library we can use.
Web3.version
Web3.version
Example:
web3.version;
> "1.2.6"
Utils
Web3.utils packages provide utility function for Ethereum based App
For example: web3.utils.randomHex(size)
This utility function helps to generate random Hex strings of the given size, generated by cryptography.
Web3.eth
The web3.eth class helps one to communicate with the Ethereum blockchain and with the smart contract(code).
examples
Var eth = require(‘web3.eth’);
var eth = new Eth(Eth.givenProvider || 'ws://some.local-or-remote.node:8546');
var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');
// -> web3.eth
The coming articles will clarify more about web3js and its use cases