Class: TezosClient

Inherits:
Object
  • Object
show all
Includes:
Commands, Crypto, Logger
Defined in:
lib/tezos_client.rb,
lib/tezos_client/crypto.rb,
lib/tezos_client/logger.rb,
lib/tezos_client/version.rb,
lib/tezos_client/commands.rb,
lib/tezos_client/exceptions.rb,
lib/tezos_client/encode_utils.rb,
lib/tezos_client/string_utils.rb,
lib/tezos_client/operation_mgr.rb,
lib/tezos_client/rpc_interface.rb,
lib/tezos_client/currency_utils.rb,
lib/tezos_client/smartpy_interface.rb,
lib/tezos_client/tools/system_call.rb,
lib/tezos_client/operations/operation.rb,
lib/tezos_client/rpc_interface/blocks.rb,
lib/tezos_client/rpc_interface/helper.rb,
lib/tezos_client/tools/temporary_file.rb,
lib/tezos_client/rpc_interface/context.rb,
lib/tezos_client/rpc_interface/monitor.rb,
lib/tezos_client/rpc_interface/contracts.rb,
lib/tezos_client/rpc_interface/operations.rb,
lib/tezos_client/operations/operation_array.rb,
lib/tezos_client/operations/reveal_operation.rb,
lib/tezos_client/rpc_interface/request_manager.rb,
lib/tezos_client/operations/raw_operation_array.rb,
lib/tezos_client/compute_operation_args_counters.rb,
lib/tezos_client/operations/origination_operation.rb,
lib/tezos_client/operations/transaction_operation.rb,
lib/tezos_client/smartpy_inteface/smartpy_wrapper.rb,
lib/tezos_client/operations/transactions_operation.rb,
lib/tezos_client/operations/activate_account_operation.rb,
lib/tezos_client/smartpy_inteface/micheline_serializer_wrapper.rb

Defined Under Namespace

Modules: Commands, Crypto, CurrencyUtils, EncodeUtils, Logger, StringUtils, Tools Classes: ActivateAccountOperation, ComputeOperationArgsCounters, InvalidActivation, Operation, OperationArray, OperationFailure, OperationMgr, OriginationOperation, PreviouslyRevealedKey, RawOperationArray, RevealOperation, RpcInterface, RpcRequestFailure, ScriptRuntimeError, SmartPyError, SmartpyInterface, SysCallError, TezBalanceTooLow, TransactionOperation, TransactionsOperation

Constant Summary collapse

RANDOM_SIGNATURE =
"edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ"
VERSION =
"1.1.4"

Constants included from Crypto

Crypto::PREFIXES, Crypto::WATERMARK

Constants included from Logger

Logger::FILTERED_KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Crypto

#checksum, #decode_account_wallet, #decode_base58, #decode_tz, #edsk2_to_edsk, #encode_base58, #encode_script_expr, #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

Methods included from Logger

#log, #tezos_contents_log, #tezos_contents_log_filter

Constructor Details

#initialize(rpc_node_address: "127.0.0.1", rpc_node_port: 8732, liquidity_options: {}) ⇒ TezosClient

Returns a new instance of TezosClient.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tezos_client.rb', line 54

def initialize(rpc_node_address: "127.0.0.1", rpc_node_port: 8732, liquidity_options: {})
  @rpc_node_address = rpc_node_address
  @rpc_node_port = rpc_node_port

  @client_config_file = ENV["TEZOS_CLIENT_CONFIG_FILE"]

  @rpc_interface = RpcInterface.new(
    host: @rpc_node_address,
    port: @rpc_node_port
  )

  @smartpy_interface = SmartpyInterface.new
end

Instance Attribute Details

#rpc_interfaceObject

Returns the value of attribute rpc_interface.



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

def rpc_interface
  @rpc_interface
end

#smartpy_interfaceObject

Returns the value of attribute smartpy_interface.



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

def smartpy_interface
  @smartpy_interface
end

Class Method Details

.root_pathObject



243
244
245
# File 'lib/tezos_client.rb', line 243

def self.root_path
  File.expand_path(File.dirname(__FILE__))
end

Instance Method Details

#activate_account(pkh:, secret:, dry_run: false, **args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/tezos_client.rb', line 127

def (pkh:, secret:, dry_run: false, **args)
  operation = ActivateAccountOperation.new(
    rpc_interface: rpc_interface,
    pkh: pkh,
    secret: secret,
    **args
  )

  broadcast_operation(operation: operation, dry_run: dry_run)
end

#block_include_operation?(operation_id, block_id) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/tezos_client.rb', line 229

def block_include_operation?(operation_id, block_id)
  retries ||= 0

  operations = rpc_interface.get("chains/main/blocks/#{block_id}/operation_hashes")
  operations.flatten.include? operation_id
rescue TezosClient::RpcRequestFailure
  if (retries += 1) < 3
    sleep(2)
    retry
  else
    raise
  end
end

#call_contract(dry_run: false, entrypoint:, params:, params_type:, **args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tezos_client.rb', line 165

