tezos_client 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,17 @@
1
1
  class TezosClient
2
2
  class ActivateAccountOperation < Operation
3
3
 
4
- def initialize_operation_args
5
- @operation_args = default_args.merge(
6
- **@init_args,
7
- operation_kind: operation_kind,
8
- branch: branch)
4
+ def rpc_operation_args
5
+ @rpc_operation_args ||= rpc_interface.activate_account_operation(
6
+ operation_args
7
+ )
9
8
  end
10
9
 
11
- def operation_kind
12
- :activate_account
10
+ def operation_args
11
+ {
12
+ pkh: @args.fetch(:pkh),
13
+ secret: @args.fetch(:secret)
14
+ }
13
15
  end
14
-
15
- def ensure_applied!(rpc_response)
16
- balance_updates = rpc_response[:metadata][:balance_updates]
17
- raise "Operation failed\n #{rpc_response.pretty_inspect}" if balance_updates.nil?
18
- if block_given?
19
- yield rpc_response[:metadata]
20
- else
21
- rpc_response[:metadata]
22
- end
23
- end
24
-
25
- private
26
-
27
- def default_args
28
- {
29
- gas_limit: 0.1,
30
- storage_limit: 0.006,
31
- fee: 0.05
32
- }
33
- end
34
16
  end
35
17
  end
@@ -0,0 +1,33 @@
1
+ class TezosClient
2
+ class Operation
3
+ delegate :run, :preapply, :test_and_broadcast, :signed_hex, to: :operation_mgr
4
+
5
+ def initialize(rpc_interface:, **args)
6
+ @rpc_interface = rpc_interface
7
+ @args = args.clone
8
+ post_initialize(**args)
9
+ end
10
+
11
+ protected
12
+
13
+ attr_reader :rpc_interface, :liquidity_interface
14
+
15
+ def rpc_operation_args
16
+ raise NotImplementedError, "#{__method__} is a virtual method"
17
+ end
18
+
19
+ def post_initialize(*_args)
20
+ end
21
+
22
+ def operation_mgr
23
+ @operation_mgr ||= OperationMgr.new(
24
+ rpc_interface: rpc_interface,
25
+ rpc_operation_args: rpc_operation_args,
26
+ **operation_options)
27
+ end
28
+
29
+ def operation_options
30
+ @args.slice(:secret_key, :protocol, :branch)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ class TezosClient
2
+ class OperationArray < Operation
3
+
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
25
+ end
26
+ end
27
+
28
+ private
29
+ attr_reader :operations
30
+
31
+ def operation_klass(operation_name)
32
+ "tezos_client/#{operation_name}_operation".camelize.constantize
33
+ end
34
+ end
35
+ end
@@ -1,50 +1,19 @@
1
1
  class TezosClient
2
2
  class OriginationOperation < Operation
