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/client_interface.rb,
lib/tezos_client/liquidity_interface.rb,
lib/tezos_client/client_interface/key.rb,
lib/tezos_client/operations/operation.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/rpc_interface/operations.rb,
lib/tezos_client/client_interface/contract.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/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/operations/transactions_operation.rb,
lib/tezos_client/liquidity_inteface/liquidity_wrapper.rb,
lib/tezos_client/operations/activate_account_operation.rb

Defined Under Namespace

Modules: Commands, Crypto, CurrencyUtils, EncodeUtils, Logger, StringUtils Classes: ActivateAccountOperation, ClientInterface, InvalidActivation, LiquidityInterface, Operation, OperationArray, OperationFailure, OperationMgr, OriginationOperation, RevealOperation, RpcInterface, RpcRequestFailure, ScriptRuntimeError, TezBalanceTooLow, TransactionOperation, TransactionsOperation

Constant Summary collapse

RANDOM_SIGNATURE =
"edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ"
VERSION =
"0.4.2"

Constants included from Crypto

Crypto::PREFIXES, Crypto::WATERMARK

Constants included from Logger

Logger::FILTERED_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Crypto

#checksum, #decode_account_wallet, #decode_base58, #decode_tz, #encode_base58, #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) ⇒ TezosClient

Returns a new instance of TezosClient.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tezos_client.rb', line 46

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.



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

def client_interface
  @client_interface
end

#liquidity_interfaceObject

Returns the value of attribute liquidity_interface.



42
43
44
# File 'lib/tezos_client.rb', line 42

def liquidity_interface
  @liquidity_interface
end

#rpc_interfaceObject

Returns the value of attribute rpc_interface.



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

def rpc_interface
  @rpc_interface
end

Instance Method Details

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



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

def (pkh:, secret:, **args)
  ActivateAccountOperation.new(
    rpc_interface: rpc_interface,
    pkh: pkh,
    secret: secret,
    **args
  ).test_and_broadcast
end

#block_include_operation?(operation_id, block_id) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
# File 'lib/tezos_client.rb', line 200

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



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

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tezos_client.rb', line 168

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:, script: nil, init_params: nil, **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



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

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

  if script != nil
    origination_args[:script] = liquidity_interface.origination_script(
      from: from,
      script: script,
      init_params: init_params
    )
  end

  res = OriginationOperation.new(origination_args).test_and_broadcast

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

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



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

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

  RevealOperation.new(
    liquidity_interface: liquidity_interface,
    rpc_interface: rpc_interface,
    public_key: public_key,
    from: from,
    secret_key: secret_key,
    **args
  ).test_and_broadcast
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



111
112
113
114
115
116
117
118
119
120
# File 'lib/tezos_client.rb', line 111

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

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



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

def transfer_to_many(from:, amounts:, secret_key:, **args)
  TransactionsOperation.new(
    rpc_interface: rpc_interface,
    from: from,
    amounts: amounts,
    secret_key: secret_key,
    **args
  ).test_and_broadcast
end