github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/ammswap/client/rest/query.go (about) 1 package rest 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "strings" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 11 "github.com/fibonacci-chain/fbc/x/ammswap/types" 12 "github.com/fibonacci-chain/fbc/x/common" 13 "github.com/gorilla/mux" 14 ) 15 16 func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { 17 r = r.PathPrefix("/swap").Subrouter() 18 r.HandleFunc("/token_pair/{name}", querySwapTokenPairHandler(cliCtx)).Methods("GET") 19 r.HandleFunc("/params", queryParamsHandler(cliCtx)).Methods("GET") 20 r.HandleFunc("/liquidity/add_quote/{token}", swapAddQuoteHandler(cliCtx)).Methods("GET") 21 r.HandleFunc("/liquidity/remove_quote/{token_pair}", queryRedeemableAssetsHandler(cliCtx)).Methods("GET") 22 r.HandleFunc("/quote/{token}", swapQuoteHandler(cliCtx)).Methods("GET") 23 } 24 25 func querySwapTokenPairHandler(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) { 26 return func(w http.ResponseWriter, r *http.Request) { 27 vars := mux.Vars(r) 28 tokenPairName := vars["name"] 29 res, _, err := cliContext.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QuerySwapTokenPair, tokenPairName), nil) 30 if err != nil { 31 sdkErr := common.ParseSDKError(err.Error()) 32 common.HandleErrorMsg(w, cliContext, sdkErr.Code, sdkErr.Message) 33 return 34 } 35 rest.PostProcessResponse(w, cliContext, res) 36 } 37 38 } 39 40 func queryParamsHandler(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) { 41 return func(w http.ResponseWriter, r *http.Request) { 42 43 res, _, err := cliContext.QueryWithData(fmt.Sprintf("custom/%s/params", types.QuerierRoute), nil) 44 if err != nil { 45 sdkErr := common.ParseSDKError(err.Error()) 46 common.HandleErrorMsg(w, cliContext, sdkErr.Code, sdkErr.Message) 47 return 48 } 49 50 formatAndReturnResult(w, cliContext, res) 51 } 52 53 } 54 55 func queryRedeemableAssetsHandler(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) { 56 return func(w http.ResponseWriter, r *http.Request) { 57 vars := mux.Vars(r) 58 tokenPair := vars["token_pair"] 59 liquidity := r.URL.Query().Get("liquidity") 60 res, _, err := cliContext.QueryWithData(fmt.Sprintf("custom/%s/%s/%s/%s", types.QuerierRoute, types.QueryRedeemableAssets, tokenPair, liquidity), nil) 61 if err != nil { 62 sdkErr := common.ParseSDKError(err.Error()) 63 common.HandleErrorMsg(w, cliContext, sdkErr.Code, sdkErr.Message) 64 return 65 } 66 formatAndReturnResult(w, cliContext, res) 67 } 68 69 } 70 71 func formatAndReturnResult(w http.ResponseWriter, cliContext context.CLIContext, data []byte) { 72 replaceStr := "replaceHere" 73 result := common.GetBaseResponse(replaceStr) 74 resultJson, err := json.Marshal(result) 75 if err != nil { 76 common.HandleErrorMsg(w, cliContext, common.CodeMarshalJSONFailed, err.Error()) 77 return 78 } 79 resultJson = []byte(strings.Replace(string(resultJson), "\""+replaceStr+"\"", string(data), 1)) 80 81 rest.PostProcessResponse(w, cliContext, resultJson) 82 } 83 84 func swapQuoteHandler(cliCtx context.CLIContext) http.HandlerFunc { 85 return func(w http.ResponseWriter, r *http.Request) { 86 vars := mux.Vars(r) 87 buyToken := vars["token"] 88 sellTokenAmount := r.URL.Query().Get("sell_token_amount") 89 90 params := types.NewQuerySwapBuyInfoParams(sellTokenAmount, buyToken) 91 bz, err := cliCtx.Codec.MarshalJSON(params) 92 if err != nil { 93 common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error()) 94 return 95 } 96 97 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySwapQuoteInfo), bz) 98 if err != nil { 99 sdkErr := common.ParseSDKError(err.Error()) 100 common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message) 101 return 102 } 103 104 rest.PostProcessResponse(w, cliCtx, res) 105 } 106 } 107 108 func swapAddQuoteHandler(cliCtx context.CLIContext) http.HandlerFunc { 109 return func(w http.ResponseWriter, r *http.Request) { 110 vars := mux.Vars(r) 111 baseToken := vars["token"] 112 quoteTokenAmount := r.URL.Query().Get("quote_token_amount") 113 114 params := types.NewQuerySwapAddInfoParams(quoteTokenAmount, baseToken) 115 bz, err := cliCtx.Codec.MarshalJSON(params) 116 if err != nil { 117 common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error()) 118 return 119 } 120 121 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySwapAddLiquidityQuote), bz) 122 if err != nil { 123 sdkErr := common.ParseSDKError(err.Error()) 124 common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message) 125 return 126 } 127 128 rest.PostProcessResponse(w, cliCtx, res) 129 } 130 }