peatio-coinpaymentnew 0.0.7 → 1.0.1

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
  SHA1:
3
- metadata.gz: 15c446d380360e20a0edee5afd62d359d981be51
4
- data.tar.gz: ad92f9e4f7d7f84225116446f20f7b44e1d82b2f
3
+ metadata.gz: 9c6e56f704eacf68895dbfa46288561887452f72
4
+ data.tar.gz: db1c498a5cad91159146be6495df805b9c1692ea
5
5
  SHA512:
6
- metadata.gz: b63546d3bb9293e2371fc065797cab9140c692bf4709362f0da6ac52d998236cb851257a775a748935c3c22aafae6fb2991b0fb6ca4ccd194a31f6f628b72dd9
7
- data.tar.gz: 94739faea1e93d666e77e334771602785dac29d9bbb91a7a446a20dbe42f547532492a68b0f88cbefe751861178381451c9aa8155e25956b9fe96749f97c479c
6
+ metadata.gz: 3e14195d4e43eeaaeb3f8b1c6787b88b8531770c5afac92e2a4ec186754bac466fc13968c373ce16b07678913b30c0512ae7fc2c1f95fd93b408dc08fdde3ba5
7
+ data.tar.gz: 5d30ac68bbce4a3e977d1cb0cc8381bc7624089edbbe51077b69265126defbbf15b101b618cc2cae757ec2abf4383c0ba19e0d22b22f6eaaa33b41208d10eba1
@@ -0,0 +1,10 @@
1
+ - key: ltc-testnet
2
+ name: Coinpayment Testnet
3
+ client: coinpayment # API client name.
4
+ server: http://user:password@127.0.0.1:18332 # Public Coinpayment node endpoint.
5
+ height: 1087160 # Initial block number from which sync will be started.
6
+ min_confirmations: 6 # Minimal confirmations needed for withdraw and deposit confirmation.
7
+ explorer:
8
+ address: https://chain.so/address/LTCTEST/#{address}
9
+ transaction: https://chain.so/tx/LTCTEST/#{txid}
10
+ status: active
@@ -0,0 +1,18 @@
1
+ - id: ltc
2
+ name: Coinpayment
3
+ blockchain_key: ltc-testnet
4
+ symbol: 'Ł'
5
+ type: coin
6
+ precision: 8
7
+ base_factor: 100_000_000
8
+ enabled: true
9
+ # Deposits with less amount are skipped during blockchain synchronization.
10
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
11
+ min_deposit_amount: 0.0004488
12
+ min_collection_amount: 0.0004488
13
+ withdraw_limit_24h: 0.5
14
+ withdraw_limit_72h: 1.2
15
+ deposit_fee: 0
16
+ withdraw_fee: 0
17
+ position: 5
18
+ options: {}
@@ -0,0 +1,6 @@
1
+ Coinpayments.configure do |config|
2
+ config.merchant_id = '81584a734276162e589fb2a5c19d1114'
3
+ config.public_api_key = '1c68e7c5cd7f03d79325bc63e2985d3dc604847242a22d682673f531af4cadcb'
4
+ config.private_api_key = '13F4855BFDa330B43063F07961CA373CfF3bb141d8ccCdF83B5e92ac2ad0f78b'
5
+ config.secret_phrase = '@hari123'
6
+ end
@@ -0,0 +1,44 @@
1
+ - name: Coinpayment Deposit Wallet
2
+ blockchain_key: ltc-testnet
3
+ currency_id: ltc
4
+ # Address where deposits will be collected to.
5
+ address: 'Qc2BM7gp8mKgJPPxLAadLAHteNQwhFwwuf' # IMPORTANT: Always wrap this value in quotes!
6
+ kind: deposit # Wallet kind (deposit, hot, warm, cold or fee).
7
+ nsig: 1 # Number of signatures required for performing withdraw.
8
+ max_balance: 0.0
9
+ status: active
10
+ gateway: coinpaymentd # Gateway client name.
11
+ settings:
12
+ #
13
+ # Coinpaymentd gateway client settings.
14
+ uri: http://user:password@127.0.0.1:19332
15
+
16
+ - name: Coinpayment Hot Wallet
17
+ blockchain_key: ltc-testnet
18
+ currency_id: ltc
19
+ # Address where deposits will be collected to.
20
+ address: 'Qc2BM7gp8mKgJPPxLAadLAHteNQwhFwwuf' # IMPORTANT: Always wrap this value in quotes!
21
+ kind: hot # Wallet kind (deposit, hot, warm, cold or fee).
22
+ nsig: 1 # Number of signatures required for performing withdraw.
23
+ max_balance: 5.0
24
+ status: active
25
+ gateway: coinpaymentd # Gateway client name.
26
+ settings:
27
+ #
28
+ # Coinpaymentd gateway client settings.
29
+ uri: http://user:password@127.0.0.1:19332
30
+
31
+ - name: Coinpayment Warm Wallet
32
+ blockchain_key: ltc-testnet
33
+ currency_id: ltc
34
+ # Address where deposits will be collected to.
35
+ address: 'Qc2BM7gp8mKgJPPxLAadLAHteNQwhFwwuf' # IMPORTANT: Always wrap this value in quotes!
36
+ kind: warm # Wallet kind (deposit, hot, warm, cold or fee).
37
+ nsig: 1 # Number of signatures required for performing withdraw.
38
+ max_balance: 50.0
39
+ status: active
40
+ gateway: coinpaymentd # Gateway client name.
41
+ settings:
42
+ #
43
+ # Coinpaymentd gateway client settings.
44
+ uri: http://user:password@127.0.0.1:19332
@@ -1,21 +1,85 @@
1
-
2
- module Peatio
3
- module Coinpaymentnew
4
- # TODO: Processing of unconfirmed transactions from mempool isn't supported now.
5
- class Blockchain < Peatio::Blockchain::Abstract
6
-
7
- DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
8
-
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
19
- end
20
- end
21
- end
1
+
2
+ module Peatio
3
+ module Coinpaymentnew
4
+ # TODO: Processing of unconfirmed transactions from mempool isn't supported now.
5
+ class Blockchain < Peatio::Blockchain::Abstract
6
+
7
+ DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
8
+
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
19
+
20
+ def fetch_block!(block_number)
21
+ block_hash = client.json_rpc(:getblockhash, [block_number])
22
+
23
+ client.json_rpc(:getblock, [block_hash, 2])
24
+ .fetch('tx').each_with_object([]) do |tx, txs_array|
25
+ txs = build_transaction(tx).map do |ntx|
26
+ Peatio::Transaction.new(ntx.merge(block_number: block_number))
27
+ end
28
+ txs_array.append(*txs)
29
+ end.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) }
30
+ rescue Client::Error => e
31
+ raise Peatio::Blockchain::ClientError, e
32
+ end
33
+
34
+ def latest_block_number
35
+ client.json_rpc(:getblockcount)
36
+ rescue Client::Error => e
37
+ raise Peatio::Blockchain::ClientError, e
38
+ end
39
+
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 }
44
+
45
+ if address_with_balance.blank?
46
+ raise Peatio::Blockchain::UnavailableAddressBalanceError, address
47
+ end
48
+
49
+ address_with_balance[1].to_d
50
+ rescue Client::Error => e
51
+ raise Peatio::Blockchain::ClientError, e
52
+ end
53
+
54
+ private
55
+
56
+ def build_transaction(tx_hash)
57
+ tx_hash.fetch('vout')
58
+ .select do |entry|
59
+ entry.fetch('value').to_d > 0 &&
60
+ entry['scriptPubKey'].has_key?('addresses')
61
+ end
62
+ .each_with_object([]) do |entry, formatted_txs|
63
+ no_currency_tx =
64
+ { hash: tx_hash['txid'], txout: entry['n'],
65
+ to_address: entry['scriptPubKey']['addresses'][0],
66
+ amount: entry.fetch('value').to_d,
67
+ status: 'success' }
68
+
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
73
+ end
74
+ end
75
+
76
+ def client
77
+ @client ||= Client.new(settings_fetch(:server))
78
+ end
79
+
80
+ def settings_fetch(key)
81
+ @settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,67 +1,60 @@
1
- # frozen_string_literal: true
2
-
3
- module Peatio
4
- module Coinpaymentnew
5
- class Client
6
- Error = Class.new(StandardError)
7
- ConnectionError = Class.new(Error)
8
-
9
- class ResponseError < Error
10
- def initialize(msg)
11
- super "#{msg}"
12
- end
13
- end
14
-
15
- def initialize(endpoint, access_token, access_token2)
16
- @endpoint = URI.parse(endpoint)
17
- @access_token = access_token
18
- @access_token2 = access_token2
19
- end
20
- def self.hmac(body)
21
- OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha512'), @access_token2, HTTParty::HashConversions.to_params(body))
22
- end
23
-
24
- def self.required_params
25
- { version: 1, key: @access_token }
26
- end
27
-
28
- # def rest_api(verb, path, data = nil)
29
- def rest_api(args)
30
- # args = [@endpoint.to_s + path]
31
-
32
- # if data
33
- # if %i[post put patch].include?(verb)
34
- # args << data.compact.to_json
35
- # args << { 'Content-Type' => 'application/json' }
36
- # else
37
- # args << data.compact
38
- # args << {}
39
- # end
40
- # else
41
- # args << nil
42
- # args << {}
43
- # end
44
- body = required_params.merge!(args)
45
- # args.last['Accept'] = 'application/json'
46
- # args.last['hmac'] = hmac(body)
47
-
48
- # body = required_params.merge!(cmd: caller[0][/`.*'/][1..-2]).merge!(args)
49
- response = HTTParty.post(@endpoint.to_s, body: body, headers: {'hmac' => hmac(body)})
50
- response['error'] == 'ok' ? Hashie::Mash.new(response['result']) : response['error']
51
-
52
- # response = Faraday.send(verb, *args)
53
- # response.assert_success!
54
- # response = JSON.parse(response.body)
55
- # response['error'].tap { |error| raise ResponseError.new(error) if error }
56
- # response
57
- rescue Faraday::Error => e
58
- if e.is_a?(Faraday::ConnectionFailed) || e.is_a?(Faraday::TimeoutError)
59
- raise ConnectionError, e
60
- else
61
- raise ConnectionError, JSON.parse(e.response.body)['message']
62
- end
63
- end
64
- end
65
- end
66
- end
67
-
1
+ # frozen_string_literal: true
2
+
3
+ require 'memoist'
4
+ require 'faraday'
5
+ require 'better-faraday'
6
+
7
+ module Peatio
8
+ module Coinpaymentnew
9
+ class Client
10
+ Error = Class.new(StandardError)
11
+ ConnectionError = Class.new(Error)
12
+
13
+ class ResponseError < Error
14
+ def initialize(code, msg)
15
+ @code = code
16
+ @msg = msg
17
+ end
18
+
19
+ def message
20
+ "#{@msg} (#{@code})"
21
+ end
22
+ end
23
+
24
+ extend Memoist
25
+
26
+ def initialize(endpoint)
27
+ @json_rpc_endpoint = URI.parse(endpoint)
28
+ end
29
+
30
+ def json_rpc(method, params = [])
31
+ response = connection.post \
32
+ '/',
33
+ { jsonrpc: '1.0', method: method, params: params }.to_json,
34
+ { 'Accept' => 'application/json',
35
+ 'Content-Type' => 'application/json' }
36
+ response.assert_2xx!
37
+ response = JSON.parse(response.body)
38
+ response['error'].tap do |e|
39
+ raise ResponseError.new(e['code'], e['message']) if e
40
+ end
41
+ response.fetch('result')
42
+ rescue Faraday::Error => e
43
+ raise ConnectionError, e
44
+ end
45
+
46
+ private
47
+
48
+ def connection
49
+ @connection ||= Faraday.new(@json_rpc_endpoint) do |f|
50
+ f.adapter :net_http_persistent, pool_size: 5
51
+ end.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
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,42 +1,42 @@
1
- module Peatio
2
- module Coinpaymentnew
3
- module Hooks
4
- BLOCKCHAIN_VERSION_REQUIREMENT = "~> 1.0.0"
5
- WALLET_VERSION_REQUIREMENT = "~> 1.0.0"
6
-
7
- class << self
8
- def check_compatibility
9
- unless Gem::Requirement.new(BLOCKCHAIN_VERSION_REQUIREMENT)
10
- .satisfied_by?(Gem::Version.new(Peatio::Blockchain::VERSION))
11
- [
12
- "Coinpaymentnew blockchain version requirement was not satisfied by Peatio::Blockchain.",
13
- "Coinpaymentnew blockchain requires #{BLOCKCHAIN_VERSION_REQUIREMENT}.",
14
- "Peatio::Blockchain version is #{Peatio::Blockchain::VERSION}"
15
- ].join('\n').tap { |s| Kernel.abort s }
16
- end
17
-
18
- unless Gem::Requirement.new(WALLET_VERSION_REQUIREMENT)
19
- .satisfied_by?(Gem::Version.new(Peatio::Wallet::VERSION))
20
- [
21
- "Coinpaymentnew wallet version requirement was not satisfied by Peatio::Wallet.",
22
- "Coinpaymentnew wallet requires #{WALLET_VERSION_REQUIREMENT}.",
23
- "Peatio::Wallet version is #{Peatio::Wallet::VERSION}"
24
- ].join('\n').tap { |s| Kernel.abort s }
25
- end
26
- end
27
-
28
- def register
29
- Peatio::Blockchain.registry[:coinpaymentnew] = Coinpaymentnew::Blockchain
30
- Peatio::Wallet.registry[:coinpaymentnew] = Coinpaymentnew::Wallet
31
- end
32
- end
33
-
34
- if defined?(Rails::Railtie)
35
- require "peatio/coinpaymentnew/railtie"
36
- else
37
- check_compatibility
38
- register
39
- end
40
- end
41
- end
42
- end
1
+ module Peatio
2
+ module Coinpaymentnew
3
+ module Hooks
4
+ BLOCKCHAIN_VERSION_REQUIREMENT = "~> 1.0.0"
5
+ WALLET_VERSION_REQUIREMENT = "~> 1.0.0"
6
+
7
+ class << self
8
+ def check_compatibility
9
+ unless Gem::Requirement.new(BLOCKCHAIN_VERSION_REQUIREMENT)
10
+ .satisfied_by?(Gem::Version.new(Peatio::Blockchain::VERSION))
11
+ [
12
+ "Coinpayment blockchain version requiremnt was not suttisfied by Peatio::Blockchain.",
13
+ "Coinpayment blockchain requires #{BLOCKCHAIN_VERSION_REQUIREMENT}.",
14
+ "Peatio::Blockchain version is #{Peatio::Blockchain::VERSION}"
15
+ ].join('\n').tap { |s| Kernel.abort s }
16
+ end
17
+
18
+ unless Gem::Requirement.new(WALLET_VERSION_REQUIREMENT)
19
+ .satisfied_by?(Gem::Version.new(Peatio::Wallet::VERSION))
20
+ [
21
+ "Coinpayment wallet version requiremnt was not suttisfied by Peatio::Wallet.",
22
+ "Coinpayment wallet requires #{WALLET_VERSION_REQUIREMENT}.",
23
+ "Peatio::Wallet version is #{Peatio::Wallet::VERSION}"
24
+ ].join('\n').tap { |s| Kernel.abort s }
25
+ end
26
+ end
27
+
28
+ def register
29
+ Peatio::Blockchain.registry[:coinpayment] = Coinpayment::Blockchain
30
+ Peatio::Wallet.registry[:coinpaymentd] = Coinpayment::Wallet
31
+ end
32
+ end
33
+
34
+ if defined?(Rails::Railtie)
35
+ require "peatio/coinpayment/railtie"
36
+ else
37
+ check_compatibility
38
+ register
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,13 +1,13 @@
1
- module Peatio
2
- module Coinpaymentnew
3
- class Railtie < Rails::Railtie
4
- config.before_initialize do
5
- Hooks.check_compatibility
6
- end
7
-
8
- config.after_initialize do
9
- Hooks.register
10
- end
11
- end
12
- end
13
- end
1
+ module Peatio
2
+ module Coinpaymentnew
3
+ class Railtie < Rails::Railtie
4
+ config.before_initialize do
5
+ Hooks.check_compatibility
6
+ end
7
+
8
+ config.after_initialize do
9
+ Hooks.register
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,7 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Peatio
4
2
  module Coinpaymentnew
