clone_cw_multi_test/queries/
wasm.rs

1use std::collections::HashMap;
2
3use anyhow::Result as AnyResult;
4use cosmwasm_std::{Addr, Binary, CodeInfoResponse, CustomQuery, Order, Storage};
5use cw_orch::daemon::queriers::CosmWasm;
6
7use crate::{
8    prefixed_storage::prefixed_read,
9    wasm::{ContractData, CONTRACTS, NAMESPACE_WASM},
10    wasm_emulation::{channel::RemoteChannel, input::WasmStorage, query::AllWasmQuerier},
11    WasmKeeper,
12};
13
14pub struct WasmRemoteQuerier;
15
16impl WasmRemoteQuerier {
17    pub fn code_info(remote: RemoteChannel, code_id: u64) -> AnyResult<CodeInfoResponse> {
18        let wasm_querier = CosmWasm::new_sync(remote.channel, &remote.rt);
19
20        let code_info = remote.rt.block_on(wasm_querier._code(code_id))?;
21        Ok(code_info)
22    }
23
24    pub fn load_distant_contract(remote: RemoteChannel, address: &Addr) -> AnyResult<ContractData> {
25        let wasm_querier = CosmWasm::new_sync(remote.channel, &remote.rt);
26
27        let code_info = remote.rt.block_on(wasm_querier._contract_info(address))?;
28
29        Ok(ContractData {
30            admin: code_info.admin.map(Addr::unchecked),
31            code_id: code_info.code_id,
32            creator: Addr::unchecked(code_info.creator),
33        })
34    }
35
36    pub fn raw_query(
37        remote: RemoteChannel,
38        contract_addr: &Addr,
39        key: Binary,
40    ) -> AnyResult<Vec<u8>> {
41        let wasm_querier = CosmWasm::new_sync(remote.channel, &remote.rt);
42        let query_result = remote
43            .rt
44            .block_on(wasm_querier._contract_raw_state(contract_addr, key.to_vec()))
45            .map(|query_result| query_result.data);
46        Ok(query_result?)
47    }
48}
49
50impl<ExecC, QueryC: CustomQuery> AllWasmQuerier for WasmKeeper<ExecC, QueryC> {
51    fn query_all(&self, storage: &dyn Storage) -> AnyResult<WasmStorage> {
52        let all_local_state: Vec<_> = storage.range(None, None, Order::Ascending).collect();
53
54        let contracts = CONTRACTS
55            .range(
56                &prefixed_read(storage, NAMESPACE_WASM),
57                None,
58                None,
59                Order::Ascending,
60            )
61            .map(|res| match res {
62                Ok((key, value)) => Ok((key.to_string(), value)),
63                Err(e) => Err(e),
64            })
65            .collect::<Result<HashMap<_, _>, _>>()?;
66
67        Ok(WasmStorage {
68            contracts,
69            storage: all_local_state,
70            codes: self.code_base.borrow().clone(),
71            code_data: self.code_data.clone(),
72        })
73    }
74}