Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block explorer links #247

Merged
merged 4 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ type netParams struct {
*chaincfg.Params
DcrdRPCServerPort string
WalletRPCServerPort string
BlockExplorerURL string
}

var mainNetParams = netParams{
Params: chaincfg.MainNetParams(),
DcrdRPCServerPort: "9109",
WalletRPCServerPort: "9110",
BlockExplorerURL: "https://dcrdata.decred.org",
}

var testNet3Params = netParams{
Params: chaincfg.TestNet3Params(),
DcrdRPCServerPort: "19109",
WalletRPCServerPort: "19110",
BlockExplorerURL: "https://testnet.dcrdata.org",
}

var simNetParams = netParams{
Params: chaincfg.SimNetParams(),
DcrdRPCServerPort: "19556",
WalletRPCServerPort: "19557",
BlockExplorerURL: "...",
}
1 change: 1 addition & 0 deletions vspd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func run(ctx context.Context) error {
apiCfg := webapi.Config{
VSPFee: cfg.VSPFee,
NetParams: cfg.netParams.Params,
BlockExplorerURL: cfg.netParams.BlockExplorerURL,
SupportEmail: cfg.SupportEmail,
VspClosed: cfg.VspClosed,
AdminPass: cfg.AdminPass,
Expand Down
21 changes: 15 additions & 6 deletions webapi/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package webapi
import (
"net/http"

"github.com/decred/vspd/database"
"github.com/decred/vspd/rpc"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
Expand All @@ -26,6 +27,14 @@ type WalletStatus struct {
BestBlockHeight int64 `json:"bestblockheight"`
}

type searchResult struct {
Hash string
Found bool
Ticket database.Ticket
VoteChanges map[uint32]database.VoteChangeRecord
MaxVoteChanges int
}

func walletStatus(c *gin.Context) map[string]WalletStatus {
walletClients := c.MustGet("WalletClients").([]*rpc.WalletRPC)
failedWalletClients := c.MustGet("FailedWalletClients").([]string)
Expand Down Expand Up @@ -113,12 +122,12 @@ func ticketSearch(c *gin.Context) {
}

c.HTML(http.StatusOK, "admin.html", gin.H{
"SearchResult": gin.H{
"Hash": hash,
"Found": found,
"Ticket": ticket,
"VoteChanges": voteChanges,
"MaxVoteChanges": cfg.MaxVoteChangeRecords,
"SearchResult": searchResult{
Hash: hash,
Found: found,
Ticket: ticket,
VoteChanges: voteChanges,
MaxVoteChanges: cfg.MaxVoteChangeRecords,
},
"VspStats": getVSPStats(),
"WalletStatus": walletStatus(c),
Expand Down
19 changes: 19 additions & 0 deletions webapi/formatting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package webapi

import "time"

func addressURL(blockExplorerURL string) func(string) string {
return func(addr string) string {
return blockExplorerURL + "/address/" + addr
}
}

func txURL(blockExplorerURL string) func(string) string {
return func(txID string) string {
return blockExplorerURL + "/tx/" + txID
}
}

func dateTime(t int64) string {
return time.Unix(t, 0).Format("2 Jan 2006 15:04:05")
}
2 changes: 1 addition & 1 deletion webapi/homepage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func updateVSPStats(db *database.VspDatabase, cfg Config) error {
Revoked: revoked,
VSPFee: cfg.VSPFee,
Network: cfg.NetParams.Name,
UpdateTime: time.Now().Format("Mon Jan _2 15:04:05 2006"),
UpdateTime: dateTime(time.Now().Unix()),
SupportEmail: cfg.SupportEmail,
VspClosed: cfg.VspClosed,
Debug: cfg.Debug,
Expand Down
26 changes: 19 additions & 7 deletions webapi/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ <h1>Search Result</h1>
<table class="table ticket-table mt-2 mb-4">
<tr>
<th>Hash</th>
<td>{{ .Ticket.Hash }}</td>
<td>
<a href="{{ txURL .Ticket.Hash }}">
{{ .Ticket.Hash }}
</a>
</td>
</tr>
<tr>
<th>Commitment Address</th>
Expand All @@ -109,15 +113,19 @@ <h1>Search Result</h1>
</tr>
<tr>
<th>Fee Address</th>
<td>{{ .Ticket.FeeAddress }}</td>
<td>
<a href="{{ addressURL .Ticket.FeeAddress }}">
{{ .Ticket.FeeAddress }}
</a>
</td>
</tr>
<tr>
<th>Fee Amount</th>
<td>{{ .Ticket.FeeAmount }} atoms</td>
</tr>
<tr>
<th>Fee Expiration</th>
<td>{{ .Ticket.FeeExpiration }}</td>
<td>{{ .Ticket.FeeExpiration }} ({{ dateTime .Ticket.FeeExpiration }}) </td>
</tr>
<tr>
<th>Confirmed</th>
Expand Down Expand Up @@ -173,12 +181,16 @@ <h1>Search Result</h1>
<td>{{ .Ticket.VotingWIF }}</td>
</tr>
<tr>
<th>Fee Tx</th>
<td>{{ .Ticket.FeeTxHex }}</td>
<th>Fee Tx Hash</th>
<td>
<a href="{{ txURL .Ticket.FeeTxHash }}">
{{ .Ticket.FeeTxHash }}
</a>
</td>
</tr>
<tr>
<th>Fee Tx Hash</th>
<td>{{ .Ticket.FeeTxHash }}</td>
<th>Fee Tx</th>
<td>{{ .Ticket.FeeTxHex }}</td>
</tr>
<tr>
<th>Fee Tx Status</th>
Expand Down
10 changes: 10 additions & 0 deletions webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"net"
"net/http"
"sync"
Expand All @@ -26,6 +27,7 @@ import (
type Config struct {
VSPFee float64
NetParams *chaincfg.Params
BlockExplorerURL string
FeeAccountName string
SupportEmail string
VspClosed bool
Expand Down Expand Up @@ -173,6 +175,14 @@ func router(debugMode bool, cookieSecret []byte, dcrd rpc.DcrdConnect, wallets r
}

router := gin.New()

// Add custom functions for use in templates.
router.SetFuncMap(template.FuncMap{
"txURL": txURL(cfg.BlockExplorerURL),
"addressURL": addressURL(cfg.BlockExplorerURL),
"dateTime": dateTime,
})

router.LoadHTMLGlob("webapi/templates/*.html")

// Recovery middleware handles any go panics generated while processing web
Expand Down