3
-
4
- def initialize_operation_args
5
- operation_args = default_args.merge(
6
- **@init_args,
7
- manager: manager,
8
- operation_kind: operation_kind,
9
- branch: branch,
10
- counter: counter)
11
-
12
- if has_script?
13
- operation_args[:script] = json_script
14
- end
15
- @operation_args = operation_args
16
- end
17
-
18
- def has_script?
19
- @init_args.key? :script
20
- end
21
-
22
- def json_script
23
- liquidity_interface.origination_script(
24
- @init_args.slice(:from, :script, :init_params)
3
+ def rpc_operation_args
4
+ @rpc_operation_args ||= rpc_interface.origination_operation(
5
+ @args.slice(
6
+ :delegatable,
7
+ :spendable,
8
+ :amount,
9
+ :from,
10
+ :gas_limit,
11
+ :storage_limit,
12
+ :fee,
13
+ :counter,
14
+ :script
15
+ )
25
16
  )
26
17
  end
27
-
28
- def manager
29
- @init_args.fetch(:manager) { @init_args.fetch(:from) }
30
- end
31
-
32
-
33
- def operation_kind
34
- :origination
35
- end
36
-
37
- private
38
-
39
- def default_args
40
- {
41
- amount: 0,
42
- spendable: false,
43
- delegatable: false,
44
- gas_limit: 0.1,
45
- storage_limit: 0.06,
46
- fee: 0.05
47
- }
48
- end
49
18
  end
50
19
  end
@@ -1,28 +1,9 @@
1
1
  class TezosClient
2
2
  class RevealOperation < Operation
3
-
4
- def initialize_operation_args
5
- @operation_args = default_args.merge(
6
- **@init_args,
7
- operation_kind: operation_kind,
8
- branch: branch,
9
- counter: counter
3
+ def rpc_operation_args
4
+ @rpc_operation_args ||= rpc_interface.reveal_operation(
5
+ @args.slice(:from, :fee, :gas_limit, :storage_limit, :counter, :public_key)
10
6
  )
11
7
  end
12
-
13
-
14
- def operation_kind
15
- :reveal
16
- end
17
-
18
- private
19
-
20
- def default_args
21
- {
22
- gas_limit: 0.1,
23
- storage_limit: 0,
24
- fee: 0.05
25
- }
26
- end
27
8
  end
28
9
  end
@@ -2,38 +2,35 @@ class TezosClient
2
2
  class TransactionOperation < Operation
3
3
  include EncodeUtils
4
4
 
5
- def initialize_operation_args
6
- @operation_args = default_args.merge(
7
- **@init_args,
8
- operation_kind: operation_kind,
9
- branch: branch,
10
- counter: counter
5
+ def rpc_operation_args
6
+ rpc_interface.transaction_operation(
7
+ operation_args
11
8
  )
12
- if has_parameters?
13
- @operation_args[:parameters] = parameters
14
- end
15
9
  end
16
10
 
17
- def has_parameters?
18
- @init_args.key? :parameters
19
- end
11
+ private
20
12
 
21
- def parameters
22
- (@init_args[:parameters].is_a? String) ? encode_args(@init_args[:parameters]) : @init_args[:parameters]
23
- end
13
+ def operation_args
14
+ operation_args = @args.slice(
15
+ :amount,
16
+ :from,
17
+ :to,
18
+ :gas_limit,
19
+ :storage_limit,
20
+ :fee,
21
+ :counter
22
+ )
23
+ operation_args[:parameters] = parameters if has_parameters?
24
24
 
25
- def operation_kind
26
- :transaction
25
+ operation_args
27
26
  end
28
27
 
29
- private
28
+ def has_parameters?
29
+ @args.key? :parameters
30
+ end
30
31
 
31
- def default_args
32
- {
33
- gas_limit: 0.1,
34
- storage_limit: 0.006,
35
- fee: 0.05
36
- }
32
+ def parameters
33
+ (@args[:parameters].is_a? String) ? encode_args(@args[:parameters]) : @args[:parameters]
37
34
  end
38
35
  end
39
36
  end
@@ -1,29 +1,31 @@
1
1
  class TezosClient
2
2
  class TransactionsOperation < Operation
3
- include EncodeUtils
4
-
5
- def initialize_operation_args
6
- @operation_args = default_args.merge(
7
- **@init_args,
8
- operation_kind: operation_kind,
9
- branch: branch,
10
- counter: counter
11
- )
3
+ def rpc_operation_args
4
+ @rpc_operation_args ||= begin
5
+ OperationArray.new(
6
+ operations: operations,
7
+ secret_key: @args.fetch(:secret_key),
8
+ from: @args.fetch(:from),
9
+ rpc_interface: rpc_interface,
10
+ liquidity_interface: liquidity_interface
11
+ ).rpc_operation_args
12
+ end
12
13
  end
13
14
 
14
-
15
- def operation_kind
16
- :transactions
17
- end
18
-
19
- private
20
-
21
- def default_args
22
- {
23
- gas_limit: 0.1,
24
- storage_limit: 0.006,
25
- fee: 0.05
26
- }
15
+ def operations
16
+ @args[:amounts].map do |destination, amount|
17
+ {
18
+ kind: :transaction,
19
+ amount: amount,
20
+ to: destination
21
+ }.merge(
22
+ @args.slice(
23
+ :gas_limit,
24
+ :storage_limit,
25
+ :fee
26
+ )
27
+ )
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -7,6 +7,7 @@ require_relative "rpc_interface/monitor"
7
7
  require_relative "rpc_interface/contracts"
8
8
  require_relative "rpc_interface/context"
9
9
  require_relative "rpc_interface/helper"
10
+ require_relative "rpc_interface/operations"
10
11
  require_relative "rpc_interface/request_manager"
11
12
  require_relative "rpc_interface/blocks"
12
13
 
@@ -17,6 +18,7 @@ class TezosClient
17
18
  include Contracts
18
19
  include Context
19
20
  include Helper
21
+ include Operations
20
22
  include RequestManager
21
23
  include Blocks
22
24
 
@@ -11,42 +11,29 @@ class TezosClient
11
11
  amount: args.fetch(:amount).to_satoshi.to_s,
12
12
  source: args.fetch(:from),
13
13
  destination: args.fetch(:to),
14
- gas_limit: args.fetch(:gas_limit).to_satoshi.to_s,
15
- storage_limit: args.fetch(:storage_limit).to_satoshi.to_s,
16
- counter: args.fetch(:counter).to_s,
17
- fee: args.fetch(:fee).to_satoshi.to_s
14
+ gas_limit: args.fetch(:gas_limit, 0.1).to_satoshi.to_s,
15
+ storage_limit: args.fetch(:storage_limit, 0.006).to_satoshi.to_s,
16
+ counter: counter(args).to_s,
17
+ fee: args.fetch(:fee, 0.05).to_satoshi.to_s
18
18
  }
19
19
  operation[:parameters] = args[:parameters] if args[:parameters]
20
20
  operation
21
21
  end
22
22
 
23
- def transactions_operation(args)
24
- args[:amounts].map.with_index do |(destination, amount), index|
25
- {
26
- kind: "transaction",
27
- amount: amount.to_satoshi.to_s,
28
- source: args.fetch(:from),
29
- destination: destination,
30
- gas_limit: args.fetch(:gas_limit).to_satoshi.to_s,
31
- storage_limit: args.fetch(:storage_limit).to_satoshi.to_s,
32
- counter: (args.fetch(:counter) + index).to_s,
33
- fee: args.fetch(:fee).to_satoshi.to_s
34
- }
35
- end
36
- end
37
-
38
23
  def origination_operation(args)
24
+ manager = (args.fetch(:manager) { args.fetch(:from) })
25
+
39
26
  operation = {
40
27
  kind: "origination",
41
- delegatable: args.fetch(:delegatable),
42
- spendable: args.fetch(:spendable),
43
- balance: args.fetch(:amount).to_satoshi.to_s,
28
+ delegatable: args.fetch(:delegatable, false),
29
+ spendable: args.fetch(:spendable, false),
30
+ balance: args.fetch(:amount,0).to_satoshi.to_s,
44
31
  source: args.fetch(:from),
45
- gas_limit: args.fetch(:gas_limit).to_satoshi.to_s,
46
- storage_limit: args.fetch(:storage_limit).to_satoshi.to_s,
47
- counter: args.fetch(:counter).to_s,
48
- fee: args.fetch(:fee).to_satoshi.to_s,
49
- managerPubkey: args.fetch(:manager)
32
+ gas_limit: args.fetch(:gas_limit, 0.1).to_satoshi.to_s,
33
+ storage_limit: args.fetch(:storage_limit, 0.06).to_satoshi.to_s,
34
+ counter: counter(args).to_s,
35
+ fee: args.fetch(:fee, 0.05).to_satoshi.to_s,
36
+ managerPubkey: manager
50
37
  }
51
38
 
52
39
  operation[:script] = args[:script] if args[:script]
@@ -66,75 +53,32 @@ class TezosClient
66
53
  {
67
54
  kind: "reveal",
68
55
  source: args.fetch(:from),
69
- fee: args.fetch(:fee).to_satoshi.to_s,
70
- counter: args.fetch(:counter).to_s,
71
- gas_limit: args.fetch(:gas_limit).to_satoshi.to_s,
72
- storage_limit: args.fetch(:storage_limit).to_satoshi.to_s,
56
+ fee: args.fetch(:fee, 0.05).to_satoshi.to_s,
57
+ counter: counter(args).to_s,
58
+ gas_limit: args.fetch(:gas_limit, 0.1).to_satoshi.to_s,
59
+ storage_limit: args.fetch(:storage_limit, 0).to_satoshi.to_s,
73
60
  public_key: args.fetch(:public_key)
74
61
  }
75
62
  end
76
63
 
77
- def operation(args)
78
- operation_kind = args.fetch(:operation_kind) { raise ArgumentError, ":operation_kind argument missing" }
79
- send("#{operation_kind}_operation", args)
80
- end
81
-
82
- %w(origination transaction).each do |operation_kind|
83
- # preapply_transaction, preapply_origination ...
84
- define_method "preapply_#{operation_kind}" do |args|
85
- preapply_operation(operation_kind: operation_kind, **args)
86
- end
87
-
88
- # run_transaction, run_origination ...
89
- define_method "run_#{operation_kind}" do |args|
90
- run_operation(operation_kind: operation_kind, **args)
91
- end
92
-
93
- # forge_transaction, forge_origination ...
94
- define_method "forge_#{operation_kind}" do |args|
95
- forge_operation(operation_kind: operation_kind, **args)
64
+ def counter(args)
65
+ args.fetch(:counter) do
66
+ contract_counter(args.fetch(:from)) + 1
96
67
  end
97
68
  end
98
69
 
99
-
100
- def preapply_operation(args)
101
- content = {
102
- protocol: args.fetch(:protocol),
103
- branch: args.fetch(:branch),
104
- contents: contents(args),
105
- signature: args.fetch(:signature)
106
- }
107
-
108
- res = post("chains/main/blocks/head/helpers/preapply/operations",
109
- [content])
110
- res[0]["contents"][0]
111
- end
112
-
113
- def run_operation(args)
114
- content = {
115
- branch: args.fetch(:branch),
116
- contents: contents(args),
117
- signature: args.fetch(:signature)
118
- }
119
- res = post("chains/main/blocks/head/helpers/scripts/run_operation", content)
120
- res["contents"][0]
121
- end
122
-
123
- def forge_operation(args)
124
- content = {
125
- branch: args.fetch(:branch),
126
- contents: contents(args)
127
- }
128
- post("chains/main/blocks/head/helpers/forge/operations", content)
70
+ def preapply_operation(operation:, **options)
71
+ res = preapply_operations(operations: [operation], **options)
72
+ res[0]
129
73
  end
130
74
 
131
- def broadcast_operation(data)
132
- post("injection/operation?chain=main", data)
75
+ def run_operation(operation:, **options)
76
+ res = run_operations(operations: [operation], **options)
77
+ res[0]
133
78
  end
134
79
 
135
- def contents(args)
136
- operation = operation(args)
137
- (operation.is_a?(Array)) ? operation : [operation]
80
+ def forge_operation(operation:, **options)
81
+ forge_operations(operations: [operation], **options)
138
82
  end
139
83
  end
140
84
  end