From 139eb374369b4bc7919badec5433a075cdb5abbd Mon Sep 17 00:00:00 2001 From: Impa10r Date: Tue, 28 Jan 2025 17:41:49 +0100 Subject: [PATCH 01/16] Fix AF decrease --- CHANGELOG.md | 4 ++++ cmd/psweb/ln/common.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f779774..89a4ba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 1.7.6 +- Fix AF decrease not working for channels with no outbound forwards + +## 1.7.6 + - Fix BTC to sats rounding bug preventing claim init or join - Fix external funding peg-in num of confirmations not registered - Fix ClaimJoin OP_RETURN string diff --git a/cmd/psweb/ln/common.go b/cmd/psweb/ln/common.go index 897b172..d437d2f 100644 --- a/cmd/psweb/ln/common.go +++ b/cmd/psweb/ln/common.go @@ -439,7 +439,7 @@ func calculateAutoFee(channelId uint64, params *AutoFeeParams, liqPct int, oldFe // must be definitely above threshold and cool-off period passed if liqPct > params.LowLiqPct && lastUpdate < time.Now().Add(-time.Duration(params.CoolOffHours)*time.Hour).Unix() { // check the inactivity period - if ts, ok := LastForwardTS.Read(channelId); ok && ts < time.Now().AddDate(0, 0, -params.InactivityDays).Unix() { + if ts, ok := LastForwardTS.Read(channelId); !ok || ok && ts < time.Now().AddDate(0, 0, -params.InactivityDays).Unix() { // decrease the fee newFee -= params.InactivityDropPPM newFee = newFee * (100 - params.InactivityDropPct) / 100 From 965e0ec69d20974922edaf6976f726a93ec463fe Mon Sep 17 00:00:00 2001 From: Impa10r Date: Tue, 28 Jan 2025 18:21:54 +0100 Subject: [PATCH 02/16] Better indication when all channels with a peer are inactive --- CHANGELOG.md | 1 + cmd/psweb/handlers.go | 5 + cmd/psweb/templates/peer.gohtml | 816 ++++++++++++++++---------------- 3 files changed, 416 insertions(+), 406 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89a4ba3..59d7b81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.7.6 - Fix AF decrease not working for channels with no outbound forwards +- Better indication when all channels with a peer are inactive ## 1.7.6 diff --git a/cmd/psweb/handlers.go b/cmd/psweb/handlers.go index 7a5053e..9d178da 100644 --- a/cmd/psweb/handlers.go +++ b/cmd/psweb/handlers.go @@ -363,6 +363,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { // to find a channel for swap-in maxRemoteBalance := uint64(0) maxRemoteBalanceIndex := 0 + isOnline := false // get routing stats for i, ch := range peer.Channels { @@ -383,6 +384,8 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { } info.Active = ch.GetActive() + isOnline = isOnline || info.Active + info.LocalPct = info.LocalBalance * 100 / info.Capacity channelInfo = append(channelInfo, info) @@ -591,6 +594,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { SelectedChannel uint64 HasDiscountedvSize bool RedColor string + IsOnline bool } redColor := "red" @@ -645,6 +649,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { SelectedChannel: selectedChannel, HasDiscountedvSize: hasDiscountedvSize, RedColor: redColor, + IsOnline: isOnline, } // executing template named "peer" diff --git a/cmd/psweb/templates/peer.gohtml b/cmd/psweb/templates/peer.gohtml index 829b676..fecc388 100644 --- a/cmd/psweb/templates/peer.gohtml +++ b/cmd/psweb/templates/peer.gohtml @@ -18,461 +18,465 @@ {{end}}
- {{if .PeerSwapPeer}} - {{if not .Peer.Channels}} -

🙁 There are no channels with {{.PeerAlias}}.

- {{else}} - {{if not .Peer.SwapsAllowed}} -

⛔ {{.PeerAlias}} did not whitelist your node. Please contact them to do it.

+ {{if .IsOnline}} + {{if .PeerSwapPeer}} + {{if not .Peer.Channels}} +

🙁 There are no channels with {{.PeerAlias}}.

{{else}} - {{if ne .ActiveSwaps ""}} -

Pending Swaps

- {{.ActiveSwaps}} - - {{else }} -

New Swap

-
-
-
- -
-
-
- + {{if not .Peer.SwapsAllowed}} +

