clone_cw_multi_test/wasm_emulation/query/
staking.rs

1use crate::wasm_emulation::query::gas::{
2    GAS_COST_ALL_DELEGATIONS, GAS_COST_ALL_VALIDATORS, GAS_COST_BONDED_DENOM, GAS_COST_DELEGATIONS,
3    GAS_COST_VALIDATOR,
4};
5use crate::wasm_emulation::query::mock_querier::QueryResultWithGas;
6use cosmwasm_std::Binary;
7use cosmwasm_vm::GasInfo;
8
9use cosmwasm_std::to_json_binary;
10use cosmwasm_std::{
11    AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, DelegationResponse,
12    FullDelegation, StakingQuery, Validator, ValidatorResponse,
13};
14use cosmwasm_std::{ContractResult, SystemResult};
15
16use serde::{Deserialize, Serialize};
17
18#[derive(Serialize, Deserialize, Debug, Clone, Default)]
19pub struct StakingQuerier {
20    denom: String,
21    validators: Vec<Validator>,
22    delegations: Vec<FullDelegation>,
23}
24
25impl StakingQuerier {
26    pub fn new(denom: &str, validators: &[Validator], delegations: &[FullDelegation]) -> Self {
27        StakingQuerier {
28            denom: denom.to_string(),
29            validators: validators.to_vec(),
30            delegations: delegations.to_vec(),
31        }
32    }
33
34    pub fn query(&self, request: &StakingQuery) -> QueryResultWithGas {
35        let contract_result: ContractResult<Binary> = match request {
36            StakingQuery::BondedDenom {} => {
37                let res = BondedDenomResponse::new(self.denom.clone());
38                to_json_binary(&res).into()
39            }
40            StakingQuery::AllValidators {} => {
41                let res = AllValidatorsResponse::new(self.validators.clone());
42                to_json_binary(&res).into()
43            }
44            StakingQuery::Validator { address } => {
45                let validator: Option<Validator> = self
46                    .validators
47                    .iter()
48                    .find(|validator| validator.address == *address)
49                    .cloned();
50                let res = ValidatorResponse::new(validator);
51                to_json_binary(&res).into()
52            }
53            StakingQuery::AllDelegations { delegator } => {
54                let delegations: Vec<_> = self
55                    .delegations
56                    .iter()
57                    .filter(|d| d.delegator.as_str() == delegator)
58                    .cloned()
59                    .map(|d| d.into())
60                    .collect();
61                let res = AllDelegationsResponse::new(delegations);
62                to_json_binary(&res).into()
63            }
64            StakingQuery::Delegation {
65                delegator,
66                validator,
67            } => {
68                let delegation = self
69                    .delegations
70                    .iter()
71                    .find(|d| d.delegator.as_str() == delegator && d.validator == *validator);
72                let res = DelegationResponse::new(delegation.cloned());
73                to_json_binary(&res).into()
74            }
75            &_ => panic!("Not implemented {:?}", request),
76        };
77
78        // We handle the gas_info
79        let gas_info = match request {
80            StakingQuery::BondedDenom { .. } => GAS_COST_BONDED_DENOM,
81            StakingQuery::AllValidators { .. } => GAS_COST_ALL_VALIDATORS,
82            StakingQuery::Validator { .. } => GAS_COST_VALIDATOR,
83            StakingQuery::AllDelegations { .. } => GAS_COST_ALL_DELEGATIONS,
84            StakingQuery::Delegation { .. } => GAS_COST_DELEGATIONS,
85            &_ => panic!("Not implemented {:?}", request),
86        };
87
88        // system result is always ok in the mock implementation
89        (
90            SystemResult::Ok(contract_result),
91            GasInfo::with_externally_used(gas_info),
92        )
93    }
94}