module EthereumUnitConverter

Constants

UNITS
VERSION

Public Class Methods

convert(amount, unit = 'wei', to_unit = 'ether') click to toggle source
# File lib/ethereum_unit_converter.rb, line 35
def convert(amount, unit = 'wei', to_unit = 'ether')
  return nil if amount.nil?

  return from_wei(amount, to_unit) if unit == 'wei'

  from_wei(to_wei(amount, unit), to_unit)
end
from_wei(amount, unit = 'ether') click to toggle source
# File lib/ethereum_unit_converter.rb, line 53
def from_wei(amount, unit = 'ether')
  return nil if amount.nil?

  begin
    (BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16)).to_s
  rescue StandardError => e
    raise e.class
  end
end
to_wei(amount, unit = 'ether') click to toggle source
# File lib/ethereum_unit_converter.rb, line 43
def to_wei(amount, unit = 'ether')
  return nil if amount.nil?

  begin
    BigDecimal(UNITS[unit.to_sym] * amount, 16).to_s.to_i
  rescue StandardError => e
    raise e.class
  end
end