Module: NEAR::CLI::Contract
- Included in:
- NEAR::CLI
- Defined in:
- lib/near/cli/contract.rb
Overview
Instance Method Summary collapse
-
#call_function(contract, method_name, args = {}, signer: nil, deposit: nil, gas: '100.0 Tgas') ⇒ String
(also: #call_method)
Calls a state-changing method on a contract.
-
#deploy_contract(contract, wasm_path, init_method: nil, init_args: {}, init_deposit: '0 NEAR', init_gas: '30 TGas') ⇒ String
Deploys a new contract.
-
#download_wasm(contract_id, output_path, block: :now) ⇒ String
Downloads a contract’s WASM code.
-
#view_call(contract, method_name, args = {}, block: :now) ⇒ String
Calls a view method on a contract (read-only).
-
#view_storage(contract_id, prefix: nil, block: :now) ⇒ String
Views contract storage state with JSON formatting.
-
#view_storage_base64(contract_id, base64_prefix, block: :now) ⇒ String
Views contract storage state with base64 key filtering.
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.
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.
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.
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).
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.
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.
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 |