class Voucher::ChainContract

Attributes

contract[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/voucher/chain_contract.rb, line 7
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

Public Instance Methods

ethereum() click to toggle source
# File lib/voucher/chain_contract.rb, line 19
def ethereum
  @client ||= Ethereum::HttpClient.new(Config.ethereum_url)
  @client.default_account = Config.default_account

  @client
end
winning_hash(block_height, shard) click to toggle source
# File lib/voucher/chain_contract.rb, line 26
def winning_hash(block_height, shard)
  @roots[shard] ||= find_root(block_height, shard)
end

Private Instance Methods

eth_format(value) click to toggle source
# File lib/voucher/chain_contract.rb, line 40
def eth_format(value)
  hex = @formatter.from_ascii(value)

  "0x#{hex}"
end
find_root(block_height, shard) click to toggle source
# File lib/voucher/chain_contract.rb, line 32
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