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/operation.rb,
lib/tezos_client/encode_utils.rb,
lib/tezos_client/string_utils.rb,
lib/tezos_client/rpc_interface.rb,
lib/tezos_client/currency_utils.rb,
lib/tezos_client/client_interface.rb,
lib/tezos_client/liquidity_interface.rb,
lib/tezos_client/client_interface/key.rb,
lib/tezos_client/rpc_interface/blocks.rb,
lib/tezos_client/rpc_interface/helper.rb,
lib/tezos_client/client_interface/misc.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/client_interface/contract.rb,
lib/tezos_client/rpc_interface/request_manager.rb,
lib/tezos_client/client_interface/client_wrapper.rb,
lib/tezos_client/operations/origination_operation.rb,
lib/tezos_client/operations/transaction_operation.rb,
lib/tezos_client/client_interface/block_contextual.rb,
lib/tezos_client/liquidity_inteface/liquidity_wrapper.rb

Defined Under Namespace

Modules: Commands, Crypto, CurrencyUtils, EncodeUtils, Logger, StringUtils Classes: ClientInterface, LiquidityInterface, Operation, OriginationOperation, RpcInterface, TransactionOperation

Constant Summary collapse

RANDOM_SIGNATURE =
"edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ"
VERSION =
"0.2.0"

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

Methods included from Logger

#log

Constructor Details

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

Returns a new instance of TezosClient.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tezos_client.rb', line 39

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

  @client_config_file = ENV["TEZOS_CLIENT_CONFIG_FILE"]
  @client_interface = ClientInterface.new(
    config_file: @client_config_file
  )

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

  @liquidity_interface = LiquidityInterface.new(
    rpc_node_address: @rpc_node_address,
    rpc_node_port: @rpc_node_port
  )
end

Instance Attribute Details

#client_interfaceObject

Returns the value of attribute client_interface.



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

def client_interface
  @client_interface
end

#liquidity_interfaceObject

Returns the value of attribute liquidity_interface.



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

def liquidity_interface
  @liquidity_interface
end

#rpc_interfaceObject

Returns the value of attribute rpc_interface.



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

def rpc_interface
  @rpc_interface
end

Instance Method Details

#block_include_operation?(operation_id, block_id) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
147
# File 'lib/tezos_client.rb', line 144

def block_include_operation?(operation_id, block_id)
  operations = rpc_interface.get("chains/main/blocks/#{block_id}/operation_hashes")
  operations.flatten.include? operation_id
end

#call_contract(args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tezos_client.rb', line 107

def call_contract(args)
  parameters = args.fetch(:parameters)

  json_params = liquidity_interface.call_parameters(
    script: args.fetch(:script),
    parameters: parameters
  )

  transfer_args = args.merge(parameters: json_params)

  transfer(transfer_args)
end

#monitor_operation(operation_id, timeout: 120) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tezos_client.rb', line 120

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 including_block.nil?
    end
  end

  monitoring_thread.terminate

  including_block
end

#originate_contract(from:, amount:, secret_key:, **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)

    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



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

def originate_contract(from:, amount:, secret_key:, **args)
  res = OriginationOperation.new(
    liquidity_interface: liquidity_interface,
    rpc_interface: rpc_interface,
    from: from,
    secret_key: secret_key,
    amount: amount,
    **args
  ).test_and_broadcast

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

#transfer(from:, amount:, to:, secret_key:, **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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tezos_client.rb', line 95

def transfer(from:, amount:, to:, secret_key:, **args)
  TransactionOperation.new(
    liquidity_interface: liquidity_interface,
    rpc_interface: rpc_interface,
    from: from,
    to: to,
    secret_key: secret_key,
    amount: amount,
    **args
  ).test_and_broadcast
end