Skip to content

Commit

Permalink
feat(api): add epoch info api
Browse files Browse the repository at this point in the history
  • Loading branch information
ezreal1997 committed Oct 9, 2024
1 parent ce83f0a commit 1e6148f
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 185 deletions.
10 changes: 5 additions & 5 deletions client/server/epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func (s *Server) initEpochsRoute() {
s.httpMux.HandleFunc("/epochs/epoch_infos", utils.SimpleWrap(s.aminoCodec, s.GetEpochInfos))
s.httpMux.HandleFunc("/epochs/current_epoch/{identifier}", utils.SimpleWrap(s.aminoCodec, s.GetCurrentEpoch))
s.httpMux.HandleFunc("/epochs/epoch_infos/{identifier}", utils.SimpleWrap(s.aminoCodec, s.GetEpochInfo))
}

// GetEpochInfos queries running epochInfos.
Expand All @@ -22,22 +22,22 @@ func (s *Server) GetEpochInfos(r *http.Request) (resp any, err error) {
return nil, err
}

queryResp, err := keeper.NewQuerier(*s.store.GetEpochsKeeper()).EpochInfos(queryContext, &epochstypes.QueryEpochsInfoRequest{})
queryResp, err := keeper.NewQuerier(*s.store.GetEpochsKeeper()).GetEpochInfos(queryContext, &epochstypes.GetEpochInfosRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}

// GetCurrentEpoch queries current epoch of specified identifier.
func (s *Server) GetCurrentEpoch(r *http.Request) (resp any, err error) {
// GetEpochInfo queries epoch info of specified identifier.
func (s *Server) GetEpochInfo(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(*s.store.GetEpochsKeeper()).CurrentEpoch(queryContext, &epochstypes.QueryCurrentEpochRequest{
queryResp, err := keeper.NewQuerier(*s.store.GetEpochsKeeper()).GetEpochInfo(queryContext, &epochstypes.GetEpochInfoRequest{
Identifier: mux.Vars(r)["identifier"],
})
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions client/x/epochs/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ 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) {
// GetEpochInfos provide running epochInfos.
func (q Querier) GetEpochInfos(ctx context.Context, _ *types.GetEpochInfosRequest) (*types.GetEpochInfosResponse, error) {
epochs, err := q.Keeper.AllEpochInfos(ctx)
return &types.QueryEpochsInfoResponse{
return &types.GetEpochInfosResponse{
Epochs: epochs,
}, err
}

// CurrentEpoch provides current epoch of specified identifier.
func (q Querier) CurrentEpoch(ctx context.Context, req *types.QueryCurrentEpochRequest) (*types.QueryCurrentEpochResponse, error) {
// GetEpochInfo provide epoch info of specified identifier.
func (q Querier) GetEpochInfo(ctx context.Context, req *types.GetEpochInfoRequest) (*types.GetEpochInfoResponse, 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)
info, err := q.Keeper.GetEpochInfo(ctx, req.Identifier)
if err != nil {
return nil, errors.New("not available identifier")
}

return &types.QueryCurrentEpochResponse{
CurrentEpoch: info.CurrentEpoch,
return &types.GetEpochInfoResponse{
Epoch: info,
}, nil
}
2 changes: 1 addition & 1 deletion client/x/epochs/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (s *KeeperTestSuite) TestQueryEpochInfos() {
queryClient := s.queryClient

// Check that querying epoch infos on default genesis returns the default genesis epoch infos
epochInfosResponse, err := queryClient.EpochInfos(s.Ctx, &types.QueryEpochsInfoRequest{})
epochInfosResponse, err := queryClient.GetEpochInfos(s.Ctx, &types.GetEpochInfosRequest{})
s.Require().NoError(err)
s.Require().Len(epochInfosResponse.Epochs, 4)
expectedEpochs := types.DefaultGenesis().Epochs
Expand Down
Loading

0 comments on commit 1e6148f

Please sign in to comment.