sibit 0.18.3 → 0.18.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6805c243e46030be83939f3f77aed146e40aa753693316b649fa465c8f1cacf
4
- data.tar.gz: 4ae1c9f68bc690ea3e73ef1686707c078bd262c4c98f87d6689c7c952bae9845
3
+ metadata.gz: 9190dd104e8af92e3a5d8f1f6a967ef5d001fcf47e51adbaf260a281ecf043a5
4
+ data.tar.gz: f6b2280a10d118ad474b02022ad428de4c5775632c8f98a7f552c100ddb25460
5
5
  SHA512:
6
- metadata.gz: f496223f5853ec59fa7b2f4647d06ffc735d7a810cc23b09cecd7621e88657b3fbcdb1d897609060913246dc20e784b916d4d997fd909f1a69e0ace2249e99bb
7
- data.tar.gz: 2da853866f8a5db42323da57b79fbc244c7eccf9d58140a5b1faa33e218f72e12d31b025c1748ab582e86d8f5983166bed3b68ca40c5e92745537d2174072e8d
6
+ metadata.gz: 863c1ce07d8c74d1029d29a27711244e12350d23f2cdcdc0da21a41793fdd98ce5685a8a725814c691d633e65e6d6858aa1cc716382a4465f93973df773a802c
7
+ data.tar.gz: 4b7556e9a790db63e63c2cdb0b836b8e3a7f1a9153cb1eba18f722c701b460c379ccda784feb40773f627e78f8129ef78ca7d2e9648431ecaa3608ef6409c89f
@@ -59,16 +59,23 @@ class Sibit
59
59
  URI("https://api-r.bitcoinchain.com/v1/block/#{hash}")
60
60
  )[0]['next_block']
61
61
  nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
62
+ @log.info("The next block of #{hash} is #{nxt}")
62
63
  nxt
63
64
  end
64
65
 
65
66
  # Gets the balance of the address, in satoshi.
66
67
  def balance(address)
67
68
  json = Sibit::Json.new(http: @http, log: @log).get(
68
- URI("https://api-r.bitcoinchain.com/v1/address/#{address}")
69
+ URI("https://api-r.bitcoinchain.com/v1/address/#{address}"),
70
+ accept: [200, 409]
69
71
  )[0]
70
- raise Sibit::Error, "Address #{address} not found" if json.nil?
71
- b = (json['balance'] * 100_000_000).to_i
72
+ b = json['balance']
73
+ if b.nil?
74
+ @log.info("The balance of #{address} is not visible")
75
+ return 0
76
+ end
77
+ b *= 100_000_000
78
+ b = b.to_i
72
79
  @log.info("The balance of #{address} is #{b} satoshi")
73
80
  b
74
81
  end
@@ -75,9 +75,9 @@ class Sibit
75
75
  # Gets the balance of the address, in satoshi.
76
76
  def balance(address)
77
77
  json = Sibit::Json.new(http: @http, log: @log).get(
78
- URI("https://blockchain.info/rawaddr/#{address}")
78
+ URI("https://blockchain.info/rawaddr/#{address}?limit=0"),
79
+ accept: [200, 500]
79
80
  )
80
- @log.info("Total transactions: #{json['n_tx']}")
81
81
  @log.info("Received/sent: #{json['total_received']}/#{json['total_sent']}")
82
82
  json['final_balance']
83
83
  end
@@ -66,7 +66,10 @@ class Sibit
66
66
  json = Sibit::Json.new(http: @http, log: @log).get(
67
67
  URI("https://api.blockchair.com/bitcoin/dashboards/address/#{address}?#{the_key}")
68
68
  )['data'][address]
69
- raise Sibit::Error, "Address #{address} not found" if json.nil?
69
+ if json.nil?
70
+ @log.info("Address #{address} not found")
71
+ return 0
72
+ end
70
73
  b = json['address']['balance']
71
74
  @log.info("The balance of #{address} is #{b} satoshi")
72
75
  b
@@ -74,17 +77,17 @@ class Sibit
74
77
 
75
78
  # Get recommended fees, in satoshi per byte.
76
79
  def fees
77
- raise Sibit::Error, 'Not implemented yet'
80
+ raise Sibit::Error, 'Blockchair doesn\'t implement fees()'
78
81
  end
79
82
 
80
83
  # Gets the hash of the latest block.
81
84
  def latest
82
- raise Sibit::Error, 'Not implemented yet'
85
+ raise Sibit::Error, 'Blockchair doesn\'t implement latest()'
83
86
  end
84
87
 
85
88
  # Fetch all unspent outputs per address.
86
89
  def utxos(_sources)
87
- raise Sibit::Error, 'Not implemented yet'
90
+ raise Sibit::Error, 'Blockchair doesn\'t implement utxos()'
88
91
  end
89
92
 
90
93
  # Push this transaction (in hex format) to the network.
@@ -93,11 +96,12 @@ class Sibit
93
96
  URI("https://api.blockchair.com/bitcoin/push/transaction?#{the_key}"),
94
97
  "data=#{hex}"
95
98
  )
99
+ @log.info("Transaction (#{hex.length} in hex) has been pushed to Blockchair")
96
100
  end
97
101
 
98
102
  # This method should fetch a Blockchain block and return as a hash.
99
103
  def block(_hash)
100
- raise Sibit::Error, 'Not implemented yet'
104
+ raise Sibit::Error, 'Blockchair doesn\'t implement block()'
101
105
  end
