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-01-26 11:52:34 +00:00
|
|
|
r.HandleFunc("/incentive/claim-cdp", postClaimCdpHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-hard", postClaimHardHandlerFn(cliCtx)).Methods("POST")
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:52:34 +00:00
|
|
|
func postClaimCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2020-04-24 15:20:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody types.PostClaimReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &requestBody) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
requestBody.BaseReq = requestBody.BaseReq.Sanitize()
|
|
|
|
if !requestBody.BaseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
|
|
|
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-01-18 19:12:37 +00:00
|
|
|
msg := types.NewMsgClaimUSDXMintingReward(requestBody.Sender, requestBody.MultiplierName)
|
2020-04-24 15:20:34 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, requestBody.BaseReq, []sdk.Msg{msg})
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 11:52:34 +00:00
|
|
|
|
|
|
|
func postClaimHardHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody types.PostClaimReq
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgClaimHardLiquidityProviderReward(requestBody.Sender, requestBody.MultiplierName)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, requestBody.BaseReq, []sdk.Msg{msg})
|
|
|
|
}
|
|
|
|
}
|