Skip to content

Commit b2660c7

Browse files
committed
Change requests applied
1 parent d6b43ce commit b2660c7

File tree

11 files changed

+128
-209
lines changed

11 files changed

+128
-209
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/src/omnipool_remove_token.rs

Lines changed: 47 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -71,79 +71,31 @@ fn populate_oracle_for_dot() {
7171
}
7272

7373
#[test]
74-
fn remove_token_should_clear_dynamic_fees_storage() {
74+
fn remove_token_should_clear_both_fees_and_oracle_entries() {
7575
TestNet::reset();
7676

7777
Hydra::execute_with(|| {
7878
// Arrange
7979
init_omnipool_with_oracle();
8080
populate_oracle_for_dot();
8181

82-
// Verify dynamic fees are set for DOT
83-
let dot_fees_before = hydradx_runtime::DynamicFees::current_fees(DOT);
84-
assert!(
85-
dot_fees_before.is_some(),
86-
"DOT should have dynamic fees entries before removal"
87-
);
88-
89-
// Get position and remove all liquidity
90-
let position_id = 2; // DOT position (HDX=0, DAI=1, DOT=2)
91-
let position = pallet_omnipool::Pallet::<hydradx_runtime::Runtime>::load_position(
92-
position_id,
93-
hydradx_runtime::Omnipool::protocol_account(),
94-
)
95-
.unwrap();
96-
97-
// Freeze the asset
98-
assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state(
99-
RuntimeOrigin::root(),
100-
DOT,
101-
Tradability::FROZEN
102-
));
103-
104-
// Sacrifice the position to remove all shares
105-
assert_ok!(hydradx_runtime::Omnipool::sacrifice_position(
106-
RuntimeOrigin::signed(hydradx_runtime::Omnipool::protocol_account()),
107-
position_id
108-
));
109-
110-
// Act - Remove token from omnipool
111-
assert_ok!(hydradx_runtime::Omnipool::remove_token(
112-
RuntimeOrigin::root(),
113-
DOT,
114-
AccountId::from(BOB),
115-
));
116-
117-
// Assert - Dynamic fees should be cleared
118-
let dot_fees_after = hydradx_runtime::DynamicFees::current_fees(DOT);
82+
// Verify both dynamic fees and oracle entries exist
11983
assert!(
120-
dot_fees_after.is_none(),
121-
"DOT dynamic fees should be cleared after token removal"
84+
hydradx_runtime::DynamicFees::current_fees(DOT).is_some(),
85+
"DOT should have dynamic fees before removal"
12286
);
12387

124-
// Verify AssetFee storage is cleared
88+
// Verify AssetFee storage exists
12589
let asset_fee = pallet_dynamic_fees::AssetFee::<hydradx_runtime::Runtime>::get(DOT);
126-
assert!(asset_fee.is_none(), "DOT AssetFee storage should be cleared");
90+
assert!(asset_fee.is_some(), "DOT AssetFee storage should exist before removal");
12791

128-
// Verify AssetFeeConfiguration storage is cleared
92+
// Verify AssetFeeConfiguration storage exists
12993
let asset_fee_config = pallet_dynamic_fees::AssetFeeConfiguration::<hydradx_runtime::Runtime>::get(DOT);
13094
assert!(
131-
asset_fee_config.is_none(),
132-
"DOT AssetFeeConfiguration storage should be cleared"
95+
asset_fee_config.is_some(),
96+
"DOT AssetFeeConfiguration storage should exist before removal"
13397
);
134-
});
135-
}
13698

