sibit 0.14.5 → 0.15.0

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: 1f5687f2fe1a1104c3beb596b08818f0a9526405df9e6022c68a47f575549c16
4
- data.tar.gz: ee3f5f71d21739855368af24ac879b4e5b4577e59f605515a662893d778cc447
3
+ metadata.gz: 64cc132d274ca5088ae00fbdacca8c5b7e689c5b13fe78cffacfd0300729d9ae
4
+ data.tar.gz: 5dd1f2f5bdc4f2450c56d9e177ac8018f81b92a0e0c43ef0a43d3324a70abb1d
5
5
  SHA512:
6
- metadata.gz: df736816b94d5975dcd6c589ab48467f3907a42091c1e032f4ef7dc8f8c89c082409555d45957d93ea74b0b3cb98bd19c53c1091df66ab7288211218f1af0b57
7
- data.tar.gz: bd341019c48a1738e6cc7f788827dbb625c7b0f8756267efbbed1efc19245c21b9cd75bc121596a89ce4ab8a010151c91b876daf9c21441fde0b2e72d1c1a283
6
+ metadata.gz: e1ef829b0e6c2b288b4dc1bdf0ed692b825ee560dd2e047f4607a38478aafef9a575e35b0f782fdfc32c7d7883fe5a7ee099374f4879df692aba81a4c8219d7a
7
+ data.tar.gz: c0c63033b166fb85ea73a25a8b9f5a6aa41020685a5ef9f16c397dd28ea9ea7111cd15f00d5c4ff598cffdd9ecc77a4e68d46cb5268df714035b6470268f14ac
@@ -52,7 +52,7 @@ class Sibit
52
52
  def balance(address)
53
53
  Sibit::Json.new(http: @http, log: @log).get(
54
54
  URI("https://api-r.bitcoinchain.com/v1/address/#{address}")
55
- )[0]['balance']
55
+ )[0]['balance'] * 100_000_000
56
56
  end
57
57
 
58
58
  # Get recommended fees, in satoshi per byte.
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-2020 Yegor Bugayenko
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 all
13
+ # 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 NONINFINGEMENT. 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 THE
21
+ # SOFTWARE.
22
+
23
+ require 'uri'
24
+ require 'json'
25
+ require_relative 'version'
26
+ require_relative 'error'
27
+ require_relative 'log'
28
+ require_relative 'http'
29
+ require_relative 'json'
30
+
31
+ # Cryptoapis.io API.
32
+ #
33
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
34
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
35
+ # License:: MIT
36
+ class Sibit
37
+ # Btc.com API.
38
+ class Cryptoapis
39
+ # Constructor.
40
+ def initialize(key, log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
41
+ @key = key
42
+ @http = http
43
+ @log = log
44
+ @dry = dry
45
+ end
46
+
47
+ # Current price of BTC in USD (float returned).
48
+ def price(_currency)
49
+ raise Sibit::Error, 'Not implemented yet'
50
+ end
51
+
52
+ # Gets the balance of the address, in satoshi.
53
+ def balance(address)
54
+ Sibit::Json.new(http: @http, log: @log).get(
55
+ URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/address/#{address}"),
56
+ headers: headers
57
+ )['payload']['balance'].to_f * 100_000_000
58
+ end
59
+
60
+ # Get recommended fees, in satoshi per byte.
61
+ def fees
62
+ raise Sibit::Error, 'Not implemented yet'
63
+ end
64
+
65
+ # Gets the hash of the latest block.
66
+ def latest
67
+ Sibit::Json.new(http: @http, log: @log).get(
68
+ URI('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/latest'),
69
+ headers: headers
70
+ )['payload']['hash']
71
+ end
72
+
73
+ # Fetch all unspent outputs per address.
74
+ def utxos(_sources)
75
+ raise Sibit::Error, 'Not implemented yet'
76
+ end
77
+
78
+ # Push this transaction (in hex format) to the network.
79
+ def push(hex)
80
+ Sibit::Json.new(http: @http, log: @log).post(
81
+ URI('https://api.cryptoapis.io/v1/bc/btc/testnet/txs/send'),
82
+ JSON.pretty_generate(hex: hex),
83
+ headers: headers
84
+ )
85
+ end
86
+
87
+ # This method should fetch a Blockchain block and return as a hash.
88
+ def block(hash)
89
+ head = Sibit::Json.new(http: @http, log: @log).get(
90
+ URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/#{hash}"),
91
+ headers: headers
92
+ )['payload']
93
+ {
94
+ hash: head['hash'],
95
+ orphan: false,
96
+ next: head['nextblockhash'],
97
+ previous: head['previousblockhash'],
98
+ txns: Sibit::Json.new(http: @http, log: @log).get(
99
+ URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/#{hash}"),
100
+ headers: headers
101
+ )['payload'].map do |t|
102
+ {
103
+ hash: t['hash'],
104
+ outputs: t['txouts'].map do |o|
105
+ {
106
+ address: o['addresses'][0],
107
+ value: o['amount'].to_f * 100_000_000
108
+ }
109
+ end
110
+ }
111
+ end
112
+ }
113
+ end
114
+
115
+ private
116
+
117
+ def headers
118
+ {
119
+ 'X-API-Key': @key
120
+ }
121
+ end
122
+ end
123
+ end
data/lib/sibit/json.rb CHANGED
@@ -48,14 +48,16 @@ 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)
51
+ def get(uri, headers: {})
52
52
  start = Time.now
