Sha256: b4d8e105da127ca92c44f5fe20f8e326d4cef9dfa7fc8b1c6713346c0fc46f55

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'json'

module RPC
  class << self
    def json_rpc_call(method, params, url)
      uri = URI(url)
      req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
      req.body = {
        'id' => 1,
        'jsonrpc' => '2.0',
        'method' => method,
        'params' => params.all?(nil) ? [] : params
      }.to_json
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true if uri.instance_of? URI::HTTPS
      res = http.request(req)
      # puts res unless res.is_a?(Net::HTTPSuccess)

      result = JSON.parse(res.body)
      raise result['error'] if result['error']

      result['result']
    end

    def chain_getBlockHash(url, block_number = nil)
      json_rpc_call('chain_getBlockHash', [block_number], url)
    end

    def chain_getBlock(url, at = nil)
      json_rpc_call('chain_getBlock', [at], url)
    end

    def state_getRuntimeVersion(url, at = nil)
      json_rpc_call('state_getRuntimeVersion', [at], url)
    end

    def state_getMetadata(url, at = nil)
      json_rpc_call('state_getMetadata', [at], url)
    end

    def state_getStorage(url, key, at = nil)
      json_rpc_call('state_getStorage', [key, at], url)
    end

    def eth_call(url, to, data, at_block_number = nil)
      json_rpc_call('eth_call', [
                      {
                        'from' => nil,
                        'to' => to,
                        'data' => data
                      },
                      at_block_number
                    ], url)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scale_rb-0.1.1 lib/client/rpc.rb
scale_rb-0.1.0 lib/client/rpc.rb