Class: Btc

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

Constant Summary collapse

SATOSHI =
BigDecimal.new('0.00000001')
MBTC =
BigDecimal.new('0.0001')
BTC =
BigDecimal.new('1.0')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amt) ⇒ Btc

Returns a new instance of Btc.

Raises:

  • (ArgumentError)

8
9
10
11
# File 'lib/btc.rb', line 8

def initialize(amt)
  raise ArgumentError, 'BTC amount must not be less than 0.00000001' unless Btc.valid_btc(amt)
  @amt = BigDecimal.new(amt.to_f.to_s)
end

Class Method Details

.from_mbtc(amt) ⇒ Object

Raises:

  • (ArgumentError)

13
14
15
16
# File 'lib/btc.rb', line 13

def self.from_mbtc(amt)
  raise ArgumentError, 'mBTC amount must not be less than 0.00001' unless Btc.valid_mbtc(amt)
  Btc.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
end

.from_satoshis(amt) ⇒ Object

Raises:

  • (ArgumentError)

18
19
20
21
# File 'lib/btc.rb', line 18

def self.from_satoshis(amt)
  raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless Btc.valid_satoshis(amt)
  Btc.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
end

.valid_btc(amt) ⇒ Object


31
32
33
# File 'lib/btc.rb', line 31

def self.valid_btc(amt)
  amt.to_f >= SATOSHI.to_f || amt.to_f == 0
end

.valid_mbtc(amt) ⇒ Object


27
28
29
# File 'lib/btc.rb', line 27

def self.valid_mbtc(amt)
  amt.to_f >= MBTC.to_f || amt.to_f == 0
end

.valid_satoshis(amt) ⇒ Object


23
24
25
# File 'lib/btc.rb', line 23

def self.valid_satoshis(amt)
  amt.to_i >= 0
end

Instance Method Details

#*(other) ⇒ Object


59
60
61
# File 'lib/btc.rb', line 59

def *(other)
  Btc.new(@amt * other.btc)
end

#**(exp) ⇒ Object


63
64
65
# File 'lib/btc.rb', line 63

def **(exp)
  Btc.new(@amt ** exp)
end

#+(other) ⇒ Object


47
48
49
# File 'lib/btc.rb', line 47

def +(other)
  Btc.new(@amt + other.btc)
end

#-(other) ⇒ Object


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

def -(other)
  Btc.new(@amt - other.btc)
end

#/(other) ⇒ Object


51
52
53
# File 'lib/btc.rb', line 51

def /(other)
  Btc.new(@amt / other.btc)
end

#==(other) ⇒ Object


67
68
69
# File 'lib/btc.rb', line 67

def ==(other)
  @amt == other.btc
end

#btcObject


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

def btc
  @amt
end

#inspectObject


71
72
73
# File 'lib/btc.rb', line 71

def inspect
  "#<Btc BTC:#{btc} mBTC:#{mbtc} Satoshis:#{satoshis}>"
end

#mbtcObject


39
40
41
# File 'lib/btc.rb', line 39

def mbtc
  @amt / MBTC
end

#satoshisObject


43
44
45
# File 'lib/btc.rb', line 43

def satoshis
  @amt / SATOSHI
end