bitcoin-money 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bitcoin-money.gemspec +3 -3
- data/lib/btc.rb +28 -28
- data/spec/btc_spec.rb +27 -27
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0d20b83dd14d7d669af9743fd274b681d78926b
|
4
|
+
data.tar.gz: aa2ef366382416fd818386bc799f26c51cc15629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6289ad3afa3f266bc4e5e971acedae165b752dbc23d2132a01f60b9ff1f9888e40af23a6b4fa550cc975d7deec9056a926c67b2d9795dce3ff7b5e61c9e2fdae
|
7
|
+
data.tar.gz: f03cf0cc62259bcac9c14ea09a9377510585069b6505c34a2b93f4ea9570f30acbda0e83bc71391e99b71aff4afa81bdec2ea0bee6cadd9f546397c11d30e420
|
data/bitcoin-money.gemspec
CHANGED
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "bitcoin-money"
|
7
|
-
spec.version = "0.
|
7
|
+
spec.version = "0.6.0"
|
8
8
|
spec.authors = ["James Larisch"]
|
9
9
|
spec.email = ["root@jameslarisch.com"]
|
10
|
-
spec.summary = "
|
11
|
-
spec.description = "
|
10
|
+
spec.summary = "Intelligently manipulate Bitcoin with a Ruby object."
|
11
|
+
spec.description = "Supports conversions and arithmetic."
|
12
12
|
spec.homepage = "https://github.com/zenchilabs/bitcoin-money"
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
data/lib/btc.rb
CHANGED
@@ -1,65 +1,65 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
|
3
|
-
class
|
3
|
+
class BTC
|
4
4
|
BTC = BigDecimal.new('1.0')
|
5
5
|
MBTC = BigDecimal.new('0.001')
|
6
|
-
|
6
|
+
BIT = BigDecimal.new('0.000001')
|
7
7
|
SATOSHI = BigDecimal.new('0.00000001')
|
8
8
|
|
9
9
|
MIN_BTC = SATOSHI.to_f
|
10
10
|
MIN_MBTC = 0.00001
|
11
|
-
|
11
|
+
MIN_BIT = 0.01
|
12
12
|
MIN_SATOSHI = 1
|
13
13
|
|
14
14
|
def initialize(amt)
|
15
|
-
raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless
|
15
|
+
raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless self.class.valid_btc?(amt)
|
16
16
|
@amt = BigDecimal.new(amt.to_f.to_s)
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.from_mbtc(amt)
|
20
|
-
raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless
|
21
|
-
|
20
|
+
raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless self.valid_mbtc?(amt)
|
21
|
+
self.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
|
22
22
|
end
|
23
23
|
|
24
|
-
def self.
|
25
|
-
raise ArgumentError, "
|
26
|
-
|
24
|
+
def self.from_bits(amt)
|
25
|
+
raise ArgumentError, "bits amount must not be less than #{MIN_BIT}" unless self.valid_bits?(amt)
|
26
|
+
self.new(BigDecimal.new(amt.to_f.to_s) * BIT)
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.from_satoshis(amt)
|
30
|
-
raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless
|
31
|
-
|
30
|
+
raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless self.valid_satoshis?(amt)
|
31
|
+
self.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
|
32
32
|
end
|
33
33
|
|
34
|
-
def self.valid_btc(amt)
|
34
|
+
def self.valid_btc?(amt)
|
35
35
|
amt.to_f >= SATOSHI.to_f || amt.to_f == 0.0
|
36
36
|
end
|
37
37
|
|
38
|
-
def self.valid_mbtc(amt)
|
38
|
+
def self.valid_mbtc?(amt)
|
39
39
|
amt.to_f >= MIN_MBTC || amt.to_f == 0.0
|
40
40
|
end
|
41
41
|
|
42
|
-
def self.
|
43
|
-
amt.to_f >=
|
42
|
+
def self.valid_bits?(amt)
|
43
|
+
amt.to_f >= MIN_BIT || amt.to_f == 0.0
|
44
44
|
end
|
45
45
|
|
46
|
-
def self.valid_satoshis(amt)
|
46
|
+
def self.valid_satoshis?(amt)
|
47
47
|
amt.to_i >= 0
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
50
|
+
def to_btc
|
51
51
|
@amt
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
54
|
+
def to_mbtc
|
55
55
|
@amt / MBTC
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
59
|
-
@amt /
|
58
|
+
def to_bits
|
59
|
+
@amt / BIT
|
60
60
|
end
|
61
61
|
|
62
|
-
def
|
62
|
+
def to_satoshis
|
63
63
|
@amt / SATOSHI
|
64
64
|
end
|
65
65
|
|
@@ -72,30 +72,30 @@ class Btc
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def +(other)
|
75
|
-
|
75
|
+
self.class.new(@amt + other.to_btc)
|
76
76
|
end
|
77
77
|
|
78
78
|
def /(other)
|
79
|
-
|
79
|
+
self.class.new(@amt / other.to_btc)
|
80
80
|
end
|
81
81
|
|
82
82
|
def -(other)
|
83
|
-
|
83
|
+
self.class.new(@amt - other.to_btc)
|
84
84
|
end
|
85
85
|
|
86
86
|
def *(other)
|
87
|
-
|
87
|
+
self.class.new(@amt * other.to_btc)
|
88
88
|
end
|
89
89
|
|
90
90
|
def **(exp)
|
91
|
-
|
91
|
+
self.class.new(@amt ** exp)
|
92
92
|
end
|
93
93
|
|
94
94
|
def ==(other)
|
95
|
-
@amt == other.
|
95
|
+
@amt == other.to_btc
|
96
96
|
end
|
97
97
|
|
98
98
|
def inspect
|
99
|
-
"#<
|
99
|
+
"#<BTC:#{to_btc.to_f} mBTC:#{to_mbtc.to_f} bits:#{to_bits.to_f} Satoshis:#{to_satoshis.to_f}>"
|
100
100
|
end
|
101
101
|
end
|
data/spec/btc_spec.rb
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
require 'btc'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe BTC do
|
4
4
|
|
5
5
|
describe '#new' do
|
6
6
|
it 'should initialize okay' do
|
7
|
-
b =
|
8
|
-
expect(b.
|
7
|
+
b = BTC.new(12345)
|
8
|
+
expect(b.to_btc).to eq(BigDecimal.new('12345.0'))
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'should throw error if invalid btc amount' do
|
12
|
-
expect{
|
12
|
+
expect{ BTC.new(0.00000000000001) }.to raise_error(ArgumentError)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe '.from_mbtc' do
|
17
17
|
it 'should throw error if invalid mbtc amount' do
|
18
|
-
expect{
|
18
|
+
expect{ BTC.from_mbtc(BTC::SATOSHI) }.to raise_error(ArgumentError)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
describe '.
|
23
|
-
it 'should throw error if invalid
|
24
|
-
expect{
|
22
|
+
describe '.from_bits' do
|
23
|
+
it 'should throw error if invalid bit amount' do
|
24
|
+
expect{ BTC.from_bits(BTC::MIN_BIT - 10) }.to raise_error(ArgumentError)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe '.from_satoshis' do
|
29
29
|
it 'should throw error if invalid satoshi amount' do
|
30
|
-
expect{
|
30
|
+
expect{ BTC.from_satoshis(-1) }.to raise_error(ArgumentError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should convert btc to mbtc and back' do
|
35
|
-
expect(
|
36
|
-
expect(
|
35
|
+
expect(BTC.new(BTC::MBTC.to_f).to_mbtc).to eq(BigDecimal.new('1'))
|
36
|
+
expect(BTC.from_mbtc(1).to_btc).to eq(BigDecimal.new(BTC::MBTC))
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should convert btc to satoshis and back' do
|
40
|
-
expect(
|
41
|
-
expect(
|
40
|
+
expect(BTC.new(BTC::SATOSHI).to_satoshis).to eq(BigDecimal.new('1'))
|
41
|
+
expect(BTC.from_satoshis(1).to_btc).to eq(BigDecimal.new(BTC::SATOSHI))
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'should convert mbtc to satoshis and back' do
|
45
|
-
expect(
|
46
|
-
expect(
|
45
|
+
expect(BTC.from_mbtc(1).to_satoshis).to eq(BigDecimal.new('100000'))
|
46
|
+
expect(BTC.from_satoshis(10000).to_mbtc).to eq(BigDecimal.new('0.1'))
|
47
47
|
end
|
48
48
|
|
49
|
-
it 'should convert btc to
|
50
|
-
expect(
|
51
|
-
expect(
|
49
|
+
it 'should convert btc to bit and back' do
|
50
|
+
expect(BTC.new(1).to_bits).to eq(BigDecimal.new('1000000'))
|
51
|
+
expect(BTC.from_bits(12).to_btc).to eq(BigDecimal('.000012'))
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should use equality correctly' do
|
55
|
-
expect(
|
56
|
-
expect(
|
57
|
-
expect(
|
55
|
+
expect(BTC.new(0.123)).to eq(BTC.from_mbtc(123))
|
56
|
+
expect(BTC.new(0.123)).to eq(BTC.from_satoshis(12300000))
|
57
|
+
expect(BTC.from_mbtc(12)).to eq(BTC.from_satoshis(1200000))
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'should use multiplication correctly' do
|
61
|
-
expect(
|
61
|
+
expect(BTC.new(0.1234) * BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') * BigDecimal.new('0.1234')))
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'should use division correctly' do
|
65
|
-
expect(
|
65
|
+
expect(BTC.new(0.1234) / BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') / BigDecimal.new('0.1234')))
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'should use addition correctly' do
|
69
|
-
expect(
|
69
|
+
expect(BTC.new(0.1234) + BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') + BigDecimal.new('0.1234')))
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'should use subtraction correctly' do
|
73
|
-
expect(
|
73
|
+
expect(BTC.new(0.1234) - BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') - BigDecimal.new('0.1234')))
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'should use to_f correctly' do
|
77
|
-
expect(
|
77
|
+
expect(BTC.new(1.5).to_f).to eq(1.5)
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'should use to_i correctly' do
|
81
|
-
expect(
|
81
|
+
expect(BTC.new(1.4).to_i).to eq(1)
|
82
82
|
end
|
83
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitcoin-money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Larisch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.0.0
|
41
|
-
description:
|
41
|
+
description: Supports conversions and arithmetic.
|
42
42
|
email:
|
43
43
|
- root@jameslarisch.com
|
44
44
|
executables: []
|
@@ -76,6 +76,7 @@ rubyforge_project:
|
|
76
76
|
rubygems_version: 2.2.2
|
77
77
|
signing_key:
|
78
78
|
specification_version: 4
|
79
|
-
summary:
|
79
|
+
summary: Intelligently manipulate Bitcoin with a Ruby object.
|
80
80
|
test_files:
|
81
81
|
- spec/btc_spec.rb
|
82
|
+
has_rdoc:
|