CosmWasm / cw-plus

Production Quality contracts under open source licenses
Apache License 2.0
504 stars 353 forks source link

How to query validator data in `cosmwasm` contract. #890

Open 99Kies opened 8 months ago

99Kies commented 8 months ago

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

99Kies commented 8 months ago

I think cosmwasm need add more infomation about validator data.

https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L96-L137

https://github.com/CosmWasm/cosmwasm/blob/main/packages/std/src/query/staking.rs#L171-L186

99Kies commented 8 months ago

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/wasmd@v0.43.0/x/wasm/keeper/keeper.go:395] With gas wanted: '18446744073709551615' and gas used: '102538' : unknown request