Skip to content

Commit

Permalink
Merge pull request #435 from getAlby/task-multi-keysend
Browse files Browse the repository at this point in the history
feat: use background context in multikeysend
  • Loading branch information
kiwiidb authored Sep 29, 2023
2 parents feaabb9 + a5cfd0d commit d1f31f3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions controllers_v2/keysend.ctrl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2controllers

import (
"context"
"encoding/hex"
"fmt"
"net/http"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}

result, errResp := controller.SingleKeySend(c, &reqBody, userID)
result, errResp := controller.SingleKeySend(c.Request().Context(), &reqBody, userID)
if errResp != nil {
c.Logger().Errorf("Failed to send keysend: %s", errResp.Message)
return c.JSON(errResp.HttpStatusCode, errResp)
Expand Down Expand Up @@ -124,7 +125,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
singleSuccesfulPayment := false
for _, keysend := range reqBody.Keysends {
keysend := keysend
res, err := controller.SingleKeySend(c, &keysend, userID)
res, err := controller.SingleKeySend(context.Background(), &keysend, userID)
if err != nil {
controller.svc.Logger.Errorf("Error making keysend split payment %v %s", keysend, err.Message)
result.Keysends = append(result.Keysends, KeySendResult{
Expand All @@ -148,7 +149,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
return c.JSON(status, result)
}

func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeySendRequestBody, userID int64) (result *KeySendResponseBody, resp *responses.ErrorResponse) {
func (controller *KeySendController) SingleKeySend(ctx context.Context, reqBody *KeySendRequestBody, userID int64) (result *KeySendResponseBody, resp *responses.ErrorResponse) {
lnPayReq := &lnd.LNPayReq{
PayReq: &lnrpc.PayReq{
Destination: reqBody.Destination,
Expand Down Expand Up @@ -180,21 +181,21 @@ func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeyS
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)

Check failure on line 181 in controllers_v2/keysend.ctrl.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

undefined: c
return nil, resp
}
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, "", lnPayReq)
invoice, err := controller.svc.AddOutgoingInvoice(ctx, userID, "", lnPayReq)
if err != nil {
controller.svc.Logger.Error(err)
return nil, &responses.GeneralServerError
}
if _, err := hex.DecodeString(invoice.DestinationPubkeyHex); err != nil || len(invoice.DestinationPubkeyHex) != common.DestinationPubkeyHexSize {
c.Logger().Errorf("Invalid destination pubkey hex user_id:%v pubkey:%v", userID, len(invoice.DestinationPubkeyHex))
controller.svc.Logger.Errorf("Invalid destination pubkey hex user_id:%v pubkey:%v", userID, len(invoice.DestinationPubkeyHex))
return nil, &responses.InvalidDestinationError
}
invoice.DestinationCustomRecords = map[uint64][]byte{}

for key, value := range customRecords {
intKey, err := strconv.Atoi(key)
if err != nil {
c.Logger().Errorj(
controller.svc.Logger.Errorj(
log.JSON{
"message": "invalid custom records",
"error": err,
Expand All @@ -205,9 +206,9 @@ func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeyS
}
invoice.DestinationCustomRecords[uint64(intKey)] = []byte(value)
}
sendPaymentResponse, err := controller.svc.PayInvoice(c.Request().Context(), invoice)
sendPaymentResponse, err := controller.svc.PayInvoice(ctx, invoice)
if err != nil {
c.Logger().Errorf("Payment failed: user_id:%v error: %v", userID, err)
controller.svc.Logger.Errorf("Payment failed: user_id:%v error: %v", userID, err)
sentry.CaptureException(err)
return nil, &responses.ErrorResponse{
Error: true,
Expand Down

0 comments on commit d1f31f3

Please sign in to comment.