satoshi-unit 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +65 -0
- data/LICENSE.txt +20 -0
- data/README.md +63 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/lib/satoshi.rb +96 -0
- data/spec/satoshi_spec.rb +32 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4078d5c7fe2449c56978ce61a22df23d34bc906
|
4
|
+
data.tar.gz: b1ca344599ad41967fc1347d78f14f97bb522bfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad99e9d1dafcf2103a294cf87ad09e37da2f87f98575dc7f8d9875fdcb5093a4b7c8b2034a65c482f4fdfae5eb6517a9bdc0f85a0c8093accaa5389f2509ce2b
|
7
|
+
data.tar.gz: cd2366b082ac389c1e62ae377ca035c2a07ed4bf91cd0a12e7310da5e618bb780fdf0f584c6d6d6837f16d058f4391f083844e26aad91f718170e78e2ea87a69
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.3.6)
|
5
|
+
builder (3.2.2)
|
6
|
+
descendants_tracker (0.0.4)
|
7
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
8
|
+
diff-lcs (1.2.5)
|
9
|
+
faraday (0.9.0)
|
10
|
+
multipart-post (>= 1.2, < 3)
|
11
|
+
git (1.2.6)
|
12
|
+
github_api (0.11.3)
|
13
|
+
addressable (~> 2.3)
|
14
|
+
descendants_tracker (~> 0.0.1)
|
15
|
+
faraday (~> 0.8, < 0.10)
|
16
|
+
hashie (>= 1.2)
|
17
|
+
multi_json (>= 1.7.5, < 2.0)
|
18
|
+
nokogiri (~> 1.6.0)
|
19
|
+
oauth2
|
20
|
+
hashie (2.1.1)
|
21
|
+
highline (1.6.21)
|
22
|
+
jeweler (2.0.1)
|
23
|
+
builder
|
24
|
+
bundler (>= 1.0)
|
25
|
+
git (>= 1.2.5)
|
26
|
+
github_api
|
27
|
+
highline (>= 1.6.15)
|
28
|
+
nokogiri (>= 1.5.10)
|
29
|
+
rake
|
30
|
+
rdoc
|
31
|
+
json (1.8.1)
|
32
|
+
jwt (1.0.0)
|
33
|
+
mini_portile (0.6.0)
|
34
|
+
multi_json (1.10.1)
|
35
|
+
multi_xml (0.5.5)
|
36
|
+
multipart-post (2.0.0)
|
37
|
+
nokogiri (1.6.2.1)
|
38
|
+
mini_portile (= 0.6.0)
|
39
|
+
oauth2 (0.9.4)
|
40
|
+
faraday (>= 0.8, < 0.10)
|
41
|
+
jwt (~> 1.0)
|
42
|
+
multi_json (~> 1.3)
|
43
|
+
multi_xml (~> 0.5)
|
44
|
+
rack (~> 1.2)
|
45
|
+
rack (1.5.2)
|
46
|
+
rake (10.3.2)
|
47
|
+
rdoc (4.1.1)
|
48
|
+
json (~> 1.4)
|
49
|
+
rspec (2.14.1)
|
50
|
+
rspec-core (~> 2.14.0)
|
51
|
+
rspec-expectations (~> 2.14.0)
|
52
|
+
rspec-mocks (~> 2.14.0)
|
53
|
+
rspec-core (2.14.8)
|
54
|
+
rspec-expectations (2.14.5)
|
55
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
56
|
+
rspec-mocks (2.14.6)
|
57
|
+
thread_safe (0.3.3)
|
58
|
+
|
59
|
+
PLATFORMS
|
60
|
+
ruby
|
61
|
+
|
62
|
+
DEPENDENCIES
|
63
|
+
bundler (~> 1.0)
|
64
|
+
jeweler (~> 2.0.1)
|
65
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Roman Snitko
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
Satoshi Unit
|
2
|
+
================
|
3
|
+
This tiny gem allows you to make easy conversions from one Bitcoin denomination into another.
|
4
|
+
It provides a class, which objects would essentially represent an amount of bitcoins measured
|
5
|
+
in the smallest possible denomination, which is Satoshi. You can then call methods on these objects
|
6
|
+
to convert it various other denominations.
|
7
|
+
|
8
|
+
Here's how it might look:
|
9
|
+
|
10
|
+
s = Satoshi.new(1) # By default, it accepts amounts in BTC denomination
|
11
|
+
s.to_i # => 100000000 (in Satoshis)
|
12
|
+
s.to_mbtc # => 1000.0
|
13
|
+
s.to_btc # => 1.0
|
14
|
+
|
15
|
+
When creating a Satoshi object, you can also specify which unit is being used to pass the amount,
|
16
|
+
for example:
|
17
|
+
|
18
|
+
s = Satoshi.new(1, from_unit: :mbtc)
|
19
|
+
|
20
|
+
s.to_i # => 100000
|
21
|
+
s.to_mbtc # => 1.0
|
22
|
+
s.to_btc # => 0.001
|
23
|
+
|
24
|
+
There's also a special method which is called `#to_unit`, it always converts to the denomination
|
25
|
+
specified in the `:to_unit` option in the constructor. It becomes really handy when you want to
|
26
|
+
specify your preferred denomination used througout the program in just one place
|
27
|
+
(to be able to change it later):
|
28
|
+
|
29
|
+
DENOMINATION = :mbtc
|
30
|
+
s = Satoshi.new(1, from_unit: DENOMINATION, from_unit: DENOMINATION)
|
31
|
+
s.to_unit # => 1.0 (in mBTC)
|
32
|
+
|
33
|
+
This can be shortened to just the `:unit` option:
|
34
|
+
|
35
|
+
s = Satoshi.new(1, unit: DENOMINATION)
|
36
|
+
|
37
|
+
But, of course, if you want your "from" denomination and "to" denomination to be different, then
|
38
|
+
you'd have to pass them both manually:
|
39
|
+
|
40
|
+
s = Satoshi.new(1, from_unit: :mbtc, to_unit: :btc)
|
41
|
+
s.to_unit # => 0.001
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
###Comparing satoshis
|
46
|
+
|
47
|
+
Satoshi objects come with methods and coercions for comparing itself with both other Satoshi objects and also numeric values:
|
48
|
+
|
49
|
+
s1 = Satoshi.new(1)
|
50
|
+
s2 = Satoshi.new(2)
|
51
|
+
|
52
|
+
s1 > s2 # => false
|
53
|
+
s1 < s2 # => true
|
54
|
+
s1 == s2 # => false
|
55
|
+
|
56
|
+
s1 > 1 # => true
|
57
|
+
1 > s2 # => false
|
58
|
+
|
59
|
+
|
60
|
+
### Unit tests
|
61
|
+
|
62
|
+
|
63
|
+
Run `rspec spec`. Pull requests with more unit tests are welcome.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "satoshi-unit"
|
18
|
+
gem.homepage = "http://github.com/snitko/satoshi-unit"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Converts various bitcoin denominations in Satoshis and back}
|
21
|
+
gem.description = %Q{Converts various bitcoin denominations in Satoshis and back}
|
22
|
+
gem.email = "roman.snitko@gmail.com"
|
23
|
+
gem.authors = ["Roman Snitko"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/satoshi.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
class Satoshi
|
2
|
+
|
3
|
+
# Says how many digits after the decimal point
|
4
|
+
# a denomination has.
|
5
|
+
UNIT_DENOMINATIONS = {
|
6
|
+
btc: 8,
|
7
|
+
mbtc: 5,
|
8
|
+
satoshi: 0
|
9
|
+
}
|
10
|
+
|
11
|
+
attr_reader :value, :from_unit, :to_unit
|
12
|
+
|
13
|
+
def initialize(n=nil, from_unit: 'btc', to_unit: 'btc', unit: nil)
|
14
|
+
if unit
|
15
|
+
@from_unit = @to_unit = unit.downcase.to_sym
|
16
|
+
else
|
17
|
+
@from_unit = from_unit.downcase.to_sym
|
18
|
+
@to_unit = to_unit.downcase.to_sym
|
19
|
+
end
|
20
|
+
@value = convert_to_satoshi(n) if n
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_btc
|
24
|
+
to_denomination(UNIT_DENOMINATIONS[:btc])
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_mbtc
|
28
|
+
to_denomination(UNIT_DENOMINATIONS[:mbtc])
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_unit
|
32
|
+
to_denomination(UNIT_DENOMINATIONS[@to_unit])
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_i
|
36
|
+
@value
|
37
|
+
end
|
38
|
+
alias :satoshi_value :to_i
|
39
|
+
|
40
|
+
def value=(n)
|
41
|
+
@value = convert_to_satoshi(n)
|
42
|
+
end
|
43
|
+
|
44
|
+
def satoshi_value=(v)
|
45
|
+
@value = v
|
46
|
+
end
|
47
|
+
|
48
|
+
def >(i)
|
49
|
+
self.to_i > i
|
50
|
+
end
|
51
|
+
|
52
|
+
def <(i)
|
53
|
+
self.to_i < i
|
54
|
+
end
|
55
|
+
|
56
|
+
def ==(i)
|
57
|
+
self.to_i == i
|
58
|
+
end
|
59
|
+
|
60
|
+
def +(i)
|
61
|
+
self.to_i + i
|
62
|
+
end
|
63
|
+
|
64
|
+
def -(i)
|
65
|
+
self.to_i - i
|
66
|
+
end
|
67
|
+
|
68
|
+
def coerce(other)
|
69
|
+
if other.kind_of?(Integer)
|
70
|
+
[other, self.to_i]
|
71
|
+
else
|
72
|
+
raise TypeError, message: "Satoshi cannot be coerced into anything but Integer"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def to_denomination(digits_after_delimiter)
|
79
|
+
return @value if digits_after_delimiter <= 0
|
80
|
+
leading_zeros = "0"*(18-@value.to_s.length)
|
81
|
+
result = leading_zeros + @value.to_s
|
82
|
+
result.reverse!
|
83
|
+
result = result.slice(0..digits_after_delimiter-1) + '.' + result.slice(digits_after_delimiter..17)
|
84
|
+
result.reverse!
|
85
|
+
result.sub(/\A0*/, '').sub(/0*\Z/, '').to_f # remove zeros on both sides
|
86
|
+
end
|
87
|
+
|
88
|
+
def convert_to_satoshi(n)
|
89
|
+
n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
|
90
|
+
n = n.split('.')
|
91
|
+
n[1] ||= '' # in the case where there's no decimal part
|
92
|
+
n[1] += "0"*(UNIT_DENOMINATIONS[@from_unit]-n[1].length) if n[1]
|
93
|
+
n.join.to_i
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../lib/satoshi'
|
2
|
+
|
3
|
+
describe Satoshi do
|
4
|
+
|
5
|
+
it "creates a Bignum representing value in satoshi units" do
|
6
|
+
expect(Satoshi.new(1.00).to_i).to eq(100000000)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "takes care of the sign before the value" do
|
10
|
+
expect(Satoshi.new(-1.00).to_i).to eq(-100000000)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "converts satoshi unit back to some more common denomination" do
|
14
|
+
expect(Satoshi.new(1.00).to_btc).to eq(1)
|
15
|
+
expect(Satoshi.new(1.08763).to_btc).to eq(1.08763)
|
16
|
+
expect(Satoshi.new(1.08763).to_mbtc).to eq(1087.63)
|
17
|
+
expect(Satoshi.new(-1.08763).to_mbtc).to eq(-1087.63)
|
18
|
+
expect(Satoshi.new(0.00000001).to_i).to eq(1)
|
19
|
+
expect(Satoshi.new(0.00000001).to_mbtc).to eq(0.00001)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "converts from various source denominations" do
|
23
|
+
expect(Satoshi.new(1, unit: 'mbtc').to_btc).to eq(0.001)
|
24
|
+
expect(Satoshi.new(1, unit: 'mbtc').to_unit).to eq(1)
|
25
|
+
expect(Satoshi.new(10000000, unit: 'mbtc').to_unit).to eq(10000000)
|
26
|
+
satoshi = Satoshi.new(10000000, unit: 'mbtc')
|
27
|
+
satoshi.satoshi_value = 1
|
28
|
+
expect(satoshi.to_unit).to eq(0.00001)
|
29
|
+
expect(Satoshi.new(100, unit: 'mbtc').to_i).to eq(10000000)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: satoshi-unit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roman Snitko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Converts various bitcoin denominations in Satoshis and back
|
56
|
+
email: roman.snitko@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
files:
|
63
|
+
- .document
|
64
|
+
- .rspec
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- VERSION
|
71
|
+
- lib/satoshi.rb
|
72
|
+
- spec/satoshi_spec.rb
|
73
|
+
homepage: http://github.com/snitko/satoshi-unit
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Converts various bitcoin denominations in Satoshis and back
|
97
|
+
test_files: []
|