Class: Ethereum::Formatter
- Inherits:
-
Object
- Object
- Ethereum::Formatter
- Defined in:
- lib/ethereum/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
- #address_to_payload(address) ⇒ Object
- #bytes_to_payload(bytes) ⇒ Object
- #from_address(address) ⇒ Object
- #from_ascii(ascii_string) ⇒ Object
- #from_bool(boolval) ⇒ Object
- #from_input(string) ⇒ Object
- #from_payload(args) ⇒ Object
- #from_utf8(utf8_string) ⇒ Object
- #from_wei(amount, unit = "ether") ⇒ Object
- #get_base_type(typename) ⇒ Object
- #int_to_payload(int) ⇒ Object
- #output_to_address(bytes) ⇒ Object
- #output_to_bool(bytes) ⇒ Object
- #output_to_bytes(bytes) ⇒ Object
- #output_to_int(bytes) ⇒ Object
- #output_to_string(bytes) ⇒ Object
- #output_to_uint(bytes) ⇒ Object
- #string_to_payload(bytes) ⇒ Object
- #to_address(hexstring) ⇒ Object
- #to_ascii(hexstring) ⇒ Object
- #to_bool(hexstring) ⇒ Object
- #to_int(hexstring) ⇒ Object
- #to_output(args) ⇒ Object
- #to_param(string) ⇒ Object
- #to_payload(args) ⇒ Object
- #to_twos_complement(number) ⇒ Object
- #to_utf8(hexstring) ⇒ Object
- #to_wei(amount, unit = "ether") ⇒ Object
- #uint_to_payload(uint) ⇒ Object
- #valid_address?(address_string) ⇒ Boolean
Instance Method Details
#address_to_payload(address) ⇒ Object
106 107 108 |
# File 'lib/ethereum/formatter.rb', line 106 def address_to_payload(address) from_address(address) end |
#bytes_to_payload(bytes) ⇒ Object
118 119 120 |
# File 'lib/ethereum/formatter.rb', line 118 def bytes_to_payload(bytes) self.from_utf8(bytes).ljust(64, '0') end |
#from_address(address) ⇒ Object
84 85 86 87 |
# File 'lib/ethereum/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/ethereum/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/ethereum/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/ethereum/formatter.rb', line 93 def from_input(string) string[10..-1].scan(/.{64}/) end |
#from_payload(args) ⇒ Object
135 136 137 138 |
# File 'lib/ethereum/formatter.rb', line 135 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/ethereum/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)}.join("") end |
#from_wei(amount, unit = "ether") ⇒ Object
79 80 81 82 |
# File 'lib/ethereum/formatter.rb', line 79 def from_wei(amount, unit = "ether") return nil if amount.nil? (BigDecimal.new(amount, 16) / BigDecimal.new(UNITS[unit.to_sym], 16)).to_s rescue nil end |
#get_base_type(typename) ⇒ Object
126 127 128 |
# File 'lib/ethereum/formatter.rb', line 126 def get_base_type(typename) typename.gsub(/\d+/,'') end |
#int_to_payload(int) ⇒ Object
114 115 116 |
# File 'lib/ethereum/formatter.rb', line 114 def int_to_payload(int) self.to_twos_complement(uint).rjust(64, '0') end |
#output_to_address(bytes) ⇒ Object
140 141 142 |
# File 'lib/ethereum/formatter.rb', line 140 def output_to_address(bytes) self.to_address(bytes) end |
#output_to_bool(bytes) ⇒ Object
160 161 162 |
# File 'lib/ethereum/formatter.rb', line 160 def output_to_bool(bytes) self.to_bool(bytes.gsub(/^0x/,'')) end |
#output_to_bytes(bytes) ⇒ Object
144 145 146 |
# File 'lib/ethereum/formatter.rb', line 144 def output_to_bytes(bytes) self.to_utf8(bytes) end |
#output_to_int(bytes) ⇒ Object
156 157 158 |
# File 'lib/ethereum/formatter.rb', line 156 def output_to_int(bytes) self.to_int(bytes) end |
#output_to_string(bytes) ⇒ Object
148 149 150 |
# File 'lib/ethereum/formatter.rb', line 148 def output_to_string(bytes) self.to_utf8(bytes) end |
#output_to_uint(bytes) ⇒ Object
152 153 154 |
# File 'lib/ethereum/formatter.rb', line 152 def output_to_uint(bytes) self.to_int(bytes) end |
#string_to_payload(bytes) ⇒ Object
122 123 124 |
# File 'lib/ethereum/formatter.rb', line 122 def string_to_payload(bytes) self.bytes_to_payload(bytes) end |
#to_address(hexstring) ⇒ Object
69 70 71 72 |
# File 'lib/ethereum/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/ethereum/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/ethereum/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/ethereum/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
164 165 166 167 |
# File 'lib/ethereum/formatter.rb', line 164 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/ethereum/formatter.rb', line 89 def to_param(string) string.ljust(64, '0') end |
#to_payload(args) ⇒ Object
130 131 132 133 |
# File 'lib/ethereum/formatter.rb', line 130 def to_payload(args) converter = "#{self.get_base_type(args[0])}_to_payload".to_sym self.send(converter, args[1]) end |
#to_twos_complement(number) ⇒ Object
97 98 99 |
# File 'lib/ethereum/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/ethereum/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/ethereum/formatter.rb', line 74 def to_wei(amount, unit = "ether") return nil if amount.nil? BigDecimal.new(UNITS[unit.to_sym] * amount, 16).to_s.to_i rescue nil end |
#uint_to_payload(uint) ⇒ Object
110 111 112 |
# File 'lib/ethereum/formatter.rb', line 110 def uint_to_payload(uint) self.to_twos_complement(uint).rjust(64, '0') end |
#valid_address?(address_string) ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/ethereum/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 |