Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce jsonrpc calls using batch job #282

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions routes/web3relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,40 +126,40 @@ exports.data = function(req, res){

var addrData = {};

if (options.indexOf("balance") > -1) {
try {
addrData["balance"] = web3.eth.getBalance(addr);
addrData["balance"] = etherUnits.toEther(addrData["balance"], 'wei');
} catch(err) {
// batch job
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request(addr));
batch.add(web3.eth.getTransactionCount.request(addr));
batch.add(web3.eth.getCode.request(addr));

batch.requestManager.sendBatch(batch.requests, function(err, results) {
if (err) {
console.error("AddrWeb3 error :" + err);
addrData = {"error": true};
}
}
if (options.indexOf("count") > -1) {
try {
addrData["count"] = web3.eth.getTransactionCount(addr);
} catch (err) {
console.error("AddrWeb3 error :" + err);
addrData = {"error": true};
res.write(JSON.stringify({"error": true}));
res.end();
return;
}
}
if (options.indexOf("bytecode") > -1) {
try {
addrData["bytecode"] = web3.eth.getCode(addr);
if (addrData["bytecode"].length > 2)

results = results || [];
var addrData = {};
batch.requests.map(function (request, index) {
return results[index] || {};
}).forEach(function (result, i) {
if (i == 0) {
addrData["balance"] = etherUnits.toEther(result.result, 'wei');
} else if (i == 1) {
addrData["count"] = Number(result.result);
} else if (i == 2) {
addrData["bytecode"] = result.result;
if (addrData["bytecode"].length > 2)
addrData["isContract"] = true;
else
else
addrData["isContract"] = false;
} catch (err) {
console.error("AddrWeb3 error :" + err);
addrData = {"error": true};
}
}

res.write(JSON.stringify(addrData));
res.end();


}
});
res.write(JSON.stringify(addrData));
res.end();
});
} else if ("block" in req.body) {
var blockNumOrHash;
if (/^(0x)?[0-9a-f]{64}$/i.test(req.body.block.trim())) {
Expand Down