Skip to content

Commit

Permalink
Account: Add account.Balance.Available() method (thrasher-corp#777)
Browse files Browse the repository at this point in the history
* add account.Balance.Available() method that returns the amount of an asset not locked/held

* account: comment Balance.Available()

* account: test Balance.Available()
  • Loading branch information
ydm authored Sep 8, 2021
1 parent 4e5d094 commit 9e745d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions exchanges/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions exchanges/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 9e745d2

Please sign in to comment.