Skip to content

Commit

Permalink
Merge pull request #94 from Impa10r/v1.7.4
Browse files Browse the repository at this point in the history
v1.7.4
  • Loading branch information
Impa10r authored Dec 15, 2024
2 parents a75638b + f378cf0 commit b56fcbe
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 121 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
push.sh
cmd/psweb/__debug_bin*
.env
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"showLog": false,
"envFile": "${workspaceFolder}/.env",
//"args": ["-datadir", "/home/vlad/.peerswap_t4"]
//"args": ["-datadir", "/home/vlad/.peerswap3"]
"args": ["-datadir", "/home/vlad/.peerswap2"]
},
// sudo bash -c 'echo 0 > /proc/sys/kernel/yama/ptrace_scope'
// go install -tags cln -gcflags 'all=-N -l' ./cmd/psweb
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Versions

## 1.7.4

- Bump Go version to v1.22.2
- Bypass BTC balance check for a pegin from an external wallet
- Assume CT discounts on mainnet for Elements v23.02.03+
- Better predict swap cost for edge cases when change is under 1000 sats
- Try to get tx fee from local Elements first
- Warn of swap amount exceeding maximum

## 1.7.3

- Show L-BTC balance changes when sending backup by telegram
Expand Down
17 changes: 6 additions & 11 deletions cmd/psweb/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
ln.ListUnspent(cl, &utxosBTC, 1)

var utxosLBTC []liquid.UTXO
liquid.ListUnspent(&utxosLBTC)
liquid.ListUnspent(&utxosLBTC, elementsBitcoinId)

// to find a channel for swap-out
maxLocalBalance := uint64(0)
Expand Down Expand Up @@ -435,7 +435,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()), int64(maxRemoteBalance)-10000)
maxLiquidSwapIn := min(int64(satAmount)-int64(swapFeeReserveLBTC(len(utxosLBTC))), int64(maxRemoteBalance)-10000)
if maxLiquidSwapIn < 100_000 {
maxLiquidSwapIn = 0
}
Expand All @@ -451,7 +451,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()))))
maxLiquidSwapOut = uint64(max(0, min(int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE, int64(ptr.Amount)-int64(swapFeeReserveLBTC(1)))))
} else {
maxLiquidSwapOut = uint64(max(0, int64(maxLocalBalance)-SWAP_OUT_CHANNEL_RESERVE))
}
Expand Down Expand Up @@ -577,8 +577,6 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
KeysendSats uint64
OutputsBTC *[]ln.UTXO
OutputsLBTC *[]liquid.UTXO
ReserveLBTC uint64
ReserveBTC uint64
HasInboundFees bool
PeerBitcoinBalance string // "" means no data
MaxBitcoinSwapOut uint64
Expand Down Expand Up @@ -633,8 +631,6 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
KeysendSats: keysendSats,
OutputsBTC: &utxosBTC,
OutputsLBTC: &utxosLBTC,
ReserveLBTC: swapFeeReserveLBTC(),
ReserveBTC: swapFeeReserveBTC,
HasInboundFees: ln.HasInboundFees(),
PeerBitcoinBalance: peerBitcoinBalance,
MaxBitcoinSwapOut: maxBitcoinSwapOut,
Expand Down Expand Up @@ -1775,7 +1771,7 @@ func liquidHandler(w http.ResponseWriter, r *http.Request) {
return
}

