From a7f456a87edb2169a1eb228a7564f470159dd97f Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:36:32 +0100 Subject: [PATCH 01/24] extended default_init capability by adding tokens provided arg --- .../contracts/cl-vault/src/test_tube/admin.rs | 16 +- .../src/test_tube/deposit_withdraw.rs | 34 +++- .../cl-vault/src/test_tube/initialize.rs | 49 +++--- .../contracts/cl-vault/src/test_tube/range.rs | 151 +++++------------- .../cl-vault/src/test_tube/rewards.rs | 46 +++++- 5 files changed, 149 insertions(+), 147 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs index 4c44ff450..2ce06feed 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs @@ -1,11 +1,25 @@ #[cfg(test)] mod tests { + use osmosis_std::types::cosmos::base::v1beta1; + use crate::test_tube::initialize::initialize::default_init; + const DENOM_BASE: &str = "uatom"; + const DENOM_QUOTE: &str = "uosmo"; + #[test] #[ignore] fn range_admin_update_works() { - let (_app, _contract_address, _cl_pool_id, _admin) = default_init(); + let (_app, _contract_address, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); // change the range admin and verify that it works } } diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs index 19ffc3d71..03d053407 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs @@ -2,6 +2,7 @@ mod tests { use cosmwasm_std::{assert_approx_eq, Coin, Uint128}; + use osmosis_std::types::cosmos::base::v1beta1; use osmosis_test_tube::{Account, Module, Wasm}; use crate::{ @@ -17,7 +18,16 @@ mod tests { #[test] #[ignore] fn single_deposit_withdraw_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(); + let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); let wasm = Wasm::new(&app); // Create Alice account @@ -126,7 +136,16 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(); + let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); let wasm = Wasm::new(&app); // Create Alice account @@ -254,7 +273,16 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_unused_funds_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(); + let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); //let bank = Bank::new(&app); let wasm = Wasm::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index ebd4cbcc2..2bcd4df8f 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -28,37 +28,29 @@ pub mod initialize { use crate::query::PoolResponse; use crate::state::VaultConfig; - const ADMIN_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455u128; - const TOKENS_PROVIDED_AMOUNT: &str = "1000000000000"; - const DENOM_BASE: &str = "uatom"; - const DENOM_QUOTE: &str = "uosmo"; + const ADMIN_BALANCE_AMOUNT: u128 = 100_000_000_000_000_000_000; - pub fn default_init() -> (OsmosisTestApp, Addr, u64, SigningAccount) { + // Define init variants here + + pub fn default_init( + tokens_provided: Vec, + ) -> (OsmosisTestApp, Addr, u64, SigningAccount) { init_test_contract( "./test-tube-build/wasm32-unknown-unknown/release/cl_vault.wasm", &[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), + Coin::new(ADMIN_BALANCE_AMOUNT, tokens_provided[0].denom.clone()), + Coin::new(ADMIN_BALANCE_AMOUNT, tokens_provided[1].denom.clone()), ], MsgCreateConcentratedPool { sender: "overwritten".to_string(), - denom0: DENOM_BASE.to_string(), - denom1: DENOM_QUOTE.to_string(), + denom0: tokens_provided[0].denom.to_string(), + denom1: tokens_provided[1].denom.to_string(), tick_spacing: 100, spread_factor: Decimal::from_str("0.01").unwrap().atomics().to_string(), }, -5000000, // 0.5 spot price 500000, // 1.5 spot price - vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - ], + tokens_provided, Uint128::zero(), Uint128::zero(), ) @@ -177,7 +169,16 @@ pub mod initialize { #[test] #[ignore] fn default_init_works() { - let (app, contract_address, cl_pool_id, admin) = default_init(); + let (app, contract_address, cl_pool_id, admin) = default_init(vec![ + v1beta1::Coin { + denom: "uatom".to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: "uosmo".to_string(), + amount: "1000000000000".to_string(), + }, + ]); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let tf = TokenFactory::new(&app); @@ -216,8 +217,8 @@ pub mod initialize { // Create Alice account let alice = app .init_account(&[ - Coin::new(1_000_000_000_000, DENOM_BASE), - Coin::new(1_000_000_000_000, DENOM_QUOTE), + Coin::new(1_000_000_000_000, "uatom"), + Coin::new(1_000_000_000_000, "uosmo"), ]) .unwrap(); @@ -227,10 +228,10 @@ pub mod initialize { sender: alice.address(), routes: vec![SwapAmountInRoute { pool_id: cl_pool_id, - token_out_denom: DENOM_BASE.to_string(), + token_out_denom: "uatom".to_string(), }], token_in: Some(v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), + denom: "uosmo".to_string(), amount: "1000".to_string(), }), token_out_min_amount: "1".to_string(), diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index b06900ed2..35be97a5b 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -1,29 +1,23 @@ #[cfg(test)] mod test { - use std::str::FromStr; - use cosmwasm_std::{coin, Coin, Decimal, Uint128}; use osmosis_std::types::{ cosmos::base::v1beta1, osmosis::{ - concentratedliquidity::{ - poolmodel::concentrated::v1beta1::MsgCreateConcentratedPool, - v1beta1::{MsgCreatePosition, Pool, PoolsRequest}, - }, + concentratedliquidity::v1beta1::{MsgCreatePosition, Pool, PoolsRequest}, poolmanager::v1beta1::{MsgSwapExactAmountIn, SwapAmountInRoute}, }, }; use osmosis_test_tube::{Account, ConcentratedLiquidity, Module, PoolManager, Wasm}; + use prost::Message; + use std::str::FromStr; use crate::{ msg::{ExecuteMsg, ModifyRangeMsg, QueryMsg}, query::PositionResponse, - test_tube::initialize::initialize::init_test_contract, + test_tube::initialize::initialize::default_init, }; - use prost::Message; - - const ADMIN_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455u128; const TOKENS_PROVIDED_AMOUNT: &str = "1000000000000"; const DENOM_BASE: &str = "uatom"; const DENOM_QUOTE: &str = "uosmo"; @@ -31,35 +25,16 @@ mod test { #[test] #[ignore] fn move_range_works() { - let (app, contract, cl_pool_id, admin) = init_test_contract( - // TODO: Evaluate creating a default_init() variant i.e. out_of_range_init() - "./test-tube-build/wasm32-unknown-unknown/release/cl_vault.wasm", - &[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), - ], - MsgCreateConcentratedPool { - sender: "overwritten".to_string(), - denom0: DENOM_BASE.to_string(), - denom1: DENOM_QUOTE.to_string(), - tick_spacing: 100, - spread_factor: Decimal::from_str("0.0001").unwrap().atomics().to_string(), + let (app, contract, cl_pool_id, admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), }, - 21205000, - 27448000, - vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - ], - Uint128::zero(), - Uint128::zero(), - ); + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); @@ -87,18 +62,11 @@ mod test { ) .unwrap(); - let alice = app - .init_account(&[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), - ]) - .unwrap(); - // do a swap to move the cur tick let pm = PoolManager::new(&app); pm.swap_exact_amount_in( MsgSwapExactAmountIn { - sender: alice.address(), + sender: admin.address(), routes: vec![SwapAmountInRoute { pool_id: cl_pool_id, token_out_denom: DENOM_BASE.to_string(), @@ -109,7 +77,7 @@ mod test { }), token_out_min_amount: "1".to_string(), }, - &alice, + &admin, ) .unwrap(); @@ -155,35 +123,16 @@ mod test { #[test] #[ignore] fn move_range_same_single_side_works() { - let (app, contract, cl_pool_id, admin) = init_test_contract( - // TODO: Evaluate creating a default_init() variant i.e. out_of_range_init() - "./test-tube-build/wasm32-unknown-unknown/release/cl_vault.wasm", - &[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), - ], - MsgCreateConcentratedPool { - sender: "overwritten".to_string(), - denom0: DENOM_BASE.to_string(), - denom1: DENOM_QUOTE.to_string(), - tick_spacing: 100, - spread_factor: Decimal::from_str("0.0001").unwrap().atomics().to_string(), + let (app, contract, cl_pool_id, admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), }, - 21205000, - 27448000, - vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: TOKENS_PROVIDED_AMOUNT.to_string(), - }, - ], - Uint128::zero(), - Uint128::zero(), - ); + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let pm = PoolManager::new(&app); @@ -212,17 +161,10 @@ mod test { ) .unwrap(); - let alice = app - .init_account(&[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), - ]) - .unwrap(); - // do a swap to move the cur tick pm.swap_exact_amount_in( MsgSwapExactAmountIn { - sender: alice.address(), + sender: admin.address(), routes: vec![SwapAmountInRoute { pool_id: cl_pool_id, token_out_denom: DENOM_BASE.to_string(), @@ -233,7 +175,7 @@ mod test { }), token_out_min_amount: "1".to_string(), }, - &alice, + &admin, ) .unwrap(); @@ -272,35 +214,18 @@ mod test { #[test] #[ignore] fn test_swap_math_poc() { - let (app, _contract, _cl_pool_id, _admin) = init_test_contract( - // TODO: Evaluate using default_init() - "./test-tube-build/wasm32-unknown-unknown/release/cl_vault.wasm", - &[ - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_BASE), - Coin::new(ADMIN_BALANCE_AMOUNT, DENOM_QUOTE), - ], - MsgCreateConcentratedPool { - sender: "overwritten".to_string(), - denom0: DENOM_BASE.to_string(), //token0 is uatom - denom1: DENOM_QUOTE.to_string(), //token1 is uosmo - tick_spacing: 100, - spread_factor: Decimal::from_str("0.0001").unwrap().atomics().to_string(), + let (app, _contract, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), }, - 30500000, // 4500 - 31500000, // 5500 - vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000".to_string(), - }, - ], - Uint128::zero(), - Uint128::zero(), - ); + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); + let cl = ConcentratedLiquidity::new(&app); + let alice = app .init_account(&[ Coin::new(1_000_000_000_000, DENOM_BASE), @@ -308,8 +233,6 @@ mod test { ]) .unwrap(); - let cl = ConcentratedLiquidity::new(&app); - let pools = cl.query_pools(&PoolsRequest { pagination: None }).unwrap(); let pool: Pool = Pool::decode(pools.pools[0].value.as_slice()).unwrap(); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index 839d47d11..6cb398b16 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -8,7 +8,7 @@ mod tests { }; use crate::test_tube::initialize::initialize::default_init; use cosmwasm_std::{assert_approx_eq, Coin, Uint128}; - use osmosis_std::types::cosmos::base::v1beta1::Coin as OsmoCoin; + use osmosis_std::types::cosmos::base::v1beta1::{self, Coin as OsmoCoin}; use osmosis_std::types::osmosis::poolmanager::v1beta1::{ MsgSwapExactAmountIn, SwapAmountInRoute, }; @@ -27,7 +27,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim() { - let (app, contract_address, cl_pool_id, _admin) = default_init(); + let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); // Initialize accounts let accounts = app @@ -220,7 +229,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_cycles() { - let (app, contract_address, cl_pool_id, _admin) = default_init(); + let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); // Initialize accounts let accounts = app @@ -406,7 +424,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_no_rewards_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(); + let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); // Initialize accounts let accounts = app @@ -506,7 +533,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_deposit_between() { - let (app, contract_address, cl_pool_id, _admin) = default_init(); + let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ + v1beta1::Coin { + denom: DENOM_BASE.to_string(), + amount: "1000000000000".to_string(), + }, + v1beta1::Coin { + denom: DENOM_QUOTE.to_string(), + amount: "1000000000000".to_string(), + }, + ]); // Initialize accounts let accounts = app From cc579e51bc67a8b7e2d253b6bdbc89f51f2ebe6d Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:57:54 +0100 Subject: [PATCH 02/24] spot price check on test_init_contract --- .../cl-vault/src/test_tube/initialize.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 2bcd4df8f..73145bf6f 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -125,14 +125,31 @@ pub mod initialize { .unwrap(); // Get and assert spot price is 1.0 - let spot_price = pm + let spot_price: osmosis_std::types::osmosis::poolmanager::v1beta1::SpotPriceResponse = pm .query_spot_price(&SpotPriceRequest { base_asset_denom: tokens_provided[0].denom.to_string(), quote_asset_denom: tokens_provided[1].denom.to_string(), pool_id: pool.id, }) .unwrap(); - assert_eq!(spot_price.spot_price, "1.000000000000000000"); + // Assuming tokens_provided[0].amount and tokens_provided[1].amount are String + let tokens_provided_0_amount: u128 = + tokens_provided[0].amount.parse().expect("Invalid number"); + let tokens_provided_1_amount: u128 = + tokens_provided[1].amount.parse().expect("Invalid number"); + // Assuming `spot_price.spot_price` is a string representation of a float. + let spot_price_float: f64 = spot_price + .spot_price + .parse() + .expect("Failed to parse spot price as float"); + let division_result: f64 = + tokens_provided_1_amount as f64 / tokens_provided_0_amount as f64; + assert!( + (spot_price_float - division_result).abs() < f64::EPSILON, + "Assertion failed: spot prices do not match. Expected: {}, got: {}", + spot_price_float, + division_result + ); // Increment the app time for twaps to function, this is needed to do not fail on querying a twap for a timeframe higher than the chain existence app.increase_time(1000000); From 9cc697a75d51f31afd019ea13a6973cf12d5e3f5 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 14:07:06 +0100 Subject: [PATCH 03/24] more enhancement on generic default_ionit function --- .../contracts/cl-vault/src/test_tube/admin.rs | 3 +- .../src/test_tube/deposit_withdraw.rs | 9 +++-- .../cl-vault/src/test_tube/initialize.rs | 39 +++++++++++++------ .../cl-vault/src/test_tube/rewards.rs | 8 ++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs index 2ce06feed..195ebe237 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs @@ -19,7 +19,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); // change the range admin and verify that it works } } diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs index 03d053407..822088923 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs @@ -27,7 +27,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); let wasm = Wasm::new(&app); // Create Alice account @@ -145,7 +146,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); let wasm = Wasm::new(&app); // Create Alice account @@ -282,7 +284,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); //let bank = Bank::new(&app); let wasm = Wasm::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 73145bf6f..027815e83 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -2,7 +2,7 @@ pub mod initialize { use std::str::FromStr; - use cosmwasm_std::{coin, Addr, Coin, Decimal, Uint128}; + use cosmwasm_std::{coin, Addr, Coin, Decimal, StdError, Uint128}; use cw_vault_multi_standard::VaultInfoResponse; use osmosis_std::types::cosmos::base::v1beta1; use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::{ @@ -34,17 +34,32 @@ pub mod initialize { pub fn default_init( tokens_provided: Vec, - ) -> (OsmosisTestApp, Addr, u64, SigningAccount) { - init_test_contract( + ) -> Result<(OsmosisTestApp, Addr, u64, SigningAccount), StdError> { + if tokens_provided.len() > 2 { + return Err(StdError::generic_err("More than two tokens provided")); + } + + // Prepare admin balances, including "uosmo" if neither of the provided tokens is "uosmo" + let mut admin_balances = tokens_provided + .iter() + .map(|coin| Coin::new(ADMIN_BALANCE_AMOUNT, coin.denom.clone())) + .collect::>(); + + if !tokens_provided.iter().any(|coin| coin.denom == "uosmo") { + admin_balances.push(Coin::new(ADMIN_BALANCE_AMOUNT, "uosmo".to_string())); + } + + let (app, contract, cl_pool_id, admin) = init_test_contract( "./test-tube-build/wasm32-unknown-unknown/release/cl_vault.wasm", - &[ - Coin::new(ADMIN_BALANCE_AMOUNT, tokens_provided[0].denom.clone()), - Coin::new(ADMIN_BALANCE_AMOUNT, tokens_provided[1].denom.clone()), - ], + &admin_balances, MsgCreateConcentratedPool { sender: "overwritten".to_string(), - denom0: tokens_provided[0].denom.to_string(), - denom1: tokens_provided[1].denom.to_string(), + denom0: tokens_provided + .get(0) + .map_or(String::new(), |coin| coin.denom.clone()), + denom1: tokens_provided + .get(1) + .map_or(String::new(), |coin| coin.denom.clone()), tick_spacing: 100, spread_factor: Decimal::from_str("0.01").unwrap().atomics().to_string(), }, @@ -53,7 +68,9 @@ pub mod initialize { tokens_provided, Uint128::zero(), Uint128::zero(), - ) + ); + + Ok((app, contract, cl_pool_id, admin)) } pub fn init_test_contract( @@ -195,7 +212,7 @@ pub mod initialize { denom: "uosmo".to_string(), amount: "1000000000000".to_string(), }, - ]); + ]).unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let tf = TokenFactory::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index 6cb398b16..c4f70bd64 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -36,7 +36,7 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]).unwrap(); // Initialize accounts let accounts = app @@ -238,7 +238,7 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]).unwrap(); // Initialize accounts let accounts = app @@ -433,7 +433,7 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]).unwrap(); // Initialize accounts let accounts = app @@ -542,7 +542,7 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]).unwrap(); // Initialize accounts let accounts = app From e1ab0a0c4f9b23490a9c52e78d32b3961f16d750 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:18:43 +0100 Subject: [PATCH 04/24] range dym test --- .../cl-vault/src/test_tube/initialize.rs | 5 +- .../contracts/cl-vault/src/test_tube/range.rs | 165 ++++++++++++++++-- .../cl-vault/src/test_tube/rewards.rs | 12 +- 3 files changed, 161 insertions(+), 21 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 027815e83..dc150d63c 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -28,7 +28,7 @@ pub mod initialize { use crate::query::PoolResponse; use crate::state::VaultConfig; - const ADMIN_BALANCE_AMOUNT: u128 = 100_000_000_000_000_000_000; + const ADMIN_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455; // Define init variants here @@ -212,7 +212,8 @@ pub mod initialize { denom: "uosmo".to_string(), amount: "1000000000000".to_string(), }, - ]).unwrap(); + ]) + .unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let tf = TokenFactory::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index 35be97a5b..d6222e97d 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod test { - use cosmwasm_std::{coin, Coin, Decimal, Uint128}; + use cosmwasm_std::{coin, Decimal, Uint128}; use osmosis_std::types::{ cosmos::base::v1beta1, osmosis::{ @@ -22,6 +22,145 @@ mod test { const DENOM_BASE: &str = "uatom"; const DENOM_QUOTE: &str = "uosmo"; + /// # Test: move_range_works_dym_usdc + /// + /// This test case initializes a Concentrated Liquidity (CL) pool with DYM and USDC tokens + /// to simulate a real-world scenario on the blockchain with a specific spot price. The purpose + /// of this test is to ensure that operations such as moving the range within the CL pool function + /// correctly, especially when dealing with tokens of different decimal precisions. + /// + /// ## Initialization Parameters: + /// - DYM Token (udym): 18 decimal places, represented as `1000000000000000000udym` (for 1 DYM). + /// - USDC Token (uusdc): 6 decimal places, represented as `7250000uusdc` (to establish a spot price of 7.25 USDC for 1 DYM). + /// + /// The test initializes a CL pool with these tokens to establish a spot price of 7.25 DYM/USD. + /// This spot price accurately reflects the real-world ratio between DYM and USDC on the mainnet, + /// adjusted for the blockchain's unit representation. + /// + /// ## Decimal Precision and Spot Price Consideration: + /// The significant difference in decimal places between DYM (18 decimals) and USDC (6 decimals) + /// necessitates precise calculation to ensure the spot price is accurately represented in the + /// blockchain's terms. The chosen amounts of `1000000000000000000udym` for DYM and `7250000uusdc` + /// for USDC effectively establish a starting spot price of 7.25 DYM/USD in the CL pool, accurately + /// representing the spot price in a manner that does not require adjustment for decimal places in + /// the context of Osmosis' handling of token amounts. + /// + /// Spot price it would be: `spot_price = 7250000 / 1000000000000000000`, + /// calculating the spot price in raw integer format without adjusting for decimal places, representing the USDC required to purchase one unit of DYM. + #[test] + #[ignore] + fn move_range_works_dym_usdc() { + let (app, contract, cl_pool_id, admin) = default_init(vec![ + v1beta1::Coin { + denom: "udym".to_string(), + amount: "1000000000000000000".to_string(), + }, + v1beta1::Coin { + denom: "uusdc".to_string(), + amount: "7250000".to_string(), + }, + ]) + .unwrap(); + let wasm = Wasm::new(&app); + let cl = ConcentratedLiquidity::new(&app); + // let pm = PoolManager::new(&app); + + // // Get pool information + // let pool_response = pm + // .query_pool(&PoolRequest { + // pool_id: cl_pool_id, + // }) + // .unwrap(); + // println!("pool: {:?}", pool_response); + + // Pool.CurrentTick here is -101750000 + // If we go out of upper range we will be one-sided token0 (base denom, left denom) + // If we go out of lower range we will be one-sided token1 (quote denom, right denom) + + // Create a second position (in range) in the pool with the admin user to allow for swapping during update range operation + cl.create_position( + MsgCreatePosition { + pool_id: cl_pool_id, + sender: admin.address(), + lower_tick: -101760000, // -10000 ticks + upper_tick: -101740000, // +10000 ticks + tokens_provided: vec![ + v1beta1::Coin { + denom: "udym".to_string(), + amount: "1000000000000000000".to_string(), + }, + v1beta1::Coin { + denom: "uusdc".to_string(), + amount: "7250000".to_string(), + }, + ], + token_min_amount0: Uint128::zero().to_string(), + token_min_amount1: Uint128::zero().to_string(), + }, + &admin, + ) + .unwrap(); + + // TODO: We should also try depositing with users here, after being able to reproduce the "Denominator zero" bug + + // TODO: At the moment we are trying that with this instantiation funds on initialize::195 + // -> sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), + + // Two sided re-range (50% 50%) + // let _result = wasm + // .execute( + // contract.as_str(), + // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( + // ModifyRangeMsg { + // lower_price: Decimal::from_str("7.0").unwrap(), + // upper_price: Decimal::from_str("7.5").unwrap(), + // max_slippage: Decimal::bps(9500), + // ratio_of_swappable_funds_to_use: Decimal::one(), + // twap_window_seconds: 45, + // }, + // )), + // &[], + // &admin, + // ) + // .unwrap(); + + // One-sided re-range (above current tick, 100% token0) + // let _result = wasm + // .execute( + // contract.as_str(), + // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( + // ModifyRangeMsg { + // lower_price: Decimal::from_str("7.5").unwrap(), + // upper_price: Decimal::from_str("8.0").unwrap(), + // max_slippage: Decimal::bps(9500), + // ratio_of_swappable_funds_to_use: Decimal::one(), + // twap_window_seconds: 45, + // }, + // )), + // &[], + // &admin, + // ) + // .unwrap(); + + // One-sided re-range (below current tick, 100% token1) + let _result = wasm + .execute( + contract.as_str(), + &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( + ModifyRangeMsg { + lower_price: Decimal::from_str("6.5").unwrap(), + upper_price: Decimal::from_str("7.0").unwrap(), + max_slippage: Decimal::bps(9500), + ratio_of_swappable_funds_to_use: Decimal::one(), + twap_window_seconds: 45, + }, + )), + &[], + &admin, + ) + .unwrap(); + } + #[test] #[ignore] fn move_range_works() { @@ -34,9 +173,11 @@ mod test { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); + let pm = PoolManager::new(&app); // Create a second position (in range) in the pool with the admin user to allow for swapping during update range operation cl.create_position( @@ -63,7 +204,6 @@ mod test { .unwrap(); // do a swap to move the cur tick - let pm = PoolManager::new(&app); pm.swap_exact_amount_in( MsgSwapExactAmountIn { sender: admin.address(), @@ -132,7 +272,8 @@ mod test { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let pm = PoolManager::new(&app); @@ -214,7 +355,7 @@ mod test { #[test] #[ignore] fn test_swap_math_poc() { - let (app, _contract, _cl_pool_id, _admin) = default_init(vec![ + let (app, _contract, _cl_pool_id, admin) = default_init(vec![ v1beta1::Coin { denom: DENOM_BASE.to_string(), amount: "1000000000000".to_string(), @@ -223,16 +364,10 @@ mod test { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]); + ]) + .unwrap(); let cl = ConcentratedLiquidity::new(&app); - let alice = app - .init_account(&[ - Coin::new(1_000_000_000_000, DENOM_BASE), - Coin::new(1_000_000_000_000, DENOM_QUOTE), - ]) - .unwrap(); - let pools = cl.query_pools(&PoolsRequest { pagination: None }).unwrap(); let pool: Pool = Pool::decode(pools.pools[0].value.as_slice()).unwrap(); @@ -240,7 +375,7 @@ mod test { // create a basic position on the pool let initial_position = MsgCreatePosition { pool_id: pool.id, - sender: alice.address(), + sender: admin.address(), lower_tick: 30500000, upper_tick: 31500000, tokens_provided: vec![ @@ -250,6 +385,6 @@ mod test { token_min_amount0: "0".to_string(), token_min_amount1: "0".to_string(), }; - let _position = cl.create_position(initial_position, &alice).unwrap(); + let _position = cl.create_position(initial_position, &admin).unwrap(); } } diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index c4f70bd64..d6b218545 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -36,7 +36,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]).unwrap(); + ]) + .unwrap(); // Initialize accounts let accounts = app @@ -238,7 +239,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]).unwrap(); + ]) + .unwrap(); // Initialize accounts let accounts = app @@ -433,7 +435,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]).unwrap(); + ]) + .unwrap(); // Initialize accounts let accounts = app @@ -542,7 +545,8 @@ mod tests { denom: DENOM_QUOTE.to_string(), amount: "1000000000000".to_string(), }, - ]).unwrap(); + ]) + .unwrap(); // Initialize accounts let accounts = app From 7118cb36904cc9cc31cb18ea2faa5bee9a5069c5 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:40:17 +0100 Subject: [PATCH 05/24] reproducing dym denominator --- .../cl-vault/src/test_tube/initialize.rs | 17 ++--- .../contracts/cl-vault/src/test_tube/range.rs | 71 ++++++++++--------- 2 files changed, 42 insertions(+), 46 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index dc150d63c..a77d27233 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -151,22 +151,16 @@ pub mod initialize { .unwrap(); // Assuming tokens_provided[0].amount and tokens_provided[1].amount are String let tokens_provided_0_amount: u128 = - tokens_provided[0].amount.parse().expect("Invalid number"); + tokens_provided[0].amount.parse().unwrap(); let tokens_provided_1_amount: u128 = - tokens_provided[1].amount.parse().expect("Invalid number"); + tokens_provided[1].amount.parse().unwrap(); // Assuming `spot_price.spot_price` is a string representation of a float. let spot_price_float: f64 = spot_price .spot_price - .parse() - .expect("Failed to parse spot price as float"); + .parse().unwrap(); let division_result: f64 = tokens_provided_1_amount as f64 / tokens_provided_0_amount as f64; - assert!( - (spot_price_float - division_result).abs() < f64::EPSILON, - "Assertion failed: spot prices do not match. Expected: {}, got: {}", - spot_price_float, - division_result - ); + assert!((spot_price_float - division_result).abs() < f64::EPSILON); // Increment the app time for twaps to function, this is needed to do not fail on querying a twap for a timeframe higher than the chain existence app.increase_time(1000000); @@ -192,7 +186,8 @@ pub mod initialize { }, Some(admin.address().as_str()), Some("cl-vault"), - sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), + // sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), + sort_tokens(vec![coin(1000000000000000, pool.token0), coin(1000, pool.token1)]).as_ref(), // TODO: De-hardcode this and makes this configurable as argument &admin, ) .unwrap(); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index d6222e97d..ffc414881 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -73,7 +73,7 @@ mod test { // .unwrap(); // println!("pool: {:?}", pool_response); - // Pool.CurrentTick here is -101750000 + // Pool.CurrentTick here is -101750000 or price is 0.00000000000725 // If we go out of upper range we will be one-sided token0 (base denom, left denom) // If we go out of lower range we will be one-sided token1 (quote denom, right denom) @@ -105,42 +105,43 @@ mod test { // TODO: At the moment we are trying that with this instantiation funds on initialize::195 // -> sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), + // and this -> sort_tokens(vec![coin(1000000000000000, pool.token0), coin(1000, pool.token1)]).as_ref(), // Two sided re-range (50% 50%) - // let _result = wasm - // .execute( - // contract.as_str(), - // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( - // ModifyRangeMsg { - // lower_price: Decimal::from_str("7.0").unwrap(), - // upper_price: Decimal::from_str("7.5").unwrap(), - // max_slippage: Decimal::bps(9500), - // ratio_of_swappable_funds_to_use: Decimal::one(), - // twap_window_seconds: 45, - // }, - // )), - // &[], - // &admin, - // ) - // .unwrap(); + let _result = wasm + .execute( + contract.as_str(), + &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( + ModifyRangeMsg { + lower_price: Decimal::from_str("0.000000000007").unwrap(), + upper_price: Decimal::from_str("0.0000000000075").unwrap(), + max_slippage: Decimal::bps(9500), + ratio_of_swappable_funds_to_use: Decimal::one(), + twap_window_seconds: 45, + }, + )), + &[], + &admin, + ) + .unwrap(); // One-sided re-range (above current tick, 100% token0) - // let _result = wasm - // .execute( - // contract.as_str(), - // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( - // ModifyRangeMsg { - // lower_price: Decimal::from_str("7.5").unwrap(), - // upper_price: Decimal::from_str("8.0").unwrap(), - // max_slippage: Decimal::bps(9500), - // ratio_of_swappable_funds_to_use: Decimal::one(), - // twap_window_seconds: 45, - // }, - // )), - // &[], - // &admin, - // ) - // .unwrap(); + let _result = wasm + .execute( + contract.as_str(), + &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( + ModifyRangeMsg { + lower_price: Decimal::from_str("0.0000000000075").unwrap(), + upper_price: Decimal::from_str("0.000000000008").unwrap(), + max_slippage: Decimal::bps(9500), + ratio_of_swappable_funds_to_use: Decimal::one(), + twap_window_seconds: 45, + }, + )), + &[], + &admin, + ) + .unwrap(); // One-sided re-range (below current tick, 100% token1) let _result = wasm @@ -148,8 +149,8 @@ mod test { contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( ModifyRangeMsg { - lower_price: Decimal::from_str("6.5").unwrap(), - upper_price: Decimal::from_str("7.0").unwrap(), + lower_price: Decimal::from_str("0.0000000000065").unwrap(), + upper_price: Decimal::from_str("0.000000000007").unwrap(), max_slippage: Decimal::bps(9500), ratio_of_swappable_funds_to_use: Decimal::one(), twap_window_seconds: 45, From 3d608d5ad55c0b4bf773b9be08ec689fca33099f Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:10:32 +0100 Subject: [PATCH 06/24] upscale and downscale if needed based on scale_factor threshold --- .../contracts/cl-vault/src/helpers.rs | 84 ++++++++++++++++--- .../contracts/cl-vault/src/instantiate.rs | 4 +- .../cl-vault/src/test_tube/initialize.rs | 16 ++-- .../src/vault/concentrated_liquidity.rs | 5 +- .../contracts/cl-vault/src/vault/deposit.rs | 4 +- .../contracts/cl-vault/src/vault/merge.rs | 4 +- .../contracts/cl-vault/src/vault/range.rs | 31 ++++++- 7 files changed, 118 insertions(+), 30 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/helpers.rs b/smart-contracts/contracts/cl-vault/src/helpers.rs index b25c43c02..17f9ecafb 100644 --- a/smart-contracts/contracts/cl-vault/src/helpers.rs +++ b/smart-contracts/contracts/cl-vault/src/helpers.rs @@ -160,6 +160,44 @@ pub fn get_twap_price( // )) // } +const SCALE_FACTOR: u128 = 10u128.pow(12); + +fn scale_if_needed( + cur_price_sqrt: Decimal256, + upper_price_sqrt: Decimal256, + lower_price_sqrt: Decimal256, + current_price: Decimal256, +) -> (bool, Decimal256, Decimal256, Decimal256, Decimal256) { + let scale_up_threshold = Decimal256::from_ratio(1u128, SCALE_FACTOR); + let product = cur_price_sqrt * upper_price_sqrt * lower_price_sqrt; + + if product < scale_up_threshold { + let scale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); + + // Scale the square root prices and current price + let scaled_cur_price_sqrt = cur_price_sqrt.checked_mul(scale_factor).unwrap(); + let scaled_upper_price_sqrt = upper_price_sqrt.checked_mul(scale_factor).unwrap(); + let scaled_lower_price_sqrt = lower_price_sqrt.checked_mul(scale_factor).unwrap(); + let scaled_current_price = current_price.checked_mul(scale_factor).unwrap(); + + return ( + true, + scaled_cur_price_sqrt, + scaled_upper_price_sqrt, + scaled_lower_price_sqrt, + scaled_current_price, + ); + } + + ( + false, + cur_price_sqrt, + upper_price_sqrt, + lower_price_sqrt, + current_price, + ) +} + // this math is straight from the readme pub fn get_single_sided_deposit_0_to_1_swap_amount( token0_balance: Uint128, @@ -167,11 +205,6 @@ pub fn get_single_sided_deposit_0_to_1_swap_amount( current_tick: i64, upper_tick: i64, ) -> Result { - // TODO error here if this condition holds - // if current_tick < lower_tick { - // return ; - // } - let lower_price = tick_to_price(lower_tick)?; let current_price = tick_to_price(current_tick)?; let upper_price = tick_to_price(upper_tick)?; @@ -180,10 +213,13 @@ pub fn get_single_sided_deposit_0_to_1_swap_amount( let lower_price_sqrt = lower_price.sqrt(); let upper_price_sqrt = upper_price.sqrt(); - // let pool_metadata_constant: Decimal256 = cur_price_sqrt - // .checked_mul(lower_price_sqrt)? - // .checked_mul(cur_price_sqrt.checked_sub(lower_price_sqrt)?)? - // .checked_div(upper_price_sqrt.checked_sub(cur_price_sqrt)?)?; + let (is_scale_needed, cur_price_sqrt, upper_price_sqrt, lower_price_sqrt, current_price) = + scale_if_needed( + cur_price_sqrt, + upper_price_sqrt, + lower_price_sqrt, + current_price, + ); let pool_metadata_constant: Decimal256 = (upper_price_sqrt .checked_mul(cur_price_sqrt)? @@ -193,7 +229,16 @@ pub fn get_single_sided_deposit_0_to_1_swap_amount( let spot_price_over_pool_metadata_constant = current_price.checked_div(pool_metadata_constant)?; - let denominator = Decimal256::one().checked_add(spot_price_over_pool_metadata_constant)?; + // Adjust spot_price_over_pool_metadata_constant back to its original scale if scaling was applied + let adjusted_spot_price_over_pool_metadata_constant = if is_scale_needed { + let underscale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); + spot_price_over_pool_metadata_constant.checked_div(underscale_factor)? + } else { + spot_price_over_pool_metadata_constant + }; + + let denominator = + Decimal256::one().checked_add(adjusted_spot_price_over_pool_metadata_constant)?; let swap_amount: Uint128 = Uint256::from(token0_balance) .multiply_ratio(denominator.denominator(), denominator.numerator()) @@ -216,6 +261,14 @@ pub fn get_single_sided_deposit_1_to_0_swap_amount( let lower_price_sqrt = lower_price.sqrt(); let upper_price_sqrt = upper_price.sqrt(); + let (is_scale_needed, cur_price_sqrt, upper_price_sqrt, lower_price_sqrt, current_price) = + scale_if_needed( + cur_price_sqrt, + upper_price_sqrt, + lower_price_sqrt, + current_price, + ); + let pool_metadata_constant: Decimal256 = (upper_price_sqrt .checked_mul(cur_price_sqrt)? .checked_mul(cur_price_sqrt.checked_sub(lower_price_sqrt)?))? @@ -224,7 +277,16 @@ pub fn get_single_sided_deposit_1_to_0_swap_amount( let pool_metadata_constant_over_spot_price: Decimal256 = pool_metadata_constant.checked_div(current_price)?; - let denominator = Decimal256::one().checked_add(pool_metadata_constant_over_spot_price)?; + // Adjust spot_price_over_pool_metadata_constant back to its original scale if scaling was applied + let adjusted_pool_metadata_over_spot_price_constant = if is_scale_needed { + let underscale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); + pool_metadata_constant_over_spot_price.checked_div(underscale_factor)? + } else { + pool_metadata_constant_over_spot_price + }; + + let denominator = + Decimal256::one().checked_add(adjusted_pool_metadata_over_spot_price_constant)?; let swap_amount: Uint128 = Uint256::from(token1_balance) .multiply_ratio(denominator.denominator(), denominator.numerator()) diff --git a/smart-contracts/contracts/cl-vault/src/instantiate.rs b/smart-contracts/contracts/cl-vault/src/instantiate.rs index 5bfe32400..7f33321c2 100644 --- a/smart-contracts/contracts/cl-vault/src/instantiate.rs +++ b/smart-contracts/contracts/cl-vault/src/instantiate.rs @@ -20,7 +20,7 @@ use crate::state::{ DISTRIBUTED_REWARDS, METADATA, POOL_CONFIG, POSITION, RANGE_ADMIN, REWARDS_STATUS, STRATEGIST_REWARDS, VAULT_CONFIG, VAULT_DENOM, }; -use crate::vault::concentrated_liquidity::create_position; +use crate::vault::concentrated_liquidity::get_create_position_msg; use crate::ContractError; pub fn handle_instantiate( @@ -86,7 +86,7 @@ pub fn handle_instantiate( // in order to create the initial position, we need some funds to throw in there, these funds should be seen as burned let (initial0, initial1) = must_pay_one_or_two(&info, (pool.token0, pool.token1))?; - let create_position_msg = create_position( + let create_position_msg = get_create_position_msg( deps, &env, msg.initial_lower_tick, diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index a77d27233..95bd0986c 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -150,14 +150,10 @@ pub mod initialize { }) .unwrap(); // Assuming tokens_provided[0].amount and tokens_provided[1].amount are String - let tokens_provided_0_amount: u128 = - tokens_provided[0].amount.parse().unwrap(); - let tokens_provided_1_amount: u128 = - tokens_provided[1].amount.parse().unwrap(); + let tokens_provided_0_amount: u128 = tokens_provided[0].amount.parse().unwrap(); + let tokens_provided_1_amount: u128 = tokens_provided[1].amount.parse().unwrap(); // Assuming `spot_price.spot_price` is a string representation of a float. - let spot_price_float: f64 = spot_price - .spot_price - .parse().unwrap(); + let spot_price_float: f64 = spot_price.spot_price.parse().unwrap(); let division_result: f64 = tokens_provided_1_amount as f64 / tokens_provided_0_amount as f64; assert!((spot_price_float - division_result).abs() < f64::EPSILON); @@ -187,7 +183,11 @@ pub mod initialize { Some(admin.address().as_str()), Some("cl-vault"), // sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), - sort_tokens(vec![coin(1000000000000000, pool.token0), coin(1000, pool.token1)]).as_ref(), // TODO: De-hardcode this and makes this configurable as argument + sort_tokens(vec![ + coin(1000000000000000, pool.token0), + coin(1000, pool.token1), + ]) + .as_ref(), // TODO: De-hardcode this and makes this configurable as argument &admin, ) .unwrap(); diff --git a/smart-contracts/contracts/cl-vault/src/vault/concentrated_liquidity.rs b/smart-contracts/contracts/cl-vault/src/vault/concentrated_liquidity.rs index f02caa266..096a50a68 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/concentrated_liquidity.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/concentrated_liquidity.rs @@ -12,7 +12,7 @@ use crate::{ ContractError, }; -pub fn create_position( +pub fn get_create_position_msg( deps: DepsMut, env: &Env, lower_tick: i64, @@ -43,6 +43,7 @@ pub fn create_position( // An sdk.Int in the Go code token_min_amount1: token_min_amount1.to_string(), }; + Ok(create_position) } @@ -165,7 +166,7 @@ mod tests { let token_min_amount0 = Uint128::new(1000); let token_min_amount1 = Uint128::new(2000); - let result = create_position( + let result = get_create_position_msg( deps_mut, &env, lower_tick, diff --git a/smart-contracts/contracts/cl-vault/src/vault/deposit.rs b/smart-contracts/contracts/cl-vault/src/vault/deposit.rs index 4ef615f4e..8017733c4 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/deposit.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/deposit.rs @@ -19,7 +19,7 @@ use crate::{ msg::{ExecuteMsg, MergePositionMsg}, reply::Replies, state::{CurrentDeposit, CURRENT_DEPOSIT, POOL_CONFIG, POSITION, SHARES, VAULT_DENOM}, - vault::concentrated_liquidity::{create_position, get_position}, + vault::concentrated_liquidity::{get_create_position_msg, get_position}, ContractError, }; @@ -76,7 +76,7 @@ pub(crate) fn execute_exact_deposit( coins_to_send.push(token1.clone()); } - let create_position_msg = create_position( + let create_position_msg = get_create_position_msg( deps, &env, position.lower_tick, diff --git a/smart-contracts/contracts/cl-vault/src/vault/merge.rs b/smart-contracts/contracts/cl-vault/src/vault/merge.rs index 52ebd792a..1cd61fcaf 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/merge.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/merge.rs @@ -16,7 +16,7 @@ use crate::{ msg::MergePositionMsg, reply::Replies, state::{CurrentMergePosition, CURRENT_MERGE, CURRENT_MERGE_POSITION, POOL_CONFIG}, - vault::concentrated_liquidity::create_position, + vault::concentrated_liquidity::get_create_position_msg, ContractError, }; @@ -159,7 +159,7 @@ pub fn handle_merge_withdraw_reply( // this is expected to panic if tokens is an empty vec![] // tokens should never be an empty vec![] as this would mean that all the current positions // are returning zero tokens and this would fail on osmosis side - let position = create_position( + let position = get_create_position_msg( deps, &env, range.lower_tick, diff --git a/smart-contracts/contracts/cl-vault/src/vault/range.rs b/smart-contracts/contracts/cl-vault/src/vault/range.rs index 1093021db..2506618e0 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/range.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/range.rs @@ -24,7 +24,7 @@ use crate::{ ModifyRangeState, Position, SwapDepositMergeState, MODIFY_RANGE_STATE, POOL_CONFIG, POSITION, RANGE_ADMIN, SWAP_DEPOSIT_MERGE_STATE, }, - vault::concentrated_liquidity::create_position, + vault::concentrated_liquidity::get_create_position_msg, vault::concentrated_liquidity::get_position, vault::merge::MergeResponse, vault::swap::swap, @@ -165,6 +165,10 @@ pub fn handle_withdraw_position_reply( .find_coin(pool_config.token1.clone()) .amount .checked_sub(amount1)?; + deps.api + .debug(format!("unused_balance0 {:?}", unused_balance0).as_str()); + deps.api + .debug(format!("unused_balance1 {:?}", unused_balance1).as_str()); amount0 = amount0.checked_add(unused_balance0)?; amount1 = amount1.checked_add(unused_balance1)?; @@ -184,8 +188,12 @@ pub fn handle_withdraw_position_reply( amount: amount1, }) } + deps.api + .debug(format!("tokens_provided {:?}", tokens_provided).as_str()); let pool_details = get_cl_pool_info(&deps.querier, pool_config.pool_id)?; + deps.api + .debug(format!("pool_details {:?}", pool_details).as_str()); // if only one token is being deposited, and we are moving into a position where any amount of the other token is needed, // creating the position here will fail because liquidityNeeded is calculated as 0 on chain level @@ -212,7 +220,7 @@ pub fn handle_withdraw_position_reply( ) } else { // we can naively re-deposit up to however much keeps the proportion of tokens the same. Then swap & re-deposit the proper ratio with the remaining tokens - let create_position_msg = create_position( + let create_position_msg = get_create_position_msg( deps, &env, modify_range_state.lower_tick, @@ -242,6 +250,9 @@ pub fn handle_initial_create_position_reply( env: Env, data: SubMsgResult, ) -> Result { + deps.api + .debug(format!("handle_initial_create_position_reply {:?}", "").as_str()); + let create_position_message: MsgCreatePositionResponse = data.try_into()?; let modify_range_state = MODIFY_RANGE_STATE.load(deps.storage)?.unwrap(); @@ -289,6 +300,9 @@ pub fn do_swap_deposit_merge( ratio_of_swappable_funds_to_use: Decimal, twap_window_seconds: u64, ) -> Result { + deps.api + .debug(format!("do_swap_deposit_merge {:?}", "").as_str()); + if SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)?.is_some() { return Err(ContractError::SwapInProgress {}); } @@ -371,6 +385,8 @@ pub fn do_swap_deposit_merge( .add_attribute("method", "no_swap") .add_attribute("new_position", position_id.unwrap().to_string())); }; + deps.api + .debug(format!("do_swap_deposit_merge 2 {:?}", "").as_str()); // todo check that this math is right with spot price (numerators, denominators) if taken by legacy gamm module instead of poolmanager let twap_price = get_twap_price(deps.storage, &deps.querier, &env, twap_window_seconds)?; @@ -427,6 +443,9 @@ fn handle_swap_success( env: Env, resp: MsgSwapExactAmountInResponse, ) -> Result { + deps.api + .debug(format!("handle_swap_success {:?}", "").as_str()); + let swap_deposit_merge_state = match SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)? { Some(swap_deposit_merge) => swap_deposit_merge, None => return Err(ContractError::SwapDepositMergeStateNotFound {}), @@ -461,7 +480,7 @@ fn handle_swap_success( amount: balance1, }); } - let create_position_msg = create_position( + let create_position_msg = get_create_position_msg( deps, &env, swap_deposit_merge_state.target_lower_tick, @@ -496,6 +515,9 @@ pub fn handle_iteration_create_position_reply( env: Env, data: SubMsgResult, ) -> Result { + deps.api + .debug(format!("handle_iteration_create_position_reply {:?}", "").as_str()); + let create_position_message: MsgCreatePositionResponse = data.try_into()?; let mut swap_deposit_merge_state = match SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)? { @@ -538,6 +560,9 @@ pub fn handle_iteration_create_position_reply( // store new position id and exit pub fn handle_merge_response(deps: DepsMut, data: SubMsgResult) -> Result { + deps.api + .debug(format!("handle_merge_response {:?}", "").as_str()); + let merge_response: MergeResponse = data.try_into()?; POSITION.save( From 2e1bc4d03100477377fc47490a6da6baab120aa6 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:13:31 +0100 Subject: [PATCH 07/24] remove debugs --- .../contracts/cl-vault/src/vault/range.rs | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/vault/range.rs b/smart-contracts/contracts/cl-vault/src/vault/range.rs index 2506618e0..262c35917 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/range.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/range.rs @@ -165,10 +165,6 @@ pub fn handle_withdraw_position_reply( .find_coin(pool_config.token1.clone()) .amount .checked_sub(amount1)?; - deps.api - .debug(format!("unused_balance0 {:?}", unused_balance0).as_str()); - deps.api - .debug(format!("unused_balance1 {:?}", unused_balance1).as_str()); amount0 = amount0.checked_add(unused_balance0)?; amount1 = amount1.checked_add(unused_balance1)?; @@ -188,12 +184,8 @@ pub fn handle_withdraw_position_reply( amount: amount1, }) } - deps.api - .debug(format!("tokens_provided {:?}", tokens_provided).as_str()); let pool_details = get_cl_pool_info(&deps.querier, pool_config.pool_id)?; - deps.api - .debug(format!("pool_details {:?}", pool_details).as_str()); // if only one token is being deposited, and we are moving into a position where any amount of the other token is needed, // creating the position here will fail because liquidityNeeded is calculated as 0 on chain level @@ -250,9 +242,6 @@ pub fn handle_initial_create_position_reply( env: Env, data: SubMsgResult, ) -> Result { - deps.api - .debug(format!("handle_initial_create_position_reply {:?}", "").as_str()); - let create_position_message: MsgCreatePositionResponse = data.try_into()?; let modify_range_state = MODIFY_RANGE_STATE.load(deps.storage)?.unwrap(); @@ -300,9 +289,6 @@ pub fn do_swap_deposit_merge( ratio_of_swappable_funds_to_use: Decimal, twap_window_seconds: u64, ) -> Result { - deps.api - .debug(format!("do_swap_deposit_merge {:?}", "").as_str()); - if SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)?.is_some() { return Err(ContractError::SwapInProgress {}); } @@ -385,8 +371,6 @@ pub fn do_swap_deposit_merge( .add_attribute("method", "no_swap") .add_attribute("new_position", position_id.unwrap().to_string())); }; - deps.api - .debug(format!("do_swap_deposit_merge 2 {:?}", "").as_str()); // todo check that this math is right with spot price (numerators, denominators) if taken by legacy gamm module instead of poolmanager let twap_price = get_twap_price(deps.storage, &deps.querier, &env, twap_window_seconds)?; @@ -443,9 +427,6 @@ fn handle_swap_success( env: Env, resp: MsgSwapExactAmountInResponse, ) -> Result { - deps.api - .debug(format!("handle_swap_success {:?}", "").as_str()); - let swap_deposit_merge_state = match SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)? { Some(swap_deposit_merge) => swap_deposit_merge, None => return Err(ContractError::SwapDepositMergeStateNotFound {}), @@ -515,9 +496,6 @@ pub fn handle_iteration_create_position_reply( env: Env, data: SubMsgResult, ) -> Result { - deps.api - .debug(format!("handle_iteration_create_position_reply {:?}", "").as_str()); - let create_position_message: MsgCreatePositionResponse = data.try_into()?; let mut swap_deposit_merge_state = match SWAP_DEPOSIT_MERGE_STATE.may_load(deps.storage)? { @@ -560,9 +538,6 @@ pub fn handle_iteration_create_position_reply( // store new position id and exit pub fn handle_merge_response(deps: DepsMut, data: SubMsgResult) -> Result { - deps.api - .debug(format!("handle_merge_response {:?}", "").as_str()); - let merge_response: MergeResponse = data.try_into()?; POSITION.save( From dd24da61daefa5594be913e29bf42e9764517338 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 20:11:33 +0100 Subject: [PATCH 08/24] unit tests scale_if_needed --- .../contracts/cl-vault/src/helpers.rs | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/smart-contracts/contracts/cl-vault/src/helpers.rs b/smart-contracts/contracts/cl-vault/src/helpers.rs index 17f9ecafb..a25179a5e 100644 --- a/smart-contracts/contracts/cl-vault/src/helpers.rs +++ b/smart-contracts/contracts/cl-vault/src/helpers.rs @@ -171,7 +171,7 @@ fn scale_if_needed( let scale_up_threshold = Decimal256::from_ratio(1u128, SCALE_FACTOR); let product = cur_price_sqrt * upper_price_sqrt * lower_price_sqrt; - if product < scale_up_threshold { + if product <= scale_up_threshold { let scale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); // Scale the square root prices and current price @@ -525,6 +525,62 @@ mod tests { use super::*; + #[test] + fn test_scale_if_needed_variants() { + // Adjusted small and large values for testing + let small_value = Decimal256::from_ratio(1u128, 10u128.pow(5)); // 1e-5 to ensure product is below 1e-12 + let large_value = Decimal256::from_ratio(1u128, 10u128.pow(3)); // 1e-3 or larger to ensure product is above 1e-12 + + // Scenario 1: All Values Below Threshold + let (needs_scaling_below, _, _, _, _) = + scale_if_needed(small_value, small_value, small_value, small_value); + assert_eq!( + needs_scaling_below, true, + "Scaling should be needed for all values below threshold" + ); + + // Scenario 2: All Values Above Threshold + let (needs_scaling_above, _, _, _, _) = + scale_if_needed(large_value, large_value, large_value, large_value); + assert_eq!( + needs_scaling_above, false, + "Scaling should not be needed for all values above threshold" + ); + + // Scenario 3: Mixed Values - Some below, some above threshold + let (needs_scaling_mixed, _, _, _, _) = + scale_if_needed(small_value, large_value, small_value, large_value); + assert_eq!( + needs_scaling_mixed, true, + "Scaling should be needed for mixed values with any below threshold" + ); + + // Scenario 4: Boundary Condition - Exactly at Threshold + // Assuming the threshold can be represented exactly, otherwise adjust the threshold or values accordingly + let threshold_value = Decimal256::from_ratio(1u128, SCALE_FACTOR); // Adjust based on actual threshold + let (needs_scaling_boundary, _, _, _, _) = scale_if_needed( + threshold_value, + threshold_value, + threshold_value, + threshold_value, + ); + // The expected result here depends on how you define "less than" in scale_if_needed + // If using <, this should be false. If using <=, adjust the assertion accordingly. + assert_eq!( + needs_scaling_boundary, true, + "Scaling should be needed exactly at threshold" + ); + + // Scenario 5: Zero and Near-Zero Values - Ensure handling of zero properly + let zero_value = Decimal256::zero(); + let (needs_scaling_zero, _, _, _, _) = + scale_if_needed(zero_value, zero_value, zero_value, zero_value); + assert_eq!( + needs_scaling_zero, true, + "Scaling should be needed for zero values" + ); + } + #[test] fn must_pay_one_or_two_works_ordered() { let expected0 = coin(100, "uatom"); From 9ed4ea227743c6e7a7fa731d7128595192e78fe3 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 23:01:07 +0100 Subject: [PATCH 09/24] generalize test case, include wide position on init --- .../cl-vault/src/test_tube/initialize.rs | 10 +-- .../contracts/cl-vault/src/test_tube/range.rs | 67 ++++--------------- 2 files changed, 17 insertions(+), 60 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 95bd0986c..92d00724f 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -126,13 +126,13 @@ pub mod initialize { // Sort tokens alphabetically by denom name or Osmosis will return an error tokens_provided.sort_by(|a, b| a.denom.cmp(&b.denom)); // can't use helpers.rs::sort_tokens() due to different Coin type - // Create a first position in the pool with the admin user + // Create a first position in the pool with the admin user (wide position) to simulate liquidity availability on the CL Pool cl.create_position( MsgCreatePosition { pool_id: pool.id, sender: admin.address(), - lower_tick, - upper_tick, + lower_tick: -108000000, // min tick + upper_tick: 342000000, // max tick tokens_provided: tokens_provided.clone(), token_min_amount0: token_min_amount0.to_string(), token_min_amount1: token_min_amount1.to_string(), @@ -184,8 +184,8 @@ pub mod initialize { Some("cl-vault"), // sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), sort_tokens(vec![ - coin(1000000000000000, pool.token0), - coin(1000, pool.token1), + coin(1000000, pool.token0), + coin(1000000, pool.token1), ]) .as_ref(), // TODO: De-hardcode this and makes this configurable as argument &admin, diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index ffc414881..b32104164 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -24,88 +24,45 @@ mod test { /// # Test: move_range_works_dym_usdc /// - /// This test case initializes a Concentrated Liquidity (CL) pool with DYM and USDC tokens + /// This test case initializes a Concentrated Liquidity (CL) pool with 18DEC and USDC tokens /// to simulate a real-world scenario on the blockchain with a specific spot price. The purpose /// of this test is to ensure that operations such as moving the range within the CL pool function /// correctly, especially when dealing with tokens of different decimal precisions. /// /// ## Initialization Parameters: - /// - DYM Token (udym): 18 decimal places, represented as `1000000000000000000udym` (for 1 DYM). - /// - USDC Token (uusdc): 6 decimal places, represented as `7250000uusdc` (to establish a spot price of 7.25 USDC for 1 DYM). + /// - 18DEC Token (u18dec): 18 decimal places, represented as `1000000000000000000u18dec` (for 1 18DEC). + /// - USDC Token (uusdc): 6 decimal places, represented as `7250000uusdc` (to establish a spot price of 7.25 USDC for 1 18DEC). /// - /// The test initializes a CL pool with these tokens to establish a spot price of 7.25 DYM/USD. - /// This spot price accurately reflects the real-world ratio between DYM and USDC on the mainnet, + /// The test initializes a CL pool with these tokens to establish a spot price of 7.25 18DEC/USD. + /// This spot price accurately reflects the real-world ratio between 18DEC and USDC on the mainnet, /// adjusted for the blockchain's unit representation. /// /// ## Decimal Precision and Spot Price Consideration: - /// The significant difference in decimal places between DYM (18 decimals) and USDC (6 decimals) + /// The significant difference in decimal places between 18DEC (18 decimals) and USDC (6 decimals) /// necessitates precise calculation to ensure the spot price is accurately represented in the - /// blockchain's terms. The chosen amounts of `1000000000000000000udym` for DYM and `7250000uusdc` - /// for USDC effectively establish a starting spot price of 7.25 DYM/USD in the CL pool, accurately + /// blockchain's terms. The chosen amounts of `1000000000000000000u18dec` for 18DEC and `7250000uusdc` + /// for USDC effectively establish a starting spot price of 7.25 18DEC/USD in the CL pool, accurately /// representing the spot price in a manner that does not require adjustment for decimal places in /// the context of Osmosis' handling of token amounts. /// /// Spot price it would be: `spot_price = 7250000 / 1000000000000000000`, - /// calculating the spot price in raw integer format without adjusting for decimal places, representing the USDC required to purchase one unit of DYM. + /// calculating the spot price in raw integer format without adjusting for decimal places, representing the USDC required to purchase one unit of 18DEC. #[test] #[ignore] fn move_range_works_dym_usdc() { let (app, contract, cl_pool_id, admin) = default_init(vec![ v1beta1::Coin { - denom: "udym".to_string(), - amount: "1000000000000000000".to_string(), + denom: "u18dec".to_string(), + amount: "1000000000000000000000000000000".to_string(), }, v1beta1::Coin { denom: "uusdc".to_string(), - amount: "7250000".to_string(), + amount: "7250000000000000000".to_string(), }, ]) .unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); - // let pm = PoolManager::new(&app); - - // // Get pool information - // let pool_response = pm - // .query_pool(&PoolRequest { - // pool_id: cl_pool_id, - // }) - // .unwrap(); - // println!("pool: {:?}", pool_response); - - // Pool.CurrentTick here is -101750000 or price is 0.00000000000725 - // If we go out of upper range we will be one-sided token0 (base denom, left denom) - // If we go out of lower range we will be one-sided token1 (quote denom, right denom) - - // Create a second position (in range) in the pool with the admin user to allow for swapping during update range operation - cl.create_position( - MsgCreatePosition { - pool_id: cl_pool_id, - sender: admin.address(), - lower_tick: -101760000, // -10000 ticks - upper_tick: -101740000, // +10000 ticks - tokens_provided: vec![ - v1beta1::Coin { - denom: "udym".to_string(), - amount: "1000000000000000000".to_string(), - }, - v1beta1::Coin { - denom: "uusdc".to_string(), - amount: "7250000".to_string(), - }, - ], - token_min_amount0: Uint128::zero().to_string(), - token_min_amount1: Uint128::zero().to_string(), - }, - &admin, - ) - .unwrap(); - - // TODO: We should also try depositing with users here, after being able to reproduce the "Denominator zero" bug - - // TODO: At the moment we are trying that with this instantiation funds on initialize::195 - // -> sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), - // and this -> sort_tokens(vec![coin(1000000000000000, pool.token0), coin(1000, pool.token1)]).as_ref(), // Two sided re-range (50% 50%) let _result = wasm From c214e334420415113e56532e0a2f9d7c2df02b34 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 23:12:12 +0100 Subject: [PATCH 10/24] correct test function name --- smart-contracts/contracts/cl-vault/src/test_tube/range.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index b32104164..5364d518e 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -22,7 +22,7 @@ mod test { const DENOM_BASE: &str = "uatom"; const DENOM_QUOTE: &str = "uosmo"; - /// # Test: move_range_works_dym_usdc + /// # Test: move_range_works_18dec_usdc /// /// This test case initializes a Concentrated Liquidity (CL) pool with 18DEC and USDC tokens /// to simulate a real-world scenario on the blockchain with a specific spot price. The purpose @@ -49,7 +49,7 @@ mod test { /// calculating the spot price in raw integer format without adjusting for decimal places, representing the USDC required to purchase one unit of 18DEC. #[test] #[ignore] - fn move_range_works_dym_usdc() { + fn move_range_works_18dec_usdc() { let (app, contract, cl_pool_id, admin) = default_init(vec![ v1beta1::Coin { denom: "u18dec".to_string(), From 6bb2d50dbc15b6b743d0a9dfcf4e50053850ac0f Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Wed, 14 Feb 2024 23:18:32 +0100 Subject: [PATCH 11/24] fmt --- .../contracts/cl-vault/src/test_tube/initialize.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 92d00724f..f8bd9a8da 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -132,7 +132,7 @@ pub mod initialize { pool_id: pool.id, sender: admin.address(), lower_tick: -108000000, // min tick - upper_tick: 342000000, // max tick + upper_tick: 342000000, // max tick tokens_provided: tokens_provided.clone(), token_min_amount0: token_min_amount0.to_string(), token_min_amount1: token_min_amount1.to_string(), @@ -183,11 +183,7 @@ pub mod initialize { Some(admin.address().as_str()), Some("cl-vault"), // sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), - sort_tokens(vec![ - coin(1000000, pool.token0), - coin(1000000, pool.token1), - ]) - .as_ref(), // TODO: De-hardcode this and makes this configurable as argument + sort_tokens(vec![coin(1000000, pool.token0), coin(1000000, pool.token1)]).as_ref(), // TODO: De-hardcode this and makes this configurable as argument &admin, ) .unwrap(); From c218532d5fea2b8de44662a8f126c6a9d0daaa17 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:06:28 +0100 Subject: [PATCH 12/24] fix test tube --- .../cl-vault/src/test_tube/initialize.rs | 6 +-- .../contracts/cl-vault/src/test_tube/range.rs | 24 ++++++++++ .../cl-vault/src/test_tube/rewards.rs | 47 ++++++++++--------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index f8bd9a8da..c1c93d397 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -126,13 +126,13 @@ pub mod initialize { // Sort tokens alphabetically by denom name or Osmosis will return an error tokens_provided.sort_by(|a, b| a.denom.cmp(&b.denom)); // can't use helpers.rs::sort_tokens() due to different Coin type - // Create a first position in the pool with the admin user (wide position) to simulate liquidity availability on the CL Pool + // Create a first position in the pool with the admin user to simulate liquidity availability on the CL Pool cl.create_position( MsgCreatePosition { pool_id: pool.id, sender: admin.address(), - lower_tick: -108000000, // min tick - upper_tick: 342000000, // max tick + lower_tick, + upper_tick, tokens_provided: tokens_provided.clone(), token_min_amount0: token_min_amount0.to_string(), token_min_amount1: token_min_amount1.to_string(), diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index 5364d518e..b87adc59d 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -64,6 +64,30 @@ mod test { let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); + // Create a second position in the pool with the admin user (wide position) to simulate liquidity availability on the CL Pool + cl.create_position( + MsgCreatePosition { + pool_id: cl_pool_id, + sender: admin.address(), + lower_tick: -108000000, // min tick + upper_tick: 342000000, // max tick + tokens_provided: vec![ + v1beta1::Coin { + denom: "u18dec".to_string(), + amount: "1000000000000000000000000000000".to_string(), + }, + v1beta1::Coin { + denom: "uusdc".to_string(), + amount: "7250000000000000000".to_string(), + }, + ], + token_min_amount0: Uint128::zero().to_string(), + token_min_amount1: Uint128::zero().to_string(), + }, + &admin, + ) + .unwrap(); + // Two sided re-range (50% 50%) let _result = wasm .execute( diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index d6b218545..768dbf4e6 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -1,7 +1,5 @@ #[cfg(test)] mod tests { - use std::ops::Mul; - use crate::msg::ExecuteMsg; use crate::test_tube::helpers::{ get_event_attributes_by_ty_and_key, get_event_value_amount_numeric, @@ -22,7 +20,7 @@ mod tests { const DEPOSIT_AMOUNT: u128 = 5_000_000; const SWAPS_NUM: usize = 10; const SWAPS_AMOUNT: &str = "1000000000"; - const DISTRIBUTION_CYCLES: usize = 25; + const DISTRIBUTION_CYCLES: usize = 10; #[test] #[ignore] @@ -724,32 +722,37 @@ mod tests { rewards_received.push(coin_received_u128); } - // Assert that 'tokens_out' values for events are empty - assert_ne!(tokens_out_spread_rewards[0].value, "".to_string()); - let tokens_out_spread_rewards_u128: u128 = - get_event_value_amount_numeric(&tokens_out_spread_rewards[0].value); - let rewards_less_performance_fee = (tokens_out_spread_rewards_u128 as f64 * 0.8) as u64; - let expected_rewards_per_user = rewards_less_performance_fee / (ACCOUNTS_NUM + 1); // hardcoding +1 due to test logic, we will deposit once more with a single account doubling its shares amount - let expected_rewards_per_user_double = expected_rewards_per_user.mul(2); + // Assert rewards - let double_rewards_value: Vec = rewards_received + let max_reward = *rewards_received .iter() - .filter(|&&x| x > expected_rewards_per_user as u128) - .cloned() - .collect(); - let single_rewards_count = rewards_received + .max() + .expect("There should be at least one reward"); + let max_count = rewards_received .iter() - .filter(|&&x| x == expected_rewards_per_user as u128) + .filter(|&&x| x == max_reward) .count(); - assert_approx_eq!( - double_rewards_value[0], - expected_rewards_per_user_double as u128, - "0.005" + assert_eq!( + max_count, 1, + "There should be exactly one account with the highest reward." ); + + let common_reward = rewards_received + .iter() + .filter(|&&x| x != max_reward) + .next() + .expect("There should be a common lower reward value"); + + let common_count = rewards_received + .iter() + .filter(|&&x| x == *common_reward) + .count(); + assert_eq!( - single_rewards_count, 9, - "There should be exactly one account with double rewards." + common_count, + rewards_received.len() - 1, + "All other rewards should be the same lower value." ); } From 55d92858e58f0728dfbdc03f1ebeb3a389ecf423 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:20:19 +0100 Subject: [PATCH 13/24] resolve warnings --- .../contracts/cl-vault/src/test_tube/rewards.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index 768dbf4e6..33008b189 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -609,13 +609,6 @@ mod tests { ) .unwrap(); - // Extract 'tokens_out' attribute value for 'total_collect_spread_rewards' - let tokens_out_spread_rewards = get_event_attributes_by_ty_and_key( - &result, - "total_collect_spread_rewards", - vec!["tokens_out"], - ); - // Collect init for _ in 0..(ACCOUNTS_NUM - 1) { let result = wasm From 74650d58d3918dd3e31a9833a90eb8adff0bd7a1 Mon Sep 17 00:00:00 2001 From: LaurensKubat <32776056+LaurensKubat@users.noreply.github.com> Date: Thu, 15 Feb 2024 20:20:03 +0100 Subject: [PATCH 14/24] Fix/simplify range swap (#571) ## 1. Overview Minor code changes that should not affect functionality and refactoring of test-tube init functions ## 2. Implementation details - Bump osmosis-std and osmosis-test-tube versions - Refactor a boolean condition for easier readability - test tube init refactor ## 3. How to test/use ## 4. Checklist [x] Does the Readme need to be updated? No ## 5. Limitations (optional) N/A ## 6. Future Work (optional) N/A --- smart-contracts/Cargo.lock | 18 +-- smart-contracts/contracts/cl-vault/Cargo.toml | 4 +- .../contracts/cl-vault/src/test_tube/admin.rs | 19 ++- .../src/test_tube/deposit_withdraw.rs | 88 ++++++----- .../cl-vault/src/test_tube/initialize.rs | 56 +++---- .../cl-vault/src/test_tube/proptest.rs | 16 +- .../contracts/cl-vault/src/test_tube/range.rs | 117 +++++++++------ .../cl-vault/src/test_tube/rewards.rs | 139 ++++++++---------- .../contracts/cl-vault/src/vault/range.rs | 19 ++- 9 files changed, 244 insertions(+), 232 deletions(-) diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 0ba69ce7a..9bdc72fdf 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -391,7 +391,7 @@ dependencies = [ "cw-vault-multi-standard", "cw2 1.1.1", "num_enum 0.7.0", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "osmosis-test-tube", "proptest", "prost 0.12.3", @@ -1855,9 +1855,9 @@ dependencies = [ [[package]] name = "osmosis-std" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e87adf61f03306474ce79ab322d52dfff6b0bcf3aed1e12d8864ac0400dec1bf" +checksum = "8641c376f01f5af329dc2a34e4f5527eaeb0bde18cda8d86fed958d04c86159c" dependencies = [ "chrono", "cosmwasm-std", @@ -1896,15 +1896,15 @@ dependencies = [ [[package]] name = "osmosis-test-tube" -version = "21.0.0" +version = "22.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3a528c942d25d3159634f77953ca0e16c5a450fc44578ad922320128e4025fd" +checksum = "a082b97136d15470a37aa758f227c865594590b69d74721248ed5adf59bf1ca2" dependencies = [ "base64", "bindgen", "cosmrs", "cosmwasm-std", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "prost 0.12.3", "serde", "serde_json", @@ -3067,14 +3067,14 @@ dependencies = [ [[package]] name = "test-tube" -version = "0.3.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17f30e7fea966bde5f9933a4cb2db79dd272115ea19d1656da2aac7ce0700fa" +checksum = "09184c7655b2bdaf4517b06141a2e4c44360904f2706a05b24c831bd97ad1db6" dependencies = [ "base64", "cosmrs", "cosmwasm-std", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "prost 0.12.3", "serde", "serde_json", diff --git a/smart-contracts/contracts/cl-vault/Cargo.toml b/smart-contracts/contracts/cl-vault/Cargo.toml index 2c69cffbe..7816bfbb0 100644 --- a/smart-contracts/contracts/cl-vault/Cargo.toml +++ b/smart-contracts/contracts/cl-vault/Cargo.toml @@ -47,11 +47,11 @@ cw-utils = "1.0.2" cw-vault-multi-standard = {git = "https://github.com/quasar-finance/cw-vault-standard", features = ["lockup", "force-unlock"]} cw2 = "1.0.1" num_enum = "0.7.0" -osmosis-std = "0.21.0" +osmosis-std = "0.22.0" prost = {version = "0.12.3", default-features = false} serde = {version = "1.0.145", default-features = false, features = ["derive"]} thiserror = {version = "1.0.31"} [dev-dependencies] -osmosis-test-tube = "21.0.0" +osmosis-test-tube = "22.1.0" proptest = "1.2.0" diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs index 195ebe237..d7cb76980 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs @@ -1,8 +1,9 @@ #[cfg(test)] mod tests { + use cosmwasm_std::coin; use osmosis_std::types::cosmos::base::v1beta1; - use crate::test_tube::initialize::initialize::default_init; + use crate::test_tube::initialize::initialize::{default_init, TOKENS_PROVIDED_AMOUNT}; const DENOM_BASE: &str = "uatom"; const DENOM_QUOTE: &str = "uosmo"; @@ -10,15 +11,13 @@ mod tests { #[test] #[ignore] fn range_admin_update_works() { - let (_app, _contract_address, _cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (_app, _contract, _cl_pool_id, _admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); // change the range admin and verify that it works diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs index 822088923..fd52ff32c 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod tests { - use cosmwasm_std::{assert_approx_eq, Coin, Uint128}; + use cosmwasm_std::{assert_approx_eq, coin, Coin, Uint128}; use osmosis_std::types::cosmos::base::v1beta1; use osmosis_test_tube::{Account, Module, Wasm}; @@ -8,7 +8,7 @@ mod tests { use crate::{ msg::{ExecuteMsg, ExtensionQueryMsg, QueryMsg}, query::{AssetsBalanceResponse, TotalAssetsResponse, UserSharesBalanceResponse}, - test_tube::initialize::initialize::default_init, + test_tube::initialize::initialize::{default_init, TOKENS_PROVIDED_AMOUNT}, }; const INITIAL_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455u128; @@ -18,15 +18,13 @@ mod tests { #[test] #[ignore] fn single_deposit_withdraw_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); let wasm = Wasm::new(&app); @@ -40,12 +38,12 @@ mod tests { .unwrap(); let vault_assets_before: TotalAssetsResponse = wasm - .query(contract_address.as_str(), &QueryMsg::TotalAssets {}) + .query(contract.as_str(), &QueryMsg::TotalAssets {}) .unwrap(); // TODO: Check this -> Certain deposit amounts do not work here due to an off by one error in Osmosis cl code. The value here is chosen to specifically work wasm.execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(1_000_000_000_000_000, DENOM_BASE), @@ -58,7 +56,7 @@ mod tests { // Get shares for Alice from vault contract and assert let shares: UserSharesBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::VaultExtension(ExtensionQueryMsg::Balances( crate::msg::UserBalanceQueryMsg::UserSharesBalance { user: alice.address(), @@ -71,7 +69,7 @@ mod tests { // Get user_assets for Alice from vault contract and assert let user_assets: AssetsBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::VaultExtension(ExtensionQueryMsg::Balances( crate::msg::UserBalanceQueryMsg::UserAssetsBalance { user: alice.address(), @@ -96,7 +94,7 @@ mod tests { // Get vault assets and assert let vault_assets: TotalAssetsResponse = wasm - .query(contract_address.as_str(), &QueryMsg::TotalAssets {}) + .query(contract.as_str(), &QueryMsg::TotalAssets {}) .unwrap(); assert_approx_eq!( vault_assets.token0.amount, @@ -121,7 +119,7 @@ mod tests { let _withdraw = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::Redeem { recipient: None, amount: shares.balance, @@ -137,15 +135,13 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); let wasm = Wasm::new(&app); @@ -160,13 +156,13 @@ mod tests { // Get vaults assets before doing anything for future assertions let vault_assets_before: TotalAssetsResponse = wasm - .query(contract_address.as_str(), &QueryMsg::TotalAssets {}) + .query(contract.as_str(), &QueryMsg::TotalAssets {}) .unwrap(); // Loop 3 times to do multiple deposits as Alice for _ in 0..3 { wasm.execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(1_000_000_000_000_000_000, DENOM_BASE), @@ -180,7 +176,7 @@ mod tests { // Get Alice shares from vault contract let shares: UserSharesBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::VaultExtension(ExtensionQueryMsg::Balances( crate::msg::UserBalanceQueryMsg::UserSharesBalance { user: alice.address(), @@ -193,7 +189,7 @@ mod tests { // Get Alice assets from vault contract let user_assets: AssetsBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::VaultExtension(ExtensionQueryMsg::Balances( crate::msg::UserBalanceQueryMsg::UserAssetsBalance { user: alice.address(), @@ -217,7 +213,7 @@ mod tests { let user_assets_again: AssetsBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::ConvertToAssets { amount: shares.balance, }, @@ -235,7 +231,7 @@ mod tests { ); let vault_assets: TotalAssetsResponse = wasm - .query(contract_address.as_str(), &QueryMsg::TotalAssets {}) + .query(contract.as_str(), &QueryMsg::TotalAssets {}) .unwrap(); assert_approx_eq!( @@ -260,7 +256,7 @@ mod tests { let _withdraw = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::Redeem { recipient: None, amount: shares.balance, @@ -275,15 +271,13 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_unused_funds_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); //let bank = Bank::new(&app); @@ -335,7 +329,7 @@ mod tests { // depositing for user in &users { wasm.execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(deposit_amount, DENOM_BASE), @@ -352,7 +346,7 @@ mod tests { for user in users { let user_shares: UserSharesBalanceResponse = wasm .query( - contract_address.as_str(), + contract.as_str(), &QueryMsg::VaultExtension(ExtensionQueryMsg::Balances( crate::msg::UserBalanceQueryMsg::UserSharesBalance { user: user.address(), @@ -363,13 +357,13 @@ mod tests { // let _balances = bank // .query_all_balances(&QueryAllBalancesRequest { - // address: contract_address.to_string(), + // address: contract.to_string(), // pagination: None, // }) // .unwrap(); // let pos_id: PositionResponse = wasm // .query( - // contract_address.as_str(), + // contract.as_str(), // &QueryMsg::VaultExtension(ExtensionQueryMsg::ConcentratedLiquidity( // crate::msg::ClQueryMsg::Position {}, // )), @@ -383,7 +377,7 @@ mod tests { // withdrawing wasm.execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::Redeem { recipient: None, amount: user_shares.balance, diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index c1c93d397..9ee22a732 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -28,24 +28,30 @@ pub mod initialize { use crate::query::PoolResponse; use crate::state::VaultConfig; - const ADMIN_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455; + pub(crate) const ADMIN_BALANCE_AMOUNT: u128 = 340282366920938463463374607431768211455u128; + pub(crate) const TOKENS_PROVIDED_AMOUNT: u128 = 1_000_000_000_000; + pub(crate) const DENOM_BASE: &str = "uatom"; + pub(crate) const DENOM_QUOTE: &str = "uosmo"; + pub(crate) const ACCOUNTS_INIT_BALANCE: u128 = 1_000_000_000_000_000; + // Define init variants here pub fn default_init( - tokens_provided: Vec, + pool_tokens: Vec, + vault_tokens: Vec, ) -> Result<(OsmosisTestApp, Addr, u64, SigningAccount), StdError> { - if tokens_provided.len() > 2 { + if pool_tokens.len() > 2 { return Err(StdError::generic_err("More than two tokens provided")); } // Prepare admin balances, including "uosmo" if neither of the provided tokens is "uosmo" - let mut admin_balances = tokens_provided + let mut admin_balances = pool_tokens .iter() .map(|coin| Coin::new(ADMIN_BALANCE_AMOUNT, coin.denom.clone())) .collect::>(); - if !tokens_provided.iter().any(|coin| coin.denom == "uosmo") { + if !pool_tokens.iter().any(|coin| coin.denom == "uosmo") { admin_balances.push(Coin::new(ADMIN_BALANCE_AMOUNT, "uosmo".to_string())); } @@ -54,10 +60,10 @@ pub mod initialize { &admin_balances, MsgCreateConcentratedPool { sender: "overwritten".to_string(), - denom0: tokens_provided + denom0: pool_tokens .get(0) .map_or(String::new(), |coin| coin.denom.clone()), - denom1: tokens_provided + denom1: pool_tokens .get(1) .map_or(String::new(), |coin| coin.denom.clone()), tick_spacing: 100, @@ -65,7 +71,8 @@ pub mod initialize { }, -5000000, // 0.5 spot price 500000, // 1.5 spot price - tokens_provided, + pool_tokens, + vault_tokens, Uint128::zero(), Uint128::zero(), ); @@ -79,7 +86,8 @@ pub mod initialize { pool: MsgCreateConcentratedPool, lower_tick: i64, upper_tick: i64, - mut tokens_provided: Vec, + mut pool_tokens: Vec, + mut vault_tokens: Vec, token_min_amount0: Uint128, token_min_amount1: Uint128, ) -> (OsmosisTestApp, Addr, u64, SigningAccount) { @@ -124,7 +132,7 @@ pub mod initialize { let pool: Pool = Pool::decode(pools.pools[0].value.as_slice()).unwrap(); // Sort tokens alphabetically by denom name or Osmosis will return an error - tokens_provided.sort_by(|a, b| a.denom.cmp(&b.denom)); // can't use helpers.rs::sort_tokens() due to different Coin type + pool_tokens.sort_by(|a, b| a.denom.cmp(&b.denom)); // can't use helpers.rs::sort_tokens() due to different Coin type // Create a first position in the pool with the admin user to simulate liquidity availability on the CL Pool cl.create_position( @@ -133,7 +141,7 @@ pub mod initialize { sender: admin.address(), lower_tick, upper_tick, - tokens_provided: tokens_provided.clone(), + tokens_provided: osmosis_std::cosmwasm_to_proto_coins(pool_tokens.clone()), token_min_amount0: token_min_amount0.to_string(), token_min_amount1: token_min_amount1.to_string(), }, @@ -144,14 +152,14 @@ pub mod initialize { // Get and assert spot price is 1.0 let spot_price: osmosis_std::types::osmosis::poolmanager::v1beta1::SpotPriceResponse = pm .query_spot_price(&SpotPriceRequest { - base_asset_denom: tokens_provided[0].denom.to_string(), - quote_asset_denom: tokens_provided[1].denom.to_string(), + base_asset_denom: pool_tokens[0].denom.to_string(), + quote_asset_denom: pool_tokens[1].denom.to_string(), pool_id: pool.id, }) .unwrap(); // Assuming tokens_provided[0].amount and tokens_provided[1].amount are String - let tokens_provided_0_amount: u128 = tokens_provided[0].amount.parse().unwrap(); - let tokens_provided_1_amount: u128 = tokens_provided[1].amount.parse().unwrap(); + let tokens_provided_0_amount: u128 = pool_tokens[0].amount.u128(); + let tokens_provided_1_amount: u128 = pool_tokens[1].amount.u128(); // Assuming `spot_price.spot_price` is a string representation of a float. let spot_price_float: f64 = spot_price.spot_price.parse().unwrap(); let division_result: f64 = @@ -182,8 +190,7 @@ pub mod initialize { }, Some(admin.address().as_str()), Some("cl-vault"), - // sort_tokens(vec![coin(1000, pool.token0), coin(1000, pool.token1)]).as_ref(), - sort_tokens(vec![coin(1000000, pool.token0), coin(1000000, pool.token1)]).as_ref(), // TODO: De-hardcode this and makes this configurable as argument + vault_tokens.as_ref(), &admin, ) .unwrap(); @@ -195,16 +202,15 @@ pub mod initialize { #[ignore] fn default_init_works() { let (app, contract_address, cl_pool_id, admin) = default_init(vec![ - v1beta1::Coin { - denom: "uatom".to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: "uosmo".to_string(), - amount: "1000000000000".to_string(), - }, + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); + let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let tf = TokenFactory::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs b/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs index 57f4cd61f..7b443d171 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod tests { - use cosmwasm_std::{Addr, Coin, Decimal, Uint128}; + use cosmwasm_std::{coin, Addr, Coin, Decimal, Uint128}; use osmosis_std::types::cosmos::bank::v1beta1::{QueryBalanceRequest, QueryBalanceResponse}; use osmosis_std::types::cosmwasm::wasm::v1::MsgExecuteContractResponse; use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::PositionByIdRequest; @@ -479,14 +479,12 @@ mod tests { initial_lower_tick, initial_upper_tick, vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000000000".to_string(), - }, + coin(1_000_000_000_000_000_000,DENOM_BASE.to_string()), + coin(1_000_000_000_000_000_000, DENOM_QUOTE.to_string()) + ], + vec![ + coin(1_000_000,DENOM_BASE.to_string()), + coin(1_000_000, DENOM_QUOTE.to_string()) ], Uint128::zero(), Uint128::zero(), diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index b87adc59d..d96fa1db3 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -15,14 +15,13 @@ mod test { use crate::{ msg::{ExecuteMsg, ModifyRangeMsg, QueryMsg}, query::PositionResponse, - test_tube::initialize::initialize::default_init, + test_tube::initialize::initialize::{ + default_init, init_test_contract, ADMIN_BALANCE_AMOUNT, DENOM_BASE, DENOM_QUOTE, + TOKENS_PROVIDED_AMOUNT, + }, }; - const TOKENS_PROVIDED_AMOUNT: &str = "1000000000000"; - const DENOM_BASE: &str = "uatom"; - const DENOM_QUOTE: &str = "uosmo"; - - /// # Test: move_range_works_18dec_usdc + /// # Test: move_range_works_dym_usdc /// /// This test case initializes a Concentrated Liquidity (CL) pool with 18DEC and USDC tokens /// to simulate a real-world scenario on the blockchain with a specific spot price. The purpose @@ -51,14 +50,12 @@ mod test { #[ignore] fn move_range_works_18dec_usdc() { let (app, contract, cl_pool_id, admin) = default_init(vec![ - v1beta1::Coin { - denom: "u18dec".to_string(), - amount: "1000000000000000000000000000000".to_string(), - }, - v1beta1::Coin { - denom: "uusdc".to_string(), - amount: "7250000000000000000".to_string(), - }, + coin(1_000_000_000_000_000_000, "udym"), + coin(7_250_000, "uusdc"), + ], + vec![ + coin(1_000_000, "udym"), + coin(1_000_000, "uusdc"), ]) .unwrap(); let wasm = Wasm::new(&app); @@ -73,7 +70,7 @@ mod test { upper_tick: 342000000, // max tick tokens_provided: vec![ v1beta1::Coin { - denom: "u18dec".to_string(), + denom: "udym".to_string(), amount: "1000000000000000000000000000000".to_string(), }, v1beta1::Coin { @@ -94,9 +91,9 @@ mod test { contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( ModifyRangeMsg { - lower_price: Decimal::from_str("0.000000000007").unwrap(), + lower_price: Decimal::from_str("0.00000000000675").unwrap(), upper_price: Decimal::from_str("0.0000000000075").unwrap(), - max_slippage: Decimal::bps(9500), + max_slippage: Decimal::bps(1), ratio_of_swappable_funds_to_use: Decimal::one(), twap_window_seconds: 45, }, @@ -106,6 +103,30 @@ mod test { ) .unwrap(); + // Create a first position in the pool with the admin user + cl.create_position( + MsgCreatePosition { + pool_id: cl_pool_id, + sender: admin.address(), + lower_tick: -10800000, + upper_tick: -1000000, + tokens_provided: vec![ + v1beta1::Coin { + denom: "udym".to_string(), + amount: 100_000_000_000u128.to_string(), + }, + v1beta1::Coin { + denom: "uusdc".to_string(), + amount: 100_000_000_000u128.to_string(), + }, + ], + token_min_amount0: Uint128::zero().to_string(), + token_min_amount1: Uint128::zero().to_string(), + }, + &admin, + ) + .unwrap(); + // One-sided re-range (above current tick, 100% token0) let _result = wasm .execute( @@ -114,7 +135,7 @@ mod test { ModifyRangeMsg { lower_price: Decimal::from_str("0.0000000000075").unwrap(), upper_price: Decimal::from_str("0.000000000008").unwrap(), - max_slippage: Decimal::bps(9500), + max_slippage: Decimal::from_str("0.00000000001").unwrap(), ratio_of_swappable_funds_to_use: Decimal::one(), twap_window_seconds: 45, }, @@ -130,7 +151,7 @@ mod test { contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ModifyRange( ModifyRangeMsg { - lower_price: Decimal::from_str("0.0000000000065").unwrap(), + lower_price: Decimal::from_str("0.000000000006").unwrap(), upper_price: Decimal::from_str("0.000000000007").unwrap(), max_slippage: Decimal::bps(9500), ratio_of_swappable_funds_to_use: Decimal::one(), @@ -147,16 +168,15 @@ mod test { #[ignore] fn move_range_works() { let (app, contract, cl_pool_id, admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); + let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let pm = PoolManager::new(&app); @@ -222,7 +242,7 @@ mod test { ModifyRangeMsg { lower_price: Decimal::from_str("400").unwrap(), upper_price: Decimal::from_str("1466").unwrap(), - max_slippage: Decimal::bps(9500), + max_slippage: Decimal::bps(5000), // this max slippage allows for a very large amount of price impact, mostly relevant here since the pool is illiquid ratio_of_swappable_funds_to_use: Decimal::one(), twap_window_seconds: 45, }, @@ -246,16 +266,15 @@ mod test { #[ignore] fn move_range_same_single_side_works() { let (app, contract, cl_pool_id, admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(100_000_000, DENOM_BASE.to_string()), + coin(100_000_000, DENOM_QUOTE.to_string()), ]) .unwrap(); + let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let pm = PoolManager::new(&app); @@ -336,18 +355,24 @@ mod test { */ #[test] #[ignore] - fn test_swap_math_poc() { - let (app, _contract, _cl_pool_id, admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + fn test_swap_math() { + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); + + let alice = app + .init_account(&[ + coin(1_000_000_000_000, DENOM_BASE), + coin(1_000_000_000_000, DENOM_QUOTE), + ]) + .unwrap(); + let cl = ConcentratedLiquidity::new(&app); let pools = cl.query_pools(&PoolsRequest { pagination: None }).unwrap(); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index 33008b189..b732759ff 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -4,8 +4,10 @@ mod tests { use crate::test_tube::helpers::{ get_event_attributes_by_ty_and_key, get_event_value_amount_numeric, }; - use crate::test_tube::initialize::initialize::default_init; - use cosmwasm_std::{assert_approx_eq, Coin, Uint128}; + use crate::test_tube::initialize::initialize::{ + default_init, ACCOUNTS_INIT_BALANCE, DENOM_BASE, DENOM_QUOTE, TOKENS_PROVIDED_AMOUNT, + }; + use cosmwasm_std::{assert_approx_eq, coin, Coin, Uint128}; use osmosis_std::types::cosmos::base::v1beta1::{self, Coin as OsmoCoin}; use osmosis_std::types::osmosis::poolmanager::v1beta1::{ MsgSwapExactAmountIn, SwapAmountInRoute, @@ -13,10 +15,7 @@ mod tests { use osmosis_test_tube::RunnerError::ExecuteError; use osmosis_test_tube::{Account, Module, PoolManager, Wasm}; - const DENOM_BASE: &str = "uatom"; - const DENOM_QUOTE: &str = "uosmo"; const ACCOUNTS_NUM: u64 = 10; - const ACCOUNTS_INIT_BALANCE: u128 = 1_000_000_000_000_000; const DEPOSIT_AMOUNT: u128 = 5_000_000; const SWAPS_NUM: usize = 10; const SWAPS_AMOUNT: &str = "1000000000"; @@ -25,15 +24,13 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim() { - let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); @@ -53,7 +50,7 @@ mod tests { for account in &accounts { let _ = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -91,7 +88,7 @@ mod tests { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), // this is ignored the first time but lets pass it anyway for now }), @@ -118,7 +115,7 @@ mod tests { for _ in 0..(ACCOUNTS_NUM - 1) { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), // this is ignored the first time but lets pass it anyway for now }), @@ -139,7 +136,7 @@ mod tests { // Collect one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), }), @@ -161,7 +158,7 @@ mod tests { // Adjust the number of distribute actions as needed let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), // hardcoding 1 @@ -185,7 +182,7 @@ mod tests { // Distribute one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), }), @@ -207,7 +204,7 @@ mod tests { for account in &accounts { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ClaimRewards {}), &[], account, @@ -228,15 +225,13 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_cycles() { - let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); @@ -261,7 +256,7 @@ mod tests { for account in &accounts { let _ = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -295,7 +290,7 @@ mod tests { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::new(1), }), @@ -320,7 +315,7 @@ mod tests { for _ in 0..(ACCOUNTS_NUM - 1) { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::new(1), @@ -339,7 +334,7 @@ mod tests { // Collect one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), }), @@ -357,7 +352,7 @@ mod tests { // Adjust the number of distribute actions as needed let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), // hardcoding 1 @@ -380,7 +375,7 @@ mod tests { // Distribute one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), @@ -400,7 +395,7 @@ mod tests { for account in &accounts { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::ClaimRewards {}, ), @@ -424,15 +419,13 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_no_rewards_works() { - let (app, contract_address, _cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); @@ -452,7 +445,7 @@ mod tests { for account in &accounts { let _ = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -469,7 +462,7 @@ mod tests { // Collect and Distribute Rewards (there should be anything) let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), }), @@ -496,7 +489,7 @@ mod tests { // Try to collect one more time, this should be closing the process and set to Ready as there are not rewards let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), }), @@ -516,7 +509,7 @@ mod tests { // Distribute just one time, as there are no rewards we expect this to clear the state even if 1 user < 10 users let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), }), @@ -534,15 +527,13 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_deposit_between() { - let (app, contract_address, cl_pool_id, _admin) = default_init(vec![ - v1beta1::Coin { - denom: DENOM_BASE.to_string(), - amount: "1000000000000".to_string(), - }, - v1beta1::Coin { - denom: DENOM_QUOTE.to_string(), - amount: "1000000000000".to_string(), - }, + let (app, contract, cl_pool_id, admin) = default_init(vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), ]) .unwrap(); @@ -562,7 +553,7 @@ mod tests { for account in &accounts { let _ = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -600,7 +591,7 @@ mod tests { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), // this is ignored the first time but lets pass it anyway for now }), @@ -613,7 +604,7 @@ mod tests { for _ in 0..(ACCOUNTS_NUM - 1) { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), // this is ignored the first time but lets pass it anyway for now }), @@ -631,7 +622,7 @@ mod tests { for account in &accounts { let _ = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::ExactDeposit { recipient: None }, &[ Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -645,7 +636,7 @@ mod tests { // Collect one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { amount_of_users: Uint128::one(), }), @@ -663,7 +654,7 @@ mod tests { // Adjust the number of distribute actions as needed let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension( crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), // hardcoding 1 @@ -683,7 +674,7 @@ mod tests { // Distribute one more time to finish, even if we extra deposited with one more user we expect the distribution to finish let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::DistributeRewards { amount_of_users: Uint128::one(), }), @@ -702,7 +693,7 @@ mod tests { for account in &accounts { let result = wasm .execute( - contract_address.as_str(), + contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ClaimRewards {}), &[], account, @@ -761,7 +752,7 @@ mod tests { // #[test] // #[ignore] // fn test_rewards_single_distribute_claim_max_users(users in 10..u64::MAX) { - // let (app, contract_address, cl_pool_id, _admin) = default_init(); + // let (app, contract, cl_pool_id, _admin) = default_init(); // // Initialize accounts // let accounts = app @@ -779,7 +770,7 @@ mod tests { // for account in &accounts { // let _ = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::ExactDeposit { recipient: None }, // &[ // Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -818,7 +809,7 @@ mod tests { // // Collect and Distribute Rewards // let result = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards {}), // &[], // claimer, @@ -847,7 +838,7 @@ mod tests { // // Adjust the number of distribute actions as needed // let result = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::VaultExtension( // crate::msg::ExtensionExecuteMsg::DistributeRewards { // amount_of_users: Uint128::new(1), // hardcoding 1 @@ -878,7 +869,7 @@ mod tests { // for account in &extra_accounts { // let _ = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::ExactDeposit { recipient: None }, // &[ // Coin::new(DEPOSIT_AMOUNT, DENOM_BASE), @@ -892,7 +883,7 @@ mod tests { // // Distribute one more time to finish, even if we extra deposited with one more user we expect the distribution to finish // let result = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::DistributeRewards { // amount_of_users: Uint128::new(1), // }), @@ -913,7 +904,7 @@ mod tests { // for account in &accounts { // let result = wasm // .execute( - // contract_address.as_str(), + // contract.as_str(), // &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::ClaimRewards {}), // &[], // account, diff --git a/smart-contracts/contracts/cl-vault/src/vault/range.rs b/smart-contracts/contracts/cl-vault/src/vault/range.rs index 262c35917..2d2630da8 100644 --- a/smart-contracts/contracts/cl-vault/src/vault/range.rs +++ b/smart-contracts/contracts/cl-vault/src/vault/range.rs @@ -188,18 +188,17 @@ pub fn handle_withdraw_position_reply( let pool_details = get_cl_pool_info(&deps.querier, pool_config.pool_id)?; // if only one token is being deposited, and we are moving into a position where any amount of the other token is needed, - // creating the position here will fail because liquidityNeeded is calculated as 0 on chain level + // creating the position here will fail because liquidityNeeded is calculated as 0 on the chain module level // we can fix this by going straight into a swap-deposit-merge before creating any positions - // todo: Check if needs LTE or just LT - // 0 token0 and current_tick > lower_tick - // 0 token1 and current_tick < upper_tick - // if (lower < current < upper) && amount0 == 0 || amount1 == 0 - // also onesided but wrong token - // bad complexity demon, grug no like - if (amount0.is_zero() && pool_details.current_tick < modify_range_state.upper_tick) - || (amount1.is_zero() && pool_details.current_tick > modify_range_state.lower_tick) - { + // we swap token 1 to token 0 if we don't have any token 0 and the upper price is above the current price + let should_swap_1_to_0 = + amount0.is_zero() && pool_details.current_tick < modify_range_state.upper_tick; + // we swap token 0 to token 1 if we don't have any token 1 and the lower price is below the current price + let should_swap_0_to_1 = + amount1.is_zero() && pool_details.current_tick > modify_range_state.lower_tick; + + if should_swap_1_to_0 || should_swap_0_to_1 { do_swap_deposit_merge( deps, env, From 94f4af47d563bafdded4bbb0f863ea8efa83a05d Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 20:28:20 +0100 Subject: [PATCH 15/24] resolve warnings --- .../contracts/cl-vault/src/test_tube/admin.rs | 19 ++--- .../src/test_tube/deposit_withdraw.rs | 55 +++++++------ .../cl-vault/src/test_tube/initialize.rs | 24 +++--- .../cl-vault/src/test_tube/proptest.rs | 5 +- .../contracts/cl-vault/src/test_tube/range.rs | 79 +++++++++---------- .../cl-vault/src/test_tube/rewards.rs | 76 ++++++++++-------- 6 files changed, 133 insertions(+), 125 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs index d7cb76980..fc74886d0 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/admin.rs @@ -1,7 +1,6 @@ #[cfg(test)] mod tests { use cosmwasm_std::coin; - use osmosis_std::types::cosmos::base::v1beta1; use crate::test_tube::initialize::initialize::{default_init, TOKENS_PROVIDED_AMOUNT}; @@ -11,14 +10,16 @@ mod tests { #[test] #[ignore] fn range_admin_update_works() { - let (_app, _contract, _cl_pool_id, _admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (_app, _contract, _cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); // change the range admin and verify that it works } diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs index fd52ff32c..cd899c161 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/deposit_withdraw.rs @@ -2,7 +2,6 @@ mod tests { use cosmwasm_std::{assert_approx_eq, coin, Coin, Uint128}; - use osmosis_std::types::cosmos::base::v1beta1; use osmosis_test_tube::{Account, Module, Wasm}; use crate::{ @@ -18,14 +17,16 @@ mod tests { #[test] #[ignore] fn single_deposit_withdraw_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, _cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); let wasm = Wasm::new(&app); @@ -135,14 +136,16 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, _cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); let wasm = Wasm::new(&app); @@ -271,14 +274,16 @@ mod tests { #[test] #[ignore] fn multiple_deposit_withdraw_unused_funds_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, _cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); //let bank = Bank::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs index 9ee22a732..a6a03f9d4 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/initialize.rs @@ -21,7 +21,6 @@ pub mod initialize { SigningAccount, TokenFactory, Wasm, }; - use crate::helpers::sort_tokens; use crate::msg::{ ClQueryMsg, ExecuteMsg, ExtensionQueryMsg, InstantiateMsg, ModifyRangeMsg, QueryMsg, }; @@ -34,7 +33,6 @@ pub mod initialize { pub(crate) const DENOM_QUOTE: &str = "uosmo"; pub(crate) const ACCOUNTS_INIT_BALANCE: u128 = 1_000_000_000_000_000; - // Define init variants here pub fn default_init( @@ -87,7 +85,7 @@ pub mod initialize { lower_tick: i64, upper_tick: i64, mut pool_tokens: Vec, - mut vault_tokens: Vec, + vault_tokens: Vec, token_min_amount0: Uint128, token_min_amount1: Uint128, ) -> (OsmosisTestApp, Addr, u64, SigningAccount) { @@ -201,16 +199,18 @@ pub mod initialize { #[test] #[ignore] fn default_init_works() { - let (app, contract_address, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract_address, cl_pool_id, admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); - + let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); let tf = TokenFactory::new(&app); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs b/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs index 7b443d171..26da00fda 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/proptest.rs @@ -3,11 +3,8 @@ mod tests { use cosmwasm_std::{coin, Addr, Coin, Decimal, Uint128}; use osmosis_std::types::cosmos::bank::v1beta1::{QueryBalanceRequest, QueryBalanceResponse}; use osmosis_std::types::cosmwasm::wasm::v1::MsgExecuteContractResponse; + use osmosis_std::types::osmosis::concentratedliquidity::poolmodel::concentrated::v1beta1::MsgCreateConcentratedPool; use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::PositionByIdRequest; - use osmosis_std::types::{ - cosmos::base::v1beta1, - osmosis::concentratedliquidity::poolmodel::concentrated::v1beta1::MsgCreateConcentratedPool, - }; use osmosis_test_tube::{ Account, Bank, ConcentratedLiquidity, ExecuteResponse, Module, OsmosisTestApp, SigningAccount, Wasm, diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs index d96fa1db3..f5e270afb 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/range.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/range.rs @@ -16,8 +16,7 @@ mod test { msg::{ExecuteMsg, ModifyRangeMsg, QueryMsg}, query::PositionResponse, test_tube::initialize::initialize::{ - default_init, init_test_contract, ADMIN_BALANCE_AMOUNT, DENOM_BASE, DENOM_QUOTE, - TOKENS_PROVIDED_AMOUNT, + default_init, DENOM_BASE, DENOM_QUOTE, TOKENS_PROVIDED_AMOUNT, }, }; @@ -49,14 +48,13 @@ mod test { #[test] #[ignore] fn move_range_works_18dec_usdc() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(1_000_000_000_000_000_000, "udym"), - coin(7_250_000, "uusdc"), - ], - vec![ - coin(1_000_000, "udym"), - coin(1_000_000, "uusdc"), - ]) + let (app, contract, cl_pool_id, admin) = default_init( + vec![ + coin(1_000_000_000_000_000_000, "udym"), + coin(7_250_000, "uusdc"), + ], + vec![coin(1_000_000, "udym"), coin(1_000_000, "uusdc")], + ) .unwrap(); let wasm = Wasm::new(&app); let cl = ConcentratedLiquidity::new(&app); @@ -167,14 +165,16 @@ mod test { #[test] #[ignore] fn move_range_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, cl_pool_id, admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); let wasm = Wasm::new(&app); @@ -265,14 +265,16 @@ mod test { #[test] #[ignore] fn move_range_same_single_side_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(100_000_000, DENOM_BASE.to_string()), - coin(100_000_000, DENOM_QUOTE.to_string()), - ]) + let (app, contract, cl_pool_id, admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(100_000_000, DENOM_BASE.to_string()), + coin(100_000_000, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); let wasm = Wasm::new(&app); @@ -356,23 +358,18 @@ mod test { #[test] #[ignore] fn test_swap_math() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, _contract, _cl_pool_id, admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); - let alice = app - .init_account(&[ - coin(1_000_000_000_000, DENOM_BASE), - coin(1_000_000_000_000, DENOM_QUOTE), - ]) - .unwrap(); - let cl = ConcentratedLiquidity::new(&app); let pools = cl.query_pools(&PoolsRequest { pagination: None }).unwrap(); diff --git a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs index b732759ff..253e981f9 100644 --- a/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs +++ b/smart-contracts/contracts/cl-vault/src/test_tube/rewards.rs @@ -8,7 +8,7 @@ mod tests { default_init, ACCOUNTS_INIT_BALANCE, DENOM_BASE, DENOM_QUOTE, TOKENS_PROVIDED_AMOUNT, }; use cosmwasm_std::{assert_approx_eq, coin, Coin, Uint128}; - use osmosis_std::types::cosmos::base::v1beta1::{self, Coin as OsmoCoin}; + use osmosis_std::types::cosmos::base::v1beta1::Coin as OsmoCoin; use osmosis_std::types::osmosis::poolmanager::v1beta1::{ MsgSwapExactAmountIn, SwapAmountInRoute, }; @@ -24,14 +24,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); // Initialize accounts @@ -225,14 +227,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_cycles() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); // Initialize accounts @@ -419,14 +423,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_no_rewards_works() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, _cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); // Initialize accounts @@ -527,14 +533,16 @@ mod tests { #[test] #[ignore] fn test_rewards_single_distribute_claim_deposit_between() { - let (app, contract, cl_pool_id, admin) = default_init(vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ], - vec![ - coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), - coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), - ]) + let (app, contract, cl_pool_id, _admin) = default_init( + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + vec![ + coin(TOKENS_PROVIDED_AMOUNT, DENOM_BASE.to_string()), + coin(TOKENS_PROVIDED_AMOUNT, DENOM_QUOTE.to_string()), + ], + ) .unwrap(); // Initialize accounts @@ -589,7 +597,7 @@ mod tests { .unwrap(); } - let result = wasm + let _result = wasm .execute( contract.as_str(), &ExecuteMsg::VaultExtension(crate::msg::ExtensionExecuteMsg::CollectRewards { From cd929b76663054d34aadab597ccd769149f3a5c3 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 20:35:54 +0100 Subject: [PATCH 16/24] addressing Laus comment --- .../contracts/cl-vault/src/helpers.rs | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/helpers.rs b/smart-contracts/contracts/cl-vault/src/helpers.rs index a25179a5e..93d478cb8 100644 --- a/smart-contracts/contracts/cl-vault/src/helpers.rs +++ b/smart-contracts/contracts/cl-vault/src/helpers.rs @@ -163,34 +163,28 @@ pub fn get_twap_price( const SCALE_FACTOR: u128 = 10u128.pow(12); fn scale_if_needed( - cur_price_sqrt: Decimal256, - upper_price_sqrt: Decimal256, - lower_price_sqrt: Decimal256, - current_price: Decimal256, + mut cur_price_sqrt: Decimal256, + mut upper_price_sqrt: Decimal256, + mut lower_price_sqrt: Decimal256, + mut current_price: Decimal256, ) -> (bool, Decimal256, Decimal256, Decimal256, Decimal256) { let scale_up_threshold = Decimal256::from_ratio(1u128, SCALE_FACTOR); let product = cur_price_sqrt * upper_price_sqrt * lower_price_sqrt; + let mut needs_scaling = false; + // Scale the square root prices and current price only if needed if product <= scale_up_threshold { let scale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); - // Scale the square root prices and current price - let scaled_cur_price_sqrt = cur_price_sqrt.checked_mul(scale_factor).unwrap(); - let scaled_upper_price_sqrt = upper_price_sqrt.checked_mul(scale_factor).unwrap(); - let scaled_lower_price_sqrt = lower_price_sqrt.checked_mul(scale_factor).unwrap(); - let scaled_current_price = current_price.checked_mul(scale_factor).unwrap(); - - return ( - true, - scaled_cur_price_sqrt, - scaled_upper_price_sqrt, - scaled_lower_price_sqrt, - scaled_current_price, - ); + needs_scaling = true; + cur_price_sqrt = cur_price_sqrt.checked_mul(scale_factor).unwrap(); + upper_price_sqrt = upper_price_sqrt.checked_mul(scale_factor).unwrap(); + lower_price_sqrt = lower_price_sqrt.checked_mul(scale_factor).unwrap(); + current_price = current_price.checked_mul(scale_factor).unwrap(); } ( - false, + needs_scaling, cur_price_sqrt, upper_price_sqrt, lower_price_sqrt, From 69e284724ca9ff60e55e5a2dd2b3dbb9d55dcc7c Mon Sep 17 00:00:00 2001 From: Laurens <32776056+LaurensKubat@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:49:13 -0300 Subject: [PATCH 17/24] make scale_if_needed arguments immutable --- .../contracts/cl-vault/src/helpers.rs | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/helpers.rs b/smart-contracts/contracts/cl-vault/src/helpers.rs index 93d478cb8..422554023 100644 --- a/smart-contracts/contracts/cl-vault/src/helpers.rs +++ b/smart-contracts/contracts/cl-vault/src/helpers.rs @@ -163,33 +163,34 @@ pub fn get_twap_price( const SCALE_FACTOR: u128 = 10u128.pow(12); fn scale_if_needed( - mut cur_price_sqrt: Decimal256, - mut upper_price_sqrt: Decimal256, - mut lower_price_sqrt: Decimal256, - mut current_price: Decimal256, + cur_price_sqrt: Decimal256, + upper_price_sqrt: Decimal256, + lower_price_sqrt: Decimal256, + current_price: Decimal256, ) -> (bool, Decimal256, Decimal256, Decimal256, Decimal256) { let scale_up_threshold = Decimal256::from_ratio(1u128, SCALE_FACTOR); let product = cur_price_sqrt * upper_price_sqrt * lower_price_sqrt; - let mut needs_scaling = false; // Scale the square root prices and current price only if needed if product <= scale_up_threshold { let scale_factor = Decimal256::from_atomics(SCALE_FACTOR, 0).unwrap(); - needs_scaling = true; - cur_price_sqrt = cur_price_sqrt.checked_mul(scale_factor).unwrap(); - upper_price_sqrt = upper_price_sqrt.checked_mul(scale_factor).unwrap(); - lower_price_sqrt = lower_price_sqrt.checked_mul(scale_factor).unwrap(); - current_price = current_price.checked_mul(scale_factor).unwrap(); + ( + true, + cur_price_sqrt.checked_mul(scale_factor).unwrap(), + upper_price_sqrt.checked_mul(scale_factor).unwrap(), + lower_price_sqrt.checked_mul(scale_factor).unwrap(), + current_price.checked_mul(scale_factor).unwrap(), + ) + } else { + ( + false, + cur_price_sqrt, + upper_price_sqrt, + lower_price_sqrt, + current_price, + ) } - - ( - needs_scaling, - cur_price_sqrt, - upper_price_sqrt, - lower_price_sqrt, - current_price, - ) } // this math is straight from the readme From 714c9234dc17976b2eee39914fa0c90ba1eb8f81 Mon Sep 17 00:00:00 2001 From: Laurens <32776056+LaurensKubat@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:19:56 -0300 Subject: [PATCH 18/24] downgrade osmosis-std and osmosis-test-tube --- smart-contracts/Cargo.lock | 18 +++++++++--------- smart-contracts/contracts/cl-vault/Cargo.toml | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 9bdc72fdf..0ba69ce7a 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -391,7 +391,7 @@ dependencies = [ "cw-vault-multi-standard", "cw2 1.1.1", "num_enum 0.7.0", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "osmosis-test-tube", "proptest", "prost 0.12.3", @@ -1855,9 +1855,9 @@ dependencies = [ [[package]] name = "osmosis-std" -version = "0.22.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8641c376f01f5af329dc2a34e4f5527eaeb0bde18cda8d86fed958d04c86159c" +checksum = "e87adf61f03306474ce79ab322d52dfff6b0bcf3aed1e12d8864ac0400dec1bf" dependencies = [ "chrono", "cosmwasm-std", @@ -1896,15 +1896,15 @@ dependencies = [ [[package]] name = "osmosis-test-tube" -version = "22.1.0" +version = "21.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a082b97136d15470a37aa758f227c865594590b69d74721248ed5adf59bf1ca2" +checksum = "c3a528c942d25d3159634f77953ca0e16c5a450fc44578ad922320128e4025fd" dependencies = [ "base64", "bindgen", "cosmrs", "cosmwasm-std", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "prost 0.12.3", "serde", "serde_json", @@ -3067,14 +3067,14 @@ dependencies = [ [[package]] name = "test-tube" -version = "0.5.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09184c7655b2bdaf4517b06141a2e4c44360904f2706a05b24c831bd97ad1db6" +checksum = "c17f30e7fea966bde5f9933a4cb2db79dd272115ea19d1656da2aac7ce0700fa" dependencies = [ "base64", "cosmrs", "cosmwasm-std", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "prost 0.12.3", "serde", "serde_json", diff --git a/smart-contracts/contracts/cl-vault/Cargo.toml b/smart-contracts/contracts/cl-vault/Cargo.toml index 7816bfbb0..2c69cffbe 100644 --- a/smart-contracts/contracts/cl-vault/Cargo.toml +++ b/smart-contracts/contracts/cl-vault/Cargo.toml @@ -47,11 +47,11 @@ cw-utils = "1.0.2" cw-vault-multi-standard = {git = "https://github.com/quasar-finance/cw-vault-standard", features = ["lockup", "force-unlock"]} cw2 = "1.0.1" num_enum = "0.7.0" -osmosis-std = "0.22.0" +osmosis-std = "0.21.0" prost = {version = "0.12.3", default-features = false} serde = {version = "1.0.145", default-features = false, features = ["derive"]} thiserror = {version = "1.0.31"} [dev-dependencies] -osmosis-test-tube = "22.1.0" +osmosis-test-tube = "21.0.0" proptest = "1.2.0" From c43c9f0cc2f995b9fbc9b6bbca6f85584fdaf596 Mon Sep 17 00:00:00 2001 From: Laurens <32776056+LaurensKubat@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:26:58 -0300 Subject: [PATCH 19/24] bump actions go version and test-tube b --- .github/workflows/build.yml | 2 +- smart-contracts/Cargo.lock | 18 +++++++++--------- smart-contracts/contracts/cl-vault/Cargo.toml | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f4125d728..a884a6b84 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,7 +38,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: 1.20.7 + go-version: 1.22.0 env: GOOS: ${{ matrix.targetos }} GOARCH: ${{ matrix.arch }} diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 0ba69ce7a..9bdc72fdf 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -391,7 +391,7 @@ dependencies = [ "cw-vault-multi-standard", "cw2 1.1.1", "num_enum 0.7.0", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "osmosis-test-tube", "proptest", "prost 0.12.3", @@ -1855,9 +1855,9 @@ dependencies = [ [[package]] name = "osmosis-std" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e87adf61f03306474ce79ab322d52dfff6b0bcf3aed1e12d8864ac0400dec1bf" +checksum = "8641c376f01f5af329dc2a34e4f5527eaeb0bde18cda8d86fed958d04c86159c" dependencies = [ "chrono", "cosmwasm-std", @@ -1896,15 +1896,15 @@ dependencies = [ [[package]] name = "osmosis-test-tube" -version = "21.0.0" +version = "22.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3a528c942d25d3159634f77953ca0e16c5a450fc44578ad922320128e4025fd" +checksum = "a082b97136d15470a37aa758f227c865594590b69d74721248ed5adf59bf1ca2" dependencies = [ "base64", "bindgen", "cosmrs", "cosmwasm-std", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "prost 0.12.3", "serde", "serde_json", @@ -3067,14 +3067,14 @@ dependencies = [ [[package]] name = "test-tube" -version = "0.3.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17f30e7fea966bde5f9933a4cb2db79dd272115ea19d1656da2aac7ce0700fa" +checksum = "09184c7655b2bdaf4517b06141a2e4c44360904f2706a05b24c831bd97ad1db6" dependencies = [ "base64", "cosmrs", "cosmwasm-std", - "osmosis-std 0.21.0", + "osmosis-std 0.22.0", "prost 0.12.3", "serde", "serde_json", diff --git a/smart-contracts/contracts/cl-vault/Cargo.toml b/smart-contracts/contracts/cl-vault/Cargo.toml index 2c69cffbe..7816bfbb0 100644 --- a/smart-contracts/contracts/cl-vault/Cargo.toml +++ b/smart-contracts/contracts/cl-vault/Cargo.toml @@ -47,11 +47,11 @@ cw-utils = "1.0.2" cw-vault-multi-standard = {git = "https://github.com/quasar-finance/cw-vault-standard", features = ["lockup", "force-unlock"]} cw2 = "1.0.1" num_enum = "0.7.0" -osmosis-std = "0.21.0" +osmosis-std = "0.22.0" prost = {version = "0.12.3", default-features = false} serde = {version = "1.0.145", default-features = false, features = ["derive"]} thiserror = {version = "1.0.31"} [dev-dependencies] -osmosis-test-tube = "21.0.0" +osmosis-test-tube = "22.1.0" proptest = "1.2.0" From 4a0d406ae209d7ed03b6d2faf79d763ce524ad96 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 21:35:44 +0100 Subject: [PATCH 20/24] go 1.21.7 ci --- .github/workflows/build.yml | 2 +- .github/workflows/lint.yml | 2 +- .github/workflows/test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a884a6b84..9c249e8fd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,7 +38,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: 1.22.0 + go-version: 1.21.7 env: GOOS: ${{ matrix.targetos }} GOARCH: ${{ matrix.arch }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 51f309fa7..fc615f2bf 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,7 +33,7 @@ jobs: if: env.GIT_DIFF uses: actions/setup-go@v4 with: - go-version: 1.20.7 + go-version: 1.21.7 - name: Display go version if: env.GIT_DIFF run: go version diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8a6d330f..6031299fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: if: env.GIT_DIFF uses: actions/setup-go@v3 with: - go-version: 1.20.7 + go-version: 1.21.7 - name: Display go version if: env.GIT_DIFF run: go version From 3168bb06186bdf4d2ab0de8743e8c1d041f2c54c Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 21:53:04 +0100 Subject: [PATCH 21/24] different go versions ci --- .github/workflows/build.yml | 9 ++++++++- .github/workflows/lint.yml | 7 ++++++- .github/workflows/test.yml | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9c249e8fd..7e5300a02 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,7 +38,7 @@ jobs: - name: Setup Golang uses: actions/setup-go@v4 with: - go-version: 1.21.7 + go-version: 1.20.7 env: GOOS: ${{ matrix.targetos }} GOARCH: ${{ matrix.arch }} @@ -70,6 +70,13 @@ jobs: **/**.rs Makefile .github/workflows/build.yml + - name: Setup Golang + uses: actions/setup-go@v4 + with: + go-version: 1.22.0 + env: + GOOS: ${{ matrix.targetos }} + GOARCH: ${{ matrix.arch }} - name: Build smart contracts if: env.GIT_DIFF run: make compile-wasm-artifacts diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index fc615f2bf..e00c1ec0b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,7 +33,7 @@ jobs: if: env.GIT_DIFF uses: actions/setup-go@v4 with: - go-version: 1.21.7 + go-version: 1.20.7 - name: Display go version if: env.GIT_DIFF run: go version @@ -51,6 +51,11 @@ jobs: PATTERNS: | **/**.rs Makefile + - name: Setup Golang + if: env.GIT_DIFF + uses: actions/setup-go@v4 + with: + go-version: 1.22.0 - name: Install Rust if: env.GIT_DIFF uses: dtolnay/rust-toolchain@stable diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6031299fc..9803d1917 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: if: env.GIT_DIFF uses: actions/setup-go@v3 with: - go-version: 1.21.7 + go-version: 1.20.7 - name: Display go version if: env.GIT_DIFF run: go version @@ -52,6 +52,11 @@ jobs: with: PATTERNS: | **/**.rs + - name: Setup Golang + if: env.GIT_DIFF + uses: actions/setup-go@v3 + with: + go-version: 1.22.0 - name: Install Rust if: env.GIT_DIFF uses: dtolnay/rust-toolchain@stable From 7e9921bc75112d3fc8c77aae698877a2408b2245 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Thu, 15 Feb 2024 22:18:30 +0100 Subject: [PATCH 22/24] revert changes outside smart contracts --- .github/workflows/build.yml | 7 ------- .github/workflows/lint.yml | 5 ----- .github/workflows/test.yml | 5 ----- 3 files changed, 17 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7e5300a02..f4125d728 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -70,13 +70,6 @@ jobs: **/**.rs Makefile .github/workflows/build.yml - - name: Setup Golang - uses: actions/setup-go@v4 - with: - go-version: 1.22.0 - env: - GOOS: ${{ matrix.targetos }} - GOARCH: ${{ matrix.arch }} - name: Build smart contracts if: env.GIT_DIFF run: make compile-wasm-artifacts diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e00c1ec0b..51f309fa7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -51,11 +51,6 @@ jobs: PATTERNS: | **/**.rs Makefile - - name: Setup Golang - if: env.GIT_DIFF - uses: actions/setup-go@v4 - with: - go-version: 1.22.0 - name: Install Rust if: env.GIT_DIFF uses: dtolnay/rust-toolchain@stable diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9803d1917..c8a6d330f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,11 +52,6 @@ jobs: with: PATTERNS: | **/**.rs - - name: Setup Golang - if: env.GIT_DIFF - uses: actions/setup-go@v3 - with: - go-version: 1.22.0 - name: Install Rust if: env.GIT_DIFF uses: dtolnay/rust-toolchain@stable From 4310d7e8fdd05befd79dbef3529bc97de3964a51 Mon Sep 17 00:00:00 2001 From: Laurens <32776056+LaurensKubat@users.noreply.github.com> Date: Thu, 15 Feb 2024 18:29:09 -0300 Subject: [PATCH 23/24] downgrade osmosis-std and test-tube --- smart-contracts/Cargo.lock | 18 +++++++++--------- smart-contracts/contracts/cl-vault/Cargo.toml | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 9bdc72fdf..0ba69ce7a 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -391,7 +391,7 @@ dependencies = [ "cw-vault-multi-standard", "cw2 1.1.1", "num_enum 0.7.0", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "osmosis-test-tube", "proptest", "prost 0.12.3", @@ -1855,9 +1855,9 @@ dependencies = [ [[package]] name = "osmosis-std" -version = "0.22.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8641c376f01f5af329dc2a34e4f5527eaeb0bde18cda8d86fed958d04c86159c" +checksum = "e87adf61f03306474ce79ab322d52dfff6b0bcf3aed1e12d8864ac0400dec1bf" dependencies = [ "chrono", "cosmwasm-std", @@ -1896,15 +1896,15 @@ dependencies = [ [[package]] name = "osmosis-test-tube" -version = "22.1.0" +version = "21.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a082b97136d15470a37aa758f227c865594590b69d74721248ed5adf59bf1ca2" +checksum = "c3a528c942d25d3159634f77953ca0e16c5a450fc44578ad922320128e4025fd" dependencies = [ "base64", "bindgen", "cosmrs", "cosmwasm-std", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "prost 0.12.3", "serde", "serde_json", @@ -3067,14 +3067,14 @@ dependencies = [ [[package]] name = "test-tube" -version = "0.5.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09184c7655b2bdaf4517b06141a2e4c44360904f2706a05b24c831bd97ad1db6" +checksum = "c17f30e7fea966bde5f9933a4cb2db79dd272115ea19d1656da2aac7ce0700fa" dependencies = [ "base64", "cosmrs", "cosmwasm-std", - "osmosis-std 0.22.0", + "osmosis-std 0.21.0", "prost 0.12.3", "serde", "serde_json", diff --git a/smart-contracts/contracts/cl-vault/Cargo.toml b/smart-contracts/contracts/cl-vault/Cargo.toml index 7816bfbb0..2c69cffbe 100644 --- a/smart-contracts/contracts/cl-vault/Cargo.toml +++ b/smart-contracts/contracts/cl-vault/Cargo.toml @@ -47,11 +47,11 @@ cw-utils = "1.0.2" cw-vault-multi-standard = {git = "https://github.com/quasar-finance/cw-vault-standard", features = ["lockup", "force-unlock"]} cw2 = "1.0.1" num_enum = "0.7.0" -osmosis-std = "0.22.0" +osmosis-std = "0.21.0" prost = {version = "0.12.3", default-features = false} serde = {version = "1.0.145", default-features = false, features = ["derive"]} thiserror = {version = "1.0.31"} [dev-dependencies] -osmosis-test-tube = "22.1.0" +osmosis-test-tube = "21.0.0" proptest = "1.2.0" From 0e3f4f183d44f01732ee7d9972c55c5ee6f87f68 Mon Sep 17 00:00:00 2001 From: magiodev <31893902+magiodev@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:48:38 +0100 Subject: [PATCH 24/24] remove migrate logic --- smart-contracts/contracts/cl-vault/src/contract.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/smart-contracts/contracts/cl-vault/src/contract.rs b/smart-contracts/contracts/cl-vault/src/contract.rs index a42e3e65a..3673f9845 100644 --- a/smart-contracts/contracts/cl-vault/src/contract.rs +++ b/smart-contracts/contracts/cl-vault/src/contract.rs @@ -11,10 +11,9 @@ use crate::query::{ use crate::reply::Replies; use crate::rewards::{ execute_collect_rewards, execute_distribute_rewards, handle_collect_incentives_reply, - handle_collect_spread_rewards_reply, CoinList, + handle_collect_spread_rewards_reply, }; -use crate::state::{RewardsStatus, CURRENT_TOTAL_SUPPLY, DISTRIBUTED_REWARDS, REWARDS_STATUS}; use crate::vault::admin::{execute_admin, execute_build_tick_exp_cache}; use crate::vault::claim::execute_claim_user_rewards; use crate::vault::deposit::{execute_exact_deposit, handle_deposit_create_position_reply}; @@ -29,7 +28,7 @@ use crate::vault::range::{ use crate::vault::withdraw::{execute_withdraw, handle_withdraw_user_reply}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, Uint128}; +use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response}; use cw2::set_contract_version; // version info for migration info @@ -186,9 +185,6 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result Result { - REWARDS_STATUS.save(deps.storage, &RewardsStatus::Ready)?; - DISTRIBUTED_REWARDS.save(deps.storage, &CoinList::new())?; - CURRENT_TOTAL_SUPPLY.save(deps.storage, &Uint128::zero())?; +pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { Ok(Response::new().add_attribute("migrate", "successful")) }