@@ -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]
0 commit comments