Module: NEAR::CLI::Transaction

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

Overview

Instance Method Summary collapse

Instance Method Details

#construct_transaction(signer_id, receiver_id, actions) ⇒ String

Constructs a new transaction with multiple actions.

Parameters:

  • signer_id (String)

    Account that signs the transaction

  • receiver_id (String)

    Account that receives the transaction

  • actions (Array<Hash>)

    Array of action hashes, each containing :type and :args

Returns:

  • (String)

    Base64-encoded unsigned transaction



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

def construct_transaction(signer_id, receiver_id, actions)
  args = [
    'transaction',
    'construct-transaction',
    signer_id,
    receiver_id
  ]

  actions.each do |action|
    args += construct_action_args(action)
  end

  args += [
    'network-config', @network
  ]

  stdout, _ = execute(*args)
  extract_unsigned_transaction(stdout)
end

#reconstruct_transaction(tx_hash) ⇒ Hash

Reconstructs a CLI command from an existing transaction.

Parameters:

  • tx_hash (String)

    The transaction hash

Returns:

  • (Hash)

    The reconstructed transaction details and CLI command



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/near/cli/transaction.rb', line 27

def reconstruct_transaction(tx_hash)
  stdout, _ = execute(
    'transaction',
    'reconstruct-transaction', tx_hash,
    'network-config', @network
  )

  {
    transaction: parse_transaction_reconstruction(stdout),
    cli_command: extract_cli_command(stdout)
  }
end

#send_meta_transaction(signed_tx) ⇒ Hash

Sends a meta transaction (relayed transaction).

Parameters:

  • signed_tx (String)

    Base64-encoded signed transaction

Returns:

  • (Hash)

    Transaction result



120
121
122
123
124
125
126
127
128
129
# File 'lib/near/cli/transaction.rb', line 120

def send_meta_transaction(signed_tx)
  stdout, stderr = execute(
    'transaction',
    'send-meta-transaction',
    signed_tx,
    'network-config', @network
  )

  parse_transaction_result(stderr)
end

#send_signed_transaction(signed_tx) ⇒ Hash

Sends a signed transaction.

Parameters:

  • signed_tx (String)

    Base64-encoded signed transaction

Returns:

  • (Hash)

    Transaction result



104
105
106
107
108
109
110
111
112
113
# File 'lib/near/cli/transaction.rb', line 104

def send_signed_transaction(signed_tx)
  stdout, stderr = execute(
    'transaction',
    'send-signed-transaction',
    signed_tx,
    'network-config', @network
  )

  parse_transaction_result(stderr)
end

#sign_transaction(unsigned_tx, options = {}) ⇒ String

Signs a previously prepared unsigned transaction.

Parameters:

  • unsigned_tx (String)

    Base64-encoded unsigned transaction

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

    Signing options (e.g., :keychain, :ledger, :private_key)

Returns:

  • (String)

    Base64-encoded signed transaction



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/near/cli/transaction.rb', line 73

def sign_transaction(unsigned_tx, options = {})
  args = [
    'transaction',
    'sign-transaction',
    unsigned_tx,
    'network-config', @network
  ]

  args += case options[:method]
  when :keychain
    ['sign-with-keychain']
  when :ledger
    ['sign-with-ledger']
  when :private_key
    [
      'sign-with-plaintext-private-key',
      options[:private_key]
    ]
  else
    raise ArgumentError, "Invalid signing method: #{options[:method]}"
  end

  stdout, _ = execute(*args)
  extract_signed_transaction(stdout)
end

#view_status(tx_hash) ⇒ Hash

Views status of a transaction.

Parameters:

  • tx_hash (String)

    The transaction hash

Returns:

  • (Hash)

    Transaction status and details



11
12
13
14
15
16
17
18
19
20
# File 'lib/near/cli/transaction.rb', line 11

def view_status(tx_hash)
  stdout, _ = execute(
    'transaction',
    'view-status', tx_hash,
    'network-config', @network
  )

  # Parse the transaction details from the output
  parse_transaction_status(stdout)
end