102
106
 
103
107
  private
data/lib/sibit/btc.rb CHANGED
@@ -54,12 +54,11 @@ class Sibit
54
54
  def balance(address)
55
55
  uri = URI("https://chain.api.btc.com/v3/address/#{address}/unspent")
56
56
  json = Sibit::Json.new(http: @http, log: @log).get(uri)
57
- data = json['data']
58
- if data.nil?
57
+ if json['err_no'] == 1
59
58
  @log.info("The balance of #{address} is zero (not found)")
60
59
  return 0
61
60
  end
62
- txns = data['list']
61
+ txns = json['data']['list']
63
62
  balance = txns.map { |tx| tx['value'] }.inject(&:+) || 0
64
63
  @log.info("The balance of #{address} is #{balance}, total txns: #{txns.count}")
65
64
  balance
@@ -72,6 +71,7 @@ class Sibit
72
71
  )
73
72
  nxt = head['data']['next_block_hash']
74
73
  nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
74
+ @log.info("The next block of #{hash} is #{nxt}")
75
75
  nxt
76
76
  end
77
77
 
data/lib/sibit/earn.rb CHANGED
@@ -69,7 +69,7 @@ class Sibit
69
69
  URI('https://bitcoinfees.earn.com/api/v1/fees/recommended')
70
70
  )
71
71
  @log.info("Current recommended Bitcoin fees: \
72
- #{json['hourFee']}/#{json['halfHourFee']}/#{json['fastestFee']} sat/byte")
72
+ #{json['hourFee']}/#{json['halfHourFee']}/#{json['fastestFee']} sat/byte")
73
73
  {
74
74
  S: json['hourFee'] / 3,
75
75
  M: json['hourFee'],
data/lib/sibit/json.rb CHANGED
@@ -48,7 +48,7 @@ class Sibit
48
48
  # Send GET request to the HTTP and return JSON response.
49
49
  # This method will also log the process and will validate the
50
50
  # response for correctness.
51
- def get(uri, headers: {})
51
+ def get(uri, headers: {}, accept: [200])
52
52
  start = Time.now
53
53
  res = @http.client(uri).get(
54
54
  "#{uri.path.empty? ? '/' : uri.path}#{uri.query ? "?#{uri.query}" : ''}",
@@ -59,7 +59,7 @@ class Sibit
59
59
  'Accept-Encoding' => ''
60
60
  }.merge(headers)
61
61
  )
62
- unless res.code == '200'
62
+ unless accept.include?(res.code.to_i)
63
63
  raise Sibit::Error, "Failed to retrieve #{uri} (#{res.code}): #{res.body}"
64
64
  end
65
65
  @log.info("GET #{uri}: #{res.code}/#{res.body.length}b in #{age(start)}")
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.18.3'
29
+ VERSION = '0.18.4'
30
30
  end
data/test/test_live.rb CHANGED
@@ -23,6 +23,7 @@
23
23
  require 'minitest/autorun'
24
24
  require 'webmock/minitest'
25
25
  require 'json'
26
+ require 'backtrace'
26
27
  require_relative '../lib/sibit'
27
28
 
28
29
  # Live tests.
@@ -60,6 +61,14 @@ class TestLive < Minitest::Test
60
61
  end
61
62
  end
62
63
 
64
+ def test_absent_balance
65
+ for_each do |api|
66
+ hash = '12NJ7DxjBMCkk7EFdb6nXnMsuJV1nAXGiM'
67
+ satoshi = api.balance(hash)
68
+ assert_equal(0, satoshi)
69
+ end
70
+ end
71
+
63
72
  def test_latest
64
73
  for_each do |api|
65
74
  hash = api.latest
@@ -108,14 +117,14 @@ class TestLive < Minitest::Test
108
117
  skip if ENV['skip_live']
109
118
  WebMock.allow_net_connect!
110
119
  apis = []
120
+ require_relative '../lib/sibit/btc'
121
+ apis << Sibit::Btc.new
111
122
  require_relative '../lib/sibit/blockchain'
112
123
  apis << Sibit::Blockchain.new
113
124
  require_relative '../lib/sibit/blockchair'
114
125
  apis << Sibit::Blockchair.new
115
126
  require_relative '../lib/sibit/cryptoapis'
116
127
  apis << Sibit::Cryptoapis.new('')
117
- require_relative '../lib/sibit/btc'
118
- apis << Sibit::Btc.new
119
128
  require_relative '../lib/sibit/cex'
120
129
  apis << Sibit::Cex.new
121
130
  require_relative '../lib/sibit/bitcoinchain'
@@ -124,7 +133,7 @@ class TestLive < Minitest::Test
124
133
  begin
125
134
  yield api
126
135
  rescue Sibit::Error => e
127
- puts e.message
136
+ puts Backtrace.new(e).to_s
128
137
  end
129
138
  end
130
139
  end
data/test/test_sibit.rb CHANGED
@@ -87,7 +87,7 @@ class TestSibit < Minitest::Test
87
87
  def test_get_balance
88
88
  stub_request(
89
89
  :get,
90
- 'https://blockchain.info/rawaddr/1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f'
90
+ 'https://blockchain.info/rawaddr/1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f?limit=0'
91
91
  ).to_return(body: '{"final_balance": 100}')
92
92
  sibit = Sibit.new
93
93
  balance = sibit.balance('1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f')
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.18.3
4
+ version: 0.18.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko