Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor deposit logic #702

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions smart-contracts/osmosis/contracts/cl-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::helpers::prepend::prepend_claim_msg;
use crate::instantiate::{
handle_create_denom_reply, handle_instantiate, handle_instantiate_create_position_reply,
};
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, ModifyRangeMsg, QueryMsg};
use crate::msg::{DepositType, ExecuteMsg, InstantiateMsg, MigrateMsg, ModifyRangeMsg, QueryMsg};
use crate::query::{
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,
Expand All @@ -32,14 +32,13 @@ use crate::state::{
#[allow(deprecated)]
use crate::state::{Position, OLD_POSITION, POSITION};
use crate::vault::admin::execute_admin;
use crate::vault::any_deposit::{execute_any_deposit, handle_any_deposit_swap_reply};
use crate::vault::autocompound::{
execute_autocompound, execute_migration_step, handle_autocompound_reply,
};
use crate::vault::deposit::{execute_deposit, handle_any_deposit_swap_reply};
use crate::vault::distribution::{
execute_collect_rewards, handle_collect_incentives_reply, handle_collect_spread_rewards_reply,
};
use crate::vault::exact_deposit::execute_exact_deposit;
use crate::vault::merge::{
execute_merge_position, handle_merge_create_position_reply,
handle_merge_withdraw_position_reply,
Expand Down Expand Up @@ -80,9 +79,15 @@ pub fn execute(
asset: _,
recipient,
max_slippage,
} => execute_any_deposit(deps, env, info, recipient, max_slippage),
} => execute_deposit(
deps,
env,
info,
recipient,
DepositType::Any { max_slippage },
),
cw_vault_multi_standard::VaultStandardExecuteMsg::ExactDeposit { recipient } => {
execute_exact_deposit(deps, env, info, recipient)
execute_deposit(deps, env, info, recipient, DepositType::Exact)
}
cw_vault_multi_standard::VaultStandardExecuteMsg::Redeem { recipient, amount } => {
prepend_claim_msg(
Expand All @@ -97,10 +102,10 @@ pub fn execute(
}
crate::msg::ExtensionExecuteMsg::Authz(msg) => match msg {
crate::msg::AuthzExtension::ExactDeposit {} => {
execute_exact_deposit(deps, env, info, None)
execute_deposit(deps, env, info, None, DepositType::Exact)
}
crate::msg::AuthzExtension::AnyDeposit { max_slippage } => {
execute_any_deposit(deps, env, info, None, max_slippage)
execute_deposit(deps, env, info, None, DepositType::Any { max_slippage })
}
crate::msg::AuthzExtension::Redeem { amount } => prepend_claim_msg(
&env,
Expand Down
35 changes: 35 additions & 0 deletions smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::math::tick::tick_to_price;
use crate::state::{PoolConfig, RANGE_ADMIN};
use crate::vault::range::SwapDirection;
use std::str::FromStr;

use osmosis_std::shim::Timestamp as OsmoTimestamp;
Expand Down Expand Up @@ -245,6 +246,40 @@ pub fn get_tokens_provided(
Ok(tokens_provided)
}

pub fn get_swap_amount_and_direction(
balance0: Uint128,
balance1: Uint128,
current_tick: i64,
target_lower_tick: i64,
target_upper_tick: i64,
) -> Result<(Uint128, SwapDirection), ContractError> {
if !balance0.is_zero() {
let token_in_amount = if current_tick > target_upper_tick {
balance0
} else {
get_single_sided_deposit_0_to_1_swap_amount(
balance0,
target_lower_tick,
current_tick,
target_upper_tick,
)?
};
Ok((token_in_amount, SwapDirection::ZeroToOne))
} else {
let token_in_amount = if current_tick < target_lower_tick {
balance1
} else {
get_single_sided_deposit_1_to_0_swap_amount(
balance1,
target_lower_tick,
current_tick,
target_upper_tick,
)?
};
Ok((token_in_amount, SwapDirection::OneToZero))
}
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
Expand Down
5 changes: 5 additions & 0 deletions smart-contracts/osmosis/contracts/cl-vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ pub struct InstantiateMsg {
pub initial_upper_tick: i64,
}

pub enum DepositType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this distinction not already made by the two different entrypoints?

Exact,
Any { max_slippage: Decimal },
}

#[cw_serde]
pub struct MigrateMsg {
pub dex_router: Addr,
Expand Down
Loading
Loading