Addressed comments

This commit is contained in:
Matt Bell 2018-05-25 00:41:17 +09:00
parent 0738de17f4
commit a9bcdb2a0a
3 changed files with 32 additions and 30 deletions

View File

@ -20,7 +20,7 @@ func registerQueryRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec
).Methods("GET")
}
// bondingStatusHandlerFn - http request handler to query delegator bonding status
// http request handler to query delegator bonding status
func bondingStatusHandlerFn(storeName string, cdc *wire.Codec, ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// read parameters

View File

@ -1,6 +1,7 @@
package rest
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
@ -16,33 +17,26 @@ import (
)
func registerTxRoutes(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/stake/bondunbond", bondUnbondRequestHandlerFn(cdc, kb, ctx)).Methods("POST")
r.HandleFunc(
"/stake/delegations",
editDelegationsRequestHandlerFn(cdc, kb, ctx),
).Methods("POST")
}
type bond struct {
Amount sdk.Coin `json:"amount"`
Candidate sdk.Address `json:"candidate"`
}
type unbond struct {
Shares string `json:"shares"`
Candidate sdk.Address `json:"candidate"`
}
type bondUnbondBody struct {
type editDelegationsBody struct {
// fees is not used currently
// Fees sdk.Coin `json="fees"`
LocalAccountName string `json:"name"`
Password string `json:"password"`
ChainID string `json:"chain_id"`
Sequence int64 `json:"sequence"`
Bond []bond `json:"bond"`
Unbond []unbond `json:"unbond"`
LocalAccountName string `json:"name"`
Password string `json:"password"`
ChainID string `json:"chain_id"`
Sequence int64 `json:"sequence"`
Delegate []stake.MsgDelegate `json:"delegate"`
Unbond []stake.MsgUnbond `json:"unbond"`
}
func bondUnbondRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc {
func editDelegationsRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var m bondUnbondBody
var m editDelegationsBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
@ -64,13 +58,21 @@ func bondUnbondRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, ctx context.Co
}
// build messages
messages := make([]sdk.Msg, 0, len(m.Bond)+len(m.Unbond))
for _, bond := range m.Bond {
msg := stake.NewMsgDelegate(info.Address(), bond.Candidate, bond.Amount)
messages := make([]sdk.Msg, 0, len(m.Delegate)+len(m.Unbond))
for _, msg := range m.Delegate {
if !bytes.Equal(info.Address(), msg.DelegatorAddr) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Must use own delegator address"))
return
}
messages = append(messages, msg)
}
for _, unbond := range m.Unbond {
msg := stake.NewMsgUnbond(info.Address(), unbond.Candidate, unbond.Shares)
for _, msg := range m.Unbond {
if !bytes.Equal(info.Address(), msg.DelegatorAddr) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Must use own delegator address"))
return
}
messages = append(messages, msg)
}

View File

@ -117,8 +117,8 @@ func (msg MsgEditCandidacy) ValidateBasic() sdk.Error {
// MsgDelegate - struct for bonding transactions
type MsgDelegate struct {
DelegatorAddr sdk.Address `json:"delegator"`
ValidatorAddr sdk.Address `json:"candidate"`
DelegatorAddr sdk.Address `json:"delegator_addr"`
ValidatorAddr sdk.Address `json:"validator_addr"`
Bond sdk.Coin `json:"bond"`
}
@ -164,8 +164,8 @@ func (msg MsgDelegate) ValidateBasic() sdk.Error {
// MsgUnbond - struct for unbonding transactions
type MsgUnbond struct {
DelegatorAddr sdk.Address `json:"delegator"`
ValidatorAddr sdk.Address `json:"candidate"`
DelegatorAddr sdk.Address `json:"delegator_addr"`
ValidatorAddr sdk.Address `json:"validator_addr"`
Shares string `json:"shares"`
}