etherlite 0.3.0 → 0.5.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
  SHA256:
3
- metadata.gz: fe14df0fc898d4e298d57704e8a7f2acf0cb3dcace02b94903b8f295f94e971c
4
- data.tar.gz: 87c4bc2dca3fb92d8a1eca54080cbfa50107defe48d0007cc5e6c3910bdfaa4e
3
+ metadata.gz: a8beb990ec1fa4fabecb75a9162645a53eef721e5893d1514d4b31ba79879fbd
4
+ data.tar.gz: b6614af7c2af90feaac3bbc20cc5b7b72bdb67cf6c3be4191a4264cab76fa4ae
5
5
  SHA512:
6
- metadata.gz: c1e16e2460b1e67948efd91921b1e03e6805c114fdba9a375c4b83695baa6d01ca80b46fc3530be5ac923c641d47190cc7fc466cdf6daef793f55cb54993be0a
7
- data.tar.gz: 76cb54b21dd785777fcd474f0bbf85e374b9a6aad8bd64d1ddfed73f195c51a736b339045b09056be8da32c34e7d272abcf7393b4bc1bd187efe2fb2186adc57
6
+ metadata.gz: b6aa631ed700987f27da859c7f68781c6ee4d148be34b1bc192af5ac5e223c96b446f4002c598e4f516ed469eeae313ffb9d2658a6dead638544ad03f2e796a2
7
+ data.tar.gz: 4349906dba722c8bdaa6a2de6e4d6a33555bf15cb15d90ac292709b4350c9a94d3128b2522d270105e6dedaec8d19e10fff06eae3f68e0750257bbfbb0046c99
@@ -9,6 +9,15 @@ module Etherlite::Account
9
9
  @normalized_address = _normalized_address
10
10
  end
11
11
 
12
+ def next_nonce
13
+ if @connection.use_parity
14
+ @connection.parity_next_nonce(address)
15
+ else
16
+ # https://github.com/ethereum/go-ethereum/issues/2736
17
+ @connection.eth_get_transaction_count(address, 'pending')
18
+ end
19
+ end
20
+
12
21
  def transfer_to(_target, _options = {})
13
22
  send_transaction _options.merge(to: _target, value: _options.fetch(:amount, 0))
14
23
  end
@@ -30,7 +39,7 @@ module Etherlite::Account
30
39
  end
31
40
 
32
41
  def send_transaction(_options = {})
33
- raise NotSupportedError, 'transactions are not supported by this kind of account'
42
+ raise Etherlite::NotSupportedError, 'transactions are not supported by this kind of account'
34
43
  end
35
44
 
36
45
  def ==(_other)
@@ -41,7 +50,6 @@ module Etherlite::Account
41
50
 
42
51
  def call_constant(_target, _function, _params, _options)
43
52
  params = {
44
- from: json_encoded_address,
45
53
  to: Etherlite::Utils.encode_address_param(_target),
46
54
  data: _function.encode(_params)
47
55
  }
@@ -8,25 +8,27 @@ module Etherlite
8
8
  super _connection, Etherlite::Utils.normalize_address(@key.address)
9
9
  end
10
10
 
11
+ def build_raw_transaction(_options = {})
12
+ nonce = nonce_manager.next_nonce_for(normalized_address, _options.slice(:replace, :nonce))
13
+
14
+ tx = Eth::Tx.new(
15
+ value: _options.fetch(:value, 0),
16
+ data: _options.fetch(:data, ''),
17
+ gas_limit: _options.fetch(:gas, 90_000),
18
+ gas_price: _options.fetch(:gas_price, gas_price),
19
+ to: (Etherlite::Utils.encode_address_param(_options[:to]) if _options.key?(:to)),
20
+ nonce: nonce
21
+ )
22
+
23
+ sign_with_connection_chain tx
24
+
25
+ tx
26
+ end
27
+
11
28
  def send_transaction(_options = {})
12
- nonce_options = {
13
- replace: _options.fetch(:replace, false)
14
- }
15
-
16
- nonce_manager.with_next_nonce_for(normalized_address, nonce_options) do |nonce|
17
- tx = Eth::Tx.new(
18
- value: _options.fetch(:value, 0),
19
- data: _options.fetch(:data, ''),
20
- gas_limit: _options.fetch(:gas, 90_000),
21
- gas_price: _options.fetch(:gas_price, gas_price),
22
- to: (Etherlite::Utils.encode_address_param(_options[:to]) if _options.key?(:to)),
23
- nonce: nonce
24
- )
25
-
26
- # Since eth gem does not allow configuration of chains for every tx, we need
27
- # to globally configure it before signing. This is not thread safe so a mutex is needed.
28
- sign_with_connection_chain tx
29
+ tx = build_raw_transaction(_options)
29
30
 
