Skip to content

Commit

Permalink
Clippy & cleanup (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthteli12 authored Sep 15, 2023
1 parent 0bc7d48 commit 972ae0e
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 306 deletions.
67 changes: 30 additions & 37 deletions pallets/automation-price/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight {
if Self::is_shutdown() == true {
if Self::is_shutdown() {
return T::DbWeight::get().reads(1u64)
}

Expand Down Expand Up @@ -344,8 +344,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;
let fee = <BalanceOf<T>>::saturated_from(1_000_000_000_000u64);
T::FeeHandler::can_pay_fee(&who, fee.clone())
.map_err(|_| Error::<T>::InsufficientBalance)?;
T::FeeHandler::can_pay_fee(&who, fee).map_err(|_| Error::<T>::InsufficientBalance)?;
Self::validate_and_schedule_task(
who.clone(),
provided_id,
Expand All @@ -355,9 +354,9 @@ pub mod pallet {
recipient,
amount,
)?;
T::FeeHandler::withdraw_fee(&who, fee.clone())
T::FeeHandler::withdraw_fee(&who, fee)
.map_err(|_| Error::<T>::LiquidityRestrictions)?;
Ok(().into())
Ok(())
}

/// Initialize an asset
Expand Down Expand Up @@ -419,7 +418,7 @@ pub mod pallet {
0,
)?;
}
Ok(().into())
Ok(())
}

/// Post asset update
Expand Down Expand Up @@ -447,8 +446,7 @@ pub mod pallet {
}
}
let fee = <BalanceOf<T>>::saturated_from(1_000_000_000_000u64);
T::FeeHandler::can_pay_fee(&who.clone(), fee.clone())
.map_err(|_| Error::<T>::InsufficientBalance)?;
T::FeeHandler::can_pay_fee(&who, fee).map_err(|_| Error::<T>::InsufficientBalance)?;
if let Some(asset_target_price) = Self::get_asset_baseline_price(asset.clone()) {
let last_asset_price: AssetPrice = match Self::get_asset_price(asset.clone()) {
None => Err(Error::<T>::AssetNotSupported)?,
Expand Down Expand Up @@ -483,13 +481,13 @@ pub mod pallet {
)?;
}
AssetPrices::<T>::insert(asset.clone(), value);
T::FeeHandler::withdraw_fee(&who, fee.clone())
T::FeeHandler::withdraw_fee(&who, fee)
.map_err(|_| Error::<T>::LiquidityRestrictions)?;
Self::deposit_event(Event::AssetUpdated { asset });
} else {
Err(Error::<T>::AssetNotSupported)?
}
Ok(().into())
Ok(())
}

/// Delete an asset
Expand Down Expand Up @@ -518,7 +516,7 @@ pub mod pallet {
} else {
Err(Error::<T>::AssetNotSupported)?
}
Ok(().into())
Ok(())
}
}

Expand Down Expand Up @@ -562,7 +560,7 @@ pub mod pallet {
};
}
ScheduledAssetDeletion::<T>::remove(current_time_slot);
weight_left = weight_left - asset_reset_weight;
weight_left -= asset_reset_weight;
}

