|
5 | 5 | "net/http" |
6 | 6 | "strconv" |
7 | 7 |
|
| 8 | + "cosmossdk.io/math" |
| 9 | + |
| 10 | + sdk "github.com/cosmos/cosmos-sdk/types" |
8 | 11 | "github.com/cosmos/cosmos-sdk/types/query" |
9 | 12 | "github.com/cosmos/cosmos-sdk/x/staking/keeper" |
10 | 13 | stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
@@ -35,6 +38,7 @@ func (s *Server) initStakingRoute() { |
35 | 38 | s.httpMux.HandleFunc("/staking/delegators/{delegator_address}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetUnbondingDelegationsByDelegatorAddress)) |
36 | 39 | s.httpMux.HandleFunc("/staking/delegators/{delegator_address}/validators", utils.AutoWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddress)) |
37 | 40 | s.httpMux.HandleFunc("/staking/delegators/{delegator_address}/validators/{validator_address}", utils.SimpleWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddressValidatorAddress)) |
| 41 | + s.httpMux.HandleFunc("/staking/delegators/{delegator_address}/total_rewards_token", utils.SimpleWrap(s.aminoCodec, s.GetTotalRewardsTokenByDelegatorAddress)) |
38 | 42 | } |
39 | 43 |
|
40 | 44 | // GetStakingParams queries the staking parameters. |
@@ -624,6 +628,64 @@ func (s *Server) GetValidatorsByDelegatorAddressValidatorAddress(r *http.Request |
624 | 628 | return queryResp, nil |
625 | 629 | } |
626 | 630 |
|
| 631 | +type QueryTotalRewardsTokenByDelegatorAddressResponse struct { |
| 632 | + RewardsToken math.LegacyDec `json:"rewards_token"` |
| 633 | +} |
| 634 | + |
| 635 | +func (s *Server) GetTotalRewardsTokenByDelegatorAddress(r *http.Request) (resp any, err error) { |
| 636 | + queryContext, err := s.createQueryContextByHeader(r) |
| 637 | + if err != nil { |
| 638 | + return nil, err |
| 639 | + } |
| 640 | + |
| 641 | + bech32AccAddress, err := utils.EvmAddressToBech32AccAddress(mux.Vars(r)["delegator_address"]) |
| 642 | + if err != nil { |
| 643 | + return nil, err |
| 644 | + } |
| 645 | + |
| 646 | + var nextKey []byte |
| 647 | + totalRewardsToken := math.LegacyZeroDec() |
| 648 | + |
| 649 | + for { |
| 650 | + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).DelegatorDelegations(queryContext, &stakingtypes.QueryDelegatorDelegationsRequest{ |
| 651 | + DelegatorAddr: bech32AccAddress.String(), |
| 652 | + Pagination: &query.PageRequest{ |
| 653 | + Key: nextKey, |
| 654 | + Limit: 100, |
| 655 | + CountTotal: false, |
| 656 | + }, |
| 657 | + }) |
| 658 | + if err != nil { |
| 659 | + return nil, err |
| 660 | + } |
| 661 | + |
| 662 | + for _, delResp := range queryResp.DelegationResponses { |
| 663 | + valAddr, err := sdk.ValAddressFromBech32(delResp.Delegation.ValidatorAddress) |
| 664 | + if err != nil { |
| 665 | + return nil, err |
| 666 | + } |
| 667 | + |
| 668 | + val, err := keeper.NewQuerier(s.store.GetStakingKeeper()).GetValidator(queryContext, valAddr) |
| 669 | + if err != nil { |
| 670 | + return nil, err |
| 671 | + } |
| 672 | + |
| 673 | + rewardsToken := val.RewardsTokensFromRewardsShares(delResp.Delegation.RewardsShares) |
| 674 | + totalRewardsToken = totalRewardsToken.Add(rewardsToken) |
| 675 | + } |
| 676 | + |
| 677 | + if queryResp.Pagination == nil || len(queryResp.Pagination.NextKey) == 0 { |
| 678 | + break |
| 679 | + } |
| 680 | + |
| 681 | + nextKey = queryResp.Pagination.NextKey |
| 682 | + } |
| 683 | + |
| 684 | + return QueryTotalRewardsTokenByDelegatorAddressResponse{ |
| 685 | + RewardsToken: totalRewardsToken, |
| 686 | + }, nil |
| 687 | +} |
| 688 | + |
627 | 689 | // GetPeriodDelegations queries period delegations info for given validator delegator pair. |
628 | 690 | func (s *Server) GetPeriodDelegations(req *getPeriodDelegationsRequest, r *http.Request) (resp any, err error) { |
629 | 691 | queryContext, err := s.createQueryContextByHeader(r) |
|
0 commit comments