github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/erc20/client/rest/rest.go (about) 1 package rest 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gorilla/mux" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 11 comm "github.com/fibonacci-chain/fbc/x/common" 12 "github.com/fibonacci-chain/fbc/x/erc20/types" 13 govRest "github.com/fibonacci-chain/fbc/x/gov/client/rest" 14 ) 15 16 // RegisterRoutes - Central function to define routes that get registered by the main application 17 func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { 18 r.HandleFunc("/erc20/token_mapping", tokenMappingHandlerFn(cliCtx)).Methods("GET") 19 r.HandleFunc("/erc20/contract/{denom_hash}", contractByDenomHandlerFn(cliCtx)).Methods("GET") 20 r.HandleFunc("/erc20/denom/{contract}", denomByContractHandlerFn(cliCtx)).Methods("GET") 21 } 22 23 func tokenMappingHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 24 return func(w http.ResponseWriter, r *http.Request) { 25 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 26 if !ok { 27 return 28 } 29 30 res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.RouterKey, types.QueryTokenMapping), nil) 31 if err != nil { 32 sdkErr := comm.ParseSDKError(err.Error()) 33 comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message) 34 return 35 } 36 37 var result []types.QueryTokenMappingResponse 38 if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil { 39 comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error()) 40 return 41 } 42 43 cliCtx = cliCtx.WithHeight(height) 44 rest.PostProcessResponse(w, cliCtx, result) 45 } 46 } 47 48 func contractByDenomHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 49 return func(w http.ResponseWriter, r *http.Request) { 50 denom := mux.Vars(r)["denom_hash"] 51 52 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 53 if !ok { 54 return 55 } 56 57 params := cliCtx.Codec.MustMarshalJSON(types.ContractByDenomRequest{types.IbcDenomPrefix + denom}) 58 res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.RouterKey, types.QueryContractByDenom), params) 59 if err != nil { 60 sdkErr := comm.ParseSDKError(err.Error()) 61 comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message) 62 return 63 } 64 65 cliCtx = cliCtx.WithHeight(height) 66 rest.PostProcessResponse(w, cliCtx, res) 67 } 68 } 69 70 func denomByContractHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 71 return func(w http.ResponseWriter, r *http.Request) { 72 contract := mux.Vars(r)["contract"] 73 74 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 75 if !ok { 76 return 77 } 78 79 params := cliCtx.Codec.MustMarshalJSON(types.DenomByContractRequest{contract}) 80 res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.RouterKey, types.QueryDenomByContract), params) 81 if err != nil { 82 sdkErr := comm.ParseSDKError(err.Error()) 83 comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message) 84 return 85 } 86 87 cliCtx = cliCtx.WithHeight(height) 88 rest.PostProcessResponse(w, cliCtx, res) 89 } 90 } 91 92 // TokenMappingProposalRESTHandler defines erc20 proposal handler 93 func TokenMappingProposalRESTHandler(context.CLIContext) govRest.ProposalRESTHandler { 94 return govRest.ProposalRESTHandler{} 95 } 96 97 // ProxyContractRedirectRESTHandler defines erc20 proxy contract redirect proposal handler 98 func ProxyContractRedirectRESTHandler(context.CLIContext) govRest.ProposalRESTHandler { 99 return govRest.ProposalRESTHandler{} 100 } 101 func ContractTemplateProposalRESTHandler(context.CLIContext) govRest.ProposalRESTHandler { 102 return govRest.ProposalRESTHandler{} 103 }