Skip to content

Commit

Permalink
Merge pull request #35 from Impa10r/v1.3.5
Browse files Browse the repository at this point in the history
v1.3.5
  • Loading branch information
Impa10r authored May 12, 2024
2 parents e227bef + b6ed082 commit 44bc64f
Show file tree
Hide file tree
Showing 18 changed files with 841 additions and 222 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.3.5

- Estimate peg-in transaction size, total fee and PPM
- Add peer fee revenue stats to the main page

## 1.3.4

- Display channel flows since their last swaps
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Vlad Goryachev
Copyright (c) 2023-2024 Vlad Goryachev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 7 additions & 3 deletions cmd/psweb/config/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ func LoadPS() {
}

// get peerswap rpc host from peerswap.conf
Config.RpcHost = GetPeerswapLNDSetting("host")
host := GetPeerswapLNDSetting("host")
if host != "" {
Config.RpcHost = host
}

wallet := GetPeerswapLNDSetting("elementsd.rpcwallet")
if wallet != "" {
Config.ElementsWallet = wallet
}

host := GetPeerswapLNDSetting("elementsd.rpchost")
host = GetPeerswapLNDSetting("elementsd.rpchost")
if host != "" {
Config.ElementsHost = host
}
Expand Down Expand Up @@ -176,6 +179,7 @@ func getConfSetting(searchVariable, filePath string) string {
// Read the entire content of the file
content, err := os.ReadFile(filePath)
if err != nil {
log.Println("Error reading file", filePath, err)
return ""
}
// Convert the content to a string
Expand All @@ -185,7 +189,7 @@ func getConfSetting(searchVariable, filePath string) string {
startIndex := index + len(searchVariable) + 1
value := ""
for _, char := range fileContent[startIndex:] {
if char == '\n' || char == '\r' {
if char == '\n' || char == '\r' || char == ' ' {
break
}
value += string(char)
Expand Down
12 changes: 6 additions & 6 deletions cmd/psweb/internet/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetNodeAlias(id string) string {
}

// fetch high priority fee rate from mempool.space
func GetFeeRate() uint32 {
func GetFeeRate() float64 {
if config.Config.BitcoinApi != "" {
api := config.Config.BitcoinApi + "/api/v1/fees/recommended"
req, err := http.NewRequest("GET", api, nil)
Expand All @@ -81,11 +81,11 @@ func GetFeeRate() uint32 {

// Define a struct to match the JSON structure
type Fees struct {
FastestFee uint32 `json:"fastestFee"`
HalfHourFee uint32 `json:"halfHourFee"`
HourFee uint32 `json:"hourFee"`
EconomyFee uint32 `json:"economyFee"`
MinimumFee uint32 `json:"minimumFee"`
FastestFee float64 `json:"fastestFee"`
HalfHourFee float64 `json:"halfHourFee"`
HourFee float64 `json:"hourFee"`
EconomyFee float64 `json:"economyFee"`
MinimumFee float64 `json:"minimumFee"`
}

// Create an instance of the struct to store the parsed data
Expand Down
61 changes: 60 additions & 1 deletion cmd/psweb/liquid/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"path/filepath"
"time"

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

"github.com/alexmullins/zip"
)
Expand Down Expand Up @@ -199,12 +201,18 @@ func ListUnspent(outputs *[]UTXO) error {
type SendParams struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
SubtractFeeFromAmount bool `json:"subtractfeefromamount"`
Comment string `json:"comment"`
SubtractFeeFromAmount bool `json:"subtractfeefromamount,omitempty"`
Replaceable bool `json:"replaceable,omitempty"`
IgnoreBlindFail bool `json:"ignoreblindfail,omitempty"`
}

func SendToAddress(address string,
amountSats uint64,
comment string,
subtractFeeFromAmount bool,
replaceable bool,
ignoreBlindFail bool,
) (string, error) {
client := ElementsClient()
service := &Elements{client}
Expand All @@ -213,7 +221,10 @@ func SendToAddress(address string,
params := &SendParams{
Address: address,
Amount: toBitcoin(amountSats),
Comment: comment,
SubtractFeeFromAmount: subtractFeeFromAmount,
Replaceable: replaceable,
IgnoreBlindFail: ignoreBlindFail,
}

r, err := service.client.call("sendtoaddress", params, "/wallet/"+wallet)
Expand Down Expand Up @@ -313,6 +324,19 @@ type PeginAddress struct {
}

func GetPeginAddress(address *PeginAddress) error {

if config.Config.Chain == "testnet" {
// to not waste testnet sats where pegin is not implemented
// return new P2TR address in our own bitcoin wallet
addr, err := ln.NewAddress()
if err != nil {
return err
}
address.ClaimScript = ""
address.MainChainAddress = addr
return nil
}

client := ElementsClient()
service := &Elements{client}
wallet := config.Config.ElementsWallet
Expand Down Expand Up @@ -357,3 +381,38 @@ func ClaimPegin(rawTx, proof, claimScript string) (string, error) {
func toBitcoin(amountSats uint64) float64 {
return float64(amountSats) / float64(100000000)
}

type MemPoolInfo struct {
Loaded bool `json:"loaded"`
Size int `json:"size"`
Bytes int `json:"bytes"`
Usage int `json:"usage"`
TotalFee float64 `json:"total_fee"`
MaxMemPool int `json:"maxmempool"`
MemPoolMinFee float64 `json:"mempoolminfee"`
MinRelayTxFee float64 `json:"minrelaytxfee"`
UnbroadcastCount int `json:"unbroadcastcount"`
}

// return min fee in sat/vB
func GetMempoolMinFee() float64 {
client := ElementsClient()
service := &Elements{client}
params := &[]string{}

r, err := service.client.call("getmempoolinfo", params, "")
if err = handleError(err, &r); err != nil {
log.Printf("getmempoolinfo: %v", err)
return 0
}

var result MemPoolInfo

err = json.Unmarshal([]byte(r.Result), &result)
if err != nil {
log.Printf("getmempoolinfo unmarshall: %v", err)
return 0
}

return math.Round(result.MemPoolMinFee*100_000_000) / 1000
}
72 changes: 65 additions & 7 deletions cmd/psweb/ln/cln.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ func GetForwardingStats(lndChannelId uint64) *ForwardingStats {
if e.ResolvedTime > timestamp7d {
amountOut7d += e.OutMsat
feeMsat7d += e.FeeMsat
log.Println(e)
}
}
}
Expand Down Expand Up @@ -468,29 +467,63 @@ func GetForwardingStats(lndChannelId uint64) *ForwardingStats {
result.FeeSat6m = feeMsat6m / 1000
result.AssistedFeeSat6m = assistedMsat6m / 1000

if result.AmountOut7d > 0 {
result.FeePPM7d = result.FeeSat7d * 1_000_000 / result.AmountOut7d
}
if result.AmountIn7d > 0 {
result.AssistedPPM7d = result.AssistedFeeSat7d * 1_000_000 / result.AmountIn7d
}
if result.AmountOut30d > 0 {
result.FeePPM30d = result.FeeSat30d * 1_000_000 / result.AmountOut30d
}
if result.AmountIn30d > 0 {
result.AssistedPPM30d = result.AssistedFeeSat30d * 1_000_000 / result.AmountIn30d
}
if result.AmountOut6m > 0 {
result.FeePPM6m = result.FeeSat6m * 1_000_000 / result.AmountOut6m
}
if result.AmountIn6m > 0 {
result.AssistedPPM6m = result.AssistedFeeSat6m * 1_000_000 / result.AmountIn6m
}

return &result
}

// net balance change for a channel
func GetNetFlow(lndChannelId uint64, timeStamp uint64) int64 {
// forwarding stats for a channel since timestamp
func GetForwardingStatsSinceTS(lndChannelId uint64, timeStamp uint64) *ShortForwardingStats {

var (
result ShortForwardingStats
amountOut uint64
amountIn uint64
feeMsat uint64
assistedMsat uint64
)

netFlow := int64(0)
channelId := ConvertLndToClnChannelId(lndChannelId)
timeStampF := float64(timeStamp)

for _, e := range forwards.Forwards {
if e.InChannel == channelId {
if e.ResolvedTime > timeStampF {
netFlow -= int64(e.OutMsat)
amountOut += e.OutMsat
feeMsat += e.FeeMsat
}
}
if e.OutChannel == channelId {
if e.ResolvedTime > timeStampF {
netFlow += int64(e.OutMsat)
amountIn += e.OutMsat
assistedMsat += e.FeeMsat
}
}
}
return netFlow / 1000

result.AmountOut = amountOut / 1000
result.AmountIn = amountIn / 1000
result.FeeSat = feeMsat / 1000
result.AssistedFeeSat = assistedMsat / 1000

return &result
}

type ListPeerChannelsRequest struct {
Expand Down Expand Up @@ -529,3 +562,28 @@ func GetChannelInfo(client *glightning.Lightning, lndChannelId uint64, nodeId st

return info
}

func NewAddress() (string, error) {
client, clean, err := GetClient()
if err != nil {
log.Println("GetClient:", err)
return "", err
}
defer clean()

var res struct {
Bech32 string `json:"bech32"`
P2SHSegwit string `json:"p2sh-segwit"`
Taproot string `json:"p2tr"`
}

err = client.Request(&glightning.NewAddrRequest{
AddressType: "bech32",
}, &res)
if err != nil {
log.Println("NewAddrRequest:", err)
return "", err
}

return res.Bech32, nil
}
13 changes: 13 additions & 0 deletions cmd/psweb/ln/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,27 @@ type ForwardingStats struct {
AmountIn7d uint64
FeeSat7d uint64
AssistedFeeSat7d uint64
FeePPM7d uint64
AssistedPPM7d uint64
AmountOut30d uint64
AmountIn30d uint64
FeeSat30d uint64
AssistedFeeSat30d uint64
FeePPM30d uint64
AssistedPPM30d uint64
AmountOut6m uint64
AmountIn6m uint64
FeeSat6m uint64
AssistedFeeSat6m uint64
FeePPM6m uint64
AssistedPPM6m uint64
}

type ShortForwardingStats struct {
AmountOut uint64
AmountIn uint64
FeeSat uint64
AssistedFeeSat uint64
}

type ChanneInfo struct {
Expand Down
Loading

0 comments on commit 44bc64f

Please sign in to comment.