Posted By : Bhuvan
Web3.js is a collection and combination of the fullness of Ethereum libraries that enables a developer to interface with a localhost or Ethereum node, using an HTTP connection in your JS file with some given aspects. This web3.js Js library interfaces with the Ethereum blockchain node. Using this library, you can create user accounts and send transactions using from address or to address, better interact with smart contracts, and more.
Best BlockChain Library for Front End developer
So, we need to do a few steps to get web3 in your javascript. So, we are moving stepwise so have a look around
Installing Web3.JS in Javascript Code
-Installing Node
Install Node js in your system and max version of node 10.16.0
-Installing Web3.js
Npm install web3 (If you are using any framework like React/Angular/Vue) So you need to installing web3 using this command
-For Normal Javascript code, you can use CDN Link
CDN: https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/ (This link provide you a whole library event so you can paste this link into index.html file
<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.js">
Web3 libraries is a repository of modules that contains functionality for the Ethereum node ecosystem.
-web3.js Object-eth is containing a Ethereum blockchain and smart contracts details.
After Installing web3 into your project, you need to create an instance of Web3. This instance object will help you a lot in the whole project. It provides a collection of modules that contain functionality for the Ethereum Blockchain
For Example : web3.js, Now you can check that Web3.givenProvider.
If this object or property is showing null in console you should connect to a remotely/local node .
const Web3JS = require('web3');
let web3 = new Web3JS(Web3JS.givenProvider || "ws://localhost:8545");
Now you can use Web3 object now they are contain all methods
So, now you have finally installed Web3 in your FrontEnd Code. I will now show you some example of how we can use the collections of the module from web3j
(Web3.Js Examples)
web3.eth.getNodeInfo(function(error, valueInResult){
if(error){
console.log( “error” ,error);
}
else{
console.log( “valueInResult”,valueInResult ); "
}
});
Also, read | Ethereum Rinkeby Testnet Setup Using Docker Compose
web3.eth.getBalance : get account’s balance
web3.eth.getAccounts(function(error, accounts) {
if(error) {
console.log(error);
}
web3.eth.getBalance(accounts[0]).then(function(result){
console.log( "Balance : " ,web3.utils.fromWei(result, 'ether'));
});
});
txnCount = web3.eth.getTransactionCount(web3.eth.accounts[0])
Transferring Ether from B to C address
txnObject ( transaction object) is first and one argu of web3.eth.sendTransaction and txnObject is based on JSON Data.
from: is in string type for adding own address
to: is in string type for adding sender address
value: Number|String|BigNumber — (optional) the amount of Ether you wish to send to the from address to address.
gas: Num |String|BigNumber — (optional) the amount of gas is used for transaction and the available amount of gas for the transaction (unused gas is refunded).
gas price: Number|String|BigNumber — (optional) this is the price of gas for the particular transaction in using web3, default that means is a gas.
var txnObject = {
"from":_from,
"to": _to,
"value": web3.utils.toWei(_Amount,'ether'),
---> this is a option "gas": 23000,
---> this is a option "gasPrice": 4600000,
---> this is a option "nonce": 11
}
web3.eth.sendTransaction(txnObject, function(error, result){
if(error){
console.log( "Transaction error" ,error);
}
else{
var txn_hash = result; ---> Get the Transaction hash
}
});
Also, you can Send ICO Token Using this Library
(Using the same methods )
My Review about this library
So, let's talk about my experience with this library. Basically, this library is perfect for using blockchain code in client-side, so this library performs very well but in some cases, they have some disadvantage because they did not allow to own HTTP header in their APIs calls. So, sometimes we are in trouble with that thing, other things are perfect and good.
Also, read | Deploying a Multisignature Wallet Smart Contract on Ethereum
Library Reference Link
November 21, 2024 at 01:35 pm
Your comment is awaiting moderation.