github.com/lino-network/lino@v0.6.11/x/bandwidth/querier.go (about) 1 package bandwidth 2 3 import ( 4 wire "github.com/cosmos/cosmos-sdk/codec" 5 sdk "github.com/cosmos/cosmos-sdk/types" 6 abci "github.com/tendermint/tendermint/abci/types" 7 8 linotypes "github.com/lino-network/lino/types" 9 "github.com/lino-network/lino/x/bandwidth/types" 10 ) 11 12 // creates a querier for account REST endpoints 13 func NewQuerier(bm BandwidthKeeper) sdk.Querier { 14 cdc := wire.New() 15 wire.RegisterCrypto(cdc) 16 return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) { 17 switch path[0] { 18 case types.QueryBandwidthInfo: 19 return queryBandwidthInfo(ctx, cdc, req, bm) 20 case types.QueryBlockInfo: 21 return queryBlockInfo(ctx, cdc, req, bm) 22 case types.QueryAppBandwidthInfo: 23 return queryAppBandwidthInfo(ctx, cdc, path[1:], req, bm) 24 default: 25 return nil, sdk.ErrUnknownRequest("unknown bandwidth query endpoint") 26 } 27 } 28 } 29 30 func queryBandwidthInfo(ctx sdk.Context, cdc *wire.Codec, req abci.RequestQuery, bm BandwidthKeeper) ([]byte, sdk.Error) { 31 bandwidthInfo, err := bm.GetBandwidthInfo(ctx) 32 if err != nil { 33 return nil, err 34 } 35 res, marshalErr := cdc.MarshalJSON(bandwidthInfo) 36 if marshalErr != nil { 37 return nil, types.ErrQueryFailed() 38 } 39 return res, nil 40 } 41 42 func queryBlockInfo(ctx sdk.Context, cdc *wire.Codec, req abci.RequestQuery, bm BandwidthKeeper) ([]byte, sdk.Error) { 43 blockInfo, err := bm.GetBlockInfo(ctx) 44 if err != nil { 45 return nil, err 46 } 47 res, marshalErr := cdc.MarshalJSON(blockInfo) 48 if marshalErr != nil { 49 return nil, types.ErrQueryFailed() 50 } 51 return res, nil 52 } 53 54 func queryAppBandwidthInfo(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, bm BandwidthKeeper) ([]byte, sdk.Error) { 55 if err := linotypes.CheckPathContentAndMinLength(path, 1); err != nil { 56 return nil, err 57 } 58 appInfo, err := bm.GetAppBandwidthInfo(ctx, linotypes.AccountKey(path[0])) 59 if err != nil { 60 return nil, err 61 } 62 res, marshalErr := cdc.MarshalJSON(appInfo) 63 if marshalErr != nil { 64 return nil, types.ErrQueryFailed() 65 } 66 return res, nil 67 }