peatio-nexbit 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/peatio/nexbit/blockchain.rb +55 -52
- data/lib/peatio/nexbit/client.rb +48 -35
- data/lib/peatio/nexbit/version.rb +1 -1
- data/lib/peatio/nexbit/wallet.rb +57 -55
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13ff7fb6158882cd96c628152920a9ce24568d3bf889e6fc1c3942800972c78c
|
4
|
+
data.tar.gz: 8ad637e6d4228a8d60d483ee73dcc116a29b030f06810db5ef232f9b9b13072a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
19
|
-
|
20
|
+
def fetch_block!(block_number)
|
21
|
+
block_hash = client.json_rpc(:getblockhash, [block_number])
|
20
22
|
|
21
|
-
|
22
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
30
|
+
rescue Client::Error => e
|
31
|
+
raise Peatio::Blockchain::ClientError, e
|
32
|
+
end
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
45
|
+
if address_with_balance.blank?
|
46
|
+
raise Peatio::Blockchain::UnavailableAddressBalanceError, address
|
47
|
+
end
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
address_with_balance[1].to_d
|
50
|
+
rescue Client::Error => e
|
51
|
+
raise Peatio::Blockchain::ClientError, e
|
52
|
+
end
|
51
53
|
|
52
|
-
|
54
|
+
private
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
def build_transaction(tx_hash)
|
57
|
+
tx_hash.fetch('vout')
|
58
|
+
.select do |entry|
|
57
59
|
entry.fetch('value').to_d > 0 &&
|
58
|
-
|
60
|
+
entry['scriptPubKey'].has_key?('addresses')
|
59
61
|
end
|
60
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
74
|
+
end
|
73
75
|
|
74
|
-
|
75
|
-
|
76
|
-
|
76
|
+
def client
|
77
|
+
@client ||= Client.new(settings_fetch(:server))
|
78
|
+
end
|
77
79
|
|
78
|
-
|
79
|
-
|
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
|
data/lib/peatio/nexbit/client.rb
CHANGED
@@ -1,48 +1,61 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'memoist'
|
2
|
+
require 'faraday'
|
3
|
+
require 'better-faraday'
|
4
4
|
|
5
|
-
|
5
|
+
module Peatio
|
6
|
+
module Nexbit
|
7
|
+
class Client
|
8
|
+
Error = Class.new(StandardError)
|
9
|
+
class ConnectionError < Error; end
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
22
|
+
extend Memoist
|
14
23
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
24
|
+
def initialize(endpoint)
|
25
|
+
@json_rpc_endpoint = URI.parse(endpoint)
|
26
|
+
end
|
19
27
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
48
|
+
private
|
37
49
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
data/lib/peatio/nexbit/wallet.rb
CHANGED
@@ -1,58 +1,60 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
+
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
|
-
|
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.
|