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

CL: Swap Non Vault Funds (#782) #802

Merged
merged 15 commits into from
Aug 23, 2024
32 changes: 24 additions & 8 deletions smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"required": [
"dex_router",
"performance_fee",
"swap_admin",
"swap_max_slippage",
"treasury"
],
Expand All @@ -86,6 +87,9 @@
"performance_fee": {
"$ref": "#/definitions/Decimal"
},
"swap_admin": {
"$ref": "#/definitions/Addr"
},
"swap_max_slippage": {
"$ref": "#/definitions/Decimal"
},
Expand Down Expand Up @@ -554,6 +558,14 @@
"items": {
"$ref": "#/definitions/SwapOperation"
}
},
"twap_window_seconds": {
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
Expand Down Expand Up @@ -687,12 +699,12 @@
"SwapOperation": {
"type": "object",
"required": [
"pool_id_0",
"pool_id_1",
"pool_id_base",
"pool_id_quote",
"token_in_denom"
],
"properties": {
"forced_swap_route_token_0": {
"forced_swap_route_base": {
"type": [
"array",
"null"
Expand All @@ -701,7 +713,7 @@
"$ref": "#/definitions/SwapAmountInRoute"
}
},
"forced_swap_route_token_1": {
"forced_swap_route_quote": {
"type": [
"array",
"null"
Expand All @@ -710,12 +722,12 @@
"$ref": "#/definitions/SwapAmountInRoute"
}
},
"pool_id_0": {
"pool_id_base": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"pool_id_1": {
"pool_id_quote": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
Expand All @@ -735,6 +747,7 @@
"required": [
"dex_router",
"performance_fee",
"swap_admin",
"swap_max_slippage",
"treasury"
],
Expand All @@ -745,6 +758,9 @@
"performance_fee": {
"$ref": "#/definitions/Decimal"
},
"swap_admin": {
"$ref": "#/definitions/Addr"
},
"swap_max_slippage": {
"$ref": "#/definitions/Decimal"
},
Expand Down Expand Up @@ -1128,10 +1144,10 @@
"title": "MigrateMsg",
"type": "object",
"required": [
"dex_router"
"swap_admin"
],
"properties": {
"dex_router": {
"swap_admin": {
"$ref": "#/definitions/Addr"
}
},
Expand Down
24 changes: 18 additions & 6 deletions smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@
"items": {
"$ref": "#/definitions/SwapOperation"
}
},
"twap_window_seconds": {
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
Expand Down Expand Up @@ -588,12 +596,12 @@
"SwapOperation": {
"type": "object",
"required": [
"pool_id_0",
"pool_id_1",
"pool_id_base",
"pool_id_quote",
"token_in_denom"
],
"properties": {
"forced_swap_route_token_0": {
"forced_swap_route_base": {
"type": [
"array",
"null"
Expand All @@ -602,7 +610,7 @@
"$ref": "#/definitions/SwapAmountInRoute"
}
},
"forced_swap_route_token_1": {
"forced_swap_route_quote": {
"type": [
"array",
"null"
Expand All @@ -611,12 +619,12 @@
"$ref": "#/definitions/SwapAmountInRoute"
}
},
"pool_id_0": {
"pool_id_base": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"pool_id_1": {
"pool_id_quote": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
Expand All @@ -636,6 +644,7 @@
"required": [
"dex_router",
"performance_fee",
"swap_admin",
"swap_max_slippage",
"treasury"
],
Expand All @@ -646,6 +655,9 @@
"performance_fee": {
"$ref": "#/definitions/Decimal"
},
"swap_admin": {
"$ref": "#/definitions/Addr"
},
"swap_max_slippage": {
"$ref": "#/definitions/Decimal"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"required": [
"dex_router",
"performance_fee",
"swap_admin",
"swap_max_slippage",
"treasury"
],
Expand All @@ -82,6 +83,9 @@
"performance_fee": {
"$ref": "#/definitions/Decimal"
},
"swap_admin": {
"$ref": "#/definitions/Addr"
},
"swap_max_slippage": {
"$ref": "#/definitions/Decimal"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"title": "MigrateMsg",
"type": "object",
"required": [
"dex_router"
"swap_admin"
],
"properties": {
"dex_router": {
"swap_admin": {
"$ref": "#/definitions/Addr"
}
},
Expand Down
43 changes: 32 additions & 11 deletions smart-contracts/osmosis/contracts/cl-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use crate::query::{
query_user_balance, query_verify_tick_cache, RangeAdminResponse,
};
use crate::reply::Replies;
#[allow(deprecated)]
use crate::state::{MigrationStatus, MIGRATION_STATUS};
use crate::state::{VaultConfig, VAULT_CONFIG};
use crate::vault::{
admin::execute_admin,
autocompound::{
Expand All @@ -34,11 +33,14 @@ use crate::vault::{
swap::execute_swap_non_vault_funds,
withdraw::{execute_withdraw, handle_withdraw_user_reply},
};
use cosmwasm_schema::cw_serde;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response};
use cosmwasm_std::{
to_json_binary, Addr, Binary, Decimal, Deps, DepsMut, Env, MessageInfo, Reply, Response,
};
use cw2::set_contract_version;

use cw_storage_plus::Item;
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cl-vault";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -63,10 +65,9 @@ pub fn execute(
) -> Result<Response, ContractError> {
match msg {
cw_vault_multi_standard::VaultStandardExecuteMsg::AnyDeposit {
amount: _,
asset: _,
recipient,
max_slippage,
.. // asset and amount fields are not used in this implementation, they are for CW20 tokens
} => execute_any_deposit(deps, env, info, recipient, max_slippage),
cw_vault_multi_standard::VaultStandardExecuteMsg::ExactDeposit { recipient } => {
execute_exact_deposit(deps, env, info, recipient)
Expand Down Expand Up @@ -123,9 +124,10 @@ pub fn execute(
claim_after,
)?,
),
crate::msg::ExtensionExecuteMsg::SwapNonVaultFunds { swap_operations } => {
execute_swap_non_vault_funds(deps, env.contract.address, info, swap_operations)
}
crate::msg::ExtensionExecuteMsg::SwapNonVaultFunds {
swap_operations,
twap_window_seconds,
} => execute_swap_non_vault_funds(deps, env, info, swap_operations, twap_window_seconds),
crate::msg::ExtensionExecuteMsg::CollectRewards {} => {
execute_collect_rewards(deps, env)
}
Expand Down Expand Up @@ -214,8 +216,27 @@ 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> {
MIGRATION_STATUS.save(deps.storage, &MigrationStatus::Open)?;
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
magiodev marked this conversation as resolved.
Show resolved Hide resolved
#[cw_serde]
struct OldVaultConfig {
pub performance_fee: Decimal,
pub treasury: Addr,
pub swap_max_slippage: Decimal,
pub dex_router: Addr,
}
const OLD_VAULT_CONFIG: Item<OldVaultConfig> = Item::new("vault_config_v2");
let old_vault_config: OldVaultConfig = OLD_VAULT_CONFIG.load(deps.storage)?;

VAULT_CONFIG.save(
deps.storage,
&VaultConfig {
performance_fee: old_vault_config.performance_fee,
treasury: old_vault_config.treasury,
swap_max_slippage: old_vault_config.swap_max_slippage,
dex_router: old_vault_config.dex_router,
swap_admin: msg.swap_admin,
},
)?;

let response = Response::new().add_attribute("migrate", "successful");
Ok(response)
Expand Down
10 changes: 9 additions & 1 deletion smart-contracts/osmosis/contracts/cl-vault/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::state::{ADMIN_ADDRESS, RANGE_ADMIN};
use crate::state::{ADMIN_ADDRESS, RANGE_ADMIN, VAULT_CONFIG};
use cosmwasm_std::{
Addr, CheckedFromRatioError, CheckedMultiplyFractionError, CheckedMultiplyRatioError, Coin,
CoinFromStrError, ConversionOverflowError, Decimal, Decimal256, Decimal256RangeExceeded,
Expand Down Expand Up @@ -179,6 +179,14 @@ pub fn assert_range_admin(storage: &dyn Storage, sender: &Addr) -> Result<(), Co
Ok(())
}

pub fn assert_swap_admin(storage: &dyn Storage, sender: &Addr) -> Result<(), ContractError> {
let vault_config = VAULT_CONFIG.load(storage)?;
if vault_config.swap_admin != sender {
return Err(ContractError::Unauthorized {});
}
Ok(())
}

pub fn assert_ratio(ratio: Decimal) -> Result<(), ContractError> {
if ratio > Decimal::one() || ratio <= Decimal::zero() {
return Err(ContractError::InvalidRatioOfSwappableFundsToUse {});
Expand Down
22 changes: 11 additions & 11 deletions smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
ContractError,
};
use cosmwasm_std::{
coin, Addr, Coin, Decimal, Decimal256, Deps, DepsMut, Env, QuerierWrapper, Storage, Uint128,
Uint256,
coin, Addr, Coin, Decimal, Decimal256, Deps, DepsMut, Env, QuerierWrapper, Storage, Timestamp,
Uint128, Uint256,
};

use super::coinlist::CoinList;
Expand All @@ -24,7 +24,7 @@ pub fn get_range_admin(deps: Deps) -> Result<Addr, ContractError> {
}

/// Calculate the total value of two assets in asset0.
pub fn get_asset0_value(
pub fn get_value_wrt_asset0(
storage: &dyn Storage,
querier: &QuerierWrapper,
token0: Uint128,
Expand All @@ -44,19 +44,19 @@ pub fn get_asset0_value(
}

pub fn get_twap_price(
storage: &dyn Storage,
querier: &QuerierWrapper,
env: &Env,
block_time: Timestamp,
twap_window_seconds: u64,
pool_id: u64,
token0_denom: String,
token1_denom: String,
) -> Result<Decimal, ContractError> {
let pool_config = POOL_CONFIG.load(storage)?;

let twap_querier = TwapQuerier::new(querier);
let start_of_window = env.block.time.minus_seconds(twap_window_seconds);
let start_of_window = block_time.minus_seconds(twap_window_seconds);
let twap_price = twap_querier.arithmetic_twap_to_now(
pool_config.pool_id,
pool_config.token0,
pool_config.token1,
pool_id,
token0_denom,
token1_denom,
Some(OsmoTimestamp {
seconds: start_of_window.seconds().try_into()?,
nanos: 0,
Expand Down
4 changes: 2 additions & 2 deletions smart-contracts/osmosis/contracts/cl-vault/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use osmosis_std::types::osmosis::tokenfactory::v1beta1::{
};

use crate::helpers::assert::must_pay_one_or_two;
use crate::helpers::getters::get_asset0_value;
use crate::helpers::getters::get_value_wrt_asset0;
use crate::math::tick::{build_tick_exp_cache, verify_tick_exp_cache};
use crate::msg::InstantiateMsg;
use crate::reply::Replies;
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn handle_instantiate_create_position_reply(
.querier
.query_balance(&env.contract.address, assets[1].denom.clone())?;

let asset_value = get_asset0_value(
let asset_value = get_value_wrt_asset0(
deps.storage,
&deps.querier,
assets[0].amount + free_asset0.amount,
Expand Down
Loading
Loading