Skip to content

Commit

Permalink
fix GetValidator so it'll fetch delegated validators (#1762)
Browse files Browse the repository at this point in the history
  • Loading branch information
oren-lava authored Oct 29, 2024
1 parent 831d379 commit 0e9c830
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
39 changes: 16 additions & 23 deletions x/dualstaking/client/cli/tx_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,30 @@ func CmdDelegate() *cobra.Command {
return cmd
}

// GetValidator gets a validator that is delegated by the `from` address
// The dualstaking mecahnism makes providers delegate to a validator when they're staking.
// Assuming that the `from` address (of the clientCtx) is a staked provider address, this
// function returns a validator that the provider is delegated to (the one with the largest delegation).
func GetValidator(clientCtx client.Context) string {
provider := clientCtx.GetFromAddress().String()
q := stakingtypes.NewQueryClient(clientCtx)
ctx := context.Background()
resD, err := q.DelegatorValidators(ctx, &stakingtypes.QueryDelegatorValidatorsRequest{DelegatorAddr: provider})
resD, err := q.DelegatorDelegations(ctx, &stakingtypes.QueryDelegatorDelegationsRequest{DelegatorAddr: provider})

if err == nil && len(resD.Validators) > 0 {
validatorBiggest := resD.Validators[0]
for _, validator := range resD.Validators {
if sdk.AccAddress(validator.OperatorAddress).String() == provider {
return validator.OperatorAddress
if err == nil && len(resD.DelegationResponses) > 0 {
delegationBiggest := resD.DelegationResponses[0]
for _, delegationResponse := range resD.DelegationResponses {
if sdk.AccAddress(delegationResponse.Delegation.ValidatorAddress).String() == provider {
return delegationResponse.Delegation.ValidatorAddress
}
if validator.Tokens.GT(validatorBiggest.Tokens) {
validatorBiggest = validator
if delegationResponse.Balance.IsGTE(delegationBiggest.Balance) {
delegationBiggest = delegationResponse
}
}
return validatorBiggest.OperatorAddress
}

resV, err := q.Validators(ctx, &stakingtypes.QueryValidatorsRequest{})
if err != nil {
panic("failed to fetch list of validators")
}
validatorBiggest := resV.Validators[0]
for _, validator := range resV.Validators {
if sdk.AccAddress(validator.OperatorAddress).String() == provider {
return validator.OperatorAddress
}
if validator.Tokens.GT(validatorBiggest.Tokens) {
validatorBiggest = validator
if !delegationBiggest.Balance.IsZero() {
return delegationBiggest.Delegation.ValidatorAddress
}
}
return validatorBiggest.OperatorAddress

return ""
}
4 changes: 4 additions & 0 deletions x/pairing/client/cli/tx_unstake_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -43,6 +44,9 @@ func CmdUnstakeProvider() *cobra.Command {
} else {
validator = dualstakingclient.GetValidator(clientCtx)
}
if validator == "" {
return fmt.Errorf("cannot unstake, the provider is not delegated to any validator.\nthe provider might have all zero delegations, to resolve please delegated to one of the validators a small amount")
}

msgs := []sdk.Msg{}
for _, chainID := range chainIDs {
Expand Down

0 comments on commit 0e9c830

Please sign in to comment.