Class: Nanook::WalletAccount

Inherits:
Object
  • Object
show all
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:

 = Nanook.new.wallet(wallet_id).()

Or compose the longhand way like this:

rpc_conn = Nanook::Rpc.new
 = Nanook::WalletAccount.new(rpc_conn, wallet_id, )

Constant Summary

Constants included from Util

Util::STEP

Instance Method Summary collapse

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,  = nil)
  @rpc = rpc
  @wallet = wallet.to_s
  @account = .to_s if 

  # 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 = (@account)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns true if accounts are equal.

Parameters:

Returns:

  • (Boolean)

    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:

.balance

Example response:

{
  balance: 2,
  pending: 1.1
}

Asking for the balance to be returned in raw instead of NANO:

.balance(unit: :raw)

Example response:

{
  balance: 2000000000000000000000000000000,
  pending: 1100000000000000000000000000000
}

Parameters:

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

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

Raises:



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_countInteger

Returns number of blocks for this account.

Returns:

  • (Integer)

    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.

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of history items to return. Defaults to 1000

  • sort (Symbol) (defaults to: :desc)

    default :asc. When set to :desc the blocks will be returned oldest to newest.

Returns:



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:

.change_representative("nano_...") # => Nanook::Block

Parameters:

  • representative (String)

    the id of the representative account to set as this account's representative

Returns:

Raises:

  • (Nanook::Error)

    if setting the representative account fails



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 (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]

Parameters:

  • n_accounts (Integer) (defaults to: 1)

    number of accounts to create

Returns:

Raises:

  • (ArgumentError)

    if n is less than 1



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)
  
  raise ArgumentError, 'number of accounts must be greater than 0' if n_accounts < 1

  if n_accounts == 1
    (rpc(:account_create, _access: :account))
  else
    rpc(:accounts_create, count: n_accounts, _access: :accounts, _coerce: Array).map do ||
      ()
    end
  end
end

#delegators(unit: Nanook.default_unit) ⇒ Hash{Nanook::Account=>Integer|Float}

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

Example:

.delegators

Example response:

{
  Nanook::Account=>50.0,
  Nanook::Account=>961.64
}

Parameters:

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

  • (Hash{Nanook::Account=>Integer|Float})

    Account accounts which delegate to this account, and their account balance

Raises:



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_countInteger

Number of accounts that have set this account as their representative.

Example:

.delegators_count # => 2

Returns:

  • (Integer)


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

#destroyBoolean

Unlinks the account from the wallet.

Example:

.destroy # => true

Returns:

  • (Boolean)

    true if action was successful, otherwise false



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:

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

Returns:

  • (Boolean)

    Indicates if this account has an open 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

#hashInteger

The hash value is used along with #eql? by the Hash class to determine if two objects reference the same hash key.

Returns:

  • (Integer)


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:

.history

Example response:

[
  {
   type: "send",
   account: Nanook::Account,
   amount: 2,
   block: Nanook::Block
  }
]

Parameters:

  • limit (Integer) (defaults to: 1000)

    maximum number of history items to return. Defaults to 1000

  • sort (Symbol) (defaults to: :desc)

    default :asc. When set to :desc the history will be returned oldest to newest.

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

  • (Array<Hash{Symbol=>String|Float|Integer|Nanook::Account|NanookBlock}>)

    the history of send and receive payments for this account

Raises:



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

#idString

The id of the account.

Example:

.id # => "nano_16u..."

Returns:

  • (String)

    the id of 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

#info(detailed: false, unit: Nanook.default_unit) ⇒ Hash{Symbol=>String|Integer|Float|Nanook::Account|Nanook::Block|Time}

Information about the account.

Examples:

.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:

.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
}

Parameters:

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

  • (Hash{Symbol=>String|Integer|Float|Nanook::Account|Nanook::Block|Time})

    information about the account containing:

    id

    The account id

    frontier

    The latest Block

    confirmation_height

    Confirmation height

    confirmation_height_frontier

    The Block of the confirmation height

    pending

    Pending balance in either NANO or raw (depending on the unit: argument)

    open_block

    The first Block in every account's blockchain. When this block was published the account was officially open

    representative_block

    The Block that named the representative for the account

    balance

    Balance in either NANO or raw (depending on the unit: argument)

    last_modified_at

    Time of when the account was last modified in UTC

    representative

    Representative Account

    block_count

    Number of blocks in the account's blockchain

    weight

    See #weight

Raises:



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_atTime

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:

.last_modified_at # => Time

Returns:

  • (Time)

    last modified time of the account in UTC. Can be nil



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:

.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 => { ... }
}

Parameters:

  • limit (Integer) (defaults to: 1)

    number of accounts to return in the ledger (default is 1000)

  • modified_since (Time) (defaults to: nil)

    optional. Return only accounts modified in the local database after this time (default is from the unix epoch)

  • sort (Symbol) (defaults to: :desc)

    default :asc. When set to :desc the ledger will be returned oldest to newest.

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

Raises:



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_blockNanook::Block

Returns the open block for the account if it exists on the node.

Returns:



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:

.pay(to: "nano_...", amount: 1.1, id: "myUniqueId123") # => "9AE2311..."
.pay(to: "nano_...", amount: 54000000000000, id: "myUniqueId123", unit: :raw) # => "9AE2311..."

Parameters:

  • to (String)

    account id of the recipient of your payment

  • amount (Integer|Float)
  • id (String)

    must be unique per payment. It serves an important purpose; it allows you to make the same call multiple times with the same id and be reassured that you will only ever send this nano payment once

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

Raises:



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:

.pending # => [Nanook::Block, ..."]

Asking for more detail to be returned:

.pending(detailed: true)

Example response:

[
  {
    block: Nanook::Block,
    amount: 6,
    source: Nanook::Account
  },
  { ... }
]

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)

  • unit (Symbol) (defaults to: Nanook.default_unit)

    default is Nanook.default_unit. Must be one of UNITS. Represents the unit that the balances will be returned in. Note: this method interprets :nano as NANO, which is technically Mnano. See What are Nano's Units

Returns:

Raises:



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_keyNanook::PublicKey

The public key of the account.

Example:

.public_key # => Nanook::PublicKey

Returns:



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:

.receive               # => Nanook::Block
.receive("718CC21...") # => Nanook::Block

Parameters:

  • block (String) (defaults to: nil)

    optional block id of pending payment. If not provided, the latest pending payment will be received

Returns:

  • (Nanook::Block)

    the receive block

  • (false)

    if there was no block to receive



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

#representativeNanook::Account

The representative for the account.

Example:

.representative # => Nanook::Account

Returns:



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:

.set_work("432e5cf728c90f4f") # => true

Returns:

  • (Boolean)

    true if action was successful



275
276
277
# File 'lib/nanook/wallet_account.rb', line 275

def set_work(work)
  rpc(:work_set, work: work).key?(:success)
end

#to_sString Also known as: inspect

Returns:

  • (String)


140
141
142
# File 'lib/nanook/wallet_account.rb', line 140

def to_s
  "#{self.class.name}(id: \"#{short_id}\")"
end

#weightInteger|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:

.weight # => 0

Returns:

  • (Integer|Float)

    the account's weight

Raises:



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

#workString

Returns the work for the account.

Example:

.work # => "432e5cf728c90f4f"

Returns:

  • (String)

    work



264
265
266
# File 'lib/nanook/wallet_account.rb', line 264

def work
  rpc(:work_get, _access: :work)
end