From 770b37f91d3dc01bf89f147a48713f025a80e58a Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 12 Jul 2024 16:07:55 +0700 Subject: [PATCH] chore: rename transaction amount values to be clearly millisats --- api/transactions.go | 6 +- db/migrations/202407012100_transactions.go | 8 ++- db/models.go | 6 +- db/queries/get_budget_usage.go | 2 +- db/queries/get_isolated_balance.go | 4 +- .../list_transactions_controller_test.go | 4 +- .../lookup_invoice_controller_test.go | 4 +- nip47/controllers/pay_invoice_controller.go | 2 +- nip47/models/transactions.go | 6 +- nip47/notifications/nip47_notifier_test.go | 8 +-- transactions/transactions_service.go | 58 +++++++++---------- 11 files changed, 55 insertions(+), 53 deletions(-) diff --git a/api/transactions.go b/api/transactions.go index cab26e48..3a4869c7 100644 --- a/api/transactions.go +++ b/api/transactions.go @@ -64,8 +64,8 @@ func (api *api) SendPayment(ctx context.Context, invoice string) (*SendPaymentRe func toApiTransaction(transaction *transactions.Transaction) *Transaction { fee := uint64(0) - if transaction.Fee != nil { - fee = *transaction.Fee + if transaction.FeeMsat != nil { + fee = *transaction.FeeMsat } createdAt := transaction.CreatedAt.Format(time.RFC3339) @@ -95,7 +95,7 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction { DescriptionHash: transaction.DescriptionHash, Preimage: preimage, PaymentHash: transaction.PaymentHash, - Amount: transaction.Amount, + Amount: transaction.AmountMsat, AppId: transaction.AppId, FeesPaid: fee, CreatedAt: createdAt, diff --git a/db/migrations/202407012100_transactions.go b/db/migrations/202407012100_transactions.go index 3b99be6f..8db7af1c 100644 --- a/db/migrations/202407012100_transactions.go +++ b/db/migrations/202407012100_transactions.go @@ -12,6 +12,8 @@ import ( // - Adds new properties to app_permissions // - balance_type string - isolated | full // - visibility string - isolated | full +// +// - Renames max amount on app permissions to be clear its in sats var _202407012100_transactions = &gormigrate.Migration{ ID: "202407012100_transactions", Migrate: func(tx *gorm.DB) error { @@ -28,9 +30,9 @@ CREATE TABLE transactions( payment_hash text, description text, description_hash text, - amount integer, - fee integer, - fee_reserve integer, + amount_msat integer, + fee_msat integer, + fee_reserve_msat integer, created_at datetime, updated_at datetime, expires_at datetime, diff --git a/db/models.go b/db/models.go index a1a6efbb..b4bd9122 100644 --- a/db/models.go +++ b/db/models.go @@ -65,9 +65,9 @@ type Transaction struct { RequestEvent *RequestEvent Type string State string - Amount uint64 // in millisats - Fee *uint64 // in millisats - FeeReserve *uint64 // in millisats, set for unsettled outgoing payments + AmountMsat uint64 + FeeMsat *uint64 + FeeReserveMsat *uint64 // non-zero for unsettled outgoing payments only PaymentRequest string PaymentHash string Description string diff --git a/db/queries/get_budget_usage.go b/db/queries/get_budget_usage.go index 31dc0855..dd5f5f6b 100644 --- a/db/queries/get_budget_usage.go +++ b/db/queries/get_budget_usage.go @@ -14,7 +14,7 @@ func GetBudgetUsageSat(tx *gorm.DB, appPermission *db.AppPermission) uint64 { } tx. Table("transactions"). - Select("SUM(amount + coalesce(fee, 0) + coalesce(fee_reserve, 0)) as sum"). + Select("SUM(amount_msat + coalesce(fee_msat, 0) + coalesce(fee_reserve_msat, 0)) as sum"). Where("app_id = ? AND type = ? AND (state = ? OR state = ?) AND created_at > ?", appPermission.AppId, constants.TRANSACTION_TYPE_OUTGOING, constants.TRANSACTION_STATE_SETTLED, constants.TRANSACTION_STATE_PENDING, getStartOfBudget(appPermission.BudgetRenewal)).Scan(&result) return result.Sum / 1000 } diff --git a/db/queries/get_isolated_balance.go b/db/queries/get_isolated_balance.go index 4b53b29d..bfc91448 100644 --- a/db/queries/get_isolated_balance.go +++ b/db/queries/get_isolated_balance.go @@ -11,7 +11,7 @@ func GetIsolatedBalance(tx *gorm.DB, appId uint) uint64 { } tx. Table("transactions"). - Select("SUM(amount) as sum"). + Select("SUM(amount_msat) as sum"). Where("app_id = ? AND type = ? AND state = ?", appId, constants.TRANSACTION_TYPE_INCOMING, constants.TRANSACTION_STATE_SETTLED).Scan(&received) var spent struct { @@ -20,7 +20,7 @@ func GetIsolatedBalance(tx *gorm.DB, appId uint) uint64 { tx. Table("transactions"). - Select("SUM(amount + coalesce(fee, 0) + coalesce(fee_reserve, 0)) as sum"). + Select("SUM(amount_msat + coalesce(fee_msat, 0) + coalesce(fee_reserve_msat, 0)) as sum"). Where("app_id = ? AND type = ? AND (state = ? OR state = ?)", appId, constants.TRANSACTION_TYPE_OUTGOING, constants.TRANSACTION_STATE_SETTLED, constants.TRANSACTION_STATE_PENDING).Scan(&spent) return received.Sum - spent.Sum diff --git a/nip47/controllers/list_transactions_controller_test.go b/nip47/controllers/list_transactions_controller_test.go index 62a972cb..14409f7c 100644 --- a/nip47/controllers/list_transactions_controller_test.go +++ b/nip47/controllers/list_transactions_controller_test.go @@ -94,8 +94,8 @@ func TestHandleListTransactionsEvent_WithPermission(t *testing.T) { DescriptionHash: tests.MockLNClientTransactions[i].DescriptionHash, Preimage: &tests.MockLNClientTransactions[i].Preimage, PaymentHash: tests.MockLNClientTransactions[i].PaymentHash, - Amount: uint64(tests.MockLNClientTransactions[i].Amount), - Fee: &feesPaid, + AmountMsat: uint64(tests.MockLNClientTransactions[i].Amount), + FeeMsat: &feesPaid, SettledAt: &settledAt, State: transactions.TRANSACTION_STATE_SETTLED, AppId: &app.ID, diff --git a/nip47/controllers/lookup_invoice_controller_test.go b/nip47/controllers/lookup_invoice_controller_test.go index 28c7645e..fb474bf7 100644 --- a/nip47/controllers/lookup_invoice_controller_test.go +++ b/nip47/controllers/lookup_invoice_controller_test.go @@ -96,8 +96,8 @@ func TestHandleLookupInvoiceEvent_WithPermission(t *testing.T) { DescriptionHash: tests.MockLNClientTransaction.DescriptionHash, Preimage: &tests.MockLNClientTransaction.Preimage, PaymentHash: tests.MockLNClientTransaction.PaymentHash, - Amount: uint64(tests.MockLNClientTransaction.Amount), - Fee: &feesPaid, + AmountMsat: uint64(tests.MockLNClientTransaction.Amount), + FeeMsat: &feesPaid, SettledAt: &settledAt, AppId: &app.ID, }).Error diff --git a/nip47/controllers/pay_invoice_controller.go b/nip47/controllers/pay_invoice_controller.go index b6416f37..d914ed58 100644 --- a/nip47/controllers/pay_invoice_controller.go +++ b/nip47/controllers/pay_invoice_controller.go @@ -97,7 +97,7 @@ func (controller *nip47Controller) pay(ctx context.Context, bolt11 string, payme ResultType: nip47Request.Method, Result: payResponse{ Preimage: *response.Preimage, - FeesPaid: response.Fee, + FeesPaid: response.FeeMsat, }, }, tags) } diff --git a/nip47/models/transactions.go b/nip47/models/transactions.go index 1e60da9d..7efa389f 100644 --- a/nip47/models/transactions.go +++ b/nip47/models/transactions.go @@ -10,8 +10,8 @@ import ( func ToNip47Transaction(transaction *transactions.Transaction) *Transaction { fees := int64(0) - if transaction.Fee != nil { - fees = int64(*transaction.Fee) + if transaction.FeeMsat != nil { + fees = int64(*transaction.FeeMsat) } var expiresAt *int64 @@ -46,7 +46,7 @@ func ToNip47Transaction(transaction *transactions.Transaction) *Transaction { DescriptionHash: transaction.DescriptionHash, Preimage: preimage, PaymentHash: transaction.PaymentHash, - Amount: int64(transaction.Amount), + Amount: int64(transaction.AmountMsat), FeesPaid: fees, CreatedAt: transaction.CreatedAt.Unix(), ExpiresAt: expiresAt, diff --git a/nip47/notifications/nip47_notifier_test.go b/nip47/notifications/nip47_notifier_test.go index 91e73e94..e1ac3163 100644 --- a/nip47/notifications/nip47_notifier_test.go +++ b/nip47/notifications/nip47_notifier_test.go @@ -44,8 +44,8 @@ func TestSendNotification_PaymentReceived(t *testing.T) { DescriptionHash: tests.MockLNClientTransaction.DescriptionHash, Preimage: &tests.MockLNClientTransaction.Preimage, PaymentHash: tests.MockLNClientTransaction.PaymentHash, - Amount: uint64(tests.MockLNClientTransaction.Amount), - Fee: &feesPaid, + AmountMsat: uint64(tests.MockLNClientTransaction.Amount), + FeeMsat: &feesPaid, SettledAt: &settledAt, AppId: &app.ID, }).Error @@ -125,8 +125,8 @@ func TestSendNotification_PaymentSent(t *testing.T) { DescriptionHash: tests.MockLNClientTransaction.DescriptionHash, Preimage: &tests.MockLNClientTransaction.Preimage, PaymentHash: tests.MockLNClientTransaction.PaymentHash, - Amount: uint64(tests.MockLNClientTransaction.Amount), - Fee: &feesPaid, + AmountMsat: uint64(tests.MockLNClientTransaction.Amount), + FeeMsat: &feesPaid, SettledAt: &settledAt, AppId: &app.ID, }).Error diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index 221ea125..6bd33f8b 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -107,7 +107,7 @@ func (svc *transactionsService) MakeInvoice(ctx context.Context, amount int64, d RequestEventId: requestEventId, Type: lnClientTransaction.Type, State: constants.TRANSACTION_STATE_PENDING, - Amount: uint64(lnClientTransaction.Amount), + AmountMsat: uint64(lnClientTransaction.Amount), Description: description, DescriptionHash: descriptionHash, PaymentRequest: lnClientTransaction.Invoice, @@ -154,8 +154,8 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri RequestEventId: requestEventId, Type: constants.TRANSACTION_TYPE_OUTGOING, State: constants.TRANSACTION_STATE_PENDING, - FeeReserve: &feeReserve, - Amount: uint64(paymentRequest.MSatoshi), + FeeReserveMsat: &feeReserve, + AmountMsat: uint64(paymentRequest.MSatoshi), PaymentRequest: payReq, PaymentHash: paymentRequest.PaymentHash, Description: paymentRequest.Description, @@ -212,11 +212,11 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri feeReserve := uint64(0) now := time.Now() dbErr := svc.db.Model(dbTransaction).Updates(&db.Transaction{ - State: constants.TRANSACTION_STATE_SETTLED, - Preimage: &response.Preimage, - Fee: response.Fee, - FeeReserve: &feeReserve, - SettledAt: &now, + State: constants.TRANSACTION_STATE_SETTLED, + Preimage: &response.Preimage, + FeeMsat: response.Fee, + FeeReserveMsat: &feeReserve, + SettledAt: &now, }).Error if dbErr != nil { logger.Logger.WithFields(logrus.Fields{ @@ -251,8 +251,8 @@ func (svc *transactionsService) SendKeysend(ctx context.Context, amount uint64, RequestEventId: requestEventId, Type: constants.TRANSACTION_TYPE_OUTGOING, State: constants.TRANSACTION_STATE_PENDING, - FeeReserve: &feeReserve, - Amount: amount, + FeeReserveMsat: &feeReserve, + AmountMsat: amount, Metadata: string(metadataBytes), } err = tx.Create(dbTransaction).Error @@ -317,12 +317,12 @@ func (svc *transactionsService) SendKeysend(ctx context.Context, amount uint64, now := time.Now() feeReserve := uint64(0) dbErr := svc.db.Model(dbTransaction).Updates(&db.Transaction{ - State: constants.TRANSACTION_STATE_SETTLED, - PaymentHash: paymentHash, - Preimage: &preimage, - Fee: &fee, - FeeReserve: &feeReserve, - SettledAt: &now, + State: constants.TRANSACTION_STATE_SETTLED, + PaymentHash: paymentHash, + Preimage: &preimage, + FeeMsat: &fee, + FeeReserveMsat: &feeReserve, + SettledAt: &now, }).Error if dbErr != nil { logger.Logger.WithFields(logrus.Fields{ @@ -449,11 +449,11 @@ func (svc *transactionsService) checkUnsettledTransaction(ctx context.Context, t fee := uint64(lnClientTransaction.FeesPaid) feeReserve := uint64(0) dbErr := svc.db.Model(transaction).Updates(&db.Transaction{ - State: constants.TRANSACTION_STATE_SETTLED, - Preimage: &lnClientTransaction.Preimage, - Fee: &fee, - FeeReserve: &feeReserve, - SettledAt: &now, + State: constants.TRANSACTION_STATE_SETTLED, + Preimage: &lnClientTransaction.Preimage, + FeeMsat: &fee, + FeeReserveMsat: &feeReserve, + SettledAt: &now, }).Error if dbErr != nil { logger.Logger.WithFields(logrus.Fields{ @@ -498,7 +498,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. } dbTransaction = db.Transaction{ Type: constants.TRANSACTION_TYPE_INCOMING, - Amount: uint64(lnClientTransaction.Amount), + AmountMsat: uint64(lnClientTransaction.Amount), PaymentRequest: lnClientTransaction.Invoice, PaymentHash: lnClientTransaction.PaymentHash, Description: lnClientTransaction.Description, @@ -519,7 +519,7 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. fee := uint64(lnClientTransaction.FeesPaid) err := tx.Model(&dbTransaction).Updates(&db.Transaction{ - Fee: &fee, + FeeMsat: &fee, Preimage: &lnClientTransaction.Preimage, State: constants.TRANSACTION_STATE_SETTLED, SettledAt: &settledAt, @@ -563,11 +563,11 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. fee := uint64(lnClientTransaction.FeesPaid) feeReserve := uint64(0) err := svc.db.Model(&dbTransaction).Updates(&db.Transaction{ - Fee: &fee, - FeeReserve: &feeReserve, - Preimage: &lnClientTransaction.Preimage, - State: constants.TRANSACTION_STATE_SETTLED, - SettledAt: &settledAt, + FeeMsat: &fee, + FeeReserveMsat: &feeReserve, + Preimage: &lnClientTransaction.Preimage, + State: constants.TRANSACTION_STATE_SETTLED, + SettledAt: &settledAt, }).Error if err != nil { logger.Logger.WithFields(logrus.Fields{ @@ -636,7 +636,7 @@ func (svc *transactionsService) interceptSelfPayment(paymentHash string) (*lncli fee := uint64(0) err := svc.db.Model(incomingTransaction).Updates(&db.Transaction{ State: constants.TRANSACTION_STATE_SETTLED, - Fee: &fee, + FeeMsat: &fee, SettledAt: &now, }).Error if err != nil {