Skip to content

Commit

Permalink
feat(api): api server enhancement (#89)
Browse files Browse the repository at this point in the history
* feat(api): enhanced api for auth module

* feat(api): enhanced api for staking module

* feat(api): enhanced api for distribution module

* feat(api): enhanced api for bank module

* feat(api): enhanced api for slashing module

* feat(api): enhanced api for upgrade module

* feat(api): enhanced api for evmstaking module

* feat(api): simplify enhanced api

* feat(api): fix decoding of staking api

* feat(api): remove withdrawal queue query API
  • Loading branch information
ezreal1997 authored Sep 9, 2024
1 parent 9ae4a63 commit 108d58c
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 57 deletions.
10 changes: 10 additions & 0 deletions client/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

"github.com/piplabs/story/client/app/keepers"
"github.com/piplabs/story/client/comet"
evmstakingkeeper "github.com/piplabs/story/client/x/evmstaking/keeper"
"github.com/piplabs/story/lib/errors"
"github.com/piplabs/story/lib/ethclient"

Expand Down Expand Up @@ -177,10 +179,18 @@ func (a App) SetCometAPI(api comet.API) {
a.Keepers.EVMEngKeeper.SetCometAPI(api)
}

func (a App) GetEvmStakingKeeper() *evmstakingkeeper.Keeper {
return a.Keepers.EvmStakingKeeper
}

func (a App) GetStakingKeeper() *stakingkeeper.Keeper {
return a.Keepers.StakingKeeper
}

func (a App) GetSlashingKeeper() slashingkeeper.Keeper {
return a.Keepers.SlashingKeeper
}

func (a App) GetAccountKeeper() authkeeper.AccountKeeper {
return a.Keepers.AccountKeeper
}
Expand Down
38 changes: 23 additions & 15 deletions client/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,31 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/gorilla/mux"

"github.com/piplabs/story/client/server/utils"
)

func (s *Server) initAuthRoute() {
s.httpMux.HandleFunc("/auth/params", utils.SimpleWrap(s.aminoCodec, s.GetAuthParams))

s.httpMux.HandleFunc("/auth/accounts", utils.AutoWrap(s.aminoCodec, s.GetAccounts))
s.httpMux.HandleFunc("/auth/accounts/{address}", utils.SimpleWrap(s.aminoCodec, s.GetAccountsByAddress))

s.httpMux.HandleFunc("/auth/bech32", utils.SimpleWrap(s.aminoCodec, s.GetBech32Prefix))
}

