Skip to content

Commit f8255c1

Browse files
remove preserve price
1 parent 7f387d1 commit f8255c1

3 files changed

Lines changed: 36 additions & 44 deletions

File tree

pallets/stableswap/src/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ benchmarks! {
421421
// Register the new peg source for benchmarking
422422
T::BenchmarkHelper::register_asset_peg((asset_id, asset_id), (2u128, 3u128), *b"benchmar")?;
423423

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

pallets/stableswap/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,21 +1269,20 @@ pub mod pallet {
12691269
///
12701270
/// This function allows updating the peg source for an asset within a pool.
12711271
/// 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.
12721273
///
12731274
/// Parameters:
12741275
/// - `origin`: Must be `T::UpdateTradabilityOrigin`.
12751276
/// - `pool_id`: The ID of the pool containing the asset.
12761277
/// - `asset_id`: The ID of the asset whose peg source is to be updated.
12771278
/// - `peg_source`: The new peg source for the asset.
1278-
/// - `preserve_price`: If true, keeps current price; if false, fetches new price from new source.
12791279
///
12801280
/// Emits `PoolPegSourceUpdated` event when successful.
12811281
///
12821282
/// # Errors
12831283
/// - `PoolNotFound`: If the specified pool does not exist.
12841284
/// - `NoPegSource`: If the pool does not have pegs configured.
12851285
/// - `AssetNotInPool`: If the specified asset is not part of the pool.
1286-
/// - `MissingTargetPegOracle`: If the new peg source cannot be accessed.
12871286
///
12881287
#[pallet::call_index(13)]
12891288
#[pallet::weight(<T as Config>::WeightInfo::update_asset_peg_source())]
@@ -1293,7 +1292,6 @@ pub mod pallet {
12931292
pool_id: T::AssetId,
12941293
asset_id: T::AssetId,
12951294
peg_source: PegSource<T::AssetId>,
1296-
preserve_price: bool,
12971295
) -> DispatchResult {
12981296
T::UpdateTradabilityOrigin::ensure_origin(origin)?;
12991297

@@ -1306,12 +1304,8 @@ pub mod pallet {
13061304
ensure!(peg_info.current.len() == pool.assets.len(), Error::<T>::IncorrectAssets);
13071305
peg_info.source[asset_index] = peg_source.clone();
13081306

1309-
// If preserve_price is false, fetch new price from the new source
1310-
if !preserve_price {
1311-
let new_price = T::TargetPegOracle::get_raw_entry(asset_id, peg_source.clone())
1312-
.map_err(|_| Error::<T>::MissingTargetPegOracle)?;
1313-
peg_info.current[asset_index] = new_price.price;
1314-
}
1307+
// Price is always preserved when updating peg source
1308+
13151309
Self::deposit_event(Event::PoolPegSourceUpdated {
13161310
pool_id,
13171311
asset_id,

pallets/stableswap/src/tests/update_peg_source.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hydradx_traits::OraclePeriod;
66
use sp_runtime::Permill;
77

88
#[test]
9-
fn update_asset_peg_source_should_work_when_preserve_price_is_true() {
9+
fn update_asset_peg_source_should_work() {
1010
let asset_a: AssetId = 1;
1111
let asset_b: AssetId = 2;
1212
let pool_id = 100;
@@ -41,14 +41,13 @@ fn update_asset_peg_source_should_work_when_preserve_price_is_true() {
4141
let initial_peg_info = PoolPegs::<Test>::get(pool_id).unwrap();
4242
let initial_price = initial_peg_info.current[0];
4343

44-
// Update peg source for asset_a with preserve_price=true
44+
// Update peg source for asset_a (price always preserved)
4545
let new_peg_source = PegSource::Value((2, 3));
4646
assert_ok!(Stableswap::update_asset_peg_source(
4747
RuntimeOrigin::root(),
4848
pool_id,
4949
asset_a,
5050
new_peg_source.clone(),
51-
true, // preserve_price
5251
));
5352

5453
// Check that peg source was updated
@@ -71,7 +70,7 @@ fn update_asset_peg_source_should_work_when_preserve_price_is_true() {
7170
}
7271

7372
#[test]
74-
fn update_asset_peg_source_should_work_when_preserve_price_is_false() {
73+
fn update_asset_peg_source_should_preserve_price() {
7574
let asset_a: AssetId = 1;
7675
let asset_b: AssetId = 2;
7776
let pool_id = 100;
@@ -102,22 +101,25 @@ fn update_asset_peg_source_should_work_when_preserve_price_is_false() {
102101
Permill::from_percent(10),
103102
));
104103

105-
// Update peg source for asset_a with preserve_price=false
104+
// Get initial price to verify it's preserved
105+
let initial_peg_info = PoolPegs::<Test>::get(pool_id).unwrap();
106+
let initial_price = initial_peg_info.current[0];
107+
108+
// Update peg source for asset_a (price always preserved)
106109
let new_peg_source = PegSource::Value((2, 3));
107110
assert_ok!(Stableswap::update_asset_peg_source(
108111
RuntimeOrigin::root(),
109112
pool_id,
110113
asset_a,
111114
new_peg_source.clone(),
112-
false, // don't preserve_price
113115
));
114116

115117
// Check that peg source was updated
116118
let updated_peg_info = PoolPegs::<Test>::get(pool_id).unwrap();
117119
assert_eq!(updated_peg_info.source[0], new_peg_source);
118120

119-
// Check that price was updated to new value
120-
assert_eq!(updated_peg_info.current[0], (2, 3));
121+
// Check that price was preserved (not updated)
122+
assert_eq!(updated_peg_info.current[0], initial_price);
121123

122124
// Check event was emitted
123125
System::assert_last_event(
@@ -149,7 +151,6 @@ fn update_asset_peg_source_should_fail_when_pool_not_found() {
149151
pool_id, // Pool doesn't exist
150152
asset_a,
151153
new_peg_source,
152-
true,
153154
),
154155
Error::<Test>::PoolNotFound
155156
);
@@ -186,7 +187,7 @@ fn update_asset_peg_source_should_fail_when_pool_has_no_pegs() {
186187
let new_peg_source = PegSource::Value((2, 3));
187188

188189
assert_noop!(
189-
Stableswap::update_asset_peg_source(RuntimeOrigin::root(), pool_id, asset_a, new_peg_source, true,),
190+
Stableswap::update_asset_peg_source(RuntimeOrigin::root(), pool_id, asset_a, new_peg_source,),
190191
Error::<Test>::NoPegSource
191192
);
192193
});
@@ -235,7 +236,6 @@ fn update_asset_peg_source_should_fail_when_asset_not_in_pool() {
235236
pool_id,
236237
asset_c, // Not in pool
237238
new_peg_source,
238-
true,
239239
),
240240
Error::<Test>::AssetNotInPool
241241
);
@@ -279,14 +279,14 @@ fn update_asset_peg_source_should_fail_when_invalid_origin() {
279279

280280
// BOB doesn't have UpdateTradabilityOrigin permission
281281
assert_noop!(
282-
Stableswap::update_asset_peg_source(RuntimeOrigin::signed(BOB), pool_id, asset_a, new_peg_source, true,),
282+
Stableswap::update_asset_peg_source(RuntimeOrigin::signed(BOB), pool_id, asset_a, new_peg_source,),
283283
sp_runtime::DispatchError::BadOrigin
284284
);
285285
});
286286
}
287287

288288
#[test]
289-
fn update_asset_peg_source_should_fail_when_oracle_entry_missing() {
289+
fn update_asset_peg_source_should_work_with_oracle_source() {
290290
let asset_a: AssetId = 1;
291291
let asset_b: AssetId = 2;
292292
let pool_id = 100;
@@ -317,19 +317,19 @@ fn update_asset_peg_source_should_fail_when_oracle_entry_missing() {
317317
Permill::from_percent(10),
318318
));
319319

320-
// Try to update with oracle source that doesn't exist
321-
let invalid_oracle_source = PegSource::Oracle((*b"nonexist", OraclePeriod::LastBlock, asset_a));
320+
// Update with oracle source (should work since price is always preserved)
321+
let oracle_source = PegSource::Oracle((*b"nonexist", OraclePeriod::LastBlock, asset_a));
322322

323-
assert_noop!(
324-
Stableswap::update_asset_peg_source(
325-
RuntimeOrigin::root(),
326-
pool_id,
327-
asset_a,
328-
invalid_oracle_source,
329-
false, // This will try to fetch from oracle
330-
),
331-
Error::<Test>::MissingTargetPegOracle
332-
);
323+
assert_ok!(Stableswap::update_asset_peg_source(
324+
RuntimeOrigin::root(),
325+
pool_id,
326+
asset_a,
327+
oracle_source.clone(),
328+
));
329+
330+
// Check that peg source was updated
331+
let updated_peg_info = PoolPegs::<Test>::get(pool_id).unwrap();
332+
assert_eq!(updated_peg_info.source[0], oracle_source);
333333
});
334334
}
335335

@@ -370,24 +370,23 @@ fn update_asset_peg_source_should_update_second_asset_correctly() {
370370
let initial_price_a = initial_peg_info.current[0];
371371
let _initial_price_b = initial_peg_info.current[1];
372372

373-
// Update peg source for asset_b (index 1) with preserve_price=false
373+
// Update peg source for asset_b (index 1) - price will be preserved
374374
let new_peg_source = PegSource::Value((3, 4));
375375
assert_ok!(Stableswap::update_asset_peg_source(
376376
RuntimeOrigin::root(),
377377
pool_id,
378378
asset_b, // Second asset
379379
new_peg_source.clone(),
380-
false, // don't preserve_price
381380
));
382381

383382
// Check that only asset_b's peg source was updated
384383
let updated_peg_info = PoolPegs::<Test>::get(pool_id).unwrap();
385384
assert_eq!(updated_peg_info.source[0], PegSource::Value((1, 1))); // asset_a unchanged
386385
assert_eq!(updated_peg_info.source[1], new_peg_source); // asset_b updated
387386

388-
// Check that only asset_b's price was updated
387+
// Check that both prices were preserved (not updated)
389388
assert_eq!(updated_peg_info.current[0], initial_price_a); // asset_a price unchanged
390-
assert_eq!(updated_peg_info.current[1], (3, 4)); // asset_b price updated
389+
assert_eq!(updated_peg_info.current[1], (2, 2)); // asset_b price preserved
391390

392391
// Check event was emitted
393392
System::assert_last_event(
@@ -447,7 +446,6 @@ fn update_asset_peg_source_should_work_with_three_assets() {
447446
pool_id,
448447
asset_b,
449448
new_peg_source.clone(),
450-
false,
451449
));
452450

453451
// Check that only asset_b's peg source was updated
@@ -456,9 +454,9 @@ fn update_asset_peg_source_should_work_with_three_assets() {
456454
assert_eq!(updated_peg_info.source[1], new_peg_source);
457455
assert_eq!(updated_peg_info.source[2], PegSource::Value((3, 3)));
458456

459-
// Check that only asset_b's price was updated
460-
assert_eq!(updated_peg_info.current[0], (1, 1));
461-
assert_eq!(updated_peg_info.current[1], (5, 6));
462-
assert_eq!(updated_peg_info.current[2], (3, 3));
457+
// Check that all prices were preserved (not updated)
458+
assert_eq!(updated_peg_info.current[0], (1, 1)); // asset_a price preserved
459+
assert_eq!(updated_peg_info.current[1], (2, 2)); // asset_b price preserved
460+
assert_eq!(updated_peg_info.current[2], (3, 3)); // asset_c price preserved
463461
});
464462
}

0 commit comments

Comments
 (0)