# 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