mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
ffef832d45
- Upgrade cosmos-sdk to v0.44.5 from v0.39.2 - Add Legacy Tx Endpoint for backwards compatibility - Add IBC v1.2.3 Support Co-authored-by: DracoLi <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: denalimarsh <denalimarsh@gmail.com> Co-authored-by: Draco Li <draco@kava.io> Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> Co-authored-by: Denali Marsh <denali@kava.io>
229 lines
5.9 KiB
Go
229 lines
5.9 KiB
Go
package rest
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
|
|
r.HandleFunc("/cdp", postCdpHandlerFn(cliCtx)).Methods("POST")
|
|
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")
|
|
r.HandleFunc("/cdp/{owner}/{collateralType}/liquidate", postLiquidateHandlerFn(cliCtx)).Methods("POST")
|
|
}
|
|
|
|
func postCdpHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostCdpReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
if !bytes.Equal(fromAddr, req.Sender) {
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Sender))
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgCreateCDP(
|
|
req.Sender,
|
|
req.Collateral,
|
|
req.Principal,
|
|
req.CollateralType,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|
|
|
|
func postDepositHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostDepositReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgDeposit(
|
|
req.Owner,
|
|
req.Depositor,
|
|
req.Collateral,
|
|
req.CollateralType,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|
|
|
|
func postWithdrawHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostWithdrawalReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgWithdraw(
|
|
req.Owner,
|
|
req.Depositor,
|
|
req.Collateral,
|
|
req.CollateralType,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|
|
|
|
func postDrawHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostDrawReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
if !bytes.Equal(fromAddr, req.Owner) {
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Owner))
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgDrawDebt(
|
|
req.Owner,
|
|
req.CollateralType,
|
|
req.Principal,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|
|
|
|
func postRepayHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostRepayReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
if !bytes.Equal(fromAddr, req.Owner) {
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("expected: %s, got: %s", fromAddr, req.Owner))
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgRepayDebt(
|
|
req.Owner,
|
|
req.CollateralType,
|
|
req.Payment,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|
|
|
|
func postLiquidateHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req PostLiquidateReq
|
|
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
|
|
return
|
|
}
|
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
if !baseReq.ValidateBasic(w) {
|
|
return
|
|
}
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(baseReq.From)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
msg := types.NewMsgLiquidate(
|
|
fromAddr,
|
|
req.Owner,
|
|
req.CollateralType,
|
|
)
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
|
|
}
|
|
}
|