walletInfo, err := liquid.GetWalletInfo(config.Config.ElementsWallet)
walletInfo, err := liquid.GetWalletInfo()
if err != nil {
redirectWithError(w, r, "/?", err)
return
Expand Down Expand Up @@ -2424,7 +2420,7 @@ func submitHandler(w http.ResponseWriter, r *http.Request) {
addressType = "bech32m"
}

addr, err := liquid.GetNewAddress(label, addressType, config.Config.ElementsWallet)
addr, err := liquid.GetNewAddress(label, addressType)
if err != nil {
redirectWithError(w, r, "/liquid?", err)
return
Expand Down Expand Up @@ -2767,9 +2763,8 @@ func loadingHandler(w http.ResponseWriter, r *http.Request) {
}

func backupHandler(w http.ResponseWriter, r *http.Request) {
wallet := config.Config.ElementsWallet
// returns .bak with the name of the wallet
if fileName, err := liquid.BackupAndZip(wallet); err == nil {
if fileName, err := liquid.BackupAndZip(); err == nil {
// Set the Content-Disposition header to suggest a filename
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
// Serve the file for download
Expand Down
56 changes: 42 additions & 14 deletions cmd/psweb/liquid/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ type UTXO struct {
Safe bool `json:"safe"`
}

func ListUnspent(outputs *[]UTXO) error {
func ListUnspent(outputs *[]UTXO, assetId string) error {
client := ElementsClient()
service := &Elements{client}
params := []string{}
queryOptions := make(map[string]interface{})
if assetId != "" {
queryOptions["asset"] = assetId
}
params := []interface{}{1, 9999999, []string{}, true, queryOptions}
wallet := config.Config.ElementsWallet

r, err := service.client.call("listunspent", params, "/wallet/"+wallet)
Expand Down Expand Up @@ -242,10 +246,11 @@ func SendToAddress(address string,

// Backup wallet and zip it with Elements Core password
// .bak's name is equal to master blinding key
func BackupAndZip(wallet string) (string, error) {
func BackupAndZip() (string, error) {

client := ElementsClient()
service := &Elements{client}
wallet := config.Config.ElementsWallet

r, err := service.client.call("dumpmasterblindingkey", []string{}, "/wallet/"+wallet)
if err = handleError(err, &r); err != nil {
Expand Down Expand Up @@ -409,20 +414,17 @@ func EstimateFee() float64 {
func GetVersion() int {
client := ElementsClient()
service := &Elements{client}
wallet := config.Config.ElementsWallet
params := &[]string{}

r, err := service.client.call("getnetworkinfo", params, "/wallet/"+wallet)
r, err := service.client.call("getnetworkinfo", params, "")
if err = handleError(err, &r); err != nil {
log.Printf("Elements GetVersion error: %v", err)
return 0
}

var response map[string]interface{}

err = json.Unmarshal([]byte(r.Result), &response)
if err != nil {
log.Printf("Elements GetVersion error: %v", err)
return 0
}

Expand Down Expand Up @@ -474,11 +476,11 @@ func CreatePSET(params interface{}) (string, error) {
return response, nil
}

func ProcessPSET(base64psbt, wallet string) (string, bool, error) {
func ProcessPSET(base64psbt string) (string, bool, error) {

client := ElementsClient()
service := &Elements{client}

wallet := config.Config.ElementsWallet
params := []interface{}{base64psbt}

r, err := service.client.call("walletprocesspsbt", params, "/wallet/"+wallet)
Expand Down Expand Up @@ -804,11 +806,11 @@ type AddressInfo struct {
Labels []string `json:"labels"`
}

func GetAddressInfo(addr, wallet string) (*AddressInfo, error) {
func GetAddressInfo(addr string) (*AddressInfo, error) {

client := ElementsClient()
service := &Elements{client}

wallet := config.Config.ElementsWallet
params := []interface{}{addr}

r, err := service.client.call("getaddressinfo", params, "/wallet/"+wallet)
Expand Down Expand Up @@ -898,10 +900,11 @@ type BalanceInfo struct {
}

// returns block hash
func GetWalletInfo(wallet string) (*WalletInfo, error) {
func GetWalletInfo() (*WalletInfo, error) {
client := ElementsClient()
service := &Elements{client}
params := &[]interface{}{}
wallet := config.Config.ElementsWallet

r, err := service.client.call("getwalletinfo", params, "/wallet/"+wallet)
if err = handleError(err, &r); err != nil {
Expand All @@ -920,11 +923,11 @@ func GetWalletInfo(wallet string) (*WalletInfo, error) {
return &response, nil
}

func GetNewAddress(label, addressType, wallet string) (string, error) {
func GetNewAddress(label, addressType string) (string, error) {

client := ElementsClient()
service := &Elements{client}

wallet := config.Config.ElementsWallet
params := []interface{}{label, addressType}

r, err := service.client.call("getnewaddress", params, "/wallet/"+wallet)
Expand All @@ -943,3 +946,28 @@ func GetNewAddress(label, addressType, wallet string) (string, error) {

return response, nil
}

func DumpAssetLabels() (*map[string]string, error) {

client := ElementsClient()
service := &Elements{client}
wallet := config.Config.ElementsWallet

params := []interface{}{}

r, err := service.client.call("dumpassetlabels", params, "/wallet/"+wallet)
if err = handleError(err, &r); err != nil {
log.Printf("Failed to DumpAssetLabels: %v", err)
return nil, err
}

var response = make(map[string]string)

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

return &response, nil
}
12 changes: 6 additions & 6 deletions cmd/psweb/ln/claimjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ create_pset:
if blinder == 0 {
// my output
log.Println(ClaimStatus)
claimPSET, _, err = liquid.ProcessPSET(claimPSET, config.Config.ElementsWallet)
claimPSET, _, err = liquid.ProcessPSET(claimPSET)
if err != nil {
log.Println("Unable to blind output, cancelling ClaimJoin:", err)
EndClaimJoin("", "Coordination failure")
Expand Down Expand Up @@ -246,7 +246,7 @@ create_pset:
if i == 0 {
// my input, last to sign
log.Println(ClaimStatus)
claimPSET, _, err = liquid.ProcessPSET(claimPSET, config.Config.ElementsWallet)
claimPSET, _, err = liquid.ProcessPSET(claimPSET)
if err != nil {
log.Println("Unable to sign input, cancelling ClaimJoin:", err)
EndClaimJoin("", "Initiator signing failure")
Expand Down Expand Up @@ -492,7 +492,7 @@ func Broadcast(fromNodeId string, message *Message) bool {
return false
}

addressInfo, err := liquid.GetAddressInfo(ClaimParties[0].Address, config.Config.ElementsWallet)
addressInfo, err := liquid.GetAddressInfo(ClaimParties[0].Address)
if err != nil {
return false
}
Expand Down Expand Up @@ -718,7 +718,7 @@ func Process(message *Message, senderNodeId string) {

// process my output
newClaimPSET := base64.StdEncoding.EncodeToString(msg.PSET)
newClaimPSET, _, err = liquid.ProcessPSET(newClaimPSET, config.Config.ElementsWallet)
newClaimPSET, _, err = liquid.ProcessPSET(newClaimPSET)
if err != nil {
log.Println("Unable to encode PSET:", err)
return
Expand Down Expand Up @@ -787,7 +787,7 @@ func Process(message *Message, senderNodeId string) {
}

// process my output
claimPSET, _, err = liquid.ProcessPSET(claimPSET, config.Config.ElementsWallet)
claimPSET, _, err = liquid.ProcessPSET(claimPSET)
if err != nil {
log.Println("Unable to process PSET:", err)
return
Expand Down Expand Up @@ -1377,7 +1377,7 @@ func verifyPSET(newClaimPSET string) bool {
}
}

addressInfo, err := liquid.GetAddressInfo(ClaimParties[0].Address, config.Config.ElementsWallet)
addressInfo, err := liquid.GetAddressInfo(ClaimParties[0].Address)
if err != nil {
return false
}
Expand Down
Loading

0 comments on commit b56fcbe

Please sign in to comment.