Skip to content

Commit

Permalink
Fix panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Impa10r committed May 22, 2024
1 parent 8a35f9f commit 41d5f83
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"buildFlags": "-tags lnd",
"buildFlags": "-tags cln",
"program": "${workspaceFolder}/cmd/psweb/",
"showLog": false,
"envFile": "${workspaceFolder}/.env",
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 1.4.7

- Remove resthost from peerswap.conf
- Fix panic in appendInvoices
- Add revenue and costs to tooltips

## 1.4.6

Expand Down
20 changes: 8 additions & 12 deletions cmd/psweb/config/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func LoadPS() {
user := GetPeerswapCLNSetting("Bitcoin", "rpcuser")
password := GetPeerswapCLNSetting("Bitcoin", "rpcpassword")

if host == "" && user == "" && password == "" {
if host == "" || user == "" || password == "" {
Config.BitcoinHost = GetBlockIoHost()
Config.BitcoinUser = ""
Config.BitcoinPass = ""
Expand Down Expand Up @@ -182,22 +182,18 @@ func GetPeerswapCLNSetting(section, searchVariable string) string {
}
// Convert the content to a string
fileContent := string(content)
value := ""

// Search section
if sectionIndex := strings.Index(fileContent, "["+section+"]"); sectionIndex > -1 {
// Search variable should start from new line '\n' and be followed with '='
if index := strings.Index(fileContent[sectionIndex:], "\n"+searchVariable+"="); index > 0 {
startIndex := sectionIndex + index + len(searchVariable) + 2
for _, char := range fileContent[startIndex:] {
if char == '\n' || char == '\r' {
break
}
if char != '"' {
value += string(char)
lines := strings.Split(string(fileContent[sectionIndex:]), "\n")
for _, line := range lines {
if parts := strings.Split(line, "="); len(parts) > 1 {
if parts[0] == searchVariable {
// Remove double quotes
return strings.ReplaceAll(strings.TrimSpace(parts[1]), `"`, "")
}
}
}
}
return value
return ""
}
13 changes: 3 additions & 10 deletions cmd/psweb/config/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,11 @@ func getConfSetting(searchVariable, filePath string) string {
}

lines := strings.Split(string(content), "\n")

for _, line := range lines {
if strings.HasPrefix(line, searchVariable+"=") {
startIndex := len(searchVariable) + 1
value := ""
for _, char := range line[startIndex:] {
if char == '\n' || char == '\r' || char == ' ' {
break
}
value += string(char)
if parts := strings.Split(line, "="); len(parts) > 1 {
if parts[0] == searchVariable {
return strings.TrimSpace(parts[1])
}
return value
}
}
// not found
Expand Down
19 changes: 10 additions & 9 deletions cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
)

const (
Implementation = "CLN"
fileRPC = "lightning-rpc"
SwapFeeReserveLBTC = uint64(000)
Implementation = "CLN"
fileRPC = "lightning-rpc"
// Liquid balance to reserve in auto swap-ins
// https://github.com/ElementsProject/peerswap/blob/master/clightning/clightning_commands.go#L392
SwapFeeReserveLBTC = uint64(0)
SwapFeeReserveBTC = uint64(2000)
)

Expand Down Expand Up @@ -894,15 +896,15 @@ func fetchPaymentsStats(client *glightning.Lightning, timeStamp uint64, channelI
continue
}
if inv.Invoices[0].PaidAt > timeStamp {
if len(inv.Invoices[0].Label) > 7 {
if inv.Invoices[0].Label[:8] == "peerswap" {
if parts := strings.Split(inv.Invoices[0].Label, " "); len(parts) > 4 {
if parts[0] == "peerswap" {
// find swap id
parts := strings.Split(inv.Invoices[0].Label, " ")
if parts[2] == "fee" && len(parts[4]) > 0 {
// save rebate payment
SwapRebates[parts[4]] = int64(htlc.AmountMsat) / 1000
}
} else {
// only account for non-peerswap related
invoicedMsat += htlc.AmountMsat
}
}
Expand All @@ -929,10 +931,9 @@ func fetchPaymentsStats(client *glightning.Lightning, timeStamp uint64, channelI
invoice, err := zpay32.Decode(pmt.Payments[0].Bolt11, harnessNetParams)
if err == nil {
if invoice.Description != nil {
if len(*invoice.Description) > 7 {
if (*invoice.Description)[:8] == "peerswap" {
if parts := strings.Split(*invoice.Description, " "); len(parts) > 4 {
if parts[0] == "peerswap" {
// find swap id
parts := strings.Split(*invoice.Description, " ")
if parts[2] == "fee" && len(parts[4]) > 0 {
// save rebate payment
SwapRebates[parts[4]] = int64(htlc.AmountMsat) / 1000
Expand Down
11 changes: 5 additions & 6 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
const (
Implementation = "LND"
// Liquid balance to reserve in auto swap-ins
// https://github.com/ElementsProject/peerswap/blob/master/peerswaprpc/server.go#L234
SwapFeeReserveLBTC = uint64(1000)
SwapFeeReserveBTC = uint64(2000)
)
Expand Down Expand Up @@ -796,10 +797,9 @@ func appendPayment(payment *lnrpc.Payment) {
invoice, err := zpay32.Decode(payment.PaymentRequest, harnessNetParams)
if err == nil {
if invoice.Description != nil {
if len(*invoice.Description) > 7 {
if (*invoice.Description)[:8] == "peerswap" {
if parts := strings.Split(*invoice.Description, " "); len(parts) > 4 {
if parts[0] == "peerswap" {
// find swap id
parts := strings.Split(*invoice.Description, " ")
if parts[2] == "fee" && len(parts[4]) > 0 {
// save rebate payment
SwapRebates[parts[4]] = int64(payment.ValueMsat) / 1000
Expand Down Expand Up @@ -1035,10 +1035,9 @@ func appendInvoice(invoice *lnrpc.Invoice) {
}
// only append settled htlcs
if invoice.State == lnrpc.Invoice_SETTLED {
if len(invoice.Memo) > 7 {
if invoice.Memo[:8] == "peerswap" {
if parts := strings.Split(invoice.Memo, " "); len(parts) > 4 {
if parts[0] == "peerswap" {
// find swap id
parts := strings.Split(invoice.Memo, " ")
if parts[2] == "fee" && len(parts[4]) > 0 {
// save rebate payment
SwapRebates[parts[4]] = int64(invoice.AmtPaidMsat) / 1000
Expand Down
6 changes: 6 additions & 0 deletions cmd/psweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2040,10 +2040,16 @@ func convertPeersToHTMLTable(peers []*peerswaprpc.PeerSwapPeer, allowlistedPeers

if stats.FeeSat > 0 {
flowText += "\nRevenue: +" + formatWithThousandSeparators(stats.FeeSat)
if stats.RoutedOut > 0 {
flowText += "\nRevenue PPM: " + formatWithThousandSeparators(stats.FeeSat*1_000_000/stats.RoutedOut)
}
}

if stats.PaidCost > 0 {
flowText += "\nCosts: -" + formatWithThousandSeparators(stats.PaidCost)
if stats.PaidOut > 0 {
flowText += "\nCosts PPM: " + formatWithThousandSeparators(stats.PaidCost*1_000_000/stats.PaidOut)
}
}

tooltip += flowText
Expand Down

0 comments on commit 41d5f83

Please sign in to comment.