Sha256: 1d503e6bb8248683cdf4638a7d53180fb50b6775028341faa49d1152bb4e9e87

Contents?: true

Size: 825 Bytes

Versions: 3

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

module Voucher
  class ElectionContract
    attr_reader :contract, :ethereum

    def initialize(options = {})
      @ethereum = Ethereum::HttpClient.new(options[:ethereum_url])

      @contract = Ethereum::Contract.create(
        name: options[:name] || 'ElectionContractClient',
        address: options[:address],
        abi: options[:abi],
        client: ethereum
      )

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

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

    private

    def find_root(shard)
      root = contract.call.roots(shard)

      raise RootHashNotFound, 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

3 entries across 3 versions & 1 rubygems

Version Path
voucher-0.2.1 lib/voucher/election_contract.rb
voucher-0.1.1 lib/voucher/election_contract.rb
voucher-0.1.0 lib/voucher/election_contract.rb