Class: Nanook::WalletAccount
- Inherits:
-
Object
- Object
- Nanook::WalletAccount
- Extended by:
- Forwardable
- Includes:
- Util
- Defined in:
- lib/nanook/wallet_account.rb
Overview
The Nanook::WalletAccount
class lets you manage your nano accounts that are on your node, including paying and receiving payment.
Initializing
Initialize this class through an instance of Wallet like this:
account = Nanook.new.wallet(wallet_id).account(account_id)
Or compose the longhand way like this:
rpc_conn = Nanook::Rpc.new
account = Nanook::WalletAccount.new(rpc_conn, wallet_id, account_id)
Constant Summary
Constants included from Util
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
True if accounts are equal.
-
#balance(unit: Nanook.default_unit) ⇒ Hash{Symbol=>Integer|Float}
The account's balance, including pending (unreceived payments).
-
#block_count ⇒ Integer
Number of blocks for this account.
-
#blocks(limit: 1000, sort: :desc) ⇒ Array<Nanook::Block>
Return blocks for the account.
-
#change_representative(representative) ⇒ Nanook::Block
Sets the representative for the account.
-
#create(n_accounts = 1) ⇒ Nanook::WalletAccount+
Creates a new account, or multiple new accounts, in this wallet.
-
#delegators(unit: Nanook.default_unit) ⇒ Hash{Nanook::Account=>Integer|Float}
Information about this accounts that have set this account as their representative.
-
#delegators_count ⇒ Integer
Number of 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.
-
#hash ⇒ Integer
The hash value is used along with #eql? by the Hash class to determine if two objects reference the same hash key.
-
#history(limit: 1000, unit: Nanook.default_unit, sort: :desc) ⇒ Array<Hash{Symbol=>String|Float|Integer|Nanook::Account|NanookBlock}>
An account's history of send and receive payments.
-
#id ⇒ String
The id of the account.
-
#info(detailed: false, unit: Nanook.default_unit) ⇒ Hash{Symbol=>String|Integer|Float|Nanook::Account|Nanook::Block|Time}
Information about the account.
-
#initialize(rpc, wallet, account = nil) ⇒ WalletAccount
constructor
A new instance of WalletAccount.
-
#last_modified_at ⇒ Time
The last modified time of the account in UTC.
-
#ledger(limit: 1, modified_since: nil, unit: Nanook.default_unit, sort: :desc) ⇒ Hash{Nanook::Account=>String|Integer}
Information about the given account as well as other accounts up the ledger.
-
#open_block ⇒ Nanook::Block
Returns the open block for the account if it exists on the node.
-
#pay(to:, amount:, id:, unit: Nanook.default_unit) ⇒ Nanook::Block
Makes a payment from this account to another account on the nano network.
-
#pending(limit: 1000, detailed: false, unit: Nanook.default_unit) ⇒ Array<Nanook::Block>, Array<Hash{Symbol=>Nanook::Block|Nanook::Account|Integer}>
Information about pending blocks (payments) that are waiting to be received by the account.
-
#public_key ⇒ Nanook::PublicKey
The public key of the account.
-
#receive(block = nil) ⇒ Nanook::Block, false
Receives a pending payment for this account.
-
#representative ⇒ Nanook::Account
The representative for the account.
-
#set_work(work) ⇒ Boolean
Set work for account.
- #to_s ⇒ String (also: #inspect)
-
#weight ⇒ Integer|Float
The account's weight.
-
#work ⇒ String
Returns the work for the account.
Constructor Details
#initialize(rpc, wallet, account = nil) ⇒ WalletAccount
Returns a new instance of WalletAccount.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/nanook/wallet_account.rb', line 66 def initialize(rpc, wallet, account = nil) @rpc = rpc @wallet = wallet.to_s @account = account.to_s if account # Initialize an instance to delegate the RPC commands that do not # need `enable_control` enabled (the read-only RPC commands). @nanook_account_instance = nil return if @account.nil? # Wallet must contain the account unless Nanook::Wallet.new(@rpc, @wallet).contains?(@account) raise ArgumentError, "Account does not exist in wallet. Account: #{@account}, wallet: #{@wallet}" end @nanook_account_instance = as_account(@account) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true if accounts are equal.
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#balance(unit: Nanook.default_unit) ⇒ Hash{Symbol=>Integer|Float}
The account's balance, including pending (unreceived payments). To receive a pending amount see #receive.
This call returns information that may be based on unconfirmed blocks. These details should not be relied on for any process or integration that requires confirmed blocks. The pending balance is calculated from potentially unconfirmed blocks.
Examples:
account.balance
Example response:
{
balance: 2,
pending: 1.1
}
Asking for the balance to be returned in raw instead of NANO:
account.balance(unit: :raw)
Example response:
{
balance: 2000000000000000000000000000000,
pending: 1100000000000000000000000000000
}
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#block_count ⇒ Integer
Returns number of blocks for this account.
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#blocks(limit: 1000, sort: :desc) ⇒ Array<Nanook::Block>
Return blocks for the account.
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#change_representative(representative) ⇒ Nanook::Block
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.
Also see Nanook::Wallet#change_default_representative for how to set a default representative for all new accounts created in a wallet.
Example:
account.change_representative("nano_...") # => Nanook::Block
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/nanook/wallet_account.rb', line 244 def change_representative(representative) unless as_account(representative).exists? raise Nanook::Error, "Representative account does not exist: #{representative}" end params = { representative: representative, _access: :block } as_block(rpc(:account_representative_set, params)) end |
#create(n_accounts = 1) ⇒ Nanook::WalletAccount+
Creates a new account, or multiple new accounts, in this wallet.
Examples:
wallet.create # => Nanook::WalletAccount
wallet.create(2) # => [Nanook::WalletAccount, Nanook::WalletAccount]
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/nanook/wallet_account.rb', line 115 def create(n_accounts = 1) skip_account_required! raise ArgumentError, 'number of accounts must be greater than 0' if n_accounts < 1 if n_accounts == 1 as_wallet_account(rpc(:account_create, _access: :account)) else rpc(:accounts_create, count: n_accounts, _access: :accounts, _coerce: Array).map do |account| as_wallet_account(account) end end end |
#delegators(unit: Nanook.default_unit) ⇒ Hash{Nanook::Account=>Integer|Float}
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#delegators_count ⇒ Integer
Number of accounts that have set this account as their representative.
Example:
account.delegators_count # => 2
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#destroy ⇒ Boolean
Unlinks the account from the wallet.
Example:
account.destroy # => true
135 136 137 |
# File 'lib/nanook/wallet_account.rb', line 135 def destroy rpc(:account_remove, _access: :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.
The reliability of this check depends on the node host having synchronized itself with most of the blocks on the nano network, otherwise you may get false
when the account does exist. You can check if a node's synchronization is particular low using Node#sync_progress.
Example:
account.exists? # => true
# or
account.open? # => true
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#hash ⇒ Integer
The hash value is used along with #eql? by the Hash class to determine if two objects reference the same hash key.
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#history(limit: 1000, unit: Nanook.default_unit, sort: :desc) ⇒ Array<Hash{Symbol=>String|Float|Integer|Nanook::Account|NanookBlock}>
An account's history of send and receive payments.
This call may return results that include unconfirmed blocks, so it should not be used in any processes or integrations requiring only details from blocks confirmed by the network.
Example:
account.history
Example response:
[
{
type: "send",
account: Nanook::Account,
amount: 2,
block: Nanook::Block
}
]
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#id ⇒ String
The id of the account.
Example:
account.id # => "nano_16u..."
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#info(detailed: false, unit: Nanook.default_unit) ⇒ Hash{Symbol=>String|Integer|Float|Nanook::Account|Nanook::Block|Time}
Information about the account.
Examples:
account.info
Example response:
{
id: "nano_16u1uufyoig8777y6r8iqjtrw8sg8maqrm36zzcm95jmbd9i9aj5i8abr8u5",
balance: 11.439597000000001,
block_count: 4,
frontier: "2C3C570EA8898443C0FD04A1C385A3E3A8C985AD792635FCDCEBB30ADF6A0570",
modified_timestamp: 1520500357,
open_block: "C82376314C387080A753871A32AD70F4168080C317C5E67356F0A62EB5F34FF9",
representative_block: "C82376314C387080A753871A32AD70F4168080C317C5E67356F0A62EB5F34FF9"
}
Asking for more detail to be returned:
account.info(detailed: true)
Example response:
{
id: "nano_16u1uufyoig8777y6r8iqjtrw8sg8maqrm36zzcm95jmbd9i9aj5i8abr8u5",
balance: 11.439597000000001,
block_count: 4,
frontier: Nanook::Block,
last_modified_at: Time,
open_block: Nanook::Block,
pending: 1.0,
representative: Nanook::Account,
representative_block: Nanook::Block,
weight: 0.1
}
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#last_modified_at ⇒ Time
The last modified time of the account in UTC. For many accounts on the node this will be the timestamp of when the node bootstrapped.
Example:
account.last_modified_at # => Time
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#ledger(limit: 1, modified_since: nil, unit: Nanook.default_unit, sort: :desc) ⇒ Hash{Nanook::Account=>String|Integer}
Information about the given account as well as other accounts up the ledger. The number of accounts returned is determined by the limit:
argument.
Example:
account.ledger(limit: 2)
Example response:
{
Nanook::Account => {
:frontier => Nanook::Block,
:open_block => Nanook::Block,
:representative_block => Nanook::Block,
:representative => Nanook::Account,
:balance => 1143.7,
:last_modified_at => Time,
:block_count => 4
:weight => 5
:pending => 2.0
},
Nanook::Account => { ... }
}
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#open_block ⇒ Nanook::Block
Returns the open block for the account if it exists on the node.
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#pay(to:, amount:, id:, unit: Nanook.default_unit) ⇒ Nanook::Block
Makes a payment from this account to another account on the nano network. Returns a send block hash if successful, or a NodeRpcError if unsuccessful.
Note, there may be a delay in receiving a response due to Proof of Work being done. From the Nano RPC:
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.
Examples:
account.pay(to: "nano_...", amount: 1.1, id: "myUniqueId123") # => "9AE2311..."
account.pay(to: "nano_...", amount: 54000000000000, id: "myUniqueId123", unit: :raw) # => "9AE2311..."
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/nanook/wallet_account.rb', line 169 def pay(to:, amount:, id:, unit: Nanook.default_unit) validate_unit!(unit) # Check that to account is a valid address valid = @rpc.call(:validate_account_number, account: to, _access: :valid) == 1 raise ArgumentError, "Account address is invalid: #{to}" unless valid # Determine amount in raw raw = if unit.to_sym.eql?(:nano) NANO_to_raw(amount) else amount end # account is called source, so don't use the normal rpc method params = { wallet: @wallet, source: @account, destination: to, amount: raw, id: id, _access: :block } as_block(@rpc.call(:send, params)) end |
#pending(limit: 1000, detailed: false, unit: Nanook.default_unit) ⇒ Array<Nanook::Block>, Array<Hash{Symbol=>Nanook::Block|Nanook::Account|Integer}>
Information about pending blocks (payments) that are waiting to be received by the account.
See also the #receive method for how to receive a pending payment.
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 # => [Nanook::Block, ..."]
Asking for more detail to be returned:
account.pending(detailed: true)
Example response:
[
{
block: Nanook::Block,
amount: 6,
source: Nanook::Account
},
{ ... }
]
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#public_key ⇒ Nanook::PublicKey
The public key of the account.
Example:
account.public_key # => Nanook::PublicKey
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#receive(block = nil) ⇒ Nanook::Block, 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 # => Nanook::Block
account.receive("718CC21...") # => Nanook::Block
217 218 219 220 221 |
# File 'lib/nanook/wallet_account.rb', line 217 def receive(block = nil) return receive_without_block if block.nil? receive_with_block(block) end |
#representative ⇒ Nanook::Account
The representative for the account.
Example:
account.representative # => Nanook::Account
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#set_work(work) ⇒ Boolean
Set work for account.
Example:
account.set_work("432e5cf728c90f4f") # => true
275 276 277 |
# File 'lib/nanook/wallet_account.rb', line 275 def set_work(work) rpc(:work_set, work: work).key?(:success) end |
#to_s ⇒ String Also known as: inspect
140 141 142 |
# File 'lib/nanook/wallet_account.rb', line 140 def to_s "#{self.class.name}(id: \"#{short_id}\")" end |
#weight ⇒ Integer|Float
The account's weight.
Weight is determined by the account's balance, and represents the voting weight that account has on the network. Only accounts with greater than 0.1% of the online voting weight and are on a node configured to vote can vote.
Example:
account.weight # => 0
60 61 62 63 |
# File 'lib/nanook/wallet_account.rb', line 60 def_delegators :@nanook_account_instance, :==, :balance, :block_count, :blocks, :delegators, :delegators_count, :eql?, :exists?, :hash, :history, :id, :info, :last_modified_at, :ledger, :open_block, :pending, :public_key, :representative, :weight |
#work ⇒ String
Returns the work for the account.
Example:
account.work # => "432e5cf728c90f4f"
264 265 266 |
# File 'lib/nanook/wallet_account.rb', line 264 def work rpc(:work_get, _access: :work) end |