Skip to content

Commit

Permalink
refactor: use local / remote terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Jul 7, 2024
1 parent 0b5c856 commit 3ea3f98
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 236 deletions.
8 changes: 4 additions & 4 deletions autoswap/autoswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ channelPollInterval = "30"
currency = "LBTC"
enabled = false
failureBackoff = "86400"
maxBalance = "0"
maxBalancePercent = 75.0
remoteBalance = "0"
remoteBalancePercent = 25.0
maxFeePercent = 1.0
maxSwapAmount = "0"
minBalance = "0"
minBalancePercent = 25.0
localBalance = "0"
localBalancePercent = 25.0
perChannel = false
staticAddress = ""
swapType = ""
Expand Down
7 changes: 5 additions & 2 deletions autoswap/balance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package autoswap

import "github.com/BoltzExchange/boltz-client/utils"
import (
"github.com/BoltzExchange/boltz-client/utils"
"math"
)

type Balance struct {
Absolute uint64
Expand All @@ -17,7 +20,7 @@ func (b Balance) IsAbsolute() bool {

func (b Balance) Get(capacity uint64) uint64 {
if b.IsAbsolute() {
return b.Absolute
return uint64(math.Min(float64(b.Absolute), float64(capacity)))
}
return b.Relative.Calculate(capacity)
}
Expand Down
37 changes: 19 additions & 18 deletions autoswap/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestGetPair(t *testing.T) {
func TestLightningConfig(t *testing.T) {
enabled := func(cfg *SerializedLnConfig) *SerializedLnConfig {
cfg.Enabled = true
cfg.MaxBalancePercent = 75
cfg.MinBalancePercent = 25
cfg.RemoteBalancePercent = 25
cfg.LocalBalancePercent = 25
return cfg
}
tt := []struct {
Expand All @@ -49,41 +49,42 @@ func TestLightningConfig(t *testing.T) {
{
name: "MissingMax",
cfg: &SerializedLnConfig{
MinBalancePercent: 25,
LocalBalancePercent: 25,
},
err: true,
},
{
name: "ValidReverse",
cfg: &SerializedLnConfig{
MaxBalancePercent: 75,
SwapType: "reverse",
RemoteBalancePercent: 25,
SwapType: "reverse",
},
err: false,
},
{
name: "MinGreaterMax/Percent",
cfg: &SerializedLnConfig{
MinBalancePercent: 75,
MaxBalancePercent: 25,
LocalBalancePercent: 75,
RemoteBalancePercent: 75,
},
err: true,
},

{
name: "MinGreaterMax/Abs",
cfg: &SerializedLnConfig{
MinBalance: 10000,
MaxBalance: 5000,
/*
{
name: "MinGreaterMax/Abs",
cfg: &SerializedLnConfig{
LocalBalance: 10000,
MaxBalance: 5000,
},
err: true,
},
err: true,
},
*/
{
name: "PerChannel/SubmarineForbidden",
cfg: &SerializedLnConfig{
MinBalance: 10000,
PerChannel: true,
SwapType: "submarine",
LocalBalance: 10000,
PerChannel: true,
SwapType: "submarine",
},
err: true,
},
Expand Down
49 changes: 23 additions & 26 deletions autoswap/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type LightningConfig struct {
maxFeePercent utils.Percentage
currency boltz.Currency
swapType boltz.SwapType
maxBalance Balance
minBalance Balance
localBalance Balance
remoteBalance Balance
strategy Strategy
description string
walletId *database.Id
Expand All @@ -52,9 +52,9 @@ func DefaultLightningConfig() *SerializedLnConfig {
// we can't include values like currency in the base config
// since we couldn't know wether the user didn't set the currency at all or set it to BTC
return withLightningBase(&SerializedLnConfig{
MaxBalancePercent: 75,
MinBalancePercent: 25,
Currency: boltzrpc.Currency_LBTC,
LocalBalancePercent: 25,
RemoteBalancePercent: 25,
Currency: boltzrpc.Currency_LBTC,
})
}

Expand All @@ -67,27 +67,24 @@ func (cfg *LightningConfig) Init() error {

cfg.currency = utils.ParseCurrency(&cfg.Currency)
cfg.maxFeePercent = utils.Percentage(cfg.MaxFeePercent)
cfg.maxBalance = Balance{Absolute: cfg.MaxBalance}
cfg.minBalance = Balance{Absolute: cfg.MinBalance}
cfg.localBalance = Balance{Absolute: cfg.LocalBalance}
cfg.remoteBalance = Balance{Absolute: cfg.RemoteBalance}

// Only consider relative values if absolute values are not set
if cfg.MaxBalance == 0 && cfg.MinBalance == 0 {
cfg.maxBalance.Relative = utils.Percentage(cfg.MaxBalancePercent)
cfg.minBalance.Relative = utils.Percentage(cfg.MinBalancePercent)
if cfg.RemoteBalance == 0 && cfg.LocalBalance == 0 {
cfg.localBalance.Relative = utils.Percentage(cfg.LocalBalancePercent)
cfg.remoteBalance.Relative = utils.Percentage(cfg.RemoteBalancePercent)
if cfg.LocalBalancePercent+cfg.RemoteBalancePercent >= 100 {
return errors.New("sum of balance percentages must be smaller than 100")
}
}

if cfg.minBalance.IsZero() && cfg.maxBalance.IsZero() {
if cfg.remoteBalance.IsZero() && cfg.localBalance.IsZero() {
return errors.New("no balance threshold set")
}

if !cfg.maxBalance.IsZero() && !cfg.minBalance.IsZero() {
if cfg.minBalance.Get(100) > cfg.maxBalance.Get(100) {
return errors.New("min balance must be smaller than max balance")
}
}

if cfg.PerChannel {
if cfg.minBalance.IsAbsolute() {
if cfg.remoteBalance.IsAbsolute() {
return errors.New("absolute balance threshold not supported for per channel rebalancing")
}
if cfg.AllowNormalSwaps() {
Expand All @@ -100,18 +97,18 @@ func (cfg *LightningConfig) Init() error {
cfg.description = "total balance"
}

if cfg.minBalance.IsZero() {
if cfg.localBalance.IsZero() {
if cfg.AllowNormalSwaps() {
return errors.New("min balance must be set for normal swaps")
return errors.New("local balance must be set for normal swaps")
}
cfg.description += fmt.Sprintf(" (max %s)", cfg.maxBalance)
} else if cfg.maxBalance.IsZero() {
cfg.description += fmt.Sprintf(" (remote %s)", cfg.remoteBalance)
} else if cfg.remoteBalance.IsZero() {
if cfg.AllowReverseSwaps() {
return errors.New("max balance must be set for reverse swaps")
return errors.New("remote balance must be set for reverse swaps")
}
cfg.description += fmt.Sprintf(" (min %s)", cfg.minBalance)
cfg.description += fmt.Sprintf(" (local %s)", cfg.localBalance)
} else {
cfg.description += fmt.Sprintf(" (min %s, max %s)", cfg.minBalance, cfg.maxBalance)
cfg.description += fmt.Sprintf(" (local %s, remote %s)", cfg.remoteBalance, cfg.localBalance)
}

if cfg.swapType != "" {
Expand All @@ -138,7 +135,7 @@ func (cfg *LightningConfig) InitWallet() (err error) {
AllowReadonly: !cfg.AllowNormalSwaps(),
})
if err != nil {
err = fmt.Errorf("could not find from wallet: %s", err)
err = fmt.Errorf("could not find wallet: %s", err)
} else {
id := wallet.GetWalletInfo().Id
cfg.walletId = &id
Expand Down
66 changes: 33 additions & 33 deletions autoswap/lightning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

func getLnSwapper(t *testing.T, cfg *SerializedLnConfig) (*LightningSwapper, *MockRpcProvider) {
swapper, mockProvider := getSwapper(t)
if cfg.MaxBalancePercent == 0 && cfg.MinBalancePercent == 0 {
cfg.MinBalancePercent = 25
cfg.MaxBalancePercent = 75
if cfg.RemoteBalancePercent == 0 && cfg.LocalBalancePercent == 0 {
cfg.LocalBalancePercent = 25
cfg.RemoteBalancePercent = 25
}
require.NoError(t, swapper.UpdateLightningConfig(&autoswaprpc.UpdateLightningConfigRequest{Config: cfg}))
return swapper.lnSwapper, mockProvider
Expand All @@ -46,14 +46,14 @@ func TestSetLnConfig(t *testing.T) {

err = swapper.UpdateLightningConfig(&autoswaprpc.UpdateLightningConfigRequest{
Config: &SerializedLnConfig{
MaxFeePercent: 10,
MaxBalancePercent: defaults.MaxBalancePercent - 10,
MaxFeePercent: 10,
RemoteBalancePercent: defaults.RemoteBalancePercent - 10,
},
FieldMask: &fieldmaskpb.FieldMask{Paths: []string{"max_balance_percent"}},
FieldMask: &fieldmaskpb.FieldMask{Paths: []string{"remote_balance_percent"}},
})
require.NoError(t, err)
require.Equal(t, defaults.MaxFeePercent, lnSwapper.cfg.MaxFeePercent)
require.NotEqual(t, defaults.MaxBalancePercent, lnSwapper.cfg.MaxBalancePercent)
require.NotEqual(t, defaults.RemoteBalancePercent, lnSwapper.cfg.RemoteBalancePercent)
require.False(t, lnSwapper.Running())
require.Empty(t, lnSwapper.Error())
}
Expand Down Expand Up @@ -426,10 +426,10 @@ func TestStrategies(t *testing.T) {
{
name: "PerChannel/Low",
config: &SerializedLnConfig{
PerChannel: true,
MaxBalancePercent: 60,
MinBalancePercent: 40,
SwapType: "reverse",
PerChannel: true,
RemoteBalancePercent: 40,
LocalBalancePercent: 40,
SwapType: "reverse",
},
outcome: []*lightningRecommendation{
recommendation(boltz.ReverseSwap, 200, channels[2]),
Expand All @@ -438,17 +438,17 @@ func TestStrategies(t *testing.T) {
{
name: "PerChannel/High",
config: &SerializedLnConfig{
PerChannel: true,
MaxBalancePercent: 90,
SwapType: "reverse",
PerChannel: true,
RemoteBalancePercent: 10,
SwapType: "reverse",
},
outcome: nil,
},
{
name: "TotalBalance/Reverse",
config: &SerializedLnConfig{
MaxBalancePercent: 60,
MinBalancePercent: 40,
RemoteBalancePercent: 40,
LocalBalancePercent: 40,
},
outcome: []*lightningRecommendation{
recommendation(boltz.ReverseSwap, 150, nil),
Expand All @@ -457,8 +457,8 @@ func TestStrategies(t *testing.T) {
{
name: "TotalBalance/Reverse",
config: &SerializedLnConfig{
MaxBalancePercent: 60,
MinBalancePercent: 40,
RemoteBalancePercent: 40,
LocalBalancePercent: 40,
},
outcome: []*lightningRecommendation{
recommendation(boltz.ReverseSwap, 150, nil),
Expand All @@ -467,8 +467,8 @@ func TestStrategies(t *testing.T) {
{
name: "TotalBalance/Normal",
config: &SerializedLnConfig{
MaxBalancePercent: 60,
MinBalancePercent: 40,
RemoteBalancePercent: 40,
LocalBalancePercent: 40,
},
channels: []*lightning.LightningChannel{
{
Expand All @@ -495,20 +495,20 @@ func TestStrategies(t *testing.T) {
},
},
{
name: "TotalBalance/Max",
name: "TotalBalance/Remote",
config: &SerializedLnConfig{
SwapType: "reverse",
MaxBalance: 600,
SwapType: "reverse",
RemoteBalance: 400,
},
outcome: []*lightningRecommendation{
recommendation(boltz.ReverseSwap, 350, nil),
},
},
{
name: "TotalBalance/Min",
name: "TotalBalance/Local",
config: &SerializedLnConfig{
SwapType: "normal",
MinBalance: 400,
SwapType: "normal",
LocalBalance: 400,
},
outcome: []*lightningRecommendation{
recommendation(boltz.NormalSwap, 400, nil),
Expand All @@ -524,8 +524,8 @@ func TestStrategies(t *testing.T) {
{
name: "TotalBalance/Both/Above",
config: &SerializedLnConfig{
MinBalance: 400,
MaxBalance: 600,
LocalBalance: 400,
RemoteBalance: 400,
},
outcome: []*lightningRecommendation{
recommendation(boltz.NormalSwap, 200, nil),
Expand All @@ -541,25 +541,25 @@ func TestStrategies(t *testing.T) {
{
name: "TotalBalance/Both/Below",
config: &SerializedLnConfig{
MinBalance: 400,
MaxBalance: 600,
LocalBalance: 400,
RemoteBalance: 400,
},
outcome: []*lightningRecommendation{
recommendation(boltz.ReverseSwap, 200, nil),
},
channels: []*lightning.LightningChannel{
{
LocalSat: 700,
RemoteSat: 500,
RemoteSat: 300,
Capacity: 1000,
},
},
},
{
name: "TotalBalance/None",
config: &SerializedLnConfig{
MinBalance: 400,
MaxBalance: 700,
LocalBalance: 400,
RemoteBalance: 300,
},
outcome: nil,
},
Expand Down
Loading

0 comments on commit 3ea3f98

Please sign in to comment.