tezos_client 0.4.8 → 0.4.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c91c8a1a25ddbd83a4e012ded7f1a05a2fdc97c08218f6b91affcf5ecf67551c
4
- data.tar.gz: 76ca606aa726bb45322b9967c0ac8c3719bb7cd605b58e9d10a68982f4be2ca1
3
+ metadata.gz: d68c46e8ce54db3ab5deaaec705d5bb3f746706aaf35f592687f5282bf847225
4
+ data.tar.gz: f5d7effdcd2a84cebf396b2901189371bc9e2f47f092aa23fbae3bdcccd764c5
5
5
  SHA512:
6
- metadata.gz: a14e2ec8094a5a882319291ae10b09b3a663ac13c6fad6130f00f48b61b77b067a4c6ce8ce90d2289a5613724a33f42c17225dfda689195d5551592f22b5664c
7
- data.tar.gz: 7f44696344a7197c8d32716e8f4ab2bbc43b6176802815d588dd4adbe7062f4be17adeb5b8cda1e7c223ba3d268a58f7cf2789b53b19257b3e9a93c6682b8328
6
+ metadata.gz: a6edec149c2d3b5390cc97c761015511e6012b5524d5dfeb0cb3a68965c28b47af5780690e66ec8b6ef8f9e67c914b2527c26b7e413bb1a7f69f2d42db2ddd07
7
+ data.tar.gz: 22c34b38fd4cd477a4c4687a5db02ed0bce9fa0f70daae778ea78fbc6bca505836dd1ea6768a644f270807e33269efb54a02dfa3ec0eb9e57ad63ca8c1f5b079
data/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tezos_client (0.4.8)
4
+ tezos_client (0.4.9)
5
5
  activesupport (~> 5.2.3)
6
6
  base58 (~> 0.2.3)
7
7
  bip_mnemonic (~> 0.0.2)
8
- httparty (~> 0.16.2)
8
+ httparty (~> 0.17.0)
9
9
  money-tree (~> 0.10.0)
10
10
  rbnacl (~> 5.0.0)
11
11
  rest-client (~> 2.0.0)
@@ -49,7 +49,7 @@ GEM
49
49
  hashdiff (1.0.0)
50
50
  http-cookie (1.0.3)
51
51
  domain_name (~> 0.5)
52
- httparty (0.16.4)
52
+ httparty (0.17.1)
53
53
  mime-types (~> 3.0)
54
54
  multi_xml (>= 0.5.2)
55
55
  i18n (1.6.0)
@@ -17,7 +17,8 @@ class TezosClient
17
17
  :blocks,
18
18
  :balance,
19
19
  :contract_manager_key,
20
- :contract_storage
20
+ :contract_storage,
21
+ :pending_operations
21
22
 
22
23
  def_delegators :client_interface,
23
24
  :gen_keys,
@@ -28,6 +28,18 @@ class TezosClient
28
28
  end
29
29
  end
30
30
 
31
+ class PreviouslyRevealedKey < RpcRequestFailure
32
+ attr_reader :contract
33
+
34
+ def initialize(error:, **_args)
35
+ @contract = error[:contract]
36
+
37
+ @message = "Previously revealed key for address #{contract}"
38
+
39
+ super
40
+ end
41
+ end
42
+
31
43
  class OperationFailure < Exception
32
44
  include Logger
33
45
 
@@ -37,9 +37,9 @@ class TezosClient
37
37
  def simulate_and_update_limits
38
38
  run_result = run
39
39
 
40
- run_result[:operations_result].zip(rpc_operation_args) do |operation_result, rpc_operation_args|
40
+ run_result[:operation_results].zip(rpc_operation_args) do |operation_result, rpc_operation_args|
41
41
  if rpc_operation_args.key?(:gas_limit)
42
- rpc_operation_args[:gas_limit] = (operation_result[:consumed_gas] + 0.001).to_satoshi.to_s
42
+ rpc_operation_args[:gas_limit] = (operation_result[:consumed_gas].to_i.from_satoshi + 0.001).to_satoshi.to_s
43
43
  end
44
44
  end
45
45
  end
@@ -73,16 +73,25 @@ class TezosClient
73
73
  @signed_hex
74
74
  end
75
75
 
76
- def test_and_broadcast
76
+ def simulate
77
77
  # simulate operations and adjust gas limits
