class Ledger

Attributes

wallets[R]

Public Class Methods

new( chain=[] ) click to toggle source
# File lib/centralbank/ledger.rb, line 5
def initialize( chain=[] )
  @wallets = {}
  chain.each do |block|
    apply_transactions( block.transactions )
  end
end

Public Instance Methods

sufficient_funds?( wallet, amount ) click to toggle source
# File lib/centralbank/ledger.rb, line 12
def sufficient_funds?( wallet, amount )
  return true   if Centralbank.config.coinbase?( wallet )
  @wallets.has_key?( wallet ) && @wallets[wallet] - amount >= 0
end

Private Instance Methods

apply_transactions( transactions ) click to toggle source
# File lib/centralbank/ledger.rb, line 20
def apply_transactions( transactions )
  transactions.each do |tx|
    if sufficient_funds?(tx.from, tx.amount)
      @wallets[tx.from] -= tx.amount   unless Centralbank.config.coinbase?( tx.from )
      @wallets[tx.to] ||= 0
      @wallets[tx.to] += tx.amount
    end
  end
end