class Trader::Balance

Attributes

backend[R]
forced_currency[R]
raw[RW]
session[R]

Public Class Methods

new(_backend, _session, _currency, _forced_currency=nil) click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 3
def initialize(_backend, _session, _currency, _forced_currency=nil)
  @backend = _backend
  @session = _session
  @currency = _currency
  @forced_currency = _forced_currency
  @raw = nil
end

Public Instance Methods

amount() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 26
def amount
  convert original_currency.pack raw.amount
end
available_amount() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 30
def available_amount
  convert original_currency.pack raw.available_amount
end
convert_to(_currency) click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 19
def convert_to(_currency)
  return self if _currency == currency
  copy = self.class.new backend, session, original_currency, _currency
  copy.raw = raw unless raw.nil?
  copy
end
currency() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 11
def currency
  forced_currency || @currency
end
frozen_amount() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 34
def frozen_amount
  convert(amount - available_amount)
end
new_deposit_endpoint() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 43
def new_deposit_endpoint
  backend.generate_endpoint session, original_currency
end
original_currency() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 15
def original_currency
  @currency
end
refresh!() click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 38
def refresh!
  self.raw = backend.get_balance session, currency
  self
end
transfer(_amount, to: nil) click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 55
def transfer(_amount, to: nil)
  withdraw _amount, to: to.new_deposit_endpoint
end
withdraw(_amount, to: nil) click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 47
def withdraw(_amount, to: nil)
  _amount = currency.pack _amount
  _amount = _amount.convert_to original_currency

  backend.withdraw_to_endpoint session, currency, _amount.amount, to
  self
end

Private Instance Methods

convert(_price) click to toggle source
# File lib/trade-o-matic/core/balance.rb, line 67
def convert(_price)
  return _price if forced_currency.nil?
  _price.convert_to forced_currency
end