78
78
  simulate_and_update_limits
79
+ res = preapply
80
+ operation_results = res.map { |metadata| metadata[:operation_result] }
81
+
82
+ {
83
+ operation_results: operation_results,
84
+ }
85
+ end
86
+
87
+ def test_and_broadcast
88
+ simulate_res = simulate
79
89
 
80
- operations_result = preapply
81
90
 
82
91
  op_id = broadcast
83
92
  {
84
93
  operation_id: op_id,
85
- operations_result: operations_result,
94
+ operation_results: simulate_res[:operation_results],
86
95
  }
87
96
  end
88
97
 
@@ -97,19 +106,19 @@ class TezosClient
97
106
 
98
107
  ensure_applied!(rpc_responses)
99
108
 
100
- operations_result = rpc_responses.map do |rpc_response|
109
+ operation_results = rpc_responses.map do |rpc_response|
101
110
  metadata = rpc_response[:metadata]
102
111
  total_consumed_storage += compute_consumed_storage(metadata)
103
112
  consumed_gas = compute_consumed_gas(metadata)
104
113
  total_consumed_gas += consumed_gas
105
- { result: metadata[:operation_result], consumed_gas: consumed_gas }
114
+ metadata[:operation_result]
106
115
  end
107
116
 
108
117
  {
109
118
  status: :applied,
110
119
  consumed_gas: total_consumed_gas,
111
120
  consumed_storage: total_consumed_storage,
112
- operations_result: operations_result
121
+ operation_results: operation_results
113
122
  }
114
123
  end
115
124
 
@@ -146,13 +155,14 @@ class TezosClient
146
155
  private
147
156
 
148
157
  def ensure_applied!(rpc_responses)
149
- operation_results = rpc_responses.map { |response| response[:metadata][:operation_result] }
158
+ metadatas = rpc_responses.map { |response| response[:metadata] }
159
+ operation_results = metadatas.map { |metadata| metadata[:operation_result] }
150
160
 
151
161
  failed = operation_results.detect do |operation_result|
152
162
  operation_result != nil && operation_result[:status] != "applied"
153
163
  end
154
164
 
155
- return operation_results if failed.nil?
165
+ return metadatas if failed.nil?
156
166
 
157
167
  failed_operation_result = operation_results.detect do |operation_result|
158
168
  operation_result[:status] == "failed"
@@ -1,6 +1,6 @@
1
1
  class TezosClient
2
2
  class Operation
3
- delegate :run, :preapply, :test_and_broadcast, :signed_hex, to: :operation_mgr
3
+ delegate :run, :preapply, :test_and_broadcast, :simulate, :signed_hex, to: :operation_mgr
4
4
 
5
5
  def initialize(rpc_interface:, **args)
6
6
  @rpc_interface = rpc_interface
@@ -1,32 +1,21 @@
1
1
  class TezosClient
2
- class OperationArray < Operation
2
+ class OperationArray < RawOperationArray
3
3
 
4
4
  def post_initialize(operations:, **args)
5
- @operations = operations.clone
6
- end
7
-
8
- def rpc_operation_args
9
- @rpc_operation_args ||= begin
10
- initial_counter = rpc_interface.contract_counter(@args.fetch(:from)) + 1
11
-
12
- operations.map.with_index do |operation, index|
13
- counter = (initial_counter + index)
14
- operation_kind = operation.delete(:kind)
15
-
16
- operation_klass(operation_kind).new(
17
- operation
18
- .merge(@args.slice(:from))
19
- .merge(
20
- rpc_interface: rpc_interface,
21
- counter: counter
22
- )
23
- ).rpc_operation_args
24
- end
5
+ @raw_operations = operations.map do |operation|
6
+ operation_kind = operation.delete(:kind)
7
+ operation_klass(operation_kind).new(
8
+ operation.merge(
9
+ from: @args.fetch(:from),
10
+ rpc_interface: rpc_interface,
11
+ counter: 0 # will be set by raw_operation_array
12
+ )
13
+ ).rpc_operation_args
25
14
  end
26
15
  end
27
16
 
28
17
  private
29
- attr_reader :operations
18
+
30
19
 
31
20
  def operation_klass(operation_name)
32
21
  "tezos_client/#{operation_name}_operation".camelize.constantize
