How to do transaction using web3 javascript API
Posted By : Rajat khurana | 27-Oct-2017
Hi guys, here you will learn how to create transactions using web3 javascript API in the Ethereum Blockchain. First of all, you need to know about web3.js.
Web3.js is the collection of libraries which help us to interact with
Now to carry a transaction from one account to another account, send transaction function is provided by web3.
To get a balance of an account getBalance function is provided by web3 js. Here is code to understand the concept.
var Web3 = require("web3");
var async = require('async');
var web3 = new Web3();
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
fromAccount = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
fromAccountPassword = 'ppppppppp';
toAccount = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
value = 500;
web3.eth.personal.unlockAccount(fromAccount, fromAccountPassword).then(function (result) {
web3.eth.sendTransaction({
from: fromAccount,
to: toAccount,
value: value
}).then(function (receipt) {
web3.eth.getBalance('' + toAccount + '').then(function (result) {
console.log(" Result in getbalance function", result);
}).catch(function (error) {
console.log("Error", error);
});
}).catch(function (error) { console.log("Error is:-", error); })
}).catch(function (error) {
console.log(" Error is:-", error);
});
Web3 contain an eth obj and with the use of this, we can get the balance of any account.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rajat khurana
Rajat is a bright Javascript Developer, he has good knowledge of HTML, WordPress, NodeJs, Core Java, Javascript, Jquery. Apart from this he loves to play Badminton and PC Gaming.