⛔ {{.PeerAlias}} did not whitelist your node. Please contact them to do it.

+ {{else}} + {{if ne .ActiveSwaps ""}} +

Pending Swaps

+ {{.ActiveSwaps}} + + {{else }} +

New Swap

+ +
+
+
-
-
-
-
- -
-
-
- +
+
+ +
-
-
-
- -
-
-
- +
+
+ +
+
+
+ +
-
-
-
- -
-
- +
+
+ +
+
+
+ +
+
-
-
-
- - +
+
+ +
+
+ +
-
- +
+
+ + +
+
+ +
-
-
- - {{range .OutputsBTC}} - - - - - {{end}} - - - {{range .OutputsLBTC}} - - - - {{end}} - - - - -
- - + // on load + fromChanged(); + + {{end}} {{end}} {{end}} - {{end}} - {{else}} -

Invitation to PeerSwap

-
-
-
-
- + Sincerely, + {{.MyAlias}} +
-
-
-
- -
-
- -
-
-
-
- - - -
- +
+
+ +
+
+ +
+
+
+
+ + + +
+ + {{end}} + {{else}} +

🚫 No active channels

{{end}}
@@ -571,7 +575,7 @@ Sincerely, {{range .ChannelInfo}} {{m .Capacity}} From 735309b6510a55c53b2b932dc656d531d1a83cf8 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Thu, 30 Jan 2025 22:41:20 +0100 Subject: [PATCH 03/16] Catch Bitcoin Core and Elements Core RPC errors --- .vscode/launch.json | 2 +- CHANGELOG.md | 1 + cmd/psweb/bitcoin/bitcoin.go | 4 ++-- cmd/psweb/liquid/elements.go | 4 ++-- cmd/psweb/main.go | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 11f003d..e9eb78d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ "program": "${workspaceFolder}/cmd/psweb/", "showLog": false, //"envFile": "${workspaceFolder}/.env", - //"args": ["-datadir", "/home/vlad/.peerswap_t4"] + "args": ["-datadir", "/home/vlad/.peerswap_t4"] //"args": ["-datadir", "/home/vlad/.peerswap2"] }, // sudo bash -c 'echo 0 > /proc/sys/kernel/yama/ptrace_scope' diff --git a/CHANGELOG.md b/CHANGELOG.md index 59d7b81..13db192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix AF decrease not working for channels with no outbound forwards - Better indication when all channels with a peer are inactive +- Catch Bitcoin Core and Elements Core RPC errors ## 1.7.6 diff --git a/cmd/psweb/bitcoin/bitcoin.go b/cmd/psweb/bitcoin/bitcoin.go index 27da7ff..f1fad1a 100644 --- a/cmd/psweb/bitcoin/bitcoin.go +++ b/cmd/psweb/bitcoin/bitcoin.go @@ -127,8 +127,8 @@ func handleError(err error, r *rpcResponse) error { if err != nil { return err } - if r.Err != nil { - return r.Err + if r != nil && r.Err != nil { + return fmt.Errorf(r.Err.Message) } return nil diff --git a/cmd/psweb/liquid/elements.go b/cmd/psweb/liquid/elements.go index 8cd39ff..6b35a7f 100644 --- a/cmd/psweb/liquid/elements.go +++ b/cmd/psweb/liquid/elements.go @@ -128,8 +128,8 @@ func handleError(err error, r *rpcResponse) error { if err != nil { return err } - if r.Err != nil { - return r.Err + if r != nil && r.Err != nil { + return fmt.Errorf(r.Err.Message) } return nil diff --git a/cmd/psweb/main.go b/cmd/psweb/main.go index f5e208c..e60cbdc 100644 --- a/cmd/psweb/main.go +++ b/cmd/psweb/main.go @@ -34,10 +34,10 @@ import ( const ( // App VERSION tag - VERSION = "v1.7.6" + VERSION = "v1.7.7" // Swap Out reserve SWAP_OUT_CHANNEL_RESERVE = 10000 - // Elements v23.02.03 introduced vsize discount enabled on testnet as default + // Elements v23.02.03 introduced vsize discount (set creatediscountct=1 for mainnet) ELEMENTS_DISCOUNTED_VSIZE_VERSION = 230203 ) From 304769099847e4b393e57dcb2448baa4d11c7a4c Mon Sep 17 00:00:00 2001 From: Impa10r Date: Thu, 30 Jan 2025 22:57:36 +0100 Subject: [PATCH 04/16] Add debugging decoderawtx --- cmd/psweb/bitcoin/bitcoin.go | 1 + cmd/psweb/ln/lnd.go | 1 + 2 files changed, 2 insertions(+) diff --git a/cmd/psweb/bitcoin/bitcoin.go b/cmd/psweb/bitcoin/bitcoin.go index f1fad1a..5ad93c2 100644 --- a/cmd/psweb/bitcoin/bitcoin.go +++ b/cmd/psweb/bitcoin/bitcoin.go @@ -331,6 +331,7 @@ func DecodeRawTransaction(hexstring string) (*Transaction, error) { r, err := service.client.call("decoderawtransaction", params, "") if err = handleError(err, &r); err != nil { log.Printf("DecodeRawTransaction: %v", err) + log.Printf("Hex: %s", hexstring) return nil, err } diff --git a/cmd/psweb/ln/lnd.go b/cmd/psweb/ln/lnd.go index 00f0e6d..551ef14 100644 --- a/cmd/psweb/ln/lnd.go +++ b/cmd/psweb/ln/lnd.go @@ -334,6 +334,7 @@ finalize: decoded, err := bitcoin.DecodeRawTransaction(hex.EncodeToString(rawTx)) if err != nil { + log.Println("Funded PSBT:", hex.EncodeToString(psbtBytes)) return nil, err } From 1c6812a7e0dbdd1dc47a4d49d651a8d6f0ddce9a Mon Sep 17 00:00:00 2001 From: Impa10r Date: Sat, 1 Feb 2025 22:37:31 +0100 Subject: [PATCH 05/16] Allow fee rate with decimals --- CHANGELOG.md | 2 ++ cmd/psweb/ln/lnd.go | 4 ++-- cmd/psweb/telegram.go | 8 ++++++-- cmd/psweb/templates/bitcoin.gohtml | 22 +++++++++++++++++----- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13db192..4998feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Fix AF decrease not working for channels with no outbound forwards - Better indication when all channels with a peer are inactive - Catch Bitcoin Core and Elements Core RPC errors +- Fix telegram bot panic on wrong token +- Allow fee rate with decimals ## 1.7.6 diff --git a/cmd/psweb/ln/lnd.go b/cmd/psweb/ln/lnd.go index 551ef14..a1e4721 100644 --- a/cmd/psweb/ln/lnd.go +++ b/cmd/psweb/ln/lnd.go @@ -334,7 +334,7 @@ finalize: decoded, err := bitcoin.DecodeRawTransaction(hex.EncodeToString(rawTx)) if err != nil { - log.Println("Funded PSBT:", hex.EncodeToString(psbtBytes)) + //log.Println("Funded PSBT:", hex.EncodeToString(psbtBytes)) return nil, err } @@ -346,7 +346,7 @@ finalize: requiredFee := int64(feeRate * float64(decoded.VSize)) if requiredFee != toSats(feePaid) { - if pass < 5 { + if pass < 3 || requiredFee > toSats(feePaid) { log.Println("Trying to fix fee paid", toSats(feePaid), "vs required", requiredFee) releaseOutputs(cl, utxos, &lockId) diff --git a/cmd/psweb/telegram.go b/cmd/psweb/telegram.go index a814377..732c26c 100644 --- a/cmd/psweb/telegram.go +++ b/cmd/psweb/telegram.go @@ -72,7 +72,9 @@ func telegramStart() { // Try saved chatId chatId = config.Config.TelegramChatId if chatId > 0 { - telegramConnect() + if !telegramConnect() { + return + } } updates := bot.GetUpdatesChan(u) @@ -158,7 +160,7 @@ func telegramStart() { } } -func telegramConnect() { +func telegramConnect() bool { if telegramSendMessage("📟 PeerSwap connected") { // successfully connected cmdCfg := tgbotapi.NewSetMyCommands( @@ -189,7 +191,9 @@ func telegramConnect() { } else { chatId = 0 bot = nil + return false } + return true } func telegramSendMessage(msgText string) bool { diff --git a/cmd/psweb/templates/bitcoin.gohtml b/cmd/psweb/templates/bitcoin.gohtml index 7283a11..05cbbe3 100644 --- a/cmd/psweb/templates/bitcoin.gohtml +++ b/cmd/psweb/templates/bitcoin.gohtml @@ -66,7 +66,13 @@ Amount is capped at remote channel balance to mimic brute force discovery" for="
- +
{{if .CanClaimJoin}} @@ -94,7 +100,7 @@ Amount is capped at remote channel balance to mimic brute force discovery" for="
- +
@@ -250,7 +256,7 @@ Amount is capped at remote channel balance to mimic brute force discovery" for="

