Browsed by
Category: restapi

Using ripple api to query network and address information

Using ripple api to query network and address information

Hey Everyone,

This will be a quick post.

It’s been awhile since I made a post on some of the interesting challenges I’ve faced on my side projects. This is a continuation of my blockchain restapi work first mentioned here: dash-node-error.

Not just limiting myself to running nodes, I wanted to experiment with using existing apis. I started off with the Ripple (XRP) api. The main two pieces of information I needed from the chain were a list of transactions and an address’ balance.

Perusing through the api, there appeared to be two existing apis that would suit my needs:

Get Account Balances - GET /v2/accounts/{:address}/balances
Get Account Transaction History - GET /v2/accounts/{:address}/transactions

The balances endpoint worked as expected. Below is an example url and response:

   URL: https://data.ripple.com/v2/accounts/rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn/balances?currency=USD

   {
      "result": "success",
      "ledger_index": ,
      "limit": 200,
      "balances": [
        {
          "currency": "XRP",
          "value": "200"
        },
      ]
   }

The transactions endpoint; however, had incomplete data. Example url:

https://data.ripple.com/v2/accounts//transactions?type=Payment

I noticed the transaction inputs did not add up to the account balance. After a bit of digging, the only thing I was able to find was that transactions were missing from exchanges that used sidechains. The solution for me was to merge data from the /payments endpoint. This allowed transaction data to mirror balances.

   URL: https://data.ripple.com/v2/accounts/rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn/payments?currency=USD

   {
      "result": "success",
      "count": 3,
      "marker": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn|20140910232220|000008803725|00001",
      "payments": [
       {
         "amount": "1.0",
         "delivered_amount": "1.0",
         "destination_balance_changes": [
          {
             "counterparty": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
             "currency": "USD",
             "value": "1"
           }
         ],
         "source_balance_changes": [
           {
             "counterparty": "ra5nK24KXen9AHvsdFTKHSANinZseWnPcX",
             "currency": "USD",
             "value": "-1"
           }
         ],
         "tx_index": 1,
         "currency": "USD",
         "destination": "ra5nK24KXen9AHvsdFTKHSANinZseWnPcX",
         "executed_time": "2014-06-02T22:47:50Z",
         "issuer": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
         "ledger_index": 6979192,
         "source": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
         "source_currency": "USD",
         "tx_hash": "7BF105CFE4EFE78ADB63FE4E03A851440551FE189FD4B51CAAD9279C9F534F0E",
         "transaction_cost": "1.0E-5"
       } 
      ]
   }
Dash node address balance querying error – ‘No information available for address’

Dash node address balance querying error – ‘No information available for address’

1 Solution

For those weary Googlers looking for the solution to the above error from the dash-cli.  When starting the dash daemon, the -addressindex=1 option must be set. If the blockchain has already been downloaded, you can enable address indexing by following the below steps:

  1. stop the daemon
  2. restart the daemon with the addressIndex and reIndex option enabled:
    dashd -rpcallowip=::/0 -reindex=1 -addressindex=1 -txindex=1
  3. wait for the blockchain to reindex

NOTE: enabling address indexing will increase the dash disk usage from about 3GB to 5GB (as of time of writing).

2 Problem

I recently starting working in blockchain and cryptocurrency development. My first hands-on project involved setting up a RESTApi that would directly talk with a Dash node daemon through the dash-cli command. I know, I could have just used an existing api implementation, like insight-api-dash, but that would be too easy.

I set up a dash node using a modified Dockerfile from docker-dashd. However, testing the node with the sample command:

dashd getaddressbalance '{"addresses": ["XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg"]}'

Resulted in the error message ‘No information available for address‘. Google searches resulted in no solutions; I decided to delve into the dash source code.

3 Discovery

After downloading the source and searching for the particular error string, I found the error getting returned from the getaddressbalance rpc procedure (rpcmisc.cpp):

for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
    if (!GetAddressIndex((*it).first, (*it).second, addressIndex)) {
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
    }
}

Interesting…but not really useful for figuring out why the procedure wasn’t returning the expected data. I needed to dig a little further, so I looked into the GetAddressIndex function. The function performed a few precondition checks that potentially would log an error message. I had previously checked the docker logs for the container but must have missed any meaningful messages. I took a closer look at the logs and found an interesting error message, ‘address index not enabled‘.

I was getting close and realized I probably missed an option during the initial start of the daemon. With more than 100 options to use, I wasn’t surprised. A few more minutes of digging and I found the answer I needed in main.cpp:

fAddressIndex = GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX);

Turns out I need to explicitly enable addressIndexing to perform the getaddressbalance and other address specific queries.

4 Solution

Once the problem was known, the solution is fairly simple. I just needed to restart the dash daemon with address indexing enabled.

Take #1, I stopped the daemon, added the option to the command, and restarted. I attempted another getaddressbalance and I received the same error message. Confused, I took a look at the logs and was greeted by an error message stating I needed to specify the -reindex=1 option for address indexing to take effect.

Take #2, I stopped the daemon, added the two options, restarted the daemon, executed the query and success!