1

I was trying to connect to my node via IPC using web3_ipc. But i got hit by:

 You tried to send "eth_getBalance" synchronously. Synchronous requests are not supported by the IPC provider.

My code is as:

var web3_extended = require('web3_ipc');
var path = require('path');
var absolutePath =  path.relative('./','/home/pr.singh/.ethereum/geth.ipc');
var options = {
  host : absolutePath,
  // host: 'http://10.10.23.139:8013',
  ipc : true,
  personal: true, 
  admin: true,
  debug: false
};

var web3 = web3_extended.create(options);

exports.checkBalance = function(req, res) {
    var bal = req.body.balance;
                var accountBalance=web3.eth.getBalance(bal).toNumber(); 
                console.log(accountBalance);
}

If I connect using rpc, everything works good but while connecting via IPC , I am getting error. Any help?

1 Answer 1

1

It seems that IPC doesn't support synchronous call which make sense in term of non-blocking code execution in this kind of architecture. Metamask has the same constraint.

Anyway, good practice in my opinion is to use asynchronous call with callback method: You can directly pass the callback function in the Web3 call:

exports.checkBalance = function(req, res) {
    var bal = req.body.balance;

    web3.eth.getBalance(bal, function(error, result) {
        // This is executed once I got the response
        console.log("Response received");

        if(error != null) {
            console.error(error);
            return;
        }

        var accountBalance = result.toNumber();
        console.log(accountBalance);
    }); 

    // This is executed just after the request is sent no matter the response
    console.log("Request sent");
}

Or for a better re-usability, define you callback function and inject it as a parameter:

exports.checkBalance = function(req, res) {
    var bal = req.body.balance;

    web3.eth.getBalance(bal, exports.checkBalanceCallback); 
}

exports.checkBalanceCallback = function(error, result) {

     if(error != null) {
        console.error(error);
         return;
     }

    var accountBalance = result.toNumber();
    console.log(accountBalance);
}

EDIT: Just edited my answer, according to the Web3 documentation about callback the method signature is function(error, result);

2
  • After implementing this method style, I am getting result as null and hence result.toNumber() is giving error. Commented Dec 18, 2016 at 12:03
  • Just edited my answer, according to the Web3 documentation about callback, the method signature is function(error, result); Commented Dec 18, 2016 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.