// GetAuthParams queries all parameters of auth module.
func (s *Server) GetAuthParams(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQueryServer(s.store.GetAccountKeeper()).Params(queryContext, &authtypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}

// GetAccounts returns all the existing accounts.
Expand All @@ -39,31 +56,22 @@ func (s *Server) GetAccounts(req *getAccountsRequest, r *http.Request) (resp any
}

for _, account := range queryResp.Accounts {
err = s.prepareUnpackInterfaces(utils.WrapTypeAny[types.AccountI](account))
if err != nil {
if err := s.prepareUnpackInterfaces(utils.WrapTypeAny[types.AccountI](account)); err != nil {
return nil, err
}
}

return queryResp, nil
}

// GetAccountsByAddress returns account details based on address.
func (s *Server) GetAccountsByAddress(r *http.Request) (resp any, err error) {
// GetBech32Prefix queries bech32Prefix.
func (s *Server) GetBech32Prefix(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQueryServer(s.store.GetAccountKeeper()).Account(queryContext, &authtypes.QueryAccountRequest{
Address: mux.Vars(r)["address"],
})

if err != nil {
return nil, err
}

err = s.prepareUnpackInterfaces(utils.WrapTypeAny[types.AccountI](queryResp.Account))
queryResp, err := keeper.NewQueryServer(s.store.GetAccountKeeper()).Bech32Prefix(queryContext, &authtypes.Bech32PrefixRequest{})
if err != nil {
return nil, err
}
Expand Down
92 changes: 92 additions & 0 deletions client/server/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,54 @@ import (
)

func (s *Server) initBankRoute() {
s.httpMux.HandleFunc("/bank/params", utils.SimpleWrap(s.aminoCodec, s.GetBankParams))

s.httpMux.HandleFunc("/bank/supply", utils.AutoWrap(s.aminoCodec, s.GetSupply))
s.httpMux.HandleFunc("/bank/supply/by_denom", utils.AutoWrap(s.aminoCodec, s.GetSupplyByDenom))

s.httpMux.HandleFunc("/bank/balances/{address}", utils.AutoWrap(s.aminoCodec, s.GetBalancesByAddress))
s.httpMux.HandleFunc("/bank/balances/{address}/by_denom", utils.AutoWrap(s.aminoCodec, s.GetBalancesByAddressDenom))

s.httpMux.HandleFunc("/bank/denom_owners/{denom}", utils.AutoWrap(s.aminoCodec, s.GetDenomOwners))
s.httpMux.HandleFunc("/bank/denom_owners_by_query", utils.AutoWrap(s.aminoCodec, s.GetDenomOwnersByQuery))
}

// GetBankParams queries the parameters of x/bank module.
func (s *Server) GetBankParams(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().Params(queryContext, &banktypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}

// GetSupply queries the total supply of all coins.
func (s *Server) GetSupply(req *getSupplyRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().TotalSupply(queryContext, &banktypes.QueryTotalSupplyRequest{
Pagination: &query.PageRequest{
Key: []byte(req.Pagination.Key),
Offset: req.Pagination.Offset,
Limit: req.Pagination.Limit,
CountTotal: req.Pagination.CountTotal,
Reverse: req.Pagination.Reverse,
},
})
if err != nil {
return nil, err
}

return queryResp, err
}

// GetSupplyByDenom queries the supply of a single coin.
Expand Down Expand Up @@ -78,3 +122,51 @@ func (s *Server) GetBalancesByAddressDenom(req *getBalancesByAddressDenomRequest

return queryResp, nil
}

// GetDenomOwners queries for all account addresses that own a particular token denomination.
func (s *Server) GetDenomOwners(req *getDenomOwnersRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().DenomOwners(queryContext, &banktypes.QueryDenomOwnersRequest{
Denom: mux.Vars(r)["denom"],
Pagination: &query.PageRequest{
Key: []byte(req.Pagination.Key),
Offset: req.Pagination.Offset,
Limit: req.Pagination.Limit,
CountTotal: req.Pagination.CountTotal,
Reverse: req.Pagination.Reverse,
},
})
if err != nil {
return nil, err
}

return queryResp, err
}

// GetDenomOwnersByQuery queries for all account addresses that own a particular token denomination.
func (s *Server) GetDenomOwnersByQuery(req *getDenomOwnersByQueryRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().DenomOwnersByQuery(queryContext, &banktypes.QueryDenomOwnersByQueryRequest{
Denom: req.Denom,
Pagination: &query.PageRequest{
Key: []byte(req.Pagination.Key),
Offset: req.Pagination.Offset,
Limit: req.Pagination.Limit,
CountTotal: req.Pagination.CountTotal,
Reverse: req.Pagination.Reverse,
},
})
if err != nil {
return nil, err
}

return queryResp, err
}
18 changes: 17 additions & 1 deletion client/server/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

func (s *Server) initDistributionRoute() {
s.httpMux.HandleFunc("/distribution/params", utils.SimpleWrap(s.aminoCodec, s.GetDistributionParams))

s.httpMux.HandleFunc("/distribution/validators/{validator_address}", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_address}/commission", utils.SimpleWrap(s.aminoCodec, s.GetValidatorCommissionByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_address}/outstanding_rewards", utils.SimpleWrap(s.aminoCodec, s.GetValidatorOutstandingRewardsByValidatorAddress))
Expand All @@ -27,6 +29,21 @@ func (s *Server) initDistributionRoute() {
s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/withdraw_address", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorWithdrawAddressByDelegatorAddress))
}

// GetDistributionParams queries params of the distribution module.
func (s *Server) GetDistributionParams(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).Params(queryContext, &distributiontypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}

// GetDistributionValidatorByValidatorAddress queries validator commission and self-delegation rewards for validator.
func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
Expand All @@ -37,7 +54,6 @@ func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (re
queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorDistributionInfo(queryContext, &distributiontypes.QueryValidatorDistributionInfoRequest{
ValidatorAddress: mux.Vars(r)["validator_address"],
})

if err != nil {
return nil, err
}
Expand Down
27 changes: 27 additions & 0 deletions client/server/evmstaking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package server

import (
"net/http"

"github.com/piplabs/story/client/server/utils"
evmstakingtypes "github.com/piplabs/story/client/x/evmstaking/types"
)

func (s *Server) initEvmStakingRoute() {
s.httpMux.HandleFunc("/evmstaking/params", utils.SimpleWrap(s.aminoCodec, s.GetEvmStakingParams))
}

// GetEvmStakingParams queries the parameters of evmstaking module.
func (s *Server) GetEvmStakingParams(r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetEvmStakingKeeper().Params(queryContext, &evmstakingtypes.QueryParamsRequest{})
if err != nil {
return nil, err
}

return queryResp, nil
}
80 changes: 50 additions & 30 deletions client/server/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,11 @@ type pagination struct {
Reverse bool `mapstructure:"reverse"`
}

type getValidatorsRequest struct {
Status string `mapstructure:"status"`
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorDelegationsByValidatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getDelegationsByDelegatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getRedelegationsByDelegatorAddressRequest struct {
SrcValidatorAddr string `mapstructure:"src_validator_addr"`
DstValidatorAddr string `mapstructure:"dst_validator_addr"`
Pagination pagination `mapstructure:"pagination"`
}

type getUnbondingDelegationsByDelegatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorsByDelegatorAddressRequest struct {
type getAccountsRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getAccountsRequest struct {
type getSupplyRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

Expand All @@ -54,14 +31,16 @@ type getBalancesByAddressRequest struct {
}

type getBalancesByAddressDenomRequest struct {
Denom string `mapstructure:"denom"`
Denom string `mapstructure:"denom"`
}

type getDenomOwnersRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorSlashesByValidatorAddressRequest struct {
StartingHeight uint64 `mapstructure:"starting_height"`
EndingHeight uint64 `mapstructure:"ending_height"`
Pagination pagination `mapstructure:"pagination"`
type getDenomOwnersByQueryRequest struct {
Denom string `mapstructure:"denom"`
Pagination pagination `mapstructure:"pagination"`
}

type getComebftBlockEventsRequest struct {
Expand All @@ -85,6 +64,47 @@ type getAllValidatorOutstandingRewardsRequestBlockResults struct {
Validators map[string]sdk.DecCoins `json:"validators"`
}

type getValidatorSlashesByValidatorAddressRequest struct {
StartingHeight uint64 `mapstructure:"starting_height"`
EndingHeight uint64 `mapstructure:"ending_height"`
Pagination pagination `mapstructure:"pagination"`
}

type getSigningInfosRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorsRequest struct {
Status string `mapstructure:"status"`
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorDelegationsByValidatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorUnbondingDelegationsRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getDelegationsByDelegatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getRedelegationsByDelegatorAddressRequest struct {
SrcValidatorAddr string `mapstructure:"src_validator_addr"`
DstValidatorAddr string `mapstructure:"dst_validator_addr"`
Pagination pagination `mapstructure:"pagination"`
}

type getUnbondingDelegationsByDelegatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getValidatorsByDelegatorAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getModuleVersionsRequest struct {
ModuleName string `mapstructure:"module_name"`
}
Loading

0 comments on commit 108d58c

Please sign in to comment.