Skip to content

Commit

Permalink
fix: set max limit for swap fees (#59)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: José A.P <[email protected]>
  • Loading branch information
markettes and Jossec101 authored Nov 17, 2023
1 parent b4d354a commit cad882e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func init() {
rootCmd.Flags().Float64("backoffLimit", 0.1, "Limit coefficient of the backoff")
viper.BindPFlag("backoffLimit", rootCmd.Flags().Lookup("backoffLimit"))

// Limit fees for swaps
rootCmd.Flags().Float64("limitFees", 0.007, "Limit fees for swaps e.g. 0.01")
viper.BindPFlag("limitFees", rootCmd.Flags().Lookup("limitFees"))

//Now we set the global vars

pollingInterval = viper.GetDuration("pollingInterval")
Expand All @@ -123,6 +127,7 @@ func init() {
retries = viper.GetInt("retriesBeforeBackoff")
backoffCoefficient = viper.GetFloat64("backoffCoefficient")
backoffLimit = viper.GetFloat64("backoffLimit")
limitFees = viper.GetFloat64("limitFees")

//Set log level and format

Expand Down
1 change: 1 addition & 0 deletions liquidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
retries int
backoffCoefficient float64
backoffLimit float64
limitFees float64
)

// Entrypoint of liquidator main logic
Expand Down
23 changes: 22 additions & 1 deletion provider/loop_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/hex"
"fmt"
"os"
"reflect"
"strconv"
"time"

"github.com/Elenpay/liquidator/errors"
Expand Down Expand Up @@ -60,6 +62,16 @@ func (l *LoopProvider) RequestSubmarineSwap(ctx context.Context, request Submari
return SubmarineSwapResponse{}, err
}

limitFeesStr := os.Getenv("LIMITFEES")
limitFees, err := strconv.ParseFloat(limitFeesStr, 64)
sumFees := quote.SwapFeeSat + quote.HtlcPublishFeeSat

if sumFees > int64(float64(request.SatsAmount)*limitFees) {
err := fmt.Errorf("swap fees are greater than max limit fees")
log.Error(err)
return SubmarineSwapResponse{}, err
}

//Get limits
limits := getInLimits(quote)

Expand Down Expand Up @@ -219,11 +231,20 @@ func (l *LoopProvider) RequestReverseSubmarineSwap(ctx context.Context, request
})

if err != nil {

log.Errorf("error getting quote for reverse submarine swap: %s", err)
return ReverseSubmarineSwapResponse{}, err
}

limitFeesStr := os.Getenv("LIMITFEES")
limitFees, err := strconv.ParseFloat(limitFeesStr, 64)
sumFees := quote.SwapFeeSat + quote.HtlcSweepFeeSat + quote.PrepayAmtSat

if sumFees > int64(float64(request.SatsAmount)*limitFees) {
err := fmt.Errorf("swap fees are greater than max limit fees")
log.Error(err)
return ReverseSubmarineSwapResponse{}, err
}

//Get limits
//Amt using btcutil
amt := btcutil.Amount(request.SatsAmount)
Expand Down
3 changes: 3 additions & 0 deletions provider/loop_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"encoding/hex"
"os"
"reflect"
"testing"
"time"
Expand All @@ -16,6 +17,8 @@ func TestLoopProvider_RequestSubmarineSwap(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

os.Setenv("LIMITFEES", "0.1")

//Mock lightning swapClient GetLoopInQuote and LoopIn methods to return fake data
swapClient := NewMockSwapClientClient(ctrl)

Expand Down

0 comments on commit cad882e

Please sign in to comment.