2020-08-17 17:09:02 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-08-17 17:09:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/issuance/types"
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
|
2020-08-17 17:09:02 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/issue", types.ModuleName), postIssueTokensHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/redeem", types.ModuleName), postRedeemTokensHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/block", types.ModuleName), postBlockAddressHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/unblock", types.ModuleName), postUnblockAddressHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/pause", types.ModuleName), postPauseHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postIssueTokensHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-08-17 17:09:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostIssueReq
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := requestBody.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgIssueTokens(
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
requestBody.Tokens,
|
2022-01-08 00:39:27 +00:00
|
|
|
requestBody.Receiver.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, msg)
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postRedeemTokensHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-08-17 17:09:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostRedeemReq
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := requestBody.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgRedeemTokens(
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
requestBody.Tokens,
|
|
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, msg)
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postBlockAddressHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-08-17 17:09:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostBlockAddressReq
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := requestBody.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgBlockAddress(
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
requestBody.Denom,
|
2022-01-08 00:39:27 +00:00
|
|
|
requestBody.Address.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, msg)
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postUnblockAddressHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-08-17 17:09:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostUnblockAddressReq
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := requestBody.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgUnblockAddress(
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
requestBody.Denom,
|
2022-01-08 00:39:27 +00:00
|
|
|
requestBody.Address.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, msg)
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postPauseHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-08-17 17:09:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestBody PostPauseReq
|
2022-01-08 00:39:27 +00:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &requestBody) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := requestBody.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-08-17 17:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgSetPauseStatus(
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr.String(),
|
2020-08-17 17:09:02 +00:00
|
|
|
requestBody.Denom,
|
|
|
|
requestBody.Status,
|
|
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, msg)
|
2020-08-17 17:09:02 +00:00
|
|
|
}
|
|
|
|
}
|