Class: TezosClient::OperationMgr
- Inherits:
-
Object
- Object
- TezosClient::OperationMgr
show all
- Includes:
- Crypto
- Defined in:
- lib/tezos_client/operation_mgr.rb
Constant Summary
Constants included
from Crypto
Crypto::PREFIXES, Crypto::WATERMARK
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Crypto
#checksum, #decode_account_wallet, #decode_base58, #decode_tz, #encode_base58, #encode_tz, #generate_key, #generate_mnemonic, #get_prefix_and_payload, #hex_prefix, #operation_id, #public_key_to_address, #secret_key_to_public_key, #sign_bytes, #sign_operation, #signing_key
Constructor Details
#initialize(rpc_interface:, rpc_operation_args:, **args) ⇒ OperationMgr
Returns a new instance of OperationMgr.
11
12
13
14
15
16
17
18
19
|
# File 'lib/tezos_client/operation_mgr.rb', line 11
def initialize(rpc_interface:, rpc_operation_args:, **args)
@rpc_interface = rpc_interface
@secret_key = args.fetch(:secret_key)
@multiple_operations = rpc_operation_args.is_a?(Array)
@rpc_operation_args = @multiple_operations ? rpc_operation_args : [rpc_operation_args]
@signed_operation_args_h = nil
@branch = args[:branch]
@protocol = args[:protocol]
end
|
Instance Attribute Details
#rpc_interface ⇒ Object
Returns the value of attribute rpc_interface.
8
9
10
|
# File 'lib/tezos_client/operation_mgr.rb', line 8
def rpc_interface
@rpc_interface
end
|
#rpc_operation_args ⇒ Object
Returns the value of attribute rpc_operation_args.
8
9
10
|
# File 'lib/tezos_client/operation_mgr.rb', line 8
def rpc_operation_args
@rpc_operation_args
end
|
Instance Method Details
#base_58_signature ⇒ Object
66
67
68
69
|
# File 'lib/tezos_client/operation_mgr.rb', line 66
def base_58_signature
sign unless signed?
@base_58_signature
end
|
#branch ⇒ Object
29
30
31
|
# File 'lib/tezos_client/operation_mgr.rb', line 29
def branch
@branch ||= rpc_interface.head_hash
end
|
#broadcast ⇒ Object
142
143
144
|
# File 'lib/tezos_client/operation_mgr.rb', line 142
def broadcast
rpc_interface.broadcast_operation(signed_hex)
end
|
#compute_consumed_gas(metadata) ⇒ Object
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/tezos_client/operation_mgr.rb', line 116
def compute_consumed_gas(metadata)
consumed_gas = (metadata.dig(:operation_result, :consumed_gas) || "0").to_i.from_satoshi
if metadata.key?(:internal_operation_results)
metadata[:internal_operation_results].each do |internal_operation_result|
consumed_gas += (internal_operation_result[:result][:consumed_gas] || "0").to_i.from_satoshi
end
end
consumed_gas
end
|
#compute_consumed_storage(metadata) ⇒ Object
127
128
129
|
# File 'lib/tezos_client/operation_mgr.rb', line 127
def compute_consumed_storage(metadata)
(metadata.dig(:operation_result, :paid_storage_size_diff) || "0").to_i.from_satoshi
end
|
#multiple_operations? ⇒ Boolean
21
22
23
|
# File 'lib/tezos_client/operation_mgr.rb', line 21
def multiple_operations?
@multiple_operations
end
|
#preapply ⇒ Object
132
133
134
135
136
137
138
139
140
|
# File 'lib/tezos_client/operation_mgr.rb', line 132
def preapply
rpc_responses = rpc_interface.preapply_operations(
operations: rpc_operation_args,
signature: base_58_signature,
protocol: protocol,
branch: branch)
ensure_applied!(rpc_responses)
end
|
#protocol ⇒ Object
33
34
35
|
# File 'lib/tezos_client/operation_mgr.rb', line 33
def protocol
@protocol ||= rpc_interface.protocol
end
|
#run ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/tezos_client/operation_mgr.rb', line 89
def run
rpc_responses = rpc_interface.run_operations(
operations: rpc_operation_args,
signature: base_58_signature,
branch: branch)
total_consumed_storage = 0
total_consumed_gas = 0
ensure_applied!(rpc_responses)
operations_result = rpc_responses.map do |rpc_response|
metadata = rpc_response[:metadata]
total_consumed_storage += compute_consumed_storage(metadata)
consumed_gas = compute_consumed_gas(metadata)
total_consumed_gas += consumed_gas
{ result: metadata[:operation_result], consumed_gas: consumed_gas }
end
{
status: :applied,
consumed_gas: total_consumed_gas,
consumed_storage: total_consumed_storage,
operations_result: operations_result
}
end
|
#sign ⇒ Object
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/tezos_client/operation_mgr.rb', line 51
def sign
sign_operation(
secret_key: @secret_key,
operation_hex: to_hex
) do |base_58_signature, signed_hex, _op_id|
@signed_operation_args_h = rpc_operation_args.hash
@base_58_signature = base_58_signature
@signed_hex = signed_hex
end
end
|
#signed? ⇒ Boolean
62
63
64
|
# File 'lib/tezos_client/operation_mgr.rb', line 62
def signed?
@signed_operation_args_h == rpc_operation_args.hash
end
|
#signed_hex ⇒ Object
71
72
73
74
|
# File 'lib/tezos_client/operation_mgr.rb', line 71
def signed_hex
sign unless signed?
@signed_hex
end
|
#simulate_and_update_limits ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/tezos_client/operation_mgr.rb', line 37
def simulate_and_update_limits
run_result = run
run_result[:operations_result].zip(rpc_operation_args) do |operation_result, rpc_operation_args|
if rpc_operation_args.key?(:gas_limit)
rpc_operation_args[:gas_limit] = (operation_result[:consumed_gas] + 0.001).to_satoshi.to_s
end
end
end
|
#single_operation? ⇒ Boolean
25
26
27
|
# File 'lib/tezos_client/operation_mgr.rb', line 25
def single_operation?
!multiple_operations?
end
|
#test_and_broadcast ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/tezos_client/operation_mgr.rb', line 76
def test_and_broadcast
simulate_and_update_limits
operations_result = preapply
op_id = broadcast
{
operation_id: op_id,
operations_result: operations_result,
}
end
|
#to_hex ⇒ Object
47
48
49
|
# File 'lib/tezos_client/operation_mgr.rb', line 47
def to_hex
rpc_interface.forge_operations(operations: rpc_operation_args, branch: branch)
end
|