github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/keeper/grpc_query.go (about)

     1  package keeper
     2  
     3  // DONTCOVER
     4  // client is excluded from test coverage in the poc phase milestone 1 and will be included in milestone 2 with completeness
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/cosmos/cosmos-sdk/store/prefix"
    10  	sdk "github.com/cosmos/cosmos-sdk/types"
    11  	"github.com/cosmos/cosmos-sdk/types/query"
    12  	"google.golang.org/grpc/codes"
    13  	"google.golang.org/grpc/status"
    14  
    15  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    16  )
    17  
    18  // Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper.
    19  type Querier struct {
    20  	Keeper
    21  }
    22  
    23  var _ types.QueryServer = Querier{}
    24  
    25  // LiquidityPool queries a liquidity pool with the given pool id.
    26  func (k Querier) LiquidityPool(c context.Context, req *types.QueryLiquidityPoolRequest) (*types.QueryLiquidityPoolResponse, error) {
    27  	empty := &types.QueryLiquidityPoolRequest{}
    28  	if req == nil || *req == *empty {
    29  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
    30  	}
    31  
    32  	ctx := sdk.UnwrapSDKContext(c)
    33  
    34  	pool, found := k.GetPool(ctx, req.PoolId)
    35  	if !found {
    36  		return nil, status.Errorf(codes.NotFound, "liquidity pool %d doesn't exist", req.PoolId)
    37  	}
    38  
    39  	return k.MakeQueryLiquidityPoolResponse(pool)
    40  }
    41  
    42  // LiquidityPool queries a liquidity pool with the given pool coin denom.
    43  func (k Querier) LiquidityPoolByPoolCoinDenom(c context.Context, req *types.QueryLiquidityPoolByPoolCoinDenomRequest) (*types.QueryLiquidityPoolResponse, error) {
    44  	empty := &types.QueryLiquidityPoolByPoolCoinDenomRequest{}
    45  	if req == nil || *req == *empty {
    46  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
    47  	}
    48  	ctx := sdk.UnwrapSDKContext(c)
    49  	reserveAcc, err := types.GetReserveAcc(req.PoolCoinDenom, false)
    50  	if err != nil {
    51  		return nil, status.Errorf(codes.NotFound, "liquidity pool with pool coin denom %s doesn't exist", req.PoolCoinDenom)
    52  	}
    53  	pool, found := k.GetPoolByReserveAccIndex(ctx, reserveAcc)
    54  	if !found {
    55  		return nil, status.Errorf(codes.NotFound, "liquidity pool with pool coin denom %s doesn't exist", req.PoolCoinDenom)
    56  	}
    57  	return k.MakeQueryLiquidityPoolResponse(pool)
    58  }
    59  
    60  // LiquidityPool queries a liquidity pool with the given reserve account address.
    61  func (k Querier) LiquidityPoolByReserveAcc(c context.Context, req *types.QueryLiquidityPoolByReserveAccRequest) (*types.QueryLiquidityPoolResponse, error) {
    62  	empty := &types.QueryLiquidityPoolByReserveAccRequest{}
    63  	if req == nil || *req == *empty {
    64  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
    65  	}
    66  	ctx := sdk.UnwrapSDKContext(c)
    67  	reserveAcc, err := sdk.AccAddressFromBech32(req.ReserveAcc)
    68  	if err != nil {
    69  		return nil, status.Errorf(codes.NotFound, "the reserve account address %s is not valid", req.ReserveAcc)
    70  	}
    71  	pool, found := k.GetPoolByReserveAccIndex(ctx, reserveAcc)
    72  	if !found {
    73  		return nil, status.Errorf(codes.NotFound, "liquidity pool with pool reserve account %s doesn't exist", req.ReserveAcc)
    74  	}
    75  	return k.MakeQueryLiquidityPoolResponse(pool)
    76  }
    77  
    78  // LiquidityPoolBatch queries a liquidity pool batch with the given pool id.
    79  func (k Querier) LiquidityPoolBatch(c context.Context, req *types.QueryLiquidityPoolBatchRequest) (*types.QueryLiquidityPoolBatchResponse, error) {
    80  	empty := &types.QueryLiquidityPoolBatchRequest{}
    81  	if req == nil || *req == *empty {
    82  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
    83  	}
    84  
    85  	ctx := sdk.UnwrapSDKContext(c)
    86  
    87  	batch, found := k.GetPoolBatch(ctx, req.PoolId)
    88  	if !found {
    89  		return nil, status.Errorf(codes.NotFound, "liquidity pool batch %d doesn't exist", req.PoolId)
    90  	}
    91  
    92  	return &types.QueryLiquidityPoolBatchResponse{
    93  		Batch: batch,
    94  	}, nil
    95  }
    96  
    97  // Pools queries all liquidity pools currently existed with each liquidity pool with batch and metadata.
    98  func (k Querier) LiquidityPools(c context.Context, req *types.QueryLiquidityPoolsRequest) (*types.QueryLiquidityPoolsResponse, error) {
    99  	ctx := sdk.UnwrapSDKContext(c)
   100  
   101  	store := ctx.KVStore(k.storeKey)
   102  	poolStore := prefix.NewStore(store, types.PoolKeyPrefix)
   103  
   104  	var pools types.Pools
   105  
   106  	pageRes, err := query.Paginate(poolStore, req.Pagination, func(key []byte, value []byte) error {
   107  		pool, err := types.UnmarshalPool(k.cdc, value)
   108  		if err != nil {
   109  			return err
   110  		}
   111  		pools = append(pools, pool)
   112  		return nil
   113  	})
   114  
   115  	if err != nil {
   116  		return nil, status.Error(codes.Internal, err.Error())
   117  	}
   118  
   119  	if len(pools) == 0 {
   120  		return nil, status.Error(codes.NotFound, "There are no pools present.")
   121  	}
   122  
   123  	return &types.QueryLiquidityPoolsResponse{
   124  		Pools:      pools,
   125  		Pagination: pageRes,
   126  	}, nil
   127  }
   128  
   129  // PoolBatchSwapMsg queries the pool batch swap message with the message index of the liquidity pool.
   130  func (k Querier) PoolBatchSwapMsg(c context.Context, req *types.QueryPoolBatchSwapMsgRequest) (*types.QueryPoolBatchSwapMsgResponse, error) {
   131  	empty := &types.QueryPoolBatchSwapMsgRequest{}
   132  	if req == nil || *req == *empty {
   133  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   134  	}
   135  
   136  	ctx := sdk.UnwrapSDKContext(c)
   137  
   138  	msg, found := k.GetPoolBatchSwapMsgState(ctx, req.PoolId, req.MsgIndex)
   139  	if !found {
   140  		return nil, status.Errorf(codes.NotFound, "the msg given msg_index %d doesn't exist or deleted", req.MsgIndex)
   141  	}
   142  
   143  	return &types.QueryPoolBatchSwapMsgResponse{
   144  		Swap: msg,
   145  	}, nil
   146  }
   147  
   148  // PoolBatchSwapMsgs queries all pool batch swap messages of the liquidity pool.
   149  func (k Querier) PoolBatchSwapMsgs(c context.Context, req *types.QueryPoolBatchSwapMsgsRequest) (*types.QueryPoolBatchSwapMsgsResponse, error) {
   150  	empty := &types.QueryPoolBatchSwapMsgsRequest{}
   151  	if req == nil || *req == *empty {
   152  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   153  	}
   154  
   155  	ctx := sdk.UnwrapSDKContext(c)
   156  
   157  	_, found := k.GetPool(ctx, req.PoolId)
   158  	if !found {
   159  		return nil, status.Errorf(codes.NotFound, "liquidity pool %d doesn't exist", req.PoolId)
   160  	}
   161  
   162  	store := ctx.KVStore(k.storeKey)
   163  	msgStore := prefix.NewStore(store, types.GetPoolBatchSwapMsgStatesPrefix(req.PoolId))
   164  
   165  	var msgs []types.SwapMsgState
   166  
   167  	pageRes, err := query.Paginate(msgStore, req.Pagination, func(key []byte, value []byte) error {
   168  		msg, err := types.UnmarshalSwapMsgState(k.cdc, value)
   169  		if err != nil {
   170  			return err
   171  		}
   172  
   173  		msgs = append(msgs, msg)
   174  
   175  		return nil
   176  	})
   177  
   178  	if err != nil {
   179  		return nil, status.Error(codes.Internal, err.Error())
   180  	}
   181  
   182  	return &types.QueryPoolBatchSwapMsgsResponse{
   183  		Swaps:      msgs,
   184  		Pagination: pageRes,
   185  	}, nil
   186  }
   187  
   188  // PoolBatchDepositMsg queries the pool batch deposit message with the msg_index of the liquidity pool.
   189  func (k Querier) PoolBatchDepositMsg(c context.Context, req *types.QueryPoolBatchDepositMsgRequest) (*types.QueryPoolBatchDepositMsgResponse, error) {
   190  	empty := &types.QueryPoolBatchDepositMsgRequest{}
   191  	if req == nil || *req == *empty {
   192  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   193  	}
   194  
   195  	ctx := sdk.UnwrapSDKContext(c)
   196  
   197  	msg, found := k.GetPoolBatchDepositMsgState(ctx, req.PoolId, req.MsgIndex)
   198  	if !found {
   199  		return nil, status.Errorf(codes.NotFound, "the msg given msg_index %d doesn't exist or deleted", req.MsgIndex)
   200  	}
   201  
   202  	return &types.QueryPoolBatchDepositMsgResponse{
   203  		Deposit: msg,
   204  	}, nil
   205  }
   206  
   207  // PoolBatchDepositMsgs queries all pool batch deposit messages of the liquidity pool.
   208  func (k Querier) PoolBatchDepositMsgs(c context.Context, req *types.QueryPoolBatchDepositMsgsRequest) (*types.QueryPoolBatchDepositMsgsResponse, error) {
   209  	empty := &types.QueryPoolBatchDepositMsgsRequest{}
   210  	if req == nil || *req == *empty {
   211  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   212  	}
   213  
   214  	ctx := sdk.UnwrapSDKContext(c)
   215  
   216  	_, found := k.GetPool(ctx, req.PoolId)
   217  	if !found {
   218  		return nil, status.Errorf(codes.NotFound, "liquidity pool %d doesn't exist", req.PoolId)
   219  	}
   220  
   221  	store := ctx.KVStore(k.storeKey)
   222  	msgStore := prefix.NewStore(store, types.GetPoolBatchDepositMsgStatesPrefix(req.PoolId))
   223  	var msgs []types.DepositMsgState
   224  
   225  	pageRes, err := query.Paginate(msgStore, req.Pagination, func(key []byte, value []byte) error {
   226  		msg, err := types.UnmarshalDepositMsgState(k.cdc, value)
   227  		if err != nil {
   228  			return err
   229  		}
   230  
   231  		msgs = append(msgs, msg)
   232  
   233  		return nil
   234  	})
   235  
   236  	if err != nil {
   237  		return nil, status.Error(codes.Internal, err.Error())
   238  	}
   239  
   240  	return &types.QueryPoolBatchDepositMsgsResponse{
   241  		Deposits:   msgs,
   242  		Pagination: pageRes,
   243  	}, nil
   244  }
   245  
   246  // PoolBatchWithdrawMsg queries the pool batch withdraw message with the msg_index of the liquidity pool.
   247  func (k Querier) PoolBatchWithdrawMsg(c context.Context, req *types.QueryPoolBatchWithdrawMsgRequest) (*types.QueryPoolBatchWithdrawMsgResponse, error) {
   248  	empty := &types.QueryPoolBatchWithdrawMsgRequest{}
   249  	if req == nil || *req == *empty {
   250  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   251  	}
   252  
   253  	ctx := sdk.UnwrapSDKContext(c)
   254  
   255  	msg, found := k.GetPoolBatchWithdrawMsgState(ctx, req.PoolId, req.MsgIndex)
   256  	if !found {
   257  		return nil, status.Errorf(codes.NotFound, "the msg given msg_index %d doesn't exist or deleted", req.MsgIndex)
   258  	}
   259  
   260  	return &types.QueryPoolBatchWithdrawMsgResponse{
   261  		Withdraw: msg,
   262  	}, nil
   263  }
   264  
   265  // PoolBatchWithdrawMsgs queries all pool batch withdraw messages of the liquidity pool.
   266  func (k Querier) PoolBatchWithdrawMsgs(c context.Context, req *types.QueryPoolBatchWithdrawMsgsRequest) (*types.QueryPoolBatchWithdrawMsgsResponse, error) {
   267  	empty := &types.QueryPoolBatchWithdrawMsgsRequest{}
   268  	if req == nil || *req == *empty {
   269  		return nil, status.Errorf(codes.InvalidArgument, "empty request")
   270  	}
   271  
   272  	ctx := sdk.UnwrapSDKContext(c)
   273  
   274  	_, found := k.GetPool(ctx, req.PoolId)
   275  	if !found {
   276  		return nil, status.Errorf(codes.NotFound, "liquidity pool %d doesn't exist", req.PoolId)
   277  	}
   278  
   279  	store := ctx.KVStore(k.storeKey)
   280  	msgStore := prefix.NewStore(store, types.GetPoolBatchWithdrawMsgsPrefix(req.PoolId))
   281  	var msgs []types.WithdrawMsgState
   282  
   283  	pageRes, err := query.Paginate(msgStore, req.Pagination, func(key []byte, value []byte) error {
   284  		msg, err := types.UnmarshalWithdrawMsgState(k.cdc, value)
   285  		if err != nil {
   286  			return err
   287  		}
   288  
   289  		msgs = append(msgs, msg)
   290  
   291  		return nil
   292  	})
   293  
   294  	if err != nil {
   295  		return nil, status.Error(codes.Internal, err.Error())
   296  	}
   297  
   298  	return &types.QueryPoolBatchWithdrawMsgsResponse{
   299  		Withdraws:  msgs,
   300  		Pagination: pageRes,
   301  	}, nil
   302  }
   303  
   304  // Params queries params of liquidity module.
   305  func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
   306  	ctx := sdk.UnwrapSDKContext(c)
   307  	params := k.GetParams(ctx)
   308  
   309  	return &types.QueryParamsResponse{
   310  		Params: params,
   311  	}, nil
   312  }
   313  
   314  // MakeQueryLiquidityPoolResponse wraps MakeQueryLiquidityPoolResponse.
   315  func (k Querier) MakeQueryLiquidityPoolResponse(pool types.Pool) (*types.QueryLiquidityPoolResponse, error) {
   316  	return &types.QueryLiquidityPoolResponse{
   317  		Pool: pool,
   318  	}, nil
   319  }
   320  
   321  // MakeQueryLiquidityPoolsResponse wraps a list of QueryLiquidityPoolResponses.
   322  func (k Querier) MakeQueryLiquidityPoolsResponse(pools types.Pools) (*[]types.QueryLiquidityPoolResponse, error) {
   323  	resp := make([]types.QueryLiquidityPoolResponse, len(pools))
   324  	for i, pool := range pools {
   325  		res := types.QueryLiquidityPoolResponse{
   326  			Pool: pool,
   327  		}
   328  		resp[i] = res
   329  	}
   330  	return &resp, nil
   331  }