Skip to content

Commit

Permalink
Merge pull request #45 from Impa10r/v1.4.5
Browse files Browse the repository at this point in the history
v1.4.5
  • Loading branch information
Impa10r authored May 20, 2024
2 parents 10f4bd5 + ecc0837 commit 0de208c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 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.4.5

- Add swap costs to home page
- Fix error reporting

## 1.4.4

- Fix panic in v1.4.3
Expand Down
57 changes: 46 additions & 11 deletions cmd/psweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

const (
// App version tag
version = "v1.4.4"
version = "v1.4.5"

// Liquid balance to reserve in auto swaps
// Min is 1000, but the swap will spend it all on fee
Expand Down Expand Up @@ -170,6 +170,9 @@ func main() {
// download and subscribe to payments
go ln.CachePayments()

// fetch all chain costs
go cacheSwapCosts()

// Handle termination signals
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
Expand Down Expand Up @@ -709,7 +712,7 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {
swapData += `</a></td></tr>
<tr><td style="text-align: right">Amount:</td><td>`
swapData += formatWithThousandSeparators(swap.Amount)
swapData += `</td></tr>
swapData += ` sats</td></tr>
<tr><td style="text-align: right">ChannelId:</td><td>`
swapData += swap.ChannelId
swapData += `</td></tr>`
Expand Down Expand Up @@ -739,14 +742,15 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {
swapData += strconv.FormatUint(uint64(swap.LndChanId), 10)

cost := swapCost(swap)
if cost > 0 {
swapData += `<tr><td style="text-align: right">Swap Cost:</td><td>`
swapData += formatWithThousandSeparators(uint64(cost)) + " sats"
}
if cost < 0 {
if cost != 0 {
ppm := cost * 1_000_000 / int64(swap.Amount)

swapData += `<tr><td style="text-align: right">Swap Cost:</td><td>`
swapData += "Rebate " + formatWithThousandSeparators(uint64(-cost)) + " sats"
swapData += formatSigned(cost) + " sats"
swapData += `<tr><td style="text-align: right">Cost PPM:</td><td>`
swapData += formatSigned(ppm)
}

swapData += `</td></tr>
</table>
</div>
Expand Down Expand Up @@ -1092,7 +1096,8 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
}

if err != nil {
if err.Error() == "Request timed out" {
e := err.Error()
if e == "Request timed out" || strings.HasPrefix(e, "rpc error: code = Unavailable desc = rpc timeout reached") {
// sometimes the swap is pending anyway
res, er := ps.ListActiveSwaps(client)
if er != nil {
Expand Down Expand Up @@ -1431,12 +1436,17 @@ func redirectWithError(w http.ResponseWriter, r *http.Request, redirectUrl strin
t := fmt.Sprintln(err)
// translate common errors into plain English
switch {
case strings.HasPrefix(t, "rpc error"):
case strings.HasPrefix(t, "rpc error: code = Unavailable desc = connection error"):
t = "Cannot connect to peerswapd. It either has not started listening yet or PeerSwap Host parameter is wrong. Check logs."
case strings.HasPrefix(t, "Unable to dial socket"):
t = "Cannot connect to lightningd. It either failed to start or has wrong configuration. Check logs."
case strings.HasPrefix(t, "-32601:Unknown command 'peerswap-reloadpolicy'"):
t = "Peerswap plugin is not installed or has wrong configuration. Check .lightning/config."
case strings.HasPrefix(t, "rpc error: code = "):
i := strings.Index(t, "desc =")
if i > 0 {
t = t[i+7:]
}
}
// display the error to the web page header
msg := url.QueryEscape(t)
Expand Down Expand Up @@ -2305,7 +2315,7 @@ func convertSwapsToHTMLTable(swaps []*peerswaprpc.PrettyPrintSwap, nodeId string
// clicking on swap status will filter swaps with equal status
table += "<a title=\"Filter by state: " + simplifySwapState(swap.State) + "\" href=\"/?id=" + nodeId + "&state=" + simplifySwapState(swap.State) + "&role=" + swapRole + "\">"
table += visualiseSwapState(swap.State, false) + "&nbsp</a>"
table += formatWithThousandSeparators(swap.Amount)
table += " <span title=\"Swap amount, sats\">" + formatWithThousandSeparators(swap.Amount) + "</span>"

asset := "🌊"
if swap.Asset == "btc" {
Expand All @@ -2323,6 +2333,12 @@ func convertSwapsToHTMLTable(swaps []*peerswaprpc.PrettyPrintSwap, nodeId string
table += " ⚡&nbsp⇨&nbsp" + asset
}

cost := swapCost(swap)
if cost != 0 {
ppm := cost * 1_000_000 / int64(swap.Amount)
table += " <span title=\"Swap cost, sats. PPM: " + formatSigned(ppm) + "\">" + formatSigned(cost) + "</span>"
}

table += "</td><td id=\"scramble\" style=\"overflow-wrap: break-word;\">"

role := ""
Expand Down Expand Up @@ -2667,3 +2683,22 @@ func onchainTxFee(asset, txId string) int64 {
}
return fee
}

func cacheSwapCosts() {
client, cleanup, err := ps.GetClient(config.Config.RpcHost)
if err != nil {
return
}
defer cleanup()

res, err := ps.ListSwaps(client)
if err != nil {
return
}

swaps := res.GetSwaps()

for _, swap := range swaps {
swapCost(swap)
}
}
8 changes: 4 additions & 4 deletions cmd/psweb/templates/peer.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ Sincerely,
<tr style="border: 1px dotted">
<td style="text-align: center">Sent
<td title="Initiated {{fmt .Peer.AsSender.SwapsIn}} swap-ins for {{fmt .Peer.AsSender.SatsIn}} sats total" style="text-align: center">{{m .Peer.AsSender.SatsIn}}</td>
<td title="Chain fees paid, Sats (PPM)" style="text-align: center">{{fs .SenderInFee}}{{if .SenderInFee}} ({{fs .SenderInFeePPM}}){{end}}</td>
<td title="Opening fees paid, Sats (PPM)" style="text-align: center">{{fs .SenderInFee}}{{if .SenderInFee}} ({{fs .SenderInFeePPM}}){{end}}</td>
<td title="Initiated {{fmt .Peer.AsSender.SwapsOut}} swap-outs for {{fmt .Peer.AsSender.SatsOut}} sats total" style="text-align: center">{{m .Peer.AsSender.SatsOut}}</td>
<td title="Fees rebated to peer, Sats (PPM)" style="text-align: center">{{fmt .Peer.PaidFee}}{{if .Peer.PaidFee}} ({{fs .SenderOutFeePPM}}){{end}}</td>
<td title="Rebated paid to peer, Sats (PPM)" style="text-align: center">{{fmt .Peer.PaidFee}}{{if .Peer.PaidFee}} ({{fs .SenderOutFeePPM}}){{end}}</td>
</tr>
<tr style="border: 1px dotted">
<td style="text-align: center">Rcvd
<td title="Received {{fmt .Peer.AsReceiver.SwapsOut}} swap-outs for {{fmt .Peer.AsReceiver.SatsOut}} sats total" style="text-align: center">{{m .Peer.AsReceiver.SatsOut}}</td>
<td title="Chain fees paid less rebates, Sats (PPM)" style="text-align: center">{{fs .ReceiverOutFee}}{{if .ReceiverOutFee}} ({{fs .ReceiverOutFeePPM}}){{end}}</td>
<td title="Opening fees less rebates, Sats (PPM)" style="text-align: center">{{fs .ReceiverOutFee}}{{if .ReceiverOutFee}} ({{fs .ReceiverOutFeePPM}}){{end}}</td>
<td title="Received {{fmt .Peer.AsReceiver.SwapsIn}} swap-ins for {{fmt .Peer.AsReceiver.SatsIn}} sats total" style="text-align: center">{{m .Peer.AsReceiver.SatsIn}}</td>
<td title="Chain fees paid, Sats (PPM)" style="text-align: center">{{fs .ReceiverInFee}}{{if .ReceiverInFee}} ({{fs .ReceiverInFeePPM}}){{end}}</td>
<td title="Claim fees paid, Sats (PPM)" style="text-align: center">{{fs .ReceiverInFee}}{{if .ReceiverInFee}} ({{fs .ReceiverInFeePPM}}){{end}}</td>
</tr>
</table>
{{end}}
Expand Down

0 comments on commit 0de208c

Please sign in to comment.