REST folder restructure & cleanup
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// nolint
|
||||
const (
|
||||
FlagAddressDelegator = "address-delegator"
|
||||
FlagAddressCandidate = "address-candidate"
|
||||
FlagPubKey = "pubkey"
|
||||
FlagAmount = "amount"
|
||||
FlagShares = "shares"
|
||||
|
||||
FlagMoniker = "moniker"
|
||||
FlagIdentity = "keybase-sig"
|
||||
FlagWebsite = "website"
|
||||
FlagDetails = "details"
|
||||
)
|
||||
|
||||
// common flagsets to add to various functions
|
||||
var (
|
||||
fsPk = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fsAmount = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fsShares = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fsDescription = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fsCandidate = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
fsDelegator = flag.NewFlagSet("", flag.ContinueOnError)
|
||||
)
|
||||
|
||||
func init() {
|
||||
fsPk.String(FlagPubKey, "", "Go-Amino encoded hex PubKey of the validator-candidate. For Ed25519 the go-amino prepend hex is 1624de6220")
|
||||
fsAmount.String(FlagAmount, "1fermion", "Amount of coins to bond")
|
||||
fsShares.String(FlagShares, "", "Amount of shares to unbond, either in decimal or keyword MAX (ex. 1.23456789, 99, MAX)")
|
||||
fsDescription.String(FlagMoniker, "", "validator-candidate name")
|
||||
fsDescription.String(FlagIdentity, "", "optional keybase signature")
|
||||
fsDescription.String(FlagWebsite, "", "optional website")
|
||||
fsDescription.String(FlagDetails, "", "optional details")
|
||||
fsCandidate.String(FlagAddressCandidate, "", "hex address of the validator/candidate")
|
||||
fsDelegator.String(FlagAddressDelegator, "", "hex address of the delegator")
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
|
||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
||||
)
|
||||
|
||||
//// create command to query for all candidates
|
||||
//func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
//cmd := &cobra.Command{
|
||||
//Use: "candidates",
|
||||
//Short: "Query for the set of validator-candidates pubkeys",
|
||||
//RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
//key := stake.CandidatesKey
|
||||
|
||||
//ctx := context.NewCoreContextFromViper()
|
||||
//res, err := ctx.Query(key, storeName)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
|
||||
//// parse out the candidates
|
||||
//candidates := new(stake.Candidates)
|
||||
//err = cdc.UnmarshalBinary(res, candidates)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
//output, err := wire.MarshalJSONIndent(cdc, candidates)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
//fmt.Println(string(output))
|
||||
//return nil
|
||||
|
||||
//// TODO output with proofs / machine parseable etc.
|
||||
//},
|
||||
//}
|
||||
|
||||
//cmd.Flags().AddFlagSet(fsDelegator)
|
||||
//return cmd
|
||||
//}
|
||||
|
||||
// get the command to query a candidate
|
||||
func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "candidate",
|
||||
Short: "Query a validator-candidate account",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
addr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key := stake.GetCandidateKey(addr)
|
||||
|
||||
ctx := context.NewCoreContextFromViper()
|
||||
|
||||
res, err := ctx.Query(key, storeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// parse out the candidate
|
||||
candidate := new(stake.Candidate)
|
||||
err = cdc.UnmarshalBinary(res, candidate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
output, err := wire.MarshalJSONIndent(cdc, candidate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
return nil
|
||||
|
||||
// TODO output with proofs / machine parseable etc.
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// get the command to query a single delegator bond
|
||||
func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "delegator-bond",
|
||||
Short: "Query a delegators bond based on address and candidate pubkey",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
addr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bz, err := hex.DecodeString(viper.GetString(FlagAddressDelegator))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delegator := crypto.Address(bz)
|
||||
|
||||
key := stake.GetDelegatorBondKey(delegator, addr, cdc)
|
||||
|
||||
ctx := context.NewCoreContextFromViper()
|
||||
|
||||
res, err := ctx.Query(key, storeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// parse out the bond
|
||||
bond := new(stake.DelegatorBond)
|
||||
err = cdc.UnmarshalBinary(res, bond)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
output, err := wire.MarshalJSONIndent(cdc, bond)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
return nil
|
||||
|
||||
// TODO output with proofs / machine parseable etc.
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
cmd.Flags().AddFlagSet(fsDelegator)
|
||||
return cmd
|
||||
}
|
||||
|
||||
//// get the command to query all the candidates bonded to a delegator
|
||||
//func GetCmdQueryDelegatorBonds(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
//cmd := &cobra.Command{
|
||||
//Use: "delegator-candidates",
|
||||
//Short: "Query all delegators bond's candidate-addresses based on delegator-address",
|
||||
//RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
//bz, err := hex.DecodeString(viper.GetString(FlagAddressDelegator))
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
//delegator := crypto.Address(bz)
|
||||
|
||||
//key := stake.GetDelegatorBondsKey(delegator, cdc)
|
||||
|
||||
//ctx := context.NewCoreContextFromViper()
|
||||
|
||||
//res, err := ctx.Query(key, storeName)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
|
||||
//// parse out the candidates list
|
||||
//var candidates []crypto.PubKey
|
||||
//err = cdc.UnmarshalBinary(res, candidates)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
//output, err := wire.MarshalJSONIndent(cdc, candidates)
|
||||
//if err != nil {
|
||||
//return err
|
||||
//}
|
||||
//fmt.Println(string(output))
|
||||
//return nil
|
||||
|
||||
//// TODO output with proofs / machine parseable etc.
|
||||
//},
|
||||
//}
|
||||
//cmd.Flags().AddFlagSet(fsDelegator)
|
||||
//return cmd
|
||||
//}
|
||||
@@ -0,0 +1,199 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
||||
)
|
||||
|
||||
// create declare candidacy command
|
||||
func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "declare-candidacy",
|
||||
Short: "create new validator-candidate account and delegate some coins to it",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pkStr := viper.GetString(FlagPubKey)
|
||||
if len(pkStr) == 0 {
|
||||
return fmt.Errorf("must use --pubkey flag")
|
||||
}
|
||||
pkBytes, err := hex.DecodeString(pkStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pk, err := crypto.PubKeyFromBytes(pkBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetString(FlagMoniker) == "" {
|
||||
return fmt.Errorf("please enter a moniker for the validator-candidate using --moniker")
|
||||
}
|
||||
description := stake.Description{
|
||||
Moniker: viper.GetString(FlagMoniker),
|
||||
Identity: viper.GetString(FlagIdentity),
|
||||
Website: viper.GetString(FlagWebsite),
|
||||
Details: viper.GetString(FlagDetails),
|
||||
}
|
||||
msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description)
|
||||
|
||||
// build and sign the transaction, then broadcast to Tendermint
|
||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||
|
||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsPk)
|
||||
cmd.Flags().AddFlagSet(fsAmount)
|
||||
cmd.Flags().AddFlagSet(fsDescription)
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// create edit candidacy command
|
||||
func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit-candidacy",
|
||||
Short: "edit and existing validator-candidate account",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
description := stake.Description{
|
||||
Moniker: viper.GetString(FlagMoniker),
|
||||
Identity: viper.GetString(FlagIdentity),
|
||||
Website: viper.GetString(FlagWebsite),
|
||||
Details: viper.GetString(FlagDetails),
|
||||
}
|
||||
msg := stake.NewMsgEditCandidacy(candidateAddr, description)
|
||||
|
||||
// build and sign the transaction, then broadcast to Tendermint
|
||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||
|
||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsDescription)
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// create edit candidacy command
|
||||
func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "delegate",
|
||||
Short: "delegate coins to an existing validator/candidate",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator))
|
||||
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := stake.NewMsgDelegate(delegatorAddr, candidateAddr, amount)
|
||||
|
||||
// build and sign the transaction, then broadcast to Tendermint
|
||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||
|
||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsAmount)
|
||||
cmd.Flags().AddFlagSet(fsDelegator)
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// create edit candidacy command
|
||||
func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "unbond",
|
||||
Short: "unbond coins from a validator/candidate",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// check the shares before broadcasting
|
||||
sharesStr := viper.GetString(FlagShares)
|
||||
var shares sdk.Rat
|
||||
if sharesStr != "MAX" {
|
||||
var err error
|
||||
shares, err = sdk.NewRatFromDecimal(sharesStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !shares.GT(sdk.ZeroRat) {
|
||||
return fmt.Errorf("shares must be positive integer or decimal (ex. 123, 1.23456789)")
|
||||
}
|
||||
}
|
||||
|
||||
delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator))
|
||||
candidateAddr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := stake.NewMsgUnbond(delegatorAddr, candidateAddr, sharesStr)
|
||||
|
||||
// build and sign the transaction, then broadcast to Tendermint
|
||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||
|
||||
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().AddFlagSet(fsShares)
|
||||
cmd.Flags().AddFlagSet(fsDelegator)
|
||||
cmd.Flags().AddFlagSet(fsCandidate)
|
||||
return cmd
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/tendermint/go-crypto/keys"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// RegisterRoutes - Central function to define routes that get registered by the main application
|
||||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
|
||||
ctx := context.NewCoreContextFromViper()
|
||||
|
||||
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET")
|
||||
}
|
||||
|
||||
// BondingStatusHandler - http request handler to query delegator bonding status
|
||||
func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// read parameters
|
||||
vars := mux.Vars(r)
|
||||
delegator := vars["delegator"]
|
||||
validator := vars["validator"]
|
||||
|
||||
bz, err := hex.DecodeString(delegator)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
delegatorAddr := sdk.Address(bz)
|
||||
|
||||
bz, err = hex.DecodeString(validator)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
candidateAddr := sdk.Address(bz)
|
||||
|
||||
key := stake.GetDelegatorBondKey(delegatorAddr, candidateAddr, cdc)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var bond stake.DelegatorBond
|
||||
err = cdc.UnmarshalBinary(res, &bond)
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user