Skip to content

Commit f18844d

Browse files
committed
Merge remote-tracking branch 'origin/master' into fix/unlock-leftover-reserves
2 parents f0c12ae + 1e9b15a commit f18844d

9 files changed

Lines changed: 870 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/stableswap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pallet-stableswap"
3-
version = "5.3.2"
3+
version = "5.4.0"
44
description = "AMM for correlated assets"
55
authors = ["GalacticCouncil"]
66
edition = "2021"

pallets/stableswap/src/benchmarks.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,37 @@ benchmarks! {
409409
assert_eq!(pool.final_block, 1000u32.into());
410410
}
411411

412+
update_asset_peg_source{
413+
let lp_provider: T::AccountId = account("provider", 0, 1);
414+
let (pool_id, pool) = setup_pool_with_initial_liquidity::<T>(&lp_provider);
415+
let successful_origin = T::AuthorityOrigin::try_successful_origin().unwrap();
416+
417+
// Get first asset from pool
418+
let asset_id = *pool.assets.first().unwrap();
419+
let new_peg_source = PegSource::Value((2, 3));
420+
421+
// Register the new peg source for benchmarking
422+
T::BenchmarkHelper::register_asset_peg((asset_id, asset_id), (2u128, 3u128), *b"benchmar")?;
423+
424+
}: _<T::RuntimeOrigin>(successful_origin, pool_id, asset_id, new_peg_source.clone())
425+
verify {
426+
let peg_info = crate::PoolPegs::<T>::get(pool_id).unwrap();
427+
assert_eq!(peg_info.source[0], new_peg_source);
428+
}
429+
430+
update_pool_max_peg_update{
431+
let lp_provider: T::AccountId = account("provider", 0, 1);
432+
let (pool_id, _pool) = setup_pool_with_initial_liquidity::<T>(&lp_provider);
433+
let successful_origin = T::AuthorityOrigin::try_successful_origin().unwrap();
434+
435+
let new_max_peg_update = Permill::from_percent(50);
436+
437+
}: _<T::RuntimeOrigin>(successful_origin, pool_id, new_max_peg_update)
438+
verify {
439+
let peg_info = crate::PoolPegs::<T>::get(pool_id).unwrap();
440+
assert_eq!(peg_info.max_peg_update, new_max_peg_update);
441+
}
442+
412443
router_execution_sell{
413444
let c in 1..2;
414445
let e in 0..1; // if e == 1, execute_sell is executed

pallets/stableswap/src/lib.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ pub mod pallet {
284284
},
285285
/// A pool has been destroyed.
286286
PoolDestroyed { pool_id: T::AssetId },
287+
/// Pool peg source has been updated.
288+
PoolPegSourceUpdated {
289+
pool_id: T::AssetId,
290+
asset_id: T::AssetId,
291+
peg_source: PegSource<T::AssetId>,
292+
},
293+
/// Pool max peg update has been updated.
294+
PoolMaxPegUpdateUpdated {
295+
pool_id: T::AssetId,
296+
max_peg_update: Permill,
297+
},
287298
}
288299

289300
#[pallet::error]
@@ -369,6 +380,9 @@ pub mod pallet {
369380

370381
/// Creating pool with pegs is not allowed for asset with different decimals.
371382
IncorrectAssetDecimals,
383+
384+
/// Pool does not have pegs configured.
385+
NoPegSource,
372386
}
373387

