Skip to content

Commit

Permalink
chore: add tests for self payments
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 19, 2024
1 parent 22fe5aa commit 3f749bf
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (api *api) LookupInvoice(ctx context.Context, paymentHash string) (*LookupI
if api.svc.GetLNClient() == nil {
return nil, errors.New("LNClient not started")
}
transaction, err := api.svc.GetTransactionsService().LookupTransaction(ctx, paymentHash, api.svc.GetLNClient(), nil)
transaction, err := api.svc.GetTransactionsService().LookupTransaction(ctx, paymentHash, nil, api.svc.GetLNClient(), nil)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion db/migrations/202407012100_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ CREATE TABLE transactions(
updated_at datetime,
expires_at datetime,
settled_at datetime,
metadata text
metadata text,
self_payment boolean
);
DROP TABLE payments;
Expand Down
1 change: 1 addition & 0 deletions db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Transaction struct {
UpdatedAt time.Time
SettledAt *time.Time
Metadata string
SelfPayment bool
}

type DBService interface {
Expand Down
2 changes: 1 addition & 1 deletion nip47/controllers/lookup_invoice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (controller *nip47Controller) HandleLookupInvoiceEvent(ctx context.Context,
paymentHash = paymentRequest.PaymentHash
}

dbTransaction, err := controller.transactionsService.LookupTransaction(ctx, paymentHash, controller.lnClient, &appId)
dbTransaction, err := controller.transactionsService.LookupTransaction(ctx, paymentHash, nil, controller.lnClient, &appId)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
Expand Down
6 changes: 4 additions & 2 deletions nip47/notifications/nip47_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (notifier *Nip47Notifier) ConsumeEvent(ctx context.Context, event *events.E
return
}

transaction, err := notifier.transactionsService.LookupTransaction(ctx, lnClientTransaction.PaymentHash, notifier.lnClient, nil)
transactionType := constants.TRANSACTION_TYPE_INCOMING
transaction, err := notifier.transactionsService.LookupTransaction(ctx, lnClientTransaction.PaymentHash, &transactionType, notifier.lnClient, nil)
if err != nil {
logger.Logger.
WithField("paymentHash", lnClientTransaction.PaymentHash).
Expand All @@ -79,7 +80,8 @@ func (notifier *Nip47Notifier) ConsumeEvent(ctx context.Context, event *events.E
return
}

transaction, err := notifier.transactionsService.LookupTransaction(ctx, paymentSentEventProperties.PaymentHash, notifier.lnClient, nil)
transactionType := constants.TRANSACTION_TYPE_OUTGOING
transaction, err := notifier.transactionsService.LookupTransaction(ctx, paymentSentEventProperties.PaymentHash, &transactionType, notifier.lnClient, nil)
if err != nil {
logger.Logger.
WithField("paymentHash", paymentSentEventProperties.PaymentHash).
Expand Down
5 changes: 5 additions & 0 deletions tests/mock_ln_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var MockLNClientTransaction = &MockLNClientTransactions[0]
type MockLn struct {
PayInvoiceResponses []*lnclient.PayInvoiceResponse
PayInvoiceErrors []error
Pubkey string
}

func NewMockLn() (*MockLn, error) {
Expand Down Expand Up @@ -178,5 +179,9 @@ func (mln *MockLn) GetSupportedNIP47NotificationTypes() []string {
return []string{"payment_received", "payment_sent"}
}
func (mln *MockLn) GetPubkey() string {
if mln.Pubkey != "" {
return mln.Pubkey
}

return "123pubkey"
}
Loading

0 comments on commit 3f749bf

Please sign in to comment.