Skip to content
6 changes: 6 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ type Store interface {
status lnrpc.Payment_PaymentStatus,
options ...UpsertPaymentOption) (bool, error)

// AdjustAccountBalance modifies the given account balance by adding or
// deducting the specified amount, depending on whether isAddition is
// true or false.
AdjustAccountBalance(ctx context.Context, alias AccountID,
amount lnwire.MilliSatoshi, isAddition bool) error

// DeleteAccountPayment removes a payment entry from the account with
// the given ID. It will return the ErrPaymentNotAssociated error if the
// payment is not associated with the account.
Expand Down
2 changes: 2 additions & 0 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
// AdjustAccountBalance modifies the given account balance by adding or
// deducting the specified amount, depending on whether isAddition is true or
// false.
//
// NOTE: This is part of the Store interface.
func (s *BoltStore) AdjustAccountBalance(_ context.Context,
id AccountID, amount lnwire.MilliSatoshi, isAddition bool) error {

Expand Down
59 changes: 59 additions & 0 deletions accounts/store_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"time"

"github.com/lightninglabs/lightning-terminal/db"
Expand Down Expand Up @@ -34,6 +35,7 @@ const (
//nolint:lll
type SQLQueries interface {
AddAccountInvoice(ctx context.Context, arg sqlc.AddAccountInvoiceParams) error
AdjustAccountBalance(ctx context.Context, arg sqlc.AdjustAccountBalanceParams) (int64, error)
DeleteAccount(ctx context.Context, id int64) error
DeleteAccountPayment(ctx context.Context, arg sqlc.DeleteAccountPaymentParams) error
GetAccount(ctx context.Context, id int64) (sqlc.Account, error)
Expand Down Expand Up @@ -598,6 +600,63 @@ func (s *SQLStore) UpsertAccountPayment(ctx context.Context, alias AccountID,
})
}

// AdjustAccountBalance modifies the given account balance by adding or
// deducting the specified amount, depending on whether isAddition is true or
// false.
//
// NOTE: This is part of the Store interface.
func (s *SQLStore) AdjustAccountBalance(ctx context.Context,
alias AccountID, amount lnwire.MilliSatoshi, isAddition bool) error {

if amount > math.MaxInt64 {
return fmt.Errorf("amount %v exceeds the maximum of %v",
amount/1000, int64(math.MaxInt64)/1000)
}

var writeTxOpts db.QueriesTxOptions
return s.db.ExecTx(ctx, &writeTxOpts, func(db SQLQueries) error {
id, err := getAccountIDByAlias(ctx, db, alias)
if err != nil {
return err
}

if !isAddition {
acct, err := db.GetAccount(ctx, id)
if err != nil {
return err
}

if acct.CurrentBalanceMsat-int64(amount) < 0 {
return fmt.Errorf("cannot deduct %v from the "+
"current balance %v, as the resulting "+
"balance would be below 0",
int64(amount/1000),
acct.CurrentBalanceMsat/1000)
}
}

isAdditionInt := int64(0)
if isAddition {
isAdditionInt = 1
}

_, err = db.AdjustAccountBalance(
ctx, sqlc.AdjustAccountBalanceParams{
IsAddition: isAdditionInt,
Amount: int64(amount),
ID: id,
},
)
if errors.Is(err, sql.ErrNoRows) {
return ErrAccNotFound
} else if err != nil {
return err
}

return s.markAccountUpdated(ctx, db, id)
})
}

// DeleteAccountPayment removes a payment entry from the account with the given
// ID. It will return an error if the payment is not associated with the
// account.
Expand Down
23 changes: 23 additions & 0 deletions db/sqlc/accounts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions db/sqlc/queries/accounts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ SET current_balance_msat = $1
WHERE id = $2
RETURNING id;

-- name: AdjustAccountBalance :one
UPDATE accounts
SET current_balance_msat = current_balance_msat + CASE
WHEN sqlc.arg(is_addition) THEN sqlc.arg(amount) -- If IsAddition is true, add the amount
ELSE -sqlc.arg(amount) -- If IsAddition is false, subtract the amount
END
WHERE id = sqlc.arg(id)
RETURNING id;

Comment on lines +12 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think let's keep things simple with just having
CreditAccount and DebitAccount queries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in the new PR :)

-- name: UpdateAccountExpiry :one
UPDATE accounts
SET expiration = $1
Expand Down