An RPC wrapper for RaiBlocks written in Ruby. It connects to an individual node that you control. There's a client object you can use to make explicit RPC calls as well as proxy objects with logically grouped methods.
To run a RaiBlocks node locally, see Docker documentation.
Add this line to your application's Gemfile:
gem 'raiblocks_rpc'And then execute:
$ bundle
Or install it yourself as:
$ gem install raiblocks_rpc
There are two ways to use this gem. You can make direct calls to the RPC client or use the provided proxy objects.
In either case, the client should first be configured to connect to a RaiBlocks node. If you do not specify host or port before using the client, then localhost:7076 will be used by default.
RaiblocksRpc::Client.host = 'localhost'
RaiblocksRpc::Client.port = 7076You can use the RPC client to make raw RPC calls to a RaiBlocks node according to the documentation.
Every call 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.
RaiblocksRpc::Client.instance.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 included so you have several options for accessing values.
data = RaiblocksRpc::Client.instance.call(:account_balance, account: 'xrb_someaddress1234')
# => {"balance"=>100, "pending"=>0}
data.balance
# => 100
data[:balance]
# => 100
data['balance']
# => 100A few proxy objects are provided as a means to logically group RPC calls. Here we do not strictly follow the grouping as expressed in the RaiBlocks RPC documentation. Instead, the following objects are provided:
RaiblocksRpc::Account # { account: 'xrb_address12345' }
RaiblocksRpc::Accounts # { accounts: ['xrb_address12345', 'xrb_address67890] }
RaiblocksRpc::Wallet # { wallet: 'F3093AB' }
RaiblocksRpc::Network
RaiblocksRpc::Node
RaiblocksRpc::UtilAccount, Accounts, and Wallet each require parameters to be passed during initialization. You can then make calls on these objects without needing to pass in the params for subsequent calls.
Methods whose prefix matches the class name, such as account_balance, also have an abbreviated version, in this case balance.
account = RaiblocksRpc::Account.new('xrb_someaddress1234')
account.balance
# => {"balance"=>100, "pending"=>0}
account.account_balance
# => {"balance"=>100, "pending"=>0}Some methods appear on multiple objects for convenience.
RaiblocksRpc::Node.new.block_count
# => {"count"=>314848, "unchecked"=>4793586}
RaiblocksRpc::Network.new.block_count
# => {"count"=>314848, "unchecked"=>4793642}The gem is available as open source under the terms of the MIT License.