Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: faulty GetValidator helper function in Unstake CLI #1762

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading