cosmos-sdk/x/epochs/keeper/grpc_query.go
Hieu Vu 1028e27f79
feat(x/epochs): upstream osmosis epoch module (#19697)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
2024-04-04 08:10:25 +00:00

52 lines
1.3 KiB
Go

package keeper
import (
"context"
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cosmossdk.io/x/epochs/types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the x/epochs keeper providing gRPC method
// handlers.
type Querier struct {
Keeper
}
// NewQuerier initializes new querier.
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}
// EpochInfos provide running epochInfos.
func (q Querier) EpochInfos(ctx context.Context, _ *types.QueryEpochsInfoRequest) (*types.QueryEpochsInfoResponse, error) {
epochs, err := q.Keeper.AllEpochInfos(ctx)
return &types.QueryEpochsInfoResponse{
Epochs: epochs,
}, err
}
// CurrentEpoch provides current epoch of specified identifier.
func (q Querier) CurrentEpoch(ctx context.Context, req *types.QueryCurrentEpochRequest) (*types.QueryCurrentEpochResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.Identifier == "" {
return nil, status.Error(codes.InvalidArgument, "identifier is empty")
}
info, err := q.Keeper.EpochInfo.Get(ctx, req.Identifier)
if err != nil {
return nil, errors.New("not available identifier")
}
return &types.QueryCurrentEpochResponse{
CurrentEpoch: info.CurrentEpoch,
}, nil
}