Module: EthereumUnitConverter
- Defined in:
- lib/ethereum_unit_converter.rb,
lib/ethereum_unit_converter/version.rb
Constant Summary collapse
- UNITS =
{ wei: 1, kwei: 1000, ada: 1000, femtoether: 1000, mwei: 1_000_000, babbage: 1_000_000, picoether: 1_000_000, gwei: 1_000_000_000, shannon: 1_000_000_000, nanoether: 1_000_000_000, nano: 1_000_000_000, szabo: 1_000_000_000_000, microether: 1_000_000_000_000, micro: 1_000_000_000_000, finney: 1_000_000_000_000_000, milliether: 1_000_000_000_000_000, milli: 1_000_000_000_000_000, ether: 1_000_000_000_000_000_000, eth: 1_000_000_000_000_000_000, kether: 1_000_000_000_000_000_000_000, grand: 1_000_000_000_000_000_000_000, einstein: 1_000_000_000_000_000_000_000, mether: 1_000_000_000_000_000_000_000_000, gether: 1_000_000_000_000_000_000_000_000_000, tether: 1_000_000_000_000_000_000_000_000_000_000 }.freeze
- VERSION =
"0.1.1"
Class Method Summary collapse
- .convert(amount, unit = 'wei', to_unit = 'ether') ⇒ Object
- .from_wei(amount, unit = 'ether') ⇒ Object
- .to_wei(amount, unit = 'ether') ⇒ Object
Class Method Details
.convert(amount, unit = 'wei', to_unit = 'ether') ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/ethereum_unit_converter.rb', line 36 def convert(amount, unit = 'wei', to_unit = 'ether') return nil if amount.nil? return from_wei(amount, to_unit).to_s('F').delete_suffix('.0') if unit == 'wei' from_wei(to_wei(amount, unit), to_unit).to_s('F').delete_suffix('.0') end |
.from_wei(amount, unit = 'ether') ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/ethereum_unit_converter.rb', line 54 def from_wei(amount, unit = 'ether') return nil if amount.nil? begin (BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16)) rescue StandardError => e raise e.class end end |
.to_wei(amount, unit = 'ether') ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/ethereum_unit_converter.rb', line 44 def to_wei(amount, unit = 'ether') return nil if amount.nil? begin BigDecimal(UNITS[unit.to_sym] * amount, 16) rescue StandardError => e raise e.class end end |