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"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-04-24 15:20:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func registerTxRoutes(cliCtx client.Context, 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-hard", postClaimHandlerFn(cliCtx, hardGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-delegator", postClaimHandlerFn(cliCtx, delegatorGenerator)).Methods("POST")
|
|
|
|
r.HandleFunc("/incentive/claim-swap", postClaimHandlerFn(cliCtx, swapGenerator)).Methods("POST")
|
2022-09-22 18:26:08 +00:00
|
|
|
r.HandleFunc("/incentive/claim-earn", postClaimHandlerFn(cliCtx, earnGenerator)).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)
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgClaimUSDXMintingReward(req.Sender.String(), req.DenomsToClaim[0].MultiplierName)
|
|
|
|
return &msg, nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func hardGenerator(req PostClaimReq) (sdk.Msg, error) {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgClaimHardReward(req.Sender.String(), req.DenomsToClaim)
|
|
|
|
return &msg, nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func delegatorGenerator(req PostClaimReq) (sdk.Msg, error) {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgClaimDelegatorReward(req.Sender.String(), req.DenomsToClaim)
|
|
|
|
return &msg, nil
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
func swapGenerator(req PostClaimReq) (sdk.Msg, error) {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := types.NewMsgClaimSwapReward(req.Sender.String(), req.DenomsToClaim)
|
|
|
|
return &msg, nil
|
2021-06-07 14:04:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:08 +00:00
|
|
|
func earnGenerator(req PostClaimReq) (sdk.Msg, error) {
|
|
|
|
msg := types.NewMsgClaimEarnReward(req.Sender.String(), req.DenomsToClaim)
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postClaimHandlerFn(cliCtx client.Context, 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
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2021-07-07 16:50:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody.BaseReq = requestBody.BaseReq.Sanitize()
|
|
|
|
if !requestBody.BaseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
2022-01-08 00:39:27 +00:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2021-07-07 16:50:14 +00:00
|
|
|
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)
|
2022-01-08 00:39:27 +00:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2021-08-04 14:55:29 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
|
2021-07-07 16:50:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, requestBody.BaseReq, msg)
|
2021-07-07 16:50:14 +00:00
|
|
|
}
|
|
|
|
}
|