53
53
  res = @http.client(uri).get(
54
54
  "#{uri.path}?#{uri.query}",
55
- 'Accept' => 'application/json',
56
- 'User-Agent' => user_agent,
57
- 'Accept-Charset' => 'UTF-8',
58
- 'Accept-Encoding' => ''
55
+ {
56
+ 'Accept' => 'application/json',
57
+ 'User-Agent' => user_agent,
58
+ 'Accept-Charset' => 'UTF-8',
59
+ 'Accept-Encoding' => ''
60
+ }.merge(headers)
59
61
  )
60
62
  unless res.code == '200'
61
63
  raise Sibit::Error, "Failed to retrieve #{uri} (#{res.code}): #{res.body}"
@@ -64,16 +66,18 @@ class Sibit
64
66
  JSON.parse(res.body)
65
67
  end
66
68
 
67
- def post(uri, body)
69
+ def post(uri, body, headers: {})
68
70
  start = Time.now
69
71
  res = @http.client(uri).post(
70
72
  "#{uri.path}?#{uri.query}",
71
73
  "tx=#{CGI.escape(body)}",
72
- 'Accept' => 'text/plain',
73
- 'User-Agent' => user_agent,
74
- 'Accept-Charset' => 'UTF-8',
75
- 'Accept-Encoding' => '',
76
- 'Content-Type' => 'application/x-www-form-urlencoded'
74
+ {
75
+ 'Accept' => 'text/plain',
76
+ 'User-Agent' => user_agent,
77
+ 'Accept-Charset' => 'UTF-8',
78
+ 'Accept-Encoding' => '',
79
+ 'Content-Type' => 'application/x-www-form-urlencoded'
80
+ }.merge(headers)
77
81
  )
78
82
  unless res.code == '200'
79
83
  raise Sibit::Error, "Failed to post tx to #{uri}: #{res.code}\n#{res.body}"
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.14.5'
29
+ VERSION = '0.15.0'
30
30
  end
@@ -45,10 +45,10 @@ class TestBitcoinchain < Minitest::Test
45
45
  def test_fetch_balance
46
46
  hash = '1Chain4asCYNnLVbvG6pgCLGBrtzh4Lx4b'
47
47
  stub_request(:get, "https://api-r.bitcoinchain.com/v1/address/#{hash}")
48
- .to_return(body: '[{"balance": 123}]')
48
+ .to_return(body: '[{"balance": 5}]')
49
49
  sibit = Sibit::Bitcoinchain.new
50
50
  satoshi = sibit.balance(hash)
51
- assert_equal(123, satoshi)
51
+ assert_equal(500_000_000, satoshi)
52
52
  end
53
53
 
