From e6cbd9042623d9fa9c904fdcc47fb6688116e297 Mon Sep 17 00:00:00 2001 From: JerryIdoko Date: Sun, 26 Apr 2026 09:56:53 +0100 Subject: [PATCH] feat: implement pool counter and V1 deprecation fail-safe (#70, #96) --- contracts/amm_factory/src/lib.rs | 13 +++++++++++++ contracts/amm_pool/src/lib.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/contracts/amm_factory/src/lib.rs b/contracts/amm_factory/src/lib.rs index 21adea0..1923ebb 100644 --- a/contracts/amm_factory/src/lib.rs +++ b/contracts/amm_factory/src/lib.rs @@ -20,6 +20,7 @@ pub enum DataKey { Pools, // Map of (TokenA, TokenB) -> Pool PoolWasmHash, // The Wasm hash of the Pool contract to deploy Admin, // The address of the factory admin + TotalPools, // The total number of pools deployed } #[contract] @@ -44,6 +45,7 @@ impl FactoryContract { env.storage().instance().set(&DataKey::FeeTo, &fee_to); env.storage().instance().set(&DataKey::Admin, &admin); env.storage().instance().set(&DataKey::PoolWasmHash, &pool_wasm_hash); + env.storage().instance().set(&DataKey::TotalPools, &0u32); } /// Helper function to sort two token addresses, creating a canonical pair @@ -151,6 +153,11 @@ impl FactoryContract { pools.set((token_0, token_1), pool); env.storage().instance().set(&DataKey::Pools, &pools); + // Increment total pools counter for UI metrics and pagination logic + let mut total_pools: u32 = env.storage().instance().get(&DataKey::TotalPools).unwrap_or(0); + total_pools += 1; + env.storage().instance().set(&DataKey::TotalPools, &total_pools); + // Emit PoolCreated event env.events().publish( (symbol_short!("PoolCreated"), token_a, token_b), @@ -212,4 +219,10 @@ impl FactoryContract { pool.paused ); } + + /// Returns the total number of pools created by the factory. + /// This is used for UI metrics and pagination logic. + pub fn get_all_pools_length(env: Env) -> u32 { + env.storage().instance().get(&DataKey::TotalPools).unwrap_or(0) + } } \ No newline at end of file diff --git a/contracts/amm_pool/src/lib.rs b/contracts/amm_pool/src/lib.rs index 7f9ead4..f31f7a8 100644 --- a/contracts/amm_pool/src/lib.rs +++ b/contracts/amm_pool/src/lib.rs @@ -98,6 +98,9 @@ impl AmmPool { user.require_auth(); Self::require_not_frozen(&env, &user); let mut state: PoolState = env.storage().instance().get(&DataKey::State).expect("Not initialized"); + if state.is_deprecated { + panic!("Pool is deprecated"); + } if state.deposits_paused { panic!("deposits are paused"); } @@ -170,6 +173,22 @@ impl AmmPool { env.storage().instance().set(&DataKey::State, &state); } + /// Admin-only: Permanently deprecate the pool. + /// This is an irreversible one-way toggle. + /// Swaps and new liquidity provision are disabled, but withdrawals remain active. + pub fn set_deprecated(env: Env) { + Self::require_admin(&env); + let mut state: PoolState = env.storage().instance().get(&DataKey::State).expect("Not initialized"); + state.is_deprecated = true; + env.storage().instance().set(&DataKey::State, &state); + + // Emit event for transparency + env.events().publish( + (symbol_short!("Admin"), symbol_short!("Deprecat")), + env.current_contract_address() + ); + } + /// Verify that `user` holds at least `required_amount` of `token` and has granted /// at least that much allowance to this contract. Panics early with a descriptive /// message if either check fails. No-ops when `required_amount <= 0`. @@ -370,6 +389,9 @@ impl AmmPool { pub fn swap(env: Env, user: Address, amount_in: i128, is_a_in: bool) -> i128 { Self::require_not_frozen(&env, &user); let state: PoolState = env.storage().instance().get(&DataKey::State).expect("Not initialized"); + if state.is_deprecated { + panic!("Pool is deprecated"); + } if state.deposits_paused { panic!("deposits are paused"); }