Account: Add account.Balance.Available() method (#777)

* add account.Balance.Available() method that returns the amount of an asset not locked/held

* account: comment Balance.Available()

* account: test Balance.Available()
This commit is contained in:
Yordan Miladinov
2021-09-09 02:35:10 +03:00
committed by GitHub
parent 4e5d094975
commit 9e745d20a3
2 changed files with 32 additions and 0 deletions

View File

@@ -93,3 +93,9 @@ func (s *Service) Update(a *Holdings) error {
return s.mux.Publish([]uuid.UUID{acc.ID}, acc.h)
}
// Available returns the amount you can use immediately. E.g. if you have $100, but $20
// are held (locked) because of a limit buy order, your available balance is $80.
func (b Balance) Available() float64 {
return b.TotalValue - b.Hold
}

View File

@@ -134,3 +134,29 @@ func TestHoldings(t *testing.T) {
wg.Wait()
}
func TestBalance_Available(t *testing.T) {
t.Parallel()
b := Balance{
CurrencyName: currency.BTC,
TotalValue: 16,
Hold: 0,
}
if have := b.Available(); have != 16 {
t.Errorf("have %f, want 16", have)
}
b.Hold = 8
if have := b.Available(); have != 8 {
t.Errorf("have %f, want 8", have)
}
b.Hold = 16
if have := b.Available(); have != 0 {
t.Errorf("have %f, want 0", have)
}
}