Skip to content

Commit

Permalink
fix interchainqueries tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Oct 16, 2024
1 parent 25bdfbd commit abebb3e
Show file tree
Hide file tree
Showing 6 changed files with 1,273 additions and 1,257 deletions.
1 change: 1 addition & 0 deletions contracts/neutron_interchain_queries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde-json-wasm = { workspace = true }
prost-types = { workspace = true }
cosmwasm-schema = { workspace = true }
neutron-std = { workspace = true }
prost = { workspace = true }

[dev-dependencies]
base64 = { workspace = true }
2 changes: 1 addition & 1 deletion contracts/neutron_interchain_queries/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ pub fn sudo_tx_query_result(
let mut stored_transfers: u64 = TRANSFERS.load(deps.storage).unwrap_or_default();
stored_transfers += deposits.len() as u64;
TRANSFERS.save(deps.storage, &stored_transfers)?;

check_deposits_size(&deposits)?;

let mut stored_deposits: Vec<Transfer> = RECIPIENT_TXS
.load(deps.storage, recipient)
.unwrap_or_default();
Expand Down
231 changes: 111 additions & 120 deletions contracts/neutron_interchain_queries/src/testing/mock_querier.rs
Original file line number Diff line number Diff line change
@@ -1,120 +1,111 @@
// use std::collections::HashMap;
// use std::marker::PhantomData;
//
// use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
// use cosmwasm_std::{
// from_json, Binary, Coin, ContractResult, CustomQuery, FullDelegation, OwnedDeps, Querier,
// QuerierResult, QueryRequest, SystemError, SystemResult, Uint128, Validator,
// };
// use schemars::JsonSchema;
// use serde::{Deserialize, Serialize};
//
// pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
//
// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
// #[serde(rename_all = "snake_case")]
// pub struct CustomQueryWrapper {}
//
// // implement custom query
// impl CustomQuery for CustomQueryWrapper {}
//
// pub fn mock_dependencies(
// contract_balance: &[Coin],
// ) -> OwnedDeps<MockStorage, MockApi, WasmMockQuerier> {
// let contract_addr = MOCK_CONTRACT_ADDR;
// let custom_querier: WasmMockQuerier =
// WasmMockQuerier::new(MockQuerier::new(&[(contract_addr, contract_balance)]));
//
// OwnedDeps {
// storage: MockStorage::default(),
// api: MockApi::default(),
// querier: custom_querier,
// custom_query_type: PhantomData,
// }
// }
//
// pub struct WasmMockQuerier {
// base: MockQuerier,
// query_responses: HashMap<u64, Binary>,
// registered_queries: HashMap<u64, Binary>,
// }
//
// impl Querier for WasmMockQuerier {
// fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
// let request: QueryRequest = match from_json(bin_request) {
// Ok(v) => v,
// Err(e) => {
// return QuerierResult::Err(SystemError::InvalidRequest {
// error: format!("Parsing query request: {}", e),
// request: bin_request.into(),
// });
// }
// };
// self.handle_query(&request)
// }
// }
//
// // TODO: fix
// impl WasmMockQuerier {
// pub fn handle_query(&self, request: &QueryRequest) -> QuerierResult {
// match &request {
// QueryRequest::Custom(NeutronQuery::InterchainQueryResult { query_id }) => {
// SystemResult::Ok(ContractResult::Ok(
// (*self.query_responses.get(query_id).unwrap()).clone(),
// ))
// }
// QueryRequest::Custom(NeutronQuery::RegisteredInterchainQuery { query_id }) => {
// SystemResult::Ok(ContractResult::Ok(
// (*self.registered_queries.get(query_id).unwrap()).clone(),
// ))
// }
// QueryRequest::Custom(NeutronQuery::RegisteredInterchainQueries {
// owners: _owners,
// connection_id: _connection_id,
// pagination: _pagination,
// }) => {
// todo!()
// }
// QueryRequest::Custom(NeutronQuery::InterchainAccountAddress { .. }) => {
// todo!()
// }
// _ => self.base.handle_query(request),
// }
// }
//
// pub fn _update_staking(
// &mut self,
// denom: &str,
// validators: &[Validator],
// delegations: &[FullDelegation],
// ) {
// self.base.staking.update(denom, validators, delegations);
// }
//
// pub fn add_query_response(&mut self, query_id: u64, response: Binary) {
// self.query_responses.insert(query_id, response);
// }
// pub fn add_registered_queries(&mut self, query_id: u64, response: Binary) {
// self.registered_queries.insert(query_id, response);
// }
// }
//
// #[derive(Clone, Default)]
// pub struct BalanceQuerier {
// _balances: HashMap<String, Coin>,
// }
//
// #[derive(Clone, Default)]
// pub struct TokenQuerier {
// _balances: HashMap<String, HashMap<String, Uint128>>,
// }
//
// impl WasmMockQuerier {
// pub fn new(base: MockQuerier) -> Self {
// WasmMockQuerier {
// base,
// query_responses: HashMap::new(),
// registered_queries: HashMap::new(),
// }
// }
// }
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{
from_json, Binary, Coin, ContractResult, CustomQuery, FullDelegation, GrpcQuery, OwnedDeps,
Querier, QuerierResult, QueryRequest, SystemError, SystemResult, Uint128, Validator,
};
use neutron_std::types::neutron::interchainqueries::{
QueryRegisteredQueryRequest, QueryRegisteredQueryResultRequest,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::marker::PhantomData;

pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct CustomQueryWrapper {}

// implement custom query
impl CustomQuery for CustomQueryWrapper {}

pub fn mock_dependencies(
contract_balance: &[Coin],
) -> OwnedDeps<MockStorage, MockApi, WasmMockQuerier> {
let contract_addr = MOCK_CONTRACT_ADDR;
let custom_querier: WasmMockQuerier =
WasmMockQuerier::new(MockQuerier::new(&[(contract_addr, contract_balance)]));

OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: custom_querier,
custom_query_type: PhantomData,
}
}

pub struct WasmMockQuerier {
base: MockQuerier,
query_responses: HashMap<u64, Binary>,
registered_queries: HashMap<u64, Binary>,
}

impl Querier for WasmMockQuerier {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
let request: QueryRequest = match from_json(bin_request) {
Ok(v) => v,
Err(e) => {
return QuerierResult::Err(SystemError::InvalidRequest {
error: format!("Parsing query request: {}", e),
request: bin_request.into(),
});
}
};
self.handle_query(&request)
}
}

