Skip to content

Commit 078ca5f

Browse files
committed
accounts: implement kvdb AdjustAccountBalance
To support updating an existing off-chain account’s balance by a specified amount in the database, we'll implement a corresponding functions for the different stores. This commit introduces the `AdjustAccountBalance` function in the kvdb store.
1 parent 3cd1569 commit 078ca5f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

accounts/store_kvdb.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,43 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
307307
return known, s.updateAccount(id, update)
308308
}
309309

310+
// AdjustAccountBalance modifies the given account balance by adding or
311+
// deducting the specified amount, depending on whether isAddition is true or
312+
// false.
313+
func (s *BoltStore) AdjustAccountBalance(_ context.Context,
314+
id AccountID, amount lnwire.MilliSatoshi, isAddition bool) error {
315+
316+
update := func(account *OffChainBalanceAccount) error {
317+
if amount > math.MaxInt64 {
318+
return fmt.Errorf("amount %v exceeds the maximum of %v",
319+
int64(amount/1000), int64(math.MaxInt64)/1000)
320+
}
321+
322+
if amount <= 0 {
323+
return fmt.Errorf("amount %v must be greater that 0",
324+
amount)
325+
}
326+
327+
if isAddition {
328+
account.CurrentBalance += int64(amount)
329+
} else {
330+
if account.CurrentBalance-int64(amount) < 0 {
331+
return fmt.Errorf("cannot deduct %v from the "+
332+
"current balance %v, as the resulting "+
333+
"balance would be below 0",
334+
int64(amount/1000),
335+
account.CurrentBalance/1000)
336+
}
337+
338+
account.CurrentBalance -= int64(amount)
339+
}
340+
341+
return nil
342+
}
343+
344+
return s.updateAccount(id, update)
345+
}
346+
310347
// DeleteAccountPayment removes a payment entry from the account with the given
311348
// ID. It will return the ErrPaymentNotAssociated error if the payment is not
312349
// associated with the account.

0 commit comments

Comments
 (0)