How to get balance of a address using bitcore api


(Ajit soman) #1

In bitcore documentation, i am unable to find any method available to get balance for a particular address. is there is any method to get balance .thanks in advance


(Braydon Fuller) #2

There is getAddressBalance:


(Ajit soman) #3

thanks braydon for your reply…

i took reference from https://bitcore.io/api/service url and have created below code with few modification

var bitcore = require(‘bitcore-node’);

var Bitcoin = bitcore.services.Bitcoin;
var Web = bitcore.services.Web;

var node = new bitcore.Node({
datadir: ‘/home/ajit/.bitcore’,
network: {
name: ‘testnet’
},
“services”: [
{
name: ‘bitcoind’,
module: Bitcoin,
config: {}
},
{
name: ‘web’,
module: Web,
config: {
port: 3001
}
}
]
});

node.services.bitcoind.getAddressBalance(‘mqKdayy1sHEJuWA1DnyT2VWB1LZUQCqLhX’, false, function(err, total) {
console.log(total.balance);
});

i am getting this exception

node.services.bitcoind.getAddressBalance(‘mqKdayy1sHEJuWA1DnyT2VWB1LZUQCqLhX’,
^
TypeError: Cannot read property ‘getAddressBalance’ of undefined
at Object. (/home/ajit/Desktop/my wallet/bitcoreNode.js:28:23)
at Module._compile (module.js:460:26)
at Object.Module._extensions…js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3


(Braydon Fuller) #4

Have you started the node?


(Braydon Fuller) #5

Also see: https://bitcore.io/guides/service-development


(Ajit soman) #6

i have started bitcore using bitcored command ,it is running well . i am able to execute command like bitcore call getBlock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

but still i am getting same exception… Cannot read property ‘getAddressBalance’ of undefined


(Ajit soman) #7

i am unable to understand how this line node.services.bitcoind.getAddressBalance(…) able to access getAddressBalance method ,does it interact with running bitcoind process ?

but when i am performing the below task iam able to access getAddressBalance method

var bitcore = require(‘bitcore-node’);

var Bitcoin = bitcore.services.Bitcoin;

Bitcoin.prototype.getAddressBalance

but still iam getting exception … get undefined in bitcoind.js file


(Ajit soman) #8

I have made few modification in my code and able to access getAddressBalance method :

var bitcore = require(‘bitcore-node’);
var Bitcoin = bitcore.services.Bitcoin;
var Web = bitcore.services.Web;
var node = new bitcore.Node({
datadir: ‘/home/ajit/.bitcoin’,
network: {
name: ‘testnet’
},
“services”: [
{
name: ‘bitcoind’,
module: Bitcoin,
config: {}
},
{
name: ‘web’,
module: Web,
config: {
port: 3001
}
}
]
});
node.start(function() { // i have added these two method start() and on() in my program
//start the node so the node.on(‘ready’) is actually called.
});
node.on(‘ready’, function() {
console.log(‘Bitcoin Node Ready’);
});
node.services.bitcoind.getAddressBalance(‘mqKdayy1sHEJuWA1DnyT2VWB1LZUQCqLhX’, false, function(err, total) {
console.log(total.balance);
});

but now iam getting exception:-

/home/ajit/Desktop/my wallet/node_modules/bitcore-node/lib/services/bitcoind.js:138
var client = self.nodes[self.nodesIndex].client;
^
TypeError: Cannot read property ‘client’ of undefined
at Bitcoin.Object.defineProperty.get [as client] (/home/ajit/Desktop/my wallet/node_modules/bitcore-node/lib/services/bitcoind.js:138:47)
at Bitcoin.getAddressBalance (/home/ajit/Desktop/my wallet/node_modules/bitcore-node/lib/services/bitcoind.js:1032:9)
at Object. (/home/ajit/Desktop/my wallet/balance.js:35:24)
at Module._compile (module.js:460:26)
at Object.Module._extensions…js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

does the client denotes to running process bitcoind…


(Braydon Fuller) #9

Please see https://github.com/bitpay/insight-api as an example of how it’s working.

Also some of the CLI commands that have JSON as an argument will not work at this time because of this: https://github.com/bitpay/bitcore-node/issues/432


Bitcore v4.0.0
(Braydon Fuller) #10

The API methods won’t be ready until the node has started. It looks like you’re calling ready, however not waiting for it to completed. BTW, it’s a bit difficult to read the pasted code examples with lack of indentation. Triple back ticks before and after a code block will format such as:

node.start(function(err) {     
  //....
});

When node.start is called it will call start on all registered services, waiting for those to be completed, and once all services are ready the callback for node will be called. The bitcoind service itself connects via ZMQ and RPC to a running instance of bitcoind (version 0.12-bitcore), this can spawn the process or connect to an already running process. Please see the bitcoind service docs for configuration options: https://github.com/bitpay/bitcore-node/blob/master/docs/services/bitcoind.md


(Ajit soman) #11

i have put console.log inside node.start method in my program :-

node.start(function(err) {
  if(err){
  console.log(err);
}

output:

[2016-06-01T18:35:42.844Z] info: Starting bitcoind
[Error: Bitcoin configuration options “spawn” or “connect” are expected]

here is my configuration:

var node = new bitcore.Node({
  datadir: '/home/ajit/.bitcoin',
  network: {
    name: 'testnet'
  },
  "services": [
    {
      name: 'bitcoind',
      module: Bitcoin,
      config: {} // i  have added config: {options:connect} but i am still  getting same error
    },
    {
      name: 'web',
      module: Web,
      config: {
        port: 3001
      }
    }
  ]
});

(Braydon Fuller) #12

One note is that “datadir” isn’t required to be set anymore with bitcore@4.0.0 (and bitcore-node@3.0.0).

Did you include the connect as an array of node options, such as?

config: {
  connect: [
    {
      "rpchost": "127.0.0.1",
      "rpcport": 30521,
      "rpcuser": "bitcoin",
      "rpcpassword": "local321",
      "zmqpubrawtx": "tcp://127.0.0.1:30611"
    }
  ]
}

Also, to use “connect”, you’ll need to have an already running bitcoind (see https://bitcore.io/guides/bitcoin) running configured with the above options.


(Ajit soman) #13

Thanks braydon… i was able to connect to running bitcoind and able to run getInfo() method but still i am unable to get balance.i am getting undefiend. also when executing bitcoin-cli getinfo i am getting below output:

ajit@ajit-Inspiron-N5050:~/Desktop/my wallet$ bitcoin-cli getinfo
{
  "version": 120100,
  "protocolversion": 70012,
  "walletversion": 60000,
  "balance": 0.02826566,
  "blocks": 868040,
  "timeoffset": -5,
  "connections": 8,
  "proxy": "",
  "difficulty": 1,
  "testnet": true,
  "keypoololdest": 1411198114,
  "keypoolsize": 101,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": "Warning: unknown new rules activated (versionbit 28)"
}

but from my code i am getting output:

{ version: 120100,
  protocolVersion: 70012,
  blocks: 868040,
  timeOffset: -5,
  connections: 8,
  proxy: '',
  difficulty: 1,
  testnet: true,         // ------->getting testnet true !!
  relayFee: 0.00001,
  errors: 'Warning: unknown new rules activated (versionbit 28)',
  network: 'livenet' } // ------->getting network livenet !!
[2016-06-02T07:01:11.522Z] info: Beginning shutdown
[2016-06-02T07:01:11.523Z] info: Stopping bitcoind
[2016-06-02T07:01:11.993Z] warn: ZMQ connection delay: tcp://127.0.0.1:8332
[2016-06-02T07:01:11.995Z] info: ZMQ connected to: tcp://127.0.0.1:8332
[2016-06-02T07:01:41.524Z] warn: ZMQ disconnect: tcp://127.0.0.1:8332
[2016-06-02T07:01:42.026Z] warn: ZMQ connection delay: tcp://127.0.0.1:8332
[2016-06-02T07:01:42.026Z] info: ZMQ connected to: tcp://127.0.0.1:8332

here is my updated code :-

var bitcore = require('bitcore-node');
var Bitcoin = bitcore.services.Bitcoin;
var Web = bitcore.services.Web;

var node = new bitcore.Node({
  // datadir: '/home/ajit/.bitcoin',
  network: {
    name: 'testnet'
  },
  "services": [{
    name: 'bitcoind',
    module: Bitcoin,
    config: {
      connect: [{
        "rpchost": "127.0.0.1",
        "rpcport": 8332,
        "rpcuser": "9808908178",
        "rpcpassword": "98808908178",
        "zmqpubrawtx": "tcp://127.0.0.1:8332"

      }]
    }
  }]
});
node.start(function(err) {
  if (err) {
    console.log(err);
  }
});

node.on('ready', function() {

  node.services.bitcoind.getAddressBalance('mqKdayy1sHEJuWA1DnyT2VWB1LZUQCqLhX', false, function(err, total) {
    console.log(total.balance); 
  });
  node.services.bitcoind.getInfo(function(err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
      node.stop(function() {
        // the shutdown is complete
      });
    }
  })
});

(Braydon Fuller) #14

Please see: https://github.com/bitpay/bitcore-node/blob/master/lib/node.js#L74-84, the network should just be a string in the options you pass into Node:

  network: "testnet",

#15

I met the same problem with you.
Have you solved it?


(Ajit soman) #16

No


(Lalitpatadiya12) #17

@Ajit_Soman and @caker

I met the same problem with you.
Have you solved it?