Skip to content

Commit

Permalink
Issue/change price feed apollo (#1277)
Browse files Browse the repository at this point in the history
* Change priceTools to fastPriceTools

* Fix clippy

---------

Co-authored-by: Vladimir Stepanenko <[email protected]>
  • Loading branch information
DanijelCBS and vovac12 authored Nov 20, 2024
1 parent 73fdb08 commit cccd53d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pallets/apollo-platform/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn run_to_block<T: Config>(n: u32) {
}
}

fn setup_benchmark<T: Config>() -> Result<(), &'static str> {
fn setup_benchmark<T: Config + price_tools::Config>() -> Result<(), &'static str> {
let owner = alice::<T>();
let pallet_account: AccountIdOf<T> = PalletId(*b"apollolb").into_account_truncating();
let owner_origin: <T as frame_system::Config>::RuntimeOrigin =
Expand Down Expand Up @@ -219,6 +219,10 @@ fn setup_benchmark<T: Config>() -> Result<(), &'static str> {
}

benchmarks! {
where_clause {
where T: price_tools::Config
}

add_pool {
let caller = pallet::AuthorityAccount::<T>::get();
let asset_id = XOR;
Expand Down
7 changes: 2 additions & 5 deletions pallets/apollo-platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub mod pallet {
#[pallet::config]
pub trait Config:
frame_system::Config
+ price_tools::Config
+ liquidity_proxy::Config
+ trading_pair::Config
+ common::Config
Expand Down Expand Up @@ -1500,8 +1499,7 @@ pub mod pallet {
&APOLLO_ASSET_ID.into(),
SwapAmount::with_desired_input(rewards_amount, Balance::zero()),
LiquiditySourceFilter::empty(DEXId::Polkaswap.into()),
)
.unwrap();
)?;

let buyback_amount = outcome.amount;

Expand Down Expand Up @@ -1556,8 +1554,7 @@ pub mod pallet {
&CERES_ASSET_ID.into(),
SwapAmount::with_desired_input(ceres_amount, Balance::zero()),
LiquiditySourceFilter::empty(DEXId::Polkaswap.into()),
)
.unwrap();
)?;

T::AssetManager::burn(
RawOrigin::Signed(caller).into(),
Expand Down
47 changes: 47 additions & 0 deletions pallets/demeter-farming-platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ pub mod pallet {
bool,
Balance,
),
/// Removed pool activated [who, base_asset, pool_asset, reward_asset, is_farm]
RemovedPoolActivated(
AccountIdOf<T>,
AssetIdOf<T>,
AssetIdOf<T>,
AssetIdOf<T>,
bool,
),
}

#[pallet::error]
Expand Down Expand Up @@ -1044,6 +1052,45 @@ pub mod pallet {
// Return a successful DispatchResult
Ok(().into())
}

/// Activate removed pool
#[pallet::call_index(11)]
#[pallet::weight(<T as Config>::WeightInfo::activate_removed_pool())]
pub fn activate_removed_pool(
origin: OriginFor<T>,
base_asset: AssetIdOf<T>,
pool_asset: AssetIdOf<T>,
reward_asset: AssetIdOf<T>,
is_farm: bool,
) -> DispatchResultWithPostInfo {
let user = ensure_signed(origin)?;

if user != AuthorityAccount::<T>::get() {
return Err(Error::<T>::Unauthorized.into());
}

// Get pool info
let mut pool_infos = <Pools<T>>::get(&pool_asset, &reward_asset);
for pool_info in pool_infos.iter_mut() {
if pool_info.is_farm == is_farm && pool_info.base_asset == base_asset {
pool_info.is_removed = false;
}
}

<Pools<T>>::insert(&pool_asset, &reward_asset, &pool_infos);

// Emit an event
Self::deposit_event(Event::<T>::RemovedPoolActivated(
user,
base_asset,
pool_asset,
reward_asset,
is_farm,
));

// Return a successful DispatchResult
Ok(().into())
}
}

