Sha256: a6b8355b8c8984cf9722c0df0bbd99b4a5c7d16f79457089d105e14f3d237e55

Contents?: true

Size: 983 Bytes

Versions: 1

Compression:

Stored size: 983 Bytes

Contents

# frozen_string_literal: true

module Voucher
  class ChainContract
    attr_reader :contract

    def initialize(options = {})
      @contract = Ethereum::Contract.create(
        name: options[:name] || 'ChainContractClient',
        address: options[:address],
        abi: options[:abi],
        client: ethereum
      )

      @roots = {}
      @formatter = Ethereum::Formatter.new
    end

    def ethereum
      @client ||= Ethereum::HttpClient.new(Config.ethereum_url)
      @client.default_account = Config.default_account

      @client
    end

    def winning_hash(block_height, shard)
      @roots[shard] ||= find_root(block_height, shard)
    end

    private

    def find_root(block_height, shard)
      root = contract.call.get_block_root(block_height, shard)

      raise RootHashNotFound.new(block_height, shard) if root.blank?

      eth_format(root)
    end

    def eth_format(value)
      hex = @formatter.from_ascii(value)

      "0x#{hex}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voucher-0.3.0 lib/voucher/chain_contract.rb