Skip to content

Commit

Permalink
feat(api): add epochs api (#176)
Browse files Browse the repository at this point in the history
* feat(api): add epochs api

* feat(api): add epoch info api
  • Loading branch information
ezreal1997 authored Oct 9, 2024
1 parent 5e97596 commit 32308f1
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 180 deletions.
5 changes: 5 additions & 0 deletions client/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/piplabs/story/client/app/keepers"
"github.com/piplabs/story/client/comet"
epochskeeper "github.com/piplabs/story/client/x/epochs/keeper"
evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
mintkeeper "github.com/piplabs/story/client/x/mint/keeper"
"github.com/piplabs/story/lib/errors"
Expand Down Expand Up @@ -212,3 +213,7 @@ func (a App) GetUpgradeKeeper() *upgradekeeper.Keeper {
func (a App) GetMintKeeper() mintkeeper.Keeper {
return a.Keepers.MintKeeper
}

func (a App) GetEpochsKeeper() *epochskeeper.Keeper {
return a.Keepers.EpochsKeeper
}
48 changes: 48 additions & 0 deletions client/server/epochs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package server

import (
"net/http"

"github.com/gorilla/mux"

"github.com/piplabs/story/client/server/utils"
"github.com/piplabs/story/client/x/epochs/keeper"
epochstypes "github.com/piplabs/story/client/x/epochs/types"
)

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

// GetEpochInfos queries running epochInfos.
func (s *Server) GetEpochInfos(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()).GetEpochInfos(queryContext, &epochstypes.GetEpochInfosRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}

// 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()).GetEpochInfo(queryContext, &epochstypes.GetEpochInfoRequest{
Identifier: mux.Vars(r)["identifier"],
})
if err != nil {
return nil, err
}

return queryResp, nil
}
3 changes: 3 additions & 0 deletions client/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"

epochskeeper "github.com/piplabs/story/client/x/epochs/keeper"
evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
mintkeeper "github.com/piplabs/story/client/x/mint/keeper"
)
Expand All @@ -41,6 +42,7 @@ type Store interface {
GetDistrKeeper() distrkeeper.Keeper
GetUpgradeKeeper() *upgradekeeper.Keeper
GetMintKeeper() mintkeeper.Keeper
GetEpochsKeeper() *epochskeeper.Keeper
}

type Server struct {
Expand Down Expand Up @@ -124,6 +126,7 @@ func (s *Server) registerHandle() {
s.initStakingRoute()
s.initUpgradeRoute()
s.initMintRoute()
s.initEpochsRoute()
}

func (s *Server) createQueryContextByHeader(r *http.Request) (sdk.Context, error) {
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 32308f1

Please sign in to comment.