@@ -0,0 +1,24 @@
1
+ class TezosClient
2
+ class RawOperationArray < Operation
3
+
4
+ def post_initialize(raw_operations:, **args)
5
+ @raw_operations = raw_operations.clone
6
+ end
7
+
8
+ def rpc_operation_args
9
+ @rpc_operation_args ||= begin
10
+ initial_counter = rpc_interface.contract_counter(@args.fetch(:from)) + 1
11
+
12
+ raw_operations.map.with_index do |operation, index|
13
+ counter = (initial_counter + index)
14
+ operation.merge(
15
+ counter: counter.to_s
16
+ )
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+ attr_reader :raw_operations
23
+ end
24
+ end
@@ -3,9 +3,11 @@ class TezosClient
3
3
  include EncodeUtils
4
4
 
5
5
  def rpc_operation_args
6
- rpc_interface.transaction_operation(
7
- operation_args
8
- )
6
+ @rpc_operation_args ||= begin
7
+ rpc_interface.transaction_operation(
8
+ operation_args
9
+ )
10
+ end
9
11
  end
10
12
 
11
13
  private
@@ -27,7 +27,7 @@ class TezosClient
27
27
  kind: "origination",
28
28
  delegatable: args.fetch(:delegatable, false),
29
29
  spendable: args.fetch(:spendable, false),
30
- balance: args.fetch(:amount,0).to_satoshi.to_s,
30
+ balance: args.fetch(:amount, 0).to_satoshi.to_s,
31
31
  source: args.fetch(:from),
32
32
  gas_limit: args.fetch(:gas_limit, 0.1).to_satoshi.to_s,
33
33
  storage_limit: args.fetch(:storage_limit, 0.06).to_satoshi.to_s,
@@ -39,6 +39,10 @@ class TezosClient
39
39
  def broadcast_operation(data)
40
40
  post("injection/operation?chain=main", data)
41
41
  end
42
+
43
+ def pending_operations
44
+ get("chains/main/mempool/pending_operations")
45
+ end
42
46
  end
43
47
  end
44
48
  end
@@ -5,13 +5,15 @@ class TezosClient
5
5
  module RequestManager
6
6
  def get(path, query: {})
7
7
  url = "http://#{@host}:#{@port}/#{path}"
8
-
9
- response = HTTParty.get(url, headers: { "Content-Type" => "application/json" }, query: query)
8
+ response = nil
9
+ exec_time = Benchmark.realtime do
10
+ response = HTTParty.get(url, headers: { "Content-Type" => "application/json" }, query: query)
11
+ end
10
12
  formatted_response = format_response(response.parsed_response)
11
13
 
12
14
  log("-------")
13
15
  log(">>> GET #{response.request.uri.to_s} \n")
14
- log("<<< code: #{response.code} \n #{tezos_contents_log(formatted_response)}")
16
+ log("<<< code: #{response.code} \n exec time: #{exec_time}\n #{tezos_contents_log(formatted_response)}")
15
17
  log("-------")
16
18
  unless response.success?
17
19
  failed!(url: url, code: response.code, responses: formatted_response)
@@ -22,15 +24,19 @@ class TezosClient
22
24
 
23
25
  def post(path, content)
24
26
  url = "http://#{@host}:#{@port}/#{path}"
25
- response = HTTParty.post(url,
26
- body: content.to_json,
27
- headers: { "Content-Type" => "application/json" })
27
+
28
+ response = nil
29
+ exec_time = Benchmark.realtime do
30
+ response = HTTParty.post(url,
31
+ body: content.to_json,
32
+ headers: { "Content-Type" => "application/json" })
33
+ end
28
34
 
29
35
  formatted_response = format_response(response.parsed_response)
30
36
 
31
37
  log("-------")
32
38
  log(">>> POST #{url} \n #{tezos_contents_log(content)}")
33
- log("<<< code: #{response.code} \n #{tezos_contents_log(formatted_response)}")
39
+ log("<<< code: #{response.code} \n exec time: #{exec_time} \n #{tezos_contents_log(formatted_response)}")
34
40
  log("-------")
35
41
 
36
42
  unless response.success?
@@ -71,15 +77,17 @@ class TezosClient
71
77
 
72
78
  def exception_klass(error)
73
79
  case get_error_id(error)
