class NanoAccount
A Nano account which has an address and a blockchain.
Attributes
Public Class Methods
Generate a new Nano account and return a mutable NanoAccount
instance for it. Params:
representative
-
(Optional) This account's representative. Set to the account itself if not specified.
Returns: A NanoAccount
instance for the new account.
# File lib/account.rb, line 32 def self.generate(representative=nil) result = NanoConfiguration.client.key_create NanoAccount.new( result["account"], result["public"], result["private"], representative ) end
Create a new NanoAccount
instance representing an existing account. public_key
and private_key
are optional, but without them the account is immutable. Params:
address
-
The address of the account.
public_key
-
(Optional) The public key of the account.
private_key
-
(Optional) The private key of the account.
representative
-
(Optional) This block's representative. Required only if the account must be opened.
# File lib/account.rb, line 20 def initialize(address, public_key=nil, private_key=nil, representative=nil) @address = address @public_key = public_key @private_key = private_key @representative = representative || address end
Public Instance Methods
The balance of the account, not including unpocketed transactions.
# File lib/account.rb, line 179 def balance NanoConfiguration.client.account_balance(account: @address)["balance"] end
The number of blocks on this account's chain.
# File lib/account.rb, line 56 def block_count begin NanoConfiguration.client.account_block_count(account: @address)["block_count"].to_i rescue => error # Unopened accounts appear to throw instead raise if error.message != "Account not found" return 0 end end
A boolean indicating whether public_key
and private_key
are specified.
# File lib/account.rb, line 43 def mutable? @private_key != nil && @public_key != nil end
The unpocketed balance of the account.
# File lib/account.rb, line 184 def pending_balance NanoConfiguration.client.account_balance(account: @address)["pending"] end
The list of pending transactions for this account.
# File lib/account.rb, line 48 def pending_transactions res = NanoConfiguration.client.accounts_pending(accounts: [ @address ])["blocks"][@address] res = [] if res == nil res end
Scan for any pending transactions on this chain and generate 'receive' or 'open' blocks for them. Requires that this account is mutable. Returns: A boolean indicating whether any transactions were pocketed or not.
# File lib/account.rb, line 71 def pocket raise ImmutableAccountError unless mutable? # Get pending transactions pending = pending_transactions return false if pending.length == 0 # Determine whether the first processed transaction must be an 'open' block block_type = (block_count == 0 ? "open" : "receive") pending.each do |pending_hash| # NOTE: This is using implicit PoW if block_type == "open" # Work against the public key work = NanoConfiguration.client.work_generate({ hash: @public_key })["work"] # Create a block for this 'open' transaction block = NanoConfiguration.client.block_create({ type: block_type, key: @private_key, account: @address, representative: @representative, source: pending_hash, work: work })["block"] # Since this block was an 'open', ensure that the next one is a 'receive' block_type = "receive" else # Get the frontier of this chain frontier = NanoConfiguration.client.account_info({ account: @address, count: 1 })["frontier"] # Work against the frontier work = NanoConfiguration.client.work_generate({ hash: frontier })["work"] # Create a block for this 'receive' transaction block = NanoConfiguration.client.block_create({ type: block_type, key: @private_key, account: @address, source: pending_hash, previous: frontier, work: work })["block"] end print "Block is: " p block # Publish the new block NanoConfiguration.client.process(block: block) end true end
Send an amount of Nano to another address. Params:
amount
-
The amount of raw Nano to send.
recipient
-
The recipient account of the Nano, as a string or
NanoAccount
.
# File lib/account.rb, line 139 def send(amount, recipient) raise ImmutableAccountError unless mutable? if recipient.is_a?(String) recipient = NanoAccount.new(recipient) end # Get the frontier of this chain frontier = NanoConfiguration.client.account_info({ account: @address, count: 1 })["frontier"] # Work against the frontier work = NanoConfiguration.client.work_generate({ hash: frontier })["work"] # Create a 'send' block block = NanoConfiguration.client.block_create({ type: "send", key: @private_key, account: @address, destination: recipient.address, previous: frontier, work: work, amount: amount.to_i.to_s, balance: balance })["block"] # Publish the block NanoConfiguration.client.process(block: block) end
The total balance of the account, including unpocketed balances.
# File lib/account.rb, line 174 def total_balance balance + pending_balance end