Ruby Nano RPC Logo

Nano RPC is a Ruby wrapper for making Remote Procedure Calls against Nano digitial currency nodes. Arbitrary RPC access is provided along with proxy objects that expose helper methods (Wiki).

To run a Nano node locally, see Nano Docker Docs.

Installation

Add this line to your application's Gemfile:

gem 'nano_rpc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nano_rpc

Usage

There are two ways to use this gem. You can make direct calls to the RPC client using Ruby hashes or you can use proxy objects for terser code.

Raw RPC Calls

The RCP client exposes raw Remote Procedure Call methods according to the Nano RPC Docs.

Every method requires an action, which is passed as the first argument to call. Depending on the action, there may be additional required or optional parameters that are passed as an options hash.

First setup the client:

# Connect to the default node (localhost:7076)
client = Nano.client

# or connect to a custom node
client = Nano::Client.new(host: 'mynanonode', port: 1234)

Then make a call, passing the action and data:

  client.call(:account_balance, account: 'xrb_someaddress1234')
  # => {"balance"=>100, "pending"=>0}

Response data are provided as Hashie objects with integer coercion, indifferent access, and method access.

  data = client.call(:account_balance, account: 'xrb_someaddress1234')
  # => {"balance"=>100, "pending"=>0}
  data.balance
  # => 100
  data[:balance]
  # => 100
  data['balance']
  # => 100

Proxy Objects / Helper Methods

Proxy objects are provided to ease interaction with the API by providing logically grouped helper methods. Here we do not strictly follow the grouping as expressed in the Nano RPC Docs. Instead, the following objects are provided:

Account, Accounts, and Wallet each require a single parameter to be passed during initialization (address, addresses, and seed, respectively). This parameter is persisted for subsequent calls. All RPC methods are provided directly as methods.

   = Nano::Account.new('xrb_someaddress1234')

  .
  # => {"balance"=>100, "pending"=>5}
  ..balance
  # => 100

There are also helper methods to bypass repetitive nested calls:

  .balance
  # => 100
  .pending_balance
  # => 5

You can point each proxy object at its own node by passing in a client instance:

  client = Nano::Client.new(host: 'mynanonode', port: 1234)
   = Nano::Account.new('xrb_someaddress1234', client: client)

For a comprehensive guide, see the Wiki.

Credits

Logo created by Andrei Luca (Twitter)

License

The gem is available as open source under the terms of the MIT License.