github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/dex/keeper/querier.go (about)

     1  package keeper
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/fibonacci-chain/fbc/x/dex/types"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    14  	"github.com/fibonacci-chain/fbc/x/common"
    15  )
    16  
    17  // NewQuerier is the module level router for state queries
    18  func NewQuerier(keeper IKeeper) sdk.Querier {
    19  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
    20  		switch path[0] {
    21  		case types.QueryProducts:
    22  			return queryProduct(ctx, req, keeper)
    23  		case types.QueryDeposits:
    24  			return queryDeposits(ctx, req, keeper)
    25  		case types.QueryMatchOrder:
    26  			return queryMatchOrder(ctx, req, keeper)
    27  		case types.QueryParameters:
    28  			return queryParams(ctx, req, keeper)
    29  		case types.QueryProductsDelisting:
    30  			return queryProductsDelisting(ctx, keeper)
    31  		case types.QueryOperator:
    32  			return queryOperator(ctx, req, keeper)
    33  		case types.QueryOperators:
    34  			return queryOperators(ctx, keeper)
    35  		default:
    36  			return nil, types.ErrDexUnknownQueryType()
    37  		}
    38  	}
    39  }
    40  
    41  func queryProduct(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
    42  	var params types.QueryDexInfoParams
    43  	errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
    44  	if errUnmarshal != nil {
    45  		return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
    46  	}
    47  
    48  	offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
    49  
    50  	if offset < 0 || limit < 0 {
    51  		return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
    52  	}
    53  
    54  	var tokenPairs []*types.TokenPair
    55  	if params.Owner != "" {
    56  		ownerAddr, err := sdk.AccAddressFromBech32(params.Owner)
    57  		if err != nil {
    58  			return nil, common.ErrCreateAddrFromBech32Failed(params.Owner, err.Error())
    59  		}
    60  
    61  		tokenPairs = keeper.GetUserTokenPairs(ctx, ownerAddr)
    62  	} else {
    63  		tokenPairs = keeper.GetTokenPairs(ctx)
    64  	}
    65  
    66  	// sort tokenPairs
    67  	sort.SliceStable(tokenPairs, func(i, j int) bool {
    68  		return tokenPairs[i].ID < tokenPairs[j].ID
    69  	})
    70  
    71  	total := len(tokenPairs)
    72  	switch {
    73  	case total < offset:
    74  		tokenPairs = tokenPairs[0:0]
    75  	case total < offset+limit:
    76  		tokenPairs = tokenPairs[offset:]
    77  	default:
    78  		tokenPairs = tokenPairs[offset : offset+limit]
    79  	}
    80  
    81  	var response *common.ListResponse
    82  	if len(tokenPairs) > 0 {
    83  		response = common.GetListResponse(total, params.Page, params.PerPage, tokenPairs)
    84  	} else {
    85  		response = common.GetEmptyListResponse(total, params.Page, params.PerPage)
    86  	}
    87  
    88  	res, errMarshal := json.MarshalIndent(response, "", "  ")
    89  	if errMarshal != nil {
    90  		return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
    91  	}
    92  	return res, nil
    93  
    94  }
    95  
    96  type depositsData struct {
    97  	ProductName     string         `json:"product"`
    98  	ProductDeposits sdk.SysCoin    `json:"deposits"`
    99  	Rank            int            `json:"rank"`
   100  	BlockHeight     int64          `json:"block_height"`
   101  	Owner           sdk.AccAddress `json:"owner"`
   102  }
   103  
   104  func queryDeposits(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
   105  	var params types.QueryDepositParams
   106  	errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
   107  	if errUnmarshal != nil {
   108  		return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
   109  	}
   110  
   111  	if params.Address == "" && params.BaseAsset == "" && params.QuoteAsset == "" {
   112  		return nil, types.ErrAddrAndProductAllRequired()
   113  	}
   114  
   115  	offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
   116  	if offset < 0 || limit < 0 {
   117  		return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
   118  	}
   119  
   120  	tokenPairs := keeper.GetTokenPairsOrdered(ctx)
   121  
   122  	var deposits []depositsData
   123  	for i, tokenPair := range tokenPairs {
   124  		if tokenPair == nil {
   125  			return nil, types.ErrTokenPairIsRequired()
   126  		}
   127  		// filter address
   128  		if params.Address != "" && tokenPair.Owner.String() != params.Address {
   129  			continue
   130  		}
   131  		// filter base asset
   132  		if params.BaseAsset != "" && !strings.Contains(tokenPair.BaseAssetSymbol, params.BaseAsset) {
   133  			continue
   134  		}
   135  		// filter quote asset
   136  		if params.QuoteAsset != "" && !strings.Contains(tokenPair.QuoteAssetSymbol, params.QuoteAsset) {
   137  			continue
   138  		}
   139  		deposits = append(deposits, depositsData{fmt.Sprintf("%s_%s", tokenPair.BaseAssetSymbol, tokenPair.QuoteAssetSymbol), tokenPair.Deposits, i + 1, tokenPair.BlockHeight, tokenPair.Owner})
   140  	}
   141  	total := len(deposits)
   142  
   143  	switch {
   144  	case total < offset:
   145  		deposits = deposits[0:0]
   146  	case total < offset+limit:
   147  		deposits = deposits[offset:]
   148  	default:
   149  		deposits = deposits[offset : offset+limit]
   150  	}
   151  
   152  	sort.SliceStable(deposits, func(i, j int) bool {
   153  		return deposits[i].ProductDeposits.IsLT(deposits[j].ProductDeposits)
   154  	})
   155  
   156  	var response *common.ListResponse
   157  	if total > 0 {
   158  		response = common.GetListResponse(total, params.Page, params.PerPage, deposits)
   159  	} else {
   160  		response = common.GetEmptyListResponse(total, params.Page, params.PerPage)
   161  	}
   162  
   163  	res, errMarshal := json.MarshalIndent(response, "", "  ")
   164  	if errMarshal != nil {
   165  		return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
   166  	}
   167  
   168  	return res, nil
   169  }
   170  
   171  func queryMatchOrder(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
   172  
   173  	var params types.QueryDexInfoParams
   174  	errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
   175  	if errUnmarshal != nil {
   176  		return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
   177  	}
   178  	offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
   179  
   180  	if offset < 0 || limit < 0 {
   181  		return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
   182  	}
   183  	tokenPairs := keeper.GetTokenPairsOrdered(ctx)
   184  
   185  	var products []string
   186  
   187  	for _, tokenPair := range tokenPairs {
   188  		if tokenPair == nil {
   189  			panic("the nil pointer is not expected")
   190  		}
   191  		products = append(products, fmt.Sprintf("%s_%s", tokenPair.BaseAssetSymbol, tokenPair.QuoteAssetSymbol))
   192  	}
   193  
   194  	switch {
   195  	case len(products) < offset:
   196  		products = products[0:0]
   197  	case len(products) < offset+limit:
   198  		products = products[offset:]
   199  	default:
   200  		products = products[offset : offset+limit]
   201  	}
   202  
   203  	res, errMarshal := codec.MarshalJSONIndent(types.ModuleCdc, products)
   204  
   205  	if errMarshal != nil {
   206  		return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
   207  	}
   208  	return res, nil
   209  
   210  }
   211  
   212  func queryParams(ctx sdk.Context, _ abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
   213  	params := keeper.GetParams(ctx)
   214  	res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, params)
   215  	if errUnmarshal != nil {
   216  		return nil, common.ErrMarshalJSONFailed(errUnmarshal.Error())
   217  	}
   218  	return res, nil
   219  }
   220  
   221  // queryProductsDelisting query the tokenpair name under dex delisting
   222  func queryProductsDelisting(ctx sdk.Context, keeper IKeeper) (res []byte, err sdk.Error) {
   223  	var tokenPairNames []string
   224  	tokenPairs := keeper.GetTokenPairs(ctx)
   225  	tokenPairLen := len(tokenPairs)
   226  	for i := 0; i < tokenPairLen; i++ {
   227  		if tokenPairs[i] == nil {
   228  			panic("the nil pointer is not expected")
   229  		}
   230  		if tokenPairs[i].Delisting {
   231  			tokenPairNames = append(tokenPairNames, fmt.Sprintf("%s_%s", tokenPairs[i].BaseAssetSymbol, tokenPairs[i].QuoteAssetSymbol))
   232  		}
   233  	}
   234  
   235  	res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, tokenPairNames)
   236  	if errUnmarshal != nil {
   237  		return nil, common.ErrMarshalJSONFailed(errUnmarshal.Error())
   238  	}
   239  
   240  	return res, nil
   241  }
   242  
   243  // nolint
   244  func queryOperator(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) ([]byte, sdk.Error) {
   245  	var params types.QueryDexOperatorParams
   246  	err := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
   247  	if err != nil {
   248  		return nil, common.ErrUnMarshalJSONFailed(err.Error())
   249  	}
   250  
   251  	operator, isExist := keeper.GetOperator(ctx, params.Addr)
   252  	if !isExist {
   253  		return nil, types.ErrUnknownOperator(params.Addr)
   254  	}
   255  
   256  	bz, err := codec.MarshalJSONIndent(types.ModuleCdc, operator)
   257  	if err != nil {
   258  		return nil, common.ErrMarshalJSONFailed(err.Error())
   259  	}
   260  	return bz, nil
   261  }
   262  
   263  // nolint
   264  func queryOperators(ctx sdk.Context, keeper IKeeper) ([]byte, sdk.Error) {
   265  	var operators types.DEXOperators
   266  	keeper.IterateOperators(ctx, func(operator types.DEXOperator) bool {
   267  		//info.HandlingFees = keeper.GetBankKeeper().GetCoins(ctx, info.HandlingFeeAddress).String()
   268  		operators = append(operators, operator)
   269  		return false
   270  	})
   271  
   272  	bz, err := codec.MarshalJSONIndent(types.ModuleCdc, operators)
   273  	if err != nil {
   274  		return nil, common.ErrMarshalJSONFailed(err.Error())
   275  	}
   276  	return bz, nil
   277  }