peatio-nexbit 0.1.4 → 0.1.5

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: a82ea55c191482e1d753cb610ca3a3ca91bcc08e6eb3a40c8ab0ed13992c1b44
4
- data.tar.gz: 59a0bde26769ec15dc2fcfaa8bc6994587b241fce4a254145cc4aac8c499dcf6
3
+ metadata.gz: 13ff7fb6158882cd96c628152920a9ce24568d3bf889e6fc1c3942800972c78c
4
+ data.tar.gz: 8ad637e6d4228a8d60d483ee73dcc116a29b030f06810db5ef232f9b9b13072a
5
5
  SHA512:
6
- metadata.gz: 5e44e3d307ec6d334b69e907242fc86574c3d38066662a7010fa77e8125960d3acc00ae6a77d8e4b65969ca72ea330f9c4ee0d247f0e13955c8bf2f88e27b5f3
7
- data.tar.gz: 3acc9945551f5c2294ce2d7300e5f2d8f4c543f0f34b9a4ec31996f0393a865f02433663ea09d80f71efc9d9d403f2aa1fc508ae1112d3d2e450f4de56a4097a
6
+ metadata.gz: 2bb54d4ba7be7b7d058ccdfbeb0bc7628708055c078263df987aca25d1a36e5863950fcf35dd41fbdc91b5a03180b70421cf68c095f1df61a7e212bbe2aa16a0
7
+ data.tar.gz: 16d31ceab3543fd72b0b9577105dc3b1120d6d3264cdfde31b34f2ce87be562702a9980baef98dff433598a4ab6087b3dc795f1cd1c51baebeae6068a9cec797
@@ -1,82 +1,85 @@
1
- module Nexbit
2
- # TODO: Processing of unconfirmed transactions from mempool isn't supported now.
3
- class Blockchain < Peatio::Blockchain::Abstract
4
1
 
5
- DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
2
+ module Peatio
3
+ module Nexbit
4
+ # TODO: Processing of unconfirmed transactions from mempool isn't supported now.
5
+ class Blockchain < Peatio::Blockchain::Abstract
6
6
 
7
- def initialize(custom_features = {})
8
- @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
9
- @settings = {}
10
- end
7
+ DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
11
8
 
12
- def configure(settings = {})
13
- # Clean client state during configure.
14
- @client = nil
15
- @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
16
- end
9
+ def initialize(custom_features = {})
10
+ @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
11
+ @settings = {}
12
+ end
13
+
14
+ def configure(settings = {})
15
+ # Clean client state during configure.
16
+ @client = nil
17
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
18
+ end
17
19
 
18
- def fetch_block!(block_number)
19
- block_hash = client.json_rpc(:getblockhash, [block_number])
20
+ def fetch_block!(block_number)
21
+ block_hash = client.json_rpc(:getblockhash, [block_number])
20
22
 
21
- client.json_rpc(:getblock, [block_hash, 2])
22
- .fetch('tx').each_with_object([]) do |tx, txs_array|
23
+ client.json_rpc(:getblock, [block_hash, 2])
24
+ .fetch('tx').each_with_object([]) do |tx, txs_array|
23
25
  txs = build_transaction(tx).map do |ntx|
24
26
  Peatio::Transaction.new(ntx.merge(block_number: block_number))
25
27
  end
26
28
  txs_array.append(*txs)
27
29
  end.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) }
28
- rescue Nexbit::Client::Error => e
29
- raise Peatio::Blockchain::ClientError, e
30
- end
30
+ rescue Client::Error => e
31
+ raise Peatio::Blockchain::ClientError, e
32
+ end
31
33
 
32
- def latest_block_number
33
- client.json_rpc(:getblockcount)
34
- rescue Nexbit::Client::Error => e
35
- raise Peatio::Blockchain::ClientError, e
36
- end
34
+ def latest_block_number
35
+ client.json_rpc(:getblockcount)
36
+ rescue Client::Error => e
37
+ raise Peatio::Blockchain::ClientError, e
38
+ end
37
39
 
38
- def load_balance_of_address!(address, _currency_id)
39
- address_with_balance = client.json_rpc(:listaddressgroupings)
40
- .flatten(1)
41
- .find { |addr| addr[0] == address }
40
+ def load_balance_of_address!(address, _currency_id)
41
+ address_with_balance = client.json_rpc(:listaddressgroupings)
42
+ .flatten(1)
43
+ .find { |addr| addr[0] == address }
42
44
 
43
- if address_with_balance.blank?
44
- raise Peatio::Blockchain::UnavailableAddressBalanceError, address
45
- end
45
+ if address_with_balance.blank?
46
+ raise Peatio::Blockchain::UnavailableAddressBalanceError, address
47
+ end
46
48
 
