2019-11-25 19:46:02 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2020-05-24 18:29:48 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-11-25 19:46:02 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-05-24 18:29:48 +00:00
|
|
|
"github.com/gorilla/mux"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
|
2020-02-03 15:41:28 +00:00
|
|
|
r.HandleFunc("/cdp", postCdpHandlerFn(cliCtx)).Methods("POST")
|
2020-08-21 19:42:46 +00:00
|
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/deposits", postDepositHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/withdraw", postWithdrawHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/draw", postDrawHandlerFn(cliCtx)).Methods("POST")
|
|
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/repay", postRepayHandlerFn(cliCtx)).Methods("POST")
|
2021-02-23 21:02:08 +00:00
|
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/liquidate", postLiquidateHandlerFn(cliCtx)).Methods("POST")
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postCdpHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-01-12 15:35:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostCdpReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
2020-05-24 18:29:48 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
if !bytes.Equal(fromAddr, req.Sender) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Sender))
|
2020-05-24 18:29:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
msg := types.NewMsgCreateCDP(
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Sender,
|
|
|
|
req.Collateral,
|
|
|
|
req.Principal,
|
|
|
|
req.CollateralType,
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
2020-05-24 18:29:48 +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-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postDepositHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2019-11-25 19:46:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostDepositReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
msg := types.NewMsgDeposit(
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Owner,
|
|
|
|
req.Depositor,
|
|
|
|
req.Collateral,
|
|
|
|
req.CollateralType,
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
2020-05-24 18:29:48 +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-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postWithdrawHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-01-12 15:35:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostWithdrawalReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
msg := types.NewMsgWithdraw(
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Owner,
|
|
|
|
req.Depositor,
|
|
|
|
req.Collateral,
|
|
|
|
req.CollateralType,
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
2020-05-24 18:29:48 +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-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postDrawHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-01-12 15:35:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostDrawReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
2020-05-24 18:29:48 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
if !bytes.Equal(fromAddr, req.Owner) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Owner))
|
2020-05-24 18:29:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
msg := types.NewMsgDrawDebt(
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Owner,
|
|
|
|
req.CollateralType,
|
|
|
|
req.Principal,
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
2020-05-24 18:29:48 +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-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postRepayHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2020-01-12 15:35:34 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostRepayReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
2020-05-24 18:29:48 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
if !bytes.Equal(fromAddr, req.Owner) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Owner))
|
2020-05-24 18:29:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
msg := types.NewMsgRepayDebt(
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Owner,
|
|
|
|
req.CollateralType,
|
|
|
|
req.Payment,
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
2020-05-24 18:29:48 +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)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 19:12:37 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func postLiquidateHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
2021-01-18 19:12:37 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-01-08 00:39:27 +00:00
|
|
|
var req PostLiquidateReq
|
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
2021-01-18 19:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2021-01-18 19:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
2021-01-18 19:12:37 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgLiquidate(
|
|
|
|
fromAddr,
|
2022-01-08 00:39:27 +00:00
|
|
|
req.Owner,
|
|
|
|
req.CollateralType,
|
2021-01-18 19:12:37 +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)
|
2021-01-18 19:12:37 +00:00
|
|
|
}
|
|
|
|
}
|