Module: NEAR::CLI::Tokens

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

Overview

Instance Method Summary collapse

Instance Method Details

#send_near(sender_id, receiver_id, amount) ⇒ Hash

Sends NEAR tokens from one account to another.

Parameters:

  • sender_id (String)

    The account sending the tokens

  • receiver_id (String)

    The account receiving the tokens

  • amount (NEAR::Balance)

    The amount to send

Returns:

  • (Hash)

    Transaction result

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/near/cli/tokens.rb', line 13

def send_near(sender_id, receiver_id, amount)
  raise ArgumentError, "amount must be a NEAR::Balance" unless amount.is_a?(NEAR::Balance)

  _, stderr = execute(
    'tokens',
    sender_id,
    'send-near', receiver_id, amount.to_cli_arg,
    'network-config', @network,
    'sign-with-keychain',
    'send'
  )

  # Extract transaction details from stderr:
  if stderr.include?('Transaction sent')
    tx_match = stderr.match(/Transaction ID: ([A-Za-z0-9]+)/)
    tx_id = tx_match ? tx_match[1] : nil
    {
      success: true,
      transaction_id: tx_id,
      message: stderr.strip
    }
  else
    {
      success: false,
      message: stderr.strip
    }
  end
end

#view_near_balance(account_id, block: :now) ⇒ Hash

Views the NEAR token balance of an account.

Parameters:

  • account_id (String)

    The account to check

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

    The block to query (default: :now)

Returns:

  • (Hash)

    Balance information including available and total balance



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

def view_near_balance(, block: :now)
  _, stderr = execute(
    'tokens',
    ,
    'view-near-balance',
    'network-config', @network,
    *block_args(block)
  )

  # Parse the balance information from stderr:
  available_match = stderr.match(/(\d+\.?\d*) NEAR available for transfer/)
  total_match = stderr.match(/total balance is (\d+\.?\d*) NEAR/)
  locked_match = stderr.match(/(\d+\.?\d*) NEAR is locked/)

  {
    account_id: ,
    available_balance: available_match ? NEAR::Balance.from_near(available_match[1]) : nil,
    total_balance: total_match ? NEAR::Balance.from_near(total_match[1]) : nil,
    locked_balance: locked_match ? NEAR::Balance.from_near(locked_match[1]) : nil,
    block: block,
  }
end