github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/token/querier.go (about) 1 package token 2 3 import ( 4 "github.com/fibonacci-chain/fbc/x/common" 5 "github.com/fibonacci-chain/fbc/x/token/types" 6 7 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 10 "github.com/spf13/viper" 11 ) 12 13 // NewQuerier is the module level router for state queries 14 func NewQuerier(keeper Keeper) sdk.Querier { 15 return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) { 16 switch path[0] { 17 case types.QueryInfo: 18 return queryInfo(ctx, path[1:], req, keeper) 19 case types.QueryTokens: 20 return queryTokens(ctx, path[1:], req, keeper) 21 case types.QueryParameters: 22 return queryParameters(ctx, keeper) 23 case types.QueryCurrency: 24 return queryCurrency(ctx, path[1:], req, keeper) 25 case types.QueryAccount: 26 return queryAccount(ctx, path[1:], req, keeper) 27 case types.QueryKeysNum: 28 return queryKeysNum(ctx, keeper) 29 case types.QueryAccountV2: 30 return queryAccountV2(ctx, path[1:], req, keeper) 31 case types.QueryTokensV2: 32 return queryTokensV2(ctx, path[1:], req, keeper) 33 case types.QueryTokenV2: 34 return queryTokenV2(ctx, path[1:], req, keeper) 35 case types.UploadAccount: 36 return uploadAccount(ctx, keeper) 37 default: 38 return nil, types.ErrUnknownTokenQueryType() 39 } 40 } 41 } 42 43 // nolint: unparam 44 func queryInfo(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { 45 name := path[0] 46 47 token := keeper.GetTokenInfo(ctx, name) 48 if token.Symbol == "" { 49 return nil, types.ErrInvalidCoins(token.Symbol) 50 51 } 52 53 tokenResp := types.GenTokenResp(token) 54 tokenResp.TotalSupply = keeper.GetTokenTotalSupply(ctx, name) 55 bz, err := codec.MarshalJSONIndent(keeper.cdc, tokenResp) 56 if err != nil { 57 return nil, common.ErrMarshalJSONFailed(err.Error()) 58 } 59 return bz, nil 60 } 61 62 func queryTokens(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { 63 var tokens []types.Token 64 if len(path) > 0 && path[0] != "" { 65 ownerAddr, err := sdk.AccAddressFromBech32(path[0]) 66 if err != nil { 67 return nil, common.ErrCreateAddrFromBech32Failed(path[0], err.Error()) 68 } 69 tokens = keeper.GetUserTokensInfo(ctx, ownerAddr) 70 } else { 71 tokens = keeper.GetTokensInfo(ctx) 72 } 73 74 var tokensResp types.Tokens 75 for _, token := range tokens { 76 tokenResp := types.GenTokenResp(token) 77 tokenResp.TotalSupply = keeper.GetTokenTotalSupply(ctx, token.Symbol) 78 tokensResp = append(tokensResp, tokenResp) 79 } 80 bz, err := codec.MarshalJSONIndent(keeper.cdc, tokensResp) 81 if err != nil { 82 return nil, common.ErrMarshalJSONFailed(err.Error()) 83 } 84 return bz, nil 85 } 86 87 func queryCurrency(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { 88 tokens := keeper.GetCurrenciesInfo(ctx) 89 90 bz, err := codec.MarshalJSONIndent(keeper.cdc, tokens) 91 if err != nil { 92 return nil, common.ErrMarshalJSONFailed(err.Error()) 93 } 94 return bz, nil 95 } 96 97 func queryAccount(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) { 98 addr, err := sdk.AccAddressFromBech32(path[0]) 99 if err != nil { 100 return nil, common.ErrCreateAddrFromBech32Failed(path[0], err.Error()) 101 } 102 103 //var queryPage QueryPage 104 var accountParam types.AccountParam 105 //var symbol string 106 err = codec.Cdc.UnmarshalJSON(req.Data, &accountParam) 107 if err != nil { 108 return nil, common.ErrUnMarshalJSONFailed(err.Error()) 109 } 110 111 coinsInfo := keeper.GetCoinsInfo(ctx, addr) 112 coinsInfoChoosen := make([]types.CoinInfo, 0) 113 if accountParam.Symbol == "" { 114 coinsInfoChoosen = coinsInfo 115 116 // show all or partial 117 if accountParam.Show == "all" { 118 tokens := keeper.GetTokensInfo(ctx) 119 120 for _, token := range tokens { 121 found := false 122 for _, coinInfo := range coinsInfo { 123 if coinInfo.Symbol == token.Symbol { 124 found = true 125 break 126 } 127 } 128 // not found 129 if !found { 130 ci := types.NewCoinInfo(token.Symbol, "0", "0") 131 coinsInfoChoosen = append(coinsInfoChoosen, *ci) 132 } 133 } 134 } 135 // page and pageSize 136 //coinsInfoChoosen = coinsInfoChoosen[Min((accountParam.Page-1)*accountParam.PerPage, len(coinsInfoChoosen)):Min(accountParam.Page*accountParam.PerPage, len(coinsInfoChoosen))] 137 } else { 138 for _, coinInfo := range coinsInfo { 139 if coinInfo.Symbol == accountParam.Symbol { 140 coinsInfoChoosen = append(coinsInfoChoosen, coinInfo) 141 } 142 } 143 } 144 accountResponse := types.NewAccountResponse(path[0]) 145 accountResponse.Currencies = coinsInfoChoosen 146 147 bz, err := codec.MarshalJSONIndent(keeper.cdc, accountResponse) 148 if err != nil { 149 return nil, common.ErrMarshalJSONFailed(err.Error()) 150 } 151 return bz, nil 152 } 153 154 func queryParameters(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) { 155 params := keeper.GetParams(ctx) 156 res, err := codec.MarshalJSONIndent(keeper.cdc, params) 157 if err != nil { 158 return nil, common.ErrMarshalJSONFailed(err.Error()) 159 } 160 return res, nil 161 } 162 163 func queryKeysNum(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) { 164 tokenStoreKeyNum, lockStoreKeyNum := keeper.getNumKeys(ctx) 165 res, err := codec.MarshalJSONIndent(keeper.cdc, 166 map[string]int64{"token": tokenStoreKeyNum, 167 "lock": lockStoreKeyNum}) 168 if err != nil { 169 return nil, common.ErrMarshalJSONFailed(err.Error()) 170 } 171 return res, nil 172 } 173 174 func uploadAccount(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) { 175 if !viper.GetBool(FlagOSSEnable) { 176 return []byte("This API is not enabled"), nil 177 } 178 // Note: very time-consuming 179 filePath := exportAccounts(ctx, keeper) 180 if filePath == "" { 181 return 182 } 183 uploadOSS(filePath) 184 185 return []byte("Complete the Export account data and Upload it to oss"), nil 186 }