class Peatio::Eos::Client

Constants

Error

Public Class Methods

new(endpoint, idle_timeout: 500) click to toggle source
# File lib/ultex/eos/client.rb, line 21
def initialize(endpoint, idle_timeout: 500)
  @rpc_endpoint = URI.parse(endpoint)
  @idle_timeout = idle_timeout
end

Public Instance Methods

json_rpc(path, params=nil, port=nil) click to toggle source
# File lib/ultex/eos/client.rb, line 26
def json_rpc(path, params=nil, port=nil)
  # We need to communicate with keosd to sign transaction,
  # keosd is running on non default eos port which is 8900
  # and passed to json_rpc function when we sign transaction
  rpc = URI.parse(@rpc_endpoint.to_s)
  rpc.port = (port.present? ? port : @rpc_endpoint.port)
  response = connection(rpc).post do |req|
    req.url path

    # To communicate with keosd to sign transaction we need to pass Host param with keosd port
    req.headers["Host"] = "0.0.0.0:#{port}" if port.present?
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json if params.present?
    req.options.timeout = 120
  end
  response.assert_success!
  response = JSON.parse(response.body)
  return response if response.is_a?(Array) # get balance call return an array

  response["error"].tap {|error| raise ResponseError.new(error["code"], error["message"]) if error }
  response
rescue Faraday::Error => e
  raise ConnectionError, e
rescue StandardError => e
  raise Error, e
end

Private Instance Methods

connection(rpc) click to toggle source
# File lib/ultex/eos/client.rb, line 55
def connection(rpc)
  Faraday.new(rpc) do |f|
    f.adapter :net_http_persistent,
              pool_size: 5,
              idle_timeout: @idle_timeout,
              open_timeout: @idle_timeout,
              timeout: @idle_timeout
  end
end