Skip to content

Commit

Permalink
Remove writing to storage from mock_deps_with_querier and introduce s… (
Browse files Browse the repository at this point in the history
  • Loading branch information
lubkoll authored Aug 27, 2024
1 parent aa4b1a6 commit 3a45b49
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 93 deletions.
99 changes: 45 additions & 54 deletions smart-contracts/osmosis/contracts/cl-vault/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::marker::PhantomData;

use cosmwasm_std::testing::{BankQuerier, MockApi, MockStorage, MOCK_CONTRACT_ADDR};
use cosmwasm_std::testing::{mock_info, BankQuerier, MockApi, MockStorage, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{
from_json, to_json_binary, Addr, BankQuery, Binary, Coin, ContractResult as CwContractResult,
Decimal, Empty, MessageInfo, OwnedDeps, Querier, QuerierResult, QueryRequest,
coin, from_json, to_json_binary, Addr, BankQuery, Binary, Coin,
ContractResult as CwContractResult, Decimal, DepsMut, Empty, Env, MessageInfo, OwnedDeps,
Querier, QuerierResult, QueryRequest,
};
use osmosis_std::types::cosmos::bank::v1beta1::{QuerySupplyOfRequest, QuerySupplyOfResponse};
use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::Pool;
Expand All @@ -19,14 +20,19 @@ use osmosis_std::types::{
},
};

use crate::contract::instantiate;
use crate::math::tick::tick_to_price;
use crate::msg::InstantiateMsg;
use crate::state::{
PoolConfig, Position, VaultConfig, POOL_CONFIG, POSITION, RANGE_ADMIN, VAULT_CONFIG,
VAULT_DENOM,
};

pub const POOL_ID: u64 = 1;
pub const POSITION_ID: u64 = 101;
pub const BASE_DENOM: &str = "base";
pub const QUOTE_DENOM: &str = "quote";
pub const TEST_VAULT_DENOM: &str = "uqsr";

pub struct QuasarQuerier {
position: FullPositionBreakdown,
Expand Down Expand Up @@ -158,7 +164,6 @@ impl Querier for QuasarQuerier {
kind: format!("Unmocked query type: {request:?}"),
}),
}
// QuerierResult::Ok(ContractResult::Ok(to_json_binary(&"hello").unwrap()))
}
}

Expand All @@ -172,7 +177,7 @@ pub fn mock_deps_with_querier_with_balance(
querier: QuasarQuerier::new_with_balances(
FullPositionBreakdown {
position: Some(OsmoPosition {
position_id: 1,
position_id: POSITION_ID,
address: MOCK_CONTRACT_ADDR.to_string(),
pool_id: POOL_ID,
lower_tick: 100,
Expand Down Expand Up @@ -237,7 +242,7 @@ pub fn mock_deps_with_querier_with_balance(
.save(
storage,
&crate::state::Position {
position_id: 1,
position_id: POSITION_ID,
join_time: 0,
claim_after: None,
},
Expand All @@ -247,18 +252,14 @@ pub fn mock_deps_with_querier_with_balance(
deps
}

pub fn mock_deps_with_querier(
info: &MessageInfo,
) -> OwnedDeps<MockStorage, MockApi, QuasarQuerier, Empty> {
let position_id = 1;

let mut deps = OwnedDeps {
pub fn mock_deps_with_querier() -> OwnedDeps<MockStorage, MockApi, QuasarQuerier, Empty> {
OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: QuasarQuerier::new(
FullPositionBreakdown {
position: Some(OsmoPosition {
position_id,
position_id: POSITION_ID,
address: MOCK_CONTRACT_ADDR.to_string(),
pool_id: POOL_ID,
lower_tick: 100,
Expand Down Expand Up @@ -290,55 +291,45 @@ pub fn mock_deps_with_querier(
500,
),
custom_query_type: PhantomData,
};

let storage = &mut deps.storage;
}
}

POSITION
.save(
storage,
&Position {
position_id,
join_time: 0,
claim_after: None,
},
)
.unwrap();
pub fn get_init_msg(admin: &str) -> InstantiateMsg {
InstantiateMsg {
admin: admin.to_string(),
pool_id: POOL_ID,
config: VaultConfig {
performance_fee: Decimal::percent(10),
treasury: Addr::unchecked(admin),
swap_max_slippage: Decimal::percent(95),
dex_router: Addr::unchecked(admin),
swap_admin: Addr::unchecked(admin),
twap_window_seconds: 24u64,
},
vault_token_subdenom: "utestvault".to_string(),
range_admin: admin.to_string(),
initial_lower_tick: 1,
initial_upper_tick: 100,
thesis: "Test thesis".to_string(),
name: "Contract".to_string(),
}
}

RANGE_ADMIN.save(storage, &info.sender).unwrap();
POOL_CONFIG
.save(
storage,
&PoolConfig {
pool_id: POOL_ID,
token0: BASE_DENOM.to_string(),
token1: QUOTE_DENOM.to_string(),
},
)
.unwrap();
VAULT_CONFIG
.save(
storage,
&VaultConfig {
performance_fee: Decimal::zero(),
treasury: Addr::unchecked("treasure"),
swap_max_slippage: Decimal::from_ratio(1u128, 20u128),
dex_router: Addr::unchecked("dex_router"),
swap_admin: Addr::unchecked("swap_admin"),
twap_window_seconds: 24u64,
},
)
pub fn instantiate_contract(mut deps: DepsMut, env: Env, admin: &str) {
let msg = get_init_msg(admin);
let info = mock_info(admin, &[coin(100, BASE_DENOM), coin(100, QUOTE_DENOM)]);
assert!(instantiate(deps.branch(), env, info, msg).is_ok());
VAULT_DENOM
.save(deps.storage, &TEST_VAULT_DENOM.to_string())
.unwrap();
POSITION
.save(
storage,
&crate::state::Position {
position_id: 1,
deps.storage,
&Position {
position_id: POSITION_ID,
join_time: 0,
claim_after: None,
},
)
.unwrap();

deps
}
48 changes: 15 additions & 33 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,53 +296,35 @@ fn execute_deposit(
mod tests {
use std::str::FromStr;

use cosmwasm_std::{testing::mock_env, Addr, BankMsg, Decimal256, Fraction, Uint256};
use cosmwasm_std::{
testing::{mock_env, mock_info},
Addr, BankMsg, Decimal256, Fraction, Uint256,
};

use crate::{
helpers::msgs::refund_bank_msg,
state::{Position, POSITION},
test_helpers::{mock_deps_with_querier, BASE_DENOM, QUOTE_DENOM},
test_helpers::{instantiate_contract, mock_deps_with_querier, BASE_DENOM, QUOTE_DENOM},
};

use super::*;

#[test]
fn execute_exact_deposit_works() {
let mut deps = mock_deps_with_querier(&MessageInfo {
sender: Addr::unchecked("alice"),
funds: vec![],
});
let mut deps = mock_deps_with_querier();
let env = mock_env();
let sender = Addr::unchecked("alice");
VAULT_DENOM
.save(deps.as_mut().storage, &"money".to_string())
.unwrap();
POSITION
.save(
deps.as_mut().storage,
&Position {
position_id: 1,
join_time: 0,
claim_after: None,
},
)
.unwrap();

execute_exact_deposit(
deps.as_mut(),
env,
MessageInfo {
sender: sender.clone(),
funds: vec![coin(100, BASE_DENOM), coin(100, QUOTE_DENOM)],
},
None,
)
.unwrap();
let sender = "alice";

instantiate_contract(deps.as_mut(), env.clone(), sender);

let info = mock_info(sender, &[coin(100, BASE_DENOM), coin(100, QUOTE_DENOM)]);
execute_exact_deposit(deps.as_mut(), env, info, None).unwrap();

// we currently have 100_000 total_vault_shares outstanding and the equivalent of 1999500token0, the user deposits the equivalent of 199token0, thus shares are
// 199 * 100000 / 1999500 = 9.95, which we round down. Thus we expect 9 shares in this example
assert_eq!(
SHARES.load(deps.as_ref().storage, sender).unwrap(),
SHARES
.load(deps.as_ref().storage, Addr::unchecked(sender))
.unwrap(),
Uint128::new(9)
);
}
Expand Down
18 changes: 12 additions & 6 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,16 @@ mod tests {
use cosmwasm_std::{
coin,
testing::{mock_dependencies, mock_env, mock_info, MOCK_CONTRACT_ADDR},
Decimal, Decimal256, Uint128, Uint256,
Addr, Decimal, Decimal256, Uint128, Uint256,
};

use crate::{
helpers::getters::get_range_admin,
math::tick::build_tick_exp_cache,
state::{MODIFY_RANGE_STATE, RANGE_ADMIN},
test_helpers::{
mock_deps_with_querier, mock_deps_with_querier_with_balance, BASE_DENOM, QUOTE_DENOM,
instantiate_contract, mock_deps_with_querier, mock_deps_with_querier_with_balance,
BASE_DENOM, POSITION_ID, QUOTE_DENOM,
},
vault::range::requires_swap,
};
Expand All @@ -331,15 +332,20 @@ mod tests {

#[test]
fn test_execute_update_range() {
let info = mock_info("addr0000", &[]);
let mut deps = mock_deps_with_querier(&info);
let range_admin = "range_admin".to_string();
let mut deps = mock_deps_with_querier();
let env = mock_env();
build_tick_exp_cache(deps.as_mut().storage).unwrap();
instantiate_contract(deps.as_mut(), env.clone(), &range_admin);
RANGE_ADMIN
.save(deps.as_mut().storage, &Addr::unchecked(range_admin.clone()))
.unwrap();

let env = mock_env();
let lower_price = Decimal::from_str("100").unwrap();
let upper_price = Decimal::from_str("100.20").unwrap();
let max_slippage = Decimal::from_str("0.5").unwrap();

let info = mock_info(&range_admin, &[]);
let res = super::execute_update_range(
deps.as_mut(),
&env,
Expand All @@ -357,7 +363,7 @@ mod tests {
assert_eq!(res.messages.len(), 1);
assert_eq!(res.attributes[0].value, "execute");
assert_eq!(res.attributes[1].value, "update_range_ticks");
assert_eq!(res.attributes[2].value, "1");
assert_eq!(res.attributes[2].value, POSITION_ID.to_string());
assert_eq!(res.attributes[3].value, "1000000.1");
}

Expand Down

0 comments on commit 3a45b49

Please sign in to comment.