74
- when "proto.003-PsddFKi3.operation.invalid_activation"
80
+ when /proto\.[^.]*\.operation\.invalid_activation/
75
81
  TezosClient::InvalidActivation
82
+ when /proto\.[^.]*\.contract\.previously_revealed_key/
83
+ TezosClient::PreviouslyRevealedKey
76
84
  else
77
85
  TezosClient::RpcRequestFailure
78
86
  end
79
87
  end
80
88
 
81
89
  def failed!(url:, code:, responses:)
82
- error = responses[0]
90
+ error = responses.is_a?(String) ? responses : responses[0]
83
91
  raise exception_klass(error).new(
84
92
  error: error,
85
93
  url: url,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TezosClient
4
- VERSION = "0.4.8"
4
+ VERSION = "0.4.9"
5
5
  end
data/lib/tezos_client.rb CHANGED
@@ -5,6 +5,7 @@ require "active_support/core_ext/hash/indifferent_access"
5
5
  require "active_support/core_ext/string/inflections"
6
6
  require "active_support/core_ext/module/delegation"
7
7
  require "timeout"
8
+ require "benchmark"
8
9
 
9
10
  require "tezos_client/version"
10
11
  require "tezos_client/string_utils"
@@ -21,6 +22,7 @@ require "tezos_client/operations/transaction_operation"
21
22
  require "tezos_client/operations/transactions_operation"
22
23
  require "tezos_client/operations/activate_account_operation"
23
24
  require "tezos_client/operations/reveal_operation"
25
+ require "tezos_client/operations/raw_operation_array"
24
26
  require "tezos_client/operations/operation_array"
25
27
 
26
28
  require "tezos_client/client_interface"
@@ -78,7 +80,7 @@ class TezosClient
78
80
  #
79
81
  # @return [Hash] result of the origination containing :operation_id, :operation_result and :originated_contract
80
82
  #
81
- def originate_contract(from:, amount:, secret_key:, script: nil, init_params: nil, **args)
83
+ def originate_contract(from:, amount:, secret_key:, script: nil, init_params: nil, dry_run: false, **args)
82
84
  origination_args = {
83
85
  rpc_interface: rpc_interface,
84
86
  from: from,
@@ -95,9 +97,12 @@ class TezosClient
95
97
  )
96
98
  end
97
99
 
98
- res = OriginationOperation.new(origination_args).test_and_broadcast
100
+ operation = OriginationOperation.new(origination_args)
101
+ res = broadcast_operation(operation: operation, dry_run: dry_run)
99
102
 
100
- res.merge(originated_contract: res[:operations_result][0][:originated_contracts][0])
103
+ res.merge(
104
+ originated_contract: res[:operation_results][0][:originated_contracts][0]
105
+ )
101
106
  end
102
107
 
103
108
  # Transfer funds to an account
@@ -110,51 +115,59 @@ class TezosClient
110
115
  #
111
116
  # @return [Hash] result of the transfer containing :operation_id and :operation_result
112
117
  #
113
- def transfer(from:, amount:, to:, secret_key:, **args)
114
- TransactionOperation.new(
118
+ def transfer(from:, amount:, to:, secret_key:, dry_run: false, **args)
119
+ operation = TransactionOperation.new(
115
120
  rpc_interface: rpc_interface,
116
121
  from: from,
117
122
  to: to,
118
123
  secret_key: secret_key,
119
124
  amount: amount,
120
125
  **args
121
- ).test_and_broadcast
126
+ )
127
+
128
+ broadcast_operation(operation: operation, dry_run: dry_run)
122
129
  end
123
130
 
124
- def activate_account(pkh:, secret:, **args)
125
- ActivateAccountOperation.new(
131
+ def activate_account(pkh:, secret:, dry_run: false, **args)
132
+ operation = ActivateAccountOperation.new(
126
133
  rpc_interface: rpc_interface,
127
134
  pkh: pkh,
128
135
  secret: secret,
129
136
  **args
130
- ).test_and_broadcast
137
+ )
138
+
139
+ broadcast_operation(operation: operation, dry_run: dry_run)
131
140
  end
132
141
 
133
- def transfer_to_many(from:, amounts:, secret_key:, **args)
134
- TransactionsOperation.new(
142
+ def transfer_to_many(from:, amounts:, secret_key:, dry_run: false, **args)
143
+ operation = TransactionsOperation.new(
135
144
  rpc_interface: rpc_interface,
136
145
  from: from,
137
146
  amounts: amounts,
138
147
  secret_key: secret_key,
139
148
  **args
140
- ).test_and_broadcast
149
+ )
150
+
151
+ broadcast_operation(operation: operation, dry_run: dry_run)
141
152
  end
142
153
 
143
- def reveal_pubkey(secret_key:, **args)
154
+ def reveal_pubkey(secret_key:, dry_run: false, **args)
144
155
  public_key = secret_key_to_public_key(secret_key)
145
156
  from = public_key_to_address(public_key)
146
157
 
147
- RevealOperation.new(
158
+ operation = RevealOperation.new(
148
159
  liquidity_interface: liquidity_interface,
149
160
  rpc_interface: rpc_interface,
150
161
  public_key: public_key,
151
162
  from: from,
152
163
  secret_key: secret_key,
153
164
  **args
154
- ).test_and_broadcast
165
+ )
166
+
167
+ broadcast_operation(operation: operation, dry_run: dry_run)
155
168
  end
156
169
 
157
- def call_contract(args)
170
+ def call_contract(dry_run: false, **args)
158
171
  parameters = args.fetch(:parameters)
159
172
 
160
173
  json_params = liquidity_interface.call_parameters(
@@ -162,11 +175,28 @@ class TezosClient
162
175
  parameters: parameters
163
176
  )
164
177
 
165
- transfer_args = args.merge(parameters: json_params)
178
+ transfer_args = args.merge(parameters: json_params, dry_run: dry_run)
166
179
 
167
180
  transfer(transfer_args)
168
181
  end
169
182
 
183
+ def inject_raw_operations(secret_key:, raw_operations:, dry_run: false, **args)
184
+ public_key = secret_key_to_public_key(secret_key)
185
+ from = public_key_to_address(public_key)
186
+
187
+ operation = RawOperationArray.new(
188
+ liquidity_interface: liquidity_interface,
189
+ rpc_interface: rpc_interface,
190
+ public_key: public_key,
191
+ from: from,
192
+ secret_key: secret_key,
193
+ raw_operations: raw_operations,
194
+ **args
195
+ )
196
+
197
+ broadcast_operation(operation: operation, dry_run: dry_run)
198
+ end
199
+
170
200
  def monitor_operation(operation_id, timeout: 120)
171
201
  including_block = nil
172
202
 
@@ -204,4 +234,18 @@ class TezosClient
204
234
  operations.flatten.include? operation_id
205
235
  end
206
236
 
237
+ private
238
+
239
+ def broadcast_operation(operation:, dry_run:)
240
+ res = if dry_run
241
+ operation.simulate
242
+ else
243
+ operation.test_and_broadcast
244
+ end
245
+
246
+ res.merge(
247
+ rpc_operation_args: operation.rpc_operation_args
248
+ )
249
+ end
250
+
207
251
  end
data/tezos_client.gemspec CHANGED
@@ -41,7 +41,7 @@ Gem::Specification.new do |spec|
41
41
  spec.add_development_dependency "vcr", "~> 4.0.0"
42
42
 
43
43
  spec.add_dependency "base58", "~> 0.2.3"
44
- spec.add_dependency "httparty", "~> 0.16.2"
44
+ spec.add_dependency "httparty", "~> 0.17.0"
45
45
  spec.add_dependency "rbnacl", "~> 5.0.0"
46
46
  spec.add_dependency "rest-client", "~>2.0.0"
47
47
  spec.add_dependency "activesupport", "~> 5.2.3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tezos_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Michard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-19 00:00:00.000000000 Z
11
+ date: 2019-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.16.2
117
+ version: 0.17.0
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.16.2
124
+ version: 0.17.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rbnacl
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -232,6 +232,7 @@ files:
232
232
  - lib/tezos_client/operations/operation.rb
233
233
  - lib/tezos_client/operations/operation_array.rb
234
234
  - lib/tezos_client/operations/origination_operation.rb
235
+ - lib/tezos_client/operations/raw_operation_array.rb
235
236
  - lib/tezos_client/operations/reveal_operation.rb
236
237
  - lib/tezos_client/operations/transaction_operation.rb
237
238
  - lib/tezos_client/operations/transactions_operation.rb