specs Maintained Last Commit Tag Stars License: MIT


A Solana RPC Client for Ruby. This gem provides a wrapper methods for Solana RPC JSON API https://docs.solana.com/developing/clients/jsonrpc-api.

Getting started


This gem requires Ruby 2.6+ and h Rails 6.0+. It MIGHT work with lower versions, but was not tested with them. Add the following line to your Gemfile:

gem 'solana_rpc_ruby'

Then run bundle install

Next, you need to run the generator:

rails g solana_rpc_ruby:install

The latter command will generate a new config file config/initializers/solana_rpc_ruby_config.rb looking like this:

require 'solana_rpc_ruby'

SolanaRpcRuby.config do |c|
  c.cluster = 'https://api.testnet.solana.com'
  c.json_rpc_version = '2.0'
  # ...other options

You can customize it to your needs.

Usage examples


# If you set default cluster you don't need to pass it every time.
method_wrapper = SolanaRpcRuby::MethodsWrapper.new(
  # optional, if not passed, default cluster from config will be used
  cluster: 'https://api.testnet.solana.com',

  # optional, if not passed, default random number 
  # from range 1 to 99_999 will be used
  id: 123 

response = method_wrapper.()
puts response

# You can check cluster and id that are used.

Subscription Websocket (BETA)

ws_method_wrapper = SolanaRpcRuby::WebsocketsMethodsWrapper.new(
  # optional, if not passed, default ws_cluster from config will be used
  cluster: 'ws://api.testnet.solana.com',

  # optional, if not passed, default random number 
  # from range 1 to 99_999 will be used
  id: 123 

# You should see stream of messages in your console.

# You can pass a block to do something with websocket's messages, ie:
block = Proc.new do |message|
  json = JSON.parse(message)
  puts json['params']


# You can check cluster and id that are used.

Websockets usage in Rails

You can easily plug-in websockets connection to your rails app by using ActionCable. Here is an example for development environment. More explanation on Action Cable here: https://www.pluralsight.com/guides/updating-a-rails-app's-wall-feed-in-real-time-with-actioncable

  1. Make sure that you have action_cable and solana_rpc_ruby gems installed properly. Also install redis unless you have it.

  2. Mount action_cable in routes.rb.

    Rails.application.routes.draw do
    mount ActionCable.server => '/cable'
  3. Update config/environments/development.rb.

    config.action_cable.url = "ws://localhost:3000/cable"
    config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
  4. Update adapter in cable.yml.

    adapter: redis
    url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  5. Create a channel.

    rails g channel wall
  6. Your wall_channel.rb should look like this:

    class WallChannel < ApplicationCable::Channel
    def subscribed
    stream_from "wall_channel"

def unsubscribed # Any cleanup needed when channel is unsubscribed end end

6. Your `wall_channel.js` should look like this (json keys are configured for `root_subscription` method response):
import consumer from "./consumer"

consumer.subscriptions.create("WallChannel", {
  connected() {
    console.log("Connected to WallChannel");
    // Called when the subscription is ready for use on the server

  disconnected() {
    // Called when the subscription has been terminated by the server

  received(data) {
    let wall = document.getElementById('wall');

    wall.innerHTML += "<p>Result: "+ data['message']['result'] + "</p>";
    // Called when there's incoming data on the websocket for this channel

  1. Create placeholder somewhere in your view for messages.

    <div id='wall' style='overflow-y: scroll; height:400px;''>
    <h1>Solana subscription messages</h1>
  2. Create a script with a block to run websockets (script/websockets_solana.rb).

    require_relative '../config/environment'

ws_method_wrapper = SolanaRpcRuby::WebsocketsMethodsWrapper.new

Example of block that can be passed to the method to manipulate the data.

block = Proc.new do |message| json = JSON.parse(message)

ActionCable.server.broadcast( "wall_channel", { message: json['params'] } ) end


9. Run `rails s`, open webpage where you put your placeholder.
10. Open `http://localhost:3000/address_with_websockets_view`.
11. Run `rails r script/websockets_solana.rb` in another terminal window.
12. You should see incoming websocket messages on your webpage.
### Demo scripts
Gem is coming with demo scripts that you can run and test API and Websockets.

1. Clone the repo
2. Set the gemset
3. Run `ruby demo.rb` or `ruby demo_ws_METHOD.rb` to see example output.
4. Check the gem or Solana JSON RPC API docs to get more information about method usage and modify demo scripts loosely.

All info about methods you can find in the docs on: https://www.rubydoc.info/github/Block-Logic/solana-rpc-ruby/main/SolanaRpcRuby

Also, as a reference you can use docs from solana: https://docs.solana.com/developing/clients/jsonrpc-api
## License

Copyright (c) [Block Logic Team]. License type is [MIT](https://github.com/Block-Logic/solana-rpc-ruby/blob/main/LICENSE).