def call_contract(dry_run: false, entrypoint:, params:, params_type:, **args)
  json_params = micheline_params(
    params: params,
    entrypoint: entrypoint,
    params_type: params_type
  )

  transfer_args = args.merge(
    entrypoint: entrypoint,
    parameters: json_params,
    dry_run: dry_run
  )

  transfer(transfer_args)
end

#inject_raw_operations(secret_key:, raw_operations:, dry_run: false, **args) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tezos_client.rb', line 181

def inject_raw_operations(secret_key:, raw_operations:, dry_run: false, **args)
  public_key = secret_key_to_public_key(secret_key)
  from = public_key_to_address(public_key)

  operation = RawOperationArray.new(
    rpc_interface: rpc_interface,
    public_key: public_key,
    from: from,
    secret_key: secret_key,
    raw_operations: raw_operations,
    **args
  )

  broadcast_operation(operation: operation, dry_run: dry_run)
end

#monitor_operation(operation_id, timeout: 120) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/tezos_client.rb', line 197

def monitor_operation(operation_id, timeout: 120)
  including_block = nil

  monitoring_thread = rpc_interface.monitor_block do |block_header|
    log "recently received block: #{block_header.pretty_inspect}"
    hash = block_header["hash"]

    if block_include_operation?(operation_id, hash)
      log "operations #{operation_id} found in block #{hash}"
      including_block = hash
    end
  end

  Timeout.timeout(timeout) do
    loop do
      sleep(0.1)
      break unless monitoring_thread.alive?
      break unless including_block.nil?
    end
  end

  if monitoring_thread.status.nil?
    # when thread raise an Exception, reraise it
    log "monitoring thread raised an exception"
    monitoring_thread.value
  else
    monitoring_thread.terminate
  end

  including_block
end

#originate_contract(from:, amount:, secret_key: nil, script: nil, init_params: nil, dry_run: false, **args) ⇒ Hash

Originates a contract on the tezos blockchain

Parameters:

  • from (String)

    Address originating the contract

  • amount (Numeric)

    amount to send to the contract

  • secret_key (String) (defaults to: nil)

    Secret key of the origination address

  • args (Hash)

    keyword options for the origination

Options Hash (**args):

  • :script (String)

    path of the liquidity script

  • :init_params (Array, String)

    params to pass to the storage initialization process

  • :spendable (Boolean)

    decide wether the contract is spendable or not

  • :delegatable (Boolean)

    decide wether the contract is delegatable or not

Returns:

  • (Hash)

    result of the origination containing :operation_id, :operation_result and :originated_contract



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tezos_client.rb', line 81

def originate_contract(from:, amount:, secret_key: nil, script: nil, init_params: nil, dry_run: false, **args)
  origination_args = {
    rpc_interface: rpc_interface,
    from: from,
    secret_key: secret_key,
    amount: amount,
    **args
  }

  origination_args[:script] = contract_interface(script).origination_script(
    from: from,
    script: script,
    init_params: init_params
  )

  operation = OriginationOperation.new(origination_args)
  res = broadcast_operation(operation: operation, dry_run: dry_run)

  res.merge(
    originated_contract: res[:operation_results][0][:originated_contracts][0]
  )
end

#reveal_pubkey(secret_key:, dry_run: false, **args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tezos_client.rb', line 150

def reveal_pubkey(secret_key:, dry_run: false, **args)
  public_key = secret_key_to_public_key(secret_key)
  from = public_key_to_address(public_key)

  operation = RevealOperation.new(
    rpc_interface: rpc_interface,
    public_key: public_key,
    from: from,
    secret_key: secret_key,
    **args
  )

  broadcast_operation(operation: operation, dry_run: dry_run)
end

#transfer(from:, amount:, to:, secret_key:, dry_run: false, **args) ⇒ Hash

Transfer funds to an account

Parameters:

  • from (String)

    Address originating the transfer

  • to (String)

    Address receiving the transfer

  • amount (Numeric)

    amount to send to the account

  • secret_key (String)

    Secret key of the origination address

  • args (Hash)

    keyword options for the transfer

Returns:

  • (Hash)

    result of the transfer containing :operation_id and :operation_result



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

def transfer(from:, amount:, to:, secret_key:, dry_run: false, **args)
  operation = TransactionOperation.new(
    rpc_interface: rpc_interface,
    from: from,
    to: to,
    secret_key: secret_key,
    amount: amount,
    **args
  )

  broadcast_operation(operation: operation, dry_run: dry_run)
end

#transfer_to_many(from:, amounts:, secret_key:, dry_run: false, **args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/tezos_client.rb', line 138

def transfer_to_many(from:, amounts:, secret_key:, dry_run: false, **args)
  operation = TransactionsOperation.new(
    rpc_interface: rpc_interface,
    from: from,
    amounts: amounts,
    secret_key: secret_key,
    **args
  )

  broadcast_operation(operation: operation, dry_run: dry_run)
end