class EvmClient::Formatter

Constants

UNITS

Public Instance Methods

from_address(address) click to toggle source
# File lib/evm_client/formatter.rb, line 84
def from_address(address)
  return "0x0000000000000000000000000000000000000000" if address.nil?
  address.gsub(/^0x/,'').rjust(64, "0")
end
from_ascii(ascii_string) click to toggle source
# File lib/evm_client/formatter.rb, line 59
def from_ascii(ascii_string)
  return nil if ascii_string.nil?
  ascii_string.unpack('H*')[0]
end
from_bool(boolval) click to toggle source
# File lib/evm_client/formatter.rb, line 39
def from_bool(boolval)
  return nil if boolval.nil?
  boolval ? "1" : "0"
end
from_input(string) click to toggle source
# File lib/evm_client/formatter.rb, line 93
def from_input(string)
  string[10..-1].scan(/.{64}/)
end
from_payload(args) click to toggle source
# File lib/evm_client/formatter.rb, line 110
def from_payload(args)
  converter = "output_to_#{self.get_base_type(args[0])}".to_sym
  self.send(converter, args[1])
end
from_utf8(utf8_string) click to toggle source
# File lib/evm_client/formatter.rb, line 64
def from_utf8(utf8_string)
  return nil if utf8_string.nil?
  utf8_string.force_encoding('UTF-8').split("").collect {|x| x.ord.to_s(16).rjust(2, '0')}.join("")
end
from_wei(amount, unit = "ether") click to toggle source
# File lib/evm_client/formatter.rb, line 79
def from_wei(amount, unit = "ether")
  return nil if amount.nil?
  (BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16)).to_s rescue nil
end
get_base_type(typename) click to toggle source
# File lib/evm_client/formatter.rb, line 106
def get_base_type(typename)
  typename.gsub(/\d+/,'')
end
output_to_address(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 115
def output_to_address(bytes)
  self.to_address(bytes)
end
output_to_bool(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 135
def output_to_bool(bytes)
  self.to_bool(bytes.gsub(/^0x/,''))
end
output_to_bytes(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 119
def output_to_bytes(bytes)
  self.to_utf8(bytes)
end
output_to_int(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 131
def output_to_int(bytes)
  self.to_int(bytes)
end
output_to_string(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 123
def output_to_string(bytes)
  self.to_utf8(bytes)
end
output_to_uint(bytes) click to toggle source
# File lib/evm_client/formatter.rb, line 127
def output_to_uint(bytes)
  self.to_int(bytes)
end
to_address(hexstring) click to toggle source
# File lib/evm_client/formatter.rb, line 69
def to_address(hexstring)
  return "0x0000000000000000000000000000000000000000" if hexstring.nil?
  "0x" + hexstring[-40..-1]
end
to_ascii(hexstring) click to toggle source
# File lib/evm_client/formatter.rb, line 49
def to_ascii(hexstring)
  return nil if hexstring.nil?
  hexstring.gsub(/^0x/,'').scan(/.{2}/).collect {|x| x.hex}.pack("c*")
end
to_bool(hexstring) click to toggle source
# File lib/evm_client/formatter.rb, line 44
def to_bool(hexstring)
  return nil if hexstring.nil?
  (hexstring == "0000000000000000000000000000000000000000000000000000000000000001")
end
to_int(hexstring) click to toggle source
# File lib/evm_client/formatter.rb, line 101
def to_int(hexstring)
  return nil if hexstring.nil?
  (hexstring.gsub(/^0x/,'')[0..1] == "ff") ? (hexstring.hex - (2 ** 256)) : hexstring.hex
end
to_output(args) click to toggle source
# File lib/evm_client/formatter.rb, line 139
def to_output(args)
  converter = "output_to_#{self.get_base_type(args[0])}".to_sym
  self.send(converter, args[1])
end
to_param(string) click to toggle source
# File lib/evm_client/formatter.rb, line 89
def to_param(string)
  string.ljust(64, '0')
end
to_twos_complement(number) click to toggle source
# File lib/evm_client/formatter.rb, line 97
def to_twos_complement(number)
  (number & ((1 << 256) - 1)).to_s(16)
end
to_utf8(hexstring) click to toggle source
# File lib/evm_client/formatter.rb, line 54
def to_utf8(hexstring)
  return nil if hexstring.nil?
  hexstring.gsub(/^0x/,'').scan(/.{2}/).collect {|x| x.hex}.pack("U*").delete("\u0000")
end
to_wei(amount, unit = "ether") click to toggle source
# File lib/evm_client/formatter.rb, line 74
def to_wei(amount, unit = "ether")
  return nil if amount.nil?
  BigDecimal(UNITS[unit.to_sym] * amount, 16).to_s.to_i rescue nil
end
valid_address?(address_string) click to toggle source
# File lib/evm_client/formatter.rb, line 32
def valid_address?(address_string)
  address = address_string.gsub(/^0x/,'')
  return false if address == "0000000000000000000000000000000000000000"
  return false if address.length != 40
  return !(address.match(/[0-9a-fA-F]+/).nil?)
end