sibit 0.11.0 → 0.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a410aaa9dcc78c30bde9cf5f0b38f5869a6b6b1bbb1a364b2a1ef603aceba72
4
- data.tar.gz: cc2b8197a9b230607b376205029300aa68ff55ac409b4c7a1334f46417ea5069
3
+ metadata.gz: 8fa2a41b4b9d7893fac773a90d7a9fcb3866339aa616b7a8e291d53bed50618f
4
+ data.tar.gz: d4f81c3c74df3927c5fec8dce9c7218cbf0a76d8cf6d1094ea2474aadd7e4525
5
5
  SHA512:
6
- metadata.gz: 3f28d118de6d6611298ebc0088c1583bf6a1d8ff99e0c46d9d37e19dfb896ae544a783933347ceb73a6335c8c88212ea73fee5cc81a136db854b903d79c68552
7
- data.tar.gz: d3f3a9ee63757febd44c8b309a6edc605f360e116fdb45d6c07f769883ba05a496dd624f424fb44fb2deb13fe71593ed3d9d334ab5dcee17ba798b1b4750556c
6
+ metadata.gz: 4ebf4d8cf70cef85a6d9d914cdec60cfa5c0164b8e7070f37e245d7b92267cf4fd6d1dd59ba6ff843c309861cbe5df2c19ec31af4a06874ad9e7b6a1191b371b
7
+ data.tar.gz: 6a9c5fca263fbec6b2aa61075c33314de00a0b22397d04662e135be46fbd556b699f21e3a4d856d43b9fcadb31eb306cb20134900125e91a3e0d9371fd1c676e
data/.rubocop.yml CHANGED
@@ -18,7 +18,7 @@ Layout/MultilineMethodCallIndentation:
18
18
  Metrics/ParameterLists:
19
19
  Max: 6
20
20
  Metrics/ClassLength:
21
- Max: 200
21
+ Max: 250
22
22
  Metrics/AbcSize:
23
23
  Enabled: false
24
24
  Metrics/BlockLength:
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  <img src="/logo.png" width="64px"/>
2
2
 
3
- [![EO principles respected here](http://www.elegantobjects.org/badge.svg)](http://www.elegantobjects.org)
3
+ [![EO principles respected here](https://www.elegantobjects.org/badge.svg)](https://www.elegantobjects.org)
4
4
  [![Managed by Zerocracy](https://www.0crat.com/badge/C3RFVLU72.svg)](https://www.0crat.com/p/C3RFVLU72)
5
5
  [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/sibit)](http://www.rultor.com/p/yegor256/sibit)
6
- [![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
6
+ [![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
7
7
 
8
8
  [![Build Status](https://travis-ci.org/yegor256/sibit.svg)](https://travis-ci.org/yegor256/sibit)
9
9
  [![Build status](https://ci.appveyor.com/api/projects/status/tbeaa0d4dk38xdb5?svg=true)](https://ci.appveyor.com/project/yegor256/sibit)
@@ -79,6 +79,13 @@ The transaction hash will be returned.
79
79
  Not all [UTXOs](https://en.wikipedia.org/wiki/Unspent_transaction_output)
80
80
  will be used, but only the necessary amount of them.
81
81
 
82
+ By default, the fee will be paid on top of the payment amount you are sending.
83
+ Say, you are sending 0.5 BTC and the fee is 0.0001 BTC. Totally, you will
84
+ spend 0.5001. However, you can make Sibit deduct the fee from the payment
85
+ amount. In this case you should provide a negative amount of the fee
86
+ or one of `-S`, `-M`, `-L`, `-XL`. You can also say `+S`, if you want the
87
+ opposite, which is the default.
88
+
82
89
  It is recommended to run it with `--dry --verbose` options first, to see
83
90
  what's going to be sent to the network. If everything looks correct, remove
84
91
  the `--dry` and run again, the transaction will be pushed to the network.
data/lib/sibit/version.rb CHANGED
@@ -26,5 +26,5 @@
26
26
  # License:: MIT
27
27
  class Sibit
28
28
  # Current version of the library.
29
- VERSION = '0.11.0'
29
+ VERSION = '0.12.0'
30
30
  end
data/lib/sibit.rb CHANGED
@@ -161,6 +161,8 @@ class Sibit
161
161
  def pay(amount, fee, sources, target, change)
162
162
  p = price
163
163
  satoshi = satoshi(amount)
164
+ f = mfee(fee, size_of(amount, sources))
165
+ satoshi -= f if f.negative?
164
166
  builder = Bitcoin::Builder::TxBuilder.new
165
167
  unspent = 0
166
168
  size = 100
@@ -259,9 +261,14 @@ class Sibit
259
261
  def mfee(fee, size)
260
262
  return fee.to_i if fee.is_a?(Integer)
261
263
  raise Error, 'Fee should either be a String or Integer' unless fee.is_a?(String)
264
+ mul = 1
265
+ if fee.start_with?('+', '-')
266
+ mul = -1 if fee.start_with?('-')
267
+ fee = fee[1..-1]
268
+ end
262
269
  sat = fees[fee.to_sym]
263
270
  raise Error, "Can't understand the fee: #{fee.inspect}" if sat.nil?
264
- sat * size
271
+ mul * sat * size
265
272
  end
266
273
 
267
274
  # Make key from private key string in Hash160.
@@ -296,6 +303,30 @@ class Sibit
296
303
  end
297
304
  end
298
305
 
306
+ # Calculate an approximate size of the transaction.
307
+ def size_of(amount, sources)
308
+ satoshi = satoshi(amount)
309
+ builder = Bitcoin::Builder::TxBuilder.new
310
+ unspent = 0
311
+ size = 100
312
+ utxos = get_json(
313
+ "/unspent?active=#{sources.keys.join('|')}&limit=1000"
314
+ )['unspent_outputs']
315
+ utxos.each do |utxo|
316
+ unspent += utxo['value']
317
+ builder.input do |i|
318
+ i.prev_out(utxo['tx_hash_big_endian'])
319
+ i.prev_out_index(utxo['tx_output_n'])
320
+ i.prev_out_script = [utxo['script']].pack('H*')
321
+ address = Bitcoin::Script.new([utxo['script']].pack('H*')).get_address
322
+ i.signature_key(key(sources[address]))
323
+ end
324
+ size += 180
325
+ break if unspent > satoshi
326
+ end
327
+ size
328
+ end
329
+
299
330
  def info(msg)
300
331
  if @log.respond_to?(:info)
301
332
  @log.info(msg)
data/test/test_sibit.rb CHANGED
@@ -129,6 +129,9 @@ class TestSibit < Minitest::Test
129
129
  end
130
130
 
131
131
  def test_fail_if_not_enough_funds
132
+ stub_request(
133
+ :get, 'https://bitcoinfees.earn.com/api/v1/fees/recommended'
134
+ ).to_return(body: '{"fastestFee":300,"halfHourFee":200,"hourFee":180}')
132
135
  stub_request(
133
136
  :get, 'https://blockchain.info/ticker'
134
137
  ).to_return(body: '{"USD" : {"15m" : 5160.04}}')
@@ -144,7 +147,7 @@ class TestSibit < Minitest::Test
144
147
  change = sibit.create(sibit.generate)
145
148
  assert_raises Sibit::Error do
146
149
  sibit.pay(
147
- '0.0001BTC', 'XL',
150
+ '0.0001BTC', '-XL',
148
151
  {
149
152
  '1JvCsJtLmCxEk7ddZFnVkGXpr9uhxZPmJi' =>
150
153
  'fd2333686f49d8647e1ce8d5ef39c304520b08f3c756b67068b30a3db217dcb2'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko