-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support custom stargate query in tests
- Loading branch information
Showing
4 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
contracts/voting/dao-voting-cosmos-staked/src/tests/multitest/app.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use crate::tests::multitest::stargate::StargateKeeper; | ||
use cosmwasm_std::{ | ||
testing::MockApi, Api, Empty, GovMsg, IbcMsg, IbcQuery, MemoryStorage, Storage, | ||
}; | ||
use cw_multi_test::{ | ||
no_init, App, AppBuilder, BankKeeper, DistributionKeeper, FailingModule, GovFailingModule, | ||
IbcFailingModule, Router, StakeKeeper, WasmKeeper, | ||
}; | ||
#[allow(clippy::type_complexity)] | ||
pub struct CustomApp( | ||
App< | ||
BankKeeper, | ||
MockApi, | ||
MemoryStorage, | ||
FailingModule<Empty, Empty, Empty>, | ||
WasmKeeper<Empty, Empty>, | ||
StakeKeeper, | ||
DistributionKeeper, | ||
FailingModule<IbcMsg, IbcQuery, Empty>, | ||
FailingModule<GovMsg, Empty, Empty>, | ||
StargateKeeper, | ||
>, | ||
); | ||
impl Deref for CustomApp { | ||
type Target = App< | ||
BankKeeper, | ||
MockApi, | ||
MemoryStorage, | ||
FailingModule<Empty, Empty, Empty>, | ||
WasmKeeper<Empty, Empty>, | ||
StakeKeeper, | ||
DistributionKeeper, | ||
FailingModule<IbcMsg, IbcQuery, Empty>, | ||
FailingModule<GovMsg, Empty, Empty>, | ||
StargateKeeper, | ||
>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for CustomApp { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
impl Default for CustomApp { | ||
fn default() -> Self { | ||
Self::new(no_init) | ||
} | ||
} | ||
|
||
impl CustomApp { | ||
pub fn new<F>(init_fn: F) -> Self | ||
where | ||
F: FnOnce( | ||
&mut Router< | ||
BankKeeper, | ||
FailingModule<Empty, Empty, Empty>, | ||
WasmKeeper<Empty, Empty>, | ||
StakeKeeper, | ||
DistributionKeeper, | ||
IbcFailingModule, | ||
GovFailingModule, | ||
StargateKeeper, | ||
>, | ||
&dyn Api, | ||
&mut dyn Storage, | ||
), | ||
{ | ||
let app_builder = AppBuilder::default(); | ||
let stargate = StargateKeeper {}; | ||
let app = app_builder.with_stargate(stargate).build(init_fn); | ||
CustomApp(app) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
contracts/voting/dao-voting-cosmos-staked/src/tests/multitest/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
mod app; | ||
mod tests; | ||
mod stargate; |
64 changes: 64 additions & 0 deletions
64
contracts/voting/dao-voting-cosmos-staked/src/tests/multitest/stargate.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{ | ||
from_json, storage_keys::to_length_prefixed, to_json_binary, Addr, Api, Binary, BlockInfo, | ||
Decimal, Querier, Storage, | ||
}; | ||
use cw_multi_test::{error::AnyResult, AppResponse, CosmosRouter, Stargate}; | ||
use cw_storage_plus::Map; | ||
use osmosis_std::types::cosmos::staking::v1beta1::{Pool, QueryPoolResponse}; | ||
|
||
use super::tests::{DELEGATOR, VALIDATOR}; | ||
|
||
// from StakingKeeper | ||
#[cw_serde] | ||
struct Shares { | ||
stake: Decimal, | ||
rewards: Decimal, | ||
} | ||
const NAMESPACE_STAKING: &[u8] = b"staking"; | ||
const STAKES: Map<(&Addr, &Addr), Shares> = Map::new("stakes"); | ||
pub struct StargateKeeper {} | ||
|
||
impl StargateKeeper {} | ||
|
||
impl Stargate for StargateKeeper { | ||
fn execute<ExecC, QueryC>( | ||
&self, | ||
_api: &dyn Api, | ||
_storage: &mut dyn Storage, | ||
_router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>, | ||
_block: &BlockInfo, | ||
_sender: Addr, | ||
_type_url: String, | ||
_value: Binary, | ||
) -> AnyResult<AppResponse> { | ||
Ok(AppResponse::default()) | ||
} | ||
|
||
fn query( | ||
&self, | ||
_api: &dyn Api, | ||
storage: &dyn Storage, | ||
_querier: &dyn Querier, | ||
_block: &BlockInfo, | ||
path: String, | ||
data: Binary, | ||
) -> AnyResult<Binary> { | ||
if path == *"/cosmos.staking.v1beta1.Query/Pool" { | ||
// since we can't access the app or staking module, let's just raw | ||
// access the storage key we know is set for staking in the test :D | ||
let mut key = to_length_prefixed(NAMESPACE_STAKING); | ||
let map_key = STAKES.key((&Addr::unchecked(DELEGATOR), &Addr::unchecked(VALIDATOR))); | ||
key.extend_from_slice(&map_key); | ||
let data: Shares = from_json(storage.get(&key).unwrap())?; | ||
|
||
return Ok(to_json_binary(&QueryPoolResponse { | ||
pool: Some(Pool { | ||
not_bonded_tokens: "0".to_string(), | ||
bonded_tokens: data.stake.to_string(), | ||
}), | ||
})?); | ||
} | ||
Ok(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters