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/tools/convert_to_hash.rb,
lib/tezos_client/rpc_interface/contracts.rb,
lib/tezos_client/rpc_interface/operations.rb,
lib/tezos_client/tools/convert_to_hash/int.rb,
lib/tezos_client/tools/convert_to_hash/key.rb,
lib/tezos_client/tools/convert_to_hash/map.rb,
lib/tezos_client/tools/convert_to_hash/nat.rb,
lib/tezos_client/tools/convert_to_hash/set.rb,
lib/tezos_client/operations/operation_array.rb,
lib/tezos_client/tools/convert_to_hash/base.rb,
lib/tezos_client/tools/convert_to_hash/list.rb,
lib/tezos_client/tools/convert_to_hash/pair.rb,
lib/tezos_client/operations/reveal_operation.rb,
lib/tezos_client/tools/convert_to_hash/bytes.rb,
lib/tezos_client/tools/hash_to_micheline/int.rb,
lib/tezos_client/tools/hash_to_micheline/key.rb,
lib/tezos_client/tools/hash_to_micheline/nat.rb,
lib/tezos_client/tools/convert_to_hash/option.rb,
lib/tezos_client/tools/convert_to_hash/string.rb,
lib/tezos_client/tools/hash_to_micheline/base.rb,
lib/tezos_client/tools/hash_to_micheline/pair.rb,
lib/tezos_client/rpc_interface/request_manager.rb,
lib/tezos_client/tools/convert_to_hash/address.rb,
lib/tezos_client/tools/convert_to_hash/big_map.rb,
lib/tezos_client/tools/hash_to_micheline/bytes.rb,
lib/tezos_client/operations/raw_operation_array.rb,
lib/tezos_client/tools/convert_to_hash/key_hash.rb,
lib/tezos_client/tools/hash_to_micheline/option.rb,
lib/tezos_client/tools/hash_to_micheline/string.rb,
lib/tezos_client/compute_operation_args_counters.rb,
lib/tezos_client/tools/convert_to_hash/signature.rb,
lib/tezos_client/tools/convert_to_hash/timestamp.rb,
lib/tezos_client/tools/hash_to_micheline/address.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/tools/hash_to_micheline/contract.rb,
lib/tezos_client/operations/transactions_operation.rb,
lib/tezos_client/tools/hash_to_micheline/signature.rb,
lib/tezos_client/tools/hash_to_micheline/timestamp.rb,
lib/tezos_client/operations/activate_account_operation.rb

Defined Under Namespace

Modules: Commands, Crypto, CurrencyUtils, EncodeUtils, Logger, StringUtils, Tools Classes: ActivateAccountOperation, BadSignatureError, BigMap, 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.4.2"

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

#check_signature, #check_signature!, #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.



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

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.



53
54
55
# File 'lib/tezos_client.rb', line 53

def rpc_interface
  @rpc_interface
end

#smartpy_interfaceObject

Returns the value of attribute smartpy_interface.



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

def smartpy_interface
  @smartpy_interface
end

Class Method Details

.root_pathObject



264
265
266
# File 'lib/tezos_client.rb', line 264

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

Instance Method Details

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



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

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)


250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/tezos_client.rb', line 250

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tezos_client.rb', line 169

def call_contract(dry_run: false, entrypoint:, params:, params_type:, **args)
  _entrypoint = select_entrypoint(
    contract_address: args[:to],
    entrypoint: entrypoint
  )

  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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/tezos_client.rb', line 202

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/tezos_client.rb', line 218

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: [], 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



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

def originate_contract(from:, amount:, secret_key: nil, script: nil, init_params: [], 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(
    script: script,
    init_params: init_params,
    **args
  )

  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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tezos_client.rb', line 154

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

#select_entrypoint(contract_address:, entrypoint:) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/tezos_client.rb', line 190

def select_entrypoint(contract_address:, entrypoint:)
  entrypoints = entrypoints(contract_address)["entrypoints"].keys

  if entrypoints.count == 0
    "default"
  elsif entrypoints.include?(entrypoint)
    entrypoint
  else
    raise ::ArgumentError, "entrypoint #{entrypoint} not found in #{entrypoints}"
  end
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



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tezos_client.rb', line 118

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



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/tezos_client.rb', line 142

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