Class: EvmClient::Formatter

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

Constant Summary collapse

UNITS =
{
  wei:        1,
  kwei:       1000,
  ada:        1000,
  femtoether: 1000,
  mwei:       1000000,
  babbage:    1000000,
  picoether:  1000000,
  gwei:       1000000000,
  shannon:    1000000000,
  nanoether:  1000000000,
  nano:       1000000000,
  szabo:      1000000000000,
  microether: 1000000000000,
  micro:      1000000000000,
  finney:     1000000000000000,
  milliether: 1000000000000000,
  milli:      1000000000000000,
  ether:      1000000000000000000,
  eth:        1000000000000000000,
  kether:     1000000000000000000000,
  grand:      1000000000000000000000,
  einstein:   1000000000000000000000,
  mether:     1000000000000000000000000,
  gether:     1000000000000000000000000000,
  tether:     1000000000000000000000000000000
}

Instance Method Summary collapse

Instance Method Details

#from_address(address) ⇒ Object



84
85
86
87
# 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) ⇒ Object



59
60
61
62
# 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) ⇒ Object



39
40
41
42
# File 'lib/evm_client/formatter.rb', line 39

def from_bool(boolval)
  return nil if boolval.nil?
  boolval ? "1" : "0"
end

#from_input(string) ⇒ Object



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

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

#from_payload(args) ⇒ Object



110
111
112
113
# 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) ⇒ Object



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



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



106
107
108
# File 'lib/evm_client/formatter.rb', line 106

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

#output_to_address(bytes) ⇒ Object



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

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

#output_to_bool(bytes) ⇒ Object



135
136
137
# File 'lib/evm_client/formatter.rb', line 135

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

#output_to_bytes(bytes) ⇒ Object



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

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

#output_to_int(bytes) ⇒ Object



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

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

#output_to_string(bytes) ⇒ Object



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

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

#output_to_uint(bytes) ⇒ Object



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

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

#to_address(hexstring) ⇒ Object



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



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



44
45
46
47
# File 'lib/evm_client/formatter.rb', line 44

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

#to_int(hexstring) ⇒ Object



101
102
103
104
# 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) ⇒ Object



139
140
141
142
# 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) ⇒ Object



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

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

#to_twos_complement(number) ⇒ Object



97
98
99
# File 'lib/evm_client/formatter.rb', line 97

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

#to_utf8(hexstring) ⇒ Object



54
55
56
57
# 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") ⇒ Object



74
75
76
77
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# 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