Module: SolanaRpcRuby::RequestBody

Included in:
MethodsWrapper, WebsocketClient, WebsocketsMethodsWrapper
Defined in:
lib/solana_rpc_ruby/request_body.rb

Instance Method Summary collapse

Instance Method Details

#base_body(id: 1) ⇒ Hash

Hash with default body params.

Parameters:

  • id (Integer) (defaults to: 1)

    Unique client-generated identifying integer.

Returns:

  • (Hash)

    hash with base params for every request.

Raises:

  • (ArgumentError)

30
31
32
33
34
35
36
37
# File 'lib/solana_rpc_ruby/request_body.rb', line 30

def base_body(id: 1)
  raise ArgumentError, 'id must be an integer' unless id.is_a?(Integer)

  {
    "jsonrpc": SolanaRpcRuby.json_rpc_version,
    "id": id
  }
end

#create_json_body(method, method_params: [], id: @id) ⇒ Json

Creates body sent to the solana JSON RPC API

From solana docs: To make a JSON-RPC request, send an HTTP POST request with a Content-Type: application/json header. The JSON request data should contain 4 fields: jsonrpc: <string>, set to “2.0” id: <number>, a unique client-generated identifying integer method: <string>, a string containing the method to be invoked params: <array>, a JSON array of ordered parameter values

Parameters:

  • method (string)

    method name.

  • method_params (Array) (defaults to: [])

    ordered array with required and/or optional params.

  • id (Integer) (defaults to: @id)

    Unique client-generated identifying integer.

Returns:

  • (Json)

    JSON string with body.


19
20
21
22
23
24
# File 'lib/solana_rpc_ruby/request_body.rb', line 19

def create_json_body(method, method_params: [], id: @id)
  body = base_body(id: id)
  body[:method] = method
  body[:params] = method_params if method_params.any?
  body.to_json
end