Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions contracts/amm_factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
}
22 changes: 22 additions & 0 deletions contracts/amm_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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");
}
Expand Down
Loading