31
+ nonce_manager.with_next_nonce_for(normalized_address, nonce: tx.nonce) do |nonce|
30
32
  Etherlite::Transaction.new @connection, @connection.eth_send_raw_transaction(tx.hex)
31
33
  end
32
34
  end
@@ -53,7 +53,11 @@ module Etherlite
53
53
  end
54
54
 
55
55
  def default_account
56
- @default_account ||= load_default_account
56
+ @default_account ||= (accounts.first || anonymous_account)
57
+ end
58
+
59
+ def anonymous_account
60
+ @anonymous_account ||= Etherlite::Account::Anonymous.new(connection)
57
61
  end
58
62
 
59
63
  def account_from_pk(_pk)
@@ -66,13 +70,6 @@ use 'load_account' instead"
66
70
  end
67
71
 
68
72
  def_delegators :default_account, :unlock, :lock, :normalized_address, :transfer_to, :call
69
-
70
- private
71
-
72
- def load_default_account
73
- # TODO: consider configuring a global PK and allow the default account to use it
74
- accounts.first || Etherlite::Account::Anonymous.new(connection)
75
- end
76
73
  end
77
74
  end
78
75
  end
@@ -81,9 +81,21 @@ module Etherlite::Abi
81
81
  )
82
82
  end,
83
83
  (_definition['outputs'] || []).map { |input| LoadType.for signature: input['type'] },
84
- _definition.fetch('payable', false),
85
- _definition.fetch('constant', false)
84
+ function_payable?(_definition),
85
+ function_constant?(_definition)
86
86
  )
87
87
  end
88
+
89
+ def function_payable?(_definition)
90
+ return _definition['payable'] if _definition.key? 'payable'
91
+
92
+ _definition['stateMutability'] == 'payable'
93
+ end
94
+
95
+ def function_constant?(_definition)
96
+ return _definition['constant'] if _definition.key? 'constant'
97
+
98
+ _definition['stateMutability'] == 'pure' || _definition['stateMutability'] == 'view'
99
+ end
88
100
  end
89
101
  end
@@ -17,14 +17,22 @@ module Etherlite
17
17
  last_nonce
18
18
  end
19
19
 
20
- def with_next_nonce_for(_normalized_address, replace: false)
20
+ def next_nonce_for(_normalized_address, replace: false, nonce: nil)
21
+ if nonce.nil?
22
+ nonce = last_nonce_for(_normalized_address)
23
+ nonce += 1 if nonce.negative? || !replace # if first tx, don't replace
24
+ end
25
+
26
+ nonce
27
+ end
28
+
29
+ def with_next_nonce_for(_normalized_address, _options = {})
21
30
  @@nonce_mutex.synchronize do
22
- next_nonce = last_nonce_for(_normalized_address)
23
- next_nonce += 1 if next_nonce.negative? || !replace # if first tx, don't replace
31
+ nonce = next_nonce_for(_normalized_address, _options)
24
32
 
25
33
  begin
26
- result = yield next_nonce
27
- @@nonce_cache[_normalized_address] = next_nonce if caching_enabled?
34
+ result = yield nonce
35
+ @@nonce_cache[_normalized_address] = nonce if caching_enabled?
28
36
  return result
29
37
  rescue
30
38
  # if yield fails, cant be sure about transaction status so must rely again on observing.
@@ -45,6 +45,12 @@ module Etherlite
45
45
  Utils.hex_to_uint(original['blockNumber'])
46
46
  end
47
47
 
48
+ def block_hash
49
+ return nil unless mined?
50
+
51
+ original['blockHash']
52
+ end
53
+
48
54
  def confirmations
49
55
  return 0 unless mined?
50
56
 
@@ -1,3 +1,3 @@
1
1
  module Etherlite
2
- VERSION = "0.3.0"
2
+ VERSION = "0.5.1"
3
3
  end
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.3.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-24 00:00:00.000000000 Z
11
+ date: 2021-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: digest-sha3