impl WasmMockQuerier {
pub fn handle_query(&self, request: &QueryRequest) -> QuerierResult {
match &request {
QueryRequest::Grpc(GrpcQuery { path, data }) => {
let quoted_path = path.trim_matches('"').to_string();
if &quoted_path == "/neutron.interchainqueries.Query/QueryResult" {
let request: QueryRegisteredQueryResultRequest =
::prost::Message::decode(&data[..]).unwrap();
SystemResult::Ok(ContractResult::Ok(
(*self.query_responses.get(&request.query_id).unwrap()).clone(),
))
} else if &quoted_path == "/neutron.interchainqueries.Query/RegisteredQuery" {
let request: QueryRegisteredQueryRequest =
::prost::Message::decode(&data[..]).unwrap();
SystemResult::Ok(ContractResult::Ok(
(*self.registered_queries.get(&request.query_id).unwrap()).clone(),
))
} else {
println!("PATH: {}", quoted_path);
self.base.handle_query(request)
}
}
_ => self.base.handle_query(request),
}
}

pub fn add_query_response(&mut self, query_id: u64, response: Binary) {
self.query_responses.insert(query_id, response);
}
pub fn add_registered_queries(&mut self, query_id: u64, response: Binary) {
self.registered_queries.insert(query_id, response);
}
}

#[derive(Clone, Default)]
pub struct BalanceQuerier {
_balances: HashMap<String, Coin>,
}

#[derive(Clone, Default)]
pub struct TokenQuerier {
_balances: HashMap<String, HashMap<String, Uint128>>,
}

impl WasmMockQuerier {
pub fn new(base: MockQuerier) -> Self {
WasmMockQuerier {
base,
query_responses: HashMap::new(),
registered_queries: HashMap::new(),
}
}
}
Loading

0 comments on commit abebb3e

Please sign in to comment.