54
54
  def test_fetch_block
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-2020 Yegor Bugayenko
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 all
13
+ # 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 NONINFINGEMENT. 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 THE
21
+ # SOFTWARE.
22
+
23
+ require 'minitest/autorun'
24
+ require 'webmock/minitest'
25
+ require 'json'
26
+ require_relative '../lib/sibit'
27
+ require_relative '../lib/sibit/cryptoapis'
28
+
29
+ # Sibit::Cryptoapis test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestCryptoapis < Minitest::Test
34
+ def test_fetch_block
35
+ hash = '000000000000000007341915521967247f1dec17b3a311b8a8f4495392f1439b'
36
+ stub_request(:get, "https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/#{hash}")
37
+ .to_return(body: '{"payload": {"nextblockhash": "n", "hash": "h", "previousblockhash": "p"}}')
38
+ stub_request(:get, "https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/#{hash}")
39
+ .to_return(body: '{"payload": [{"hash": "thash",
40
+ "txouts": [{"addresses": ["a1"], "value": 123}]}]}')
41
+ sibit = Sibit::Cryptoapis.new('-')
42
+ json = sibit.block(hash)
43
+ assert(json[:next])
44
+ assert(json[:previous])
45
+ assert_equal('h', json[:hash])
46
+ assert(json[:txns].is_a?(Array))
47
+ assert_equal('thash', json[:txns][0][:hash])
48
+ assert(json[:txns][0][:outputs].is_a?(Array))
49
+ end
50
+ end
data/test/test_live.rb CHANGED
@@ -24,9 +24,6 @@ require 'minitest/autorun'
24
24
  require 'webmock/minitest'
25
25
  require 'json'
26
26
  require_relative '../lib/sibit'
27
- require_relative '../lib/sibit/blockchain'
28
- require_relative '../lib/sibit/btc'
29
- require_relative '../lib/sibit/bitcoinchain'
30
27
 
31
28
  # Live tests.
32
29
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -34,7 +31,6 @@ require_relative '../lib/sibit/bitcoinchain'
34
31
  # License:: MIT
35
32
  class TestLive < Minitest::Test
36
33
  def test_fetch_block
37
- skip
38
34
  for_each do |api|
39
35
  hash = '000000003031a0e73735690c5a1ff2a4be82553b2a12b776fbd3a215dc8f778d'
40
36
  json = api.block(hash)
@@ -55,11 +51,36 @@ class TestLive < Minitest::Test
55
51
  end
56
52
  end
57
53
 
54
+ def test_balance
55
+ for_each do |api|
56
+ hash = '1GkQmKAmHtNfnD3LHhTkewJxKHVSta4m2a'
57
+ satoshi = api.balance(hash)
58
+ assert_equal(5_000_028_421, satoshi)
59
+ end
60
+ end
61
+
62
+ def test_latest
63
+ for_each do |api|
64
+ hash = api.latest
65
+ assert_equal(64, hash.length)
66
+ end
67
+ end
68
+
58
69
  private
59
70
 
60
71
  def for_each
72
+ skip
61
73
  WebMock.allow_net_connect!
62
- [Sibit::Btc.new, Sibit::Bitcoinchain.new, Sibit::Blockchain.new].each do |api|
74
+ apis = []
75
+ require_relative '../lib/sibit/cryptoapis'
76
+ apis << Sibit::Cryptoapis.new('--api-key--')
77
+ require_relative '../lib/sibit/btc'
78
+ apis << Sibit::Btc.new
79
+ require_relative '../lib/sibit/bitcoinchain'
80
+ apis << Sibit::Bitcoinchain.new
81
+ require_relative '../lib/sibit/blockchain'
82
+ apis << Sibit::Blockchain.new
83
+ apis.each do |api|
63
84
  begin
64
85
  yield api
65
86
  rescue Sibit::Error => e
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.14.5
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -284,6 +284,7 @@ files:
284
284
  - lib/sibit/bitcoinchain.rb
285
285
  - lib/sibit/blockchain.rb
286
286
  - lib/sibit/btc.rb
287
+ - lib/sibit/cryptoapis.rb
287
288
  - lib/sibit/earn.rb
288
289
  - lib/sibit/error.rb
289
290
  - lib/sibit/fake.rb
@@ -297,6 +298,7 @@ files:
297
298
  - test/test_bitcoinchain.rb
298
299
  - test/test_blockchain.rb
299
300
  - test/test_btc.rb
301
+ - test/test_cryptoapis.rb
300
302
  - test/test_fake.rb
301
303
  - test/test_json.rb
302
304
  - test/test_live.rb
@@ -334,6 +336,7 @@ test_files:
334
336
  - test/test_bitcoinchain.rb
335
337
  - test/test_blockchain.rb
336
338
  - test/test_btc.rb
339
+ - test/test_cryptoapis.rb
337
340
  - test/test_fake.rb
338
341
  - test/test_json.rb
339
342
  - test/test_live.rb