peatio-nexbit 0.1.2 → 0.1.3
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/peatio/nexbit/blockchain.rb +52 -55
- data/lib/peatio/nexbit/client.rb +35 -48
- data/lib/peatio/nexbit/version.rb +1 -1
- data/lib/peatio/nexbit/wallet.rb +55 -57
- data/peatio-nexbit.gemspec +13 -2
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff46d7b79a5e6df6ebab164f67cdb0f871feec60429524c1a436e8511290fa61
|
4
|
+
data.tar.gz: 90f29909ff30698347514c0d6125cab42e50dbad7b2ae4cba8543bc69baa97b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 437eef825cf2086705eafe6e732bf973c9fc81847ac7790c006feb1af73d7df4f512711ca2d642830da550b429cd6d65906e9a63843bcebbf29f9f4748f47d9e
|
7
|
+
data.tar.gz: 150936d3f69fe8f9cd9e39845a4b4f8154383cca9f311d586a260d65ba0a5e4788d168a88f2e00f0da6255c5bfc747317a30c47f04f4455303e7db4a98e800d9
|
@@ -1,85 +1,82 @@
|
|
1
|
+
module Nexbit
|
2
|
+
# TODO: Processing of unconfirmed transactions from mempool isn't supported now.
|
3
|
+
class Blockchain < Peatio::Blockchain::Abstract
|
1
4
|
|
2
|
-
|
3
|
-
module Nexbit
|
4
|
-
# TODO: Processing of unconfirmed transactions from mempool isn't supported now.
|
5
|
-
class Blockchain < Peatio::Blockchain::Abstract
|
5
|
+
DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@settings = {}
|
12
|
-
end
|
7
|
+
def initialize(custom_features = {})
|
8
|
+
@features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
|
9
|
+
@settings = {}
|
10
|
+
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
def configure(settings = {})
|
13
|
+
# Clean client state during configure.
|
14
|
+
@client = nil
|
15
|
+
@settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
18
|
+
def fetch_block!(block_number)
|
19
|
+
block_hash = client.json_rpc(:getblockhash, [block_number])
|
22
20
|
|
23
|
-
|
24
|
-
|
21
|
+
client.json_rpc(:getblock, [block_hash, 2])
|
22
|
+
.fetch('tx').each_with_object([]) do |tx, txs_array|
|
25
23
|
txs = build_transaction(tx).map do |ntx|
|
26
24
|
Peatio::Transaction.new(ntx.merge(block_number: block_number))
|
27
25
|
end
|
28
26
|
txs_array.append(*txs)
|
29
27
|
end.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) }
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
rescue Nexbit::Client::Error => e
|
29
|
+
raise Peatio::Blockchain::ClientError, e
|
30
|
+
end
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
def latest_block_number
|
33
|
+
client.json_rpc(:getblockcount)
|
34
|
+
rescue Nexbit::Client::Error => e
|
35
|
+
raise Peatio::Blockchain::ClientError, e
|
36
|
+
end
|
39
37
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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 }
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
address_with_balance[1].to_d
|
50
|
-
rescue Client::Error => e
|
51
|
-
raise Peatio::Blockchain::ClientError, e
|
43
|
+
if address_with_balance.blank?
|
44
|
+
raise Peatio::Blockchain::UnavailableAddressBalanceError, address
|
52
45
|
end
|
53
46
|
|
54
|
-
|
47
|
+
address_with_balance[1].to_d
|
48
|
+
rescue Nexbit::Client::Error => e
|
49
|
+
raise Peatio::Blockchain::ClientError, e
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
def build_transaction(tx_hash)
|
55
|
+
tx_hash.fetch('vout')
|
56
|
+
.select do |entry|
|
59
57
|
entry.fetch('value').to_d > 0 &&
|
60
|
-
|
58
|
+
entry['scriptPubKey'].has_key?('addresses')
|
61
59
|
end
|
62
|
-
|
60
|
+
.each_with_object([]) do |entry, formatted_txs|
|
63
61
|
no_currency_tx =
|
64
62
|
{ hash: tx_hash['txid'], txout: entry['n'],
|
65
63
|
to_address: entry['scriptPubKey']['addresses'][0],
|
66
64
|
amount: entry.fetch('value').to_d,
|
67
65
|
status: 'success' }
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
73
71
|
end
|
74
|
-
|
72
|
+
end
|
75
73
|
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
def client
|
75
|
+
@client ||= Nexbit::Client.new(settings_fetch(:server))
|
76
|
+
end
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
end
|
78
|
+
def settings_fetch(key)
|
79
|
+
@settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
|
83
80
|
end
|
84
81
|
end
|
85
82
|
end
|
data/lib/peatio/nexbit/client.rb
CHANGED
@@ -1,61 +1,48 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Nexbit
|
2
|
+
class Client
|
3
|
+
Error = Class.new(StandardError)
|
4
4
|
|
5
|
-
|
6
|
-
module Nexbit
|
7
|
-
class Client
|
8
|
-
Error = Class.new(StandardError)
|
9
|
-
class ConnectionError < Error; end
|
5
|
+
class ConnectionError < Error; end
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@msg = msg
|
15
|
-
end
|
16
|
-
|
17
|
-
def message
|
18
|
-
"#{@msg} (#{@code})"
|
19
|
-
end
|
7
|
+
class ResponseError < Error
|
8
|
+
def initialize(code, msg)
|
9
|
+
super "#{msg} (#{code})"
|
20
10
|
end
|
11
|
+
end
|
21
12
|
|
22
|
-
|
13
|
+
extend Memoist
|
23
14
|
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
def initialize(endpoint, idle_timeout: 5)
|
16
|
+
@json_rpc_endpoint = URI.parse(endpoint)
|
17
|
+
@idle_timeout = idle_timeout
|
18
|
+
end
|
27
19
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
else
|
44
|
-
raise Error, e
|
45
|
-
end
|
46
|
-
end
|
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
|
47
35
|
|
48
|
-
|
36
|
+
private
|
49
37
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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)
|
56
44
|
end
|
57
45
|
end
|
58
|
-
memoize :connection
|
59
46
|
end
|
60
47
|
end
|
61
48
|
end
|
data/lib/peatio/nexbit/wallet.rb
CHANGED
@@ -1,60 +1,58 @@
|
|
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
|
-
|
56
|
-
@client ||= Client.new(uri)
|
57
|
-
end
|
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)
|
58
56
|
end
|
59
57
|
end
|
60
58
|
end
|
data/peatio-nexbit.gemspec
CHANGED
@@ -16,6 +16,17 @@ Gem::Specification.new do |spec|
|
|
16
16
|
|
17
17
|
# Specify which files should be added to the gem when it is released.
|
18
18
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/ndexnetwork/peatio-ndex"
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/ndexnetwork/peatio-ndex/blob/master/CHANGELOG.md"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
30
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
31
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
32
|
end
|
@@ -27,9 +38,9 @@ Gem::Specification.new do |spec|
|
|
27
38
|
spec.add_dependency "better-faraday", "~> 1.0.5"
|
28
39
|
spec.add_dependency "faraday", "~> 0.15.4"
|
29
40
|
spec.add_dependency "memoist", "~> 0.16.0"
|
30
|
-
spec.add_dependency "peatio", "~> 0.6.
|
41
|
+
spec.add_dependency "peatio", "~> 0.6.3"
|
31
42
|
|
32
|
-
spec.add_development_dependency "bundler", "~> 1.
|
43
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
33
44
|
spec.add_development_dependency "mocha", "~> 1.8"
|
34
45
|
spec.add_development_dependency "pry-byebug"
|
35
46
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BitBD
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03
|
11
|
+
date: 2020-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -72,28 +72,28 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.6.
|
75
|
+
version: 0.6.3
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.6.
|
82
|
+
version: 0.6.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: bundler
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '1.
|
89
|
+
version: '1.17'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '1.
|
96
|
+
version: '1.17'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: mocha
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -198,7 +198,10 @@ files:
|
|
198
198
|
homepage: https://nexbit.io/
|
199
199
|
licenses:
|
200
200
|
- MIT
|
201
|
-
metadata:
|
201
|
+
metadata:
|
202
|
+
homepage_uri: https://nexbit.io/
|
203
|
+
source_code_uri: https://github.com/ndexnetwork/peatio-ndex
|
204
|
+
changelog_uri: https://github.com/ndexnetwork/peatio-ndex/blob/master/CHANGELOG.md
|
202
205
|
post_install_message:
|
203
206
|
rdoc_options: []
|
204
207
|
require_paths:
|