From d2f9993690392ea9d9276999bb4971fdcc284b12 Mon Sep 17 00:00:00 2001 From: Lars Lubkoll <11710767+lubkoll@users.noreply.github.com> Date: Tue, 20 Aug 2024 19:24:28 +0000 Subject: [PATCH] Replace PoolAssets with PoolPair --- .gitignore | 1 + .../osmosis/contracts/cl-vault/Cargo.toml | 1 + .../contracts/cl-vault/schema/cl-vault.json | 60 +------ .../cl-vault/schema/raw/execute.json | 31 +--- .../cl-vault/schema/raw/instantiate.json | 29 +--- .../contracts/cl-vault/src/helpers/getters.rs | 148 ++++++------------ 6 files changed, 64 insertions(+), 206 deletions(-) diff --git a/.gitignore b/.gitignore index bf8fbbe51..51b1b4015 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ containers_homes/ # Cargo+Git helper file (https://github.com/rust-lang/cargo/blob/0.44.1/src/cargo/sources/git/utils.rs#L320-L327) **/.cargo-ok **/*.rs.bk +.devcontainer/ diff --git a/smart-contracts/osmosis/contracts/cl-vault/Cargo.toml b/smart-contracts/osmosis/contracts/cl-vault/Cargo.toml index 4fe5c01c2..01f3569f2 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/Cargo.toml +++ b/smart-contracts/osmosis/contracts/cl-vault/Cargo.toml @@ -47,6 +47,7 @@ apollo-cw-asset = { workspace = true } dex-router-osmosis = {workspace = true} cw-vault-multi-standard = {git = "https://github.com/quasar-finance/cw-vault-standard", branch ="master", features = ["lockup", "force-unlock"]} osmosis-test-tube = { workspace = true, optional = true } +quasar-types = { workspace = true } [dev-dependencies] proptest = { workspace = true } diff --git a/smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json b/smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json index a58f08468..e9942c696 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json +++ b/smart-contracts/osmosis/contracts/cl-vault/schema/cl-vault.json @@ -72,7 +72,6 @@ "type": "string" }, "VaultConfig": { - "description": "VAULT_CONFIG: Base config struct for the contract.", "type": "object", "required": [ "dex_router", @@ -82,36 +81,16 @@ ], "properties": { "dex_router": { - "description": "Dex router address", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" }, "performance_fee": { - "description": "Percentage of profit to be charged as performance fee", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "swap_max_slippage": { - "description": "swap max slippage", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "treasury": { - "description": "Account to receive fee payments", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -602,7 +581,6 @@ "additionalProperties": false }, "Metadata": { - "description": "metadata useful for display purposes", "type": "object", "required": [ "name", @@ -610,7 +588,6 @@ ], "properties": { "name": { - "description": "the name of the vault", "type": "string" }, "thesis": { @@ -754,7 +731,6 @@ "type": "string" }, "VaultConfig": { - "description": "VAULT_CONFIG: Base config struct for the contract.", "type": "object", "required": [ "dex_router", @@ -764,36 +740,16 @@ ], "properties": { "dex_router": { - "description": "Dex router address", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" }, "performance_fee": { - "description": "Percentage of profit to be charged as performance fee", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "swap_max_slippage": { - "description": "swap max slippage", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "treasury": { - "description": "Account to receive fee payments", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" } }, "additionalProperties": false diff --git a/smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json b/smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json index 6825e09a0..fbbd23d71 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json +++ b/smart-contracts/osmosis/contracts/cl-vault/schema/raw/execute.json @@ -482,7 +482,6 @@ "additionalProperties": false }, "Metadata": { - "description": "metadata useful for display purposes", "type": "object", "required": [ "name", @@ -490,7 +489,6 @@ ], "properties": { "name": { - "description": "the name of the vault", "type": "string" }, "thesis": { @@ -634,7 +632,6 @@ "type": "string" }, "VaultConfig": { - "description": "VAULT_CONFIG: Base config struct for the contract.", "type": "object", "required": [ "dex_router", @@ -644,36 +641,16 @@ ], "properties": { "dex_router": { - "description": "Dex router address", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" }, "performance_fee": { - "description": "Percentage of profit to be charged as performance fee", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "swap_max_slippage": { - "description": "swap max slippage", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "treasury": { - "description": "Account to receive fee payments", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" } }, "additionalProperties": false diff --git a/smart-contracts/osmosis/contracts/cl-vault/schema/raw/instantiate.json b/smart-contracts/osmosis/contracts/cl-vault/schema/raw/instantiate.json index 7d7ae5b4d..780c92660 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/schema/raw/instantiate.json +++ b/smart-contracts/osmosis/contracts/cl-vault/schema/raw/instantiate.json @@ -68,7 +68,6 @@ "type": "string" }, "VaultConfig": { - "description": "VAULT_CONFIG: Base config struct for the contract.", "type": "object", "required": [ "dex_router", @@ -78,36 +77,16 @@ ], "properties": { "dex_router": { - "description": "Dex router address", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" }, "performance_fee": { - "description": "Percentage of profit to be charged as performance fee", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "swap_max_slippage": { - "description": "swap max slippage", - "allOf": [ - { - "$ref": "#/definitions/Decimal" - } - ] + "$ref": "#/definitions/Decimal" }, "treasury": { - "description": "Account to receive fee payments", - "allOf": [ - { - "$ref": "#/definitions/Addr" - } - ] + "$ref": "#/definitions/Addr" } }, "additionalProperties": false diff --git a/smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs b/smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs index c9bffb286..62020a93a 100644 --- a/smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs +++ b/smart-contracts/osmosis/contracts/cl-vault/src/helpers/getters.rs @@ -4,6 +4,7 @@ use std::str::FromStr; use osmosis_std::shim::Timestamp as OsmoTimestamp; use osmosis_std::types::osmosis::poolmanager::v1beta1::PoolmanagerQuerier; use osmosis_std::types::osmosis::twap::v1beta1::TwapQuerier; +use quasar_types::pool_pair::PoolPair; use crate::vault::concentrated_liquidity::get_position; use crate::{ @@ -66,17 +67,6 @@ pub fn get_twap_price( Ok(Decimal::from_str(&twap_price.arithmetic_twap)?) } -struct PoolAssets { - pub base: Coin, - pub quote: Coin, -} - -impl PoolAssets { - pub fn new(base: Coin, quote: Coin) -> Self { - Self { base, quote } - } -} - #[derive(Debug, PartialEq)] pub struct DepositInfo { pub base_deposit: Uint128, @@ -96,7 +86,7 @@ pub fn get_depositable_tokens( )?; let position = get_position(deps.storage, &deps.querier)?; - let assets = PoolAssets::new( + let assets = PoolPair::new( position.asset0.unwrap_or_default().try_into()?, position.asset1.unwrap_or_default().try_into()?, ); @@ -104,7 +94,7 @@ pub fn get_depositable_tokens( } fn get_deposit_info( - assets: &PoolAssets, + assets: &PoolPair, provided_base: Coin, provided_quote: Coin, ) -> Result { @@ -238,10 +228,13 @@ mod tests { use super::*; use crate::math::tick::{build_tick_exp_cache, price_to_tick}; use cosmwasm_std::testing::mock_dependencies; - use cosmwasm_std::{coin, Coin, Decimal256}; + use cosmwasm_std::{coin, Decimal256}; use std::collections::HashMap; use std::str::FromStr; + const TOKEN0: &str = "token0"; + const TOKEN1: &str = "token1"; + #[test] fn test_0_to_1_swap() { let mut deps = mock_dependencies(); @@ -361,141 +354,92 @@ mod tests { #[test] fn test_position_in_both_asset() { - let token0 = Coin { - denom: "token0".to_string(), - amount: Uint128::new(1_000_000_000u128), - }; - let token1 = Coin { - denom: "token1".to_string(), - amount: Uint128::new(100_000_000_000_000_000_000_000_000_000u128), - }; - - let assets = PoolAssets { - base: token0.clone(), - quote: token1.clone(), - }; + let token0 = coin(1_000_000_000, TOKEN0); + let token1 = coin(100_000_000_000_000_000_000_000_000_000, TOKEN1); + + let assets = PoolPair::new(token0.clone(), token1.clone()); let result = get_deposit_info(&assets, token0.clone(), token1.clone()).unwrap(); assert_eq!( result, DepositInfo { base_deposit: Uint128::zero(), - quote_deposit: Uint128::new(100_000_000_000_000_000_000_000_000_000u128), - base_refund: coin(1_000_000_000u128, token0.denom), - quote_refund: coin(0u128, token1.denom), + quote_deposit: token1.amount, + base_refund: token0, + quote_refund: coin(0, token1.denom), } ); } #[test] fn test_position_in_asset1_only() { - let token0 = Coin { - denom: "token0".to_string(), - amount: Uint128::new(50), - }; - let token1 = Coin { - denom: "token1".to_string(), - amount: Uint128::new(100), - }; - - let assets = PoolAssets { - base: Coin { - denom: "token0".to_string(), - amount: Uint128::zero(), - }, - quote: token1.clone(), - }; + let token0 = coin(50, TOKEN0); + let token1 = coin(100, TOKEN1); + + let assets = PoolPair::new(coin(0, TOKEN0), token1.clone()); let result = get_deposit_info(&assets, token0.clone(), token1.clone()).unwrap(); assert_eq!( result, DepositInfo { base_deposit: Uint128::zero(), - quote_deposit: Uint128::new(100), - base_refund: coin(50u128, token0.denom), - quote_refund: coin(0u128, token1.denom), + quote_deposit: token1.amount, + base_refund: token0, + quote_refund: coin(0, token1.denom), } ); } #[test] fn test_position_in_asset0_only() { - let token0 = Coin { - denom: "token0".to_string(), - amount: Uint128::new(50), - }; - let token1 = Coin { - denom: "token1".to_string(), - amount: Uint128::new(100), - }; - - let assets = PoolAssets { - quote: Coin { - denom: "token1".to_string(), - amount: Uint128::zero(), - }, - base: token0.clone(), - }; + let token0 = coin(50, TOKEN0); + let token1 = coin(100, TOKEN1); + + let assets = PoolPair::new(token0.clone(), coin(0, TOKEN1)); let result = get_deposit_info(&assets, token0.clone(), token1.clone()).unwrap(); assert_eq!( result, DepositInfo { - base_deposit: Uint128::new(50), + base_deposit: token0.amount, quote_deposit: Uint128::zero(), - base_refund: coin(0u128, token0.denom), - quote_refund: coin(100u128, token1.denom), + base_refund: coin(0, token0.denom), + quote_refund: token1, } ); } #[test] fn test_both_assets_present_token0_limiting() { - let token0 = Coin { - denom: "token0".to_string(), - amount: Uint128::new(50), - }; - let token1 = Coin { - denom: "token1".to_string(), - amount: Uint128::new(100), - }; - - let assets = PoolAssets { - base: token0.clone(), - quote: token1.clone(), - }; - let result = get_deposit_info(&assets, coin(2000, "token0"), coin(5000, "token1")).unwrap(); + let token0 = coin(50, TOKEN0); + let token1 = coin(100, TOKEN1); + + let assets = PoolPair::new(token0.clone(), token1.clone()); + let base_deposit = coin(2000, TOKEN0); + let result = get_deposit_info(&assets, base_deposit.clone(), coin(5000, TOKEN1)).unwrap(); assert_eq!( result, DepositInfo { - base_deposit: Uint128::new(2000), + base_deposit: base_deposit.amount, quote_deposit: Uint128::new(4000), - base_refund: coin(0u128, token0.denom), - quote_refund: coin(1000u128, token1.denom), + base_refund: coin(0, token0.denom), + quote_refund: coin(1000, token1.denom), } ); } #[test] fn test_both_assets_present_token1_limiting() { - let token0 = Coin { - denom: "token0".to_string(), - amount: Uint128::new(50), - }; - let token1 = Coin { - denom: "token1".to_string(), - amount: Uint128::new(100), - }; - - let assets = PoolAssets { - base: token0.clone(), - quote: token1.clone(), - }; - let result = get_deposit_info(&assets, coin(2000, "token0"), coin(3000, "token1")).unwrap(); + let token0 = coin(50, TOKEN0); + let token1 = coin(100, TOKEN1); + + let assets = PoolPair::new(token0.clone(), token1.clone()); + let quote_deposit = coin(3000, TOKEN1); + let result = get_deposit_info(&assets, coin(2000, TOKEN0), quote_deposit.clone()).unwrap(); assert_eq!( result, DepositInfo { base_deposit: Uint128::new(1500), - quote_deposit: Uint128::new(3000), - base_refund: coin(500u128, token0.denom), - quote_refund: coin(0u128, token1.denom), + quote_deposit: quote_deposit.amount, + base_refund: coin(500, token0.denom), + quote_refund: coin(0, token1.denom), } ); }