Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamchordia committed Dec 17, 2024
1 parent ead143d commit 1b11ae3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
10 changes: 8 additions & 2 deletions smart-contracts/osmosis/contracts/cl-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use crate::msg::{
ClQueryMsg, ExecuteMsg, ExtensionExecuteMsg, ExtensionQueryMsg, InstantiateMsg, MigrateMsg,
ModifyRangeMsg, QueryMsg,
};
use crate::query::{query_active_users, query_assets_from_shares, query_dex_router, query_info, query_metadata, query_pool, query_position, query_total_assets, query_total_vault_token_supply, query_user_assets, query_user_balance, query_verify_tick_cache, RangeAdminResponse};
use crate::query::{
query_active_users, query_assets_from_shares, query_dex_router, query_info, query_metadata,
query_pool, query_position, query_total_assets, query_total_vault_token_supply,
query_user_assets, query_user_balance, query_verify_tick_cache, RangeAdminResponse,
};
use crate::reply::Replies;
use crate::state::{VaultConfig, VAULT_CONFIG};
use crate::vault::{
Expand Down Expand Up @@ -167,7 +171,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
}
ClQueryMsg::VerifyTickCache => Ok(to_json_binary(&query_verify_tick_cache(deps)?)?),
},
ExtensionQueryMsg::ActiveUsers {next_token, limit} => Ok(to_json_binary(&query_active_users(deps, next_token, limit)?)?),
ExtensionQueryMsg::ActiveUsers { next_token, limit } => Ok(to_json_binary(
&query_active_users(deps, next_token, limit)?,
)?),
},
}
}
Expand Down
4 changes: 1 addition & 3 deletions smart-contracts/osmosis/contracts/cl-vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ pub enum AdminExtensionExecuteMsg {
/// Build tick exponent cache
BuildTickCache {},
/// Auto claim endpoint
AutoWithdraw {
users: Vec<(Addr, Uint256)>,
}
AutoWithdraw { users: Vec<(Addr, Uint256)> },
}

#[cw_serde]
Expand Down
28 changes: 15 additions & 13 deletions smart-contracts/osmosis/contracts/cl-vault/src/query.rs
Original file line number Diff line number Diff line change
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>, // List of user addresses only
pub next_token: Option<String>, // Token for the next page
}

