Class: Sibit::Coins

Inherits:
Object
  • Object
show all
Defined in:
lib/sibit/coins.rb

Overview

An amount of bitcoins, convertible to an exact number of satoshi.

It parses the amount through BigDecimal instead of a binary Float, so decimal values like 0.29 map to exactly 29,000,000 satoshi. An amount with sub-satoshi precision is rejected instead of being truncated.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(btc) ⇒ Coins

Returns a new instance of Coins.



21
22
23
# File 'lib/sibit/coins.rb', line 21

def initialize(btc)
  @btc = btc
end

Instance Method Details

#satoshiObject



25
26
27
28
29
30
31
32
33
# File 'lib/sibit/coins.rb', line 25

def satoshi
  sat = BigDecimal(@btc.to_s) * 100_000_000
  unless sat.frac.zero?
    raise(Sibit::Error, "The amount #{@btc.inspect} is finer than one satoshi")
  end
  Integer(sat)
rescue ArgumentError, TypeError
  raise(Sibit::Error, "The amount #{@btc.inspect} is not a valid number of bitcoins")
end