Class: Banano::Account
- Inherits:
-
Object
- Object
- Banano::Account
- Defined in:
- lib/banano/account.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
Instance Method Summary collapse
-
#balance(raw = true) ⇒ Hash{Symbol=>Integer|Float}
The account’s balance, including pending (unreceived payments).
-
#block_count ⇒ Integer
Number of blocks for this account.
-
#delegators(raw = true) ⇒ Hash{Symbol=>Integer}
Information about this accounts that have set this account as their representative.
-
#exists? ⇒ Boolean
(also: #open?)
Returns true if the account has an open block.
-
#history(limit: 1000, raw: true) ⇒ Array<Hash{Symbol=>String}>
An account’s history of send and receive payments.
-
#id ⇒ String
The id of the account.
-
#info ⇒ Hash{Symbol=>String|Integer|Float}
Information about the account.
-
#initialize(node:, address:) ⇒ Account
constructor
A new instance of Account.
-
#last_modified_at ⇒ Time
The last modified time of the account in the time zone of your node (usually UTC).
-
#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.
-
#public_key ⇒ String
The public key of the account.
-
#representative ⇒ String
The representative account id for the account.
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
#address ⇒ Object
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:
account.balance
Example response:
{
balance: 2,
pending: 1.1
}
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_count ⇒ Integer
Returns 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:
account.delegators
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:
account.exists? # => true
# or
account.open? # => true
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:
account.history
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 |
#id ⇒ String
The id of the account.
Example:
account.id # => "ban_16u..."
124 125 126 |
# File 'lib/banano/account.rb', line 124 def id @address end |
#info ⇒ Hash{Symbol=>String|Integer|Float}
Information about the account.
Examples:
account.info
153 154 155 |
# File 'lib/banano/account.rb', line 153 def info rpc(action: :account_info) end |
#last_modified_at ⇒ Time
The last modified time of the account in the time zone of your node (usually UTC).
Example:
account.last_modified_at # => Time
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:
account.pending # => ["000D1BA..."]
Asking for more detail to be returned:
account.pending(detailed: true)
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_key ⇒ String
The public key of the account.
Example:
account.public_key # => "3068BB1..."
33 34 35 |
# File 'lib/banano/account.rb', line 33 def public_key rpc(action: :account_key)[:key] end |
#representative ⇒ String
The representative account id for the account. Representatives are accounts that cast votes in the case of a fork in the network.
Example:
account.representative # => "ban_3pc..."
46 47 48 |
# File 'lib/banano/account.rb', line 46 def representative rpc(action: :account_representative)[:representative] end |