374388
#[pallet::call]
@@ -1250,6 +1264,98 @@ pub mod pallet {
12501264

12511265
Ok(())
12521266
}
1267+
1268+
/// Update the peg source for a specific asset in a pool.
1269+
///
1270+
/// This function allows updating the peg source for an asset within a pool.
1271+
/// The pool must exist and have pegs configured. The asset must be part of the pool.
1272+
/// The current price is always preserved when updating the peg source.
1273+
///
1274+
/// Parameters:
1275+
/// - `origin`: Must be `T::AuthorityOrigin`.
1276+
/// - `pool_id`: The ID of the pool containing the asset.
1277+
/// - `asset_id`: The ID of the asset whose peg source is to be updated.
1278+
/// - `peg_source`: The new peg source for the asset.
1279+
///
1280+
/// Emits `PoolPegSourceUpdated` event when successful.
1281+
///
1282+
/// # Errors
1283+
/// - `PoolNotFound`: If the specified pool does not exist.
1284+
/// - `NoPegSource`: If the pool does not have pegs configured.
1285+
/// - `AssetNotInPool`: If the specified asset is not part of the pool.
1286+
///
1287+
#[pallet::call_index(13)]
1288+
#[pallet::weight(<T as Config>::WeightInfo::update_asset_peg_source())]
1289+
#[transactional]
1290+
pub fn update_asset_peg_source(
1291+
origin: OriginFor<T>,
1292+
pool_id: T::AssetId,
1293+
asset_id: T::AssetId,
1294+
peg_source: PegSource<T::AssetId>,
1295+
) -> DispatchResult {
1296+
T::AuthorityOrigin::ensure_origin(origin)?;
1297+
1298+
let pool = Pools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
1299+
let asset_index = pool.find_asset(asset_id).ok_or(Error::<T>::AssetNotInPool)?;
1300+
1301+
PoolPegs::<T>::try_mutate(pool_id, |maybe_peg_info| -> DispatchResult {
1302+
let peg_info = maybe_peg_info.as_mut().ok_or(Error::<T>::NoPegSource)?;
1303+
1304+
debug_assert_eq!(peg_info.source.len(), pool.assets.len(), "Peg source length mismatch");
1305+
ensure!(peg_info.source.len() == pool.assets.len(), Error::<T>::IncorrectAssets);
1306+
peg_info.source[asset_index] = peg_source.clone();
1307+
1308+
Self::deposit_event(Event::PoolPegSourceUpdated {
1309+
pool_id,
1310+
asset_id,
1311+
peg_source,
1312+
});
1313+
1314+
Ok(())
1315+
})
1316+
}
1317+
1318+
/// Update the maximum peg update percentage for a pool.
1319+
///
1320+
/// This function allows updating the maximum percentage by which peg values
1321+
/// can change in a pool with pegs configured.
1322+
///
1323+
/// Parameters:
1324+
/// - `origin`: Must be `T::AuthorityOrigin`.
1325+
/// - `pool_id`: The ID of the pool to update.
1326+
/// - `max_peg_update`: The new maximum peg update percentage.
1327+
///
1328+
/// Emits `PoolMaxPegUpdateUpdated` event when successful.
1329+
///
1330+
/// # Errors
1331+
/// - `PoolNotFound`: If the specified pool does not exist.
1332+
/// - `NoPegSource`: If the pool does not have pegs configured.
1333+
///
1334+
#[pallet::call_index(14)]
1335+
#[pallet::weight(<T as Config>::WeightInfo::update_pool_max_peg_update())]
1336+
#[transactional]
1337+
pub fn update_pool_max_peg_update(
1338+
origin: OriginFor<T>,
1339+
pool_id: T::AssetId,
1340+
max_peg_update: Permill,
1341+
) -> DispatchResult {
1342+
T::AuthorityOrigin::ensure_origin(origin)?;
1343+
1344+
ensure!(Pools::<T>::contains_key(pool_id), Error::<T>::PoolNotFound);
1345+
1346+
PoolPegs::<T>::try_mutate(pool_id, |maybe_peg_info| -> DispatchResult {
1347+
let peg_info = maybe_peg_info.as_mut().ok_or(Error::<T>::NoPegSource)?;
1348+
1349+
peg_info.max_peg_update = max_peg_update;
1350+
1351+
Self::deposit_event(Event::PoolMaxPegUpdateUpdated {
1352+
pool_id,
1353+
max_peg_update,
1354+
});
1355+
1356+
Ok(())
1357+
})
1358+
}
12531359
}
12541360

12551361
#[pallet::hooks]

pallets/stableswap/src/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod peg_one;
1616
mod price;
1717
mod remove_liquidity;
1818
mod trades;
19+
mod update_max_peg_update;
20+
mod update_peg_source;
1921
mod update_pool;
2022

2123
type Balance = u128;

0 commit comments

Comments
 (0)