Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8beb6d7

Browse files
committed
add pools query and rest sample
1 parent 62526d0 commit 8beb6d7

File tree

16 files changed

+209
-66
lines changed

16 files changed

+209
-66
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Usage:
107107
108108
109109
Available Commands:
110-
create-liquidity-pool Create Liquidity pool with the specified pool-type, deposit coins
110+
create-pool Create Liquidity pool with the specified pool-type, deposit coins
111111
112112
*WIP, More will soon be added.*
113113
@@ -131,7 +131,7 @@ Usage:
131131
liquidityd query liquidity [command]
132132
133133
Available Commands:
134-
liquidity-pool Query details of a liquidity pool
134+
pool Query details of a liquidity pool
135135
params Query the current liquidity parameters information
136136
*WIP, More will soon be added.*
137137

doc/client.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Usage:
4646
4747
4848
Available Commands:
49-
create-liquidity-pool Create Liquidity pool with the specified pool-type, deposit coins
49+
create-pool Create Liquidity pool with the specified pool-type, deposit coins
5050
5151
*WIP, More will soon be added.*
5252
@@ -59,27 +59,27 @@ Global Flags:
5959
--trace print out full stack trace on errors
6060
```
6161
62-
`$ liquidityd tx liquidity create-liquidity-pool --help`
62+
`$ liquidityd tx liquidity create-pool --help`
6363
```
6464
Create Liquidity pool with the specified pool-type-index, deposit coins for reserve
6565
6666
Example:
67-
$ liquidity tx liquidity create-liquidity-pool 1 100000000acoin,100000000bcoin --from mykey
67+
$ liquidity tx liquidity create-pool 1 100000000acoin,100000000bcoin --from mykey
6868
6969
Currently, only the default pool-type-index 1 is available
7070
the number of deposit coins must be two in the pool-type-index 1
7171
7272
{"pool_type_index":1,"name":"ConstantProductLiquidityPool","min_reserve_coin_num":2,"max_reserve_coin_num":2,"description":""}
7373
7474
Usage:
75-
liquidityd tx liquidity create-liquidity-pool [pool-type-index] [deposit-coins] [flags]
75+
liquidityd tx liquidity create-pool [pool-type-index] [deposit-coins] [flags]
7676
```
7777
78-
### create-liquidity-pool
78+
### create-pool
7979
8080
example tx command with result
8181
82-
`$ liquidityd tx liquidity create-liquidity-pool 1 100000000reservecoin1,100000000reservecoin2 --from node0 --home ./output/node0/liquidityd/ --fees 2stake --chain-id chain-3MYSLc`
82+
`$ liquidityd tx liquidity create-pool 1 100000000reservecoin1,100000000reservecoin2 --from node0 --home ./output/node0/liquidityd/ --fees 2stake --chain-id chain-3MYSLc`
8383
8484
```json
8585
{
@@ -252,7 +252,7 @@ Usage:
252252
liquidityd query liquidity [command]
253253
254254
Available Commands:
255-
liquidity-pool Query details of a liquidity pool
255+
pool Query details of a liquidity pool
256256
params Query the current liquidity parameters information
257257
*WIP, More will soon be added.*
258258
@@ -267,18 +267,18 @@ Global Flags:
267267
Use "liquidityd query liquidity [command] --help" for more information about a command.
268268
```
269269
270-
`$ ./liquidityd query liquidity liquidity-pool --help `
270+
`$ ./liquidityd query liquidity pool --help `
271271
```
272272
Query details of a liquidity pool
273273
Example:
274-
$ liquidity query liquidity liquidity-pool 1
274+
$ liquidity query liquidity pool 1
275275
276276
Usage:
277-
liquidityd query liquidity liquidity-pool [pool-id] [flags]
277+
liquidityd query liquidity pool [pool-id] [flags]
278278
279279
Flags:
280280
--height int Use a specific height to query state at (this can error if the node is pruning state)
281-
-h, --help help for liquidity-pool
281+
-h, --help help for pool
282282
--node string <host>:<port> to Tendermint RPC interface for this chain (default "tcp://localhost:26657")
283283
-o, --output string Output format (text|json) (default "text")
284284
@@ -289,11 +289,11 @@ Global Flags:
289289
--trace print out full stack trace on errors
290290
291291
```
292-
### liquidity-pool
292+
### pool
293293
294294
example query command with result
295295
296-
`./liquidityd query liquidity liquidity-pool 1`
296+
`./liquidityd query liquidity pool 1`
297297
298298
```bash
299299
pool_coin_denom: cosmos1qz38nymksetqd2d4qesrxpffzywuel82a4l0vs
@@ -388,7 +388,7 @@ swap_fee_rate: "0.003000000000000000"
388388
389389
`cat output/node0/liquidityd/config/genesis.json | grep chain_id`
390390
391-
`./liquidityd tx liquidity create-liquidity-pool 1 100000000reservecoin1,100000000reservecoin2 --from node0 --home ./output/node0/liquidityd/ --fees 2stake --chain-id <CHAIN-ID>`
391+
`./liquidityd tx liquidity create-pool 1 100000000reservecoin1,100000000reservecoin2 --from node0 --home ./output/node0/liquidityd/ --fees 2stake --chain-id <CHAIN-ID>`
392392
393393
`./liquidityd export --home ./output/node0/liquidityd/`
394394

x/liquidity/client/cli/query.go

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,61 @@ func GetQueryCmd() *cobra.Command {
2727
}
2828

2929
liquidityQueryCmd.AddCommand(
30-
GetCmdQueryLiquidityPool(),
3130
GetCmdQueryParams(),
31+
GetCmdQueryLiquidityPool(),
32+
GetCmdQueryLiquidityPools(),
3233
)
3334

3435
return liquidityQueryCmd
3536
}
3637

