list_transactions_controller.go raw

   1  package controllers
   2  
   3  import (
   4  	"context"
   5  
   6  	"github.com/getAlby/go-nostr"
   7  	"github.com/getAlby/hub/logger"
   8  	"github.com/getAlby/hub/nip47/models"
   9  	"github.com/getAlby/hub/transactions"
  10  	"github.com/sirupsen/logrus"
  11  )
  12  
  13  type listTransactionsParams struct {
  14  	From           uint64 `json:"from,omitempty"`
  15  	Until          uint64 `json:"until,omitempty"`
  16  	Limit          uint64 `json:"limit,omitempty"`
  17  	Offset         uint64 `json:"offset,omitempty"`
  18  	Unpaid         bool   `json:"unpaid,omitempty"`
  19  	UnpaidOutgoing bool   `json:"unpaid_outgoing,omitempty"`
  20  	UnpaidIncoming bool   `json:"unpaid_incoming,omitempty"`
  21  	Type           string `json:"type,omitempty"`
  22  }
  23  
  24  type listTransactionsResponse struct {
  25  	Transactions []models.Transaction `json:"transactions"`
  26  	TotalCount   uint64               `json:"total_count"`
  27  }
  28  
  29  func (controller *nip47Controller) HandleListTransactionsEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, appId uint, publishResponse publishFunc) {
  30  
  31  	listParams := &listTransactionsParams{}
  32  	resp := decodeRequest(nip47Request, listParams)
  33  	if resp != nil {
  34  		publishResponse(resp, nostr.Tags{})
  35  		return
  36  	}
  37  
  38  	logger.Logger.WithFields(logrus.Fields{
  39  		"params":           listParams,
  40  		"request_event_id": requestEventId,
  41  	}).Debug("Fetching transactions")
  42  
  43  	limit := listParams.Limit
  44  	maxLimit := uint64(50)
  45  	if limit == 0 || limit > maxLimit {
  46  		// make sure a sensible limit is passed
  47  		limit = maxLimit
  48  	}
  49  	var transactionType *string
  50  	if listParams.Type != "" {
  51  		transactionType = &listParams.Type
  52  	}
  53  
  54  	dbTransactions, totalCount, err := controller.transactionsService.ListTransactions(ctx, listParams.From, listParams.Until, limit, listParams.Offset, listParams.Unpaid || listParams.UnpaidOutgoing, listParams.Unpaid || listParams.UnpaidIncoming, controller.lnClient, &appId, false, &transactions.ListTransactionsFilters{
  55  		Type: transactionType,
  56  	})
  57  	if err != nil {
  58  		logger.Logger.WithFields(logrus.Fields{
  59  			"params":           listParams,
  60  			"request_event_id": requestEventId,
  61  		}).WithError(err).Error("Failed to fetch transactions")
  62  
  63  		publishResponse(&models.Response{
  64  			ResultType: nip47Request.Method,
  65  			Error:      mapNip47Error(err),
  66  		}, nostr.Tags{})
  67  		return
  68  	}
  69  
  70  	transactions := []models.Transaction{}
  71  	for _, dbTransaction := range dbTransactions {
  72  		transactions = append(transactions, *models.ToNip47Transaction(&dbTransaction))
  73  	}
  74  
  75  	responsePayload := &listTransactionsResponse{
  76  		Transactions: transactions,
  77  		TotalCount:   totalCount,
  78  	}
  79  
  80  	publishResponse(&models.Response{
  81  		ResultType: nip47Request.Method,
  82  		Result:     responsePayload,
  83  	}, nostr.Tags{})
  84  }
  85