Skip to content

coingecko/solana-rpc-ruby

 
 

Repository files navigation

specs Maintained Last Commit Tag Stars License: MIT

solana_rpc_ruby

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

Requirements

This gem requires Ruby 2.7+ (as denoted in the .ruby-version file) and it has Rails 6.0+ on board. 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
end

You can customize it to your needs.

Usage examples

JSON RPC API

# 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.get_account_info(account_pubkey)
puts response

# You can check cluster and id that are used.
method_wrapper.cluster
method_wrapper.id

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.
ws_method_wrapper.root_subscribe

# 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']
end

ws_method_wrapper.root_subscribe(&block)

# You can check cluster and id that are used.
ws_method_wrapper.cluster
ws_method_wrapper.id

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'
  ...
end
  1. Update config/environments/development.rb.
config.action_cable.url = "ws://localhost:3000/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
  1. Update adapter in cable.yml.
development:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  1. Create a channel.
rails g channel wall
  1. Your wall_channel.rb should look like this:
class WallChannel < ApplicationCable::Channel
  def subscribed
    stream_from "wall_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end
  1. 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>
</div>
  1. 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

ws_method_wrapper.root_subscribe(&block)
  1. Run rails s, open webpage where you put your placeholder.
  2. Open http://localhost:3000/address_with_websockets_view.
  3. Run rails r script/websockets_solana.rb in another terminal window.
  4. 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.

About

A Solana RPC Client for Ruby

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 96.5%
  • HTML 3.1%
  • Other 0.4%