2018-06-21 07:37:46 -06:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-06-26 05:02:53 -06:00
|
|
|
"blockbook/api"
|
2018-06-21 07:37:46 -06:00
|
|
|
"blockbook/bchain"
|
|
|
|
"blockbook/common"
|
|
|
|
"blockbook/db"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-06-26 16:44:23 -06:00
|
|
|
"html/template"
|
2018-10-25 11:40:07 -06:00
|
|
|
"io/ioutil"
|
2018-10-25 10:26:51 -06:00
|
|
|
"math/big"
|
2018-06-21 07:37:46 -06:00
|
|
|
"net/http"
|
2018-08-24 08:17:43 -06:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2018-06-21 07:37:46 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2018-09-05 07:13:45 -06:00
|
|
|
const txsOnPage = 25
|
2018-09-14 04:10:03 -06:00
|
|
|
const blocksOnPage = 50
|
2018-08-21 02:11:27 -06:00
|
|
|
const txsInAPI = 1000
|
2018-06-21 07:37:46 -06:00
|
|
|
|
2018-08-21 02:11:27 -06:00
|
|
|
// PublicServer is a handle to public http server
|
2018-06-21 07:37:46 -06:00
|
|
|
type PublicServer struct {
|
2018-09-20 14:07:42 -06:00
|
|
|
binding string
|
|
|
|
certFiles string
|
|
|
|
socketio *SocketIoServer
|
|
|
|
https *http.Server
|
|
|
|
db *db.RocksDB
|
|
|
|
txCache *db.TxCache
|
|
|
|
chain bchain.BlockChain
|
|
|
|
chainParser bchain.BlockChainParser
|
|
|
|
api *api.Worker
|
|
|
|
explorerURL string
|
|
|
|
internalExplorer bool
|
|
|
|
metrics *common.Metrics
|
|
|
|
is *common.InternalState
|
|
|
|
templates []*template.Template
|
|
|
|
debug bool
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
|
2018-08-21 02:11:27 -06:00
|
|
|
// NewPublicServer creates new public server http interface to blockbook and returns its handle
|
2018-09-20 04:15:46 -06:00
|
|
|
// only basic functionality is mapped, to map all functions, call
|
2018-08-24 08:17:43 -06:00
|
|
|
func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxCache, explorerURL string, metrics *common.Metrics, is *common.InternalState, debugMode bool) (*PublicServer, error) {
|
2018-06-21 07:37:46 -06:00
|
|
|
|
2018-06-27 16:36:56 -06:00
|
|
|
api, err := api.NewWorker(db, chain, txCache, is)
|
2018-06-26 05:02:53 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-21 07:37:46 -06:00
|
|
|
socketio, err := NewSocketIoServer(db, chain, txCache, metrics, is)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, path := splitBinding(binding)
|
|
|
|
serveMux := http.NewServeMux()
|
|
|
|
https := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: serveMux,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &PublicServer{
|
2018-09-20 14:07:42 -06:00
|
|
|
binding: binding,
|
|
|
|
certFiles: certFiles,
|
|
|
|
https: https,
|
|
|
|
api: api,
|
|
|
|
socketio: socketio,
|
|
|
|
db: db,
|
|
|
|
txCache: txCache,
|
|
|
|
chain: chain,
|
|
|
|
chainParser: chain.GetChainParser(),
|
|
|
|
explorerURL: explorerURL,
|
|
|
|
internalExplorer: explorerURL == "",
|
|
|
|
metrics: metrics,
|
|
|
|
is: is,
|
|
|
|
debug: debugMode,
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
2018-09-20 04:15:46 -06:00
|
|
|
s.templates = parseTemplates()
|
2018-06-21 07:37:46 -06:00
|
|
|
|
2018-09-20 04:15:46 -06:00
|
|
|
// map only basic functions, the rest is enabled by method MapFullPublicInterface
|
2018-06-26 16:44:23 -06:00
|
|
|
serveMux.Handle(path+"favicon.ico", http.FileServer(http.Dir("./static/")))
|
2018-09-20 04:15:46 -06:00
|
|
|
serveMux.Handle(path+"static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
|
|
|
// default handler
|
|
|
|
serveMux.HandleFunc(path, s.htmlTemplateHandler(s.explorerIndex))
|
|
|
|
// default API handler
|
|
|
|
serveMux.HandleFunc(path+"api/", s.jsonHandler(s.apiIndex))
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the server
|
|
|
|
func (s *PublicServer) Run() error {
|
|
|
|
if s.certFiles == "" {
|
|
|
|
glog.Info("public server: starting to listen on http://", s.https.Addr)
|
|
|
|
return s.https.ListenAndServe()
|
|
|
|
}
|
|
|
|
glog.Info("public server starting to listen on https://", s.https.Addr)
|
|
|
|
return s.https.ListenAndServeTLS(fmt.Sprint(s.certFiles, ".crt"), fmt.Sprint(s.certFiles, ".key"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectFullPublicInterface enables complete public functionality
|
|
|
|
func (s *PublicServer) ConnectFullPublicInterface() {
|
|
|
|
serveMux := s.https.Handler.(*http.ServeMux)
|
|
|
|
_, path := splitBinding(s.binding)
|
2018-06-21 07:37:46 -06:00
|
|
|
// support for tests of socket.io interface
|
|
|
|
serveMux.Handle(path+"test.html", http.FileServer(http.Dir("./static/")))
|
2018-09-20 14:07:42 -06:00
|
|
|
if s.internalExplorer {
|
|
|
|
// internal explorer handlers
|
|
|
|
serveMux.HandleFunc(path+"tx/", s.htmlTemplateHandler(s.explorerTx))
|
|
|
|
serveMux.HandleFunc(path+"address/", s.htmlTemplateHandler(s.explorerAddress))
|
|
|
|
serveMux.HandleFunc(path+"search/", s.htmlTemplateHandler(s.explorerSearch))
|
|
|
|
serveMux.HandleFunc(path+"blocks", s.htmlTemplateHandler(s.explorerBlocks))
|
|
|
|
serveMux.HandleFunc(path+"block/", s.htmlTemplateHandler(s.explorerBlock))
|
2018-09-27 05:44:13 -06:00
|
|
|
serveMux.HandleFunc(path+"spending/", s.htmlTemplateHandler(s.explorerSpendingTx))
|
2018-10-22 07:41:16 -06:00
|
|
|
serveMux.HandleFunc(path+"sendtx", s.htmlTemplateHandler(s.explorerSendTx))
|
2018-09-20 14:07:42 -06:00
|
|
|
} else {
|
|
|
|
// redirect to wallet requests for tx and address, possibly to external site
|
|
|
|
serveMux.HandleFunc(path+"tx/", s.txRedirect)
|
|
|
|
serveMux.HandleFunc(path+"address/", s.addressRedirect)
|
|
|
|
}
|
2018-06-26 16:44:23 -06:00
|
|
|
// API calls
|
2018-08-24 08:17:43 -06:00
|
|
|
serveMux.HandleFunc(path+"api/block-index/", s.jsonHandler(s.apiBlockIndex))
|
|
|
|
serveMux.HandleFunc(path+"api/tx/", s.jsonHandler(s.apiTx))
|
2018-10-16 04:25:31 -06:00
|
|
|
serveMux.HandleFunc(path+"api/tx-specific/", s.jsonHandler(s.apiTxSpecific))
|
2018-08-24 08:17:43 -06:00
|
|
|
serveMux.HandleFunc(path+"api/address/", s.jsonHandler(s.apiAddress))
|
2018-11-14 06:46:42 -07:00
|
|
|
serveMux.HandleFunc(path+"api/utxo/", s.jsonHandler(s.apiAddressUtxo))
|
2018-09-17 10:28:08 -06:00
|
|
|
serveMux.HandleFunc(path+"api/block/", s.jsonHandler(s.apiBlock))
|
2018-10-25 10:26:51 -06:00
|
|
|
serveMux.HandleFunc(path+"api/sendtx/", s.jsonHandler(s.apiSendTx))
|
|
|
|
serveMux.HandleFunc(path+"api/estimatefee/", s.jsonHandler(s.apiEstimateFee))
|
2018-09-20 04:15:46 -06:00
|
|
|
// socket.io interface
|
|
|
|
serveMux.Handle(path+"socket.io/", s.socketio.GetHandler())
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the server
|
|
|
|
func (s *PublicServer) Close() error {
|
|
|
|
glog.Infof("public server: closing")
|
|
|
|
return s.https.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the server
|
|
|
|
func (s *PublicServer) Shutdown(ctx context.Context) error {
|
|
|
|
glog.Infof("public server: shutdown")
|
|
|
|
return s.https.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2018-09-11 05:37:12 -06:00
|
|
|
// OnNewBlock notifies users subscribed to bitcoind/hashblock about new block
|
|
|
|
func (s *PublicServer) OnNewBlock(hash string, height uint32) {
|
2018-06-21 07:37:46 -06:00
|
|
|
s.socketio.OnNewBlockHash(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnNewTxAddr notifies users subscribed to bitcoind/addresstxid about new block
|
2018-10-03 03:20:23 -06:00
|
|
|
func (s *PublicServer) OnNewTxAddr(txid string, desc bchain.AddressDescriptor, isOutput bool) {
|
|
|
|
s.socketio.OnNewTxAddr(txid, desc, isOutput)
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) txRedirect(w http.ResponseWriter, r *http.Request) {
|
2018-09-20 14:07:42 -06:00
|
|
|
http.Redirect(w, r, joinURL(s.explorerURL, r.URL.Path), 302)
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "tx-redirect"}).Inc()
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PublicServer) addressRedirect(w http.ResponseWriter, r *http.Request) {
|
2018-09-20 14:07:42 -06:00
|
|
|
http.Redirect(w, r, joinURL(s.explorerURL, r.URL.Path), 302)
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "address-redirect"}).Inc()
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
|
2018-06-21 07:37:46 -06:00
|
|
|
func splitBinding(binding string) (addr string, path string) {
|
|
|
|
i := strings.Index(binding, "/")
|
|
|
|
if i >= 0 {
|
|
|
|
return binding[0:i], binding[i:]
|
|
|
|
}
|
|
|
|
return binding, "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinURL(base string, part string) string {
|
|
|
|
if len(base) > 0 {
|
|
|
|
if len(base) > 0 && base[len(base)-1] == '/' && len(part) > 0 && part[0] == '/' {
|
|
|
|
return base + part[1:]
|
|
|
|
}
|
|
|
|
return base + part
|
|
|
|
}
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func getFunctionName(i interface{}) string {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PublicServer) jsonHandler(handler func(r *http.Request) (interface{}, error)) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type jsonError struct {
|
2018-10-25 11:40:07 -06:00
|
|
|
Text string `json:"error"`
|
|
|
|
HTTPStatus int `json:"-"`
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var data interface{}
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
glog.Error(getFunctionName(handler), " recovered from panic: ", e)
|
|
|
|
if s.debug {
|
2018-10-25 11:40:07 -06:00
|
|
|
data = jsonError{fmt.Sprint("Internal server error: recovered from panic ", e), http.StatusInternalServerError}
|
2018-08-24 08:17:43 -06:00
|
|
|
} else {
|
2018-10-25 11:40:07 -06:00
|
|
|
data = jsonError{"Internal server error", http.StatusInternalServerError}
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2018-10-25 11:40:07 -06:00
|
|
|
if e, isError := data.(jsonError); isError {
|
|
|
|
w.WriteHeader(e.HTTPStatus)
|
2018-09-24 06:06:58 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
json.NewEncoder(w).Encode(data)
|
|
|
|
}()
|
|
|
|
data, err = handler(r)
|
|
|
|
if err != nil || data == nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
if apiErr, ok := err.(*api.APIError); ok {
|
2018-10-25 11:40:07 -06:00
|
|
|
if apiErr.Public {
|
|
|
|
data = jsonError{apiErr.Error(), http.StatusBadRequest}
|
|
|
|
} else {
|
|
|
|
data = jsonError{apiErr.Error(), http.StatusInternalServerError}
|
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(getFunctionName(handler), " error: ", err)
|
|
|
|
}
|
|
|
|
if s.debug {
|
2018-10-25 10:26:51 -06:00
|
|
|
if data != nil {
|
2018-10-25 11:40:07 -06:00
|
|
|
data = jsonError{fmt.Sprintf("Internal server error: %v, data %+v", err, data), http.StatusInternalServerError}
|
2018-10-25 10:26:51 -06:00
|
|
|
} else {
|
2018-10-25 11:40:07 -06:00
|
|
|
data = jsonError{fmt.Sprintf("Internal server error: %v", err), http.StatusInternalServerError}
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
} else {
|
2018-10-25 11:40:07 -06:00
|
|
|
data = jsonError{"Internal server error", http.StatusInternalServerError}
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) newTemplateData() *TemplateData {
|
|
|
|
return &TemplateData{
|
2018-09-20 14:07:42 -06:00
|
|
|
CoinName: s.is.Coin,
|
|
|
|
CoinShortcut: s.is.CoinShortcut,
|
2018-10-09 16:23:31 -06:00
|
|
|
CoinLabel: s.is.CoinLabel,
|
2018-09-21 00:51:41 -06:00
|
|
|
InternalExplorer: s.internalExplorer && !s.is.InitialSync,
|
2018-10-11 07:38:45 -06:00
|
|
|
TOSLink: api.Text.TOSLink,
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) newTemplateDataWithError(text string) *TemplateData {
|
2018-09-24 06:06:58 -06:00
|
|
|
td := s.newTemplateData()
|
2018-11-05 03:26:23 -07:00
|
|
|
td.Error = &api.APIError{Text: text}
|
2018-09-24 06:06:58 -06:00
|
|
|
return td
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
2018-09-24 06:06:58 -06:00
|
|
|
|
2018-09-13 16:13:25 -06:00
|
|
|
func (s *PublicServer) htmlTemplateHandler(handler func(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error)) func(w http.ResponseWriter, r *http.Request) {
|
2018-08-24 08:17:43 -06:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var t tpl
|
|
|
|
var data *TemplateData
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
glog.Error(getFunctionName(handler), " recovered from panic: ", e)
|
2018-09-24 06:06:58 -06:00
|
|
|
t = errorInternalTpl
|
2018-08-24 08:17:43 -06:00
|
|
|
if s.debug {
|
|
|
|
data = s.newTemplateDataWithError(fmt.Sprint("Internal server error: recovered from panic ", e))
|
|
|
|
} else {
|
|
|
|
data = s.newTemplateDataWithError("Internal server error")
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 16:13:25 -06:00
|
|
|
// noTpl means the handler completely handled the request
|
|
|
|
if t != noTpl {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2018-09-24 06:06:58 -06:00
|
|
|
// return 500 Internal Server Error with errorInternalTpl
|
|
|
|
if t == errorInternalTpl {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2018-09-13 16:13:25 -06:00
|
|
|
if err := s.templates[t].ExecuteTemplate(w, "base.html", data); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if s.debug {
|
|
|
|
// reload templates on each request
|
|
|
|
// to reflect changes during development
|
|
|
|
s.templates = parseTemplates()
|
|
|
|
}
|
2018-09-13 16:13:25 -06:00
|
|
|
t, data, err = handler(w, r)
|
|
|
|
if err != nil || (data == nil && t != noTpl) {
|
2018-09-24 06:06:58 -06:00
|
|
|
t = errorInternalTpl
|
2018-11-05 03:26:23 -07:00
|
|
|
if apiErr, ok := err.(*api.APIError); ok {
|
2018-08-24 08:17:43 -06:00
|
|
|
data = s.newTemplateData()
|
|
|
|
data.Error = apiErr
|
2018-09-24 06:06:58 -06:00
|
|
|
if apiErr.Public {
|
|
|
|
t = errorTpl
|
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(getFunctionName(handler), " error: ", err)
|
|
|
|
}
|
|
|
|
if s.debug {
|
|
|
|
data = s.newTemplateDataWithError(fmt.Sprintf("Internal server error: %v, data %+v", err, data))
|
|
|
|
} else {
|
|
|
|
data = s.newTemplateDataWithError("Internal server error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tpl int
|
|
|
|
|
|
|
|
const (
|
2018-09-13 16:13:25 -06:00
|
|
|
noTpl = tpl(iota)
|
|
|
|
errorTpl
|
2018-09-24 06:06:58 -06:00
|
|
|
errorInternalTpl
|
2018-09-17 03:17:40 -06:00
|
|
|
indexTpl
|
2018-08-24 08:17:43 -06:00
|
|
|
txTpl
|
|
|
|
addressTpl
|
2018-09-14 04:10:03 -06:00
|
|
|
blocksTpl
|
2018-09-17 10:28:08 -06:00
|
|
|
blockTpl
|
2018-10-22 07:41:16 -06:00
|
|
|
sendTransactionTpl
|
2018-09-05 07:13:45 -06:00
|
|
|
|
|
|
|
tplCount
|
2018-08-24 08:17:43 -06:00
|
|
|
)
|
|
|
|
|
2018-10-22 07:41:16 -06:00
|
|
|
// TemplateData is used to transfer data to the templates
|
2018-06-29 18:02:16 -06:00
|
|
|
type TemplateData struct {
|
2018-09-20 14:07:42 -06:00
|
|
|
CoinName string
|
|
|
|
CoinShortcut string
|
2018-10-09 16:23:31 -06:00
|
|
|
CoinLabel string
|
2018-09-20 14:07:42 -06:00
|
|
|
InternalExplorer bool
|
|
|
|
Address *api.Address
|
|
|
|
AddrStr string
|
|
|
|
Tx *api.Tx
|
2018-10-16 04:25:31 -06:00
|
|
|
TxSpecific json.RawMessage
|
2018-11-05 03:26:23 -07:00
|
|
|
Error *api.APIError
|
2018-09-20 14:07:42 -06:00
|
|
|
Blocks *api.Blocks
|
|
|
|
Block *api.Block
|
|
|
|
Info *api.SystemInfo
|
|
|
|
Page int
|
|
|
|
PrevPage int
|
|
|
|
NextPage int
|
|
|
|
PagingRange []int
|
2018-10-11 07:38:45 -06:00
|
|
|
TOSLink string
|
2018-10-22 07:41:16 -06:00
|
|
|
SendTxHex string
|
|
|
|
Status string
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseTemplates() []*template.Template {
|
|
|
|
templateFuncMap := template.FuncMap{
|
2018-09-17 03:17:40 -06:00
|
|
|
"formatTime": formatTime,
|
2018-08-24 08:17:43 -06:00
|
|
|
"formatUnixTime": formatUnixTime,
|
|
|
|
"formatAmount": formatAmount,
|
|
|
|
"setTxToTemplateData": setTxToTemplateData,
|
|
|
|
"stringInSlice": stringInSlice,
|
|
|
|
}
|
2018-09-05 07:13:45 -06:00
|
|
|
t := make([]*template.Template, tplCount)
|
|
|
|
t[errorTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
2018-09-24 06:06:58 -06:00
|
|
|
t[errorInternalTpl] = template.Must(template.New("error").Funcs(templateFuncMap).ParseFiles("./static/templates/error.html", "./static/templates/base.html"))
|
2018-09-17 03:17:40 -06:00
|
|
|
t[indexTpl] = template.Must(template.New("index").Funcs(templateFuncMap).ParseFiles("./static/templates/index.html", "./static/templates/base.html"))
|
2018-08-24 08:17:43 -06:00
|
|
|
t[txTpl] = template.Must(template.New("tx").Funcs(templateFuncMap).ParseFiles("./static/templates/tx.html", "./static/templates/txdetail.html", "./static/templates/base.html"))
|
2018-09-05 07:13:45 -06:00
|
|
|
t[addressTpl] = template.Must(template.New("address").Funcs(templateFuncMap).ParseFiles("./static/templates/address.html", "./static/templates/txdetail.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
2018-09-14 04:10:03 -06:00
|
|
|
t[blocksTpl] = template.Must(template.New("blocks").Funcs(templateFuncMap).ParseFiles("./static/templates/blocks.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
2018-09-17 10:28:08 -06:00
|
|
|
t[blockTpl] = template.Must(template.New("block").Funcs(templateFuncMap).ParseFiles("./static/templates/block.html", "./static/templates/txdetail.html", "./static/templates/paging.html", "./static/templates/base.html"))
|
2018-10-22 07:41:16 -06:00
|
|
|
t[sendTransactionTpl] = template.Must(template.New("block").Funcs(templateFuncMap).ParseFiles("./static/templates/sendtx.html", "./static/templates/base.html"))
|
2018-08-24 08:17:43 -06:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatUnixTime(ut int64) string {
|
2018-09-17 03:17:40 -06:00
|
|
|
return formatTime(time.Unix(ut, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatTime(t time.Time) string {
|
|
|
|
return t.Format(time.RFC1123)
|
2018-08-24 08:17:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// for now return the string as it is
|
|
|
|
// in future could be used to do coin specific formatting
|
|
|
|
func formatAmount(a string) string {
|
|
|
|
return a
|
2018-06-29 18:02:16 -06:00
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
// called from template to support txdetail.html functionality
|
2018-06-29 18:02:16 -06:00
|
|
|
func setTxToTemplateData(td *TemplateData, tx *api.Tx) *TemplateData {
|
|
|
|
td.Tx = tx
|
|
|
|
return td
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:13:25 -06:00
|
|
|
func (s *PublicServer) explorerTx(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
2018-06-26 05:02:53 -06:00
|
|
|
var tx *api.Tx
|
2018-10-16 04:25:31 -06:00
|
|
|
var txSpecific json.RawMessage
|
2018-10-08 06:55:21 -06:00
|
|
|
var err error
|
2018-09-13 16:18:13 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "tx"}).Inc()
|
2018-06-26 05:02:53 -06:00
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
txid := r.URL.Path[i+1:]
|
2018-10-08 06:55:21 -06:00
|
|
|
tx, err = s.api.GetTransaction(txid, false)
|
2018-06-28 13:18:52 -06:00
|
|
|
if err != nil {
|
2018-08-24 08:17:43 -06:00
|
|
|
return errorTpl, nil, err
|
2018-06-26 05:02:53 -06:00
|
|
|
}
|
2018-10-16 04:25:31 -06:00
|
|
|
txSpecific, err = s.chain.GetTransactionSpecific(txid)
|
|
|
|
if err != nil {
|
|
|
|
return errorTpl, nil, err
|
|
|
|
}
|
2018-06-26 16:44:23 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
data := s.newTemplateData()
|
2018-09-13 16:13:25 -06:00
|
|
|
data.Tx = tx
|
2018-10-16 04:25:31 -06:00
|
|
|
data.TxSpecific = txSpecific
|
2018-09-13 16:13:25 -06:00
|
|
|
return txTpl, data, nil
|
2018-06-26 05:02:53 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 05:44:13 -06:00
|
|
|
func (s *PublicServer) explorerSpendingTx(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "spendingtx"}).Inc()
|
|
|
|
var err error
|
|
|
|
parts := strings.Split(r.URL.Path, "/")
|
|
|
|
if len(parts) > 2 {
|
|
|
|
tx := parts[len(parts)-2]
|
|
|
|
n, ec := strconv.Atoi(parts[len(parts)-1])
|
|
|
|
if ec == nil {
|
|
|
|
spendingTx, err := s.api.GetSpendingTxid(tx, n)
|
|
|
|
if err == nil && spendingTx != "" {
|
|
|
|
http.Redirect(w, r, joinURL("/tx/", spendingTx), 302)
|
|
|
|
return noTpl, nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
err = api.NewAPIError("Transaction not found", true)
|
2018-09-27 05:44:13 -06:00
|
|
|
}
|
|
|
|
return errorTpl, nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:13:25 -06:00
|
|
|
func (s *PublicServer) explorerAddress(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
2018-06-29 18:02:16 -06:00
|
|
|
var address *api.Address
|
|
|
|
var err error
|
2018-09-13 16:18:13 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "address"}).Inc()
|
2018-06-29 18:02:16 -06:00
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
2018-07-20 11:07:43 -06:00
|
|
|
page, ec := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if ec != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
2018-08-28 16:25:26 -06:00
|
|
|
address, err = s.api.GetAddress(r.URL.Path[i+1:], page, txsOnPage, false)
|
2018-06-29 18:02:16 -06:00
|
|
|
if err != nil {
|
2018-08-24 08:17:43 -06:00
|
|
|
return errorTpl, nil, err
|
2018-06-29 18:02:16 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 16:13:25 -06:00
|
|
|
data := s.newTemplateData()
|
|
|
|
data.AddrStr = address.AddrStr
|
|
|
|
data.Address = address
|
|
|
|
data.Page = address.Page
|
|
|
|
data.PagingRange, data.PrevPage, data.NextPage = getPagingRange(address.Page, address.TotalPages)
|
|
|
|
return addressTpl, data, nil
|
2018-09-13 06:25:24 -06:00
|
|
|
}
|
|
|
|
|
2018-09-14 04:10:03 -06:00
|
|
|
func (s *PublicServer) explorerBlocks(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
|
|
|
var blocks *api.Blocks
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "blocks"}).Inc()
|
|
|
|
page, ec := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if ec != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
blocks, err = s.api.GetBlocks(page, blocksOnPage)
|
|
|
|
if err != nil {
|
|
|
|
return errorTpl, nil, err
|
|
|
|
}
|
|
|
|
data := s.newTemplateData()
|
|
|
|
data.Blocks = blocks
|
|
|
|
data.Page = blocks.Page
|
|
|
|
data.PagingRange, data.PrevPage, data.NextPage = getPagingRange(blocks.Page, blocks.TotalPages)
|
|
|
|
return blocksTpl, data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-17 10:28:08 -06:00
|
|
|
func (s *PublicServer) explorerBlock(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
|
|
|
var block *api.Block
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "block"}).Inc()
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
page, ec := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if ec != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
block, err = s.api.GetBlock(r.URL.Path[i+1:], page, txsOnPage)
|
|
|
|
if err != nil {
|
|
|
|
return errorTpl, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := s.newTemplateData()
|
|
|
|
data.Block = block
|
|
|
|
data.Page = block.Page
|
|
|
|
data.PagingRange, data.PrevPage, data.NextPage = getPagingRange(block.Page, block.TotalPages)
|
|
|
|
return blockTpl, data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:17:40 -06:00
|
|
|
func (s *PublicServer) explorerIndex(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
|
|
|
var si *api.SystemInfo
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "index"}).Inc()
|
2018-09-19 07:59:49 -06:00
|
|
|
si, err = s.api.GetSystemInfo(false)
|
2018-09-17 03:17:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return errorTpl, nil, err
|
|
|
|
}
|
|
|
|
data := s.newTemplateData()
|
|
|
|
data.Info = si
|
|
|
|
return indexTpl, data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:13:25 -06:00
|
|
|
func (s *PublicServer) explorerSearch(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
2018-09-13 06:25:24 -06:00
|
|
|
q := strings.TrimSpace(r.URL.Query().Get("q"))
|
|
|
|
var tx *api.Tx
|
|
|
|
var address *api.Address
|
2018-09-17 10:28:08 -06:00
|
|
|
var block *api.Block
|
2018-09-13 06:25:24 -06:00
|
|
|
var err error
|
2018-09-13 16:18:13 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "search"}).Inc()
|
2018-09-13 16:13:25 -06:00
|
|
|
if len(q) > 0 {
|
2018-09-17 10:28:08 -06:00
|
|
|
block, err = s.api.GetBlock(q, 0, 1)
|
|
|
|
if err == nil {
|
2018-09-20 14:07:42 -06:00
|
|
|
http.Redirect(w, r, joinURL("/block/", block.Hash), 302)
|
2018-09-17 10:28:08 -06:00
|
|
|
return noTpl, nil, nil
|
|
|
|
}
|
2018-10-08 06:55:21 -06:00
|
|
|
tx, err = s.api.GetTransaction(q, false)
|
2018-09-17 10:28:08 -06:00
|
|
|
if err == nil {
|
2018-10-08 06:55:21 -06:00
|
|
|
http.Redirect(w, r, joinURL("/tx/", tx.Txid), 302)
|
|
|
|
return noTpl, nil, nil
|
2018-09-13 06:25:24 -06:00
|
|
|
}
|
2018-09-17 10:28:08 -06:00
|
|
|
address, err = s.api.GetAddress(q, 0, 1, true)
|
|
|
|
if err == nil {
|
2018-09-20 14:07:42 -06:00
|
|
|
http.Redirect(w, r, joinURL("/address/", address.AddrStr), 302)
|
2018-09-17 10:28:08 -06:00
|
|
|
return noTpl, nil, nil
|
|
|
|
}
|
2018-09-13 06:25:24 -06:00
|
|
|
}
|
2018-11-05 03:26:23 -07:00
|
|
|
return errorTpl, nil, api.NewAPIError(fmt.Sprintf("No matching records found for '%v'", q), true)
|
2018-06-29 18:02:16 -06:00
|
|
|
}
|
|
|
|
|
2018-10-22 07:41:16 -06:00
|
|
|
func (s *PublicServer) explorerSendTx(w http.ResponseWriter, r *http.Request) (tpl, *TemplateData, error) {
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "sendtx"}).Inc()
|
|
|
|
data := s.newTemplateData()
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return sendTransactionTpl, data, err
|
|
|
|
}
|
|
|
|
hex := r.FormValue("hex")
|
|
|
|
if len(hex) > 0 {
|
|
|
|
res, err := s.chain.SendRawTransaction(hex)
|
|
|
|
if err != nil {
|
|
|
|
data.SendTxHex = hex
|
2018-11-05 03:26:23 -07:00
|
|
|
data.Error = &api.APIError{Text: err.Error(), Public: true}
|
2018-10-22 07:41:16 -06:00
|
|
|
return sendTransactionTpl, data, nil
|
|
|
|
}
|
2018-10-25 10:26:51 -06:00
|
|
|
data.Status = "Transaction sent, result " + res
|
2018-10-22 07:41:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sendTransactionTpl, data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 07:13:45 -06:00
|
|
|
func getPagingRange(page int, total int) ([]int, int, int) {
|
|
|
|
if total < 2 {
|
|
|
|
return nil, 0, 0
|
|
|
|
}
|
|
|
|
pp, np := page-1, page+1
|
|
|
|
if np > total {
|
|
|
|
np = total
|
|
|
|
}
|
|
|
|
if pp < 1 {
|
|
|
|
pp = 1
|
|
|
|
}
|
|
|
|
r := make([]int, 0, 8)
|
|
|
|
if total < 6 {
|
|
|
|
for i := 1; i <= total; i++ {
|
|
|
|
r = append(r, i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r = append(r, 1)
|
|
|
|
if page > 3 {
|
|
|
|
r = append(r, 0)
|
|
|
|
}
|
|
|
|
if pp == 1 {
|
|
|
|
if page == 1 {
|
|
|
|
r = append(r, np)
|
|
|
|
r = append(r, np+1)
|
|
|
|
r = append(r, np+2)
|
|
|
|
} else {
|
|
|
|
r = append(r, page)
|
|
|
|
r = append(r, np)
|
|
|
|
r = append(r, np+1)
|
|
|
|
}
|
|
|
|
} else if np == total {
|
|
|
|
if page == total {
|
|
|
|
r = append(r, pp-2)
|
|
|
|
r = append(r, pp-1)
|
|
|
|
r = append(r, pp)
|
|
|
|
} else {
|
|
|
|
r = append(r, pp-1)
|
|
|
|
r = append(r, pp)
|
|
|
|
r = append(r, page)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r = append(r, pp)
|
|
|
|
r = append(r, page)
|
|
|
|
r = append(r, np)
|
|
|
|
}
|
|
|
|
if page <= total-3 {
|
|
|
|
r = append(r, 0)
|
|
|
|
}
|
|
|
|
r = append(r, total)
|
|
|
|
}
|
|
|
|
return r, pp, np
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:17:40 -06:00
|
|
|
func (s *PublicServer) apiIndex(r *http.Request) (interface{}, error) {
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-index"}).Inc()
|
2018-09-19 07:59:49 -06:00
|
|
|
return s.api.GetSystemInfo(false)
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) apiBlockIndex(r *http.Request) (interface{}, error) {
|
2018-06-21 07:37:46 -06:00
|
|
|
type resBlockIndex struct {
|
|
|
|
BlockHash string `json:"blockHash"`
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var hash string
|
|
|
|
height := -1
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
if h, err := strconv.Atoi(r.URL.Path[i+1:]); err == nil {
|
|
|
|
height = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if height >= 0 {
|
|
|
|
hash, err = s.db.GetBlockHash(uint32(height))
|
|
|
|
} else {
|
|
|
|
_, hash, err = s.db.GetBestBlock()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
2018-08-24 08:17:43 -06:00
|
|
|
return nil, err
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
return resBlockIndex{
|
|
|
|
BlockHash: hash,
|
|
|
|
}, nil
|
2018-06-21 07:37:46 -06:00
|
|
|
}
|
2018-06-26 16:44:23 -06:00
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) apiTx(r *http.Request) (interface{}, error) {
|
2018-06-26 16:44:23 -06:00
|
|
|
var tx *api.Tx
|
|
|
|
var err error
|
2018-09-13 16:18:13 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-tx"}).Inc()
|
2018-06-26 16:44:23 -06:00
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
txid := r.URL.Path[i+1:]
|
2018-10-25 10:32:03 -06:00
|
|
|
spendingTxs := false
|
|
|
|
p := r.URL.Query().Get("spending")
|
|
|
|
if len(p) > 0 {
|
|
|
|
spendingTxs, err = strconv.ParseBool(p)
|
|
|
|
if err != nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Parameter 'spending' cannot be converted to boolean", true)
|
2018-10-25 10:32:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tx, err = s.api.GetTransaction(txid, spendingTxs)
|
2018-06-26 16:44:23 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
return tx, err
|
2018-06-26 16:44:23 -06:00
|
|
|
}
|
2018-06-29 18:02:16 -06:00
|
|
|
|
2018-10-16 04:25:31 -06:00
|
|
|
func (s *PublicServer) apiTxSpecific(r *http.Request) (interface{}, error) {
|
|
|
|
var tx json.RawMessage
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-tx-specific"}).Inc()
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
txid := r.URL.Path[i+1:]
|
|
|
|
tx, err = s.chain.GetTransactionSpecific(txid)
|
|
|
|
}
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:17:43 -06:00
|
|
|
func (s *PublicServer) apiAddress(r *http.Request) (interface{}, error) {
|
2018-06-29 18:02:16 -06:00
|
|
|
var address *api.Address
|
|
|
|
var err error
|
2018-09-13 16:18:13 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-address"}).Inc()
|
2018-06-29 18:02:16 -06:00
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
2018-07-20 11:07:43 -06:00
|
|
|
page, ec := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if ec != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
2018-08-28 16:25:26 -06:00
|
|
|
address, err = s.api.GetAddress(r.URL.Path[i+1:], page, txsInAPI, true)
|
2018-06-29 18:02:16 -06:00
|
|
|
}
|
2018-08-24 08:17:43 -06:00
|
|
|
return address, err
|
2018-06-29 18:02:16 -06:00
|
|
|
}
|
2018-09-17 10:28:08 -06:00
|
|
|
|
2018-11-14 06:46:42 -07:00
|
|
|
func (s *PublicServer) apiAddressUtxo(r *http.Request) (interface{}, error) {
|
|
|
|
var utxo []api.AddressUtxo
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-address"}).Inc()
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
utxo, err = s.api.GetAddressUtxo(r.URL.Path[i+1:])
|
|
|
|
}
|
|
|
|
return utxo, err
|
|
|
|
}
|
|
|
|
|
2018-09-17 10:28:08 -06:00
|
|
|
func (s *PublicServer) apiBlock(r *http.Request) (interface{}, error) {
|
|
|
|
var block *api.Block
|
|
|
|
var err error
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-block"}).Inc()
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
page, ec := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if ec != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
block, err = s.api.GetBlock(r.URL.Path[i+1:], page, txsInAPI)
|
|
|
|
}
|
|
|
|
return block, err
|
|
|
|
}
|
2018-10-25 10:26:51 -06:00
|
|
|
|
|
|
|
type resultSendTransaction struct {
|
|
|
|
Result string `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PublicServer) apiSendTx(r *http.Request) (interface{}, error) {
|
|
|
|
var err error
|
|
|
|
var res resultSendTransaction
|
2018-10-25 11:40:07 -06:00
|
|
|
var hex string
|
2018-10-25 10:26:51 -06:00
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-sendtx"}).Inc()
|
2018-10-25 11:40:07 -06:00
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Missing tx blob", true)
|
2018-10-25 11:40:07 -06:00
|
|
|
}
|
|
|
|
hex = string(data)
|
|
|
|
} else {
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
hex = r.URL.Path[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(hex) > 0 {
|
|
|
|
res.Result, err = s.chain.SendRawTransaction(hex)
|
|
|
|
if err != nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError(err.Error(), true)
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
2018-10-25 11:40:07 -06:00
|
|
|
return res, nil
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Missing tx blob", true)
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type resultEstimateFeeAsString struct {
|
|
|
|
Result string `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PublicServer) apiEstimateFee(r *http.Request) (interface{}, error) {
|
|
|
|
var res resultEstimateFeeAsString
|
|
|
|
s.metrics.ExplorerViews.With(common.Labels{"action": "api-estimatefee"}).Inc()
|
|
|
|
if i := strings.LastIndexByte(r.URL.Path, '/'); i > 0 {
|
|
|
|
b := r.URL.Path[i+1:]
|
|
|
|
if len(b) > 0 {
|
|
|
|
blocks, err := strconv.Atoi(b)
|
|
|
|
if err != nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Parameter 'number of blocks' is not a number", true)
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
|
|
|
conservative := true
|
|
|
|
c := r.URL.Query().Get("conservative")
|
|
|
|
if len(c) > 0 {
|
|
|
|
conservative, err = strconv.ParseBool(c)
|
|
|
|
if err != nil {
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Parameter 'conservative' cannot be converted to boolean", true)
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var fee big.Int
|
|
|
|
fee, err = s.chain.EstimateSmartFee(blocks, conservative)
|
|
|
|
if err != nil {
|
|
|
|
fee, err = s.chain.EstimateFee(blocks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.Result = s.chainParser.AmountToDecimalString(&fee)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 03:26:23 -07:00
|
|
|
return nil, api.NewAPIError("Missing parameter 'number of blocks'", true)
|
2018-10-25 10:26:51 -06:00
|
|
|
}
|