Skip to content

Commit

Permalink
query and test updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamchordia committed Dec 18, 2024
1 parent 66080cf commit b1833b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion smart-contracts/osmosis/contracts/cl-vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum AdminExtensionExecuteMsg {
/// Build tick exponent cache
BuildTickCache {},
/// Auto claim endpoint
AutoWithdraw { users: Vec<(Addr, Uint256)> },
AutoWithdraw { users: Vec<(String, Uint256)> },
}

#[cw_serde]
Expand Down
19 changes: 12 additions & 7 deletions smart-contracts/osmosis/contracts/cl-vault/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::state::{
use crate::vault::concentrated_liquidity::get_position;
use crate::ContractError;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{coin, Coin, Decimal, Deps, Env, Uint128};
use cosmwasm_std::{coin, Coin, Decimal, Deps, Env, Uint128, Uint256};
use osmosis_std::types::cosmos::bank::v1beta1::BankQuerier;
use quasar_types::cw_vault_multi_standard::VaultInfoResponse;

Expand Down Expand Up @@ -75,7 +75,7 @@ pub struct DexRouterResponse {

#[cw_serde]
pub struct ActiveUsersResponse {
pub users: Vec<String>, // List of user addresses only
pub users: Vec<(String, Uint256)>, // List of user addresses only
pub next_token: Option<String>, // Token for the next page
}

Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn query_active_users(
next_token: Option<String>,
limit: u64,
) -> Result<ActiveUsersResponse, ContractError> {
let mut users: Vec<String> = Vec::new();
let mut users: Vec<(String, Uint256)> = Vec::new();
let mut start_index = 0;

if let Some(token) = next_token {
Expand All @@ -193,14 +193,14 @@ pub fn query_active_users(
}

for result in SHARES.range(deps.storage, None, None, cosmwasm_std::Order::Ascending) {
let (addr, _balance) = result.map_err(ContractError::Std)?;
let (addr, balance) = result.map_err(ContractError::Std)?;

if start_index > 0 {
start_index -= 1;
continue;
}

users.push(addr.to_string());
users.push((addr.to_string(), Uint256::from(balance)));

if users.len() as u64 >= limit {
break;
Expand Down Expand Up @@ -294,12 +294,17 @@ mod tests {

// Test without next_token and limit of 2
let res = query_active_users(deps.as_ref(), None, 2).unwrap();
assert_eq!(res.users, vec!["user1", "user2"]);
assert_eq!(res.users, vec![
(String::from("user1"), Uint256::from(100u128)),
(String::from("user2"), Uint256::from(200u128)),
]);
assert_eq!(res.next_token, Some("2".to_string())); // Expecting next_token for pagination

// Test with next_token to get the next user
let res = query_active_users(deps.as_ref(), Some("2".to_string()), 2).unwrap();
assert_eq!(res.users, vec!["user3"]);
assert_eq!(res.users, vec![
(String::from("user3"), Uint256::from(300u128)),
]);
assert_eq!(res.next_token, None); // No more users, so next_token should be None
}
}
4 changes: 2 additions & 2 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn execute_auto_claim(
mut deps: DepsMut,
env: &Env,
info: MessageInfo,
users: Vec<(Addr, Uint256)>,
users: Vec<(String, Uint256)>,
) -> Result<Response, ContractError> {
assert_admin(deps.storage, &info.sender)?;
let mut res = Response::new();
Expand All @@ -155,7 +155,7 @@ pub fn execute_auto_claim(
deps.api.addr_validate(user_data.0.as_str())?;

let user_info = MessageInfo {
sender: user_data.0.clone(),
sender: Addr::unchecked(user_data.0.clone()),
funds: vec![],
};
let withdraw_response = execute_withdraw(
Expand Down
25 changes: 14 additions & 11 deletions smart-contracts/osmosis/contracts/cl-vault/tests/test-tube/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::setup::{
fixture_default, fixture_dex_router, ACCOUNTS_INIT_BALANCE, DENOM_BASE,
DENOM_QUOTE, MAX_SLIPPAGE_HIGH, PERFORMANCE_FEE_DEFAULT,
};
use cosmwasm_std::{Addr, Coin, Decimal, Uint128, Uint256};
use cosmwasm_std::{Coin, Decimal, Uint128, Uint256};
use cl_vault::{
msg::{
AdminExtensionExecuteMsg, ClQueryMsg, ExecuteMsg, ExtensionExecuteMsg, ExtensionQueryMsg,
Expand Down Expand Up @@ -90,6 +90,7 @@ fn admin_execute_auto_claim_works() {
.unwrap();
}

// Query active users
let query_resp: ActiveUsersResponse = wasm
.query(
contract_address.as_str(),
Expand All @@ -99,18 +100,17 @@ fn admin_execute_auto_claim_works() {
}),
)
.unwrap();

assert!(!query_resp.users.is_empty(), "Expected users to be present");

let users: Vec<(Addr, Uint256)> = query_resp
// Prepare users for auto claim
let users: Vec<(String, Uint256)> = query_resp
.users
.iter()
.map(|user| {
let addr = Addr::unchecked(user);
(addr, Uint256::from(100u128))
})
.map(|(addr, balance)| (addr.clone(), *balance)) // Keep as (String, Uint256)
.collect();

// Execute auto claim
let _ = wasm
.execute(
contract_address.as_str(),
Expand All @@ -122,6 +122,7 @@ fn admin_execute_auto_claim_works() {
)
.unwrap();

// Query active users again
let updated_query_resp: ActiveUsersResponse = wasm
.query(
contract_address.as_str(),
Expand All @@ -132,11 +133,13 @@ fn admin_execute_auto_claim_works() {
)
.unwrap();

for (addr, _) in users.clone() {
for (addr, _) in &users {
let user_balance = updated_query_resp.users.iter().find(|(user_addr, _)| user_addr == addr);
assert!(
updated_query_resp.users.contains(&addr.to_string()),
"Expected user {} to be present in the active users list after auto claim",
addr
user_balance.is_some() && user_balance.unwrap().1.is_zero(),
"Expected user {} to have a balance of 0 after auto claim, but found {}",
addr,
user_balance.map(|(_, balance)| balance).unwrap_or(&Uint256::zero())
);
}
}

0 comments on commit b1833b5

Please sign in to comment.