Skip to content

Commit

Permalink
feat(evmstaking): using pubkey instead address (#249)
Browse files Browse the repository at this point in the history
- all staking and distribution API interface use pubkey instead of
address
- add get delegator info API interface

issue: none
  • Loading branch information
jhd2best authored Oct 20, 2024
1 parent 617fb3e commit a73c638
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 51 deletions.
81 changes: 63 additions & 18 deletions client/server/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ import (
"github.com/gorilla/mux"

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

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))
s.httpMux.HandleFunc("/distribution/validators/{validator_address}/slashes", utils.AutoWrap(s.aminoCodec, s.GetValidatorSlashesByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/commission", utils.SimpleWrap(s.aminoCodec, s.GetValidatorCommissionByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/outstanding_rewards", utils.SimpleWrap(s.aminoCodec, s.GetValidatorOutstandingRewardsByValidatorAddress))
s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/slashes", utils.AutoWrap(s.aminoCodec, s.GetValidatorSlashesByValidatorAddress))
s.httpMux.HandleFunc("/distribution/all_validators/outstanding_rewards", utils.AutoWrap(s.aminoCodec, s.GetAllValidatorOutstandingRewards))

s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/validators", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorsByDelegatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/rewards", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/rewards/{validator_address}", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddressValidatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/withdraw_address", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorWithdrawAddressByDelegatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/validators", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorsByDelegatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/rewards", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/rewards/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddressValidatorAddress))
s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/withdraw_address", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorWithdrawAddressByDelegatorAddress))
}

// GetDistributionParams queries params of the distribution module.
Expand All @@ -46,13 +47,18 @@ func (s *Server) GetDistributionParams(r *http.Request) (resp any, err error) {

// GetDistributionValidatorByValidatorAddress queries validator commission and self-delegation rewards for validator.
func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (resp any, err error) {
valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorDistributionInfo(queryContext, &distributiontypes.QueryValidatorDistributionInfoRequest{
ValidatorAddress: mux.Vars(r)["validator_address"],
ValidatorAddress: valAddr,
})
if err != nil {
return nil, err
Expand All @@ -63,13 +69,18 @@ func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (re

// GetValidatorCommissionByValidatorAddress queries accumulated commission for a validator.
func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp any, err error) {
valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorCommission(queryContext, &distributiontypes.QueryValidatorCommissionRequest{
ValidatorAddress: mux.Vars(r)["validator_address"],
ValidatorAddress: valAddr,
})

if err != nil {
Expand All @@ -81,13 +92,18 @@ func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp

// GetValidatorOutstandingRewardsByValidatorAddress queries rewards of a validator address.
func (s *Server) GetValidatorOutstandingRewardsByValidatorAddress(r *http.Request) (resp any, err error) {
valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorOutstandingRewards(queryContext, &distributiontypes.QueryValidatorOutstandingRewardsRequest{
ValidatorAddress: mux.Vars(r)["validator_address"],
ValidatorAddress: valAddr,
})

if err != nil {
Expand Down Expand Up @@ -140,13 +156,18 @@ func (s *Server) GetAllValidatorOutstandingRewards(req *getAllValidatorOutstandi

// GetValidatorSlashesByValidatorAddress queries slash events of a validator.
func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesByValidatorAddressRequest, r *http.Request) (resp any, err error) {
valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorSlashes(queryContext, &distributiontypes.QueryValidatorSlashesRequest{
ValidatorAddress: mux.Vars(r)["validator_address"],
ValidatorAddress: valAddr,
StartingHeight: req.StartingHeight,
EndingHeight: req.EndingHeight,
Pagination: &query.PageRequest{
Expand All @@ -167,13 +188,18 @@ func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesB

// GetDistributionValidatorsByDelegatorAddress queries the validators of a delegator.
func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (resp any, err error) {
delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegatorValidators(queryContext, &distributiontypes.QueryDelegatorValidatorsRequest{
DelegatorAddress: mux.Vars(r)["delegator_address"],
DelegatorAddress: delAddr,
})

if err != nil {
Expand All @@ -185,13 +211,18 @@ func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (r

// GetDelegatorRewardsByDelegatorAddress queries the total rewards accrued by each validator.
func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp any, err error) {
delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegationTotalRewards(queryContext, &distributiontypes.QueryDelegationTotalRewardsRequest{
DelegatorAddress: mux.Vars(r)["delegator_address"],
DelegatorAddress: delAddr,
})

if err != nil {
Expand All @@ -203,15 +234,24 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp an

// GetDelegatorRewardsByDelegatorAddressValidatorAddress queries the total rewards accrued by a delegation.
func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.Request) (resp any, err error) {
valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"])
if err != nil {
return nil, err
}

delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

muxVars := mux.Vars(r)
queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegationRewards(queryContext, &distributiontypes.QueryDelegationRewardsRequest{
DelegatorAddress: muxVars["delegator_address"],
ValidatorAddress: muxVars["validator_address"],
DelegatorAddress: delAddr,
ValidatorAddress: valAddr,
})

if err != nil {
Expand All @@ -223,13 +263,18 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.R

// GetDelegatorWithdrawAddressByDelegatorAddress queries withdraw address of a delegator.
func (s *Server) GetDelegatorWithdrawAddressByDelegatorAddress(r *http.Request) (resp any, err error) {
delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"])
if err != nil {
return nil, err
}

queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegatorWithdrawAddress(queryContext, &distributiontypes.QueryDelegatorWithdrawAddressRequest{
DelegatorAddress: mux.Vars(r)["delegator_address"],
DelegatorAddress: delAddr,
})

if err != nil {
Expand Down
Loading

0 comments on commit a73c638

Please sign in to comment.