cosmos-sdk/x/stake/client/rest/query.go

116 lines
3.1 KiB
Go
Raw Normal View History

2018-04-23 08:30:54 -07:00
package rest
import (
"encoding/hex"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/stake"
)
func registerQueryRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", bondingStatusHandlerFn("stake", cdc, ctx)).Methods("GET")
r.HandleFunc("/stake/candidates", candidatesHandlerFn("stake", cdc, ctx)).Methods("GET")
2018-04-23 08:30:54 -07:00
}
// bondingStatusHandlerFn - http request handler to query delegator bonding status
func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc {
2018-04-23 08:30:54 -07:00
return func(w http.ResponseWriter, r *http.Request) {
// read parameters
vars := mux.Vars(r)
delegator := vars["delegator"]
2018-05-14 17:01:51 -07:00
candidate := vars["candidate"]
2018-04-23 08:30:54 -07:00
bz, err := hex.DecodeString(delegator)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
delegatorAddr := sdk.Address(bz)
2018-05-14 17:01:51 -07:00
bz, err = hex.DecodeString(candidate)
2018-04-23 08:30:54 -07:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
2018-05-09 21:01:58 -07:00
validatorAddr := sdk.Address(bz)
2018-04-23 08:30:54 -07:00
2018-05-09 21:01:58 -07:00
key := stake.GetDelegationKey(delegatorAddr, validatorAddr, cdc)
2018-04-23 08:30:54 -07:00
res, err := ctx.Query(key, storeName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't query bond. Error: %s", err.Error())))
return
}
// the query will return empty if there is no data for this bond
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
2018-05-09 18:39:14 -07:00
var bond stake.Delegation
err = cdc.UnmarshalBinary(res, &bond)
2018-04-23 08:30:54 -07:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't decode bond. Error: %s", err.Error())))
return
}
output, err := cdc.MarshalJSON(bond)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
}
// candidatesHandlerFn - http request handler to query list of candidates
func candidatesHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := ctx.QuerySubspace(cdc, stake.CandidatesKey, storeName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't query candidates. Error: %s", err.Error())))
return
}
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
candidates := make(stake.Candidates, 0, len(res))
for _, kv := range res {
var candidate stake.Candidate
err = cdc.UnmarshalBinary(kv.Value, &candidate)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Couldn't decode candidate. Error: %s", err.Error())))
return
}
candidates = append(candidates, candidate)
}
output, err := cdc.MarshalJSON(candidates)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
}