- +

@@ -440,6 +446,12 @@ Amount is capped at remote channel balance to mimic brute force discovery" for=" {{end}} } + function formatFeeRate(input) { + if (input.value) { + input.value = parseFloat(input.value).toFixed(2); + } + } + // Function to format number with thousand separators function formatWithThousandSeparators(number) { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); @@ -677,8 +689,8 @@ Amount is capped at remote channel balance to mimic brute force discovery" for=" totalSize += 4; // vbyte and fee calculation - vbyteSize = Math.ceil((3 * baseSize + totalSize) / 4) - let fee = feeRate * vbyteSize + vbyteSize = Math.ceil((3 * baseSize + totalSize) / 4); + let fee = Math.ceil(feeRate * vbyteSize); let text = "Transaction size: " + vbyteSize + " vBytes\n"; From 059f51b9acf4fa396570298f763c830e88fe799a Mon Sep 17 00:00:00 2001 From: Impa10r Date: Sat, 1 Feb 2025 22:40:57 +0100 Subject: [PATCH 06/16] New fee rate to hundredth --- cmd/psweb/templates/bitcoin.gohtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/psweb/templates/bitcoin.gohtml b/cmd/psweb/templates/bitcoin.gohtml index 05cbbe3..4f3faf2 100644 --- a/cmd/psweb/templates/bitcoin.gohtml +++ b/cmd/psweb/templates/bitcoin.gohtml @@ -256,7 +256,7 @@ Amount is capped at remote channel balance to mimic brute force discovery" for="

