Class: Platon::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/platon/formatter.rb

Constant Summary collapse

UNITS =
{
  'von':          1,
  'kvon':         1000,
  'mvon':         1000000,
  'gvon':         1000000000,  ## 10 ** 9
  'microatp':     1000000000000,
  'milliatp':     1000000000000000,
  'atp':          1000000000000000000, ## 10 ** 18
  'katp':         1000000000000000000000,
  'matp':         1000000000000000000000000,
  'gatp':         1000000000000000000000000000,
  'tatp':         1000000000000000000000000000000
}

Instance Method Summary collapse

Instance Method Details

#from_address(address) ⇒ Object



80
81
82
83
# 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) ⇒ Object



45
46
47
48
# 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) ⇒ Object



25
26
27
28
# 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") ⇒ Object



75
76
77
78
# 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) ⇒ Object



89
90
91
# File 'lib/platon/formatter.rb', line 89

def from_input(string)
  string[10..-1].scan(/.{64}/)
end

#from_payload(args) ⇒ Object



106
107
108
109
# 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) ⇒ Object



50
51
52
53
# 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") ⇒ Object

TODO



65
66
67
68
# 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) ⇒ Object



102
103
104
# File 'lib/platon/formatter.rb', line 102

def get_base_type(typename)
  typename.gsub(/\d+/,'')
end

#output_to_address(bytes) ⇒ Object



111
112
113
# File 'lib/platon/formatter.rb', line 111

def output_to_address(bytes)
  self.to_address(bytes)
end

#output_to_bool(bytes) ⇒ Object



131
132
133
# File 'lib/platon/formatter.rb', line 131

def output_to_bool(bytes)
  self.to_bool(bytes.gsub(/^0x/,''))
end

#output_to_bytes(bytes) ⇒ Object



115
116
117
# File 'lib/platon/formatter.rb', line 115

def output_to_bytes(bytes)
  self.to_utf8(bytes)
end

#output_to_int(bytes) ⇒ Object



127
128
129
# File 'lib/platon/formatter.rb', line 127

def output_to_int(bytes)
  self.to_int(bytes)
end

#output_to_string(bytes) ⇒ Object



119
120
121
# File 'lib/platon/formatter.rb', line 119

def output_to_string(bytes)
  self.to_utf8(bytes)
end

#output_to_uint(bytes) ⇒ Object



123
124
125
# File 'lib/platon/formatter.rb', line 123

def output_to_uint(bytes)
  self.to_int(bytes)
end

#to_address(hexstring) ⇒ Object



55
56
57
58
# File 'lib/platon/formatter.rb', line 55

def to_address(hexstring)
  return "0x0000000000000000000000000000000000000000" if hexstring.nil?
  "0x" + hexstring[-40..-1]
end

#to_ascii(hexstring) ⇒ Object



35
36
37
38
# 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) ⇒ Object



30
31
32
33
# File 'lib/platon/formatter.rb', line 30

def to_bool(hexstring)
  return nil if hexstring.nil?
  (hexstring == "0000000000000000000000000000000000000000000000000000000000000001")
end

#to_gvon(amount, unit = "gvon") ⇒ Object



70
71
72
73
# 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) ⇒ Object



97
98
99
100
# 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) ⇒ Object



135
136
137
138
# 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) ⇒ Object



85
86
87
# File 'lib/platon/formatter.rb', line 85

def to_param(string)
  string.ljust(64, '0')
end

#to_twos_complement(number) ⇒ Object



93
94
95
# File 'lib/platon/formatter.rb', line 93

def to_twos_complement(number)
  (number & ((1 << 256) - 1)).to_s(16)
end

#to_utf8(hexstring) ⇒ Object



40
41
42
43
# 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") ⇒ Object

TODO



60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# 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