module Bluzelle

Public Class Methods

new_client(options) click to toggle source
# File lib/bluzelle.rb, line 43
def self.new_client options
  raise OptionsError, 'address is required' unless options.fetch('address', nil)
  raise OptionsError, 'mnemonic is required' unless options.fetch('mnemonic', nil)

  Bluzelle::validate_gas_info options.fetch('gas_info', {})

  options['debug'] = false unless options.fetch('debug', false)
  options['chain_id'] = DEFAULT_CHAIN_ID unless options.fetch('chain_id', nil)
  options['endpoint'] = DEFAULT_ENDPOINT unless options.fetch('endpoint', nil)

  c = Client.new options
  c.setup_logging
  c.set_private_key
  c.verify_address
  c.set_account
  c
end

Private Class Methods

base64_encode(b) click to toggle source
# File lib/bluzelle.rb, line 453
def self.base64_encode(b)
  Base64.strict_encode64 b
end
bin_to_hex(b) click to toggle source
# File lib/bluzelle.rb, line 461
def self.bin_to_hex(b)
  Secp256k1::Utils.encode_hex b
end
hex_to_ascii(h) click to toggle source
# File lib/bluzelle.rb, line 465
def self.hex_to_ascii(h)
  [h].pack('H*')
end
hex_to_bin(h) click to toggle source
# File lib/bluzelle.rb, line 457
def self.hex_to_bin(h)
  Secp256k1::Utils.decode_hex h
end
json_dumps(h) click to toggle source
# File lib/bluzelle.rb, line 473
def self.json_dumps(h)
  JSON.dump h
  # h.to_json
  # Hash[*h.sort.flatten].to_json
end
lease_blocks_to_seconds(blocks) click to toggle source
# File lib/bluzelle.rb, line 519
def self.lease_blocks_to_seconds(blocks)
  blocks * BLOCK_TIME_IN_SECONDS
end
lease_info_to_blocks(lease_info) click to toggle source
# File lib/bluzelle.rb, line 479
def self.lease_info_to_blocks(lease_info)
  if !lease_info
    raise OptionsError, 'provided lease info is nil'
  end
  unless lease_info.class.equal?(Hash)
    raise OptionsError, 'lease_info should be a hash of {days, hours, minutes, seconds}'
  end

  days = lease_info.fetch('days', 0)
  hours = lease_info.fetch('hours', 0)
  minutes = lease_info.fetch('minutes', 0)
  seconds = lease_info.fetch('seconds', 0)

  if seconds
    unless seconds.class.equal?(Integer)
      raise OptionsError, 'lease_info[seconds] should be an int'
    end
  end
  if minutes
    unless minutes.class.equal?(Integer)
      raise OptionsError, 'lease_info[minutes] should be an int'
    end
  end
  if hours
    unless hours.class.equal?(Integer)
      raise OptionsError, 'lease_info[hours] should be an int'
    end
  end
  if days
    unless days.class.equal?(Integer)
      raise OptionsError, 'lease_info[days] should be an int'
    end
  end

  seconds += days * 24 * 60 * 60
      seconds += hours * 60 * 60
      seconds += minutes * 60
  seconds / BLOCK_TIME_IN_SECONDS # rounded down
end
make_random_string(size) click to toggle source
# File lib/bluzelle.rb, line 469
def self.make_random_string(size)
  SecureRandom.alphanumeric size
end
validate_gas_info(gas_info) click to toggle source
# File lib/bluzelle.rb, line 523
def self.validate_gas_info gas_info
  unless gas_info.class.equal?(Hash)
    raise OptionsError, 'gas_info should be a hash of {gas_price, max_fee, max_gas}'
  end

  gas_info_keys = %w[gas_price max_fee max_gas]
  gas_info_keys.each do |k|
    v = gas_info.fetch(k, 0)
    unless v.class.equal?(Integer)
      raise OptionsError, "gas_info[#{k}] should be an int"
    end

    gas_info[k] = v
  end
  gas_info
end