Web3.
js and Nodejs
Notes are from :
https://github.com/ethereum/wiki/wiki/JavaScript-API
Web3
• To talk to an ethereum node from inside a JavaScript
application use the web3.js library, which gives a
convenient interface for the RPC methods.
• This is the Ethereum compatible JavaScript API which
implements the Generic JSON RPC spec.
• https://github.com/ethereum/web3.js/
• https://github.com/ethereum/wiki/wiki/JavaScript-
API
Installing Web3
npm: npm install web3
bower: bower install web3
Using Web3 in a Javascript Program
• you need to create a web3 instance, setting a
provider.
var Web3 = require('web3')
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
• After this you can use the API of the web3
object.
Function Calls
• Functions use synchronous HTTP requests by default.
• If you want to make an asynchronous request, you can
pass an optional callback as the last parameter to most
functions.
• All callbacks are using an error first callback style:
web3.eth.getBlock(48, function(error, result){
if(!error)
console.log(result)
else
console.error(error);
})
Batch requests
• Batch requests allow queuing up requests and
processing them at once.
• Note Batch requests are not faster! In fact making
many requests at once will in some cases be faster, as
requests are processed asynchronous.
• Batch requests are mainly useful to ensure the serial
processing of requests.
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000
000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));
batch.execute();
A note on big numbers in web3.js
• You will always get a BigNumber object for number values as JavaScript is
not able to handle big numbers correctly.
• web3.js depends on the BigNumber Library and adds it automatically.
• it is recommended to keep you balance always in wei and only transform
it to other units when presenting to the user:
Example: eth.getTransaction
var blockNumber = 668;
var indexOfTransaction = 0
var transaction = web3.eth.getTransaction(blockNumber, indexOfTransaction);
console.log(transaction);
/*
{
"hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
"nonce": 2,
"blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
"blockNumber": 3,
"transactionIndex": 0,
"from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
"value": BigNumber,
"gas": 314159,
"gasPrice": BigNumber,
"input": "0x57cb2fc4"
}
*/
Example: getBlockTransactionCount
var number =
web3.eth.getBlockTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
console.log(number); // 1
getCode
var code = web3.eth.getCode("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8");
console.log(code); //
"0x600160008035811a818181146012578301005b601b6001356025565b806000526020
6000f25b600060078202905091905056"
Nodejs Example
• https://github.com/ebloc/eBlocXplore
var Web3 = require('./node_modules/web3/index.js');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
if(!web3.isConnected()) {
console.log("not connected"); ….
}
else app.get('/txtabl', function (req, res) {
console.log("connected"); abtx = [] ;
getLatestTransactions(6) ;
var express = require('express'); res.setHeader('Content-Type', 'application/json');
var app = express(); res.send(JSON.stringify(abtx));
var bodyParser = require('body-parser'); }) ;
// Create application/x-www-form-urlencoded parser ..
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var server = app.listen(8000, function () {
//var host = server.address().address
app.use(express.static('.')); var host = "localhost"
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})