Module: NEAR::CLI::Contract

Included in:
NEAR::CLI
Defined in:
lib/near/cli/contract.rb

Overview

Instance Method Summary collapse

Instance Method Details

#call_function(contract, method_name, args = {}, signer: nil, deposit: nil, gas: '100.0 Tgas') ⇒ String Also known as: call_method

Calls a state-changing method on a contract.

Parameters:

  • contract (NEAR::Account)
  • method_name (String)
  • args (Hash, Array, String, Pathname) (defaults to: {})

    Arguments for the method

  • signer (NEAR::Account) (defaults to: nil)

    Account that signs the transaction

  • deposit (NEAR::Balance) (defaults to: nil)

    Amount of NEAR to attach

  • gas (NEAR::Gas, #to_s) (defaults to: '100.0 Tgas')

    Amount of gas to attach

Returns:

  • (String)

    Transaction result



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/near/cli/contract.rb', line 39

def call_function(contract, method_name, args = {}, signer: nil, deposit: nil, gas: '100.0 Tgas')
  args = case args
    when Hash, Array then ['json-args', args.to_json]
    when String then case args
      when /[^[:print:]]/ then ['base64-args', Base64.strict_encode64(args)]
      else ['text-args', args]
    end
    when Pathname then ['file-args', args.to_s]
    else raise ArgumentError, "Invalid argument type: #{args.inspect}"
  end
  stdout, stderr = execute(
    'contract',
    'call-function',
    'as-transaction', contract.to_s, method_name.to_s,
    *args,
    'prepaid-gas', gas.to_s,
    'attached-deposit', (deposit ? deposit.to_s : '0') + ' NEAR',
    'sign-as', (signer || contract).to_s,
    'network-config', @network,
    'sign-with-keychain',
    'send'
  )
  stderr
end

#deploy_contract(contract, wasm_path, init_method: nil, init_args: {}, init_deposit: '0 NEAR', init_gas: '30 TGas') ⇒ String

Deploys a new contract.

Parameters:

  • contract (NEAR::Account, #to_s)

    Account to deploy the contract to

  • wasm_path (String)

    Path to the .wasm file

  • init_method (String, nil) (defaults to: nil)

    Method to call after deployment

  • init_args (Hash) (defaults to: {})

    Arguments for the init method

  • init_deposit (String) (defaults to: '0 NEAR')

    Deposit for the init method

  • init_gas (String) (defaults to: '30 TGas')

    Gas for the init method

Returns:

  • (String)

    Transaction result



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/near/cli/contract.rb', line 75

def deploy_contract(contract, wasm_path, init_method: nil, init_args: {},
                   init_deposit: '0 NEAR', init_gas: '30 TGas')
  args = [
    'contract',
    'deploy',
    contract.to_s,
    'use-file', wasm_path
  ]

  args += if init_method
    [
      'with-init-call', init_method,
      'json-args', init_args.to_json,
      'prepaid-gas', init_gas,
      'attached-deposit', init_deposit
    ]
  else
    ['without-init-call']
  end

  args += [
    'network-config', @network,
    'sign-with-keychain',
    'send'
  ]

  stdout, stderr = execute(*args)
  stderr
end

#download_wasm(contract_id, output_path, block: :now) ⇒ String

Downloads a contract’s WASM code.

Parameters:

  • contract_id (String)
  • output_path (String)

    Path to write the .wasm file to

  • block (Block, Integer, String, Symbol) (defaults to: :now)

Returns:

  • (String)

    Path to downloaded file



112
113
114
115
116
117
118
119
120
121
# File 'lib/near/cli/contract.rb', line 112

def download_wasm(contract_id, output_path, block: :now)
  _, _ = execute(
    'contract',
    'download-wasm', contract_id,
    'save-to-file', output_path,
    'network-config', @network,
    *block_args(block)
  )
  output_path
end

#view_call(contract, method_name, args = {}, block: :now) ⇒ String

Calls a view method on a contract (read-only).

Parameters:

  • contract (NEAR::Account)
  • method_name (String)
  • args (Hash) (defaults to: {})

    JSON arguments for the method

  • block (Block, Integer, String, Symbol) (defaults to: :now)

Returns:

  • (String)

    The method call result



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/near/cli/contract.rb', line 17

def view_call(contract, method_name, args = {}, block: :now)
  stdout, _ = execute(
    'contract',
    'call-function',
    'as-read-only', contract.to_s, method_name,
    'json-args', args.to_json,
    'network-config', @network,
    *block_args(block)
  )
  stdout
end

#view_storage(contract_id, prefix: nil, block: :now) ⇒ String

Views contract storage state with JSON formatting.

Parameters:

  • contract_id (String)
  • prefix (String, nil) (defaults to: nil)

    Filter keys by prefix

  • block (Block, Integer, String, Symbol) (defaults to: :now)

Returns:

  • (String)

    Contract storage state



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/near/cli/contract.rb', line 130

def view_storage(contract_id, prefix: nil, block: :now)
  args = [
    'contract',
    'view-storage', contract_id
  ]

  args += if prefix
    ['keys-start-with-string', prefix]
  else
    ['all']
  end

  args += [
    'as-json',
    'network-config', @network,
    *block_args(block)
  ]

  stdout, _ = execute(*args)
  stdout
end

#view_storage_base64(contract_id, base64_prefix, block: :now) ⇒ String

Views contract storage state with base64 key filtering.

Parameters:

  • contract_id (String)
  • base64_prefix (String)

    Filter keys by base64 prefix

  • block (Block, Integer, String, Symbol) (defaults to: :now)

Returns:

  • (String)

    Contract storage state



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/near/cli/contract.rb', line 159

def view_storage_base64(contract_id, base64_prefix, block: :now)
  stdout, _ = execute(
    'contract',
    'view-storage', contract_id,
    'keys-start-with-bytes-as-base64', base64_prefix,
    'as-json',
    'network-config', @network,
    *block_args(block)
  )
  stdout
end