class TezosClient

Constants

RANDOM_SIGNATURE
VERSION

Attributes

rpc_interface[RW]
smartpy_interface[RW]

Public Class Methods

new(rpc_node_address: "127.0.0.1", rpc_node_port: 8732, liquidity_options: {}) click to toggle source
# 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
root_path() click to toggle source
# File lib/tezos_client.rb, line 264
def self.root_path
  File.expand_path(File.dirname(__FILE__))
end

Public Instance Methods

activate_account(pkh:, secret:, dry_run: false, **args) click to toggle source
# File lib/tezos_client.rb, line 131
def activate_account(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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Originates a contract on the tezos blockchain

@param from [String] Address originating the contract @param amount [Numeric] amount to send to the contract @param secret_key [String] Secret key of the origination address @param args [Hash] keyword options for the origination @option args [String] :script path of the liquidity script @option args [Array, String] :init_params params to pass to the storage initialization process @option args [Boolean] :spendable decide wether the contract is spendable or not @option args [Boolean] :delegatable decide wether the contract is delegatable or not

@return [Hash] result of the origination containing :operation_id, :operation_result and :originated_contract

# 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) click to toggle source
# 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:) click to toggle source
# 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) click to toggle source

Transfer funds to an account

@param from [String] Address originating the transfer @param to [String] Address receiving the transfer @param amount [Numeric] amount to send to the account @param secret_key [String] Secret key of the origination address @param args [Hash] keyword options for the transfer

@return [Hash] result of the transfer containing :operation_id and :operation_result

# 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) click to toggle source
# 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

Private Instance Methods

broadcast_operation(operation:, dry_run:) click to toggle source
# File lib/tezos_client.rb, line 269
def broadcast_operation(operation:, dry_run:)
  res = if dry_run
    operation.simulate
  else
    operation.test_and_broadcast
  end

  res.merge(
    rpc_operation_args: operation.rpc_operation_args
  )
end
contract_interface(script) click to toggle source
# File lib/tezos_client.rb, line 301
def contract_interface(script)
  case script.to_s
  when /[A-Za-z_\/\-]*.py/
    smartpy_interface
  when nil
    raise "script var unset"
  else
    raise "unknown contract type"
  end
end
convert_params(params:, params_type:) click to toggle source
# File lib/tezos_client.rb, line 291
def convert_params(params:,  params_type:)
  case params_type.to_sym
  when :micheline
    params
  else
    raise ::ArgumentError, "params type must be equal to [ :micheline ]"
  end
end
micheline_params(params:, entrypoint:, params_type:) click to toggle source
# File lib/tezos_client.rb, line 281
def micheline_params(params:, entrypoint:, params_type:)
  {
    entrypoint: entrypoint,
    value: convert_params(
      params: params,
      params_type: params_type
    )
  }
end