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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pallets/stableswap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-stableswap"
version = "5.3.0"
version = "5.4.0"
description = "AMM for correlated assets"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
31 changes: 31 additions & 0 deletions pallets/stableswap/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,37 @@ benchmarks! {
assert_eq!(pool.final_block, 1000u32.into());
}

update_asset_peg_source{
let lp_provider: T::AccountId = account("provider", 0, 1);
let (pool_id, pool) = setup_pool_with_initial_liquidity::<T>(&lp_provider);
let successful_origin = T::AuthorityOrigin::try_successful_origin().unwrap();

// Get first asset from pool
let asset_id = *pool.assets.first().unwrap();
let new_peg_source = PegSource::Value((2, 3));

// Register the new peg source for benchmarking
T::BenchmarkHelper::register_asset_peg((asset_id, asset_id), (2u128, 3u128), *b"benchmar")?;

}: _<T::RuntimeOrigin>(successful_origin, pool_id, asset_id, new_peg_source.clone())
verify {
let peg_info = crate::PoolPegs::<T>::get(pool_id).unwrap();
assert_eq!(peg_info.source[0], new_peg_source);
}

update_pool_max_peg_update{
let lp_provider: T::AccountId = account("provider", 0, 1);
let (pool_id, _pool) = setup_pool_with_initial_liquidity::<T>(&lp_provider);
let successful_origin = T::AuthorityOrigin::try_successful_origin().unwrap();

let new_max_peg_update = Permill::from_percent(50);

}: _<T::RuntimeOrigin>(successful_origin, pool_id, new_max_peg_update)
verify {
let peg_info = crate::PoolPegs::<T>::get(pool_id).unwrap();
assert_eq!(peg_info.max_peg_update, new_max_peg_update);
}

router_execution_sell{
let c in 1..2;
let e in 0..1; // if e == 1, execute_sell is executed
Expand Down
105 changes: 105 additions & 0 deletions pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ pub mod pallet {
},
/// A pool has been destroyed.
PoolDestroyed { pool_id: T::AssetId },
/// Pool peg source has been updated.
PoolPegSourceUpdated {
pool_id: T::AssetId,
asset_id: T::AssetId,
peg_source: PegSource<T::AssetId>,
},
/// Pool max peg update has been updated.
PoolMaxPegUpdateUpdated {
pool_id: T::AssetId,
max_peg_update: Permill,
},
}

#[pallet::error]
Expand Down Expand Up @@ -366,6 +377,9 @@ pub mod pallet {

/// Creating pool with pegs is not allowed for asset with different decimals.
IncorrectAssetDecimals,

/// Pool does not have pegs configured.
NoPegSource,
}

#[pallet::call]
Expand Down Expand Up @@ -1247,6 +1261,97 @@ pub mod pallet {

Ok(())
}

/// Update the peg source for a specific asset in a pool.
///
/// This function allows updating the peg source for an asset within a pool.
/// The pool must exist and have pegs configured. The asset must be part of the pool.
/// The current price is always preserved when updating the peg source.
///
/// Parameters:
/// - `origin`: Must be `T::AuthorityOrigin`.
/// - `pool_id`: The ID of the pool containing the asset.
/// - `asset_id`: The ID of the asset whose peg source is to be updated.
/// - `peg_source`: The new peg source for the asset.
///
/// Emits `PoolPegSourceUpdated` event when successful.
///
/// # Errors
/// - `PoolNotFound`: If the specified pool does not exist.
/// - `NoPegSource`: If the pool does not have pegs configured.
/// - `AssetNotInPool`: If the specified asset is not part of the pool.
///
#[pallet::call_index(13)]
#[pallet::weight(<T as Config>::WeightInfo::update_asset_peg_source())]
#[transactional]
pub fn update_asset_peg_source(
origin: OriginFor<T>,
pool_id: T::AssetId,
asset_id: T::AssetId,
peg_source: PegSource<T::AssetId>,
) -> DispatchResult {
T::AuthorityOrigin::ensure_origin(origin)?;

let pool = Pools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let asset_index = pool.find_asset(asset_id).ok_or(Error::<T>::AssetNotInPool)?;

PoolPegs::<T>::try_mutate(pool_id, |maybe_peg_info| -> DispatchResult {
let peg_info = maybe_peg_info.as_mut().ok_or(Error::<T>::NoPegSource)?;

ensure!(peg_info.current.len() == pool.assets.len(), Error::<T>::IncorrectAssets);
peg_info.source[asset_index] = peg_source.clone();

Self::deposit_event(Event::PoolPegSourceUpdated {
pool_id,
asset_id,
peg_source,
});

Ok(())
})
}

/// Update the maximum peg update percentage for a pool.
///
/// This function allows updating the maximum percentage by which peg values
/// can change in a pool with pegs configured.
///
/// Parameters:
/// - `origin`: Must be `T::AuthorityOrigin`.
/// - `pool_id`: The ID of the pool to update.
/// - `max_peg_update`: The new maximum peg update percentage.
///
/// Emits `PoolMaxPegUpdateUpdated` event when successful.
///
/// # Errors
/// - `PoolNotFound`: If the specified pool does not exist.
/// - `NoPegSource`: If the pool does not have pegs configured.
///
#[pallet::call_index(14)]
#[pallet::weight(<T as Config>::WeightInfo::update_pool_max_peg_update())]
#[transactional]
pub fn update_pool_max_peg_update(
origin: OriginFor<T>,
pool_id: T::AssetId,
max_peg_update: Permill,
) -> DispatchResult {
T::AuthorityOrigin::ensure_origin(origin)?;

let _pool = Pools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;

PoolPegs::<T>::try_mutate(pool_id, |maybe_peg_info| -> DispatchResult {
let peg_info = maybe_peg_info.as_mut().ok_or(Error::<T>::NoPegSource)?;

peg_info.max_peg_update = max_peg_update;

Self::deposit_event(Event::PoolMaxPegUpdateUpdated {
pool_id,
max_peg_update,
});

Ok(())
})
}
}

#[pallet::hooks]
Expand Down
2 changes: 2 additions & 0 deletions pallets/stableswap/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod peg_one;
mod price;
mod remove_liquidity;
mod trades;
mod update_max_peg_update;
mod update_peg_source;
mod update_pool;

type Balance = u128;
Expand Down
Loading