From 3e52f55ef00368c408a5f7fc53edf90a989f3aa7 Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Tue, 3 Sep 2024 17:56:59 +0100 Subject: [PATCH 1/3] fix: add max_spread param to router operations simulation --- Cargo.lock | 65 +++++++------------ Cargo.toml | 4 +- .../liquidity_hub/fee_collector/Cargo.toml | 2 +- .../fee_collector/src/commands.rs | 11 ++-- .../fee_collector/src/tests/integration.rs | 6 +- .../pool-network/stableswap_3pool/Cargo.toml | 2 +- .../stableswap_3pool/src/commands.rs | 7 +- .../pool-network/terraswap_factory/Cargo.toml | 2 +- .../terraswap_factory/src/state.rs | 2 +- .../pool-network/terraswap_pair/Cargo.toml | 2 +- .../terraswap_pair/src/commands.rs | 19 +----- .../pool-network/terraswap_router/Cargo.toml | 2 +- .../terraswap_router/src/contract.rs | 15 +++++ .../terraswap_router/src/testing/tests.rs | 2 + packages/white-whale-std/Cargo.toml | 2 +- .../src/pool_network/router.rs | 1 + 16 files changed, 61 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 833e8c71e..c98e8d4e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -541,7 +541,7 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -587,12 +587,12 @@ dependencies = [ "schemars", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] name = "fee_collector" -version = "1.1.7" +version = "1.1.8" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -614,7 +614,7 @@ dependencies = [ "vault", "vault_factory", "whale-lair", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -631,7 +631,7 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -685,7 +685,7 @@ dependencies = [ "serde", "terraswap-pair", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -787,7 +787,7 @@ dependencies = [ "serde", "terraswap-token", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -804,7 +804,7 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1515,7 +1515,7 @@ dependencies = [ [[package]] name = "stableswap-3pool" -version = "1.2.5" +version = "1.2.6" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -1532,7 +1532,7 @@ dependencies = [ "serde", "stable-swap-sim", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1590,7 +1590,7 @@ dependencies = [ [[package]] name = "terraswap-factory" -version = "1.2.4" +version = "1.2.5" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -1602,12 +1602,12 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] name = "terraswap-pair" -version = "1.3.7" +version = "1.3.8" dependencies = [ "anybuf", "cosmwasm-schema", @@ -1623,12 +1623,12 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] name = "terraswap-router" -version = "1.1.2" +version = "1.1.3" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -1640,7 +1640,7 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1657,7 +1657,7 @@ dependencies = [ "schemars", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1733,7 +1733,7 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1754,7 +1754,7 @@ dependencies = [ "serde", "thiserror", "vault", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1776,7 +1776,7 @@ dependencies = [ "thiserror", "vault", "vault_factory", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] @@ -1820,32 +1820,13 @@ dependencies = [ "semver", "serde", "thiserror", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", "white-whale-testing", ] [[package]] name = "white-whale-std" -version = "1.1.5" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cw2", - "cw20", - "osmosis-std-derive", - "prost 0.11.9", - "prost-types", - "protobuf", - "schemars", - "serde", - "uint", -] - -[[package]] -name = "white-whale-std" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3015d5c0c46b6e1cf2fb02c036e8d1481174508a32572d62e6f240a4dec4077" +version = "1.1.8" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -1872,7 +1853,7 @@ dependencies = [ "schemars", "serde", "whale-lair", - "white-whale-std 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "white-whale-std", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4e2cc7ca1..60acd1f63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,8 +39,8 @@ prost = { version = "0.11.9", default-features = false, features = [ ] } prost-types = { version = "0.11.9", default-features = false } # for local development -#white-whale-std = { path = "packages/white-whale-std" } -white-whale-std = { version = "1.1.5" } +white-whale-std = { path = "packages/white-whale-std" } +#white-whale-std = { version = "1.1.5" } white-whale-testing = { path = "./packages/white-whale-testing" } cw-multi-test = { version = "0.16.5" } uint = "0.9.5" diff --git a/contracts/liquidity_hub/fee_collector/Cargo.toml b/contracts/liquidity_hub/fee_collector/Cargo.toml index fec63ae0c..929b389c5 100644 --- a/contracts/liquidity_hub/fee_collector/Cargo.toml +++ b/contracts/liquidity_hub/fee_collector/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fee_collector" -version = "1.1.7" +version = "1.1.8" authors = ["Kerber0x "] edition.workspace = true description = "Contract to collect the fees accrued by the pools and vaults in the liquidity hub" diff --git a/contracts/liquidity_hub/fee_collector/src/commands.rs b/contracts/liquidity_hub/fee_collector/src/commands.rs index 0a1dd632c..945f17001 100644 --- a/contracts/liquidity_hub/fee_collector/src/commands.rs +++ b/contracts/liquidity_hub/fee_collector/src/commands.rs @@ -231,7 +231,7 @@ pub fn aggregate_fees( })?, }))?; - if balance_response.balance > Uint128::zero() { + if balance_response.balance > MINIMUM_AGGREGABLE_BALANCE { // if balance is greater than zero, some swap will occur // Increase the allowance for the cw20 token so the router can perform the swap aggregate_fees_messages.push(CosmosMsg::Wasm(WasmMsg::Execute { @@ -284,6 +284,7 @@ pub fn aggregate_fees( msg: to_json_binary(&router::QueryMsg::SimulateSwapOperations { offer_amount: balance, operations: operations.clone(), + max_spread: Some(Decimal::percent(50u64)), })?, })); @@ -365,7 +366,7 @@ pub fn forward_fees( factory_addr: config.vault_factory.to_string(), factory_type: FactoryType::Vault { start_after: None, - limit: Some(30u32), + limit: Some(60u32), }, }, })?, @@ -384,7 +385,7 @@ pub fn forward_fees( factory_addr: config.pool_factory.to_string(), factory_type: FactoryType::Pool { start_after: None, - limit: Some(30u32), + limit: Some(60u32), }, }, })?, @@ -404,7 +405,7 @@ pub fn forward_fees( factory_addr: config.vault_factory.to_string(), factory_type: FactoryType::Vault { start_after: None, - limit: Some(30u32), + limit: Some(60u32), }, }, })?, @@ -423,7 +424,7 @@ pub fn forward_fees( factory_addr: config.pool_factory.to_string(), factory_type: FactoryType::Pool { start_after: None, - limit: Some(30u32), + limit: Some(60u32), }, }, })?, diff --git a/contracts/liquidity_hub/fee_collector/src/tests/integration.rs b/contracts/liquidity_hub/fee_collector/src/tests/integration.rs index 58f0f0b09..af6170dbe 100644 --- a/contracts/liquidity_hub/fee_collector/src/tests/integration.rs +++ b/contracts/liquidity_hub/fee_collector/src/tests/integration.rs @@ -814,7 +814,7 @@ fn collect_cw20_fees_for_specific_contracts_successfully() { amount: Uint128::new(200_000u128), msg: to_json_binary(&pool_network::pair::Cw20HookMsg::Swap { belief_price: None, - max_spread: Some(Decimal::percent(30u64)), + max_spread: Some(Decimal::percent(50u64)), to: None, }) .unwrap(), @@ -3311,7 +3311,7 @@ fn collect_and_distribute_fees_successfully() { amount: Uint128::new(200_000u128), }, belief_price: None, - max_spread: Some(Decimal::percent(30u64)), + max_spread: Some(Decimal::percent(50u64)), to: None, }, &[Coin { @@ -4737,7 +4737,7 @@ fn collect_and_distribute_fees_with_expiring_epoch_successfully() { amount: Uint128::new(200_000u128), }, belief_price: None, - max_spread: Some(Decimal::percent(30u64)), + max_spread: Some(Decimal::percent(50u64)), to: None, }, &[Coin { diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/Cargo.toml b/contracts/liquidity_hub/pool-network/stableswap_3pool/Cargo.toml index 158aebf54..d58005836 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/Cargo.toml +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stableswap-3pool" -version = "1.2.5" +version = "1.2.6" authors = ["Adam J. Weigold "] edition.workspace = true license.workspace = true diff --git a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs index f4218fcea..133d245fd 100644 --- a/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs +++ b/contracts/liquidity_hub/pool-network/stableswap_3pool/src/commands.rs @@ -455,17 +455,12 @@ pub fn swap( amount: swap_computation.return_amount, }; - let fees = swap_computation - .swap_fee_amount - .checked_add(swap_computation.protocol_fee_amount)? - .checked_add(swap_computation.burn_fee_amount)?; - // check max spread limit if exist swap::assert_max_spread( belief_price, max_spread, offer_asset.amount, - return_asset.amount.checked_add(fees)?, + return_asset.amount, swap_computation.spread_amount, )?; diff --git a/contracts/liquidity_hub/pool-network/terraswap_factory/Cargo.toml b/contracts/liquidity_hub/pool-network/terraswap_factory/Cargo.toml index cfca4f570..acdad661d 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_factory/Cargo.toml +++ b/contracts/liquidity_hub/pool-network/terraswap_factory/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "terraswap-factory" -version = "1.2.4" +version = "1.2.5" authors = [ "Terraform Labs, PTE.", "DELIGHT LABS", diff --git a/contracts/liquidity_hub/pool-network/terraswap_factory/src/state.rs b/contracts/liquidity_hub/pool-network/terraswap_factory/src/state.rs index e7872f46b..bc3f2eeeb 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_factory/src/state.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_factory/src/state.rs @@ -57,7 +57,7 @@ pub fn trio_key(asset_infos: &[AssetInfoRaw; 3]) -> Vec { } // settings for pagination -const MAX_LIMIT: u32 = 30; +const MAX_LIMIT: u32 = 60; const DEFAULT_LIMIT: u32 = 10; pub fn read_pairs( storage: &dyn Storage, diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/Cargo.toml b/contracts/liquidity_hub/pool-network/terraswap_pair/Cargo.toml index c44756d82..956189f5a 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/Cargo.toml +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "terraswap-pair" -version = "1.3.7" +version = "1.3.8" authors = [ "Terraform Labs, PTE.", "DELIGHT LABS", diff --git a/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs b/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs index 9e922544b..841d8ea37 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_pair/src/commands.rs @@ -411,29 +411,12 @@ pub fn swap( amount: swap_computation.return_amount, }; - let fees = { - let base_fees = swap_computation - .swap_fee_amount - .checked_add(swap_computation.protocol_fee_amount)? - .checked_add(swap_computation.burn_fee_amount)?; - - #[cfg(feature = "osmosis")] - { - base_fees.checked_add(swap_computation.osmosis_fee_amount)? - } - - #[cfg(not(feature = "osmosis"))] - { - base_fees - } - }; - // check max spread limit if exist swap::assert_max_spread( belief_price, max_spread, offer_asset.amount, - return_asset.amount.checked_add(fees)?, + return_asset.amount, swap_computation.spread_amount, )?; diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/Cargo.toml b/contracts/liquidity_hub/pool-network/terraswap_router/Cargo.toml index 2226b7229..ef8fb3c1b 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/Cargo.toml +++ b/contracts/liquidity_hub/pool-network/terraswap_router/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "terraswap-router" -version = "1.1.2" +version = "1.1.3" authors = [ "Terraform Labs, PTE.", "DELIGHT LABS", diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/src/contract.rs b/contracts/liquidity_hub/pool-network/terraswap_router/src/contract.rs index dff627d18..4e7b2c032 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/src/contract.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_router/src/contract.rs @@ -17,6 +17,7 @@ use white_whale_std::pool_network::router::{ ConfigResponse, Cw20HookMsg, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, SimulateSwapOperationsResponse, SwapOperation, SwapRoute, SwapRouteResponse, }; +use white_whale_std::pool_network::swap; use crate::error::ContractError; use crate::error::ContractError::MigrateInvalidVersion; @@ -248,6 +249,7 @@ fn add_swap_routes( deps.as_ref(), Uint128::one(), swap_route.clone().swap_operations, + None, ) .map_err(|_| ContractError::InvalidSwapRoute(swap_route.clone()))?; @@ -299,10 +301,12 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result Ok(to_json_binary(&simulate_swap_operations( deps, offer_amount, operations, + max_spread, )?)?), QueryMsg::ReverseSimulateSwapOperations { ask_amount, @@ -338,6 +342,7 @@ fn simulate_swap_operations( deps: Deps, offer_amount: Uint128, operations: Vec, + max_spread: Option, ) -> Result { let config: Config = CONFIG.load(deps.storage)?; let terraswap_factory = deps.api.addr_humanize(&config.terraswap_factory)?; @@ -369,6 +374,16 @@ fn simulate_swap_operations( }, )?; + if max_spread.is_some() && res.return_amount > Uint128::zero() { + swap::assert_max_spread( + None, + max_spread, + offer_amount, + res.return_amount, + res.spread_amount, + )?; + } + offer_amount = res.return_amount; } } diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs b/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs index 46e8a6bf2..ea440917b 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs +++ b/contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs @@ -469,6 +469,7 @@ fn query_buy_with_routes() { let empty_operations_msg = QueryMsg::SimulateSwapOperations { offer_amount: Uint128::from(1000000u128), operations: vec![], + max_spread: None, }; let error = query(deps.as_ref(), mock_env(), empty_operations_msg).unwrap_err(); @@ -498,6 +499,7 @@ fn query_buy_with_routes() { }, }, ], + max_spread: None, }; deps.querier.with_pool_factory( diff --git a/packages/white-whale-std/Cargo.toml b/packages/white-whale-std/Cargo.toml index f716910a3..24b11307c 100644 --- a/packages/white-whale-std/Cargo.toml +++ b/packages/white-whale-std/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "white-whale-std" -version = "1.1.5" +version = "1.1.8" edition.workspace = true authors = [ "Kerber0x ", diff --git a/packages/white-whale-std/src/pool_network/router.rs b/packages/white-whale-std/src/pool_network/router.rs index d71651ee8..71882d47e 100644 --- a/packages/white-whale-std/src/pool_network/router.rs +++ b/packages/white-whale-std/src/pool_network/router.rs @@ -121,6 +121,7 @@ pub enum QueryMsg { SimulateSwapOperations { offer_amount: Uint128, operations: Vec, + max_spread: Option, }, /// Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset /// is needed to perform the swap. From 36a8cf3a951e7b4d66d1230b040efb36fca33317 Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Wed, 4 Sep 2024 17:41:49 +0100 Subject: [PATCH 2/3] chore: remove v2 contracts from v1 branch --- .../liquidity_hub/epoch-manager/.cargo/config | 4 - .../liquidity_hub/epoch-manager/.editorconfig | 11 -- .../liquidity_hub/epoch-manager/.gitignore | 16 --- .../liquidity_hub/epoch-manager/Cargo.toml | 38 ------- .../liquidity_hub/epoch-manager/README.md | 99 ----------------- .../epoch-manager/src/bin/schema.rs | 11 -- .../epoch-manager/src/commands.rs | 98 ----------------- .../epoch-manager/src/contract.rs | 104 ------------------ .../liquidity_hub/epoch-manager/src/error.rs | 37 ------- .../liquidity_hub/epoch-manager/src/lib.rs | 7 -- .../epoch-manager/src/queries.rs | 42 ------- .../liquidity_hub/epoch-manager/src/state.rs | 8 -- 12 files changed, 475 deletions(-) delete mode 100644 contracts/liquidity_hub/epoch-manager/.cargo/config delete mode 100644 contracts/liquidity_hub/epoch-manager/.editorconfig delete mode 100644 contracts/liquidity_hub/epoch-manager/.gitignore delete mode 100644 contracts/liquidity_hub/epoch-manager/Cargo.toml delete mode 100644 contracts/liquidity_hub/epoch-manager/README.md delete mode 100644 contracts/liquidity_hub/epoch-manager/src/bin/schema.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/commands.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/contract.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/error.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/lib.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/queries.rs delete mode 100644 contracts/liquidity_hub/epoch-manager/src/state.rs diff --git a/contracts/liquidity_hub/epoch-manager/.cargo/config b/contracts/liquidity_hub/epoch-manager/.cargo/config deleted file mode 100644 index af5698e58..000000000 --- a/contracts/liquidity_hub/epoch-manager/.cargo/config +++ /dev/null @@ -1,4 +0,0 @@ -[alias] -wasm = "build --release --lib --target wasm32-unknown-unknown" -unit-test = "test --lib" -schema = "run --bin schema" diff --git a/contracts/liquidity_hub/epoch-manager/.editorconfig b/contracts/liquidity_hub/epoch-manager/.editorconfig deleted file mode 100644 index 3d36f20b1..000000000 --- a/contracts/liquidity_hub/epoch-manager/.editorconfig +++ /dev/null @@ -1,11 +0,0 @@ -root = true - -[*] -indent_style = space -indent_size = 2 -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.rs] -indent_size = 4 diff --git a/contracts/liquidity_hub/epoch-manager/.gitignore b/contracts/liquidity_hub/epoch-manager/.gitignore deleted file mode 100644 index 9095deaa4..000000000 --- a/contracts/liquidity_hub/epoch-manager/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -# Build results -/target -/schema - -# Cargo+Git helper file (https://github.com/rust-lang/cargo/blob/0.44.1/src/cargo/sources/git/utils.rs#L320-L327) -.cargo-ok - -# Text file backups -**/*.rs.bk - -# macOS -.DS_Store - -# IDEs -*.iml -.idea diff --git a/contracts/liquidity_hub/epoch-manager/Cargo.toml b/contracts/liquidity_hub/epoch-manager/Cargo.toml deleted file mode 100644 index 0a963cbe3..000000000 --- a/contracts/liquidity_hub/epoch-manager/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -[package] -name = "epoch-manager" -version = "0.1.0" -authors = ["Kerber0x "] -edition = "2021" - -exclude = [ - # Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication. - "contract.wasm", - "hash.txt", -] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -crate-type = ["cdylib", "rlib"] - -[features] -osmosis_token_factory = ["white-whale-std/osmosis_token_factory"] -injective = ["white-whale-std/injective"] -osmosis = ["osmosis_token_factory"] -# for more explicit tests, cargo test --features=backtraces -backtraces = ["cosmwasm-std/backtraces"] - -[dependencies] -cosmwasm-schema.workspace = true -cosmwasm-std.workspace = true -cw-storage-plus.workspace = true -cw2.workspace = true -schemars.workspace = true -serde.workspace = true -semver.workspace = true -thiserror.workspace = true -white-whale-std.workspace = true -cw-controllers.workspace = true - -[dev-dependencies] -cw-multi-test.workspace = true diff --git a/contracts/liquidity_hub/epoch-manager/README.md b/contracts/liquidity_hub/epoch-manager/README.md deleted file mode 100644 index 054ea4814..000000000 --- a/contracts/liquidity_hub/epoch-manager/README.md +++ /dev/null @@ -1,99 +0,0 @@ -# CosmWasm Starter Pack - -This is a template to build smart contracts in Rust to run inside a -[Cosmos SDK](https://github.com/cosmos/cosmos-sdk) module on all chains that enable it. -To understand the framework better, please read the overview in the -[cosmwasm repo](https://github.com/CosmWasm/cosmwasm/blob/master/README.md), -and dig into the [cosmwasm docs](https://www.cosmwasm.com). -This assumes you understand the theory and just want to get coding. - -## Creating a new repo from template - -Assuming you have a recent version of Rust and Cargo installed -(via [rustup](https://rustup.rs/)), -then the following should get you a new repo to start a contract: - -Install [cargo-generate](https://github.com/ashleygwilliams/cargo-generate) and cargo-run-script. -Unless you did that before, run this line now: - -```sh -cargo install cargo-generate --features vendored-openssl -cargo install cargo-run-script -``` - -Now, use it to create your new contract. -Go to the folder in which you want to place it and run: - -**Latest** - -```sh -cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME -``` - -For cloning minimal code repo: - -```sh -cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME -d minimal=true -``` - -**Older Version** - -Pass version as branch flag: - -```sh -cargo generate --git https://github.com/CosmWasm/cw-template.git --branch --name PROJECT_NAME -``` - -Example: - -```sh -cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 0.16 --name PROJECT_NAME -``` - -You will now have a new folder called `PROJECT_NAME` (I hope you changed that to something else) -containing a simple working contract and build system that you can customize. - -## Create a Repo - -After generating, you have a initialized local git repo, but no commits, and no remote. -Go to a server (eg. github) and create a new upstream repo (called `YOUR-GIT-URL` below). -Then run the following: - -```sh -# this is needed to create a valid Cargo.lock file (see below) -cargo check -git branch -M main -git add . -git commit -m 'Initial Commit' -git remote add origin YOUR-GIT-URL -git push -u origin main -``` - -## CI Support - -We have template configurations for both [GitHub Actions](.github/workflows/Basic.yml) -and [Circle CI](.circleci/config.yml) in the generated project, so you can -get up and running with CI right away. - -One note is that the CI runs all `cargo` commands -with `--locked` to ensure it uses the exact same versions as you have locally. This also means -you must have an up-to-date `Cargo.lock` file, which is not auto-generated. -The first time you set up the project (or after adding any dep), you should ensure the -`Cargo.lock` file is updated, so the CI will test properly. This can be done simply by -running `cargo check` or `cargo unit-test`. - -## Using your project - -Once you have your custom repo, you should check out [Developing](./Developing.md) to explain -more on how to run tests and develop code. Or go through the -[online tutorial](https://docs.cosmwasm.com/) to get a better feel -of how to develop. - -[Publishing](./Publishing.md) contains useful information on how to publish your contract -to the world, once you are ready to deploy it on a running blockchain. And -[Importing](./Importing.md) contains information about pulling in other contracts or crates -that have been published. - -Please replace this README file with information about your specific project. You can keep -the `Developing.md` and `Publishing.md` files as useful referenced, but please set some -proper description in the README. diff --git a/contracts/liquidity_hub/epoch-manager/src/bin/schema.rs b/contracts/liquidity_hub/epoch-manager/src/bin/schema.rs deleted file mode 100644 index 7b0b4383e..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/bin/schema.rs +++ /dev/null @@ -1,11 +0,0 @@ -use cosmwasm_schema::write_api; - -use white_whale_std::epoch_manager::epoch_manager::{ExecuteMsg, InstantiateMsg, QueryMsg}; - -fn main() { - write_api! { - instantiate: InstantiateMsg, - execute: ExecuteMsg, - query: QueryMsg, - } -} diff --git a/contracts/liquidity_hub/epoch-manager/src/commands.rs b/contracts/liquidity_hub/epoch-manager/src/commands.rs deleted file mode 100644 index 321f67513..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/commands.rs +++ /dev/null @@ -1,98 +0,0 @@ -use cosmwasm_std::{Api, DepsMut, Env, MessageInfo, Response, SubMsg}; - -use white_whale_std::epoch_manager::epoch_manager::EpochConfig; -use white_whale_std::epoch_manager::hooks::EpochChangedHookMsg; - -use crate::state::{ADMIN, CONFIG, EPOCH, HOOKS}; -use crate::ContractError; - -/// Adds a new hook to the contract. -pub fn add_hook( - deps: DepsMut, - info: MessageInfo, - api: &dyn Api, - contract_addr: &str, -) -> Result { - Ok(HOOKS.execute_add_hook(&ADMIN, deps, info, api.addr_validate(contract_addr)?)?) -} - -pub(crate) fn remove_hook( - deps: DepsMut, - info: MessageInfo, - api: &dyn Api, - contract_addr: &str, -) -> Result { - Ok(HOOKS.execute_remove_hook(&ADMIN, deps, info, api.addr_validate(contract_addr)?)?) -} - -/// Creates a new epoch. -pub fn create_epoch(deps: DepsMut, env: Env) -> Result { - let mut current_epoch = EPOCH.load(deps.storage)?; - let config = CONFIG.load(deps.storage)?; - - if env - .block - .time - .minus_nanos(current_epoch.start_time.nanos()) - .nanos() - < config.epoch_config.duration.u64() - { - return Err(ContractError::CurrentEpochNotExpired); - } - - current_epoch.id = current_epoch - .id - .checked_add(1u64) - .ok_or(ContractError::EpochOverflow)?; - current_epoch.start_time = current_epoch - .start_time - .plus_nanos(config.epoch_config.duration.u64()); - - EPOCH.save(deps.storage, ¤t_epoch)?; - - let messages = HOOKS.prepare_hooks(deps.storage, |hook| { - EpochChangedHookMsg { - current_epoch: current_epoch.clone(), - } - .into_cosmos_msg(hook) - .map(SubMsg::new) - })?; - - Ok(Response::default() - .add_submessages(messages) - .add_attributes(vec![ - ("action", "create_epoch".to_string()), - ("current_epoch", current_epoch.to_string()), - ])) -} - -/// Updates the config of the contract. -pub fn update_config( - mut deps: DepsMut, - info: &MessageInfo, - owner: Option, - epoch_config: Option, -) -> Result { - ADMIN.assert_admin(deps.as_ref(), &info.sender)?; - - if let Some(owner) = owner.clone() { - let new_admin = deps.api.addr_validate(owner.as_str())?; - ADMIN.set(deps.branch(), Some(new_admin))?; - } - - let mut config = CONFIG.load(deps.storage)?; - - if let Some(epoch_config) = epoch_config.clone() { - config.epoch_config = epoch_config; - CONFIG.save(deps.storage, &config)?; - } - - Ok(Response::default().add_attributes(vec![ - ("action", "update_config".to_string()), - ("owner", owner.unwrap_or_else(|| info.sender.to_string())), - ( - "epoch_config", - epoch_config.unwrap_or(config.epoch_config).to_string(), - ), - ])) -} diff --git a/contracts/liquidity_hub/epoch-manager/src/contract.rs b/contracts/liquidity_hub/epoch-manager/src/contract.rs deleted file mode 100644 index f3e3679e0..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/contract.rs +++ /dev/null @@ -1,104 +0,0 @@ -use cosmwasm_std::{entry_point, to_json_binary, StdError}; -use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; -use cw2::{get_contract_version, set_contract_version}; -use semver::Version; - -use white_whale_std::epoch_manager::epoch_manager::{ - Config, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, -}; -use white_whale_std::migrate_guards::check_contract_name; - -use crate::error::ContractError; -use crate::state::{ADMIN, CONFIG, EPOCH}; -use crate::{commands, queries}; - -// version info for migration info -const CONTRACT_NAME: &str = "white_whale-epoch-manager"; -const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); - -#[entry_point] -pub fn instantiate( - mut deps: DepsMut, - env: Env, - info: MessageInfo, - msg: InstantiateMsg, -) -> Result { - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; - - // validate start_time for the initial epoch - if msg.start_epoch.start_time < env.block.time { - return Err(ContractError::Std(StdError::generic_err( - "start_time must be in the future", - ))); - } - - if msg.epoch_config.genesis_epoch.u64() != msg.start_epoch.start_time.nanos() { - return Err(ContractError::Std(StdError::generic_err( - "genesis_epoch must be equal to start_epoch.start_time", - ))); - } - - ADMIN.set(deps.branch(), Some(info.sender))?; - EPOCH.save(deps.storage, &msg.start_epoch)?; - CONFIG.save( - deps.storage, - &Config { - epoch_config: msg.epoch_config.clone(), - }, - )?; - Ok(Response::default().add_attributes(vec![ - ("action", "instantiate".to_string()), - ("start_epoch", msg.start_epoch.to_string()), - ("epoch_config", msg.epoch_config.to_string()), - ])) -} - -#[entry_point] -pub fn execute( - deps: DepsMut, - env: Env, - info: MessageInfo, - msg: ExecuteMsg, -) -> Result { - let api = deps.api; - match msg { - ExecuteMsg::AddHook { contract_addr } => { - commands::add_hook(deps, info, api, &contract_addr) - } - ExecuteMsg::RemoveHook { contract_addr } => { - commands::remove_hook(deps, info, api, &contract_addr) - } - ExecuteMsg::CreateEpoch {} => commands::create_epoch(deps, env), - ExecuteMsg::UpdateConfig { - owner, - epoch_config, - } => commands::update_config(deps, &info, owner, epoch_config), - } -} - -#[entry_point] -pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { - match msg { - QueryMsg::Config {} => Ok(to_json_binary(&queries::query_config(deps)?)?), - QueryMsg::CurrentEpoch {} => Ok(to_json_binary(&queries::query_current_epoch(deps)?)?), - QueryMsg::Epoch { id } => Ok(to_json_binary(&queries::query_epoch(deps, id)?)?), - } -} - -#[entry_point] -pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { - check_contract_name(deps.storage, CONTRACT_NAME.to_string())?; - let version: Version = CONTRACT_VERSION.parse()?; - let storage_version: Version = get_contract_version(deps.storage)?.version.parse()?; - - if storage_version >= version { - return Err(ContractError::MigrateInvalidVersion { - current_version: storage_version, - new_version: version, - }); - } - - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; - - Ok(Response::default()) -} diff --git a/contracts/liquidity_hub/epoch-manager/src/error.rs b/contracts/liquidity_hub/epoch-manager/src/error.rs deleted file mode 100644 index 7a2bd5f1c..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/error.rs +++ /dev/null @@ -1,37 +0,0 @@ -use cosmwasm_std::StdError; -use cw_controllers::{AdminError, HookError}; -use semver::Version; -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum ContractError { - #[error("{0}")] - Std(#[from] StdError), - - #[error("{0}")] - AdminError(#[from] AdminError), - - #[error("{0}")] - HookError(#[from] HookError), - - #[error("The epoch id has overflowed.")] - EpochOverflow, - - #[error("Semver parsing error: {0}")] - SemVer(String), - - #[error("Attempt to migrate to version {new_version}, but contract is on a higher version {current_version}")] - MigrateInvalidVersion { - new_version: Version, - current_version: Version, - }, - - #[error("The current epoch epoch has not expired yet.")] - CurrentEpochNotExpired, -} - -impl From for ContractError { - fn from(err: semver::Error) -> Self { - Self::SemVer(err.to_string()) - } -} diff --git a/contracts/liquidity_hub/epoch-manager/src/lib.rs b/contracts/liquidity_hub/epoch-manager/src/lib.rs deleted file mode 100644 index 95db3d1bf..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub use crate::error::ContractError; - -pub mod commands; -pub mod contract; -mod error; -pub mod queries; -pub mod state; diff --git a/contracts/liquidity_hub/epoch-manager/src/queries.rs b/contracts/liquidity_hub/epoch-manager/src/queries.rs deleted file mode 100644 index 75775a591..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/queries.rs +++ /dev/null @@ -1,42 +0,0 @@ -use cosmwasm_std::{Addr, Deps, StdResult}; - -use white_whale_std::epoch_manager::epoch_manager::{ConfigResponse, EpochResponse, EpochV2}; - -use crate::state::{ADMIN, CONFIG, EPOCH}; - -/// Queries the config. Returns a [ConfigResponse]. -pub(crate) fn query_config(deps: Deps) -> StdResult { - let admin = ADMIN.get(deps)?.unwrap_or(Addr::unchecked("")); - let config = CONFIG.load(deps.storage)?; - - Ok(ConfigResponse { - owner: admin, - epoch_config: config.epoch_config, - }) -} - -/// Queries the current epoch. Returns an [EpochResponse]. -pub(crate) fn query_current_epoch(deps: Deps) -> StdResult { - EPOCH - .load(deps.storage) - .map(|epoch| epoch.to_epoch_response()) -} - -/// Queries the current epoch. Returns an [EpochResponse]. -pub(crate) fn query_epoch(deps: Deps, id: u64) -> StdResult { - let current_epoch = EPOCH.load(deps.storage)?; - - if current_epoch.id == id { - Ok(current_epoch.to_epoch_response()) - } else { - let epoch_difference = current_epoch.id.saturating_sub(id); - - let epoch = EpochV2 { - id, - start_time: current_epoch.start_time.minus_nanos( - CONFIG.load(deps.storage)?.epoch_config.duration.u64() * epoch_difference, - ), - }; - Ok(epoch.to_epoch_response()) - } -} diff --git a/contracts/liquidity_hub/epoch-manager/src/state.rs b/contracts/liquidity_hub/epoch-manager/src/state.rs deleted file mode 100644 index 1e0c424bf..000000000 --- a/contracts/liquidity_hub/epoch-manager/src/state.rs +++ /dev/null @@ -1,8 +0,0 @@ -use cw_controllers::{Admin, Hooks}; -use cw_storage_plus::Item; -use white_whale_std::epoch_manager::epoch_manager::{Config, EpochV2}; - -pub const CONFIG: Item = Item::new("config"); -pub const ADMIN: Admin = Admin::new("admin"); -pub const HOOKS: Hooks = Hooks::new("hooks"); -pub const EPOCH: Item = Item::new("epoch"); From 3eb627da7086c7cfdaf6dfd67f29aad77d63586a Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Wed, 4 Sep 2024 17:42:22 +0100 Subject: [PATCH 3/3] chore: regenerate schemas --- Cargo.lock | 32 ------------------- Cargo.toml | 1 - .../terraswap_router/schema/raw/query.json | 14 ++++++++ .../schema/terraswap-router.json | 14 ++++++++ 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c98e8d4e6..5cc8ad24a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,21 +266,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "cw-controllers" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d8edce4b78785f36413f67387e4be7d0cb7d032b5d4164bcc024f9c3f3f2ea" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cw-storage-plus", - "cw-utils", - "schemars", - "serde", - "thiserror", -] - [[package]] name = "cw-multi-test" version = "0.16.5" @@ -527,23 +512,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "epoch-manager" -version = "0.1.0" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cw-controllers", - "cw-multi-test", - "cw-storage-plus", - "cw2", - "schemars", - "semver", - "serde", - "thiserror", - "white-whale-std", -] - [[package]] name = "errno" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 60acd1f63..9bc73e2a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "contracts/liquidity_hub/fee-distributor-mock", "contracts/liquidity_hub/whale_lair", "contracts/liquidity_hub/vault-network/*", - "contracts/liquidity_hub/epoch-manager", ] [workspace.package] diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/schema/raw/query.json b/contracts/liquidity_hub/pool-network/terraswap_router/schema/raw/query.json index 42060dccf..525d1f1f2 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/schema/raw/query.json +++ b/contracts/liquidity_hub/pool-network/terraswap_router/schema/raw/query.json @@ -30,6 +30,16 @@ "operations" ], "properties": { + "max_spread": { + "anyOf": [ + { + "$ref": "#/definitions/Decimal" + }, + { + "type": "null" + } + ] + }, "offer_amount": { "$ref": "#/definitions/Uint128" }, @@ -163,6 +173,10 @@ } ] }, + "Decimal": { + "description": "A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0\n\nThe greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)", + "type": "string" + }, "SwapOperation": { "oneOf": [ { diff --git a/contracts/liquidity_hub/pool-network/terraswap_router/schema/terraswap-router.json b/contracts/liquidity_hub/pool-network/terraswap_router/schema/terraswap-router.json index e48336853..d00284087 100644 --- a/contracts/liquidity_hub/pool-network/terraswap_router/schema/terraswap-router.json +++ b/contracts/liquidity_hub/pool-network/terraswap_router/schema/terraswap-router.json @@ -373,6 +373,16 @@ "operations" ], "properties": { + "max_spread": { + "anyOf": [ + { + "$ref": "#/definitions/Decimal" + }, + { + "type": "null" + } + ] + }, "offer_amount": { "$ref": "#/definitions/Uint128" }, @@ -506,6 +516,10 @@ } ] }, + "Decimal": { + "description": "A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0\n\nThe greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)", + "type": "string" + }, "SwapOperation": { "oneOf": [ {