Skip to content

Commit

Permalink
Refactor raiju interface for importing
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Apr 11, 2023
1 parent df11db4 commit 0300f4c
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 301 deletions.
64 changes: 53 additions & 11 deletions cmd/raiju/raiju.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/nyonson/raiju"
"github.com/nyonson/raiju/lightning"
"github.com/nyonson/raiju/lnd"
"github.com/nyonson/raiju/view"
)

const (
Expand Down Expand Up @@ -143,9 +144,16 @@ func main() {
Clearnet: *clearnet,
}

_, err = r.Candidates(ctx, request)
cmdLog.Printf("filtering candidates by capacity: %d, channels: %d, distance: %d, distant neighbors: %d\n", request.MinCapacity, request.MinChannels, request.MinDistance, request.MinDistantNeighbors)

return err
candidates, err := r.Candidates(ctx, request)
if err != nil {
return err
}

view.PrintNodes(candidates)

return nil
},
}

Expand Down Expand Up @@ -182,13 +190,30 @@ func main() {
return err
}

f.PrintSettings()
view.PrintFees(f)

r := raiju.New(c, f)

_, err = r.Fees(ctx, *daemon)
uc, ec, err := r.Fees(ctx)
if err != nil {
return err
}

// listen for updates
if *daemon {
for {
select {
case u := <-uc:
for id, fee := range u {
cmdLog.Printf("channel %d updated to %f fee PPM", id, fee)
}
case err := <-ec:
return err
}
}
}

return err
return nil
},
}

Expand Down Expand Up @@ -241,7 +266,7 @@ func main() {
return err
}

f.PrintSettings()
view.PrintFees(f)

r := raiju.New(c, f)

Expand All @@ -252,12 +277,24 @@ func main() {
}

if *lastHopPubkey != "" {
_, _, err = r.Rebalance(ctx, lightning.ChannelID(*outChannelID), lightning.PubKey(*lastHopPubkey), stepPercent, maxPercent, maxFee)
cmdLog.Println("Rebalancing channel...")
percent, fee, err := r.Rebalance(ctx, lightning.ChannelID(*outChannelID), lightning.PubKey(*lastHopPubkey), stepPercent, maxPercent, maxFee)
if err != nil {
return err
}
cmdLog.Printf("rebalanced %f percent with a %d sat fee\n", percent, fee)
} else {
err = r.RebalanceAll(ctx, stepPercent, maxPercent, maxFee)
cmdLog.Println("Rebalancing all channels...")
rebalanced, err := r.RebalanceAll(ctx, stepPercent, maxPercent, maxFee)
if err != nil {
return err
}
for id, percent := range rebalanced {
cmdLog.Printf("rebalanced %f percent of channel %d\n", percent, id)
}
}

return err
return nil
},
}

Expand Down Expand Up @@ -295,9 +332,14 @@ func main() {

r := raiju.New(c, f)

_, err = r.Reaper(ctx)
channels, err := r.Reaper(ctx)
if err != nil {
return err
}

view.PrintChannels(channels)

return err
return nil
},
}

Expand Down
57 changes: 18 additions & 39 deletions fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package raiju

