From 67e5dbabdd384aa2d4d80cd8f98efc3cecaed797 Mon Sep 17 00:00:00 2001 From: im-adithya Date: Wed, 30 Apr 2025 18:58:25 +0530 Subject: [PATCH] chore: replace hardcoded value with fetched fee rate --- swaps/swaps_service.go | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/swaps/swaps_service.go b/swaps/swaps_service.go index efc7692e..f43ba7a3 100644 --- a/swaps/swaps_service.go +++ b/swaps/swaps_service.go @@ -4,8 +4,11 @@ import ( "context" "crypto/rand" "crypto/sha256" + "encoding/json" "errors" "fmt" + "io" + "net/http" "strconv" "time" @@ -34,6 +37,14 @@ type SwapsService interface { ReverseSwap(ctx context.Context, amount uint64, destination string, lnClient lnclient.LNClient) error } +type FeeRates struct { + FastestFee uint64 `json:"fastestFee"` + HalfHourFee uint64 `json:"halfHourFee"` + HourFee uint64 `json:"hourFee"` + EconomyFee uint64 `json:"economyFee"` + MinimumFee uint64 `json:"minimumFee"` +} + func NewSwapsService(cfg config.Config, eventPublisher events.EventPublisher, transactionsService transactions.TransactionsService) *swapsService { return &swapsService{ cfg: cfg, @@ -244,7 +255,11 @@ func (svc *swapsService) ReverseSwap(ctx context.Context, amount uint64, destina return err } - satPerVbyte := float64(2) + feeRates, err := svc.GetFeeRates() + if err != nil { + return err + } + claimTransaction, _, err := boltz.ConstructTransaction( network, boltz.CurrencyBtc, @@ -261,7 +276,7 @@ func (svc *swapsService) ReverseSwap(ctx context.Context, amount uint64, destina Cooperative: true, }, }, - satPerVbyte, + float64(feeRates.FastestFee), svc.boltzApi, ) if err != nil { @@ -315,3 +330,47 @@ func (svc *swapsService) CalculateFee() (float64, error) { return fees.Percentage, nil } + +func (svc *swapsService) GetFeeRates() (*FeeRates, error) { + url := svc.cfg.GetEnv().MempoolApi + "/v1/fees/recommended" + + client := http.Client{ + Timeout: time.Second * 10, + } + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + logger.Logger.WithError(err).WithFields(logrus.Fields{ + "url": url, + }).Error("Failed to create http request") + return nil, err + } + + res, err := client.Do(req) + if err != nil { + logger.Logger.WithError(err).WithFields(logrus.Fields{ + "url": url, + }).Error("Failed to send request") + return nil, err + } + + defer res.Body.Close() + + body, readErr := io.ReadAll(res.Body) + if readErr != nil { + logger.Logger.WithError(err).WithFields(logrus.Fields{ + "url": url, + }).Error("Failed to read response body") + return nil, errors.New("failed to read response body") + } + + var rates FeeRates + jsonErr := json.Unmarshal(body, &rates) + if jsonErr != nil { + logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ + "url": url, + }).Error("Failed to deserialize json") + return nil, fmt.Errorf("failed to deserialize json %s %s", url, string(body)) + } + return &rates, nil +}