diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index ea8423782..31eff0e71 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -354,6 +354,8 @@ func (appKeepers *AppKeepers) InitNormalKeepers( AddRoute(ibchost.RouterKey, ibcclient.NewClientProposalHandler(appKeepers.IBCKeeper.ClientKeeper)) govConfig := govtypes.DefaultConfig() + // Set the maximum metadata length for government-related configurations to 10,200, deviating from the default value of 256. + govConfig.MaxMetadataLen = 10200 govKeeper := govkeeper.NewKeeper( appCodec, appKeepers.keys[govtypes.StoreKey], diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit index 824d4d02e..f1b5e0b3b 100755 --- a/scripts/git/pre-commit +++ b/scripts/git/pre-commit @@ -13,12 +13,13 @@ then exit 1 fi -update_schemas_and_formatting() { - local contract=$1 - if [[ $CHANGES == *contracts/${contract}* ]] +update_schemas() { + local workspace=$1 + local contract=$2 + if [[ $CHANGES == *${workspace}/contracts/${contract}* ]] then echo "Changes in ${contract} contract" - cd ${REPO_ROOT}/smart-contracts/osmosis/contracts/${contract} + cd ${REPO_ROOT}/smart-contracts/${workspace}/contracts/${contract} # generate schemas cargo schema @@ -29,6 +30,15 @@ update_schemas_and_formatting() { else echo "No schema changes for ${contract}." fi + fi +} + +fix_formatting() { + local target=$1 + if [[ $CHANGES == *${target}* ]] + then + echo "Changes in ${target}" + cd ${REPO_ROOT}/smart-contracts/${target} # fix formatting cargo fmt --all @@ -38,9 +48,17 @@ update_schemas_and_formatting() { fi } -update_schemas_and_formatting lst-dex-adapter-osmosis -update_schemas_and_formatting dex-router-osmosis -update_schemas_and_formatting token-burner -update_schemas_and_formatting cl-vault -update_schemas_and_formatting merkle-incentives -update_schemas_and_formatting range-middleware +update_schemas osmosis lst-dex-adapter-osmosis +update_schemas osmosis dex-router-osmosis +update_schemas osmosis token-burner +update_schemas osmosis cl-vault +update_schemas osmosis merkle-incentives +update_schemas osmosis range-middleware +fix_formatting osmosis/contracts/lst-dex-adapter-osmosis +fix_formatting osmosis/contracts/dex-router-osmosis +fix_formatting osmosis/contracts/token-burner +fix_formatting osmosis/contracts/cl-vault +fix_formatting osmosis/contracts/merkle-incentives +fix_formatting osmosis/contracts/range-middleware +fix_formatting osmosis/packages/quasar-types +fix_formatting quasar/proto-build diff --git a/smart-contracts/osmosis/contracts/cl-vault/src/contract.rs b/smart-contracts/osmosis/contracts/cl-vault/src/contract.rs index e78e31cf5..7241494af 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/src/contract.rs +++ b/smart-contracts/osmosis/contracts/cl-vault/src/contract.rs @@ -32,14 +32,15 @@ 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_any_deposit, execute_exact_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, diff --git a/smart-contracts/osmosis/contracts/cl-vault/src/vault/any_deposit.rs b/smart-contracts/osmosis/contracts/cl-vault/src/vault/any_deposit.rs deleted file mode 100644 index 443185180..000000000 --- a/smart-contracts/osmosis/contracts/cl-vault/src/vault/any_deposit.rs +++ /dev/null @@ -1,178 +0,0 @@ -use cosmwasm_std::{ - attr, coin, Coin, Decimal, DepsMut, Env, MessageInfo, Response, SubMsg, SubMsgResult, Uint128, -}; -use osmosis_std::types::osmosis::poolmanager::v1beta1::MsgSwapExactAmountInResponse; - -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, - }, - reply::Replies, - state::{CURRENT_SWAP_ANY_DEPOSIT, POOL_CONFIG}, - vault::{ - concentrated_liquidity::{get_cl_pool_info, get_position}, - exact_deposit::execute_deposit, - swap::{calculate_swap_amount, SwapDirection}, - }, - ContractError, -}; - -pub(crate) fn execute_any_deposit( - mut deps: DepsMut, - env: Env, - info: MessageInfo, - recipient: Option, - max_slippage: Decimal, -) -> Result { - let recipient = recipient.map_or(Ok(info.sender.clone()), |x| deps.api.addr_validate(&x))?; - - let pool_config = POOL_CONFIG.load(deps.storage)?; - let pool_details = get_cl_pool_info(&deps.querier, pool_config.pool_id)?; - let position = get_position(deps.storage, &deps.querier)? - .position - .ok_or(ContractError::MissingPosition {})?; - - // get the amount of funds we can deposit from this ratio - let (deposit_amount_in_ratio, swappable_amount) = - get_depositable_tokens(&deps.branch(), &info.funds, &pool_config)?; - - if swappable_amount.0.is_zero() && swappable_amount.1.is_zero() { - return execute_deposit( - &mut deps, - env, - recipient, - deposit_amount_in_ratio, - ( - coin(0u128, pool_config.token0), - coin(0u128, pool_config.token1), - ), - ); - } - - // Swap logic - // TODO: Optimize this if conditions - // TODO: Deprecate swapDirection here or in the calculate_swap_amount function, - // probably better here so we can do from any of the invoking places where we invoke calculate_swap_amount - let (token_in, swap_direction, left_over_amount) = if !swappable_amount.0.is_zero() { - // range is above current tick - let token_in = if pool_details.current_tick > position.upper_tick { - Coin { - denom: pool_config.token0.clone(), - amount: swappable_amount.0, - } - } else { - Coin { - denom: pool_config.token0.clone(), - amount: get_single_sided_deposit_0_to_1_swap_amount( - swappable_amount.0, - position.lower_tick, - pool_details.current_tick, - position.upper_tick, - )?, - } - }; - let left_over_amount = swappable_amount.0.checked_sub(token_in.amount)?; - (token_in, SwapDirection::ZeroToOne, left_over_amount) - } else { - // current tick is above range - let token_in = if pool_details.current_tick < position.lower_tick { - Coin { - denom: pool_config.token1.clone(), - amount: swappable_amount.1, - } - } else { - Coin { - denom: pool_config.token1.clone(), - amount: get_single_sided_deposit_1_to_0_swap_amount( - swappable_amount.1, - position.lower_tick, - pool_details.current_tick, - position.upper_tick, - )?, - } - }; - let left_over_amount = swappable_amount.1.checked_sub(token_in.amount)?; - (token_in, SwapDirection::OneToZero, left_over_amount) - }; - CURRENT_SWAP_ANY_DEPOSIT.save( - deps.storage, - &( - swap_direction.clone(), - left_over_amount, - recipient.clone(), - deposit_amount_in_ratio, - ), - )?; - let swap_calc_result = calculate_swap_amount( - &deps, - &env, - swap_direction, - token_in.clone(), - max_slippage, - None, - 0u64, // TODO: Check if we need a vault_config.twap_in_seconds as default as we do for slippage - )?; - - // rest minting logic remains same - Ok(Response::new() - .add_submessage(SubMsg::reply_on_success( - swap_calc_result.swap_msg, - Replies::AnyDepositSwap.into(), - )) - .add_attributes(vec![ - attr("method", "execute"), - attr("action", "any_deposit"), - attr("token_in", token_in.to_string()), - attr("min_token_out", swap_calc_result.min_token_out.to_string()), - ])) -} - -pub fn handle_any_deposit_swap_reply( - mut deps: DepsMut, - env: Env, - data: SubMsgResult, -) -> Result { - // Attempt to directly parse the data to MsgSwapExactAmountInResponse outside of the match - let resp: MsgSwapExactAmountInResponse = data.try_into()?; - - let (swap_direction, left_over_amount, recipient, deposit_amount_in_ratio) = - CURRENT_SWAP_ANY_DEPOSIT.load(deps.storage)?; - CURRENT_SWAP_ANY_DEPOSIT.remove(deps.storage); - - // get post swap balances to create positions with - let (balance0, balance1): (Uint128, Uint128) = match swap_direction { - SwapDirection::ZeroToOne => ( - left_over_amount, - Uint128::new(resp.token_out_amount.parse()?), - ), - SwapDirection::OneToZero => ( - Uint128::new(resp.token_out_amount.parse()?), - left_over_amount, - ), - _ => return Err(ContractError::InvalidSwapDirection {}), - }; - - let pool_config = POOL_CONFIG.load(deps.storage)?; - 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, - }, - ); - - execute_deposit( - &mut deps, - env, - recipient, - (coins_to_mint_for.0.amount, coins_to_mint_for.1.amount), - ( - coin(0u128, pool_config.token0), - coin(0u128, pool_config.token1), - ), - ) -} diff --git a/smart-contracts/osmosis/contracts/cl-vault/src/vault/exact_deposit.rs b/smart-contracts/osmosis/contracts/cl-vault/src/vault/deposit.rs similarity index 57% rename from smart-contracts/osmosis/contracts/cl-vault/src/vault/exact_deposit.rs rename to smart-contracts/osmosis/contracts/cl-vault/src/vault/deposit.rs index 3600c1f33..1bb91676e 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/src/vault/exact_deposit.rs +++ b/smart-contracts/osmosis/contracts/cl-vault/src/vault/deposit.rs @@ -1,15 +1,27 @@ -use cosmwasm_std::{coin, Addr, Coin, DepsMut, Env, MessageInfo, Response, Uint128, Uint256}; - -use osmosis_std::types::osmosis::tokenfactory::v1beta1::MsgMint; - -use crate::helpers::getters::{get_asset0_value, get_depositable_tokens}; -use crate::helpers::msgs::refund_bank_msg; -use crate::query::query_total_vault_token_supply; use crate::{ - query::query_total_assets, - state::{POOL_CONFIG, SHARES, VAULT_DENOM}, + helpers::{ + getters::{ + get_asset0_value, get_depositable_tokens, get_single_sided_deposit_0_to_1_swap_amount, + get_single_sided_deposit_1_to_0_swap_amount, + }, + msgs::refund_bank_msg, + }, + query::{query_total_assets, query_total_vault_token_supply}, + reply::Replies, + state::{CURRENT_SWAP_ANY_DEPOSIT, POOL_CONFIG, SHARES, VAULT_DENOM}, + vault::{ + concentrated_liquidity::{get_cl_pool_info, get_position}, + swap::{calculate_swap_amount, SwapDirection}, + }, ContractError, }; +use cosmwasm_std::{ + attr, coin, Addr, Coin, Decimal, DepsMut, Env, MessageInfo, Response, SubMsg, SubMsgResult, + Uint128, Uint256, +}; +use osmosis_std::types::osmosis::{ + poolmanager::v1beta1::MsgSwapExactAmountInResponse, tokenfactory::v1beta1::MsgMint, +}; pub(crate) fn execute_exact_deposit( mut deps: DepsMut, @@ -34,9 +46,160 @@ pub(crate) fn execute_exact_deposit( ) } +pub(crate) fn execute_any_deposit( + mut deps: DepsMut, + env: Env, + info: MessageInfo, + recipient: Option, + max_slippage: Decimal, +) -> Result { + let recipient = recipient.map_or(Ok(info.sender.clone()), |x| deps.api.addr_validate(&x))?; + + let pool_config = POOL_CONFIG.load(deps.storage)?; + let pool_details = get_cl_pool_info(&deps.querier, pool_config.pool_id)?; + let position = get_position(deps.storage, &deps.querier)? + .position + .ok_or(ContractError::MissingPosition {})?; + + // get the amount of funds we can deposit from this ratio + let (deposit_amount_in_ratio, swappable_amount): ((Uint128, Uint128), (Uint128, Uint128)) = + get_depositable_tokens(&deps.branch(), &info.funds, &pool_config)?; + + if swappable_amount.0.is_zero() && swappable_amount.1.is_zero() { + return execute_deposit( + &mut deps, + env, + recipient, + deposit_amount_in_ratio, + ( + coin(0u128, pool_config.token0), + coin(0u128, pool_config.token1), + ), + ); + } + + // Swap logic + // TODO_FUTURE: Optimize this if conditions + let (swap_amount, swap_direction, left_over_amount) = if !swappable_amount.0.is_zero() { + // range is above current tick + let swap_amount = if pool_details.current_tick > position.upper_tick { + swappable_amount.0 + } else { + get_single_sided_deposit_0_to_1_swap_amount( + swappable_amount.0, + position.lower_tick, + pool_details.current_tick, + position.upper_tick, + )? + }; + let left_over_amount = swappable_amount.0.checked_sub(swap_amount)?; + (swap_amount, SwapDirection::ZeroToOne, left_over_amount) + } else { + // current tick is above range + let swap_amount = if pool_details.current_tick < position.lower_tick { + swappable_amount.1 + } else { + get_single_sided_deposit_1_to_0_swap_amount( + swappable_amount.1, + position.lower_tick, + pool_details.current_tick, + position.upper_tick, + )? + }; + let left_over_amount = swappable_amount.1.checked_sub(swap_amount)?; + (swap_amount, SwapDirection::OneToZero, left_over_amount) + }; + CURRENT_SWAP_ANY_DEPOSIT.save( + deps.storage, + &( + swap_direction.clone(), + left_over_amount, + recipient.clone(), + deposit_amount_in_ratio, + ), + )?; + let swap_calc_result = calculate_swap_amount( + deps, + &env, + pool_config, + swap_direction, + swap_amount, + max_slippage, + None, // TODO: check this None + 24u64, + )?; + + // rest minting logic remains same + Ok(Response::new() + .add_submessage(SubMsg::reply_on_success( + swap_calc_result.swap_msg, + Replies::AnyDepositSwap.into(), + )) + .add_attributes(vec![ + attr("method", "execute"), + attr("action", "any_deposit"), + attr( + "token_in", + format!("{}{}", swap_amount, swap_calc_result.token_in_denom), + ), + attr( + "token_out_min", + format!("{}", swap_calc_result.token_out_min_amount), + ), + ])) +} + +pub fn handle_any_deposit_swap_reply( + mut deps: DepsMut, + env: Env, + data: SubMsgResult, +) -> Result { + // Attempt to directly parse the data to MsgSwapExactAmountInResponse outside of the match + let resp: MsgSwapExactAmountInResponse = data.try_into()?; + + let (swap_direction, left_over_amount, recipient, deposit_amount_in_ratio) = + CURRENT_SWAP_ANY_DEPOSIT.load(deps.storage)?; + CURRENT_SWAP_ANY_DEPOSIT.remove(deps.storage); + + // get post swap balances to create positions with + let (balance0, balance1): (Uint128, Uint128) = match swap_direction { + SwapDirection::ZeroToOne => ( + left_over_amount, + Uint128::new(resp.token_out_amount.parse()?), + ), + SwapDirection::OneToZero => ( + Uint128::new(resp.token_out_amount.parse()?), + left_over_amount, + ), + }; + + let pool_config = POOL_CONFIG.load(deps.storage)?; + 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, + }, + ); + + execute_deposit( + &mut deps, + env, + recipient, + (coins_to_mint_for.0.amount, coins_to_mint_for.1.amount), + ( + coin(0u128, pool_config.token0), + coin(0u128, pool_config.token1), + ), + ) +} + /// Try to deposit as much user funds as we can in the current ratio of the vault and /// refund the rest to the caller. -pub(crate) fn execute_deposit( +fn execute_deposit( deps: &mut DepsMut, env: Env, recipient: Addr, diff --git a/smart-contracts/osmosis/contracts/cl-vault/src/vault/mod.rs b/smart-contracts/osmosis/contracts/cl-vault/src/vault/mod.rs index b4aa809cb..1b628ec8d 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/src/vault/mod.rs +++ b/smart-contracts/osmosis/contracts/cl-vault/src/vault/mod.rs @@ -1,9 +1,8 @@ pub mod admin; -pub mod any_deposit; pub mod autocompound; pub mod concentrated_liquidity; +pub mod deposit; pub mod distribution; -pub mod exact_deposit; pub mod merge; pub mod range; pub mod swap;