Skip to content

Commit

Permalink
Merge pull request #43 from Impa10r/v1.4.3
Browse files Browse the repository at this point in the history
v1.4.3
  • Loading branch information
Impa10r authored May 19, 2024
2 parents 9112a9b + 3f15da1 commit e740d12
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 89 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 cln",
"buildFlags": "-tags lnd",
"program": "${workspaceFolder}/cmd/psweb/",
"showLog": false,
"envFile": "${workspaceFolder}/.env",
Expand Down
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.3

- Add http retry middleware
- Add swap costs display

## 1.4.2

- Subscribe to LND events to reduce CPU overhead
Expand Down
29 changes: 29 additions & 0 deletions cmd/psweb/internet/mempool.go → cmd/psweb/internet/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,32 @@ func SendRawTransaction(rawTx string) string {
}
return ""
}

// fetch transaction fee from api
func GetBitcoinTxFee(txid string) int64 {
if config.Config.BitcoinApi != "" {
api := config.Config.BitcoinApi + "/api/tx/" + txid
req, err := http.NewRequest("GET", api, nil)
if err == nil {
cl := GetHttpClient(true)
if cl == nil {
return 0
}
resp, err2 := cl.Do(req)
if err2 == nil {
defer resp.Body.Close()

// Create an instance of the struct to store the parsed data
var tx map[string]interface{}

err = json.NewDecoder(resp.Body).Decode(&tx)
if err != nil {
return 0
}
fee := tx["fee"].(float64)
return int64(fee)
}
}
}
return 0
}
38 changes: 38 additions & 0 deletions cmd/psweb/internet/liquid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package internet

import (
"encoding/json"
"net/http"

"peerswap-web/cmd/psweb/config"
)

// fetch transaction fee from liquid.network
func GetLiquidTxFee(txid string) int64 {
testnet := ""
if config.Config.Chain == "testnet" {
testnet = "/liquidtestnet"
}
api := "https://liquid.network" + testnet + "/api/tx/" + txid
req, err := http.NewRequest("GET", api, nil)
if err == nil {
cl := GetHttpClient(true)
if cl == nil {
return 0
}
resp, err2 := cl.Do(req)
if err2 == nil {
defer resp.Body.Close()

var tx map[string]interface{}

err = json.NewDecoder(resp.Body).Decode(&tx)
if err != nil {
return 0
}
fee := tx["fee"].(float64)
return int64(fee)
}
}
return 0
}
15 changes: 14 additions & 1 deletion cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,14 @@ func GetChannelStats(lndChannelId uint64, timeStamp uint64) *ChannelStats {
}
if inv.Invoices[0].PaidAt > timeStamp {
if len(inv.Invoices[0].Label) > 7 {
if inv.Invoices[0].Label[:8] != "peerswap" {
if inv.Invoices[0].Label[:8] == "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 {
invoicedMsat += htlc.AmountMsat
}
}
Expand Down Expand Up @@ -651,6 +658,12 @@ func GetChannelStats(lndChannelId uint64, timeStamp uint64) *ChannelStats {
if invoice.Description != nil {
if len(*invoice.Description) > 7 {
if (*invoice.Description)[:8] == "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
}
// skip peerswap-related payments
continue
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type ChanneInfo struct {
Active bool
}

// lighting payments from swap out initiator to receiver
var SwapRebates = make(map[string]int64)

func toSats(amount float64) int64 {
return int64(float64(100000000) * amount)
}
Expand Down
17 changes: 15 additions & 2 deletions cmd/psweb/ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,12 @@ func appendPayment(payment *lnrpc.Payment) {
if invoice.Description != nil {
if len(*invoice.Description) > 7 {
if (*invoice.Description)[:8] == "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
}
// skip peerswap-related payments
return
}
Expand Down Expand Up @@ -1021,8 +1027,15 @@ func appendInvoice(invoice *lnrpc.Invoice) {
}
// only append settled htlcs
if invoice.State == lnrpc.Invoice_SETTLED {
// skip peerswap-related
if len(invoice.Memo) < 8 || invoice.Memo[:8] != "peerswap" {
if len(invoice.Memo) > 7 && invoice.Memo[:8] == "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
}
} else {
// skip peerswap-related
for _, htlc := range invoice.Htlcs {
if htlc.State == lnrpc.InvoiceHTLCState_SETTLED {
invoiceHtlcs[htlc.ChanId] = append(invoiceHtlcs[htlc.ChanId], htlc)
Expand Down
Loading

0 comments on commit e740d12

Please sign in to comment.