Skip to content

Commit

Permalink
Fix/querier error to anyhow (#362)
Browse files Browse the repository at this point in the history
* Added first steps

* Fix querier error types

* Clippy
  • Loading branch information
Kayanski authored Apr 9, 2024
1 parent ab461e6 commit 845707c
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ mod tests {
t.instantiate(0, &Empty {}, None, None, &[])?;
Ok(())
}

#[test]
fn tx_handler_error_usable_on_anyhow() -> anyhow::Result<()> {
associated_error(MockHandler {})?;
Expand Down
179 changes: 177 additions & 2 deletions packages/cw-orch-core/src/environment/queriers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod wasm;
/// This trait acts as the high-level trait bound for supported queries on a `CwEnv` environment.
/// It also implements some high-level functionality to make it easy to access.
pub trait QueryHandler: DefaultQueriers {
type Error: Into<CwEnvError> + Debug;
type Error: Into<CwEnvError> + Debug + std::error::Error + Send + Sync + 'static;

/// Wait for an amount of blocks.
fn wait_blocks(&self, amount: u64) -> Result<(), Self::Error>;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait QuerierGetter<Q: Querier> {
}

pub trait Querier {
type Error: Into<CwEnvError> + Debug;
type Error: Into<CwEnvError> + Debug + std::error::Error + Send + Sync + 'static;
}

pub trait DefaultQueriers:
Expand All @@ -69,3 +69,178 @@ pub trait DefaultQueriers:
self.querier()
}
}

#[cfg(test)]
pub mod test {
use cosmwasm_std::{Binary, Coin};
use serde::Serialize;

use crate::{
environment::{DefaultQueriers, EnvironmentQuerier, IndexResponse, NodeQuerier},
CwEnvError,
};

use super::{bank::BankQuerier, wasm::WasmQuerier, QuerierGetter, QueryHandler};

impl crate::environment::queriers::Querier for MockQuerier {
type Error = CwEnvError;
}

#[derive(Clone)]
struct MockHandler {}

impl BankQuerier for MockQuerier {
fn balance(
&self,
_address: impl Into<String>,
_denom: Option<String>,
) -> Result<Vec<Coin>, Self::Error> {
// Returns an empty balance
Ok(vec![])
}

fn total_supply(&self) -> Result<Vec<Coin>, Self::Error> {
unimplemented!()
}

fn supply_of(&self, _denom: impl Into<String>) -> Result<Coin, Self::Error> {
unimplemented!()
}
}
impl WasmQuerier for MockQuerier {
fn code_id_hash(&self, _code_id: u64) -> Result<cosmwasm_std::HexBinary, Self::Error> {
unimplemented!()
}

fn contract_info(
&self,
_address: impl Into<String>,
) -> Result<cosmwasm_std::ContractInfoResponse, Self::Error> {
unimplemented!()
}

fn raw_query(
&self,
_address: impl Into<String>,
_query_keys: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
unimplemented!()
}

fn smart_query<Q: Serialize, T: serde::de::DeserializeOwned>(
&self,
_address: impl Into<String>,
_query_msg: &Q,
) -> Result<T, Self::Error> {
unimplemented!()
}

fn code(&self, _code_id: u64) -> Result<cosmwasm_std::CodeInfoResponse, Self::Error> {
unimplemented!()
}

fn instantiate2_addr(
&self,
_code_id: u64,
_creator: impl Into<String>,
_salt: cosmwasm_std::Binary,
) -> Result<String, Self::Error> {
unimplemented!()
}
}

impl NodeQuerier for MockQuerier {
type Response = MockQuerier;

fn latest_block(&self) -> Result<cosmwasm_std::BlockInfo, Self::Error> {
unimplemented!()
}

fn block_by_height(&self, _height: u64) -> Result<cosmwasm_std::BlockInfo, Self::Error> {
unimplemented!()
}

fn block_height(&self) -> Result<u64, Self::Error> {
unimplemented!()
}

fn block_time(&self) -> Result<u128, Self::Error> {
unimplemented!()
}

fn simulate_tx(&self, _tx_bytes: Vec<u8>) -> Result<u64, Self::Error> {
unimplemented!()
}

fn find_tx(&self, _hash: String) -> Result<Self::Response, Self::Error> {
unimplemented!()
}
}

#[derive(Clone, Debug)]
pub struct MockQuerier {}

impl IndexResponse for MockQuerier {
fn events(&self) -> Vec<cosmwasm_std::Event> {
unimplemented!()
}

fn event_attr_value(
&self,
_event_type: &str,
_attr_key: &str,
) -> cosmwasm_std::StdResult<String> {
unimplemented!()
}

fn data(&self) -> Option<Binary> {
unimplemented!()
}
}

impl QuerierGetter<MockQuerier> for MockHandler {
fn querier(&self) -> MockQuerier {
MockQuerier {}
}
}

impl EnvironmentQuerier for MockHandler {
fn env_info(&self) -> crate::environment::EnvironmentInfo {
unimplemented!()
}
}

impl DefaultQueriers for MockHandler {
type Bank = MockQuerier;
type Wasm = MockQuerier;
type Node = MockQuerier;
}

impl QueryHandler for MockHandler {
type Error = CwEnvError;

fn wait_blocks(&self, _amount: u64) -> Result<(), Self::Error> {
Ok(())
}

fn wait_seconds(&self, _secs: u64) -> Result<(), Self::Error> {
unimplemented!()
}

fn next_block(&self) -> Result<(), Self::Error> {
unimplemented!()
}
}

fn associated_querier_error<T: QueryHandler>(t: T) -> anyhow::Result<()> {
t.bank_querier().balance("anyone".to_string(), None)?;
t.wait_blocks(7)?;
Ok(())
}

#[test]
fn query_handler_error_usable_on_anyhow() -> anyhow::Result<()> {
associated_querier_error(MockHandler {})?;
Ok(())
}
}

0 comments on commit 845707c

Please sign in to comment.