Skip to content

Commit

Permalink
Remove dex-router
Browse files Browse the repository at this point in the history
  • Loading branch information
lubkoll committed Aug 28, 2024
1 parent 159db8b commit f69aca2
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 224 deletions.
23 changes: 0 additions & 23 deletions smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,29 +338,6 @@
},
"additionalProperties": false
},
{
"description": "Update the dex router address.",
"type": "object",
"required": [
"update_dex_router"
],
"properties": {
"update_dex_router": {
"type": "object",
"properties": {
"address": {
"description": "The new dex router address.",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Build tick exponent cache",
"type": "object",
Expand Down
23 changes: 0 additions & 23 deletions smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,6 @@
},
"additionalProperties": false
},
{
"description": "Update the dex router address.",
"type": "object",
"required": [
"update_dex_router"
],
"properties": {
"update_dex_router": {
"type": "object",
"properties": {
"address": {
"description": "The new dex router address.",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Build tick exponent cache",
"type": "object",
Expand Down
2 changes: 2 additions & 0 deletions smart-contracts/osmosis/contracts/cl-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
let dex_router_item: Item<Addr> = Item::new("dex_router");
dex_router_item.remove(deps.storage);
// VaultConfig
#[cw_serde]
struct OldVaultConfig {
Expand Down
5 changes: 0 additions & 5 deletions smart-contracts/osmosis/contracts/cl-vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ pub enum AdminExtensionExecuteMsg {
/// The metadata updates.
updates: Metadata,
},
/// Update the dex router address.
UpdateDexRouter {
/// The new dex router address.
address: Option<String>,
},
/// Build tick exponent cache
BuildTickCache {},
}
Expand Down
7 changes: 3 additions & 4 deletions smart-contracts/osmosis/contracts/cl-vault/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::helpers::coinlist::CoinList;
use crate::helpers::getters::get_unused_balances;
use crate::math::tick::verify_tick_exp_cache;
use crate::state::DEX_ROUTER;
use crate::state::{
PoolConfig, ADMIN_ADDRESS, METADATA, POOL_CONFIG, POSITION, SHARES, VAULT_DENOM,
PoolConfig, ADMIN_ADDRESS, METADATA, POOL_CONFIG, POSITION, SHARES, VAULT_CONFIG, VAULT_DENOM,
};
use crate::vault::concentrated_liquidity::get_position;
use crate::ContractError;
Expand Down Expand Up @@ -106,10 +105,10 @@ pub fn query_metadata(deps: Deps) -> Result<MetadataResponse, ContractError> {
}

pub fn query_dex_router(deps: Deps) -> Result<DexRouterResponse, ContractError> {
let dex_router = DEX_ROUTER.load(deps.storage)?;
let config = VAULT_CONFIG.load(deps.storage)?;

Ok(DexRouterResponse {
dex_router: dex_router.to_string(),
dex_router: config.dex_router.to_string(),
})
}

Expand Down
5 changes: 2 additions & 3 deletions smart-contracts/osmosis/contracts/cl-vault/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::vault::merge::CurrentMergeWithdraw;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Coin, Decimal, Decimal256, Uint128};
use cosmwasm_std::{Addr, Decimal, Decimal256, Uint128};
use cw_storage_plus::{Deque, Item, Map};
use osmosis_std::types::osmosis::poolmanager::v1beta1::SwapAmountInRoute;

Expand Down Expand Up @@ -72,8 +72,7 @@ pub struct CurrentDeposit {
pub sender: Addr,
}

pub const CURRENT_SWAP_ANY_DEPOSIT: Item<(Coin, Addr, (Uint128, Uint128))> =
Item::new("current_swap_any_deposit");
pub const CURRENT_SWAP_RECIPIENT: Item<Addr> = Item::new("current_swap_recipient");

pub const DEX_ROUTER: Item<Addr> = Item::new("dex_router");

Expand Down
32 changes: 1 addition & 31 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/admin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::error::assert_admin;
use crate::math::tick::build_tick_exp_cache;
use crate::state::{
Metadata, VaultConfig, ADMIN_ADDRESS, DEX_ROUTER, METADATA, RANGE_ADMIN, VAULT_CONFIG,
};
use crate::state::{Metadata, VaultConfig, ADMIN_ADDRESS, METADATA, RANGE_ADMIN, VAULT_CONFIG};
use crate::{msg::AdminExtensionExecuteMsg, ContractError};
use cosmwasm_std::{Decimal, DepsMut, MessageInfo, Response, StdError};
use cw_utils::nonpayable;
Expand All @@ -25,9 +23,6 @@ pub(crate) fn execute_admin(
AdminExtensionExecuteMsg::UpdateRangeAdmin { address } => {
execute_update_range_admin(deps, info, address)
}
AdminExtensionExecuteMsg::UpdateDexRouter { address } => {
execute_update_dex_router(deps, info, address)
}
AdminExtensionExecuteMsg::BuildTickCache {} => execute_build_tick_exp_cache(deps, info),
}
}
Expand Down Expand Up @@ -80,31 +75,6 @@ pub fn execute_update_range_admin(
.add_attribute("new_admin", &new_admin))
}

/// Updates the dex router address
pub fn execute_update_dex_router(
deps: DepsMut,
info: MessageInfo,
address: Option<String>,
) -> Result<Response, ContractError> {
nonpayable(&info).map_err(|_| ContractError::NonPayable {})?;
assert_admin(deps.storage, &info.sender)?;

match address.clone() {
Some(address) => {
let new_router = deps.api.addr_validate(&address)?;
DEX_ROUTER.save(deps.storage, &new_router.clone())?;
}
None => {
DEX_ROUTER.remove(deps.storage);
}
}

Ok(Response::new()
.add_attribute("method", "execute")
.add_attribute("action", "update_dex_router")
.add_attribute("new_router", address.unwrap_or_default().to_string()))
}

/// Updates the configuration of the contract.
///
/// This function first checks if the message sender is nonpayable. If the sender sent funds, a `ContractError::NonPayable` error is returned.
Expand Down
86 changes: 16 additions & 70 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ use crate::{
helpers::{
getters::{
get_depositable_tokens, get_single_sided_deposit_0_to_1_swap_amount,
get_single_sided_deposit_1_to_0_swap_amount, get_twap_price, get_value_wrt_asset0,
DepositInfo,
get_single_sided_deposit_1_to_0_swap_amount, get_twap_price, get_unused_pair_balances,
get_value_wrt_asset0, DepositInfo,
},
msgs::refund_bank_msg,
},
query::{query_total_assets, query_total_vault_token_supply},
reply::Replies,
state::{CURRENT_SWAP_ANY_DEPOSIT, DEX_ROUTER, POOL_CONFIG, SHARES, VAULT_CONFIG, VAULT_DENOM},
state::{CURRENT_SWAP_RECIPIENT, POOL_CONFIG, SHARES, VAULT_CONFIG, VAULT_DENOM},
vault::{
concentrated_liquidity::{get_cl_pool_info, get_position},
swap::{estimate_swap_min_out_amount, swap_msg},
},
ContractError,
};
use cosmwasm_std::{
attr, coin, Addr, Coin, Decimal, DepsMut, Env, Fraction, MessageInfo, Response, SubMsg,
SubMsgResult, Uint128, Uint256,
};
use osmosis_std::types::osmosis::{
poolmanager::v1beta1::MsgSwapExactAmountInResponse, tokenfactory::v1beta1::MsgMint,
attr, coin, Addr, Decimal, DepsMut, Env, Fraction, MessageInfo, Response, SubMsg, SubMsgResult,
Uint128, Uint256,
};
use osmosis_std::types::osmosis::tokenfactory::v1beta1::MsgMint;

pub(crate) fn execute_exact_deposit(
mut deps: DepsMut,
Expand Down Expand Up @@ -69,7 +67,7 @@ pub(crate) fn execute_any_deposit(
pool_config.clone().token0,
pool_config.clone().token1,
)?;
let (token_in, out_denom, remainder, price) = if !deposit_info.base_refund.amount.is_zero() {
let (token_in, out_denom, price) = if !deposit_info.base_refund.amount.is_zero() {
let token_in_amount = if pool_details.current_tick > position.upper_tick {
deposit_info.base_refund.amount
} else {
Expand All @@ -81,15 +79,7 @@ pub(crate) fn execute_any_deposit(
)?
};
let token_in = coin(token_in_amount.into(), pool_config.token0.clone());
let remainder = coin(
deposit_info
.base_refund
.amount
.checked_sub(token_in.amount)?
.into(),
pool_config.token0.clone(),
);
(token_in, pool_config.token1.clone(), remainder, twap_price)
(token_in, pool_config.token1.clone(), twap_price)
} else {
let token_in_amount = if pool_details.current_tick < position.lower_tick {
deposit_info.quote_refund.amount
Expand All @@ -102,40 +92,21 @@ pub(crate) fn execute_any_deposit(
)?
};
let token_in = coin(token_in_amount.into(), pool_config.token1.clone());
let remainder = coin(
deposit_info
.quote_refund
.amount
.checked_sub(token_in.amount)?
.into(),
pool_config.token1.clone(),
);
(
token_in,
pool_config.token0.clone(),
remainder,
twap_price.inv().expect("Invalid price"),
)
};
CURRENT_SWAP_ANY_DEPOSIT.save(
deps.storage,
&(
remainder,
recipient.clone(),
(deposit_info.base_deposit, deposit_info.quote_deposit),
),
)?;
CURRENT_SWAP_RECIPIENT.save(deps.storage, &recipient)?;

let token_out_min_amount = estimate_swap_min_out_amount(token_in.amount, price, max_slippage)?;

let dex_router = DEX_ROUTER.may_load(deps.storage)?;
let swap_msg = swap_msg(
env.contract.address,
pool_config.pool_id,
vault_config.dex_router,
token_in.clone(),
coin(token_out_min_amount.into(), out_denom.clone()),
None, // TODO: check this None
dex_router,
)?;

Ok(Response::new()
Expand All @@ -154,46 +125,21 @@ pub(crate) fn execute_any_deposit(
pub fn handle_any_deposit_swap_reply(
mut deps: DepsMut,
env: Env,
data: SubMsgResult,
_data: SubMsgResult,
) -> Result<Response, ContractError> {
// Attempt to directly parse the data to MsgSwapExactAmountInResponse outside of the match
let resp: MsgSwapExactAmountInResponse = data.try_into()?;

let (remainder, recipient, deposit_amount_in_ratio) =
CURRENT_SWAP_ANY_DEPOSIT.load(deps.storage)?;
CURRENT_SWAP_ANY_DEPOSIT.remove(deps.storage);
let recipient = CURRENT_SWAP_RECIPIENT.load(deps.storage)?;
CURRENT_SWAP_RECIPIENT.remove(deps.storage);

let pool_config = POOL_CONFIG.load(deps.storage)?;
let (balance0, balance1): (Uint128, Uint128) = if remainder.denom == pool_config.token0 {
(
remainder.amount,
Uint128::new(resp.token_out_amount.parse()?),
)
} else {
(
Uint128::new(resp.token_out_amount.parse()?),
remainder.amount,
)
};

let coins_to_mint_for = (
Coin {
denom: pool_config.token0.clone(),
amount: balance0 + deposit_amount_in_ratio.0,
},
Coin {
denom: pool_config.token1.clone(),
amount: balance1 + deposit_amount_in_ratio.1,
},
);
let balances = get_unused_pair_balances(&deps, &env, &pool_config)?;

execute_deposit(
&mut deps,
env,
recipient,
DepositInfo {
base_deposit: coins_to_mint_for.0.amount,
quote_deposit: coins_to_mint_for.1.amount,
base_deposit: balances[0].amount,
quote_deposit: balances[1].amount,
base_refund: coin(0u128, pool_config.token0),
quote_refund: coin(0u128, pool_config.token1),
},
Expand Down
12 changes: 5 additions & 7 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{
math::tick::{price_to_tick, tick_to_price},
reply::Replies,
state::{
ModifyRangeState, Position, SwapDepositMergeState, DEX_ROUTER, MODIFY_RANGE_STATE,
POOL_CONFIG, POSITION, SWAP_DEPOSIT_MERGE_STATE,
ModifyRangeState, Position, SwapDepositMergeState, MODIFY_RANGE_STATE, POOL_CONFIG,
POSITION, SWAP_DEPOSIT_MERGE_STATE, VAULT_CONFIG,
},
vault::{
concentrated_liquidity::{
Expand Down Expand Up @@ -215,14 +215,12 @@ pub fn handle_withdraw_position_reply(deps: DepsMut, env: Env) -> Result<Respons
let token_out_min_amount =
estimate_swap_min_out_amount(token_in.amount, price, modify_range_state.max_slippage)?;

let dex_router = DEX_ROUTER.may_load(deps.storage)?;
let vault_config = VAULT_CONFIG.load(deps.storage)?;
let swap_msg = swap_msg(
env.contract.address,
pool_config.pool_id,
vault_config.dex_router,
token_in.clone(),
coin(token_out_min_amount.into(), out_denom.clone()),
None, // TODO: check this None
dex_router,
None,
)?;
Ok(response
.add_submessage(SubMsg::reply_on_success(swap_msg, Replies::Swap.into()))
Expand Down
Loading

0 comments on commit f69aca2

Please sign in to comment.