cw_orch_clone_testing/queriers/
wasm.rs1use std::marker::PhantomData;
2use std::{cell::RefCell, rc::Rc};
3
4use crate::{core::CloneTestingApp, CloneTesting};
5use clone_cw_multi_test::AddressGenerator;
6use clone_cw_multi_test::CosmosRouter;
7use cosmwasm_std::{instantiate2_address, Addr, Api, Checksum, ContractInfoResponse};
8use cw_orch_core::{
9 contract::interface_traits::{ContractInstance, Uploadable},
10 environment::{Querier, QuerierGetter, StateInterface, WasmQuerier},
11 CwEnvError,
12};
13use serde::{de::DeserializeOwned, Serialize};
14use sha2::{Digest, Sha256};
15pub struct CloneWasmQuerier<S> {
16 app: Rc<RefCell<CloneTestingApp>>,
17 _state: PhantomData<S>,
18}
19
20impl<S: StateInterface> CloneWasmQuerier<S> {
21 fn new(mock: &CloneTesting<S>) -> Self {
22 Self {
23 app: mock.app.clone(),
24 _state: PhantomData,
25 }
26 }
27}
28
29impl<S> Querier for CloneWasmQuerier<S> {
30 type Error = CwEnvError;
31}
32
33impl<S: StateInterface> QuerierGetter<CloneWasmQuerier<S>> for CloneTesting<S> {
34 fn querier(&self) -> CloneWasmQuerier<S> {
35 CloneWasmQuerier::new(self)
36 }
37}
38
39impl<S: StateInterface> WasmQuerier for CloneWasmQuerier<S> {
40 type Chain = CloneTesting<S>;
41 fn code_id_hash(&self, code_id: u64) -> Result<Checksum, CwEnvError> {
42 let code_info = self.app.borrow().wrap().query_wasm_code_info(code_id)?;
43 Ok(code_info.checksum)
44 }
45
46 fn contract_info(&self, address: &Addr) -> Result<ContractInfoResponse, CwEnvError> {
48 let info = self.app.borrow().wrap().query_wasm_contract_info(address)?;
49 Ok(info)
50 }
51
52 fn local_hash<T: Uploadable + ContractInstance<Self::Chain>>(
53 &self,
54 contract: &T,
55 ) -> Result<Checksum, CwEnvError> {
56 let hash: [u8; 32] = Sha256::digest(contract.id()).into();
59 Ok(hash.into())
60 }
61
62 fn raw_query(&self, address: &Addr, query_data: Vec<u8>) -> Result<Vec<u8>, Self::Error> {
63 let block = self.app.borrow().block_info();
64 Ok(self
65 .app
66 .borrow()
67 .read_module(|router, api, storage| {
68 router.query(
69 api,
70 storage,
71 &block,
72 cosmwasm_std::QueryRequest::Wasm(cosmwasm_std::WasmQuery::Raw {
73 contract_addr: address.into(),
74 key: query_data.into(),
75 }),
76 )
77 })?
78 .as_slice()
79 .to_vec())
80 }
81
82 fn smart_query<Q, T>(&self, address: &Addr, query_data: &Q) -> Result<T, Self::Error>
83 where
84 T: DeserializeOwned,
85 Q: Serialize,
86 {
87 Ok(self
88 .app
89 .borrow()
90 .wrap()
91 .query_wasm_smart(address, query_data)?)
92 }
93
94 fn code(&self, code_id: u64) -> Result<cosmwasm_std::CodeInfoResponse, Self::Error> {
95 Ok(self
96 .app
97 .borrow()
98 .wrap()
99 .query(&cosmwasm_std::QueryRequest::Wasm(
100 cosmwasm_std::WasmQuery::CodeInfo { code_id },
101 ))?)
102 }
103
104 fn instantiate2_addr(
105 &self,
106 code_id: u64,
107 creator: &Addr,
108 salt: cosmwasm_std::Binary,
109 ) -> Result<String, Self::Error> {
110 let checksum = self.code_id_hash(code_id)?;
112 let canon_creator = self
113 .app
114 .borrow()
115 .api()
116 .addr_canonicalize(creator.as_str())?;
117 let canonical_addr = instantiate2_address(checksum.as_slice(), &canon_creator, &salt)?;
118 Ok(self
119 .app
120 .borrow()
121 .api()
122 .addr_humanize(&canonical_addr)?
123 .to_string())
124 }
125}
126
127impl<S> AddressGenerator for CloneWasmQuerier<S> {}