2018-05-14 16:43:51 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-05-24 08:41:17 -07:00
|
|
|
"bytes"
|
2018-05-14 16:43:51 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2018-08-31 10:04:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-05-14 16:43:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-08-22 04:38:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-08-06 11:11:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2018-05-14 16:43:51 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-07 10:04:58 -07:00
|
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
2018-05-14 16:43:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2018-05-14 16:43:51 -07:00
|
|
|
)
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
|
2018-05-24 08:41:17 -07:00
|
|
|
r.HandleFunc(
|
2018-08-08 03:38:39 -07:00
|
|
|
"/stake/delegators/{delegatorAddr}/delegations",
|
|
|
|
delegationsRequestHandlerFn(cdc, kb, cliCtx),
|
2018-05-24 08:41:17 -07:00
|
|
|
).Methods("POST")
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
type (
|
|
|
|
msgDelegationsInput struct {
|
|
|
|
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
|
|
|
ValidatorAddr string `json:"validator_addr"` // in bech32
|
|
|
|
Delegation sdk.Coin `json:"delegation"`
|
|
|
|
}
|
2018-06-05 21:53:04 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
msgBeginRedelegateInput struct {
|
|
|
|
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
|
|
|
ValidatorSrcAddr string `json:"validator_src_addr"` // in bech32
|
|
|
|
ValidatorDstAddr string `json:"validator_dst_addr"` // in bech32
|
|
|
|
SharesAmount string `json:"shares"`
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBeginUnbondingInput struct {
|
|
|
|
DelegatorAddr string `json:"delegator_addr"` // in bech32
|
|
|
|
ValidatorAddr string `json:"validator_addr"` // in bech32
|
|
|
|
SharesAmount string `json:"shares"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// the request body for edit delegations
|
|
|
|
EditDelegationsReq struct {
|
2018-10-07 21:43:47 -07:00
|
|
|
BaseReq utils.BaseReq `json:"base_req"`
|
|
|
|
Delegations []msgDelegationsInput `json:"delegations"`
|
|
|
|
BeginUnbondings []msgBeginUnbondingInput `json:"begin_unbondings"`
|
|
|
|
BeginRedelegates []msgBeginRedelegateInput `json:"begin_redelegates"`
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
)
|
2018-05-14 16:43:51 -07:00
|
|
|
|
2018-07-09 16:08:35 -07:00
|
|
|
// TODO: Split this up into several smaller functions, and remove the above nolint
|
2018-08-08 03:38:39 -07:00
|
|
|
// TODO: use sdk.ValAddress instead of sdk.AccAddress for validators in messages
|
2018-09-26 06:29:39 -07:00
|
|
|
// TODO: Seriously consider how to refactor...do we need to make it multiple txs?
|
|
|
|
// If not, we can just use CompleteAndBroadcastTxREST.
|
2018-09-13 11:17:32 -07:00
|
|
|
func delegationsRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
2018-05-14 16:43:51 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-09-26 06:29:39 -07:00
|
|
|
var req EditDelegationsReq
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-05-14 16:43:51 -07:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
err = cdc.UnmarshalJSON(body, &req)
|
2018-05-14 16:43:51 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := kb.Get(baseReq.Name)
|
2018-05-14 16:43:51 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// build messages
|
2018-09-26 06:29:39 -07:00
|
|
|
messages := make([]sdk.Msg, len(req.Delegations)+
|
|
|
|
len(req.BeginRedelegates)+
|
2018-10-07 21:43:47 -07:00
|
|
|
len(req.BeginUnbondings))
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-05-30 09:00:00 -07:00
|
|
|
i := 0
|
2018-09-26 06:29:39 -07:00
|
|
|
for _, msg := range req.Delegations {
|
2018-08-30 21:06:44 -07:00
|
|
|
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-05 21:53:04 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddr)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-05 21:53:04 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
2018-05-24 08:41:17 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
messages[i] = stake.MsgDelegate{
|
2018-08-30 21:06:44 -07:00
|
|
|
DelegatorAddr: delAddr,
|
|
|
|
ValidatorAddr: valAddr,
|
2018-07-10 17:16:37 -07:00
|
|
|
Delegation: msg.Delegation,
|
2018-06-05 21:53:04 -07:00
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-05-30 09:00:00 -07:00
|
|
|
i++
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
for _, msg := range req.BeginRedelegates {
|
2018-08-30 21:06:44 -07:00
|
|
|
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
valDstAddr, err := sdk.ValAddressFromBech32(msg.ValidatorDstAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-14 17:15:02 -07:00
|
|
|
shares, err := sdk.NewDecFromStr(msg.SharesAmount)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
messages[i] = stake.MsgBeginRedelegate{
|
2018-08-30 21:06:44 -07:00
|
|
|
DelegatorAddr: delAddr,
|
|
|
|
ValidatorSrcAddr: valSrcAddr,
|
|
|
|
ValidatorDstAddr: valDstAddr,
|
2018-06-26 19:00:12 -07:00
|
|
|
SharesAmount: shares,
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
for _, msg := range req.BeginUnbondings {
|
2018-08-30 21:06:44 -07:00
|
|
|
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
if !bytes.Equal(info.GetPubKey().Address(), delAddr) {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own delegator address")
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-08-14 17:15:02 -07:00
|
|
|
shares, err := sdk.NewDecFromStr(msg.SharesAmount)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
messages[i] = stake.MsgBeginUnbonding{
|
2018-08-30 21:06:44 -07:00
|
|
|
DelegatorAddr: delAddr,
|
|
|
|
ValidatorAddr: valAddr,
|
2018-06-26 19:00:12 -07:00
|
|
|
SharesAmount: shares,
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
simulateGas, gas, err := client.ReadGasFlag(baseReq.Gas)
|
2018-09-11 17:31:30 -07:00
|
|
|
if err != nil {
|
|
|
|
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
|
|
|
adjustment, ok := utils.ParseFloat64OrReturnBadRequest(w, baseReq.GasAdjustment, client.DefaultGasAdjustment)
|
2018-09-11 17:31:30 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-09-07 10:15:49 -07:00
|
|
|
txBldr := authtxb.TxBuilder{
|
2018-09-11 17:31:30 -07:00
|
|
|
Codec: cdc,
|
|
|
|
Gas: gas,
|
|
|
|
GasAdjustment: adjustment,
|
|
|
|
SimulateGas: simulateGas,
|
2018-09-26 06:29:39 -07:00
|
|
|
ChainID: baseReq.ChainID,
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
2018-06-11 18:12:37 -07:00
|
|
|
|
2018-05-14 16:43:51 -07:00
|
|
|
// sign messages
|
2018-05-30 09:00:00 -07:00
|
|
|
signedTxs := make([][]byte, len(messages[:]))
|
|
|
|
for i, msg := range messages {
|
2018-05-14 16:43:51 -07:00
|
|
|
// increment sequence for each message
|
2018-09-26 06:29:39 -07:00
|
|
|
txBldr = txBldr.WithAccountNumber(baseReq.AccountNumber)
|
|
|
|
txBldr = txBldr.WithSequence(baseReq.Sequence)
|
|
|
|
|
|
|
|
baseReq.Sequence++
|
2018-05-14 16:43:51 -07:00
|
|
|
|
2018-09-11 17:31:30 -07:00
|
|
|
if utils.HasDryRunArg(r) || txBldr.SimulateGas {
|
2018-09-26 06:29:39 -07:00
|
|
|
newBldr, err := utils.EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, []sdk.Msg{msg})
|
2018-08-22 04:38:55 -07:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-08-22 04:38:55 -07:00
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-08-31 10:04:11 -07:00
|
|
|
if utils.HasDryRunArg(r) {
|
2018-09-11 17:31:30 -07:00
|
|
|
utils.WriteSimulationResponse(w, newBldr.Gas)
|
2018-08-31 10:04:11 -07:00
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-09-11 17:31:30 -07:00
|
|
|
txBldr = newBldr
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
|
|
|
|
2018-09-02 11:20:14 -07:00
|
|
|
if utils.HasGenerateOnlyArg(r) {
|
2018-09-07 10:15:49 -07:00
|
|
|
utils.WriteGenerateStdTxResponse(w, txBldr, []sdk.Msg{msg})
|
2018-09-02 11:20:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
txBytes, err := txBldr.BuildAndSign(baseReq.Name, baseReq.Password, []sdk.Msg{msg})
|
2018-05-14 16:43:51 -07:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-30 09:00:00 -07:00
|
|
|
signedTxs[i] = txBytes
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// send
|
|
|
|
// XXX the operation might not be atomic if a tx fails
|
|
|
|
// should we have a sdk.MultiMsg type to make sending atomic?
|
2018-05-30 09:00:00 -07:00
|
|
|
results := make([]*ctypes.ResultBroadcastTxCommit, len(signedTxs[:]))
|
|
|
|
for i, txBytes := range signedTxs {
|
2018-08-06 11:11:30 -07:00
|
|
|
res, err := cliCtx.BroadcastTx(txBytes)
|
2018-05-14 16:43:51 -07:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-05-30 09:00:00 -07:00
|
|
|
results[i] = res
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
output, err := codec.MarshalJSONIndent(cdc, results[:])
|
2018-05-14 16:43:51 -07:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
}
|