Class: Banano::WalletAccount

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/banano/wallet_account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 
  @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

#accountObject

Returns the value of attribute account.



10
11
12
# File 'lib/banano/wallet_account.rb', line 10

def 
  @account
end

#walletObject

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:

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



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_countInteger

Returns number of blocks for this account.

Returns:

  • (Integer)

    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.

Parameters:

  • representative (String)

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

Returns:

  • (String)

    id of the change block created

Raises:

  • (ArgumentError)

    if the representative account does not exist



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:

.create     # => Banano::WalletAccount
.create(2)  # => [Banano::WalletAccount, Banano::WalletAccount]

Parameters:

  • count (Integer) (defaults to: 1)

    number of accounts to create

Returns:

Raises:

  • (ArgumentError)

    if n is less than 1



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 ||
    Banano::WalletAccount.new(node: @node,
                              wallet: @wallet,
                              account: )
    end
  end
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



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

#destroyBoolean

Unlinks the account from the wallet.

Example:

.destroy # => true

Returns:

  • (Boolean)

    true if action was successful, otherwise false



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:

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

Returns:

  • (Boolean)

    Indicates if this account has an open block



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:

.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



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

#idString

The id of the account.

Example:

.id # => "ban_16u..."

Returns:

  • (String)

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

#info(detailed: false, raw: true) ⇒ Hash{Symbol=>String|Integer|Float}

Information about the account.

Examples:

.info

Returns:

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

    information about the 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

#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).



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.

Parameters:

  • to (String)

    account id of the recipient of your payment

  • amount (Integer|Float)
  • raw (Boolean) (defaults to: true)

    raw or banano units

  • 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

Returns:

  • (String)

    the send block id for the payment

Raises:



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:

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


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_keyString

The public key of the account.

Example:

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

Returns:

  • (String)

    public key of the 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

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

.receive("718CC21...") # => "9AE2311..."

Parameters:

  • block (String)

    block id of pending payment.

Returns:

  • (String)

    the receive block id

  • (false)

    if there was no block to receive



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

#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



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