account: check if balance exists

This commit is contained in:
c9s 2022-06-12 03:45:28 +08:00
parent 5949c7587e
commit ce70bbbc4a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -145,7 +145,12 @@ func (a *Account) UseLockedBalance(currency string, fund fixedpoint.Value) error
defer a.Unlock()
balance, ok := a.balances[currency]
if ok && balance.Locked.Compare(fund) >= 0 {
if !ok {
return fmt.Errorf("account balance %s does not exist", currency)
}
// simple case, using fund less than locked
if balance.Locked.Compare(fund) >= 0 {
balance.Locked = balance.Locked.Sub(fund)
a.balances[currency] = balance
return nil