![specs](https://github.com/Block-Logic/solana-rpc-ruby/actions/workflows/specs.yml/badge.svg) # 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 againt them. Add the following line to your Gemfile: ```ruby gem 'solana_rpc_ruby' ``` Then run `bundle install` Next, you need to run the generator: ```console 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: ```ruby 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 ```ruby # 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) ```ruby 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 0. Make sure that you have action_cable and solana_rpc_ruby gems installed properly. Also install redis unless you have it. 1. Mount action_cable in `routes.rb`. ``` Rails.application.routes.draw do mount ActionCable.server => '/cable' ... end ``` 2. Update `config/environments/development.rb`. ``` config.action_cable.url = "ws://localhost:3000/cable" config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/] ``` 3. Update adapter in `cable.yml`. ``` development: adapter: redis url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> ``` 4. Create a channel. ``` rails g channel wall ``` 5. 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 ``` 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 += "
Result: "+ data['message']['result'] + "
"; // Called when there's incoming data on the websocket for this channel } }); ``` 7. Create placeholder somewhere in your view for messages. ```