import (
"errors"
"fmt"
"os"

"github.com/nyonson/raiju/lightning"
)
Expand All @@ -14,9 +12,9 @@ import (
// When liquidity is low, there is too much inbound.
// When liquidity is high, there is too much outbound.
type LiquidityFees struct {
thresholds []float64
fees []lightning.FeePPM
stickiness float64
Thresholds []float64
Fees []lightning.FeePPM
Stickiness float64
}

// Fee for channel based on its current liquidity.
Expand All @@ -35,48 +33,42 @@ func (lf LiquidityFees) PotentialFee(channel lightning.Channel, additionalLocal

func (lf LiquidityFees) findFee(liquidity float64, currentFee lightning.FeePPM) lightning.FeePPM {
bucket := 0
for bucket < len(lf.thresholds) {
if liquidity > lf.thresholds[bucket] {
for bucket < len(lf.Thresholds) {
if liquidity > lf.Thresholds[bucket] {
break
} else {
bucket += 1
}

}

newFee := lf.fees[bucket]
newFee := lf.Fees[bucket]

// apply stickiness if fee is heading in the right direction, but wanna hold on for a bit to limit gossip
if liquidity < 50 && newFee < currentFee {
lowBucket := 0
for lowBucket < len(lf.thresholds) {
if liquidity > lf.thresholds[lowBucket]+lf.stickiness {
for lowBucket < len(lf.Thresholds) {
if liquidity > lf.Thresholds[lowBucket]+lf.Stickiness {
break
} else {
lowBucket += 1
}

}

if lowBucket != bucket {
fmt.Fprintf(os.Stderr, "keeping fee due to stickiness\n")
}
newFee = lf.fees[lowBucket]
newFee = lf.Fees[lowBucket]
} else if liquidity >= 50 && newFee > currentFee {
highBucket := 0
for highBucket < len(lf.thresholds) {
if liquidity > lf.thresholds[highBucket]-lf.stickiness {
for highBucket < len(lf.Thresholds) {
if liquidity > lf.Thresholds[highBucket]-lf.Stickiness {
break
} else {
highBucket += 1
}

}

if highBucket != bucket {
fmt.Fprintf(os.Stderr, "keeping fee due to stickiness\n")
}
newFee = lf.fees[highBucket]
newFee = lf.Fees[highBucket]
}

return newFee
Expand All @@ -86,11 +78,11 @@ func (lf LiquidityFees) findFee(liquidity float64, currentFee lightning.FeePPM)
func (lf LiquidityFees) RebalanceChannels(channels lightning.Channels) (high lightning.Channels, low lightning.Channels) {
for _, c := range channels {
l := c.Liquidity()
if l > lf.thresholds[0] {
if l > lf.Thresholds[0] {
high = append(high, c)
}

if l <= lf.thresholds[len(lf.thresholds)-1] {
if l <= lf.Thresholds[len(lf.Thresholds)-1] {
low = append(low, c)
}
}
Expand All @@ -100,20 +92,7 @@ func (lf LiquidityFees) RebalanceChannels(channels lightning.Channels) (high lig

// RebalanceFee is the max fee to use in a circular rebalance to ensure its not wasted.
func (lf LiquidityFees) RebalanceFee() lightning.FeePPM {
return lf.fees[len(lf.fees)-1]
}

// PrintSettings to output.
func (lf LiquidityFees) PrintSettings() {
for i := 0; i < len(lf.fees); i++ {
if i == len(lf.fees)-1 {
fmt.Fprintf(os.Stderr, "channels under %g%% local liquidity to %g ppm\n", lf.thresholds[i-1], lf.fees[i])
} else if i == 0 {
fmt.Fprintf(os.Stderr, "channels over %g%% local liquidity to %g ppm, ", lf.thresholds[i], lf.fees[i])
} else {
fmt.Fprintf(os.Stderr, "channels between %g%% and %g%% local liquidity to %g ppm, ", lf.thresholds[i-1], lf.thresholds[i], lf.fees[i])
}
}
return lf.Fees[len(lf.Fees)-1]
}

// NewLiquidityFees with threshold and fee validation.
Expand Down Expand Up @@ -144,8 +123,8 @@ func NewLiquidityFees(thresholds []float64, fees []lightning.FeePPM, stickiness
}

return LiquidityFees{
thresholds: thresholds,
fees: fees,
stickiness: stickiness,
Thresholds: thresholds,
Fees: fees,
Stickiness: stickiness,
}, nil
}
54 changes: 15 additions & 39 deletions fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func TestLiquidityFees_Fee(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lf := LiquidityFees{
thresholds: tt.fields.thresholds,
fees: tt.fields.fees,
stickiness: tt.fields.stickiness,
Thresholds: tt.fields.thresholds,
Fees: tt.fields.fees,
Stickiness: tt.fields.stickiness,
}
if got := lf.Fee(tt.args.channel); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LiquidityFees.Fee() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -198,9 +198,9 @@ func TestLiquidityFees_PotentialFee(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lf := LiquidityFees{
thresholds: tt.fields.thresholds,
fees: tt.fields.fees,
stickiness: tt.fields.stickiness,
Thresholds: tt.fields.thresholds,
Fees: tt.fields.fees,
Stickiness: tt.fields.stickiness,
}
if got := lf.PotentialFee(tt.args.channel, tt.args.additionalLocal); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LiquidityFees.PotentialFee() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -330,9 +330,9 @@ func TestLiquidityFees_RebalanceChannels(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lf := LiquidityFees{
thresholds: tt.fields.thresholds,
fees: tt.fields.fees,
stickiness: tt.fields.stickiness,
Thresholds: tt.fields.thresholds,
Fees: tt.fields.fees,
Stickiness: tt.fields.stickiness,
}
gotHigh, gotLow := lf.RebalanceChannels(tt.args.channels)
if !reflect.DeepEqual(gotHigh, tt.wantHigh) {
Expand Down Expand Up @@ -369,9 +369,9 @@ func TestLiquidityFees_RebalanceFee(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lf := LiquidityFees{
thresholds: tt.fields.thresholds,
fees: tt.fields.fees,
stickiness: tt.fields.stickiness,
Thresholds: tt.fields.thresholds,
Fees: tt.fields.fees,
Stickiness: tt.fields.stickiness,
}
if got := lf.RebalanceFee(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LiquidityFees.RebalanceFee() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -400,9 +400,9 @@ func TestNewLiquidityFees(t *testing.T) {
stickiness: 0,
},
want: LiquidityFees{
thresholds: []float64{80, 20},
fees: []lightning.FeePPM{5, 50, 500},
stickiness: 0,
Thresholds: []float64{80, 20},
Fees: []lightning.FeePPM{5, 50, 500},
Stickiness: 0,
},
wantErr: false,
},
Expand Down Expand Up @@ -447,27 +447,3 @@ func TestNewLiquidityFees(t *testing.T) {
})
}
}

func TestLiquidityFees_PrintSettings(t *testing.T) {
type fields struct {
thresholds []float64
fees []lightning.FeePPM
stickiness float64
}
tests := []struct {
name string
fields fields
}{
// No tests
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
lf := LiquidityFees{
thresholds: tt.fields.thresholds,
fees: tt.fields.fees,
stickiness: tt.fields.stickiness,
}
lf.PrintSettings()
})
}
}
31 changes: 0 additions & 31 deletions print.go

This file was deleted.

Loading

0 comments on commit 0300f4c

Please sign in to comment.