Class: TezosClient::Operation

Inherits:
Object
  • Object
show all
Includes:
Crypto
Defined in:
lib/tezos_client/operation.rb

Direct Known Subclasses

OriginationOperation, TransactionOperation

Constant Summary

Constants included from Crypto

Crypto::PREFIXES, Crypto::WATERMARK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Crypto

#checksum, #decode_base58, #decode_tz, #encode_base58, #encode_tz, #generate_key, #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.



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

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
  @signed = false
  @operation_args = {}
  initialize_operation_args
  @rpc_args = rpc_interface.operation(@operation_args)
end

Instance Attribute Details

#base_58_signatureObject

Returns the value of attribute base_58_signature.



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

def base_58_signature
  @base_58_signature
end

#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

#signed_hexObject

Returns the value of attribute signed_hex.



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

def signed_hex
  @signed_hex
end

Instance Method Details

#branchObject



36
37
38
# File 'lib/tezos_client/operation.rb', line 36

def branch
  rpc_interface.head_hash
end

#broadcastObject



110
111
112
113
# File 'lib/tezos_client/operation.rb', line 110

def broadcast
  raise "can not preapply unsigned operations" unless @signed
  rpc_interface.broadcast_operation(signed_hex)
end

#counterObject



40
41
42
# File 'lib/tezos_client/operation.rb', line 40

def counter
  @init_args.fetch(:counter) { rpc_interface.contract_counter(from) + 1 }
end

#initialize_operation_argsObject

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/tezos_client/operation.rb', line 28

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

#operation_kindObject

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/tezos_client/operation.rb', line 32

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

#preapplyObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/tezos_client/operation.rb', line 99

def preapply
  raise "can not preapply unsigned operations" unless @signed

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

  ensure_applied!(res)
end

#protocolObject



44
45
46
# File 'lib/tezos_client/operation.rb', line 44

def protocol
  rpc_interface.protocols[0]
end

#runObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tezos_client/operation.rb', line 83

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

  operation_result = ensure_applied!(rpc_response)

  consumed_storage = operation_result.fetch(:paid_storage_size_diff, "0").to_i.from_satoshi
  consumed_gas = operation_result.fetch(:consumed_gas, "0").to_i.from_satoshi

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

#signObject



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

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

  @signed = true
end

#simulate_and_update_limitsObject



48
49
50
51
52
53
# File 'lib/tezos_client/operation.rb', line 48

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tezos_client/operation.rb', line 71

def test_and_broadcast
  # simulate operations and adjust gas limits
  simulate_and_update_limits
  sign
  operation_result = preapply
  op_id = broadcast
  {
    operation_id: op_id,
    operation_result: operation_result
  }
end

#to_hexObject



55
56
57
# File 'lib/tezos_client/operation.rb', line 55

def to_hex
  rpc_interface.forge_operation(operation_args)
end