// run as many scheduled tasks as we can
Expand All @@ -571,7 +569,7 @@ pub mod pallet {
.saturating_sub(T::DbWeight::get().reads(1u64))
// For measuring the TaskQueue::<T>::put(tasks_left);
.saturating_sub(T::DbWeight::get().writes(1u64));
if task_queue.len() > 0 {
if !task_queue.is_empty() {
let (tasks_left, new_weight_left) = Self::run_tasks(task_queue, weight_left);
weight_left = new_weight_left;
TaskQueue::<T>::put(tasks_left);
Expand All @@ -588,13 +586,13 @@ pub mod pallet {
if let Some(mut future_scheduled_deletion_assets) =
Self::get_scheduled_asset_period_reset(new_time_slot)
{
future_scheduled_deletion_assets.push(asset.clone());
future_scheduled_deletion_assets.push(asset);
<ScheduledAssetDeletion<T>>::insert(
new_time_slot,
future_scheduled_deletion_assets,
);
} else {
let new_asset_list = vec![asset.clone()];
let new_asset_list = vec![asset];
<ScheduledAssetDeletion<T>>::insert(new_time_slot, new_asset_list);
}
};
Expand All @@ -614,7 +612,7 @@ pub mod pallet {
upper_bound,
lower_bound,
expiration_period,
asset_sudo: asset_owner.clone(),
asset_sudo: asset_owner,
};
AssetMetadata::<T>::insert(asset.clone(), asset_metadatum);
let new_time_slot = Self::get_current_time_slot()?.saturating_add(expiration_period);
Expand Down Expand Up @@ -694,7 +692,7 @@ pub mod pallet {
consumed_task_index.saturating_inc();
let action_weight = match Self::get_task(task_id) {
None => {
Self::deposit_event(Event::TaskNotFound { task_id: task_id.1.clone() });
Self::deposit_event(Event::TaskNotFound { task_id: task_id.1 });
<T as Config>::WeightInfo::emit_event()
},
Some(task) => {
Expand Down Expand Up @@ -725,15 +723,14 @@ pub mod pallet {
}

if consumed_task_index == task_ids.len() {
return (vec![], weight_left)
(vec![], weight_left)
} else {
return (task_ids.split_off(consumed_task_index), weight_left)
(task_ids.split_off(consumed_task_index), weight_left)
}
}

pub fn generate_task_id(owner_id: AccountOf<T>, provided_id: Vec<u8>) -> T::Hash {
let task_hash_input =
TaskHashInput::<T> { owner_id: owner_id.clone(), provided_id: provided_id.clone() };
let task_hash_input = TaskHashInput::<T> { owner_id, provided_id };
T::Hashing::hash_of(&task_hash_input)
}

Expand All @@ -747,22 +744,20 @@ pub mod pallet {
direction: AssetDirection,
trigger_percentage: AssetPercentage,
) -> Result<T::Hash, Error<T>> {
let task_id = Self::generate_task_id(owner_id.clone(), provided_id.clone());
if let Some(_) = Self::get_task((asset.clone(), task_id.clone())) {
let task_id = Self::generate_task_id(owner_id, provided_id);
if let Some(_) = Self::get_task((asset.clone(), task_id)) {
Err(Error::<T>::DuplicateTask)?
}
if let Some(mut asset_tasks) = Self::get_scheduled_tasks((
asset.clone(),
direction.clone(),
trigger_percentage.clone(),
)) {
if let Err(_) = asset_tasks.try_push(task_id.clone()) {
if let Some(mut asset_tasks) =
Self::get_scheduled_tasks((asset.clone(), direction.clone(), trigger_percentage))
{
if asset_tasks.try_push(task_id).is_err() {
Err(Error::<T>::MaxTasksReached)?
}
<ScheduledTasks<T>>::insert((asset, direction, trigger_percentage), asset_tasks);
} else {
let scheduled_tasks: BoundedVec<T::Hash, T::MaxTasksPerSlot> =
vec![task_id.clone()].try_into().unwrap();
vec![task_id].try_into().unwrap();
<ScheduledTasks<T>>::insert(
(asset, direction, trigger_percentage),
scheduled_tasks,
Expand All @@ -782,7 +777,7 @@ pub mod pallet {
recipient: T::AccountId,
amount: BalanceOf<T>,
) -> Result<(), Error<T>> {
if provided_id.len() == 0 {
if provided_id.is_empty() {
Err(Error::<T>::EmptyProvidedId)?
}
let asset_target_price: AssetPrice = match Self::get_asset_baseline_price(asset.clone())
Expand All @@ -794,7 +789,7 @@ pub mod pallet {
None => Err(Error::<T>::AssetNotSupported)?,
Some(asset_price) => asset_price,
};
match direction.clone() {
match direction {
Direction::Down =>
if last_asset_price < asset_target_price {
let last_asset_percentage =
Expand Down Expand Up @@ -848,11 +843,9 @@ pub mod pallet {
};
for percentage in lower..adjusted_higher {
// TODO: pull all and cycle through in memory
if let Some(asset_tasks) = Self::get_scheduled_tasks((
asset.clone(),
direction.clone(),
percentage.clone(),
)) {
if let Some(asset_tasks) =
Self::get_scheduled_tasks((asset.clone(), direction.clone(), percentage))
{
for task in asset_tasks {
existing_task_queue.push((asset.clone(), task));
}
Expand Down
10 changes: 5 additions & 5 deletions pallets/automation-time/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where
let uxt: Block::Extrinsic = Decode::decode(&mut &*encoded_xt).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
format!("Unable to decode extrinsic."),
"Unable to decode extrinsic.".to_string(),
Some(format!("{:?}", e)),
))
})?;
Expand All @@ -126,15 +126,15 @@ where
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
format!("Unable to query fee details."),
"Unable to query fee details.".to_string(),
Some(format!("{:?}", e)),
))
})?
.map_err(|e| {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to get fees.",
Some(String::from_utf8(e).unwrap_or(String::default())),
Some(String::from_utf8(e).unwrap_or_default()),
)))
})?;

Expand Down Expand Up @@ -176,7 +176,7 @@ where
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"RPC value doesn't fit in u64 representation",
Some(format!("RPC value cannot be translated into u64 representation")),
Some("RPC value cannot be translated into u64 representation".to_string()),
)))
})
}
Expand All @@ -198,7 +198,7 @@ where
};
runtime_api_result
.map_err(|e| mapped_err(format!("{:?}", e)))
.map(|r| r.map_err(|e| mapped_err(String::from_utf8(e).unwrap_or(String::default()))))?
.map(|r| r.map_err(|e| mapped_err(String::from_utf8(e).unwrap_or_default())))?
}