#[pallet::hooks]
Expand Down
64 changes: 64 additions & 0 deletions pallets/demeter-farming-platform/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3999,4 +3999,68 @@ mod tests {
}
});
}

#[test]
fn activate_removed_pool_ok() {
let mut ext = ExtBuilder::default().build();
ext.execute_with(|| {
let pool_asset = XOR;
let reward_asset = CERES_ASSET_ID;
let is_farm = true;

let pool_info = PoolData {
multiplier: 1,
deposit_fee: balance!(0),
is_core: true,
is_farm,
total_tokens_in_pool: 0,
rewards: 100,
rewards_to_be_distributed: 0,
is_removed: false,
base_asset: XOR,
};

demeter_farming_platform::Pools::<Runtime>::append(
&pool_asset,
&reward_asset,
&pool_info,
);

assert_ok!(demeter_farming_platform::Pallet::<Runtime>::remove_pool(
RuntimeOrigin::signed(demeter_farming_platform::AuthorityAccount::<Runtime>::get()),
pool_asset,
pool_asset,
reward_asset,
is_farm,
));

let mut pool_infos =
demeter_farming_platform::Pools::<Runtime>::get(&pool_asset, &reward_asset);
for pool_info in pool_infos.iter_mut() {
if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset {
assert_eq!(pool_info.is_removed, true);
}
}

assert_ok!(
demeter_farming_platform::Pallet::<Runtime>::activate_removed_pool(
RuntimeOrigin::signed(
demeter_farming_platform::AuthorityAccount::<Runtime>::get()
),
pool_asset,
pool_asset,
reward_asset,
is_farm,
)
);

let mut pool_infos =
demeter_farming_platform::Pools::<Runtime>::get(&pool_asset, &reward_asset);
for pool_info in pool_infos.iter_mut() {
if pool_info.is_farm == is_farm && pool_info.base_asset == pool_asset {
assert_eq!(pool_info.is_removed, false);
}
}
});
}
}
27 changes: 27 additions & 0 deletions pallets/demeter-farming-platform/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub trait WeightInfo {
fn change_total_tokens() -> Weight;
fn change_info() -> Weight;
fn change_token_info() -> Weight;
fn activate_removed_pool() -> Weight;
}

/// Weights for demeter_farming_platform using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -249,6 +250,19 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: DemeterFarmingPlatform AuthorityAccount (r:1 w:0)
/// Proof Skipped: DemeterFarmingPlatform AuthorityAccount (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: DemeterFarmingPlatform Pools (r:1 w:1)
/// Proof Skipped: DemeterFarmingPlatform Pools (max_values: None, max_size: None, mode: Measured)
fn activate_removed_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `268`
// Estimated: `3506`
// Minimum execution time: 30_569_000 picoseconds.
Weight::from_parts(30_867_000, 3506)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -426,4 +440,17 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: DemeterFarmingPlatform AuthorityAccount (r:1 w:0)
/// Proof Skipped: DemeterFarmingPlatform AuthorityAccount (max_values: Some(1), max_size: None, mode: Measured)
/// Storage: DemeterFarmingPlatform Pools (r:1 w:1)
/// Proof Skipped: DemeterFarmingPlatform Pools (max_values: None, max_size: None, mode: Measured)
fn activate_removed_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `268`
// Estimated: `3506`
// Minimum execution time: 30_569_000 picoseconds.
Weight::from_parts(30_867_000, 3506)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ parameter_types! {
impl apollo_platform::Config for Runtime {
const BLOCKS_PER_FIFTEEN_MINUTES: BlockNumber = 15 * MINUTES;
type RuntimeEvent = RuntimeEvent;
type PriceTools = PriceTools;
type PriceTools = price_tools::FastPriceTools<Runtime>;
type LiquidityProxyPallet = LiquidityProxy;
type UnsignedPriority = ApolloOffchainWorkerTxPriority;
type UnsignedLongevity = ApolloOffchainWorkerTxLongevity;
Expand Down

0 comments on commit cccd53d

Please sign in to comment.