Skip to content

Commit 7c908b8

Browse files
authored
Merge pull request #104 from warioishere/codex/remove-global-premium-from-peer-list
remove global premium from peer list and create dedicated page for it
2 parents a7435b7 + 97b8c18 commit 7c908b8

5 files changed

Lines changed: 143 additions & 26 deletions

File tree

cmd/psweb/handlers.go

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ type Premium struct {
213213
PremiumRatePpm int64
214214
}
215215

216+
type PremiumCard struct {
217+
Premium
218+
Title string
219+
Description string
220+
FormID string
221+
}
222+
216223
func peerHandler(w http.ResponseWriter, r *http.Request) {
217224
keys, ok := r.URL.Query()["id"]
218225
if !ok || len(keys[0]) < 1 {
@@ -325,7 +332,7 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
325332

326333
btcBalance := ln.ConfirmedWalletBalance(cl)
327334

328-
var globalPremium, peerPremium []Premium
335+
var peerPremium []Premium
329336

330337
psPeer := true
331338
if peer == nil {
@@ -353,13 +360,6 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
353360

354361
for _, asset := range []peerswaprpc.AssetType{peerswaprpc.AssetType_BTC, peerswaprpc.AssetType_LBTC} {
355362
for _, operation := range []peerswaprpc.OperationType{peerswaprpc.OperationType_SWAP_IN, peerswaprpc.OperationType_SWAP_OUT} {
356-
globalRate, _ := ps.GetGlobalPremiumRate(client, asset, operation)
357-
globalPremium = append(globalPremium, Premium{
358-
Asset: int32(asset.Number()),
359-
Operation: int32(operation.Number()),
360-
PremiumRatePpm: globalRate.PremiumRatePpm,
361-
})
362-
363363
peerRate, _ := ps.GetPremiumRate(client, peer.NodeId, asset, operation)
364364
peerPremium = append(peerPremium, Premium{
365365
Asset: int32(asset.Number()),
@@ -640,7 +640,6 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
640640
OPENING_TX_SIZE_BTC int64
641641
OPENING_TX_SIZE_LBTC int64
642642
OPENING_TX_SIZE_LBTC_DISCOUNTED int64
643-
GlobalPremium []Premium
644643
PeerPremium []Premium
645644
}
646645

@@ -702,14 +701,108 @@ func peerHandler(w http.ResponseWriter, r *http.Request) {
702701
OPENING_TX_SIZE_BTC: OPENING_TX_SIZE_BTC,
703702
OPENING_TX_SIZE_LBTC: OPENING_TX_SIZE_LBTC,
704703
OPENING_TX_SIZE_LBTC_DISCOUNTED: OPENING_TX_SIZE_LBTC_DISCOUNTED,
705-
GlobalPremium: globalPremium,
706704
PeerPremium: peerPremium,
707705
}
708706

709707
// executing template named "peer"
710708
executeTemplate(w, "peer", data)
711709
}
712710

711+
func globalPremiumsHandler(w http.ResponseWriter, r *http.Request) {
712+
errorMessage := ""
713+
keys, ok := r.URL.Query()["err"]
714+
if ok && len(keys[0]) > 0 {
715+
errorMessage = keys[0]
716+
}
717+
718+
popupMessage := ""
719+
keys, ok = r.URL.Query()["msg"]
720+
if ok && len(keys[0]) > 0 {
721+
popupMessage = keys[0]
722+
}
723+
724+
client, cleanup, err := ps.GetClient(config.Config.RpcHost)
725+
if err != nil {
726+
redirectWithError(w, r, "/config?", err)
727+
return
728+
}
729+
defer cleanup()
730+
731+
combos := []struct {
732+
asset peerswaprpc.AssetType
733+
operation peerswaprpc.OperationType
734+
title string
735+
description string
736+
}{
737+
{
738+
asset: peerswaprpc.AssetType_BTC,
739+
operation: peerswaprpc.OperationType_SWAP_IN,
740+
title: "Bitcoin → Lightning",
741+
description: "Premium applied when swapping on-chain bitcoin into Lightning.",
742+
},
743+
{
744+
asset: peerswaprpc.AssetType_BTC,
745+
operation: peerswaprpc.OperationType_SWAP_OUT,
746+
title: "Lightning → Bitcoin",
747+
description: "Premium applied when sending bitcoin on-chain using Lightning liquidity.",
748+
},
749+
{
750+
asset: peerswaprpc.AssetType_LBTC,
751+
operation: peerswaprpc.OperationType_SWAP_IN,
752+
title: "Liquid → Lightning",
753+
description: "Premium applied when converting Liquid bitcoin into Lightning.",
754+
},
755+
{
756+
asset: peerswaprpc.AssetType_LBTC,
757+
operation: peerswaprpc.OperationType_SWAP_OUT,
758+
title: "Lightning → Liquid",
759+
description: "Premium applied when sending swaps out to Liquid bitcoin.",
760+
},
761+
}
762+
763+
premiums := make([]PremiumCard, 0, len(combos))
764+
for i, combo := range combos {
765+
rate, err := ps.GetGlobalPremiumRate(client, combo.asset, combo.operation)
766+
ppm := int64(0)
767+
if err != nil {
768+
log.Printf("GetGlobalPremiumRate(%s,%s): %v", combo.asset.String(), combo.operation.String(), err)
769+
} else if rate != nil {
770+
ppm = rate.PremiumRatePpm
771+
}
772+
773+
premiums = append(premiums, PremiumCard{
774+
Premium: Premium{
775+
Asset: int32(combo.asset.Number()),
776+
Operation: int32(combo.operation.Number()),
777+
PremiumRatePpm: ppm,
778+
},
779+
Title: combo.title,
780+
Description: combo.description,
781+
FormID: fmt.Sprintf("premium_%d", i),
782+
})
783+
}
784+
785+
type Page struct {
786+
Authenticated bool
787+
ErrorMessage string
788+
PopUpMessage string
789+
ColorScheme string
790+
MempoolFeeRate float64
791+
Premiums []PremiumCard
792+
}
793+
794+
data := Page{
795+
Authenticated: config.Config.SecureConnection && config.Config.Password != "",
796+
ErrorMessage: errorMessage,
797+
PopUpMessage: popupMessage,
798+
ColorScheme: config.Config.ColorScheme,
799+
MempoolFeeRate: mempoolFeeRate,
800+
Premiums: premiums,
801+
}
802+
803+
executeTemplate(w, "premiums", data)
804+
}
805+
713806
func bitcoinHandler(w http.ResponseWriter, r *http.Request) {
714807
//check for error message to display
715808
errorMessage := ""

cmd/psweb/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func start() {
167167
r.HandleFunc("/pegin", peginHandler)
168168
r.HandleFunc("/bumpfee", bumpfeeHandler)
169169
r.HandleFunc("/ca", caHandler)
170+
r.HandleFunc("/premiums", globalPremiumsHandler)
170171
r.HandleFunc("/login", loginHandler)
171172
r.HandleFunc("/logout", logoutHandler)
172173
r.HandleFunc("/downloadca", downloadCaHandler)

cmd/psweb/templates/peer.gohtml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -537,22 +537,6 @@ Sincerely,
537537
<th style="text-align: center">🌊⇨⚡</th>
538538
<th style="text-align: center">⚡⇨🌊</th>
539539
</tr>
540-
<tr style="border: 1px dotted">
541-
<td style="padding: 0px; text-align: center">Global</td>
542-
{{range $i, $rate := .GlobalPremium}}
543-
<td style="padding: 0px; text-align: center">
544-
<form id="{{$i}}_default_premium" autocomplete="off" action="/submit" method="post">
545-
<input autocomplete="false" name="hidden" type="text" style="display:none;">
546-
<input type="hidden" name="action" value="setPremium">
547-
<input type="hidden" name="peerNodeId" value="">
548-
<input type="hidden" name="asset" value="{{ $rate.Asset }}">
549-
<input type="hidden" name="operation" value="{{ $rate.Operation }}">
550-
<input type="hidden" name="nextPage" value="/peer?id={{$.Peer.NodeId}}&">
551-
<input class="transparent-input" {{if eq $.ColorScheme "light"}}style="color: black"{{end}} type="number" name="premium" value="{{ $rate.PremiumRatePpm }}" onchange="submitForm('{{$i}}_default_premium')">
552-
</form>
553-
</td>
554-
{{end}}
555-
</tr>
556540
<tr style="border: 1px dotted">
557541
<td style="padding: 0px; text-align: center">For Peer</td>
558542
{{range $i, $rate := .PeerPremium}}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{{define "premiums"}}
2+
{{template "header" .}}
3+
<div class="container">
4+
<div class="columns is-centered">
5+
<div class="column is-three-quarters">
6+
<div class="box has-text-centered">
7+
<h3 class="title is-3">Global Premiums</h3>
8+
<p class="subtitle is-6">Adjust the default swap premiums that apply to every peer. Values are expressed in parts-per-million (PPM).</p>
9+
</div>
10+
</div>
11+
</div>
12+
<div class="columns is-multiline">
13+
{{range .Premiums}}
14+
<div class="column is-half">
15+
<div class="box has-text-centered">
16+
<p class="title is-4">{{.Title}}</p>
17+
<p class="subtitle is-6" style="min-height: 3em;">{{.Description}}</p>
18+
<form id="{{.FormID}}" action="/submit" method="post" autocomplete="off" class="field has-addons has-addons-centered" style="justify-content: center;">
19+
<input autocomplete="false" name="hidden" type="text" style="display:none;">
20+
<input type="hidden" name="action" value="setPremium">
21+
<input type="hidden" name="peerNodeId" value="">
22+
<input type="hidden" name="asset" value="{{.Asset}}">
23+
<input type="hidden" name="operation" value="{{.Operation}}">
24+
<input type="hidden" name="nextPage" value="/premiums?">
25+
<div class="control">
26+
<input class="input is-medium has-text-centered" type="number" name="premium" value="{{.PremiumRatePpm}}" onchange="this.form.submit()" aria-label="Premium in parts-per-million">
27+
</div>
28+
<div class="control">
29+
<a class="button is-medium is-static has-text-black">PPM</a>
30+
</div>
31+
</form>
32+
</div>
33+
</div>
34+
{{end}}
35+
</div>
36+
</div>
37+
{{template "footer" .}}
38+
{{end}}

cmd/psweb/templates/reusable.gohtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<a href="/bitcoin" class="dropdown-item"> Bitcoin Wallet </a>
3535
<a href="/liquid" class="dropdown-item"> Liquid Wallet </a>
3636
<a href="/af" class="dropdown-item"> Channel Fees </a>
37+
<a href="/premiums" class="dropdown-item"> Global Premiums </a>
3738
<hr class="dropdown-divider" />
3839
<a href="/config" class="dropdown-item"> Configuration </a>
3940
<a href="/log?log=psweb.log" class="dropdown-item"> Logs </a>

0 commit comments

Comments
 (0)