etherlite 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/etherlite.rb +2 -0
- data/lib/etherlite/account.rb +7 -5
- data/lib/etherlite/connection.rb +5 -1
- data/lib/etherlite/nonce_manager.rb +45 -0
- data/lib/etherlite/pk_account.rb +18 -10
- data/lib/etherlite/transaction.rb +10 -0
- data/lib/etherlite/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed20095933d11d8e8d7b9cda7dce06cfb6acd1bf
|
4
|
+
data.tar.gz: aebb4e88d292b3c021cebb1b87318fd8b51abd2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dff58138afca0dd99d83692a218a4e410880c6f07cb6bdf0727ff8603f8ce107a5ec9c5ab7886462e831ef4bbd0dcb16bb9b2f254ea5dd7e9a328f3a345ff79d
|
7
|
+
data.tar.gz: d7c84736ea549eb7b3f59c9bf41e5fc493ebb3c37aaad2b25caff2a7c1d6db5ce196c7809eccf5fe1252ef1b1cd8705f6400819b6345f1af9ae54c851d880ce9
|
data/lib/etherlite.rb
CHANGED
@@ -28,6 +28,8 @@ require "etherlite/configuration"
|
|
28
28
|
require "etherlite/abi"
|
29
29
|
require "etherlite/utils"
|
30
30
|
require "etherlite/connection"
|
31
|
+
require "etherlite/transaction"
|
32
|
+
require "etherlite/nonce_manager"
|
31
33
|
require "etherlite/pk_account"
|
32
34
|
require "etherlite/account"
|
33
35
|
require "etherlite/address"
|
data/lib/etherlite/account.rb
CHANGED
@@ -73,11 +73,13 @@ module Etherlite
|
|
73
73
|
_params[:gasPrice] = Utils.encode_quantity_param(_opt[:gas_price]) if _opt.key? :gas_price
|
74
74
|
|
75
75
|
passphrase = _opt.fetch(:passphrase, @passphrase)
|
76
|
-
if passphrase.nil?
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
tx_hash = if passphrase.nil?
|
77
|
+
@connection.ipc_call(:eth_sendTransaction, _params)
|
78
|
+
else
|
79
|
+
@connection.ipc_call(:personal_sendTransaction, _params, passphrase)
|
80
|
+
end
|
81
|
+
|
82
|
+
Transaction.new @connection, tx_hash
|
81
83
|
end
|
82
84
|
end
|
83
85
|
end
|
data/lib/etherlite/connection.rb
CHANGED
@@ -9,7 +9,7 @@ module Etherlite
|
|
9
9
|
payload = { jsonrpc: "2.0", method: _method, params: _params, id: id }
|
10
10
|
|
11
11
|
# TODO: support ipc
|
12
|
-
Net::HTTP.start(@uri.hostname, @uri.port) do |http|
|
12
|
+
Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: use_ssl?) do |http|
|
13
13
|
return handle_response http.post(
|
14
14
|
@uri.path.empty? ? '/' : @uri.path,
|
15
15
|
payload.to_json,
|
@@ -24,6 +24,10 @@ module Etherlite
|
|
24
24
|
(Time.now.to_f * 1000.0).to_i
|
25
25
|
end
|
26
26
|
|
27
|
+
def use_ssl?
|
28
|
+
@uri.scheme == 'https'
|
29
|
+
end
|
30
|
+
|
27
31
|
def handle_response(_response, _id)
|
28
32
|
case _response
|
29
33
|
when Net::HTTPSuccess
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Etherlite
|
2
|
+
class NonceManager
|
3
|
+
@@nonce_cache = {}
|
4
|
+
@@nonce_mutex = Mutex.new
|
5
|
+
|
6
|
+
def initialize(_connection)
|
7
|
+
@connection = _connection
|
8
|
+
end
|
9
|
+
|
10
|
+
def last_nonce_for(_address)
|
11
|
+
_address = Utils.encode_address_param _address
|
12
|
+
last_nonce = @@nonce_cache[_address]
|
13
|
+
last_nonce = last_observed_nonce_for(_address) if last_nonce.nil?
|
14
|
+
last_nonce
|
15
|
+
end
|
16
|
+
|
17
|
+
def with_next_nonce_for(_address)
|
18
|
+
_address = Utils.encode_address_param _address
|
19
|
+
|
20
|
+
@@nonce_mutex.synchronize do
|
21
|
+
next_nonce = last_nonce_for(_address) + 1
|
22
|
+
|
23
|
+
begin
|
24
|
+
result = yield next_nonce
|
25
|
+
@@nonce_cache[_address] = next_nonce
|
26
|
+
return result
|
27
|
+
rescue
|
28
|
+
# if yield fails, cant be sure about transaction status so must rely again on observing.
|
29
|
+
@@nonce_cache.delete _address
|
30
|
+
raise
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def last_observed_nonce_for(_address)
|
38
|
+
# TODO: support using tx_pool API to improve this:
|
39
|
+
# http://qnimate.com/calculating-nonce-for-raw-transactions-in-geth/
|
40
|
+
Etherlite::Utils.hex_to_uint(
|
41
|
+
@connection.ipc_call(:eth_getTransactionCount, _address, 'pending')
|
42
|
+
) - 1
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/etherlite/pk_account.rb
CHANGED
@@ -52,23 +52,31 @@ module Etherlite
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def send_transaction(_value, _hex_data, _hex_address, _opt)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
tx_hash = nonce_manager.with_next_nonce_for(@key.address) do |nonce|
|
56
|
+
tx = Eth::Tx.new(
|
57
|
+
value: _value,
|
58
|
+
data: _hex_data,
|
59
|
+
gas_limit: _opt.fetch(:gas, 90_000),
|
60
|
+
gas_price: _opt.fetch(:gas_price, gas_price),
|
61
|
+
to: _hex_address,
|
62
|
+
nonce: nonce
|
63
|
+
)
|
64
|
+
|
65
|
+
tx.sign @key
|
63
66
|
|
64
|
-
|
67
|
+
@connection.ipc_call(:eth_sendRawTransaction, tx.hex)
|
68
|
+
end
|
65
69
|
|
66
|
-
@connection
|
70
|
+
Transaction.new @connection, tx_hash
|
67
71
|
end
|
68
72
|
|
69
73
|
def gas_price
|
70
74
|
# TODO: improve on this
|
71
75
|
@gas_price ||= Etherlite::Utils.hex_to_uint @connection.ipc_call(:eth_gasPrice)
|
72
76
|
end
|
77
|
+
|
78
|
+
def nonce_manager
|
79
|
+
NonceManager.new @connection
|
80
|
+
end
|
73
81
|
end
|
74
82
|
end
|
data/lib/etherlite/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: etherlite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignacio Baixas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: digest-sha3
|
@@ -175,10 +175,12 @@ files:
|
|
175
175
|
- lib/etherlite/contract/event_base.rb
|
176
176
|
- lib/etherlite/contract/event_input.rb
|
177
177
|
- lib/etherlite/contract/function.rb
|
178
|
+
- lib/etherlite/nonce_manager.rb
|
178
179
|
- lib/etherlite/pk_account.rb
|
179
180
|
- lib/etherlite/railtie.rb
|
180
181
|
- lib/etherlite/railties/configuration_extensions.rb
|
181
182
|
- lib/etherlite/railties/utils.rb
|
183
|
+
- lib/etherlite/transaction.rb
|
182
184
|
- lib/etherlite/types/address.rb
|
183
185
|
- lib/etherlite/types/array_base.rb
|
184
186
|
- lib/etherlite/types/array_dynamic.rb
|