137-
#[test]
138-
fn remove_token_should_clear_oracle_entries() {
139-
TestNet::reset();
140-
141-
Hydra::execute_with(|| {
142-
// Arrange
143-
init_omnipool_with_oracle();
144-
populate_oracle_for_dot();
145-
146-
// Verify oracle entries exist for DOT before removal
14799
let hub_asset = LRNA;
148100
let assets = if DOT < hub_asset {
149101
(DOT, hub_asset)
@@ -163,35 +115,53 @@ fn remove_token_should_clear_oracle_entries() {
163115
);
164116
}
165117

166-
// Get position and remove all liquidity
167-
let position_id = 2; // DOT position
168-
let position = pallet_omnipool::Pallet::<hydradx_runtime::Runtime>::load_position(
169-
position_id,
170-
hydradx_runtime::Omnipool::protocol_account(),
171-
)
172-
.unwrap();
118+
// Verify whitelist entry exists
119+
let whitelist = pallet_ema_oracle::WhitelistedAssets::<hydradx_runtime::Runtime>::get();
120+
assert!(
121+
whitelist.contains(&(*b"omnipool", assets)),
122+
"DOT should be in oracle whitelist before removal"
123+
);
173124

174-
// Freeze the asset
125+
// Act
126+
// Prepare for token removal
127+
let position_id = 2;
175128
assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state(
176129
RuntimeOrigin::root(),
177130
DOT,
178131
Tradability::FROZEN
179132
));
180133

181-
// Sacrifice the position
182134
assert_ok!(hydradx_runtime::Omnipool::sacrifice_position(
183135
RuntimeOrigin::signed(hydradx_runtime::Omnipool::protocol_account()),
184136
position_id
185137
));
186138

187-
// Act - Remove token from omnipool
139+
// Remove token
188140
assert_ok!(hydradx_runtime::Omnipool::remove_token(
189141
RuntimeOrigin::root(),
190142
DOT,
191143
AccountId::from(BOB),
192144
));
193145

194-
// Assert - Oracle entries should be cleared
146+
// Assert
147+
// Verify dynamic fees are cleared
148+
assert!(
149+
hydradx_runtime::DynamicFees::current_fees(DOT).is_none(),
150+
"DOT dynamic fees should be cleared"
151+
);
152+
153+
// Verify AssetFee storage is cleared
154+
let asset_fee_after = pallet_dynamic_fees::AssetFee::<hydradx_runtime::Runtime>::get(DOT);
155+
assert!(asset_fee_after.is_none(), "DOT AssetFee storage should be cleared");
156+
157+
// Verify AssetFeeConfiguration storage is cleared
158+
let asset_fee_config_after = pallet_dynamic_fees::AssetFeeConfiguration::<hydradx_runtime::Runtime>::get(DOT);
159+
assert!(
160+
asset_fee_config_after.is_none(),
161+
"DOT AssetFeeConfiguration storage should be cleared"
162+
);
163+
164+
// Verify oracle entries are cleared for all periods
195165
for period in supported_periods.iter() {
196166
let oracle_entry =
197167
pallet_ema_oracle::Oracles::<hydradx_runtime::Runtime>::get((*b"omnipool", assets, period));
@@ -203,74 +173,19 @@ fn remove_token_should_clear_oracle_entries() {
203173
}
204174

205175
// Verify whitelist entry is removed
206-
let whitelist = pallet_ema_oracle::WhitelistedAssets::<hydradx_runtime::Runtime>::get();
176+
let whitelist_after = pallet_ema_oracle::WhitelistedAssets::<hydradx_runtime::Runtime>::get();
207177
assert!(
208-
!whitelist.contains(&(*b"omnipool", assets)),
178+
!whitelist_after.contains(&(*b"omnipool", assets)),
209179
"DOT should be removed from oracle whitelist"
210180
);
211-
});
212-
}
213-
214-
#[test]
215-
fn remove_token_should_clear_both_fees_and_oracle_entries() {
216-
TestNet::reset();
217-
218-
Hydra::execute_with(|| {
219-
// Arrange
220-
init_omnipool_with_oracle();
221-
populate_oracle_for_dot();
222181

223-
// Verify both dynamic fees and oracle entries exist
182+
// Verify Accumulator storage is cleared
183+
let accumulator = pallet_ema_oracle::Accumulator::<hydradx_runtime::Runtime>::get();
224184
assert!(
225-
hydradx_runtime::DynamicFees::current_fees(DOT).is_some(),
226-
"DOT should have dynamic fees before removal"
185+
!accumulator.contains_key(&(*b"omnipool", assets)),
186+
"DOT should be removed from oracle accumulator"
227187
);
228188

229-
let hub_asset = LRNA;
230-
let assets = if DOT < hub_asset {
231-
(DOT, hub_asset)
232-
} else {
233-
(hub_asset, DOT)
234-
};
235-
236-
let period = OraclePeriod::LastBlock;
237-
let oracle_entry_before =
238-
pallet_ema_oracle::Oracles::<hydradx_runtime::Runtime>::get((*b"omnipool", assets, period));
239-
assert!(
240-
oracle_entry_before.is_some(),
241-
"Oracle entry should exist for DOT before removal"
242-
);
243-
244-
// Prepare for token removal
245-
let position_id = 2;
246-
assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state(
247-
RuntimeOrigin::root(),
248-
DOT,
249-
Tradability::FROZEN
250-
));
251-
252-
assert_ok!(hydradx_runtime::Omnipool::sacrifice_position(
253-
RuntimeOrigin::signed(hydradx_runtime::Omnipool::protocol_account()),
254-
position_id
255-
));
256-
257-
// Act - Remove token
258-
assert_ok!(hydradx_runtime::Omnipool::remove_token(
259-
RuntimeOrigin::root(),
260-
DOT,
261-
AccountId::from(BOB),
262-
));
263-
264-
// Assert - Both should be cleared
265-
assert!(
266-
hydradx_runtime::DynamicFees::current_fees(DOT).is_none(),
267-
"DOT dynamic fees should be cleared"
268-
);
269-
270-
let oracle_entry_after =
271-
pallet_ema_oracle::Oracles::<hydradx_runtime::Runtime>::get((*b"omnipool", assets, period));
272-
assert!(oracle_entry_after.is_none(), "Oracle entry should be cleared for DOT");
273-
274189
// Verify the asset itself is removed
275190
let asset_state = hydradx_runtime::Omnipool::assets(DOT);
276191
assert!(asset_state.is_none(), "DOT asset should be removed from omnipool");
@@ -301,6 +216,7 @@ fn remove_token_should_not_affect_other_assets() {
301216
let hdx_oracle_before =
302217
pallet_ema_oracle::Oracles::<hydradx_runtime::Runtime>::get((*b"omnipool", hdx_assets, period));
303218

219+
// Act
304220
// Remove DOT
305221
let position_id = 2;
306222
assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state(
@@ -320,7 +236,8 @@ fn remove_token_should_not_affect_other_assets() {
320236
AccountId::from(BOB),
321237
));
322238

323-
// Assert - Other assets' fees and oracle entries should remain
239+
// Assert
240+
// Other assets' fees and oracle entries should remain
324241
let hdx_fees_after = hydradx_runtime::DynamicFees::current_fees(HDX);
325242
let dai_fees_after = hydradx_runtime::DynamicFees::current_fees(DAI);
326243

pallets/dynamic-fees/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-dynamic-fees"
3-
version = "4.0.1"
3+
version = "4.1.0"
44
description = "A pallet to provide support for dynamic fees"
55
authors = ["GalacticCouncil"]
66
edition = "2021"

pallets/dynamic-fees/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,25 @@ where
426426
log::trace!(target: "dynamic-fees", "new fees: {:?} {:?}", asset_fee, protocol_fee);
427427
(asset_fee, protocol_fee)
428428
}
429+
430+
/// Clears all fee-related storage for an asset
431+
///
432+
/// This function removes both the asset fee and asset fee configuration entries for the specified asset.
433+
/// It emits an `AssetFeeConfigRemoved` event.
434+
///
435+
/// # Arguments
436+
/// * `asset_id` - The asset ID to clear fees for
437+
///
438+
/// # Returns
439+
/// Weight of the operation
440+
pub fn clear_asset_fees(asset_id: T::AssetId) -> Weight {
441+
AssetFee::<T>::remove(asset_id);
442+
AssetFeeConfiguration::<T>::remove(asset_id);
443+
444+
Self::deposit_event(Event::AssetFeeConfigRemoved { asset_id });
445+
446+
T::WeightInfo::remove_asset_fee()
447+
}
429448
}
430449

431450
/// Main interface for retrieving dynamic fees

pallets/ema-oracle/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-ema-oracle"
3-
version = "1.8.1"
3+
version = "1.9.0"
44
description = "Exponential moving average oracle for AMM pools"
55
authors = ["GalacticCouncil"]
66
edition = "2021"

pallets/ema-oracle/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,47 @@ impl<T: Config> Pallet<T> {
539539

540540
new_value >= lower_bound && new_value <= upper_bound
541541
}
542+
543+
/// Clears all oracle-related storage for a given asset paired with hub asset
544+
///
545+
/// This function removes:
546+
/// - Oracle entries for all supported periods
547+
/// - Whitelist entry
548+
/// - Accumulator entry (if present)
549+
///
550+
/// It emits a `RemovedFromWhitelist` event.
551+
///
552+
/// # Arguments
553+
/// * `source` - The source of the oracle data
554+
/// * `asset_id` - The asset ID to clear oracle data for
555+
/// * `hub_asset` - The hub asset that `asset_id` is paired with
556+
///
557+
/// # Returns
558+
/// Weight of the operation
559+
pub fn clear_asset_oracle(source: Source, asset_id: AssetId, hub_asset: AssetId) -> Weight {
560+
let assets = ordered_pair(asset_id, hub_asset);
561+
562+
// Remove from whitelist
563+
WhitelistedAssets::<T>::mutate(|list| {
564+
list.remove(&(source, assets));
565+
});
566+
567+
// Remove oracle storage entries for all supported periods
568+
let supported_periods = T::SupportedPeriods::get();
569+
for period in supported_periods.into_iter() {
570+
Oracles::<T>::remove((source, assets, period));
571+
}
572+
573+
// Remove from accumulator if present
574+
let _ = Accumulator::<T>::mutate(|accumulator| {
575+
accumulator.remove(&(source, assets));
576+
Ok::<(), ()>(())
577+
});
578+
579+
Self::deposit_event(Event::RemovedFromWhitelist { source, assets });
580+
581+
T::WeightInfo::remove_oracle()
582+
}
542583
}
543584

544585
/// A callback handler for trading and liquidity activity that schedules oracle updates.

pallets/omnipool/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-omnipool"
3-
version = "5.1.10"
3+
version = "5.2.0"
44
authors = ['GalacticCouncil']
55
edition = "2021"
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)