2020-04-24 15:20:34 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2020-05-24 18:29:48 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-04-24 15:20:34 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2021-07-15 14:05:54 +00:00
|
|
|
r.HandleFunc("/incentive/claim-cdp", postClaimHandlerFn(cliCtx, usdxMintingGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-cdp-vesting", postClaimVVestingHandlerFn(cliCtx, usdxMintingVVGenerator)).Methods("POST")
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
r.HandleFunc("/incentive/claim-hard", postClaimHandlerFn(cliCtx, hardGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-hard-vesting", postClaimVVestingHandlerFn(cliCtx, hardVVGenerator)).Methods("POST")
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
r.HandleFunc("/incentive/claim-delegator", postClaimHandlerFn(cliCtx, delegatorGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-delegator-vesting", postClaimVVestingHandlerFn(cliCtx, delegatorVVGenerator)).Methods("POST")
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
r.HandleFunc("/incentive/claim-swap", postClaimHandlerFn(cliCtx, swapGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-swap-vesting", postClaimVVestingHandlerFn(cliCtx, swapVVGenerator)).Methods("POST")
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
2021-01-26 11:52:34 +00:00
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func usdxMintingGenerator(req PostClaimReq) (sdk.Msg, error) {
|
|
|
|
if len(req.DenomsToClaim) != 1 {
|
|
|
|
return nil, fmt.Errorf("must only claim %s denom for usdx minting rewards, got '%s", types.USDXMintingRewardDenom, req.DenomsToClaim)
|
|
|
|
}
|
|
|
|
return types.NewMsgClaimUSDXMintingReward(req.Sender, req.DenomsToClaim[0].MultiplierName), nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func hardGenerator(req PostClaimReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimHardReward(req.Sender, req.DenomsToClaim...), nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func delegatorGenerator(req PostClaimReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimDelegatorReward(req.Sender, req.DenomsToClaim...), nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func swapGenerator(req PostClaimReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimSwapReward(req.Sender, req.DenomsToClaim...), nil
|
2021-06-07 14:04:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func postClaimHandlerFn(cliCtx context.CLIContext, msgGenerator func(req PostClaimReq) (sdk.Msg, error)) http.HandlerFunc {
|
2021-01-26 11:52:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-04-02 21:34:42 +00:00
|
|
|
var requestBody PostClaimReq
|
2021-01-26 11:52:34 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &requestBody) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody.BaseReq = requestBody.BaseReq.Sanitize()
|
|
|
|
if !requestBody.BaseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(fromAddr, requestBody.Sender) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, requestBody.Sender))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
msg, err := msgGenerator(requestBody)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:52:34 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, requestBody.BaseReq, []sdk.Msg{msg})
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 14:04:32 +00:00
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func usdxMintingVVGenerator(req PostClaimVVestingReq) (sdk.Msg, error) {
|
|
|
|
if len(req.DenomsToClaim) != 1 {
|
|
|
|
return nil, fmt.Errorf("must only claim %s denom for usdx minting rewards, got '%s", types.USDXMintingRewardDenom, req.DenomsToClaim)
|
|
|
|
}
|
|
|
|
return types.NewMsgClaimUSDXMintingRewardVVesting(req.Sender, req.Receiver, req.DenomsToClaim[0].MultiplierName), nil
|
2021-06-07 14:04:32 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func hardVVGenerator(req PostClaimVVestingReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimHardRewardVVesting(req.Sender, req.Receiver, req.DenomsToClaim...), nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func delegatorVVGenerator(req PostClaimVVestingReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimDelegatorRewardVVesting(req.Sender, req.Receiver, req.DenomsToClaim...), nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2021-08-04 14:55:29 +00:00
|
|
|
func swapVVGenerator(req PostClaimVVestingReq) (sdk.Msg, error) {
|
|
|
|
return types.NewMsgClaimSwapRewardVVesting(req.Sender, req.Receiver, req.DenomsToClaim...), nil
|
2021-07-07 16:50:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func postClaimVVestingHandlerFn(cliCtx context.CLIContext, msgGenerator func(req PostClaimVVestingReq) (sdk.Msg, error)) http.HandlerFunc {
|
2021-07-07 16:50:14 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostClaimVVestingReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &requestBody) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody.BaseReq = requestBody.BaseReq.Sanitize()
|
|
|
|
if !requestBody.BaseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(fromAddr, requestBody.Sender) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, requestBody.Sender))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
msg, err := msgGenerator(requestBody)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, requestBody.BaseReq, []sdk.Msg{msg})
|
|
|
|
}
|
|
|
|
}
|