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

How to query validator data in cosmwasm contract. #890

Open
99Kies opened this issue Jan 5, 2024 · 2 comments
Open

How to query validator data in cosmwasm contract. #890

99Kies opened this issue Jan 5, 2024 · 2 comments

Comments

@99Kies
Copy link

99Kies commented Jan 5, 2024

I want to query validator data in cosmwasm contract, what should I do? (like: delegation,stake,voting_power...data(validator-set))

@99Kies
Copy link
Author

99Kies commented Jan 5, 2024

@99Kies
Copy link
Author

99Kies commented Jan 6, 2024

In order to query more information about the data (tokens, delegate_shares), I modified validatorsResponse, but this doesn't get me the data I need, why is that?

code:

// msg.rs

/// Instances are created in the querier.
#[cw_serde]
pub struct Validator {
    /// The operator address of the validator (e.g. cosmosvaloper1...).
    /// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96
    /// for more information.
    ///
    /// This uses `String` instead of `Addr` since the bech32 address prefix is different from
    /// the ones that regular user accounts use.
    pub address: String,
    pub commission: Decimal,
    pub max_commission: Decimal,
    /// The maximum daily increase of the commission
    pub max_change_rate: Decimal,
    pub tokens: String,
    pub delegator_shares: String,
}

/// The data format returned from StakingRequest::AllValidators query
#[cw_serde]
pub struct AllValidatorsResponseBak {
    pub validators: Vec<Validator>,
}


// contracts.rs
pub fn execute_test(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    // beneficiaries: Vec<BeneficiaryBody>,
) -> Result<Response, ContractError> {
    let vals = deps.querier.query_all_validators()?;

    let request = StakingQuery::AllValidators {}.into();
    let data: AllValidatorsResponseBak = deps.querier.query(&request)?;
    let res_data = data.validators;
    Ok(Response::new()
        .add_attribute("action", "add_whitelists")
        .add_attribute("vals", format!("{:?}", vals))
        .add_attribute("vals", format!("{:?}", res_data)))
}

log:

Error: rpc error: code = Unknown desc = rpc error: code = Unknown desc = failed to execute message; message index: 0: Error parsing into type cw_vesting::msg::AllValidatorsResponseBak: missing field `tokens`: execute wasm contract failed [!cosm!wasm/[email protected]/x/wasm/keeper/keeper.go:395] With gas wanted: '18446744073709551615' and gas used: '102538' : unknown request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant