class Xgt::Ruby::Rpc

Public Class Methods

new(url, client: nil) click to toggle source
# File lib/xgt/ruby.rb, line 21
def initialize(url, client: nil)
  @url = url
  @client = client || Faraday::Connection.new(url: @url) do |faraday|
    faraday.request(:json)
    faraday.request :retry,
      max: 10,
      interval: 0.05,
      interval_randomness: 0.5,
      backoff_factor: 2,
      # TODO: Make this more specific
      exceptions: ['Error']
    faraday.response(:json)
    faraday.response(:logger) if ENV['LOGGING_ENABLED']
    faraday.adapter(Faraday.default_adapter)
  end
end

Public Instance Methods

broadcast_transaction(txn, wifs, chain_id) click to toggle source
# File lib/xgt/ruby.rb, line 66
def broadcast_transaction(txn, wifs, chain_id)
  signed = Xgt::Ruby::Auth.sign_transaction(self, txn, wifs, chain_id)
  response = self.call('transaction_api.broadcast_transaction', [signed])
  response['id']
end
call(mthd, params) click to toggle source
# File lib/xgt/ruby.rb, line 38
def call(mthd, params)
  id = SecureRandom.hex(6).to_i(16)
  payload = {
    'jsonrpc' => '2.0',
    'method' => mthd,
    'id' => id
  }
  payload['params'] = params unless params.nil?

  response = @client.post('/', payload)

  # TODO: Verify status code
  unless response.body
    raise RpcError.new(%(No response body!\n#{response.inspect}), response)
  end

  if response.body['error']
    raise RpcError.new(%(Endpoint returned an error response!\n#{JSON.pretty_generate(response.body['error'])}), response)
  end

  unless response.body['result']
    raise RpcError.new(%(No result in response body!\n#{response.inspect}), response)
  end

  # TODO: XXX: Breaking change!
  response.body['result']
end
transaction_ready?(id) click to toggle source
# File lib/xgt/ruby.rb, line 72
def transaction_ready?(id)
  begin
    self.call('wallet_history_api.get_transaction', { 'id' => id })
    true
  rescue Xgt::Ruby::RpcError => e
    message = e&.response
               &.body
               &.fetch('error', nil)
               &.fetch('message', nil)
    wait_regexps = Regexp.union(%r(transaction.*?>.*?trx_in_block))
    raise e unless message.match(wait_regexps)
    false
  end
end