Module: Paleth::Helpers

Included in:
Paleth, Eth
Defined in:
lib/paleth/helpers.rb

Overview

Various helpers to handle web3.js data transformations

Instance Method Summary collapse

Instance Method Details

#address?(hex) ⇒ Boolean

returns true if the value is a valid ethereum address, false otherwise

Returns:

  • (Boolean)

100
101
102
# File 'lib/paleth/helpers.rb', line 100

def address?(hex)
  Paleth._dummy_web3.JS.isAddress(hex)
end

#from_ascii(str, bytes = nil) ⇒ Object

Converts an ascii string to its hexadecimal representation with optional padding

  • str: The string to be converted

  • bytes: The number of bytes of the returned string should have. Padding will be added at the end if needed


45
46
47
# File 'lib/paleth/helpers.rb', line 45

def from_ascii(str, bytes = nil)
  Paleth._dummy_web3.JS.fromAscii(str, bytes)
end

#from_bigdecimal(number) ⇒ Object

Converts a ruby BigDecimal to a JS BigNumber, or pass the value untouched


91
92
93
94
95
96
97
# File 'lib/paleth/helpers.rb', line 91

def from_bigdecimal(number)
  if number.is_a? BigDecimal
    number.bignumber
  else
    number
  end
end

#from_decimal(number) ⇒ Object

Converts a decimal to its hexadecimal string representation


55
56
57
# File 'lib/paleth/helpers.rb', line 55

def from_decimal(number)
  Paleth._dummy_web3.JS.fromDecimal(from_bigdecimal(number))
end

#from_wei(number, unit) ⇒ Object

Converts a value in Wei to any supported unit, see Paleth::UNITS


60
61
62
63
64
65
# File 'lib/paleth/helpers.rb', line 60

def from_wei(number, unit)
  raise "Unsupported unit #{unit}" unless Paleth::UNITS.include?(unit)

  to_bigdecimal(Paleth._dummy_web3.JS.fromWei(from_bigdecimal(number),
                                              unit))
end

#sha3(string, opts = {}) ⇒ Object

Returns a provided string hashed using the Keccak-256 SHA3 algorithm

You can optionnaly provide the the `encoding: 'hex'` option if the string is encoded in hexadecimal


24
25
26
# File 'lib/paleth/helpers.rb', line 24

def sha3(string, opts = {})
  Paleth._dummy_web3.JS.sha3(string, opts)
end

#to_ascii(hex_string) ⇒ Object

Converts back an hexadecimal representation of a string into the ascii representation of the string


35
36
37
# File 'lib/paleth/helpers.rb', line 35

def to_ascii(hex_string)
  Paleth._dummy_web3.JS.toAscii(hex_string)
end

#to_bigdecimal(number) ⇒ Object

Converts a JS Number, BigNumber, or Hex string into a ruby BigDecimal If it's neither a String, a Number or a BigNumber, just returns the value untouched


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/paleth/helpers.rb', line 78

def to_bigdecimal(number)
  type = JS.typeof(number)
  is_big_number = number.JS[:isBigNumber]
  if type == 'number' || is_big_number
    BigDecimal.new number
  elsif type == 'string'
    BigDecimal.new Paleth._dummy_web3.JS.toBigNumber(number)
  else
    number
  end
end

#to_decimal(hex) ⇒ Object

Converts an hexadecimal string to its decimal representation


50
51
52
# File 'lib/paleth/helpers.rb', line 50

def to_decimal(hex)
  Paleth._dummy_web3.JS.toDecimal(hex)
end

#to_hex(value) ⇒ Object

Converts any value into a string hexadecimal representation


29
30
31
# File 'lib/paleth/helpers.rb', line 29

def to_hex(value)
  Paleth._dummy_web3.JS.toHex(from_bigdecimal(value))
end

#to_wei(number, unit) ⇒ Object

Converts a value from any supported unit (see Paleth::UNITS) in Wei


68
69
70
71
72
73
# File 'lib/paleth/helpers.rb', line 68

def to_wei(number, unit)
  raise "Unsupported unit #{unit}" unless Paleth::UNITS.include?(unit)

  to_bigdecimal(Paleth._dummy_web3.JS.toWei(from_bigdecimal(number),
                                            unit))
end