2020-09-21 21:08:43 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/parameters", types.ModuleName), queryParamsHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/deposits", types.ModuleName), queryDepositsHandlerFn(cliCtx)).Methods("GET")
|
2021-01-13 18:14:58 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/total-deposited", types.ModuleName), queryTotalDepositedHandlerFn(cliCtx)).Methods("GET")
|
2020-09-21 21:08:43 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/accounts", types.ModuleName), queryModAccountsHandlerFn(cliCtx)).Methods("GET")
|
2020-12-22 16:08:27 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/borrows", types.ModuleName), queryBorrowsHandlerFn(cliCtx)).Methods("GET")
|
2021-01-13 18:14:58 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/total-borrowed", types.ModuleName), queryTotalBorrowedHandlerFn(cliCtx)).Methods("GET")
|
2021-02-10 17:53:53 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/interest-rate", types.ModuleName), queryInterestRateHandlerFn(cliCtx)).Methods("GET")
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-22 14:42:30 +00:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryGetParams)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var denom string
|
|
|
|
var owner sdk.AccAddress
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
denom = strings.TrimSpace(x)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestOwner); len(x) != 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
ownerStr := strings.ToLower(strings.TrimSpace(x))
|
|
|
|
owner, err = sdk.AccAddressFromBech32(ownerStr)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2021-01-13 18:14:58 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("cannot parse address from deposit owner %s", ownerStr))
|
2020-10-20 17:18:12 +00:00
|
|
|
return
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
params := types.NewQueryDepositsParams(page, limit, denom, owner)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetDeposits)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
func queryTotalDepositedHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2020-09-21 21:08:43 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-01-13 18:14:58 +00:00
|
|
|
_, _, _, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var denom string
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
denom = strings.TrimSpace(x)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
params := types.NewQueryTotalDepositedParams(denom)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetTotalDeposited)
|
2020-09-21 21:08:43 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
func queryBorrowsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2020-12-22 16:08:27 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-01-13 18:14:58 +00:00
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
2020-12-22 16:08:27 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var denom string
|
|
|
|
var owner sdk.AccAddress
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
|
|
|
denom = strings.TrimSpace(x)
|
|
|
|
}
|
2020-12-22 16:08:27 +00:00
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestOwner); len(x) != 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
ownerStr := strings.ToLower(strings.TrimSpace(x))
|
|
|
|
owner, err = sdk.AccAddressFromBech32(ownerStr)
|
2020-12-22 16:08:27 +00:00
|
|
|
if err != nil {
|
2021-01-13 18:14:58 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("cannot parse address from borrow owner %s", ownerStr))
|
2020-12-22 16:08:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
params := types.NewQueryBorrowsParams(page, limit, owner, denom)
|
2020-12-22 16:08:27 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetBorrows)
|
2020-12-22 16:08:27 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
func queryTotalBorrowedHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2020-12-22 16:08:27 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _, _, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var denom string
|
2020-12-22 16:08:27 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
|
|
|
denom = strings.TrimSpace(x)
|
2020-12-22 16:08:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
params := types.NewQueryTotalBorrowedParams(denom)
|
2020-12-22 16:08:27 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetTotalBorrowed)
|
2020-12-22 16:08:27 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:53:53 +00:00
|
|
|
func queryInterestRateHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _, _, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var denom string
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
|
|
|
denom = strings.TrimSpace(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQueryInterestRateParams(denom)
|
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetInterestRate)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func queryModAccountsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var name string
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestName); len(x) != 0 {
|
|
|
|
name = strings.TrimSpace(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQueryAccountParams(page, limit, name)
|
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetModuleAccounts)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|