sibit 0.18.3 → 0.18.4
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 +4 -4
- data/lib/sibit/bitcoinchain.rb +10 -3
- data/lib/sibit/blockchain.rb +2 -2
- data/lib/sibit/blockchair.rb +9 -5
- data/lib/sibit/btc.rb +3 -3
- data/lib/sibit/earn.rb +1 -1
- data/lib/sibit/json.rb +2 -2
- data/lib/sibit/version.rb +1 -1
- data/test/test_live.rb +12 -3
- data/test/test_sibit.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9190dd104e8af92e3a5d8f1f6a967ef5d001fcf47e51adbaf260a281ecf043a5
|
|
4
|
+
data.tar.gz: f6b2280a10d118ad474b02022ad428de4c5775632c8f98a7f552c100ddb25460
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 863c1ce07d8c74d1029d29a27711244e12350d23f2cdcdc0da21a41793fdd98ce5685a8a725814c691d633e65e6d6858aa1cc716382a4465f93973df773a802c
|
|
7
|
+
data.tar.gz: 4b7556e9a790db63e63c2cdb0b836b8e3a7f1a9153cb1eba18f722c701b460c379ccda784feb40773f627e78f8129ef78ca7d2e9648431ecaa3608ef6409c89f
|
data/lib/sibit/bitcoinchain.rb
CHANGED
|
@@ -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
|
-
|
|
71
|
-
b
|
|
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
|
data/lib/sibit/blockchain.rb
CHANGED
|
@@ -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
|
data/lib/sibit/blockchair.rb
CHANGED
|
@@ -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
|
-
|
|
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, '
|
|
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, '
|
|
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, '
|
|
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, '
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
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.
|
|
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')
|