Skip to content

Commit

Permalink
Merge pull request #86 from Impa10r/v1.6.8
Browse files Browse the repository at this point in the history
v1.6.8
  • Loading branch information
Impa10r authored Jul 31, 2024
2 parents ea8532d + fe42d61 commit f36eb8b
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"program": "${workspaceFolder}/cmd/psweb/",
"showLog": false,
"envFile": "${workspaceFolder}/.env",
"args": ["-datadir", "/home/vlad/.peerswap2"]
//"args": ["-datadir", "/home/vlad/.peerswap2"]
}
]
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Versions

## 1.6.8

- AutoFees: do not change inbound fee during pending HTLCs
- AutoFees: bump Low Liq Rate of the channel's rule on failed HTLCs
- AutoFees: avoid redundant fee rate updates
- Fix telegram bot errors by escaping message text as MarkdownV2

## 1.6.7

- Fix AutoFees stop working bug
Expand Down
12 changes: 11 additions & 1 deletion cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@ func SetFeeRate(peerNodeId string,
req.FeePPM = feeRate
}

if oldRate == int(feeRate) {
return oldRate, errors.New("rate was already set")
}

err = client.Request(&req, &res)
if err != nil {
log.Println("SetFeeRate:", err)
Expand Down Expand Up @@ -1201,8 +1205,14 @@ func ApplyAutoFees() {

// set the new rate
if newFee != oldFee {
// check if the fee was already set
if lastFeeIsTheSame(channelId, newFee, false) {
continue
}

peerId := channelMap["peer_id"].(string)
if _, err = SetFeeRate(peerId, channelId, int64(newFee), false, false); err == nil {
_, err = SetFeeRate(peerId, channelId, int64(newFee), false, false)
if err == nil && !lastFeeIsTheSame(channelId, newFee, false) {
// log the last change
LogFee(channelId, oldFee, newFee, false, false)
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,14 @@ func saveSwapRabate(swapId string, rebate int64) {
// save rebate payment
SwapRebates[swapId] = rebate
}

// check if the last logged fee rate is the same as newFee
func lastFeeIsTheSame(channelId uint64, newFee int, isInbound bool) bool {
lastFee := LastAutoFeeLog(channelId, isInbound)
if lastFee != nil {
if newFee == lastFee.NewRate {
return true
}
}
return false
}
49 changes: 40 additions & 9 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"peerswap-web/cmd/psweb/bitcoin"
"peerswap-web/cmd/psweb/config"
"peerswap-web/cmd/psweb/db"

"github.com/elementsproject/peerswap/peerswaprpc"

Expand Down Expand Up @@ -1709,6 +1710,10 @@ func SetFeeRate(peerNodeId string,
}
}

if oldRate == int(feeRate) {
return oldRate, errors.New("rate was already set")
}

_, err = client.UpdateChannelPolicy(context.Background(), &req)
if err != nil {
log.Println("SetFeeRate:", err)
Expand Down Expand Up @@ -1868,6 +1873,18 @@ func applyAutoFee(client lnrpc.LightningClient, channelId uint64, htlcFail bool)
if liqPct < params.LowLiqPct {
// increase fee to help prevent further failed HTLCs
newFee += params.FailedBumpPPM

// bump LowLiqRate
if AutoFee[channelId] == nil {
// add custom parameters
AutoFee[channelId] = new(AutoFeeParams)
// clone default values
*AutoFee[channelId] = AutoFeeDefaults
}

AutoFee[channelId].LowLiqRate = newFee
// persist to db
db.Save("AutoFees", "AutoFee", AutoFee)
} else if liqPct > params.LowLiqPct {
// move threshold
moveLowLiqThreshold(channelId, params.FailedMoveThreshold)
Expand All @@ -1883,9 +1900,17 @@ func applyAutoFee(client lnrpc.LightningClient, channelId uint64, htlcFail bool)
// do not lower fees for temporary balance spikes due to pending HTLCs
return
}

// check if the fee was already set
if lastFeeIsTheSame(channelId, newFee, false) {
return
}

if old, err := SetFeeRate(peerId, channelId, int64(newFee), false, false); err == nil {
// log the last change
LogFee(channelId, old, newFee, false, false)
if !lastFeeIsTheSame(channelId, newFee, false) {
// log the last change
LogFee(channelId, old, newFee, false, false)
}
}
}
}
Expand Down Expand Up @@ -1947,25 +1972,31 @@ func ApplyAutoFees() {
}

oldFee := int(policy.FeeRateMilliMsat)
newFee := oldFee
liqPct := int((ch.LocalBalance + ch.UnsettledBalance) * 100 / r.Capacity)

newFee = calculateAutoFee(ch.ChanId, params, liqPct, oldFee)
newFee := calculateAutoFee(ch.ChanId, params, liqPct, oldFee)

// set the new rate
if newFee != oldFee {
if ch.UnsettledBalance > 0 && newFee < oldFee {
// do not lower fees for temporary balance spikes due to pending HTLCs
// do not lower fees during pending HTLCs
continue
}

// check if the fee was already set
if lastFeeIsTheSame(ch.ChanId, newFee, false) {
continue
}

_, err := SetFeeRate(peerId, ch.ChanId, int64(newFee), false, false)
if err == nil {
if err == nil && !lastFeeIsTheSame(ch.ChanId, newFee, false) {
// log the last change
LogFee(ch.ChanId, oldFee, newFee, false, false)
}
}

if HasInboundFees() {
// do not change inbound fee during pending HTLCs
if HasInboundFees() && ch.UnsettledBalance == 0 {
toSet := false
discountRate := int64(0)

Expand All @@ -1983,9 +2014,9 @@ func ApplyAutoFees() {
}
}

if toSet {
if toSet && !lastFeeIsTheSame(ch.ChanId, int(discountRate), true) {
oldRate, err := SetFeeRate(peerId, ch.ChanId, discountRate, true, false)
if err == nil {
if err == nil && !lastFeeIsTheSame(ch.ChanId, int(discountRate), true) {
// log the last change
LogFee(ch.ChanId, oldRate, int(discountRate), true, false)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/psweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ import (

const (
// App version tag
version = "v1.6.7"
version = "v1.6.8"

// Swap Out reserves are hardcoded here:
// https://github.com/ElementsProject/peerswap/blob/c77a82913d7898d0d3b7c83e4a990abf54bd97e5/peerswaprpc/server.go#L105
swapOutChannelReserve = 5000
// https://github.com/ElementsProject/peerswap/blob/c77a82913d7898d0d3b7c83e4a990abf54bd97e5/swap/actions.go#L388
// increased by extra 1000 sats to avoid huge fee rate
swapOutChainReserve = 20300
// for Swap In reserves see /ln
)
Expand Down
32 changes: 29 additions & 3 deletions cmd/psweb/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"

"peerswap-web/cmd/psweb/config"
Expand Down Expand Up @@ -105,7 +106,7 @@ func telegramStart() {
}
}
telegramSendMessage(t)
case "/auto":
case "/autoswaps":
t := "🤖 Auto swap-ins are "
if config.Config.AutoSwapEnabled {
t += "Enabled"
Expand Down Expand Up @@ -156,7 +157,7 @@ func telegramConnect() {
Description: "Status of peg-in or BTC withdrawal",
},
tgbotapi.BotCommand{
Command: "auto",
Command: "autoswaps",
Description: "Status of auto swap-ins",
},
tgbotapi.BotCommand{
Expand All @@ -177,7 +178,7 @@ func telegramSendMessage(msgText string) bool {
if chatId == 0 {
return false
}
msg := tgbotapi.NewMessage(chatId, msgText)
msg := tgbotapi.NewMessage(chatId, EscapeMarkdownV2(msgText))
msg.ParseMode = "MarkdownV2"

_, err := bot.Send(msg)
Expand Down Expand Up @@ -212,3 +213,28 @@ func telegramSendFile(folder, fileName, satAmount string) error {

return nil
}

// EscapeMarkdownV2 escapes special characters for MarkdownV2
func EscapeMarkdownV2(text string) string {
replacer := strings.NewReplacer(
"_", "\\_",
"*", "\\*",
"[", "\\[",
"]", "\\]",
"(", "\\(",
")", "\\)",
"~", "\\~",
"`", "\\`",
">", "\\>",
"#", "\\#",
"+", "\\+",
"-", "\\-",
"=", "\\=",
"|", "\\|",
"{", "\\{",
"}", "\\}",
".", "\\.",
"!", "\\!",
)
return replacer.Replace(text)
}

0 comments on commit f36eb8b

Please sign in to comment.