vspd/webapi/formatting.go
Jamie Holdstock 9867f78385
Add network proportion to homepage and /vspinfo (and revoked proportion) (#264)
* Add network proportion to homepage and /vspinfo.

Proportion is calculated using the number of tickets  currently registered with the VSP, divided by the total size of the network ticket pool as reported by `getblockheader`.

The value will only ever be an estimate because:

- it's possible for a single ticket to be added to multiple VSPs.
- vspd does not distinguish between immature and live tickets, whereas `getblockheader` only reports live tickets.
- `getblockheader` is reporting the size of the ticket pool as of the previous block, not the current block.

* xaur suggestions

* Show missed ticket %, not just the raw number.
2021-06-08 08:47:07 -05:00

60 lines
1.3 KiB
Go

package webapi
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"strings"
"time"
"github.com/decred/dcrd/dcrutil/v4"
)
func addressURL(blockExplorerURL string) func(string) string {
return func(addr string) string {
return fmt.Sprintf("%s/address/%s", blockExplorerURL, addr)
}
}
func txURL(blockExplorerURL string) func(string) string {
return func(txID string) string {
return fmt.Sprintf("%s/tx/%s", blockExplorerURL, txID)
}
}
func blockURL(blockExplorerURL string) func(int64) string {
return func(height int64) string {
return fmt.Sprintf("%s/block/%d", blockExplorerURL, height)
}
}
func dateTime(t int64) string {
return time.Unix(t, 0).Format("2 Jan 2006 15:04:05 MST")
}
func stripWss(input string) string {
input = strings.ReplaceAll(input, "wss://", "")
input = strings.ReplaceAll(input, "/ws", "")
return input
}
func indentJSON(input string) template.HTML {
var indented bytes.Buffer
err := json.Indent(&indented, []byte(input), "<br/>", "&nbsp;&nbsp;&nbsp;&nbsp;")
if err != nil {
log.Errorf("Failed to indent JSON: %w", err)
return template.HTML(input)
}
return template.HTML(indented.String())
}
func atomsToDCR(atoms int64) string {
return dcrutil.Amount(atoms).String()
}
func float32ToPercent(input float32) string {
return fmt.Sprintf("%.2f%%", input*100)
}