Class: Banano::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/banano/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, address:) ⇒ Account

Returns a new instance of Account.



7
8
9
10
# File 'lib/banano/account.rb', line 7

def initialize(node:, address:)
  @node = node
  @address = address
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/banano/account.rb', line 5

def address
  @address
end

Instance Method Details

#balance(raw = true) ⇒ Hash{Symbol=>Integer|Float}

The account’s balance, including pending (unreceived payments). To receive a pending amount see WalletAccount#receive.

Examples:

.balance

Example response:

{
  balance: 2,
  pending: 1.1
}

Parameters:

  • raw (Boolean) (defaults to: true)

    if true raw, else banano units

Returns:

  • (Hash{Symbol=>Integer|Float})

Raises:

  • ArgumentError if an invalid unit was given.



67
68
69
70
71
72
73
74
# File 'lib/banano/account.rb', line 67

def balance(raw = true)
  rpc(action: :account_balance).tap do |r|
    unless raw == true
      r[:balance] = Banano::Unit.raw_to_ban(r[:balance]).to_f
      r[:pending] = Banano::Unit.raw_to_ban(r[:pending]).to_f
    end
  end
end

#block_countInteger

Returns number of blocks for this account.

Returns:

  • (Integer)

    number of blocks for this account



77
78
79
# File 'lib/banano/account.rb', line 77

def block_count
  rpc(action: :account_block_count)[:block_count].to_i
end

#delegators(raw = true) ⇒ Hash{Symbol=>Integer}

Information about this accounts that have set this account as their representative.

Example:

.delegators

Parameters:

  • raw (Boolean) (defaults to: true)

    if true return raw balances, else banano units

Returns:

  • (Hash{Symbol=>Integer})

    account ids which delegate to this account



136
137
138
139
140
141
142
143
144
# File 'lib/banano/account.rb', line 136

def delegators(raw = true)
  response = rpc(action: :delegators)[:delegators]
  return response if raw == true

  r = response.map do |address, balance|
    [address.to_s, Banano::Unit.raw_to_ban(balance).to_f]
  end
  Hash[r]
end

#exists?Boolean Also known as: open?

Returns true if the account has an open block.

An open block gets published when an account receives a payment for the first time.

Example:

.exists? # => true
# or
.open?   # => true

Returns:

  • (Boolean)

    Indicates if this account has an open block



169
170
171
172
# File 'lib/banano/account.rb', line 169

def exists?
  response = info
  !response.empty? && !response[:open_block].nil?
end

#history(limit: 1000, raw: true) ⇒ Array<Hash{Symbol=>String}>

An account’s history of send and receive payments.

Example:

.history

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of history items to return

  • raw (Boolean) (defaults to: true)

    raw or banano

Returns:

  • (Array<Hash{Symbol=>String}>)

    the history of send and receive payments for this account



184
185
186
187
188
189
190
191
192
193
# File 'lib/banano/account.rb', line 184

def history(limit: 1000, raw: true)
  response = Array(rpc(action: :account_history, params: {count: limit})[:history])
  response = response.collect {|h| Banano::Util.symbolize_keys(h) }
  return response if raw == true

  response.map! do |history|
    history[:amount] = Banano::Unit.raw_to_ban(history[:amount]).to_f
    history
  end
end

#idString

The id of the account.

Example:

.id # => "ban_16u..."

Returns:

  • (String)

    the id of the account



124
125
126
# File 'lib/banano/account.rb', line 124

def id
  @address
end

#infoHash{Symbol=>String|Integer|Float}

Information about the account.

Examples:

.info

Returns:

  • (Hash{Symbol=>String|Integer|Float})

    information about the account



153
154
155
# File 'lib/banano/account.rb', line 153

def info
  rpc(action: :account_info)
end

#last_modified_atTime

The last modified time of the account in the time zone of your node (usually UTC).

Example:

.last_modified_at # => Time

Returns:

  • (Time)

    last modified time of the account in the time zone of your nano node (usually UTC).



21
22
23
24
# File 'lib/banano/account.rb', line 21

def last_modified_at
  response = rpc(action: :account_info)
  Time.at(response[:modified_timestamp].to_i)
end

#pending(limit: 1000, detailed: false, raw: true) ⇒ Array<String>, Array<Hash{Symbol=>String|Integer}>

Information about pending blocks (payments) that are waiting to be received by the account.

The default response is an Array of block ids.

With the detailed: argument, the method returns an Array of Hashes, which contain the source account id, amount pending and block id.

Examples:

.pending # => ["000D1BA..."]

Asking for more detail to be returned:

.pending(detailed: true)

Parameters:

  • limit (Integer) (defaults to: 1000)

    number of pending blocks to return (default is 1000)

  • detailed (Boolean) (defaults to: false)

    return a more complex Hash of pending block information (default is false)

  • raw (Boolean) (defaults to: true)

    if true raw, else banano units

Returns:

  • (Array<String>)
  • (Array<Hash{Symbol=>String|Integer}>)


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/banano/account.rb', line 103

def pending(limit: 1000, detailed: false, raw: true)
  params = {count: limit}
  params[:source] = true if detailed

  response = rpc(action: :wallet_pending, params: params)[:blocks]
  return response unless detailed && !response.empty?

  response.map do |key, val|
    p = val.merge(block: key.to_s)
    p[:amount] = Banano::Unit.raw_to_ban(p[:amount]).to_f unless raw == true
    p
  end
end

#public_keyString

The public key of the account.

Example:

.public_key # => "3068BB1..."

Returns:

  • (String)

    public key of the account



33
34
35
# File 'lib/banano/account.rb', line 33

def public_key
  rpc(action: :account_key)[:key]
end

#representativeString

The representative account id for the account. Representatives are accounts that cast votes in the case of a fork in the network.

Example:

.representative # => "ban_3pc..."

Returns:

  • (String)

    Representative account of the account



46
47
48
# File 'lib/banano/account.rb', line 46

def representative
  rpc(action: :account_representative)[:representative]
end