module-render-xyz 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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/Dockerfile +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +114 -0
- data/LICENSE.txt +21 -0
- data/README.md +108 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/blockchains.yml +10 -0
- data/config/currencies.yml +20 -0
- data/config/wallets.yml +47 -0
- data/docs/integration.md +29 -0
- data/docs/json-rpc.md +29 -0
- data/docs/testnet.md +5 -0
- data/lib/peatio/infura.rb +18 -0
- data/lib/peatio/infura/blockchain.rb +221 -0
- data/lib/peatio/infura/client.rb +77 -0
- data/lib/peatio/infura/hooks.rb +42 -0
- data/lib/peatio/infura/railtie.rb +13 -0
- data/lib/peatio/infura/version.rb +5 -0
- data/lib/peatio/infura/wallet.rb +223 -0
- data/peatio-infura.gemspec +39 -0
- metadata +237 -0
data/docs/testnet.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "active_support/core_ext/object/blank"
|
2
|
+
require "active_support/core_ext/enumerable"
|
3
|
+
require "peatio"
|
4
|
+
|
5
|
+
module Peatio
|
6
|
+
module Infura
|
7
|
+
require "bigdecimal"
|
8
|
+
require "bigdecimal/util"
|
9
|
+
|
10
|
+
require "peatio/infura/blockchain"
|
11
|
+
require "peatio/infura/client"
|
12
|
+
require "peatio/infura/wallet"
|
13
|
+
|
14
|
+
require "peatio/infura/hooks"
|
15
|
+
|
16
|
+
require "peatio/infura/version"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
|
2
|
+
module Peatio
|
3
|
+
module Infura
|
4
|
+
class Blockchain < Peatio::Blockchain::Abstract
|
5
|
+
|
6
|
+
UndefinedCurrencyError = Class.new(StandardError)
|
7
|
+
|
8
|
+
TOKEN_EVENT_IDENTIFIER = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
|
9
|
+
SUCCESS = '0x1'
|
10
|
+
FAILED = '0x0'
|
11
|
+
|
12
|
+
DEFAULT_FEATURES = { case_sensitive: false, cash_addr_format: false }.freeze
|
13
|
+
|
14
|
+
def initialize(custom_features = {})
|
15
|
+
@features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
|
16
|
+
@settings = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure(settings = {})
|
20
|
+
# Clean client state during configure.
|
21
|
+
@client = nil
|
22
|
+
@erc20 = []; @eth = []
|
23
|
+
|
24
|
+
@settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
|
25
|
+
@settings[:currencies]&.each do |c|
|
26
|
+
if c.dig(:options, :erc20_contract_address).present?
|
27
|
+
@erc20 << c
|
28
|
+
else
|
29
|
+
@eth << c
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def fetch_block!(block_number)
|
35
|
+
block_json = client.json_rpc(:eth_getBlockByNumber, ["0x#{block_number.to_s(16)}", true])
|
36
|
+
|
37
|
+
if block_json.blank? || block_json['transactions'].blank?
|
38
|
+
return Peatio::Block.new(block_number, [])
|
39
|
+
end
|
40
|
+
block_json.fetch('transactions').each_with_object([]) do |tx, block_arr|
|
41
|
+
if tx.fetch('input').hex <= 0
|
42
|
+
next if invalid_eth_transaction?(tx)
|
43
|
+
else
|
44
|
+
next if @erc20.find { |c| c.dig(:options, :erc20_contract_address) == normalize_address(tx.fetch('to')) }.blank?
|
45
|
+
tx = client.json_rpc(:eth_getTransactionReceipt, [normalize_txid(tx.fetch('hash'))])
|
46
|
+
next if tx.nil? || tx.fetch('to').blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
txs = build_transactions(tx).map do |ntx|
|
50
|
+
Peatio::Transaction.new(ntx)
|
51
|
+
end
|
52
|
+
|
53
|
+
block_arr.append(*txs)
|
54
|
+
end.yield_self { |block_arr| Peatio::Block.new(block_number, block_arr) }
|
55
|
+
rescue Infura::Client::Error => e
|
56
|
+
raise Peatio::Blockchain::ClientError, e
|
57
|
+
end
|
58
|
+
|
59
|
+
def latest_block_number
|
60
|
+
client.json_rpc(:eth_blockNumber).to_i(16)
|
61
|
+
rescue Infura::Client::Error => e
|
62
|
+
raise Peatio::Blockchain::ClientError, e
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_balance_of_address!(address, currency_id)
|
66
|
+
currency = settings[:currencies].find { |c| c[:id] == currency_id.to_s }
|
67
|
+
raise UndefinedCurrencyError unless currency
|
68
|
+
|
69
|
+
if currency.dig(:options, :erc20_contract_address).present?
|
70
|
+
load_erc20_balance(address, currency)
|
71
|
+
else
|
72
|
+
client.json_rpc(:eth_getBalance, [normalize_address(address), 'latest'])
|
73
|
+
.hex
|
74
|
+
.to_d
|
75
|
+
.yield_self { |amount| convert_from_base_unit(amount, currency) }
|
76
|
+
end
|
77
|
+
rescue Infura::Client::Error => e
|
78
|
+
raise Peatio::Blockchain::ClientError, e
|
79
|
+
end
|
80
|
+
|
81
|
+
def fetch_transaction(transaction)
|
82
|
+
currency = settings[:currencies].find { |c| c.fetch(:id) == transaction.currency_id }
|
83
|
+
return if currency.blank?
|
84
|
+
txn_receipt = client.json_rpc(:eth_getTransactionReceipt, [transaction.hash])
|
85
|
+
if currency.in?(@eth)
|
86
|
+
txn_json = client.json_rpc(:eth_getTransactionByHash, [transaction.hash])
|
87
|
+
attributes = {
|
88
|
+
amount: convert_from_base_unit(txn_json.fetch('value').hex, currency),
|
89
|
+
to_address: normalize_address(txn_json['to']),
|
90
|
+
status: transaction_status(txn_receipt)
|
91
|
+
}
|
92
|
+
else
|
93
|
+
txn_json = txn_receipt.fetch('logs').find { |log| log['logIndex'].to_i(16) == transaction.txout }
|
94
|
+
attributes = {
|
95
|
+
amount: convert_from_base_unit(txn_json.fetch('data').hex, currency),
|
96
|
+
to_address: normalize_address('0x' + txn_json.fetch('topics').last[-40..-1]),
|
97
|
+
status: transaction_status(txn_receipt)
|
98
|
+
}
|
99
|
+
end
|
100
|
+
transaction.assign_attributes(attributes)
|
101
|
+
transaction
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def load_erc20_balance(address, currency)
|
107
|
+
data = abi_encode('balanceOf(address)', normalize_address(address))
|
108
|
+
client.json_rpc(:eth_call, [{ to: contract_address(currency), data: data }, 'latest'])
|
109
|
+
.hex
|
110
|
+
.to_d
|
111
|
+
.yield_self { |amount| convert_from_base_unit(amount, currency) }
|
112
|
+
end
|
113
|
+
|
114
|
+
def client
|
115
|
+
@client ||= Infura::Client.new(settings_fetch(:server))
|
116
|
+
end
|
117
|
+
|
118
|
+
def settings_fetch(key)
|
119
|
+
@settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
|
120
|
+
end
|
121
|
+
|
122
|
+
def normalize_txid(txid)
|
123
|
+
txid.try(:downcase)
|
124
|
+
end
|
125
|
+
|
126
|
+
def normalize_address(address)
|
127
|
+
address.try(:downcase)
|
128
|
+
end
|
129
|
+
|
130
|
+
def build_transactions(tx_hash)
|
131
|
+
if tx_hash.has_key?('logs')
|
132
|
+
build_erc20_transactions(tx_hash)
|
133
|
+
else
|
134
|
+
build_eth_transactions(tx_hash)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def build_eth_transactions(block_txn)
|
139
|
+
@eth.map do |currency|
|
140
|
+
{ hash: normalize_txid(block_txn.fetch('hash')),
|
141
|
+
amount: convert_from_base_unit(block_txn.fetch('value').hex, currency),
|
142
|
+
to_address: normalize_address(block_txn['to']),
|
143
|
+
txout: block_txn.fetch('transactionIndex').to_i(16),
|
144
|
+
block_number: block_txn.fetch('blockNumber').to_i(16),
|
145
|
+
currency_id: currency.fetch(:id),
|
146
|
+
status: transaction_status(block_txn) }
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def build_erc20_transactions(txn_receipt)
|
151
|
+
# Build invalid transaction for failed withdrawals
|
152
|
+
if transaction_status(txn_receipt) == 'fail' && txn_receipt.fetch('logs').blank?
|
153
|
+
return build_invalid_erc20_transaction(txn_receipt)
|
154
|
+
end
|
155
|
+
|
156
|
+
txn_receipt.fetch('logs').each_with_object([]) do |log, formatted_txs|
|
157
|
+
|
158
|
+
next if log.fetch('topics').blank? || log.fetch('topics')[0] != TOKEN_EVENT_IDENTIFIER
|
159
|
+
|
160
|
+
# Skip if ERC20 contract address doesn't match.
|
161
|
+
currencies = @erc20.select { |c| c.dig(:options, :erc20_contract_address) == log.fetch('address') }
|
162
|
+
next if currencies.blank?
|
163
|
+
|
164
|
+
destination_address = normalize_address('0x' + log.fetch('topics').last[-40..-1])
|
165
|
+
|
166
|
+
currencies.each do |currency|
|
167
|
+
formatted_txs << { hash: normalize_txid(txn_receipt.fetch('transactionHash')),
|
168
|
+
amount: convert_from_base_unit(log.fetch('data').hex, currency),
|
169
|
+
to_address: destination_address,
|
170
|
+
txout: log['logIndex'].to_i(16),
|
171
|
+
block_number: txn_receipt.fetch('blockNumber').to_i(16),
|
172
|
+
currency_id: currency.fetch(:id),
|
173
|
+
status: transaction_status(txn_receipt) }
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def build_invalid_erc20_transaction(txn_receipt)
|
179
|
+
currencies = @erc20.select { |c| c.dig(:options, :erc20_contract_address) == txn_receipt.fetch('to') }
|
180
|
+
return if currencies.blank?
|
181
|
+
|
182
|
+
currencies.each_with_object([]) do |currency, invalid_txs|
|
183
|
+
invalid_txs << { hash: normalize_txid(txn_receipt.fetch('transactionHash')),
|
184
|
+
block_number: txn_receipt.fetch('blockNumber').to_i(16),
|
185
|
+
currency_id: currency.fetch(:id),
|
186
|
+
status: transaction_status(txn_receipt) }
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def transaction_status(block_txn)
|
191
|
+
if block_txn.dig('status') == SUCCESS
|
192
|
+
'success'
|
193
|
+
elsif block_txn.dig('status') == FAILED
|
194
|
+
'failed'
|
195
|
+
else
|
196
|
+
'pending'
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def invalid_eth_transaction?(block_txn)
|
201
|
+
block_txn.fetch('to').blank? \
|
202
|
+
|| block_txn.fetch('value').hex.to_d <= 0 && block_txn.fetch('input').hex <= 0
|
203
|
+
end
|
204
|
+
|
205
|
+
def contract_address(currency)
|
206
|
+
normalize_address(currency.dig(:options, :erc20_contract_address))
|
207
|
+
end
|
208
|
+
|
209
|
+
def abi_encode(method, *args)
|
210
|
+
'0x' + args.each_with_object(Digest::SHA3.hexdigest(method, 256)[0...8]) do |arg, data|
|
211
|
+
data.concat(arg.gsub(/\A0x/, '').rjust(64, '0'))
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def convert_from_base_unit(value, currency)
|
216
|
+
value.to_d / currency.fetch(:base_factor).to_d
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'memoist'
|
2
|
+
require 'faraday'
|
3
|
+
require 'better-faraday'
|
4
|
+
|
5
|
+
module Peatio
|
6
|
+
module Infura
|
7
|
+
class Client
|
8
|
+
Error = Class.new(StandardError)
|
9
|
+
|
10
|
+
class ConnectionError < Error; end
|
11
|
+
|
12
|
+
class ResponseError < Error
|
13
|
+
def initialize(code, msg)
|
14
|
+
super "#{msg} (#{code})"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
extend Memoist
|
19
|
+
|
20
|
+
def initialize(endpoint, sign_uri, idle_timeout: 5)
|
21
|
+
@json_rpc_endpoint = URI.parse(endpoint)
|
22
|
+
@sign_rpc_endpoint = URI.parse(sign_uri)
|
23
|
+
@json_rpc_call_id = 0
|
24
|
+
@idle_timeout = idle_timeout
|
25
|
+
end
|
26
|
+
|
27
|
+
def json_rpc(method, params = [], conn_type=nil)
|
28
|
+
current_conn = connection
|
29
|
+
unless conn_type.nil?
|
30
|
+
current_conn = sign_connection
|
31
|
+
end
|
32
|
+
response = current_conn.post \
|
33
|
+
'/',
|
34
|
+
{jsonrpc: '2.0', id: rpc_call_id, method: method, params: params}.to_json,
|
35
|
+
{'Accept' => 'application/json',
|
36
|
+
'Content-Type' => 'application/json'}
|
37
|
+
response.assert_success!
|
38
|
+
response = JSON.parse(response.body)
|
39
|
+
response['error'].tap { |error| raise ResponseError.new(error['code'], error['message']) if error }
|
40
|
+
response.fetch('result')
|
41
|
+
rescue Faraday::Error => e
|
42
|
+
raise ConnectionError, e
|
43
|
+
rescue StandardError => e
|
44
|
+
raise Error, e
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def rpc_call_id
|
50
|
+
@json_rpc_call_id += 1
|
51
|
+
end
|
52
|
+
|
53
|
+
def sign_connection
|
54
|
+
@connection ||= Faraday.new(@sign_rpc_endpoint) do |f|
|
55
|
+
f.adapter :net_http_persistent, pool_size: 5, idle_timeout: @idle_timeout
|
56
|
+
end.tap do |connection|
|
57
|
+
unless @json_rpc_endpoint.user.blank?
|
58
|
+
connection.basic_auth(@json_rpc_endpoint.user, @json_rpc_endpoint.password)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def connection
|
64
|
+
@connection ||= Faraday.new(@json_rpc_endpoint) do |f|
|
65
|
+
f.adapter :net_http_persistent, pool_size: 5, idle_timeout: @idle_timeout
|
66
|
+
end.tap do |connection|
|
67
|
+
unless @json_rpc_endpoint.user.blank?
|
68
|
+
connection.basic_auth(@json_rpc_endpoint.user, @json_rpc_endpoint.password)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Peatio
|
2
|
+
module Infura
|
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
|
+
"Infura blockchain version requiremnt was not suttisfied by Peatio::Blockchain.",
|
13
|
+
"Infura 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
|
+
"Infura wallet version requiremnt was not suttisfied by Peatio::Wallet.",
|
22
|
+
"Infura 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[:infura] = Infura::Blockchain.new
|
30
|
+
Peatio::Wallet.registry[:infurad] = Infura::Wallet.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if defined?(Rails::Railtie)
|
35
|
+
require "peatio/infura/railtie"
|
36
|
+
else
|
37
|
+
check_compatibility
|
38
|
+
register
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
module Peatio
|
2
|
+
module Infura
|
3
|
+
class Wallet < Peatio::Wallet::Abstract
|
4
|
+
|
5
|
+
DEFAULT_ETH_FEE = { gas_limit: 21_000, gas_price: 1_000_000_000 }.freeze
|
6
|
+
|
7
|
+
DEFAULT_ERC20_FEE = { gas_limit: 90_000, gas_price: 1_000_000_000 }.freeze
|
8
|
+
|
9
|
+
def initialize(settings = {})
|
10
|
+
@settings = settings
|
11
|
+
end
|
12
|
+
|
13
|
+
def configure(settings = {})
|
14
|
+
# Clean client state during configure.
|
15
|
+
@client = nil
|
16
|
+
|
17
|
+
@settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
|
18
|
+
|
19
|
+
@wallet = @settings.fetch(:wallet) do
|
20
|
+
raise Peatio::Wallet::MissingSettingError, :wallet
|
21
|
+
end.slice(:uri, :sign_uri, :address, :secret)
|
22
|
+
|
23
|
+
@currency = @settings.fetch(:currency) do
|
24
|
+
raise Peatio::Wallet::MissingSettingError, :currency
|
25
|
+
end.slice(:id, :base_factor, :options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_address!(options = {})
|
29
|
+
secret = options.fetch(:secret) { PasswordGenerator.generate(64) }
|
30
|
+
secret.yield_self do |password|
|
31
|
+
{ address: normalize_address(client.json_rpc(:personal_newAccount, [password], conn_type="sign")),
|
32
|
+
secret: password }
|
33
|
+
end
|
34
|
+
rescue Infura::Client::Error => e
|
35
|
+
raise Peatio::Wallet::ClientError, e
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_transaction!(transaction, options = {})
|
39
|
+
if @currency.dig(:options, :erc20_contract_address).present?
|
40
|
+
create_erc20_transaction!(transaction)
|
41
|
+
else
|
42
|
+
create_eth_transaction!(transaction, options)
|
43
|
+
end
|
44
|
+
rescue Infura::Client::Error => e
|
45
|
+
raise Peatio::Wallet::ClientError, e
|
46
|
+
end
|
47
|
+
|
48
|
+
def sign_transaction(data)
|
49
|
+
client.json_rpc(:eth_signTransaction, [ data ], conn_type="sign").tap do |response|
|
50
|
+
unless response
|
51
|
+
raise Infura::WalletClient::Error, \
|
52
|
+
"#{walletnormalize_address.name} withdrawal from #{normalize_address(issuer[:address])} to #{normalize_address(recipient[:address])} is not permitted."
|
53
|
+
end
|
54
|
+
end
|
55
|
+
rescue Infura::Client::Error => e
|
56
|
+
raise Peatio::Wallet::ClientError, e
|
57
|
+
end
|
58
|
+
|
59
|
+
def prepare_deposit_collection!(transaction, deposit_spread, deposit_currency)
|
60
|
+
# TODO: Add spec for this behaviour.
|
61
|
+
# Don't prepare for deposit_collection in case of eth deposit.
|
62
|
+
return [] if deposit_currency.dig(:options, :erc20_contract_address).blank?
|
63
|
+
|
64
|
+
options = DEFAULT_ERC20_FEE.merge(deposit_currency.fetch(:options).slice(:gas_limit, :gas_price))
|
65
|
+
|
66
|
+
# We collect fees depending on the number of spread deposit size
|
67
|
+
# Example: if deposit spreads on three wallets need to collect eth fee for 3 transactions
|
68
|
+
fees = convert_from_base_unit(options.fetch(:gas_limit).to_i * options.fetch(:gas_price).to_i)
|
69
|
+
transaction.amount = fees * deposit_spread.size
|
70
|
+
|
71
|
+
[create_eth_transaction!(transaction)]
|
72
|
+
rescue Infura::Client::Error => e
|
73
|
+
raise Peatio::Wallet::ClientError, e
|
74
|
+
end
|
75
|
+
|
76
|
+
def load_balance!
|
77
|
+
if @currency.dig(:options, :erc20_contract_address).present?
|
78
|
+
load_erc20_balance(@wallet.fetch(:address))
|
79
|
+
else
|
80
|
+
client.json_rpc(:eth_getBalance, [normalize_address(@wallet.fetch(:address)), 'latest'])
|
81
|
+
.hex
|
82
|
+
.to_d
|
83
|
+
.yield_self { |amount| convert_from_base_unit(amount) }
|
84
|
+
end
|
85
|
+
rescue Infura::Client::Error => e
|
86
|
+
raise Peatio::Wallet::ClientError, e
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def load_erc20_balance(address)
|
92
|
+
data = abi_encode('balanceOf(address)', normalize_address(address))
|
93
|
+
client.json_rpc(:eth_call, [{ to: contract_address, data: data }, 'latest'])
|
94
|
+
.hex
|
95
|
+
.to_d
|
96
|
+
.yield_self { |amount| convert_from_base_unit(amount) }
|
97
|
+
end
|
98
|
+
|
99
|
+
def permit_transaction
|
100
|
+
client.json_rpc(:personal_unlockAccount, [normalize_address(@wallet.fetch(:address)), @wallet.fetch(:secret), 5], conn_type="sign").tap do |response|
|
101
|
+
unless response
|
102
|
+
raise Infura::WalletClient::Error, \
|
103
|
+
"#{wallet.name} withdrawal from #{normalize_address(@wallet.fetch(:address))} to address is not permitted."
|
104
|
+
end
|
105
|
+
end
|
106
|
+
rescue Infura::Client::Error => e
|
107
|
+
raise Peatio::Wallet::ClientError, e
|
108
|
+
end
|
109
|
+
|
110
|
+
def get_transaction_count
|
111
|
+
json_rpc(:eth_getTransactionCount, [normalize_address(@wallet.fetch(:address)), "latest"]).tap do |response|
|
112
|
+
unless response
|
113
|
+
raise Infura::WalletClient::Error, \
|
114
|
+
"#{wallet.name} withdrawal from #{normalize_address(@wallet.fetch(:address))} to address is not permitted."
|
115
|
+
end
|
116
|
+
end
|
117
|
+
rescue Infura::Client::Error => e
|
118
|
+
raise Peatio::Wallet::ClientError, e
|
119
|
+
end
|
120
|
+
|
121
|
+
def create_eth_transaction!(transaction, options = {})
|
122
|
+
currency_options = @currency.fetch(:options).slice(:gas_limit, :gas_price)
|
123
|
+
options.merge!(DEFAULT_ETH_FEE, currency_options)
|
124
|
+
|
125
|
+
amount = convert_to_base_unit(transaction.amount)
|
126
|
+
|
127
|
+
# Subtract fees from initial deposit amount in case of deposit collection
|
128
|
+
amount -= options.fetch(:gas_limit).to_i * options.fetch(:gas_price).to_i if options.dig(:subtract_fee)
|
129
|
+
|
130
|
+
permit_transaction
|
131
|
+
signed_hash = sign_transaction({
|
132
|
+
from: normalize_address(@wallet.fetch(:address)),
|
133
|
+
to: normalize_address(transaction.to_address),
|
134
|
+
value: '0x' + amount.to_s(16),
|
135
|
+
gas: '0x' + options.fetch(:gas_limit).to_i.to_s(16),
|
136
|
+
gasPrice: '0x' + options.fetch(:gas_price).to_i.to_s(16),
|
137
|
+
nonce: get_transaction_count
|
138
|
+
}.compact)
|
139
|
+
|
140
|
+
txid = client.json_rpc(:eth_sendRawTransaction, [ signed_hash.fetch('raw') ] )
|
141
|
+
|
142
|
+
unless valid_txid?(normalize_txid(txid))
|
143
|
+
raise Infura::WalletClient::Error, \
|
144
|
+
"Withdrawal from #{@wallet.fetch(:address)} to #{transaction.to_address} failed."
|
145
|
+
end
|
146
|
+
transaction.amount = convert_from_base_unit(amount)
|
147
|
+
transaction.hash = normalize_txid(txid)
|
148
|
+
transaction
|
149
|
+
end
|
150
|
+
|
151
|
+
def create_erc20_transaction!(transaction, options = {})
|
152
|
+
currency_options = @currency.fetch(:options).slice(:gas_limit, :gas_price, :erc20_contract_address)
|
153
|
+
options.merge!(DEFAULT_ERC20_FEE, currency_options)
|
154
|
+
|
155
|
+
amount = convert_to_base_unit(transaction.amount)
|
156
|
+
data = abi_encode('transfer(address,uint256)',
|
157
|
+
normalize_address(transaction.to_address),
|
158
|
+
'0x' + amount.to_s(16))
|
159
|
+
|
160
|
+
permit_transaction
|
161
|
+
signed_hash = sign_transaction({
|
162
|
+
from: normalize_address(@wallet.fetch(:address)),
|
163
|
+
to: normalize_address(transaction.to_address),
|
164
|
+
data: data,
|
165
|
+
gas: '0x' + options.fetch(:gas_limit).to_i.to_s(16),
|
166
|
+
gasPrice: '0x' + options.fetch(:gas_price).to_i.to_s(16),
|
167
|
+
nonce: get_transaction_count
|
168
|
+
}.compact)
|
169
|
+
|
170
|
+
txid = client.json_rpc(:eth_sendRawTransaction, [ signed_hash.fetch('raw') ] )
|
171
|
+
|
172
|
+
unless valid_txid?(normalize_txid(txid))
|
173
|
+
raise Infura::WalletClient::Error, \
|
174
|
+
"Withdrawal from #{@wallet.fetch(:address)} to #{transaction.to_address} failed."
|
175
|
+
end
|
176
|
+
transaction.hash = normalize_txid(txid)
|
177
|
+
transaction
|
178
|
+
end
|
179
|
+
|
180
|
+
def normalize_address(address)
|
181
|
+
address.downcase
|
182
|
+
end
|
183
|
+
|
184
|
+
def normalize_txid(txid)
|
185
|
+
txid.downcase
|
186
|
+
end
|
187
|
+
|
188
|
+
def contract_address
|
189
|
+
normalize_address(@currency.dig(:options, :erc20_contract_address))
|
190
|
+
end
|
191
|
+
|
192
|
+
def valid_txid?(txid)
|
193
|
+
txid.to_s.match?(/\A0x[A-F0-9]{64}\z/i)
|
194
|
+
end
|
195
|
+
|
196
|
+
def abi_encode(method, *args)
|
197
|
+
'0x' + args.each_with_object(Digest::SHA3.hexdigest(method, 256)[0...8]) do |arg, data|
|
198
|
+
data.concat(arg.gsub(/\A0x/, '').rjust(64, '0'))
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def convert_from_base_unit(value)
|
203
|
+
value.to_d / @currency.fetch(:base_factor) #/
|
204
|
+
end
|
205
|
+
|
206
|
+
def convert_to_base_unit(value)
|
207
|
+
x = value.to_d * @currency.fetch(:base_factor)
|
208
|
+
unless (x % 1).zero?
|
209
|
+
raise Peatio::WalletClient::Error,
|
210
|
+
"Failed to convert value to base (smallest) unit because it exceeds the maximum precision: " \
|
211
|
+
"#{value.to_d} - #{x.to_d} must be equal to zero."
|
212
|
+
end
|
213
|
+
x.to_i
|
214
|
+
end
|
215
|
+
|
216
|
+
def client
|
217
|
+
uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
|
218
|
+
sign_uri = @wallet.fetch(:sign_uri) { raise Peatio::Wallet::MissingSettingError, :sign_uri }
|
219
|
+
@client ||= Client.new(uri, sign_uri, idle_timeout: 1)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|