47
- address_with_balance[1].to_d
48
- rescue Nexbit::Client::Error => e
49
- raise Peatio::Blockchain::ClientError, e
50
- end
49
+ address_with_balance[1].to_d
50
+ rescue Client::Error => e
51
+ raise Peatio::Blockchain::ClientError, e
52
+ end
51
53
 
52
- private
54
+ private
53
55
 
54
- def build_transaction(tx_hash)
55
- tx_hash.fetch('vout')
56
- .select do |entry|
56
+ def build_transaction(tx_hash)
57
+ tx_hash.fetch('vout')
58
+ .select do |entry|
57
59
  entry.fetch('value').to_d > 0 &&
58
- entry['scriptPubKey'].has_key?('addresses')
60
+ entry['scriptPubKey'].has_key?('addresses')
59
61
  end
60
- .each_with_object([]) do |entry, formatted_txs|
62
+ .each_with_object([]) do |entry, formatted_txs|
61
63
  no_currency_tx =
62
64
  { hash: tx_hash['txid'], txout: entry['n'],
63
65
  to_address: entry['scriptPubKey']['addresses'][0],
64
66
  amount: entry.fetch('value').to_d,
65
67
  status: 'success' }
66
68
 
67
- # Build transaction for each currency belonging to blockchain.
68
- settings_fetch(:currencies).pluck(:id).each do |currency_id|
69
- formatted_txs << no_currency_tx.merge(currency_id: currency_id)
70
- end
69
+ # Build transaction for each currency belonging to blockchain.
70
+ settings_fetch(:currencies).pluck(:id).each do |currency_id|
71
+ formatted_txs << no_currency_tx.merge(currency_id: currency_id)
72
+ end
71
73
  end
72
- end
74
+ end
73
75
 
74
- def client
75
- @client ||= Nexbit::Client.new(settings_fetch(:server))
76
- end
76
+ def client
77
+ @client ||= Client.new(settings_fetch(:server))
78
+ end
77
79
 
78
- def settings_fetch(key)
79
- @settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
80
+ def settings_fetch(key)
81
+ @settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
82
+ end
80
83
  end
81
84
  end
82
85
  end
@@ -1,48 +1,61 @@
1
- module Nexbit
2
- class Client
3
- Error = Class.new(StandardError)
1
+ require 'memoist'
2
+ require 'faraday'
3
+ require 'better-faraday'
4
4
 
5
- class ConnectionError < Error; end
5
+ module Peatio
6
+ module Nexbit
7
+ class Client
8
+ Error = Class.new(StandardError)
9
+ class ConnectionError < Error; end
6
10
 
7
- class ResponseError < Error
8
- def initialize(code, msg)
9
- super "#{msg} (#{code})"
11
+ class ResponseError < Error
12
+ def initialize(code, msg)
13
+ @code = code
14
+ @msg = msg
15
+ end
16
+
17
+ def message
18
+ "#{@msg} (#{@code})"
19
+ end
10
20
  end
11
- end
12
21
 
13
- extend Memoist
22
+ extend Memoist
14
23
 
15
- def initialize(endpoint, idle_timeout: 5)
16
- @json_rpc_endpoint = URI.parse(endpoint)
17
- @idle_timeout = idle_timeout
18
- end
24
+ def initialize(endpoint)
25
+ @json_rpc_endpoint = URI.parse(endpoint)
26
+ end
19
27
 
20
- def json_rpc(method, params = [])
21
- response = connection.post \
22
- '/',
23
- {jsonrpc: '1.0', method: method, params: params}.to_json,
24
- {'Accept' => 'application/json',
25
- 'Content-Type' => 'application/json'}
26
- response.assert_success!
27
- response = JSON.parse(response.body)
28
- response['error'].tap { |error| raise ResponseError.new(error['code'], error['message']) if error }
29
- response.fetch('result')
30
- rescue Faraday::Error => e
31
- raise ConnectionError, e
32
- rescue StandardError => e
33
- raise Error, e
34
- end
28
+ def json_rpc(method, params = [])
29
+ response = connection.post \
30
+ '/',
31
+ { jsonrpc: '1.0', method: method, params: params }.to_json,
32
+ { 'Accept' => 'application/json',
33
+ 'Content-Type' => 'application/json' }
34
+ response.assert_2xx!
35
+ response = JSON.parse(response.body)
36
+ response['error'].tap { |e| raise ResponseError.new(e['code'], e['message']) if e }
37
+ response.fetch('result')
38
+ rescue => e
39
+ if e.is_a?(Error)
40
+ raise e
41
+ elsif e.is_a?(Faraday::Error)
42
+ raise ConnectionError, e
43
+ else
44
+ raise Error, e
45
+ end
46
+ end
35
47
 
36
- private
48
+ private
37
49
 
