Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/balance in events #363

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions db/models/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ type Invoice struct {
SettledAt bun.NullTime `json:"settled_at"`
}

type WebhookInvoicePayload struct {
ID int64 `json:"id"`
Type string `json:"type"`
UserLogin string `json:"user_login"`
Amount int64 `json:"amount"`
Fee int64 `json:"fee"`
Balance int64 `json:"balance"`
Memo string `json:"memo"`
DescriptionHash string `json:"description_hash,omitempty"`
PaymentRequest string `json:"payment_request"`
DestinationPubkeyHex string `json:"destination_pubkey_hex"`
DestinationCustomRecords map[uint64][]byte `json:"custom_records,omitempty"`
RHash string `json:"r_hash"`
Preimage string `json:"preimage"`
Keysend bool `json:"keysend"`
State string `json:"state"`
ErrorMessage string `json:"error_message,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UpdatedAt time.Time `json:"updated_at"`
SettledAt time.Time `json:"settled_at"`
}

func (i *Invoice) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.UpdateQuery:
Expand Down
5 changes: 3 additions & 2 deletions integration_tests/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (suite *RabbitMQTestSuite) SetupSuite() {
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.svc).AddInvoice)
suite.echo.POST("/payinvoice", controllers.NewPayInvoiceController(suite.svc).PayInvoice)
go func() {
err = svc.RabbitMQClient.StartPublishInvoices(ctx, svc.SubscribeIncomingOutgoingInvoices, svc.EncodeInvoiceWithUserLogin)
err = svc.RabbitMQClient.StartPublishInvoices(ctx, svc.SubscribeIncomingOutgoingInvoices, svc.AddInvoiceMetadata)
assert.NoError(suite.T(), err)
}()
}
Expand Down Expand Up @@ -150,13 +150,14 @@ func (suite *RabbitMQTestSuite) TestConsumeAndPublishInvoice() {

msg := <-m

var receivedInvoice models.Invoice
var receivedInvoice models.WebhookInvoicePayload
r := bytes.NewReader(msg.Body)
err = json.NewDecoder(r).Decode(&receivedInvoice)
assert.NoError(suite.T(), err)

assert.Equal(suite.T(), invoice.RHash, receivedInvoice.RHash)
assert.Equal(suite.T(), common.InvoiceTypeIncoming, receivedInvoice.Type)
assert.Equal(suite.T(), int64(1000), receivedInvoice.Balance)
}

func (suite *RabbitMQTestSuite) TestPublishInvoice() {
Expand Down
46 changes: 10 additions & 36 deletions lib/service/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"io"
"net/http"
"time"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/db/models"
Expand All @@ -30,16 +29,8 @@ func (svc *LndhubService) StartWebhookSubscription(ctx context.Context, url stri
}
}
func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) {

//Look up the user's login to add it to the invoice
user, err := svc.FindUser(context.Background(), invoice.UserID)
if err != nil {
svc.Logger.Error(err)
return
}

payload := new(bytes.Buffer)
err = json.NewEncoder(payload).Encode(ConvertPayload(invoice, user))
err := svc.AddInvoiceMetadata(context.Background(), payload, invoice)
if err != nil {
svc.Logger.Error(err)
return
Expand All @@ -59,28 +50,6 @@ func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) {
}
}

type WebhookInvoicePayload struct {
ID int64 `json:"id"`
Type string `json:"type"`
UserLogin string `json:"user_login"`
Amount int64 `json:"amount"`
Fee int64 `json:"fee"`
Memo string `json:"memo"`
DescriptionHash string `json:"description_hash,omitempty"`
PaymentRequest string `json:"payment_request"`
DestinationPubkeyHex string `json:"destination_pubkey_hex"`
DestinationCustomRecords map[uint64][]byte `json:"custom_records,omitempty"`
RHash string `json:"r_hash"`
Preimage string `json:"preimage"`
Keysend bool `json:"keysend"`
State string `json:"state"`
ErrorMessage string `json:"error_message,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UpdatedAt time.Time `json:"updated_at"`
SettledAt time.Time `json:"settled_at"`
}

func (svc *LndhubService) SubscribeIncomingOutgoingInvoices() (incoming, outgoing chan models.Invoice, err error) {
incomingInvoices, _, err := svc.InvoicePubSub.Subscribe(common.InvoiceTypeIncoming)
if err != nil {
Expand All @@ -93,27 +62,32 @@ func (svc *LndhubService) SubscribeIncomingOutgoingInvoices() (incoming, outgoin
return incomingInvoices, outgoingInvoices, nil
}

func (svc *LndhubService) EncodeInvoiceWithUserLogin(ctx context.Context, w io.Writer, invoice models.Invoice) error {
func (svc *LndhubService) AddInvoiceMetadata(ctx context.Context, w io.Writer, invoice models.Invoice) error {
user, err := svc.FindUser(ctx, invoice.UserID)
if err != nil {
return err
}

err = json.NewEncoder(w).Encode(ConvertPayload(invoice, user))
balance, err := svc.CurrentUserBalance(ctx, invoice.UserID)
if err != nil {
return err
}
err = json.NewEncoder(w).Encode(ConvertPayload(invoice, user, balance))
if err != nil {
return err
}

return nil
}

func ConvertPayload(invoice models.Invoice, user *models.User) (result WebhookInvoicePayload) {
return WebhookInvoicePayload{
func ConvertPayload(invoice models.Invoice, user *models.User, balance int64) (result models.WebhookInvoicePayload) {
return models.WebhookInvoicePayload{
ID: invoice.ID,
Type: invoice.Type,
UserLogin: user.Login,
Amount: invoice.Amount,
Fee: invoice.Fee,
Balance: balance,
Memo: invoice.Memo,
DescriptionHash: invoice.DescriptionHash,
PaymentRequest: invoice.PaymentRequest,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func main() {
go func() {
err = svc.RabbitMQClient.StartPublishInvoices(backGroundCtx,
svc.SubscribeIncomingOutgoingInvoices,
svc.EncodeInvoiceWithUserLogin,
svc.AddInvoiceMetadata,
)
if err != nil {
svc.Logger.Error(err)
Expand Down