class Platon::Formatter

Constants

UNITS

Public Instance Methods

from_address(address) click to toggle source
# File lib/platon/formatter.rb, line 80
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/platon/formatter.rb, line 45
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/platon/formatter.rb, line 25
def from_bool(boolval)
  return nil if boolval.nil?
  boolval ? "1" : "0"
end
from_gvon(amount, unit = "gvon") click to toggle source
# File lib/platon/formatter.rb, line 75
def from_gvon(amount, unit = "gvon") 
  return nil if amount.nil?
  (BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16)).to_s rescue nil
end
from_input(string) click to toggle source
# File lib/platon/formatter.rb, line 89
def from_input(string)
  string[10..-1].scan(/.{64}/)
end
from_payload(args) click to toggle source
# File lib/platon/formatter.rb, line 106
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/platon/formatter.rb, line 50
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_von(amount, unit = "atp") click to toggle source
# File lib/platon/formatter.rb, line 65
def from_von(amount, unit = "atp") #TODO
  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/platon/formatter.rb, line 102
def get_base_type(typename)
  typename.gsub(/\d+/,'')
end
output_to_address(bytes) click to toggle source
# File lib/platon/formatter.rb, line 111
def output_to_address(bytes)
  self.to_address(bytes)
end
output_to_bool(bytes) click to toggle source
# File lib/platon/formatter.rb, line 131
def output_to_bool(bytes)
  self.to_bool(bytes.gsub(/^0x/,''))
end
output_to_bytes(bytes) click to toggle source
# File lib/platon/formatter.rb, line 115
def output_to_bytes(bytes)
  self.to_utf8(bytes)
end
output_to_int(bytes) click to toggle source
# File lib/platon/formatter.rb, line 127
def output_to_int(bytes)
  self.to_int(bytes)
end
output_to_string(bytes) click to toggle source
# File lib/platon/formatter.rb, line 119
def output_to_string(bytes)
  self.to_utf8(bytes)
end
output_to_uint(bytes) click to toggle source
# File lib/platon/formatter.rb, line 123
def output_to_uint(bytes)
  self.to_int(bytes)
end
to_address(hexstring) click to toggle source
# File lib/platon/formatter.rb, line 55
def to_address(hexstring)
  return "0x0000000000000000000000000000000000000000" if hexstring.nil?
  "0x" + hexstring[-40..-1]
end
to_ascii(hexstring) click to toggle source
# File lib/platon/formatter.rb, line 35
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/platon/formatter.rb, line 30
def to_bool(hexstring)
  return nil if hexstring.nil?
  (hexstring == "0000000000000000000000000000000000000000000000000000000000000001")
end
to_gvon(amount, unit = "gvon") click to toggle source
# File lib/platon/formatter.rb, line 70
def to_gvon(amount, unit = "gvon") 
  return nil if amount.nil?
  BigDecimal(UNITS[unit.to_sym] * amount, 16).to_s.to_i rescue nil
end
to_int(hexstring) click to toggle source
# File lib/platon/formatter.rb, line 97
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/platon/formatter.rb, line 135
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/platon/formatter.rb, line 85
def to_param(string)
  string.ljust(64, '0')
end
to_twos_complement(number) click to toggle source
# File lib/platon/formatter.rb, line 93
def to_twos_complement(number)
  (number & ((1 << 256) - 1)).to_s(16)
end
to_utf8(hexstring) click to toggle source
# File lib/platon/formatter.rb, line 40
def to_utf8(hexstring)
  return nil if hexstring.nil?
  hexstring.gsub(/^0x/,'').scan(/.{2}/).collect {|x| x.hex}.pack("U*").delete("\u0000")
end
to_von(amount, unit = "atp") click to toggle source
# File lib/platon/formatter.rb, line 60
def to_von(amount, unit = "atp") #TODO
  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/platon/formatter.rb, line 18
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