- +

From cfac7a41870924043cc983df3aaab2a721f6f2c7 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Sat, 1 Feb 2025 22:54:58 +0100 Subject: [PATCH 07/16] Reset chat id --- CHANGELOG.md | 6 +++--- cmd/psweb/telegram.go | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4998feb..bd382a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,12 @@ # Versions -## 1.7.6 +## 1.7.7 - Fix AF decrease not working for channels with no outbound forwards - Better indication when all channels with a peer are inactive - Catch Bitcoin Core and Elements Core RPC errors -- Fix telegram bot panic on wrong token -- Allow fee rate with decimals +- Fix telegram bot panic on wrong token or chat id +- Allow fee rate with decimals precision ## 1.7.6 diff --git a/cmd/psweb/telegram.go b/cmd/psweb/telegram.go index 732c26c..cc911a6 100644 --- a/cmd/psweb/telegram.go +++ b/cmd/psweb/telegram.go @@ -189,7 +189,12 @@ func telegramConnect() bool { config.Config.TelegramChatId = chatId config.Save() } else { - chatId = 0 + if chatId > 0 { + chatId = 0 + config.Config.TelegramChatId = chatId + config.Save() + log.Println("Chat Id was reset. Use /start in Telegram to start the bot.") + } bot = nil return false } From 2b0597477e672cfe744009a023a8a93d1e1f89d5 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Wed, 5 Feb 2025 22:06:01 +0100 Subject: [PATCH 08/16] LBTC balance reserve to 2000 sats --- .vscode/launch.json | 2 +- CHANGELOG.md | 1 + cmd/psweb/handlers.go | 4 ++-- cmd/psweb/main.go | 20 +++++--------------- cmd/psweb/templates/peer.gohtml | 2 +- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e9eb78d..9df7531 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ "program": "${workspaceFolder}/cmd/psweb/", "showLog": false, //"envFile": "${workspaceFolder}/.env", - "args": ["-datadir", "/home/vlad/.peerswap_t4"] + //"args": ["-datadir", "/home/vlad/.peerswap2_t4"] //"args": ["-datadir", "/home/vlad/.peerswap2"] }, // sudo bash -c 'echo 0 > /proc/sys/kernel/yama/ptrace_scope' diff --git a/CHANGELOG.md b/CHANGELOG.md index bd382a4..be8854b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Catch Bitcoin Core and Elements Core RPC errors - Fix telegram bot panic on wrong token or chat id - Allow fee rate with decimals precision +- Set LBTC balance reserve to 2000 sats to save on fees ## 1.7.6 diff --git a/cmd/psweb/handlers.go b/cmd/psweb/handlers.go index 9d178da..b76e78a 100644 --- a/cmd/psweb/handlers.go +++ b/cmd/psweb/handlers.go @@ -438,7 +438,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { swapFeeReserveBTC := uint64(math.Ceil(bitcoinFeeRate * 350)) // arbitrary haircut to avoid 'no matching outgoing channel available' - maxLiquidSwapIn := min(int64(satAmount)-int64(swapFeeReserveLBTC(len(utxosLBTC))), int64(maxRemoteBalance)-10000) + maxLiquidSwapIn := min(int64(satAmount)-SWAP_LBTC_RESERVE, int64(maxRemoteBalance)-10000) if maxLiquidSwapIn < 100_000 { maxLiquidSwapIn = 0 } @@ -454,7 +454,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { } else { peerLiquidBalance = formatWithThousandSeparators(ptr.Amount) } - maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE, int64(ptr.Amount)-int64(swapFeeReserveLBTC(1))))) + maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE, int64(ptr.Amount)-SWAP_LBTC_RESERVE))) } else { maxLiquidSwapOut = uint64(max(0, int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE)) } diff --git a/cmd/psweb/main.go b/cmd/psweb/main.go index e60cbdc..4b79f5b 100644 --- a/cmd/psweb/main.go +++ b/cmd/psweb/main.go @@ -35,8 +35,10 @@ import ( const ( // App VERSION tag VERSION = "v1.7.7" - // Swap Out reserve + // Swap Out reserve to deduct from channel local balance SWAP_OUT_CHANNEL_RESERVE = 10000 + // https://github.com/ElementsProject/peerswap/pull/304#issuecomment-2303931071 + SWAP_LBTC_RESERVE = 2000 // Elements v23.02.03 introduced vsize discount (set creatediscountct=1 for mainnet) ELEMENTS_DISCOUNTED_VSIZE_VERSION = 230203 ) @@ -1319,7 +1321,7 @@ func cacheAliases() { // The goal is to spend maximum available liquid // To rebalance a channel with high enough historic fee PPM func findSwapInCandidate(candidate *SwapParams) error { - minAmount := config.Config.AutoSwapThresholdAmount - swapFeeReserveLBTC(10) // assume 10 UTXOs + minAmount := config.Config.AutoSwapThresholdAmount - SWAP_LBTC_RESERVE minPPM := config.Config.AutoSwapThresholdPPM client, cleanup, err := ps.GetClient(config.Config.RpcHost) @@ -1500,7 +1502,7 @@ func executeAutoSwap() { return } - amount = min(amount, satAmount-swapFeeReserveLBTC(10)) // assume 10 UTXOs + amount = min(amount, satAmount-SWAP_LBTC_RESERVE) // execute swap autoSwapId, err = ps.SwapIn(client, amount, candidate.ChannelId, "lbtc", false) @@ -1932,15 +1934,3 @@ func pollBalances() { log.Println("Polled peers for balances") } } - -// depends on number of UTXOs -func swapFeeReserveLBTC(numUTXOs int) uint64 { - if hasDiscountedvSize { - n := numUTXOs*7 + 20 // better estimate for lots of UTXOs - if n < 75 { - n = 75 // peerswap assumes 75 sats - } - return uint64(n) - } - return 300 -} diff --git a/cmd/psweb/templates/peer.gohtml b/cmd/psweb/templates/peer.gohtml index fecc388..b6bcd6c 100644 --- a/cmd/psweb/templates/peer.gohtml +++ b/cmd/psweb/templates/peer.gohtml @@ -345,7 +345,7 @@ change = Math.floor(-amountToAllocate); // elements spends dust change as extra fee - if (change < 1000) { + if (change <= 1100) { // one output only {{if .HasDiscountedvSize}} // 1->1 230 (= 1->2 - 39) From 6e3f8eaefbb699c09c5e8b7414468a9d86102e44 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Wed, 5 Feb 2025 22:15:14 +0100 Subject: [PATCH 09/16] LBTC balance reserve to 1200 --- CHANGELOG.md | 2 +- cmd/psweb/main.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be8854b..5e98310 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - Catch Bitcoin Core and Elements Core RPC errors - Fix telegram bot panic on wrong token or chat id - Allow fee rate with decimals precision -- Set LBTC balance reserve to 2000 sats to save on fees +- Set LBTC balance reserve to 1200 sats to save on fees ## 1.7.6 diff --git a/cmd/psweb/main.go b/cmd/psweb/main.go index 4b79f5b..c6486b5 100644 --- a/cmd/psweb/main.go +++ b/cmd/psweb/main.go @@ -38,8 +38,8 @@ const ( // Swap Out reserve to deduct from channel local balance SWAP_OUT_CHANNEL_RESERVE = 10000 // https://github.com/ElementsProject/peerswap/pull/304#issuecomment-2303931071 - SWAP_LBTC_RESERVE = 2000 - // Elements v23.02.03 introduced vsize discount (set creatediscountct=1 for mainnet) + SWAP_LBTC_RESERVE = 1200 + // assume creatediscountct=1 for mainnet in elements.conf ELEMENTS_DISCOUNTED_VSIZE_VERSION = 230203 ) From 7e9eb1019893161981aea14c25425046ae96ab4e Mon Sep 17 00:00:00 2001 From: Impa10r Date: Wed, 5 Feb 2025 22:54:16 +0100 Subject: [PATCH 10/16] Predict reserve as change --- cmd/psweb/templates/peer.gohtml | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/cmd/psweb/templates/peer.gohtml b/cmd/psweb/templates/peer.gohtml index b6bcd6c..36cbd1f 100644 --- a/cmd/psweb/templates/peer.gohtml +++ b/cmd/psweb/templates/peer.gohtml @@ -305,8 +305,8 @@ return amountB - amountA; }); - // total amount to allocate among UTXOs - let amountToAllocate = swapAmount; + // total amount to allocate among UTXOs, including reserve + let amountToAllocate = swapAmount + 1200; // Iterate through table rows tableArray.forEach(function(row) { @@ -338,26 +338,11 @@ // 2->2 338 (= 1->2 + 69) // 3->2 406 (= 2->2 + 68) vbyteSize = 200 + inputs * 69; - discount =" discount"; + discount =" discounted"; {{end}} fee = Math.ceil(vbyteSize * feeRate); change = Math.floor(-amountToAllocate); - - // elements spends dust change as extra fee - if (change <= 1100) { - // one output only - {{if .HasDiscountedvSize}} - // 1->1 230 (= 1->2 - 39) - // 2->1 271 (= 2->2 - 67) - // 3->1 340 (= 3->2 - 66) - vbyteSize -= 66; - {{else}} - vbyteSize -= 1,191; - {{end}} - // but the fee increases by change - fee += change; - } } } else { let claimSize = 149; // BTC From c79648f38e6cd368c5b7d78b87f0c9b6824ec085 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Thu, 6 Feb 2025 00:00:37 +0100 Subject: [PATCH 11/16] Remove anchor warning for swap outss --- CHANGELOG.md | 4 ++-- cmd/psweb/handlers.go | 6 +++++- cmd/psweb/main.go | 22 ++++++++++++---------- cmd/psweb/templates/peer.gohtml | 7 ++++--- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e98310..3f876ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,8 @@ - Better indication when all channels with a peer are inactive - Catch Bitcoin Core and Elements Core RPC errors - Fix telegram bot panic on wrong token or chat id -- Allow fee rate with decimals precision -- Set LBTC balance reserve to 1200 sats to save on fees +- Allow Bitcoin fee rates with 0.01 precision +- Reserve 1200 sats of LBTC balance to save on Elements fees ## 1.7.6 diff --git a/cmd/psweb/handlers.go b/cmd/psweb/handlers.go index b76e78a..5398bfd 100644 --- a/cmd/psweb/handlers.go +++ b/cmd/psweb/handlers.go @@ -454,7 +454,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { } else { peerLiquidBalance = formatWithThousandSeparators(ptr.Amount) } - maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE, int64(ptr.Amount)-SWAP_LBTC_RESERVE))) + maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE, int64(ptr.Amount)))) } else { maxLiquidSwapOut = uint64(max(0, int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE)) } @@ -595,6 +595,8 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { HasDiscountedvSize bool RedColor string IsOnline bool + AnchorReserve uint64 + LiquidReserve uint64 } redColor := "red" @@ -650,6 +652,8 @@ func peerHandler(w http.ResponseWriter, r *http.Request) { HasDiscountedvSize: hasDiscountedvSize, RedColor: redColor, IsOnline: isOnline, + AnchorReserve: ANCHOR_RESERVE, + LiquidReserve: SWAP_LBTC_RESERVE, } // executing template named "peer" diff --git a/cmd/psweb/main.go b/cmd/psweb/main.go index c6486b5..5d753f5 100644 --- a/cmd/psweb/main.go +++ b/cmd/psweb/main.go @@ -36,9 +36,11 @@ const ( // App VERSION tag VERSION = "v1.7.7" // Swap Out reserve to deduct from channel local balance - SWAP_OUT_CHANNEL_RESERVE = 10000 + SWAP_OUT_CHANNEL_RESERVE = 10_000 // https://github.com/ElementsProject/peerswap/pull/304#issuecomment-2303931071 - SWAP_LBTC_RESERVE = 1200 + SWAP_LBTC_RESERVE = 1_200 + // Unusable BTC balance + ANCHOR_RESERVE = 25_000 // assume creatediscountct=1 for mainnet in elements.conf ELEMENTS_DISCOUNTED_VSIZE_VERSION = 230203 ) @@ -1040,12 +1042,12 @@ func convertSwapsToHTMLTable(swaps []*peerswaprpc.PrettyPrintSwap, nodeId string if cost != 0 { totalCost += cost ppm := cost * 1_000_000 / int64(swap.Amount) - table += " +" + table += "profit, sats. PPM: " + formatSigned(-ppm) + "\">+" } else { - table += formatSigned(ppm) + "\">" + table += "cost, sats. PPM: " + formatSigned(ppm) + "\">" } table += formatSigned(-cost) + "" } @@ -1797,14 +1799,14 @@ func advertiseBalances() { bitcoinBalance := uint64(ln.ConfirmedWalletBalance(cl)) // haircut by anchor reserve - if bitcoinBalance >= 25000 { - bitcoinBalance -= 25000 + if bitcoinBalance >= ANCHOR_RESERVE { + bitcoinBalance -= ANCHOR_RESERVE } liquidBalance := res2.GetSatAmount() // Elements fee bug does not permit sending the whole balance, haircut it - if liquidBalance >= 2000 { - liquidBalance -= 2000 + if liquidBalance >= SWAP_LBTC_RESERVE { + liquidBalance -= SWAP_LBTC_RESERVE } cutOff := time.Now().AddDate(0, 0, -1).Unix() - 120 @@ -1814,7 +1816,7 @@ func advertiseBalances() { maxBalance := uint64(0) if ln.AdvertiseBitcoinBalance || ln.AdvertiseLiquidBalance { for _, ch := range peer.Channels { - maxBalance = max(maxBalance, ch.RemoteBalance) + maxBalance = max(maxBalance, ch.RemoteBalance-SWAP_OUT_CHANNEL_RESERVE) } } diff --git a/cmd/psweb/templates/peer.gohtml b/cmd/psweb/templates/peer.gohtml index 36cbd1f..3f3b2e3 100644 --- a/cmd/psweb/templates/peer.gohtml +++ b/cmd/psweb/templates/peer.gohtml @@ -227,7 +227,8 @@ return; } - if (document.getElementById("from").value != "ln") { + const isSwapIn = document.getElementById("from").value != "ln"; + if (isSwapIn) { title = "Assumed UTXOs:\n"; if (asset == "btc") { // Initialize total UTXO amount and UTXO counters @@ -306,7 +307,7 @@ }); // total amount to allocate among UTXOs, including reserve - let amountToAllocate = swapAmount + 1200; + let amountToAllocate = swapAmount + {{.LiquidReserve}}; // Iterate through table rows tableArray.forEach(function(row) { @@ -383,7 +384,7 @@ text += "Total cost: " + formatWithThousandSeparators(fee) + " sats\n"; text += "Cost PPM: " + formatWithThousandSeparators(Math.round(fee * 1000000 / swapAmount)); - if (asset == "btc" && change < 25000) { + if (isSwapIn && asset == "btc" && change < {{.AnchorReserve}}) { text += "\nWARNING: No reserve is left for anchor fee bumping!"; } From d0ccb0497dc6f82f373e5a363d91635be299d8d1 Mon Sep 17 00:00:00 2001 From: Impa10r Date: Thu, 6 Feb 2025 15:55:04 +0100 Subject: [PATCH 12/16] Show claim join time limit --- cmd/psweb/handlers.go | 12 ++++++++---- cmd/psweb/main.go | 11 ++++------- cmd/psweb/templates/bitcoin.gohtml | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/cmd/psweb/handlers.go b/cmd/psweb/handlers.go index 5398bfd..425474d 100644 --- a/cmd/psweb/handlers.go +++ b/cmd/psweb/handlers.go @@ -723,6 +723,7 @@ func bitcoinHandler(w http.ResponseWriter, r *http.Request) { ClaimJoinStatus string HasClaimJoinPending bool ClaimJoinETA int + ClaimJointTimeLimit string } btcBalance := ln.ConfirmedWalletBalance(cl) @@ -761,14 +762,16 @@ func bitcoinHandler(w http.ResponseWriter, r *http.Request) { duration := time.Duration(10*(int32(peginBlocks)-confs)) * time.Minute maxConfs := int32(peginBlocks) cjETA := 34 + cjTimeLimit := "" - bh := int32(ln.GetBlockHeight()) + currentBlockHeight := int32(ln.GetBlockHeight()) if ln.MyRole != "none" { target := int32(ln.ClaimBlockHeight) - maxConfs = target - bh + confs - duration = time.Duration(10*(target-bh)) * time.Minute + maxConfs = target - currentBlockHeight + confs + duration = time.Duration(10*(target-currentBlockHeight)) * time.Minute } else if ln.ClaimJoinHandler != "" { - cjETA = int((int32(ln.JoinBlockHeight) - bh + int32(peginBlocks)) / 6) + cjETA = int((int32(ln.JoinBlockHeight) - currentBlockHeight + int32(peginBlocks)) / 6) + cjTimeLimit = time.Now().Add(time.Duration(10*(ln.JoinBlockHeight-uint32(currentBlockHeight))) * time.Minute).Format("3:04 PM") } progress := confs * 100 / int32(maxConfs) @@ -810,6 +813,7 @@ func bitcoinHandler(w http.ResponseWriter, r *http.Request) { IsClaimJoin: config.Config.PeginClaimJoin, ClaimJoinStatus: ln.ClaimStatus, HasClaimJoinPending: ln.ClaimJoinHandler != "", + ClaimJointTimeLimit: cjTimeLimit, ClaimJoinETA: cjETA, } diff --git a/cmd/psweb/main.go b/cmd/psweb/main.go index 5d753f5..4ae6057 100644 --- a/cmd/psweb/main.go +++ b/cmd/psweb/main.go @@ -1133,7 +1133,6 @@ func checkPegin() { // invitation expired ln.ClaimStatus = "No ClaimJoin peg-in is pending" log.Println("Invitation expired from", ln.ClaimJoinHandler) - telegramSendMessage("🧬 ClaimJoin Invitation expired") ln.ClaimJoinHandler = "" db.Save("ClaimJoin", "ClaimStatus", ln.ClaimStatus) @@ -1143,13 +1142,11 @@ func checkPegin() { if config.Config.PeginTxId == "" { // send telegram if received new ClaimJoin invitation if peginInvite != ln.ClaimJoinHandler { - t := "🧬 There is a ClaimJoin peg-in pending" - if ln.ClaimJoinHandler == "" { - t = "🧬 ClaimJoin peg-in has ended" - } else { + t := "🧬 ClaimJoin invitation has expired" + if ln.ClaimJoinHandler != "" { duration := time.Duration(10*(ln.JoinBlockHeight-currentBlockHeight)) * time.Minute - formattedDuration := time.Time{}.Add(duration).Format("15h 04m") - t += ", time limit to join: " + formattedDuration + timeLimit := time.Now().Add(duration).Format("3:04 PM") + t = "🧬 Invitation to join a confidential peg-in before " + timeLimit } if telegramSendMessage(t) { peginInvite = ln.ClaimJoinHandler diff --git a/cmd/psweb/templates/bitcoin.gohtml b/cmd/psweb/templates/bitcoin.gohtml index 4f3faf2..72bb265 100644 --- a/cmd/psweb/templates/bitcoin.gohtml +++ b/cmd/psweb/templates/bitcoin.gohtml @@ -85,7 +85,7 @@ Amount is capped at remote channel balance to mimic brute force discovery" for="