38+
//GetCmdQueryParams implements the params query command.
39+
func GetCmdQueryParams() *cobra.Command {
40+
cmd := &cobra.Command{
41+
Use: "params",
42+
Args: cobra.NoArgs,
43+
Short: "Query the current liquidity parameters information",
44+
Long: strings.TrimSpace(
45+
fmt.Sprintf(`Query values set as liquidity parameters.
46+
47+
Example:
48+
$ %s query liquidity params
49+
`,
50+
version.AppName,
51+
),
52+
),
53+
RunE: func(cmd *cobra.Command, args []string) error {
54+
clientCtx := client.GetClientContextFromCmd(cmd)
55+
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
56+
if err != nil {
57+
return err
58+
}
59+
60+
queryClient := types.NewQueryClient(clientCtx)
61+
62+
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
63+
if err != nil {
64+
return err
65+
}
66+
67+
return clientCtx.PrintOutput(&res.Params)
68+
},
69+
}
70+
71+
flags.AddQueryFlagsToCmd(cmd)
72+
73+
return cmd
74+
}
75+
3776
func GetCmdQueryLiquidityPool() *cobra.Command {
3877
cmd := &cobra.Command{
39-
Use: "liquidity-pool [pool-id]",
78+
Use: "pool [pool-id]",
4079
Args: cobra.ExactArgs(1),
4180
Short: "Query details of a liquidity pool",
4281
Long: strings.TrimSpace(
4382
fmt.Sprintf(`Query details of a liquidity pool
4483
Example:
45-
$ %s query liquidity liquidity-pool 1
84+
$ %s query liquidity pool 1
4685
`,
4786
version.AppName,
4887
),
@@ -93,17 +132,15 @@ $ %s query liquidity liquidity-pool 1
93132
return cmd
94133
}
95134

96-
//GetCmdQueryParams implements the params query command.
97-
func GetCmdQueryParams() *cobra.Command {
135+
func GetCmdQueryLiquidityPools() *cobra.Command {
98136
cmd := &cobra.Command{
99-
Use: "params",
137+
Use: "pools",
100138
Args: cobra.NoArgs,
101-
Short: "Query the current liquidity parameters information",
139+
Short: "Query for all liquidity pools",
102140
Long: strings.TrimSpace(
103-
fmt.Sprintf(`Query values set as liquidity parameters.
104-
141+
fmt.Sprintf(`Query details about all liquidity pools on a network.
105142
Example:
106-
$ %s query liquidity params
143+
$ %s query liquidity pools
107144
`,
108145
version.AppName,
109146
),
@@ -116,16 +153,17 @@ $ %s query liquidity params
116153
}
117154

118155
queryClient := types.NewQueryClient(clientCtx)
119-
120-
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
156+
pageReq, err := client.ReadPageRequest(cmd.Flags())
121157
if err != nil {
122158
return err
123159
}
124-
125-
return clientCtx.PrintOutput(&res.Params)
160+
result, err := queryClient.LiquidityPools(context.Background(), &types.QueryLiquidityPoolsRequest{Pagination: pageReq})
161+
if err != nil {
162+
return err
163+
}
164+
return clientCtx.PrintOutput(result)
126165
},
127166
}
128-
129167
flags.AddQueryFlagsToCmd(cmd)
130168

131169
return cmd

x/liquidity/client/cli/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func GetTxCmd() *cobra.Command {
3535

3636
func NewCreateLiquidityPoolCmd() *cobra.Command {
3737
cmd := &cobra.Command{
38-
Use: "create-liquidity-pool [pool-type-index] [deposit-coins]",
38+
Use: "create-pool [pool-type-index] [deposit-coins]",
3939
Args: cobra.ExactArgs(2),
4040
Short: "Create Liquidity pool with the specified pool-type, deposit coins",
4141
Long: strings.TrimSpace(
4242
fmt.Sprintf(`Create Liquidity pool with the specified pool-type-index, deposit coins for reserve
4343
4444
Example:
45-
$ %s tx liquidity create-liquidity-pool 1 100000000acoin,100000000bcoin --from mykey
45+
$ %s tx liquidity create-pool 1 100000000acoin,100000000bcoin --from mykey
4646
4747
Currently, only the default pool-type-index 1 is available
4848
the number of deposit coins must be two in the pool-type-index 1

x/liquidity/client/rest/query.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@ import (
1616
// TODO: Plans to increase completeness on Milestone 2
1717

1818
func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
19-
// query liquidity
20-
//r.HandleFunc(fmt.Sprintf("/liquidity/pool/{%s}", RestPoolId), queryLiquidityHandlerFn(cliCtx)).Methods("GET")
19+
// Get the liquidity pool
20+
r.HandleFunc(
21+
fmt.Sprintf("/liquidity/pool/{%s}", RestPoolId),
22+
queryLiquidityPoolHandlerFn(cliCtx),
23+
).Methods("GET")
24+
25+
// Get all liquidity pools
26+
r.HandleFunc(
27+
"/liquidity/pools",
28+
queryLiquidityPoolsHandlerFn(cliCtx),
29+
).Methods("GET")
2130
}
2231

2332
// HTTP request handler to query liquidity information.
24-
func queryLiquidityHandlerFn(cliCtx client.Context) http.HandlerFunc {
33+
func queryLiquidityPoolHandlerFn(cliCtx client.Context) http.HandlerFunc {
2534
return func(w http.ResponseWriter, r *http.Request) {
2635
vars := mux.Vars(r)
2736
strPoolId := vars[RestPoolId]
@@ -45,14 +54,52 @@ func queryLiquidityHandlerFn(cliCtx client.Context) http.HandlerFunc {
4554
}
4655

4756
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLiquidityPool)
48-
fmt.Println(route)
4957
res, height, err := cliCtx.QueryWithData(route, bz)
50-
if err != nil {
51-
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
58+
if rest.CheckInternalServerError(w, err) {
5259
return
5360
}
61+
//if err != nil {
62+
// rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
63+
// return
64+
//}
5465

5566
cliCtx = cliCtx.WithHeight(height)
5667
rest.PostProcessResponse(w, cliCtx, res)
5768
}
5869
}
70+
71+
// HTTP request handler to query list of validators
72+
func queryLiquidityPoolsHandlerFn(clientCtx client.Context) http.HandlerFunc {
73+
return func(w http.ResponseWriter, r *http.Request) {
74+
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
75+
fmt.Println(page, limit, err)
76+
if rest.CheckBadRequestError(w, err) {
77+
return
78+
}
79+
80+
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
81+
if !ok {
82+
return
83+
}
84+
85+
params := types.NewQueryLiquidityPoolsParams(page, limit)
86+
87+
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)
88+
fmt.Println(bz, err)
89+
if rest.CheckBadRequestError(w, err) {
90+
return
91+
}
92+
93+
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLiquidityPools)
94+
95+
res, height, err := clientCtx.QueryWithData(route, bz)
96+
97+
fmt.Println(route, res, height, err)
98+
if rest.CheckInternalServerError(w, err) {
99+
return
100+
}
101+
102+
clientCtx = clientCtx.WithHeight(height)
103+
rest.PostProcessResponse(w, clientCtx, res)
104+
}
105+
}

x/liquidity/client/rest/rest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ const (
1717

1818
// RegisterHandlers registers asset-related REST handlers to a router
1919
func RegisterHandlers(cliCtx client.Context, r *mux.Router) {
20-
//registerQueryRoutes(cliCtx, r)
20+
registerQueryRoutes(cliCtx, r)
2121
//registerTxRoutes(cliCtx, r)
2222
}

x/liquidity/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestMsgServerGetLiquidityPoolMetaData(t *testing.T) {
217217
err = simapp.LiquidityKeeper.CreateLiquidityPool(ctx, msg)
218218
require.Error(t, err, types.ErrPoolAlreadyExists)
219219

220-
metaData := simapp.LiquidityKeeper.GetLiquidityPoolMetaData(ctx, lpList[0])
220+
metaData := simapp.LiquidityKeeper.GetPoolMetaData(ctx, lpList[0])
221221
require.Equal(t, lpList[0].PoolId, metaData.PoolId)
222222

223223
reserveCoin := simapp.LiquidityKeeper.GetReserveCoins(ctx, lpList[0])

0 commit comments

Comments
 (0)