Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

feat: add payment_sent notification #436

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions events/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type PaymentReceivedEventProperties struct {
NodeType string `json:"node_type"`
}

type PaymentSentEventProperties struct {
PaymentId string `json:"payment_id"`
rolznz marked this conversation as resolved.
Show resolved Hide resolved
PaymentHash string `json:"payment_hash"`
Amount uint64 `json:"amount"`
NodeType string `json:"node_type"`
}

type ChannelBackupEvent struct {
Channels []ChannelBackupInfo `json:"channels"`
}
Expand Down
10 changes: 10 additions & 0 deletions lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,16 @@ func (ls *LDKService) handleLdkEvent(event *ldk_node.Event) {
NodeType: config.LDKBackendType,
},
})
case ldk_node.EventPaymentSuccessful:
ls.eventPublisher.Publish(&events.Event{
Event: "nwc_payment_sent",
Properties: &events.PaymentSentEventProperties{
PaymentId: *eventType.PaymentId,
PaymentHash: eventType.PaymentHash,
Amount: *eventType.FeePaidMsat / 1000,
NodeType: config.LDKBackendType,
},
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion nip47/notifications/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ type Notification struct {
}

const (
NOTIFICATION_TYPES = "payment_received" // e.g. "payment_received payment_sent balance_updated payment_sent channel_opened channel_closed ..."
NOTIFICATION_TYPES = "payment_received payment_sent" // e.g. "payment_received payment_sent balance_updated payment_sent channel_opened channel_closed ..."
PAYMENT_RECEIVED_NOTIFICATION = "payment_received"
PAYMENT_SENT_NOTIFICATION = "payment_sent"
)

type PaymentReceivedNotification struct {
Expand Down
54 changes: 40 additions & 14 deletions nip47/notifications/nip47_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,54 @@ func NewNip47Notifier(relay Relay, db *gorm.DB, cfg config.Config, keys keys.Key
}

func (notifier *Nip47Notifier) ConsumeEvent(ctx context.Context, event *events.Event) error {
if event.Event != "nwc_payment_received" {
if event.Event != "nwc_payment_received" && event.Event != "nwc_payment_sent" {
return nil
}

paymentReceivedEventProperties, ok := event.Properties.(*events.PaymentReceivedEventProperties)
if !ok {
logger.Logger.WithField("event", event).Error("Failed to cast event")
return errors.New("failed to cast event")
}
var transaction interface{}
rolznz marked this conversation as resolved.
Show resolved Hide resolved
var notificationType string
switch event.Event {
case "nwc_payment_received":
paymentReceivedEventProperties, ok := event.Properties.(*events.PaymentReceivedEventProperties)
if !ok {
logger.Logger.WithField("event", event).Error("Failed to cast event")
return errors.New("failed to cast event")
}

transaction, err := notifier.lnClient.LookupInvoice(ctx, paymentReceivedEventProperties.PaymentHash)
if err != nil {
logger.Logger.
WithField("paymentHash", paymentReceivedEventProperties.PaymentHash).
WithError(err).
Error("Failed to lookup invoice by payment hash")
return err
var err error
transaction, err = notifier.lnClient.LookupInvoice(ctx, paymentReceivedEventProperties.PaymentHash)
if err != nil {
logger.Logger.
WithField("paymentHash", paymentReceivedEventProperties.PaymentHash).
WithError(err).
Error("Failed to lookup invoice by payment hash")
return err
}
notificationType = PAYMENT_RECEIVED_NOTIFICATION
case "nwc_payment_sent":
paymentSentEventProperties, ok := event.Properties.(*events.PaymentSentEventProperties)
if !ok {
logger.Logger.WithField("event", event).Error("Failed to cast event")
return errors.New("failed to cast event")
}

var err error
transaction, err = notifier.lnClient.LookupInvoice(ctx, paymentSentEventProperties.PaymentHash)
if err != nil {
logger.Logger.
WithField("paymentHash", paymentSentEventProperties.PaymentHash).
WithError(err).
Error("Failed to lookup invoice by payment hash")
return err
}
notificationType = PAYMENT_SENT_NOTIFICATION
default:
logger.Logger.Fatalf("Unsupported notification type: %v", event.Event)
}

notifier.notifySubscribers(ctx, &Notification{
Notification: transaction,
NotificationType: PAYMENT_RECEIVED_NOTIFICATION,
NotificationType: notificationType,
}, nostr.Tags{})
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions nip47/notifications/nip47_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ func TestSendNotification(t *testing.T) {
assert.Equal(t, tests.MockTransaction.Amount, transaction.Amount)
assert.Equal(t, tests.MockTransaction.FeesPaid, transaction.FeesPaid)
assert.Equal(t, tests.MockTransaction.SettledAt, transaction.SettledAt)

rolznz marked this conversation as resolved.
Show resolved Hide resolved
testEvent = &events.Event{
Event: "nwc_payment_sent",
Properties: &events.PaymentSentEventProperties{
PaymentId: tests.MockPaymentId,
PaymentHash: tests.MockPaymentHash,
Amount: uint64(tests.MockTransaction.Amount),
NodeType: "LDK",
},
}

svc.EventPublisher.Publish(testEvent)

receivedEvent = <-nip47NotificationQueue.Channel()
assert.Equal(t, testEvent, receivedEvent)

notifier.ConsumeEvent(ctx, receivedEvent)

assert.NotNil(t, relay.publishedEvent)
assert.NotEmpty(t, relay.publishedEvent.Content)
}

func TestSendNotificationNoPermission(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions tests/mock_ln_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const MockPaymentHash500 = "be8ad5d0b82071d538dcd160e3a3af444bd890de68388a4d771b

const MockInvoice = "lntb1230n1pjypux0pp5xgxzcks5jtx06k784f9dndjh664wc08ucrganpqn52d0ftrh9n8sdqyw3jscqzpgxqyz5vqsp5rkx7cq252p3frx8ytjpzc55rkgyx2mfkzzraa272dqvr2j6leurs9qyyssqhutxa24r5hqxstchz5fxlslawprqjnarjujp5sm3xj7ex73s32sn54fthv2aqlhp76qmvrlvxppx9skd3r5ut5xutgrup8zuc6ay73gqmra29m"
const MockPaymentHash = "320c2c5a1492ccfd5bc7aa4ad9b657d6aaec3cfcc0d1d98413a29af4ac772ccf" // for the above invoice
const MockPaymentId = "paymentId123"

var MockNodeInfo = lnclient.NodeInfo{
Alias: "bob",
Expand Down
Loading