Class: Banano::WalletAccount
- Inherits:
-
Object
- Object
- Banano::WalletAccount
- Extended by:
- Forwardable
- Defined in:
- lib/banano/wallet_account.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#wallet ⇒ Object
Returns the value of attribute wallet.
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.
-
#change_representative(representative) ⇒ String
Sets the representative for the account.
-
#create(count = 1) ⇒ Banano::WalletAccount+
Creates a new account, or multiple new accounts, in this wallet.
-
#delegators(raw: true) ⇒ Hash{Symbol=>Integer}
Information about this accounts that have set this account as their representative.
-
#destroy ⇒ Boolean
Unlinks the account from the wallet.
-
#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(detailed: false, raw: true) ⇒ Hash{Symbol=>String|Integer|Float}
Information about the account.
-
#initialize(node:, wallet:, account: nil) ⇒ WalletAccount
constructor
A new instance of WalletAccount.
-
#last_modified_at ⇒ Time
The last modified time of the account in the time zone of your node (usually UTC).
-
#pay(to:, amount:, raw: true, id:) ⇒ String
Makes a payment from this account to another account on the banano network.
-
#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.
-
#receive(block) ⇒ String, false
Receives a pending payment for this account.
-
#representative ⇒ String
The representative account id for the account.
Constructor Details
#initialize(node:, wallet:, account: nil) ⇒ WalletAccount
Returns a new instance of WalletAccount.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/banano/wallet_account.rb', line 48 def initialize(node:, wallet:, account: nil) @node = node @wallet = wallet @account = account @banano_account_instance = nil unless @account.nil? # Wallet must contain the account unless Banano::Wallet.new(node: @node, wallet: @wallet).contains?(@account) raise ArgumentError, "Account does not exist in wallet. Account: #{@account}, wallet: #{@wallet}" end # An object to delegate account methods that don't # expect a wallet param in the RPC call, to allow this # class to support all methods that can be called on Banano::Account @banano_account_instance = Banano::Account.new(node: @node, address: @account) end end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
10 11 12 |
# File 'lib/banano/wallet_account.rb', line 10 def account @account end |
#wallet ⇒ Object
Returns the value of attribute wallet.
10 11 12 |
# File 'lib/banano/wallet_account.rb', line 10 def wallet @wallet 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 #receive.
Examples:
account.balance
Example response:
{
balance: 2,
pending: 1.1
}
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#block_count ⇒ Integer
Returns number of blocks for this account.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#change_representative(representative) ⇒ String
Sets the representative for the account.
A representative is an account that will vote on your account’s behalf on the nano network if your account is offline and there is a fork of the network that requires voting on.
Returns the change block that was broadcast to the nano network. The block contains the information about the representative change for your account.
187 188 189 190 191 192 193 194 |
# File 'lib/banano/wallet_account.rb', line 187 def change_representative(representative) unless Banano::Account.new(node: @node, address: representative).exists? raise ArgumentError, "Representative account does not exist: #{representative}" end rpc(action: :account_representative_set, params: {representative: representative})[:block] end |
#create(count = 1) ⇒ Banano::WalletAccount+
Creates a new account, or multiple new accounts, in this wallet.
Examples:
wallet_account.create # => Banano::WalletAccount
wallet_account.create(2) # => [Banano::WalletAccount, Banano::WalletAccount]
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/banano/wallet_account.rb', line 81 def create(count = 1) raise ArgumentError, "number of accounts must be greater than 0" if count < 1 if count == 1 Banano::WalletAccount.new(node: @node, wallet: @wallet, account: rpc(action: :account_create)[:account]) else Array(rpc(action: :accounts_create, params: {count: count})[:accounts]).map do |account| Banano::WalletAccount.new(node: @node, wallet: @wallet, account: account) end end end |
#delegators(raw: true) ⇒ Hash{Symbol=>Integer}
Information about this accounts that have set this account as their representative.
Example:
account.delegators
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#destroy ⇒ Boolean
Unlinks the account from the wallet.
Example:
wallet_account.destroy # => true
104 105 106 |
# File 'lib/banano/wallet_account.rb', line 104 def destroy rpc(action: :account_remove)[:removed] == '1' 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
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#history(limit: 1000, raw: true) ⇒ Array<Hash{Symbol=>String}>
An account’s history of send and receive payments.
Example:
account.history
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#id ⇒ String
The id of the account.
Example:
account.id # => "ban_16u..."
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#info(detailed: false, raw: true) ⇒ Hash{Symbol=>String|Integer|Float}
Information about the account.
Examples:
account.info
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#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
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#pay(to:, amount:, raw: true, id:) ⇒ String
Makes a payment from this account to another account on the banano network. Returns a send block hash if successful, or a Error if unsuccessful.
Note, there may be a delay in receiving a response due to Proof of Work being done. Proof of Work is precomputed for one transaction in the background. If it has been a while since your last transaction it will send instantly, the next one will need to wait for Proof of Work to be generated.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/banano/wallet_account.rb', line 128 def pay(to:, amount:, raw: true, id:) # Check that to account is a valid address response = rpc(action: :validate_account_number, params: {account: to}) raise ArgumentError, "Account address is invalid: #{to}" unless response[:valid] == '1' raw_amount = raw ? amount : Banano::Unit.ban_to_raw(amount).to_s # account is called source, so don't use the normal rpc method p = { wallet: @wallet, source: @account, destination: to, amount: raw_amount, id: id } response = rpc(action: :send, params: p) return Banano::Error.new(response[:error]) if response.key?(:error) response[:block] 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)
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#public_key ⇒ String
The public key of the account.
Example:
account.public_key # => "3068BB1..."
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |
#receive(block) ⇒ String, false
Receives a pending payment for this account.
When called with no block argument, the latest pending payment for the account will be received.
Returns a receive block id if a receive was successful, or false if there were no pending payments to receive.
You can receive a specific pending block if you know it by passing the block in as an argument.
Examples:
account.receive("718CC21...") # => "9AE2311..."
168 169 170 171 |
# File 'lib/banano/wallet_account.rb', line 168 def receive(block) response = rpc(action: :receive, params: {block: block})[:block] response.nil? ? false : response 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..."
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banano/wallet_account.rb', line 35 def_delegators :@banano_account_instance, :balance, :delegators, :exists?, :id, :info, :last_modified_at, :pending, :public_key, :history, :representative |