Gem Version Gem Downloads Build Status codecov Maintainability

Ruby Nano RPC Logo

Nano RPC is a Ruby wrapper for making Remote Procedure Calls against Nano digital 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 RPC calls using Ruby hashes or you can use proxy objects for terser code.

Raw RPC Calls

The NanoRpc::Node object 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 node connection:

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

# or connect to a custom node
node = NanoRpc::Node.new(host: 'mynanonode', port: 1234)

If you're using Nanode or similar service that requires Authorization key in HTTP header, you can specify it using auth.

node = NanoRpc::Node.new(auth: 'someauthkey')

You can also specify custom headers as a hash. These will be sent with every RPC request.

node = NanoRpc::Node.new(headers: { 'Authorization' => 'someauthkey' })

Once the node is setup, make a call, passing the action and data:

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

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

data = node.call(:account_balance, account: 'xrb_1234')
# => {"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.

 = NanoRpc.node.('xrb_1234') # Account address required
accounts = NanoRpc.node.accounts(%w[xrb_1234 xrb_456]) # Array of account addresses required
wallet = NanoRpc.node.wallet('3AF91AE') # Wallet seed required

You can call standard RPC methods on each object:

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

There are also helper methods to bypass repetitive nested calls:

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

To convert from Nano to raw and back, use #to_raw and #to_nano, available on all Numeric objects:

4622800482000000000000000000000000.to_nano
# => 4622.800482

4622.800482.to_raw
# => 4622800482000000000000000000000000

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.