fn get_auto_compound_delegated_stake_task_ids(
Expand Down
4 changes: 2 additions & 2 deletions pallets/automation-time/src/autocompounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ pub fn do_calculate_optimal_autostaking(
if total_earnings > best_earnings {
best_earnings = total_earnings;
best_period = period;
best_apy = (total_earnings as f64 / principal as f64) * (365 as f64 / duration as f64);
best_apy = (total_earnings as f64 / principal as f64) * (365_f64 / duration as f64);
}
}

return (best_period, best_apy)
(best_period, best_apy)
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions pallets/automation-time/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
action: &ActionOf<T>,
executions: u32,
) -> Result<Self, DispatchError> {
let schedule_fee_location = action.schedule_fee_location::<T>().into();
let schedule_fee_location = action.schedule_fee_location::<T>();

let schedule_fee_amount: u128 =
Pallet::<T>::calculate_schedule_fee_amount(action, executions)?.saturated_into();
Expand Down Expand Up @@ -173,15 +173,15 @@ mod tests {
fn pay_checked_fees_for_success() {
new_test_ext(0).execute_with(|| {
let alice = AccountId32::new(ALICE);
fund_account(&alice.clone(), 900_000_000, 1, Some(0));
fund_account(&alice, 900_000_000, 1, Some(0));
let starting_funds = Balances::free_balance(alice.clone());

let call: <Test as frame_system::Config>::RuntimeCall =
frame_system::Call::remark_with_event { remark: vec![50] }.into();
let mut spy = 0;
let result = <Test as crate::Config>::FeeHandler::pay_checked_fees_for(
&alice,
&Action::DynamicDispatch { encoded_call: call.clone().encode() },
&Action::DynamicDispatch { encoded_call: call.encode() },
1,
|| {
spy += 1;
Expand All @@ -202,7 +202,7 @@ mod tests {
frame_system::Call::remark_with_event { remark: vec![50] }.into();
let result = <Test as crate::Config>::FeeHandler::pay_checked_fees_for(
&alice,
&Action::DynamicDispatch { encoded_call: call.clone().encode() },
&Action::DynamicDispatch { encoded_call: call.encode() },
1,
|| Ok(()),
);
Expand All @@ -214,15 +214,15 @@ mod tests {
fn does_not_charge_fees_when_prereq_errors() {
new_test_ext(0).execute_with(|| {
let alice = AccountId32::new(ALICE);
fund_account(&alice.clone(), 900_000_000, 1, Some(0));
fund_account(&alice, 900_000_000, 1, Some(0));

let starting_funds = Balances::free_balance(alice.clone());
let call: <Test as frame_system::Config>::RuntimeCall =
frame_system::Call::remark_with_event { remark: vec![50] }.into();

let result = <Test as crate::Config>::FeeHandler::pay_checked_fees_for::<(), _>(
&alice,
&Action::DynamicDispatch { encoded_call: call.clone().encode() },
&Action::DynamicDispatch { encoded_call: call.encode() },
1,
|| Err("error".into()),
);
Expand Down
Loading

0 comments on commit 972ae0e

Please sign in to comment.