class Banano::WalletAccount

Attributes

account[RW]
wallet[RW]

Public Class Methods

new(node:, wallet:, account: nil) click to toggle source
# 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

Public Instance Methods

change_representative(representative) click to toggle source

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.

@param [String] representative the id of the representative account

to set as this account's representative

@return [String] id of the change block created @raise [ArgumentError] if the representative account does not exist

# 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) click to toggle source

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]

@param count [Integer] number of accounts to create

@return [Banano::WalletAccount] returns a single {Banano::WalletAccount}

if invoked with no argument

@return [Array<Banano::WalletAccount>] returns an Array of {Banano::WalletAccount}

if method was called with argument +n+ >  1

@raise [ArgumentError] if n is less than 1

# 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
destroy() click to toggle source

Unlinks the account from the wallet.

Example:

wallet_account.destroy # => true

@return [Boolean] true if action was successful, otherwise false

# File lib/banano/wallet_account.rb, line 104
def destroy
  rpc(action: :account_remove)[:removed] == '1'
end
pay(to:, amount:, raw: true, id:) click to toggle source

Makes a payment from this account to another account on the banano network. Returns a send block hash if successful, or a {Banano::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.

@param to [String] account id of the recipient of your payment @param amount [Integer|Float] @param raw [Boolean] raw or banano units @param 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

@return [String] the send block id for the payment @raise [Banano::Error] if unsuccessful

# 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
receive(block) click to toggle source

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

@param block [String] block id of pending payment.

@return [String] the receive block id @return [false] if there was no block to receive

# File lib/banano/wallet_account.rb, line 168
def receive(block)
  response = rpc(action: :receive, params: {block: block})[:block]
  response.nil? ? false : response
end

Private Instance Methods

rpc(action:, params: {}) click to toggle source
# File lib/banano/wallet_account.rb, line 198
def rpc(action:, params: {})
  p = {}
  return p unless @wallet

  p[:wallet] = @wallet
  p[:account] = @account unless @account.nil?

  @node.rpc(action: action, params: p.merge(params))
end