Skip to content

Repository files navigation

specsMaintainedLast CommitTagStarsLicense: 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.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.configdo |c|
c.cluster='https://api.testnet.solana.com'c.json_rpc_version='2.0'# ...other optionsend

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 usedcluster: 'https://api.testnet.solana.com',# optional, if not passed, default random number # from range 1 to 99_999 will be usedid: 123)response=method_wrapper.get_account_info(account_pubkey)putsresponse# You can check cluster and id that are used.method_wrapper.clustermethod_wrapper.id

Subscription Websocket (BETA)

ws_method_wrapper=SolanaRpcRuby::WebsocketsMethodsWrapper.new(# optional, if not passed, default ws_cluster from config will be usedcluster: 'ws://api.testnet.solana.com',# optional, if not passed, default random number # from range 1 to 99_999 will be usedid: 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.newdo |message|
json=JSON.parse(message)putsjson['params']endws_method_wrapper.root_subscribe(&block)# You can check cluster and id that are used.ws_method_wrapper.clusterws_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.drawdomountActionCable.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: redisurl: <%= ENV.fetch("REDIS_URL"){"redis://localhost:6379/1"} %>
  1. Create a channel.
railsgchannelwall
  1. Your wall_channel.rb should look like this:
classWallChannel < ApplicationCable::Channeldefsubscribedstream_from"wall_channel"enddefunsubscribed# Any cleanup needed when channel is unsubscribedendend
  1. Your wall_channel.js should look like this (json keys are configured for root_subscription method response):
importconsumerfrom"./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){letwall=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.
<divid='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.newdo |message|
json=JSON.parse(message)ActionCable.server.broadcast("wall_channel",{message: json['params']})endws_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

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages