Skip to content

Commit 07e3f0e

Browse files
accounts: implement sqlc AdjustAccountBalance
This commit introduces the `AdjustAccountBalance` function in the sqlc store, to support updating an existing off-chain account’s balance by a specified amount for sql backends.
1 parent 078ca5f commit 07e3f0e

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

accounts/interface.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ type Store interface {
268268
status lnrpc.Payment_PaymentStatus,
269269
options ...UpsertPaymentOption) (bool, error)
270270

271+
// AdjustAccountBalance modifies the given account balance by adding or
272+
// deducting the specified amount, depending on whether isAddition is
273+
// true or false.
274+
AdjustAccountBalance(ctx context.Context, alias AccountID,
275+
amount lnwire.MilliSatoshi, isAddition bool) error
276+
271277
// DeleteAccountPayment removes a payment entry from the account with
272278
// the given ID. It will return the ErrPaymentNotAssociated error if the
273279
// payment is not associated with the account.

accounts/store_kvdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
310310
// AdjustAccountBalance modifies the given account balance by adding or
311311
// deducting the specified amount, depending on whether isAddition is true or
312312
// false.
313+
//
314+
// NOTE: This is part of the Store interface.
313315
func (s *BoltStore) AdjustAccountBalance(_ context.Context,
314316
id AccountID, amount lnwire.MilliSatoshi, isAddition bool) error {
315317

accounts/store_sql.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"errors"
99
"fmt"
10+
"math"
1011
"time"
1112

1213
"github.com/lightninglabs/lightning-terminal/db"
@@ -34,6 +35,7 @@ const (
3435
//nolint:lll
3536
type SQLQueries interface {
3637
AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
38+
AdjustAccountBalance(ctx context.Context, arg sqlc.AdjustAccountBalanceParams) (int64, error)
3739
DeleteAccount(ctx context.Context, id int64) error
3840
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
3941
GetAccount(ctx context.Context, id int64) (sqlc.Account, error)
@@ -598,6 +600,58 @@ func (s *SQLStore) UpsertAccountPayment(ctx context.Context, alias AccountID,
598600
})
599601
}
600602

603+
// AdjustAccountBalance modifies the given account balance by adding or
604+
// deducting the specified amount, depending on whether isAddition is true or
605+
// false.
606+
//
607+
// NOTE: This is part of the Store interface.
608+
func (s *SQLStore) AdjustAccountBalance(ctx context.Context,
609+
alias AccountID, amount lnwire.MilliSatoshi, isAddition bool) error {
610+
611+
if amount > math.MaxInt64 {
612+
return fmt.Errorf("amount %v exceeds the maximum of %v",
613+
amount/1000, int64(math.MaxInt64)/1000)
614+
}
615+
616+
var writeTxOpts db.QueriesTxOptions
617+
return s.db.ExecTx(ctx, &writeTxOpts, func(db SQLQueries) error {
618+
id, err := getAccountIDByAlias(ctx, db, alias)
619+
if err != nil {
620+
return err
621+
}
622+
623+
if !isAddition {
624+
acct, err := db.GetAccount(ctx, id)
625+
if err != nil {
626+
return err
627+
}
628+
629+
if acct.CurrentBalanceMsat-int64(amount) < 0 {
630+
return fmt.Errorf("cannot deduct %v from the "+
631+
"current balance %v, as the resulting "+
632+
"balance would be below 0",
633+
int64(amount/1000),
634+
acct.CurrentBalanceMsat/1000)
635+
}
636+
}
637+
638+
_, err = db.AdjustAccountBalance(
639+
ctx, sqlc.AdjustAccountBalanceParams{
640+
ID: id,
641+
Amount: int64(amount),
642+
IsAddition: isAddition,
643+
},
644+
)
645+
if errors.Is(err, sql.ErrNoRows) {
646+
return ErrAccNotFound
647+
} else if err != nil {
648+
return err
649+
}
650+
651+
return s.markAccountUpdated(ctx, db, id)
652+
})
653+
}
654+
601655
// DeleteAccountPayment removes a payment entry from the account with the given
602656
// ID. It will return an error if the payment is not associated with the
603657
// account.

db/sqlc/accounts.sql.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/sqlc/queries/accounts.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ SET current_balance_msat = $1
99
WHERE id = $2
1010
RETURNING id;
1111

12+
-- name: AdjustAccountBalance :one
13+
UPDATE accounts
14+
SET current_balance_msat = current_balance_msat + CASE
15+
WHEN sqlc.arg(is_addition)::BOOLEAN THEN sqlc.arg(amount) -- If IsAddition is true, add the amount
16+
ELSE -sqlc.arg(amount) -- If IsAddition is false, subtract the amount
17+
END
18+
WHERE id = sqlc.arg(id)
19+
RETURNING id;
20+
1221
-- name: UpdateAccountExpiry :one
1322
UPDATE accounts
1423
SET expiration = $1

0 commit comments

Comments
 (0)