sibit 0.15.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -0
- data/lib/sibit/bitcoinchain.rb +3 -3
- data/lib/sibit/btc.rb +1 -1
- data/lib/sibit/cryptoapis.rb +32 -14
- data/lib/sibit/version.rb +1 -1
- data/test/test_cryptoapis.rb +3 -2
- data/test/test_live.rb +3 -2
- 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: ef4603f30e6d52d2efec48d698778505150df7b5e98400938db804a1a412a053
|
4
|
+
data.tar.gz: 1c30fe4e9d7e1c077204b26282ed78a169b6e86cc630baf376903bb2f8103601
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e1c22106253d2ee0ef9ab0d38e27526b4553c26bd1c01c8cb66910f692c19cd465b85b8104d5ea09c9dd2b22446e917bb333ff8cb7e1db3c730e0e6f3b472aa
|
7
|
+
data.tar.gz: 035dfbccaef19b8ff39c7c404a0b11f058d813a0df492c741a6849ddac8eb0b990892523c186929e4b2822939d6011f246417c31ff3c24a5123732b3fe30b0db
|
data/README.md
CHANGED
@@ -113,6 +113,41 @@ tx = sibit.pay(10_000_000, 'XL', { address => pkey }, target, change)
|
|
113
113
|
|
114
114
|
Should work.
|
115
115
|
|
116
|
+
## APIs
|
117
|
+
|
118
|
+
The library works through one (or a few) public APIs for fetching
|
119
|
+
Bitcoin data and pushing transactions to the network. At the moment we
|
120
|
+
work with the following APIs:
|
121
|
+
|
122
|
+
* [Blockchain.com](https://www.blockchain.com/api/blockchain_api)
|
123
|
+
* [BTC.com](https://btc.com/api-doc)
|
124
|
+
* [Cryptoapis.io](https://docs.cryptoapis.io/rest-apis/blockchain-as-a-service-apis/btc/index)
|
125
|
+
* [Bitcoinchain.com](https://bitcoinchain.com/api)
|
126
|
+
* [Earn.com](https://bitcoinfees.earn.com/api)
|
127
|
+
|
128
|
+
The first one in this list is used by default. If you want to use a diffent
|
129
|
+
one, you just specify it in the constructor of `Sibit` object:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
require 'sibit'
|
133
|
+
require 'sibit/btc'
|
134
|
+
sibit = Sibit.new(api: Sibit::Btc.new)
|
135
|
+
```
|
136
|
+
|
137
|
+
You may also use a combination of APIs. This may be very useful since
|
138
|
+
some APIs are not reliable. You can provide an array of objects and they
|
139
|
+
will be used one by one, until a successful response is obtained:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
require 'sibit'
|
143
|
+
require 'sibit/btc'
|
144
|
+
require 'sibit/cryptoapis'
|
145
|
+
sibit = Sibit.new(api: [Sibit::Btc.new, Sibit::Cryptoapis.new('key')])
|
146
|
+
```
|
147
|
+
|
148
|
+
If you think we may need to use some other API, you can submit a ticket,
|
149
|
+
or implement it yourself and submit a pull request.
|
150
|
+
|
116
151
|
## How to install
|
117
152
|
|
118
153
|
To install on a fresh Ubuntu 18:
|
data/lib/sibit/bitcoinchain.rb
CHANGED
@@ -45,14 +45,14 @@ class Sibit
|
|
45
45
|
|
46
46
|
# Current price of BTC in USD (float returned).
|
47
47
|
def price(_currency)
|
48
|
-
raise Sibit::Error, '
|
48
|
+
raise Sibit::Error, 'Bitcoinchain API doesn\'t provide BTC price'
|
49
49
|
end
|
50
50
|
|
51
51
|
# Gets the balance of the address, in satoshi.
|
52
52
|
def balance(address)
|
53
|
-
Sibit::Json.new(http: @http, log: @log).get(
|
53
|
+
(Sibit::Json.new(http: @http, log: @log).get(
|
54
54
|
URI("https://api-r.bitcoinchain.com/v1/address/#{address}")
|
55
|
-
)[0]['balance'] * 100_000_000
|
55
|
+
)[0]['balance'] * 100_000_000).to_i
|
56
56
|
end
|
57
57
|
|
58
58
|
# Get recommended fees, in satoshi per byte.
|
data/lib/sibit/btc.rb
CHANGED
data/lib/sibit/cryptoapis.rb
CHANGED
@@ -46,15 +46,15 @@ class Sibit
|
|
46
46
|
|
47
47
|
# Current price of BTC in USD (float returned).
|
48
48
|
def price(_currency)
|
49
|
-
raise Sibit::Error, '
|
49
|
+
raise Sibit::Error, 'Cryptoapis doesn\'t provide BTC price'
|
50
50
|
end
|
51
51
|
|
52
52
|
# Gets the balance of the address, in satoshi.
|
53
53
|
def balance(address)
|
54
|
-
Sibit::Json.new(http: @http, log: @log).get(
|
54
|
+
(Sibit::Json.new(http: @http, log: @log).get(
|
55
55
|
URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/address/#{address}"),
|
56
56
|
headers: headers
|
57
|
-
)['payload']['balance'].to_f * 100_000_000
|
57
|
+
)['payload']['balance'].to_f * 100_000_000).to_i
|
58
58
|
end
|
59
59
|
|
60
60
|
# Get recommended fees, in satoshi per byte.
|
@@ -95,8 +95,30 @@ class Sibit
|
|
95
95
|
orphan: false,
|
96
96
|
next: head['nextblockhash'],
|
97
97
|
previous: head['previousblockhash'],
|
98
|
-
txns:
|
99
|
-
|
98
|
+
txns: txns(hash)
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def headers
|
105
|
+
{
|
106
|
+
'X-API-Key': @key
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
def txns(hash)
|
111
|
+
index = 0
|
112
|
+
limit = 200
|
113
|
+
all = []
|
114
|
+
loop do
|
115
|
+
txns = Sibit::Json.new(http: @http, log: @log).get(
|
116
|
+
URI(
|
117
|
+
[
|
118
|
+
'https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/',
|
119
|
+
"#{hash}?index=#{index}&limit=#{limit}"
|
120
|
+
].join
|
121
|
+
),
|
100
122
|
headers: headers
|
101
123
|
)['payload'].map do |t|
|
102
124
|
{
|
@@ -109,15 +131,11 @@ class Sibit
|
|
109
131
|
end
|
110
132
|
}
|
111
133
|
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def headers
|
118
|
-
{
|
119
|
-
'X-API-Key': @key
|
120
|
-
}
|
134
|
+
all += txns
|
135
|
+
index += txns.length
|
136
|
+
break if txns.length < limit
|
137
|
+
end
|
138
|
+
all
|
121
139
|
end
|
122
140
|
end
|
123
141
|
end
|
data/lib/sibit/version.rb
CHANGED
data/test/test_cryptoapis.rb
CHANGED
@@ -33,9 +33,10 @@ require_relative '../lib/sibit/cryptoapis'
|
|
33
33
|
class TestCryptoapis < Minitest::Test
|
34
34
|
def test_fetch_block
|
35
35
|
hash = '000000000000000007341915521967247f1dec17b3a311b8a8f4495392f1439b'
|
36
|
-
|
36
|
+
url = 'https://api.cryptoapis.io/v1/bc/btc/mainnet'
|
37
|
+
stub_request(:get, "#{url}/blocks/#{hash}")
|
37
38
|
.to_return(body: '{"payload": {"nextblockhash": "n", "hash": "h", "previousblockhash": "p"}}')
|
38
|
-
stub_request(:get, "
|
39
|
+
stub_request(:get, "#{url}/txs/block/#{hash}?index=0&limit=200")
|
39
40
|
.to_return(body: '{"payload": [{"hash": "thash",
|
40
41
|
"txouts": [{"addresses": ["a1"], "value": 123}]}]}')
|
41
42
|
sibit = Sibit::Cryptoapis.new('-')
|
data/test/test_live.rb
CHANGED
@@ -30,7 +30,7 @@ require_relative '../lib/sibit'
|
|
30
30
|
# Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
|
31
31
|
# License:: MIT
|
32
32
|
class TestLive < Minitest::Test
|
33
|
-
def
|
33
|
+
def test_block
|
34
34
|
for_each do |api|
|
35
35
|
hash = '000000003031a0e73735690c5a1ff2a4be82553b2a12b776fbd3a215dc8f778d'
|
36
36
|
json = api.block(hash)
|
@@ -55,6 +55,7 @@ class TestLive < Minitest::Test
|
|
55
55
|
for_each do |api|
|
56
56
|
hash = '1GkQmKAmHtNfnD3LHhTkewJxKHVSta4m2a'
|
57
57
|
satoshi = api.balance(hash)
|
58
|
+
assert(satoshi.is_a?(Integer), "Wrong type of balance: #{satoshi.class.name}")
|
58
59
|
assert_equal(5_000_028_421, satoshi)
|
59
60
|
end
|
60
61
|
end
|
@@ -73,7 +74,7 @@ class TestLive < Minitest::Test
|
|
73
74
|
WebMock.allow_net_connect!
|
74
75
|
apis = []
|
75
76
|
require_relative '../lib/sibit/cryptoapis'
|
76
|
-
apis << Sibit::Cryptoapis.new('
|
77
|
+
apis << Sibit::Cryptoapis.new('-key-')
|
77
78
|
require_relative '../lib/sibit/btc'
|
78
79
|
apis << Sibit::Btc.new
|
79
80
|
require_relative '../lib/sibit/bitcoinchain'
|