Expand Down Expand Up @@ -186,31 +186,27 @@ pub fn query_active_users(
let mut users: Vec<String> = Vec::new();
let mut start_index = 0;

// If there's a next_token, parse it to find the starting index
if let Some(token) = next_token {
start_index = token.parse::<usize>().map_err(|_| ContractError::InvalidToken { /* provide necessary fields if any */ })?;
start_index = token
.parse::<usize>()
.map_err(|_| ContractError::InvalidToken {})?;
}

// Iterate over the SHARES map to collect user addresses
for result in SHARES.range(deps.storage, None, None, cosmwasm_std::Order::Ascending) {
let (addr, _balance) = result.map_err(|e| ContractError::Std(e))?; // Handle the Result properly
let user_addr = addr;
let (addr, _balance) = result.map_err(ContractError::Std)?;

// Only add users starting from the start_index
if start_index > 0 {
start_index -= 1;
continue;
}

users.push(user_addr.to_string());
users.push(addr.to_string());

// Stop if we reach the limit
if users.len() as u64 >= limit {
break;
}
}

// Determine the next token for pagination
let next_token = if users.len() as u64 == limit {
Some((start_index + limit as usize).to_string())
} else {
Expand Down Expand Up @@ -286,9 +282,15 @@ mod tests {
let user3 = Addr::unchecked("user3");

// Insert mock user balances into SHARES
SHARES.save(deps.as_mut().storage, user1, &Uint128::new(100)).unwrap();
SHARES.save(deps.as_mut().storage, user2, &Uint128::new(200)).unwrap();
SHARES.save(deps.as_mut().storage, user3, &Uint128::new(300)).unwrap();
SHARES
.save(deps.as_mut().storage, user1, &Uint128::new(100))
.unwrap();
SHARES
.save(deps.as_mut().storage, user2, &Uint128::new(200))
.unwrap();
SHARES
.save(deps.as_mut().storage, user3, &Uint128::new(300))
.unwrap();

// Test without next_token and limit of 2
let res = query_active_users(deps.as_ref(), None, 2).unwrap();
Expand Down
22 changes: 13 additions & 9 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/admin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::error::assert_admin;
use crate::math::tick::build_tick_exp_cache;
use crate::state::{Metadata, VaultConfig, ADMIN_ADDRESS, METADATA, RANGE_ADMIN, VAULT_CONFIG};
use crate::vault::withdraw::execute_withdraw;
use crate::{msg::AdminExtensionExecuteMsg, ContractError};
use cosmwasm_std::{Addr, Decimal, DepsMut, Env, MessageInfo, Response, StdError, Uint256};
use cw_utils::nonpayable;
use crate::vault::withdraw::execute_withdraw;

pub(crate) fn execute_admin(
deps: DepsMut,
Expand All @@ -26,7 +26,9 @@ pub(crate) fn execute_admin(
execute_update_range_admin(deps, info, address)
}
AdminExtensionExecuteMsg::BuildTickCache {} => execute_build_tick_exp_cache(deps, info),
AdminExtensionExecuteMsg::AutoWithdraw { users } => execute_auto_claim(deps, &env, info, users),
AdminExtensionExecuteMsg::AutoWithdraw { users } => {
execute_auto_claim(deps, &env, info, users)
}
}
}

Expand Down Expand Up @@ -156,13 +158,15 @@ pub fn execute_auto_claim(
sender: user_data.0.clone(),
funds: vec![],
};
let withdraw_response =
execute_withdraw(deps.branch(), env, user_info, Some(user_data.0.to_string()), user_data.1)?;

let withdraw_messages = withdraw_response
.messages
.iter()
.map(|sm| sm.msg.clone());
let withdraw_response = execute_withdraw(
deps.branch(),
env,
user_info,
Some(user_data.0.to_string()),
user_data.1,
)?;

let withdraw_messages = withdraw_response.messages.iter().map(|sm| sm.msg.clone());

res = res
.add_messages(withdraw_messages)
Expand Down
45 changes: 26 additions & 19 deletions smart-contracts/osmosis/contracts/cl-vault/tests/test-tube/admin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 crate::setup::{fixture_default, fixture_dex_router, ACCOUNTS_INIT_BALANCE, ACCOUNTS_NUM, DENOM_BASE, DENOM_QUOTE, MAX_SLIPPAGE_HIGH, PERFORMANCE_FEE_DEFAULT};

use cl_vault::{
msg::{
AdminExtensionExecuteMsg, ClQueryMsg, ExecuteMsg, ExtensionExecuteMsg, ExtensionQueryMsg,
Expand All @@ -9,7 +11,6 @@ use cl_vault::{
query::{ActiveUsersResponse, VerifyTickCacheResponse},
};
use osmosis_test_tube::{Account, Module, Wasm};
use crate::any_deposit::do_and_verify_any_deposit;

#[test]
fn admin_build_tick_cache_works() {
Expand Down Expand Up @@ -49,7 +50,8 @@ fn admin_build_tick_cache_works() {

#[test]
fn admin_execute_auto_claim_works() {
let (app, contract_address, _, _cl_pool_id, _, admin, _) = fixture_dex_router(PERFORMANCE_FEE_DEFAULT);
let (app, contract_address, _, _cl_pool_id, _, admin, _) =
fixture_dex_router(PERFORMANCE_FEE_DEFAULT);
let wasm = Wasm::new(&app);

for _i in 1..10 {
Expand All @@ -73,24 +75,26 @@ fn admin_execute_auto_claim_works() {
if amount_quote > Uint128::zero() {
deposit_coins.push(Coin::new(amount_quote.u128(), DENOM_QUOTE));
}
let _ = wasm.execute(
contract_address.as_str(),
&ExecuteMsg::AnyDeposit {
amount: amount_base,
asset: DENOM_BASE.to_string(),
recipient: Some(accounts[0].address()),
max_slippage: Decimal::bps(MAX_SLIPPAGE_HIGH),
},
&deposit_coins,
&accounts[0],
).unwrap();
let _ = wasm
.execute(
contract_address.as_str(),
&ExecuteMsg::AnyDeposit {
amount: amount_base,
asset: DENOM_BASE.to_string(),
recipient: Some(accounts[0].address()),
max_slippage: Decimal::bps(MAX_SLIPPAGE_HIGH),
},
&deposit_coins,
&accounts[0],
)
.unwrap();
}

let query_resp: ActiveUsersResponse = wasm
.query(
contract_address.as_str(),
&QueryMsg::VaultExtension(ExtensionQueryMsg::ActiveUsers {
limit: 10, // Adjust limit as needed
limit: 100,
next_token: None,
}),
)
Expand All @@ -102,8 +106,8 @@ fn admin_execute_auto_claim_works() {
.users
.iter()
.map(|user| {
let addr = Addr::unchecked(user); // Convert String to Addr
(addr, Uint256::from(100u128)) // Use from method to create Uint256
let addr = Addr::unchecked(user);
(addr, Uint256::from(100u128))
})
.collect();

Expand All @@ -125,5 +129,8 @@ fn admin_execute_auto_claim_works() {
.iter()
.any(|attr| attr.key == "action" && attr.value == "auto_withdraw")
});
assert!(has_expected_event, "Expected event not found in auto_claim_resp");
assert!(
has_expected_event,
"Expected event not found in auto_claim_resp"
);
}

0 comments on commit 1b11ae3

Please sign in to comment.