38
- def connection
39
- @connection ||= Faraday.new(@json_rpc_endpoint) do |f|
40
- f.adapter :net_http_persistent, pool_size: 5, idle_timeout: @idle_timeout
41
- end.tap do |connection|
42
- unless @json_rpc_endpoint.user.blank?
43
- connection.basic_auth(@json_rpc_endpoint.user, @json_rpc_endpoint.password)
50
+ def connection
51
+ Faraday.new(@json_rpc_endpoint).tap do |connection|
52
+ unless @json_rpc_endpoint.user.blank?
53
+ connection.basic_auth(@json_rpc_endpoint.user,
54
+ @json_rpc_endpoint.password)
55
+ end
44
56
  end
45
57
  end
58
+ memoize :connection
46
59
  end
47
60
  end
48
61
  end
@@ -1,5 +1,5 @@
1
1
  module Peatio
2
2
  module Nexbit
3
- VERSION = "0.1.4".freeze
3
+ VERSION = "0.1.5".freeze
4
4
  end
5
5
  end
@@ -1,58 +1,60 @@
1
- module Nexbit
2
- class Wallet < Peatio::Wallet::Abstract
3
-
4
- def initialize(settings = {})
5
- @settings = settings
6
- end
7
-
8
- def configure(settings = {})
9
- # Clean client state during configure.
10
- @client = nil
11
-
12
- @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
13
-
14
- @wallet = @settings.fetch(:wallet) do
15
- raise Peatio::Wallet::MissingSettingError, :wallet
16
- end.slice(:uri, :address)
17
-
18
- @currency = @settings.fetch(:currency) do
19
- raise Peatio::Wallet::MissingSettingError, :currency
20
- end.slice(:id, :base_factor, :options)
21
- end
22
-
23
- def create_address!(_options = {})
24
- { address: client.json_rpc(:getnewaddress) }
25
- rescue Nexbit::Client::Error => e
26
- raise Peatio::Wallet::ClientError, e
27
- end
28
-
29
- def create_transaction!(transaction, options = {})
30
- txid = client.json_rpc(:sendtoaddress,
31
- [
32
- transaction.to_address,
33
- transaction.amount,
34
- '',
35
- '',
36
- options[:subtract_fee].to_s == 'true' # subtract fee from transaction amount.
37
- ])
38
- transaction.hash = txid
39
- transaction
40
- rescue Nexbit::Client::Error => e
41
- raise Peatio::Wallet::ClientError, e
42
- end
43
-
44
- def load_balance!
45
- client.json_rpc(:getbalance).to_d
46
-
47
- rescue Nexbit::Client::Error => e
48
- raise Peatio::Wallet::ClientError, e
49
- end
50
-
51
- private
52
-
53
- def client
54
- uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
55
- @client ||= Client.new(uri, idle_timeout: 1)
1
+ module Peatio
2
+ module Nexbit
3
+ class Wallet < Peatio::Wallet::Abstract
4
+
5
+ def initialize(settings = {})
6
+ @settings = settings
7
+ end
8
+
9
+ def configure(settings = {})
10
+ # Clean client state during configure.
11
+ @client = nil
12
+
13
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
14
+
15
+ @wallet = @settings.fetch(:wallet) do
16
+ raise Peatio::Wallet::MissingSettingError, :wallet
17
+ end.slice(:uri, :address)
18
+
19
+ @currency = @settings.fetch(:currency) do
20
+ raise Peatio::Wallet::MissingSettingError, :currency
21
+ end.slice(:id, :base_factor, :options)
22
+ end
23
+
24
+ def create_address!(_options = {})
25
+ { address: client.json_rpc(:getnewaddress) }
26
+ rescue Nexbit::Client::Error => e
27
+ raise Peatio::Wallet::ClientError, e
28
+ end
29
+
30
+ def create_transaction!(transaction, options = {})
31
+ txid = client.json_rpc(:sendtoaddress,
32
+ [
33
+ transaction.to_address,
34
+ transaction.amount,
35
+ '',
36
+ '',
37
+ options[:subtract_fee].to_s == 'true' # subtract fee from transaction amount.
38
+ ])
39
+ transaction.hash = txid
40
+ transaction
41
+ rescue Nexbit::Client::Error => e
42
+ raise Peatio::Wallet::ClientError, e
43
+ end
44
+
45
+ def load_balance!
46
+ client.json_rpc(:getbalance).to_d
47
+
48
+ rescue Nexbit::Client::Error => e
49
+ raise Peatio::Wallet::ClientError, e
50
+ end
51
+
52
+ private
53
+
54
+ def client
55
+ uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
56
+ @client ||= Client.new(uri)
57
+ end
56
58
  end
57
59
  end
58
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peatio-nexbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - BitBD
@@ -231,7 +231,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  - !ruby/object:Gem::Version
232
232
  version: '0'
233
233
  requirements: []
234
- rubygems_version: 3.0.1
234
+ rubyforge_project:
235
+ rubygems_version: 2.7.6
235
236
  signing_key:
236
237
  specification_version: 4
237
238
  summary: Gem for extending Peatio plugable system with Nexbit implementation.