github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/token/client/rest/rest_v2.go (about) 1 package rest 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/fibonacci-chain/fbc/x/token/types" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 10 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 "github.com/fibonacci-chain/fbc/x/common" 12 "github.com/gorilla/mux" 13 ) 14 15 // RegisterRoutesV2, a central function to define routes 16 // which is called by the rest module in main application 17 func RegisterRoutesV2(cliCtx context.CLIContext, r *mux.Router, storeName string) { 18 r.HandleFunc(fmt.Sprintf("/tokens/{currency}"), tokenHandlerV2(cliCtx, storeName)).Methods("GET") 19 r.HandleFunc(fmt.Sprintf("/tokens"), tokensHandlerV2(cliCtx, storeName)).Methods("GET") 20 r.HandleFunc(fmt.Sprintf("/accounts/{address}"), accountsHandlerV2(cliCtx, storeName)).Methods("GET") 21 } 22 23 func tokenHandlerV2(cliCtx context.CLIContext, storeName string) http.HandlerFunc { 24 return func(w http.ResponseWriter, r *http.Request) { 25 vars := mux.Vars(r) 26 tokenName := vars["currency"] 27 28 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", storeName, types.QueryTokenV2, tokenName), nil) 29 common.HandleResponseV2(w, res, err) 30 } 31 } 32 33 func tokensHandlerV2(cliCtx context.CLIContext, storeName string) http.HandlerFunc { 34 return func(w http.ResponseWriter, r *http.Request) { 35 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", storeName, types.QueryTokensV2), nil) 36 common.HandleResponseV2(w, res, err) 37 } 38 } 39 40 func accountsHandlerV2(cliCtx context.CLIContext, storeName string) http.HandlerFunc { 41 return func(w http.ResponseWriter, r *http.Request) { 42 vars := mux.Vars(r) 43 address := vars["address"] 44 45 currency := r.URL.Query().Get("currency") 46 hideZero := r.URL.Query().Get("hide_zero") 47 48 if hideZero == "" { 49 hideZero = "yes" 50 } 51 if hideZero != "yes" && hideZero != "no" { 52 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam) 53 return 54 } 55 56 // valid address 57 if _, err := sdk.AccAddressFromBech32(address); err != nil { 58 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidAddress) 59 return 60 } 61 62 accountParam := types.AccountParamV2{ 63 Currency: currency, 64 HideZero: hideZero, 65 } 66 67 req, err := cliCtx.Codec.MarshalJSON(accountParam) 68 69 if err != nil { 70 common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error()) 71 return 72 } 73 74 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", storeName, types.QueryAccountV2, address), req) 75 common.HandleResponseV2(w, res, err) 76 } 77 }