2023-04-06 22:51:34 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2024-02-27 19:40:52 +00:00
|
|
|
"context"
|
2023-04-06 22:51:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/client/rest"
|
2023-04-06 22:51:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/validator-vesting/types"
|
2023-04-06 22:51:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/circulatingsupply", types.QueryPath), getCirculatingSupplyHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/totalsupply", types.QueryPath), getTotalSupplyHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/circulatingsupplyhard", types.QueryPath), getCirculatingSupplyHARDHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/circulatingsupplyusdx", types.QueryPath), getCirculatingSupplyUSDXHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/circulatingsupplyswp", types.QueryPath), getCirculatingSupplySWPHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/totalsupplyhard", types.QueryPath), getTotalSupplyHARDHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/totalsupplyusdx", types.QueryPath), getTotalSupplyUSDXHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTotalSupplyHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.TotalSupply(context.Background(), &types.QueryTotalSupplyRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCirculatingSupplyHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.CirculatingSupply(context.Background(), &types.QueryCirculatingSupplyRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCirculatingSupplyHARDHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.CirculatingSupplyHARD(context.Background(), &types.QueryCirculatingSupplyHARDRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCirculatingSupplyUSDXHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.CirculatingSupplyUSDX(context.Background(), &types.QueryCirculatingSupplyUSDXRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCirculatingSupplySWPHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.CirculatingSupplySWP(context.Background(), &types.QueryCirculatingSupplySWPRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTotalSupplyHARDHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.TotalSupplyHARD(context.Background(), &types.QueryTotalSupplyHARDRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTotalSupplyUSDXHandlerFn(cliCtx client.Context) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:40:52 +00:00
|
|
|
queryClient := types.NewQueryClient(cliCtx)
|
|
|
|
res, err := queryClient.TotalSupplyUSDX(context.Background(), &types.QueryTotalSupplyUSDXRequest{})
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:40:52 +00:00
|
|
|
resBytes, err := json.Marshal(res.Amount.Int64())
|
2023-04-06 22:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
}
|