Last active
January 25, 2021 18:00
-
-
Save descovi/5056ab3f433db61c7c19d040c4c03921 to your computer and use it in GitHub Desktop.
Ruby Wei to eth converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example: WeiConverter.new(wei_number).call | |
class WeiConverter | |
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 | |
} | |
def initialize(amount) | |
@amount = amount | |
end | |
def call(unit: "ether") | |
return nil if @amount.nil? | |
unit = UNITS[unit.to_sym] | |
(BigDecimal.new(@amount, 16) / BigDecimal.new(unit, 16)).to_s rescue nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment