Skip to content

Commit

Permalink
Merge pull request #73 from Impa10r/v1.6.1
Browse files Browse the repository at this point in the history
v1.6.1
  • Loading branch information
Impa10r authored Jul 5, 2024
2 parents 0014a4c + 6e5bfa0 commit cb289fa
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 99 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Versions

## 1.6.1

- AutoFee: add Update All to set paramereter(s) to all custom rules
- Fix forwards subscription to add channel IDs

## 1.6.0

- Make New Swap form inputs more intuitive
Expand Down
197 changes: 123 additions & 74 deletions cmd/psweb/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -810,8 +811,8 @@ type FeeLog struct {
TimeStamp int64
TimeUTC string
TimeAgo string
OldRate int
NewRate int
OldRate int64
NewRate int64
IsInbound bool
IsManual bool
}
Expand Down Expand Up @@ -901,6 +902,11 @@ func afHandler(w http.ResponseWriter, r *http.Request) {
}
}

if peerId == "" {
// non-existing channel
channelId = 0
}

// sort by LocalPct ascending
sort.Slice(channelList, func(i, j int) bool {
return channelList[i].LocalPct < channelList[j].LocalPct
Expand All @@ -923,7 +929,7 @@ func afHandler(w http.ResponseWriter, r *http.Request) {
// bubble square area reflects amount
for i, p := range *chart {
(*chart)[i].R = uint64(math.Sqrt(float64(p.Amount) / 10_000))
(*chart)[i].Label = "Routed: " + formatWithThousandSeparators(p.Amount) + ", Fee: " + formatWithThousandSeparators(p.Fee) + ", PPM: " + formatWithThousandSeparators(p.PPM)
(*chart)[i].Label = "Routed: " + formatWithThousandSeparators(p.Amount) + ", Fee: " + formatFloat(p.Fee) + ", PPM: " + formatWithThousandSeparators(p.PPM)
}

var feeLog []FeeLog
Expand All @@ -948,8 +954,8 @@ func afHandler(w http.ResponseWriter, r *http.Request) {
TimeAgo: timeAgo,
Alias: getNodeAlias(peerNodeId[id]),
ChannelId: id,
OldRate: event.OldRate,
NewRate: event.NewRate,
OldRate: int64(event.OldRate),
NewRate: int64(event.NewRate),
IsInbound: event.IsInbound,
IsManual: event.IsManual,
})
Expand Down Expand Up @@ -1508,98 +1514,141 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
return
}

rule := &ln.AutoFeeDefaults
msg := ""
var newRule ln.AutoFeeParams

if r.FormValue("update_button") != "" {
// channelId == 0 means default rule
msg = "Default rule updated"
newRule.FailedBumpPPM, err = strconv.Atoi(r.FormValue("failBump"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

if channelId > 0 {
// custom rule
msg = "Custom rule updated"
if ln.AutoFee[channelId] == nil {
// add new
ln.AutoFee[channelId] = new(ln.AutoFeeParams)
msg = "Custom rule added"
}
rule = ln.AutoFee[channelId]
}
newRule.FailedMoveThreshold, err = strconv.Atoi(r.FormValue("failedMoveThreshold"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.FailedBumpPPM, err = strconv.Atoi(r.FormValue("failBump"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.LowLiqPct, err = strconv.Atoi(r.FormValue("lowLiqPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.FailedMoveThreshold, err = strconv.Atoi(r.FormValue("failedMoveThreshold"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.LowLiqRate, err = strconv.Atoi(r.FormValue("lowLiqRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.LowLiqPct, err = strconv.Atoi(r.FormValue("lowLiqPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.NormalRate, err = strconv.Atoi(r.FormValue("normalRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.LowLiqRate, err = strconv.Atoi(r.FormValue("lowLiqRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.ExcessPct, err = strconv.Atoi(r.FormValue("excessPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.NormalRate, err = strconv.Atoi(r.FormValue("normalRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.ExcessRate, err = strconv.Atoi(r.FormValue("excessRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.ExcessPct, err = strconv.Atoi(r.FormValue("excessPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.InactivityDays, err = strconv.Atoi(r.FormValue("inactivityDays"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.ExcessRate, err = strconv.Atoi(r.FormValue("excessRate"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.InactivityDropPPM, err = strconv.Atoi(r.FormValue("inactivityDropPPM"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.InactivityDays, err = strconv.Atoi(r.FormValue("inactivityDays"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
newRule.InactivityDropPct, err = strconv.Atoi(r.FormValue("inactivityDropPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

newRule.CoolOffHours, err = strconv.Atoi(r.FormValue("coolOffHours"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}

rule.InactivityDropPPM, err = strconv.Atoi(r.FormValue("inactivityDropPPM"))
if ln.HasInboundFees() {
newRule.LowLiqDiscount, err = strconv.Atoi(r.FormValue("lowLiqDiscount"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
}
}
rule := &ln.AutoFeeDefaults
msg := ""
updateAll := false

rule.InactivityDropPct, err = strconv.Atoi(r.FormValue("inactivityDropPct"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
if _, isCustom := ln.AutoFeeRule(channelId); isCustom {
updateAll = r.FormValue("update_all") != ""
}

if updateAll {
msg = "All custom rules updated:"
old := reflect.ValueOf(*ln.AutoFee[channelId])
new := reflect.ValueOf(newRule)

// find what will be updated
for i := 0; i < old.NumField(); i++ {
if old.Field(i).Int() != new.Field(i).Int() {
msg += fmt.Sprintf(" %s=%v", new.Type().Field(i).Name, new.Field(i).Interface())
}
}

rule.CoolOffHours, err = strconv.Atoi(r.FormValue("coolOffHours"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
for _, rulePtr := range ln.AutoFee {
if rulePtr == nil {
continue
}
// Use Elem() to get the underlying struct from the pointer
current := reflect.ValueOf(rulePtr).Elem()

for i := 0; i < old.NumField(); i++ {
if old.Field(i).Int() != new.Field(i).Int() {
if current.Field(i).CanSet() {
current.Field(i).SetInt(new.Field(i).Int())
} else {
redirectWithError(w, r, "/af?", errors.New("unable to set the value of "+current.Type().Field(i).Name))
return
}
}
}
}

if ln.HasInboundFees() {
rule.LowLiqDiscount, err = strconv.Atoi(r.FormValue("lowLiqDiscount"))
if err != nil {
redirectWithError(w, r, "/af?", err)
return
// persist to db
db.Save("AutoFees", "AutoFee", ln.AutoFee)

} else if r.FormValue("update_button") != "" {
// channelId == 0 means default rule
msg = "Default rule updated"

if channelId > 0 {
// custom rule
msg = "Custom rule updated"
if ln.AutoFee[channelId] == nil {
// add new
ln.AutoFee[channelId] = new(ln.AutoFeeParams)
msg = "Custom rule added"
}
rule = ln.AutoFee[channelId]
}

// clone the new data
*rule = newRule

// persist to db
if channelId > 0 {
db.Save("AutoFees", "AutoFee", ln.AutoFee)
Expand Down
6 changes: 3 additions & 3 deletions cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ func PlotPPM(channelId uint64) *[]DataPoint {
plot = append(plot, DataPoint{
TS: uint64(e.ResolvedTime),
Amount: e.OutMsat / 1000,
Fee: e.FeeMsat / 1000,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.OutMsat,
})
}
Expand All @@ -1246,7 +1246,7 @@ func ForwardsLog(channelId uint64, fromTS int64) *[]DataPoint {
log = append(log, DataPoint{
TS: uint64(e.ResolvedTime),
Amount: e.OutMsat / 1000,
Fee: e.FeeMsat / 1000,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.OutMsat,
ChanIdIn: ConvertClnToLndChannelId(e.InChannel),
ChanIdOut: chId,
Expand All @@ -1262,7 +1262,7 @@ func ForwardsLog(channelId uint64, fromTS int64) *[]DataPoint {
log = append(log, DataPoint{
TS: uint64(e.ResolvedTime),
Amount: e.OutMsat / 1000,
Fee: e.FeeMsat / 1000,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.OutMsat,
ChanIdIn: channelId,
ChanIdOut: ConvertClnToLndChannelId(e.OutChannel),
Expand Down
4 changes: 2 additions & 2 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type AutoFeeEvent struct {
type DataPoint struct {
TS uint64
Amount uint64
Fee uint64
Fee float64
PPM uint64
R uint64
Label string
Expand Down Expand Up @@ -230,7 +230,7 @@ func stringIsInSlice(whatToFind string, whereToSearch []string) bool {
func AutoFeeRule(channelId uint64) (*AutoFeeParams, bool) {
params := &AutoFeeDefaults
isCustom := false
if AutoFee[channelId] != nil {
if channelId > 0 && AutoFee[channelId] != nil {
// channel has custom parameters
params = AutoFee[channelId]
isCustom = true
Expand Down
17 changes: 9 additions & 8 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,11 +939,14 @@ func subscribeForwards(ctx context.Context, client routerrpc.RouterClient) error
forwardingEvent := new(lnrpc.ForwardingEvent)

forwardingEvent.FeeMsat = info.IncomingAmtMsat - info.OutgoingAmtMsat
forwardingEvent.Fee = forwardingEvent.FeeMsat / 1000
forwardingEvent.AmtInMsat = info.IncomingAmtMsat
forwardingEvent.AmtOutMsat = info.OutgoingAmtMsat
forwardingEvent.AmtIn = info.IncomingAmtMsat / 1000
forwardingEvent.AmtOut = info.OutgoingAmtMsat / 1000
forwardingEvent.TimestampNs = htlcEvent.TimestampNs
forwardingEvent.ChanIdIn = htlcEvent.IncomingChannelId
forwardingEvent.ChanIdOut = htlcEvent.OutgoingChannelId

htlc.forwardingEvent = forwardingEvent

Expand Down Expand Up @@ -1892,12 +1895,10 @@ func PlotPPM(channelId uint64) *[]DataPoint {
// ignore small forwards
if e.AmtOutMsat > ignoreForwardsMsat {
plot = append(plot, DataPoint{
TS: e.TimestampNs / 1_000_000_000,
Amount: e.AmtOut,
Fee: e.Fee,
PPM: e.FeeMsat * 1_000_000 / e.AmtOutMsat,
ChanIdIn: e.ChanIdIn,
ChanIdOut: e.ChanIdOut,
TS: e.TimestampNs / 1_000_000_000,
Amount: e.AmtOut,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.AmtOutMsat,
})
}
}
Expand All @@ -1920,7 +1921,7 @@ func ForwardsLog(channelId uint64, fromTS int64) *[]DataPoint {
log = append(log, DataPoint{
TS: e.TimestampNs / 1_000_000_000,
Amount: e.AmtOut,
Fee: e.Fee,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.AmtOutMsat,
ChanIdIn: e.ChanIdIn,
ChanIdOut: e.ChanIdOut,
Expand All @@ -1936,7 +1937,7 @@ func ForwardsLog(channelId uint64, fromTS int64) *[]DataPoint {
log = append(log, DataPoint{
TS: e.TimestampNs / 1_000_000_000,
Amount: e.AmtOut,
Fee: e.Fee,
Fee: float64(e.FeeMsat) / 1000,
PPM: e.FeeMsat * 1_000_000 / e.AmtOutMsat,
ChanIdIn: e.ChanIdIn,
ChanIdOut: e.ChanIdOut,
Expand Down
7 changes: 6 additions & 1 deletion cmd/psweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

const (
// App version tag
version = "v1.6.0"
version = "v1.6.1"
)

type SwapParams struct {
Expand Down Expand Up @@ -142,6 +142,7 @@ func main() {
"u": toUint,
"fmt": formatWithThousandSeparators,
"fs": formatSigned,
"ff": formatFloat,
"m": toMil,
"last": last,
}).
Expand Down Expand Up @@ -1107,6 +1108,10 @@ func getNodeAlias(key string) string {
return alias
}

if key == "" {
return "* closed channel *"
}

// try lightning
alias = ln.GetAlias(key)

Expand Down
Loading

0 comments on commit cb289fa

Please sign in to comment.