Class: TezosClient::OperationMgr

Inherits:
Object
  • Object
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_interfaceObject

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_argsObject

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_signatureObject



68
69
70
71
# File 'lib/tezos_client/operation_mgr.rb', line 68

def base_58_signature
  sign unless signed?
  @base_58_signature
end

#branchObject



29
30
31
# File 'lib/tezos_client/operation_mgr.rb', line 29

def branch
  @branch ||= rpc_interface.head_hash
end

#broadcastObject



143
144
145
# File 'lib/tezos_client/operation_mgr.rb', line 143

def broadcast
  rpc_interface.broadcast_operation(signed_hex)
end

#compute_consumed_gas(metadata) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/tezos_client/operation_mgr.rb', line 115

def compute_consumed_gas()
  consumed_gas = (.dig(:operation_result, :consumed_gas) || "0").to_i.from_satoshi

  if .key?(:internal_operation_results)
    [: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



126
127
128
# File 'lib/tezos_client/operation_mgr.rb', line 126

def compute_consumed_storage()
  (.dig(:operation_result, :paid_storage_size_diff) || "0").to_i.from_satoshi
end

#multiple_operations?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/tezos_client/operation_mgr.rb', line 21

def multiple_operations?
  @multiple_operations
end

#preapplyObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tezos_client/operation_mgr.rb', line 131

def preapply
  rpc_responses = rpc_interface.preapply_operations(
    operations: rpc_operation_args,
    signature: base_58_signature,
    protocol: protocol,
    branch: branch)

  rpc_responses.map do |rpc_response|
    ensure_applied!(rpc_response[:metadata])
  end
end

#protocolObject



33
34
35
# File 'lib/tezos_client/operation_mgr.rb', line 33

def protocol
  @protocol ||= rpc_interface.protocol
end

#runObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tezos_client/operation_mgr.rb', line 90

def run
  rpc_responses = rpc_interface.run_operations(
    operations: rpc_operation_args,
    signature: base_58_signature,
    branch: branch)

  consumed_storage = 0
  consumed_gas = 0

  operations_result = rpc_responses.map do |rpc_response|
     = rpc_response[:metadata]
    ensure_applied!()
    consumed_storage += compute_consumed_storage()
    consumed_gas += compute_consumed_gas()
    [:operation_result]
  end

  {
    status: :applied,
    consumed_gas: consumed_gas,
    consumed_storage: consumed_storage,
    operations_result: operations_result
  }
end

#signObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/tezos_client/operation_mgr.rb', line 53

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/tezos_client/operation_mgr.rb', line 64

def signed?
  @signed_operation_args_h == rpc_operation_args.hash
end

#signed_hexObject



73
74
75
76
# File 'lib/tezos_client/operation_mgr.rb', line 73

def signed_hex
  sign unless signed?
  @signed_hex
end

#simulate_and_update_limitsObject



37
38
39
40
41
42
43
44
45
46
47
# 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].to_i + 0.001.to_satoshi).to_s
    end
  end

  run_result
end

#single_operation?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/tezos_client/operation_mgr.rb', line 25

def single_operation?
  !multiple_operations?
end

#test_and_broadcastObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tezos_client/operation_mgr.rb', line 78

def test_and_broadcast
  # simulate operations and adjust gas limits
  simulate_and_update_limits
  operations_result = preapply

  op_id = broadcast
  {
    operation_id: op_id,
    operations_result: operations_result,
  }
end

#to_hexObject



49
50
51
# File 'lib/tezos_client/operation_mgr.rb', line 49

def to_hex
  rpc_interface.forge_operations(operations: rpc_operation_args, branch: branch)
end