Class: TezosClient::Operation

Inherits:
Object
  • Object
show all
Includes:
Crypto
Defined in:
lib/tezos_client/operation.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(liquidity_interface:, rpc_interface:, **args) ⇒ Operation

Returns a new instance of Operation.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tezos_client/operation.rb', line 14

def initialize(liquidity_interface:, rpc_interface:, **args)
  @liquidity_interface = liquidity_interface
  @rpc_interface = rpc_interface
  @from = args.fetch(:from) { raise ArgumentError, "Argument :from missing" }
  @secret_key = args.fetch(:secret_key)
  @init_args = args
  @operation_args = {}
  initialize_operation_args
  @signed_operation_args_h = nil
  @rpc_args = rpc_interface.operation(@operation_args)
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/tezos_client/operation.rb', line 8

def from
  @from
end

#liquidity_interfaceObject

Returns the value of attribute liquidity_interface.



8
9
10
# File 'lib/tezos_client/operation.rb', line 8

def liquidity_interface
  @liquidity_interface
end

#operation_argsObject

Returns the value of attribute operation_args.



8
9
10
# File 'lib/tezos_client/operation.rb', line 8

def operation_args
  @operation_args
end

#rpc_argsObject

Returns the value of attribute rpc_args.



8
9
10
# File 'lib/tezos_client/operation.rb', line 8

def rpc_args
  @rpc_args
end

#rpc_interfaceObject

Returns the value of attribute rpc_interface.



8
9
10
# File 'lib/tezos_client/operation.rb', line 8

def rpc_interface
  @rpc_interface
end

Instance Method Details

#base_58_signatureObject



76
77
78
79
# File 'lib/tezos_client/operation.rb', line 76

def base_58_signature
  sign unless signed?
  @base_58_signature
end

#branchObject



34
35
36
# File 'lib/tezos_client/operation.rb', line 34

def branch
  rpc_interface.head_hash
end

#broadcastObject



141
142
143
# File 'lib/tezos_client/operation.rb', line 141

def broadcast
  rpc_interface.broadcast_operation(signed_hex)
end

#counterObject



42
43
44
# File 'lib/tezos_client/operation.rb', line 42

def counter
  @counter ||= @init_args.fetch(:counter) { remote_counter }
end

#initialize_operation_argsObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/tezos_client/operation.rb', line 26

def initialize_operation_args
  raise NotImplementedError.new("#{self.class.name}##{__method__} is an abstract method.")
end

#operation_kindObject

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/tezos_client/operation.rb', line 30

def operation_kind
  raise NotImplementedError.new("#{self.class.name}##{__method__} is an abstract method.")
end

#preapplyObject



132
133
134
135
136
137
138
139
# File 'lib/tezos_client/operation.rb', line 132

def preapply
  res = rpc_interface.preapply_operation(
    **operation_args,
    signature: base_58_signature,
    protocol: protocol)

  ensure_applied!(res)
end

#protocolObject



46
47
48
# File 'lib/tezos_client/operation.rb', line 46

def protocol
  rpc_interface.protocol
end

#remote_counterObject



38
39
40
# File 'lib/tezos_client/operation.rb', line 38

def remote_counter
  @remote_counter ||= rpc_interface.contract_counter(from) + 1
end

#runObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tezos_client/operation.rb', line 105

def run
  rpc_response = rpc_interface.run_operation(**operation_args, signature: base_58_signature)

  consumed_storage = 0
  consumed_gas = 0

  operation_result = ensure_applied!(rpc_response) do |result|
    consumed_storage = (result.dig(:operation_result, :paid_storage_size_diff) || "0").to_i.from_satoshi
    consumed_gas = (result.dig(:operation_result, :consumed_gas) || "0").to_i.from_satoshi

    unless result[:internal_operation_results].nil?
      result[:internal_operation_results].each do |internal_operation_result|
        consumed_gas += (internal_operation_result[:result][:consumed_gas] || "0").to_i.from_satoshi
      end
    end

    result[:operation_result]
  end

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

#signObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/tezos_client/operation.rb', line 61

def sign
  sign_operation(
    secret_key: @secret_key,
    operation_hex: to_hex
  ) do |base_58_signature, signed_hex, _op_id|
    @signed_operation_args_h = operation_args.hash
    @base_58_signature = base_58_signature
    @signed_hex = signed_hex
  end
end

#signed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/tezos_client/operation.rb', line 72

def signed?
  @signed_operation_args_h == operation_args.hash
end

#signed_hexObject



81
82
83
84
# File 'lib/tezos_client/operation.rb', line 81

def signed_hex
  sign unless signed?
  @signed_hex
end

#simulate_and_update_limitsObject



50
51
52
53
54
55
# File 'lib/tezos_client/operation.rb', line 50

def simulate_and_update_limits
  run_result = run

  @operation_args[:gas_limit] = run_result[:consumed_gas] + 0.01
  #@operation_args[:storage_limit] = run_result[:consumed_storage]
end

#test_and_broadcastObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tezos_client/operation.rb', line 86

def test_and_broadcast
  # https://gitlab.com/tezos/tezos/issues/376
  operation_args.merge!(counter: remote_counter)

  # simulate operations and adjust gas limits
  simulate_and_update_limits
  operation_result = preapply

  # https://gitlab.com/tezos/tezos/issues/376
  operation_args.merge!(counter: counter)

  op_id = broadcast
  {
    operation_id: op_id,
    operation_result: operation_result,
    counter: counter
  }
end

#to_hexObject



57
58
59
# File 'lib/tezos_client/operation.rb', line 57

def to_hex
  rpc_interface.forge_operation(operation_args)
end