Skip to content

Commit

Permalink
chore: rename transaction amount values to be clearly millisats
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 12, 2024
1 parent 6827da5 commit 770b37f
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 53 deletions.
6 changes: 3 additions & 3 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions db/migrations/202407012100_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion db/queries/get_budget_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions db/queries/get_isolated_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions nip47/controllers/list_transactions_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 100 in nip47/controllers/list_transactions_controller_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: transactions.TRANSACTION_STATE_SETTLED

Check failure on line 100 in nip47/controllers/list_transactions_controller_test.go

View workflow job for this annotation

GitHub Actions / build (armv6, arm-unknown-linux-gnueabihf)

undefined: transactions.TRANSACTION_STATE_SETTLED

Check failure on line 100 in nip47/controllers/list_transactions_controller_test.go

View workflow job for this annotation

GitHub Actions / build (aarch64, linux-aarch64, aarch64-unknown-linux-gnu)

undefined: transactions.TRANSACTION_STATE_SETTLED
AppId: &app.ID,
Expand Down
4 changes: 2 additions & 2 deletions nip47/controllers/lookup_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nip47/controllers/pay_invoice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions nip47/models/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions nip47/notifications/nip47_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 29 additions & 29 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 770b37f

Please sign in to comment.