Sha256: 31cec77a9c1dae84f55c619d75f7937675d797d955ad969f379405ca92e11e02

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module BitWallet
  class Account

    attr_reader :wallet, :name
    delegate :client, to: :wallet

    def initialize(wallet, name)
      @wallet = wallet
      @name = name
      self.addresses.new
    end

    def addresses
      @addresses ||= Addresses.new(self)
    end

    def balance(min_conf=BitWallet.config.min_conf)
      client.getbalance(self.name, min_conf)
    end

    def send_amount(amount, options={})
      if options[:to]
        options[:to] = options[:to].address if options[:to].is_a?(Address)
      else
        fail ArgumentError, 'address must be specified'
      end

      client.sendfrom(self.name,
                      options[:to],
                      amount,
                      BitWallet.config.min_conf)
    rescue Bitcoin::Errors::RPCError => e
      parse_error e.message
    end

    def total_received
      client.getreceivedbyaccount(self.name, BitWallet.config.min_conf)
    end

    def ==(other_account)
      self.name == other_account.name
    end

    def recent_transactions(options={})
      count = options.delete(:limit) || 10
      client.listtransactions(self.name, count).map do |hash|
        Transaction.new self.wallet, hash
      end
    end

    def send_many(account_values={})
      addresses_values = {}
      account_values.each do |key, value|
        address = key.respond_to?(:address) ? key.address : key
        addresses_values[address] = value
      end

      txid = client.send_many(self.name,
                              addresses_values,
                              BitWallet.config.min_conf)
      txid
    rescue => e
      error_message = JSON.parse(e.response).with_indifferent_access
      if error_message[:error][:code] == -6
        fail InsufficientFunds, "'#{self.name}' does not have enough funds"
      else
        raise e
      end
    end

    private

    def parse_error(message)
      HandlesError.from(message)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bit_wallet-0.5.0 lib/bit_wallet/account.rb