bitcoin_unit_converter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 898fa8312f0e20d0e86d2f3676d390344ba8688c6b4070f9c9b298844ca5fbdc
4
+ data.tar.gz: b3a14f2307241d9084c3f84ee13af5b2683f4250d62349ca42072aaa9f80a964
5
+ SHA512:
6
+ metadata.gz: d7bf6ae8a0b71be3f0a2ae79f0518aa4bcecec6ba477f30efc378c181b136c8ff14f8c4a9c1ac6630bf5c04c63c7c05ab776414548ea1272a536643f20abd87f
7
+ data.tar.gz: 5bcd019b00412484ad5e1b556d47d7d62a6658fd514e5f5706c24f4c92ed0d559d7bfe71a3a91eed7480897bc813a53f428f5c656ea5a58958194e698fa838f1
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bitcoin_unit_converter.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bitcoin_unit_converter (0.1.0)
5
+ bigdecimal (~> 3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ bigdecimal (3.1.2)
12
+ builder (3.2.4)
13
+ minitest (5.15.0)
14
+ minitest-reporters (1.5.0)
15
+ ansi
16
+ builder
17
+ minitest (>= 5.0)
18
+ ruby-progressbar
19
+ rake (13.0.6)
20
+ ruby-progressbar (1.11.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bitcoin_unit_converter!
27
+ minitest (~> 5.0)
28
+ minitest-reporters (~> 1.5)
29
+ rake (~> 13.0)
30
+
31
+ BUNDLED WITH
32
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # BitcoinUnitConverter
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bitcoin_unit_converter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bitcoin_unit_converter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bitcoin_unit_converter
22
+
23
+ ## Usage
24
+ ### Convert from unit to unit
25
+ ```ruby
26
+ # BitcoinUnitConverter.convert(amount, unit = 'sat', to_unit = 'btc')
27
+ BitcoinUnitConverter.convert(100_000_000, 'sat', 'btc') # '1'
28
+ BitcoinUnitConverter.convert(1, 'sat', 'btc') # '0.00000001'
29
+ BitcoinUnitConverter.convert(1, 'btc', 'uBTC') # '1000000'
30
+ ```
31
+ ### Supported Units
32
+ ```ruby
33
+ UNITS = {
34
+ 'sat': 1,
35
+ 'Satoshi': 1,
36
+ 'uBTC': 100, # MicroBit
37
+ 'ubtc': 100, # MicroBit
38
+ 'mBTC': 100_000, # Milibit
39
+ 'mbtc': 100_000, # Milibit
40
+ 'cBTC': 1_000_000, # Bitcent
41
+ 'cbtc': 1_000_000, # Bitcent
42
+ 'dBTC': 10_000_000, # deciBitcoin
43
+ 'dbtc': 10_000_000, # deciBitcoin
44
+ 'BTC': 100_000_000,
45
+ 'btc': 100_000_000,
46
+ 'daBTC': 10_000_000, # decaBitcoin
47
+ 'dabtc': 1_000_000_000 # decaBitcoin
48
+ }
49
+ ```
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bitcoin_unit_converter.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ t.warning = false
11
+ end
12
+
13
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bitcoin_unit_converter"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bitcoin_unit_converter/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bitcoin_unit_converter'
7
+ spec.version = BitcoinUnitConverter::VERSION
8
+ spec.authors = ['Serigne Mouhamadou Bassirou Diaby']
9
+ spec.email = ['bayevels@gmail.com']
10
+
11
+ spec.summary = 'Bitcoin unit converter in Ruby'
12
+ spec.homepage = 'https://github.com/bayevels/bitcoin_unit_converter'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.2')
15
+ spec.metadata = {
16
+ 'documentation_uri' => spec.homepage.to_s,
17
+ 'homepage_uri' => spec.homepage.to_s,
18
+ 'source_code_uri' => spec.homepage.to_s
19
+ }
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'bigdecimal', '~> 3.0'
33
+ spec.add_development_dependency 'minitest', '~> 5.0'
34
+ spec.add_development_dependency 'minitest-reporters', '~> 1.5'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BitcoinUnitConverter
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bitcoin_unit_converter/version'
4
+ require 'bigdecimal'
5
+
6
+ module BitcoinUnitConverter
7
+ # Later add more units https://www.cryps.info/en/BTC_to_Satoshi/1/
8
+ UNITS = {
9
+ 'sat': 1,
10
+ 'Satoshi': 1,
11
+ 'uBTC': 100, # MicroBit
12
+ 'ubtc': 100, # MicroBit
13
+ 'mBTC': 100_000, # Milibit
14
+ 'mbtc': 100_000, # Milibit
15
+ 'cBTC': 1_000_000, # Bitcent
16
+ 'cbtc': 1_000_000, # Bitcent
17
+ 'dBTC': 10_000_000, # deciBitcoin
18
+ 'dbtc': 10_000_000, # deciBitcoin
19
+ 'BTC': 100_000_000,
20
+ 'btc': 100_000_000,
21
+ 'daBTC': 10_000_000, # decaBitcoin
22
+ 'dabtc': 1_000_000_000 # decaBitcoin
23
+ }.freeze
24
+
25
+ class << self
26
+ def convert(amount, unit = 'sat', to_unit = 'btc')
27
+ return nil if amount.nil?
28
+
29
+ return from_sat(amount, to_unit).to_s('F').delete_suffix('.0') if unit == 'sat'
30
+
31
+ from_sat(to_sat(amount, unit), to_unit).to_s('F').delete_suffix('.0')
32
+ end
33
+
34
+ def to_sat(amount, unit = 'btc')
35
+ return nil if amount.nil?
36
+
37
+ begin
38
+ BigDecimal(UNITS[unit.to_sym] * amount, 16)
39
+ rescue StandardError => e
40
+ raise e.class
41
+ end
42
+ end
43
+
44
+ def from_sat(amount, unit = 'btc')
45
+ return nil if amount.nil?
46
+
47
+ begin
48
+ (BigDecimal(amount, 16) / BigDecimal(UNITS[unit.to_sym], 16))
49
+ rescue StandardError => e
50
+ raise e.class
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module BitcoinUnitConverter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitcoin_unit_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Serigne Mouhamadou Bassirou Diaby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bigdecimal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-reporters
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ description:
70
+ email:
71
+ - bayevels@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".ruby-version"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - bitcoin_unit_converter.gemspec
85
+ - lib/bitcoin_unit_converter.rb
86
+ - lib/bitcoin_unit_converter/version.rb
87
+ - sig/bitcoin_unit_converter.rbs
88
+ homepage: https://github.com/bayevels/bitcoin_unit_converter
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ documentation_uri: https://github.com/bayevels/bitcoin_unit_converter
93
+ homepage_uri: https://github.com/bayevels/bitcoin_unit_converter
94
+ source_code_uri: https://github.com/bayevels/bitcoin_unit_converter
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.7.2
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.1.4
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Bitcoin unit converter in Ruby
114
+ test_files: []