5
- VERSION = "0.0.7"
3
+ VERSION = "1.0.1".freeze
6
4
  end
7
5
  end
@@ -1,353 +1,69 @@
1
- module Peatio
2
- module Coinpaymentnew
3
- class Wallet < Peatio::Wallet::Abstract
4
- TIME_DIFFERENCE_IN_MINUTES = 10
5
- XLM_MEMO_TYPES = { 'memoId': 'id', 'memoText': 'text', 'memoHash': 'hash', 'memoReturn': 'return' }
6
-
7
- DEFAULT_FEATURES = { skip_deposit_collection: false }.freeze
8
-
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
-
18
- @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
19
-
20
- @wallet = @settings.fetch(:wallet) do
21
- raise Peatio::Wallet::MissingSettingError, :wallet
22
- end.slice(:uri, :address, :secret, :access_token, :access_token2, :wallet_id, :testnet)
23
-
24
- @currency = @settings.fetch(:currency) do
25
- raise Peatio::Wallet::MissingSettingError, :currency
26
- end.slice(:id, :base_factor, :code, :options)
27
- end
28
-
29
- def create_address!(options = {})
30
- currency = erc20_currency_id
31
- options.deep_symbolize_keys!
32
-
33
- if options.dig(:pa_details, :address_id).present? &&
34
- options.dig(:pa_details, :updated_at).present? &&
35
- time_difference_in_minutes(options.dig(:pa_details, :updated_at)) >= TIME_DIFFERENCE_IN_MINUTES
36
- args = { cmd: 'get_deposit_address', currency: currency.code }.merge!(options)
37
- response = client.rest_api(args)
38
- { address: response['address'], secret: coinpayment_wallet_passphrase }
39
- elsif options.dig(:pa_details, :address_id).blank?
40
- args = { cmd: 'get_deposit_address', currency: currency.code }.merge!(options)
41
- response = client.rest_api(args)
42
- # response = client.rest_api(:post, "#{currency}/wallet/#{wallet_id}/address")
43
- { address: response['address'], secret: coinpayment_wallet_passphrase, details: { address_id: response['address'] }}
44
- end
45
- rescue Coinpayment::Client::Error => e
46
- raise Peatio::Wallet::ClientError, e
47
- end
48
-
49
- def create_transaction!(transaction, options = {})
50
- currency_options = @currency.fetch(:options).slice(:gas_limit, :gas_price, :erc20_contract_address)
51
-
52
- # if currency_options[:gas_limit].present? && currency_options[:gas_price].present?
53
- # options.merge!(currency_options)
54
- # create_eth_transaction(transaction, options)
55
- # else
56
- amount = convert_to_base_unit(transaction.amount)
57
-
58
- # if options[:subtract_fee].to_s == 'true'
59
- # fee = build_raw_transaction(transaction)
60
- # baseFeeInfo = fee['tx_fee']
61
- # # fee = baseFeeInfo.present? ? baseFeeInfo : fee.dig('txInfo','Fee')
62
- # amount -= fee.to_i
63
- # end
64
-
65
- args = { cmd: "create_transaction", amount: amount, currency1: "USD", currency2: currency.code, buyer_email: "joshirockstar007@gmail.com", address: normalize_address(transaction.to_address.to_s) }
66
- response = client.rest_api(args)
67
- # response = client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/sendcoins", {
68
- # address: normalize_address(transaction.to_address.to_s),
69
- # amount: amount.to_s,
70
- # walletPassphrase: coinpayment_wallet_passphrase,
71
- # memo: xlm_memo(transaction.to_address.to_s)
72
- # }.compact)
73
-
74
- if response['feeString'].present?
75
- fee = convert_from_base_unit(response['feeString'])
76
- transaction.fee = fee
77
- end
78
-
79
- transaction.hash = normalize_txid(response['txn_id'])
80
- transaction.fee_currency_id = erc20_currency_id
81
- transaction
82
- # end
83
- rescue Coinpayment::Client::Error => e
84
- raise Peatio::Wallet::ClientError, e
85
- end
86
-
87
-
88
- def build_raw_transaction(transaction)
89
- args = { cmd: "rates" }
90
- response = client.rest_api(args)
91
- response[currency.code]
92
- # client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/tx/build", {
93
- # recipients: [{
94
- # address: transaction.to_address,
95
- # amount: convert_to_base_unit(transaction.amount).to_s
96
- # }]
97
- # }.compact)
98
- end
99
-
100
- # def create_eth_transaction(transaction, options = {})
101
- # amount = convert_to_base_unit(transaction.amount)
102
- # hop = true unless options.slice(:gas_price).present?
103
-
104
- # fee_estimate = fee_estimate(amount.to_s, hop)
105
-
106
- # if transaction.options.present? && transaction.options[:gas_price].present?
107
- # options[:gas_price] = transaction.options[:gas_price]
108
- # else
109
- # options[:gas_price] = fee_estimate['minGasPrice'].to_i
110
- # end
111
-
112
- # response = client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/sendcoins", {
113
- # address: transaction.to_address.to_s,
114
- # amount: amount.to_s,
115
- # walletPassphrase: coinpayment_wallet_passphrase,
116
- # gas: options.fetch(:gas_limit).to_i,
117
- # gasPrice: options.fetch(:gas_price).to_i,
118
- # hop: hop
119
- # }.compact)
120
-
121
- # if response['feeString'].present?
122
- # fee = convert_from_base_unit(response['feeString'])
123
- # transaction.fee = fee
124
- # end
125
-
126
- # transaction.hash = normalize_txid(response['txid'])
127
- # transaction.fee_currency_id = erc20_currency_id
128
- # transaction.options = options
129
- # transaction
130
- # end
131
-
132
- # def fee_estimate(amount, hop)
133
- # client.rest_api(:get, "#{erc20_currency_id}/tx/fee", { amount: amount, hop: hop }.compact)
134
- # end
135
-
136
- def load_balance!
137
- args = { cmd: "balances" }
138
- response = client.rest_api(args)
139
- response[currency.code]['balancef']
140
- # if @currency.fetch(:options).slice(:erc20_contract_address).present?
141
- # load_erc20_balance!
142
- # else
143
- # response = client.rest_api(:get, "#{currency_id}/wallet/#{wallet_id}")
144
- # convert_from_base_unit(response.fetch('balanceString'))
145
- # end
146
- rescue Coinpayment::Client::Error => e
147
- raise Peatio::Wallet::ClientError, e
148
- end
149
-
150
- # def load_erc20_balance!
151
- # response = client.rest_api(:get, "#{erc20_currency_id}/wallet/#{wallet_id}?allTokens=true")
152
- # convert_from_base_unit(response.dig('tokens', currency_id, 'balanceString'))
153
- # rescue Coinpayment::Client::Error => e
154
- # raise Peatio::Wallet::ClientError, e
155
- # end
156
-
157
- def trigger_webhook_event(request)
158
- currency = @wallet.fetch(:testnet).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id)
159
- if request.params['ipn_type'] == 'transfer'
160
- return unless currency == request.params['coin'] &&
161
- @wallet.fetch(:wallet_id) == request.params['wallet']
162
- else
163
- return unless @wallet.fetch(:wallet_id) == request.params['walletId']
164
- end
165
-
166
- if request.params['ipn_type'] == 'transfer'
167
- transactions = fetch_transfer!(request.params['txn_id'])
168
- return transactions
169
- # elsif request.params['type'] == 'address_confirmation'
170
- # address_id = fetch_address_id(request.params['address'])
171
- # return { address_id: address_id, address: request.params['address'], currency_id: currency_id }
172
- end
173
- end
174
-
175
- def register_webhooks!(url)
176
- transfer_webhook(url)
177
- address_confirmation_webhook(url)
178
- end
179
-
180
- # def fetch_address_id(address)
181
- # currency = erc20_currency_id
182
- # client.rest_api(:get, "#{currency}/wallet/#{wallet_id}/address/#{address}")
183
- # .fetch('id')
184
- # rescue Coinpayment::Client::Error => e
185
- # raise Peatio::Wallet::ClientError, e
186
- # end
187
-
188
- def fetch_transfer!(id)
189
- args = { cmd: "get_tx_info", txid: id }
190
- response = client.rest_api(args)
191
- # response[currency.code]['balancef']
192
- # response = client.rest_api(:get, "#{currency_id}/wallet/#{wallet_id}/transfer/#{id}")
193
- # parse_entries(response['entries']).map do |entry|
194
- # to_address = if response.dig('coinSpecific', 'memo').present?
195
- # memo = response.dig('coinSpecific', 'memo')
196
- # memo_type = memo.kind_of?(Array) ? memo.first : memo
197
- # build_address(entry['address'], memo_type)
198
- # else
199
- # entry['address']
200
- # end
201
- state = define_transaction_state(response['status'])
202
-
203
- # if response['outputs'].present?
204
- # output = response['outputs'].find { |out| out['address'] == to_address }
205
- # txout = output['index'] if output.present?
206
- # end
207
-
208
- # if response['feeString'].present?
209
- # fee = convert_from_base_unit(response['feeString']) / response['entries'].count
210
- # end
211
-
212
- transaction = Peatio::Transaction.new(
213
- currency_id: @currency.fetch(:id),
214
- amount: convert_from_base_unit(response['amountf']),
215
- fee: 0,
216
- fee_currency_id: response["coin"],
217
- hash: normalize_txid(response['txid']),
218
- to_address: response["payment_address"],
219
- block_number: 0,
220
- txout: 0,
221
- status: state
222
- )
223
-
224
- transaction if transaction.valid?
225
- # end.compact
226
- rescue Coinpayment::Client::Error => e
227
- raise Peatio::Wallet::ClientError, e
228
- end
229
-
230
- def transfer_webhook(url)
231
- client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", {
232
- type: 'transfer',
233
- allToken: true,
234
- url: url,
235
- label: "webhook for #{url}",
236
- listenToFailureStates: false
237
- })
238
- end
239
-
240
- def address_confirmation_webhook(url)
241
- client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", {
242
- type: 'address_confirmation',
243
- allToken: true,
244
- url: url,
245
- label: "webhook for #{url}",
246
- listenToFailureStates: false
247
- })
248
- end
249
-
250
- def parse_entries(entries)
251
- entries.map do |e|
252
- e if e["valueString"].to_i.positive?
253
- end.compact
254
- end
255
-
256
- def client
257
- uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
258
- access_token = @wallet.fetch(:access_token) { raise Peatio::Wallet::MissingSettingError, :access_token }
259
- access_token2 = @wallet.fetch(:access_token2) { raise Peatio::Wallet::MissingSettingError, :access_token2 }
260
-
261
- currency_code_prefix = @wallet.fetch(:testnet) ? 't' : ''
262
- uri = uri.gsub(/\/+\z/, '') + '/' + currency_code_prefix
263
- @client ||= Client.new(uri, access_token, access_token2)
264
- end
265
-
266
- def build_address(address, memo)
267
- "#{address}?memoId=#{memo['value']}"
268
- end
269
-
270
- # All these functions will have to be done with the coin set to eth or teth
271
- # since that is the actual coin type being used.
272
- def erc20_currency_id
273
- return 'eth' if @currency.fetch(:options).slice(:erc20_contract_address).present?
274
-
275
- currency_id
276
- end
277
-
278
- def xlm_memo(address)
279
- build_xlm_memo(address) if @currency.fetch(:id) == 'xlm'
280
- end
281
-
282
- def build_xlm_memo(address)
283
- case address.split('?').last.split('=').first
284
- when 'memoId'
285
- memo_value_from(address, 'memoId')
286
- when 'memoText'
287
- memo_value_from(address, 'memoText')
288
- when 'memoHash'
289
- memo_value_from(address, 'memoHash')
290
- when 'memoReturn'
291
- memo_value_from(address, 'memoReturn')
292
- end
293
- end
294
-
295
- def memo_value_from(address, type)
296
- memo_value = address.partition(type + '=').last
297
- return { type: XLM_MEMO_TYPES[type.to_sym], value: memo_value } if memo_value.present?
298
- end
299
-
300
- def currency_id
301
- @currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
302
- end
303
-
304
- def coinpayment_wallet_passphrase
305
- @wallet.fetch(:secret)
306
- end
307
-
308
- def wallet_id
309
- @wallet.fetch(:wallet_id)
310
- end
311
-
312
- def normalize_address(address)
313
- if @currency.fetch(:id) == 'xlm'
314
- address.split('?').first
315
- else
316
- address
317
- end
318
- end
319
-
320
- def normalize_txid(txid)
321
- txid.downcase
322
- end
323
-
324
- def convert_from_base_unit(value)
325
- value.to_d / @currency.fetch(:base_factor)
326
- end
327
-
328
- def convert_to_base_unit(value)
329
- x = value.to_d * @currency.fetch(:base_factor)
330
- unless (x % 1).zero?
331
- raise Peatio::WalletClient::Error,
332
- "Failed to convert value to base (smallest) unit because it exceeds the maximum precision: " \
333
- "#{value.to_d} - #{x.to_d} must be equal to zero."
334
- end
335
- x.to_i
336
- end
337
-
338
- def time_difference_in_minutes(updated_at)
339
- (Time.now - updated_at)/60
340
- end
341
-
342
- def define_transaction_state(state)
343
- if state >= 100
344
- "success"
345
- elsif state >= 0 and state <= 99
346
- 'pending'
347
- else
348
- 'failed'
349
- end
350
- end
351
- end
352
- end
353
- end
1
+ module Peatio
2
+ module Coinpaymentnew
3
+ class Wallet < Peatio::Wallet::Abstract
4
+
5
+ DEFAULT_FEATURES = { skip_deposit_collection: false }.freeze
6
+
7
+ def initialize(custom_features = {})
8
+ @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
9
+ @settings = {}
10
+ end
11
+
12
+ def configure(settings = {})
13
+ # Clean client state during configure.
14
+ @client = nil
15
+
16
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
17
+
18
+ @wallet = @settings.fetch(:wallet) do
19
+ raise Peatio::Wallet::MissingSettingError, :wallet
20
+ end.slice(:uri, :address)
21
+
22
+ @currency = @settings.fetch(:currency) do
23
+ raise Peatio::Wallet::MissingSettingError, :currency
24
+ end.slice(:id, :base_factor, :options)
25
+ end
26
+
27
+ def create_address!(_options = {})
28
+ r = Coinpayments.get_callback_address('BTC', {ipn_url: "https://v2app.rokes.exchange/api/v2/peatio/public/webhooks/deposit"})
29
+ { address: r[:address] }
30
+ rescue Coinpaymentnew::Client::Error => e
31
+ raise Peatio::Wallet::ClientError, e
32
+ end
33
+
34
+ def create_transaction!(transaction, options = {})
35
+ res = Coinpayments.create_transaction(transaction.amount, "USD", currency_id, {buyer_email: "joshirockstar007@gmail.com", address: transaction.to_address, ipn_url: "https://v2app.rokes.exchange/api/v2/peatio/public/webhooks/deposit"})
36
+ # txid = client.json_rpc(:sendtoaddress,
37
+ # [
38
+ # transaction.to_address,
39
+ # transaction.amount,
40
+ # '',
41
+ # '',
42
+ # options[:subtract_fee].to_s == 'true' # subtract fee from transaction amount.
43
+ # ])
44
+ transaction.hash = res[:txn_id]
45
+ transaction
46
+ rescue Coinpaymentnew::Client::Error => e
47
+ raise Peatio::Wallet::ClientError, e
48
+ end
49
+
50
+ def load_balance!
51
+ client.json_rpc(:getbalance).to_d
52
+
53
+ rescue Coinpaymentnew::Client::Error => e
54
+ raise Peatio::Wallet::ClientError, e
55
+ end
56
+
57
+ private
58
+
59
+ def client
60
+ uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
61
+ @client ||= Client.new(uri)
62
+ end
63
+
64
+ def currency_id
65
+ @currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
66
+ end
67
+ end
68
+ end
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peatio-coinpaymentnew
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harishankar Joshi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-27 00:00:00.000000000 Z
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -205,6 +205,10 @@ files:
205
205
  - Rakefile
206
206
  - bin/console
207
207
  - bin/setup
208
+ - config/blockchains.yml
209
+ - config/currencies.yml
210
+ - config/initializers/coinpayments.rb
211
+ - config/wallets.yml
208
212
  - lib/peatio/coinpaymentnew.rb
209
213
  - lib/peatio/coinpaymentnew/blockchain.rb
210
214
  - lib/peatio/coinpaymentnew/client.rb
@@ -212,9 +216,6 @@ files:
212
216
  - lib/peatio/coinpaymentnew/railtie.rb
213
217
  - lib/peatio/coinpaymentnew/version.rb
214
218
  - lib/peatio/coinpaymentnew/wallet.rb
215
- - peatio-coinpaymentnew-0.0.2.gem
216
- - peatio-coinpaymentnew-0.0.3.gem
217
- - peatio-coinpaymentnew-0.0.4.gem
218
219
  - peatio-coinpaymentnew.gemspec
219
220
  homepage: https://openware.com/
220
221
  licenses:
Binary file
Binary file
Binary file