Skip to content

Commit

Permalink
Zepter
Browse files Browse the repository at this point in the history
  • Loading branch information
lrazovic authored and JuaniRios committed Sep 11, 2024
1 parent 7d5b956 commit d9ac4a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
1 change: 1 addition & 0 deletions pallets/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ std = [
"frame-system/std",
"itertools/use_std",
"log/std",
"on-slash-vesting/std",
"pallet-assets/std",
"pallet-balances/std",
"pallet-insecure-randomness-collective-flip/std",
Expand Down
22 changes: 11 additions & 11 deletions pallets/on-slash-vesting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ workspace = true
default = [ "std" ]

std = [
"pallet-vesting/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"log/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-runtime/std",
"sp-io/std",
"serde/std",
]
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"pallet-vesting/std",
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"sp-io/std",
"sp-runtime/std",
]
41 changes: 18 additions & 23 deletions pallets/on-slash-vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ mod mock;
#[cfg(test)]
mod test;

extern crate alloc;
use alloc::vec::Vec;
use frame_support::{
sp_runtime::{traits::Convert, FixedPointNumber, FixedU128},
traits::{Currency, OriginTrait},
};
use pallet_vesting::Vesting;
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::{traits::BlockNumberProvider, BoundedVec};

pub trait OnSlash<AccountId, Balance: Clone> {
fn on_slash(account: &AccountId, amount: Balance);
Expand All @@ -33,27 +31,24 @@ where
T::Currency: Currency<AccountIdOf<T>, Balance = u128>,
{
fn on_slash(account: &AccountIdOf<T>, slashed_amount: u128) {
let Some(vesting_schedules) = <Vesting<T>>::get(account) else { return };
let vesting_schedules = vesting_schedules.to_vec();
let mut new_vesting_schedules = Vec::new();
let now = T::BlockNumberProvider::current_block_number();
for schedule in vesting_schedules {
let total_locked = schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_sub(slashed_amount);
let start_block: u128 = T::BlockNumberToBalance::convert(now);
let end_block: u128 = schedule.ending_block_as_balance::<T::BlockNumberToBalance>();
let duration = end_block.saturating_sub(start_block);
let per_block = FixedU128::from_rational(total_locked, duration).saturating_mul_int(1u128);
let new_schedule = pallet_vesting::VestingInfo::new(total_locked, per_block, now);
if new_schedule.is_valid() {
new_vesting_schedules.push(new_schedule);
if let Some(vesting_schedules) = <Vesting<T>>::get(account) {
let mut new_vesting_schedules = BoundedVec::with_bounded_capacity(vesting_schedules.len());
let now = T::BlockNumberProvider::current_block_number();
for schedule in vesting_schedules {
let total_locked = schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_sub(slashed_amount);
let start_block = T::BlockNumberToBalance::convert(now);
let end_block = schedule.ending_block_as_balance::<T::BlockNumberToBalance>();
let duration = end_block.saturating_sub(start_block);
let per_block = FixedU128::from_rational(total_locked, duration).saturating_mul_int(1u128);
let new_schedule = pallet_vesting::VestingInfo::new(total_locked, per_block, now);
if new_schedule.is_valid() {
// The push should always succeed because we are iterating over a bounded vector.
let push_result = new_vesting_schedules.try_push(new_schedule);
debug_assert!(push_result.is_ok());
}
}
<Vesting<T>>::set(account, Some(new_vesting_schedules));
let _ = <pallet_vesting::Pallet<T>>::vest(T::RuntimeOrigin::signed(account.clone()));
}
let Ok(new_vesting_schedules) = new_vesting_schedules.try_into() else {
log::error!("Failed to convert new vesting schedules into BoundedVec");
return
};
<Vesting<T>>::set(account, Some(new_vesting_schedules));
let vest_result = <pallet_vesting::Pallet<T>>::vest(T::RuntimeOrigin::signed(account.clone()));
debug_assert!(vest_result.is_ok());
}
}
6 changes: 5 additions & 1 deletion runtimes/polimec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ std = [
"frame-system/std",
"frame-try-runtime?/std",
"log/std",
"on-slash-vesting/std",
"orml-oracle/std",
"pallet-assets/std",
"pallet-aura/std",
Expand Down Expand Up @@ -276,6 +277,9 @@ try-runtime = [
# A feature that should be enabled when the runtime should be built for on-chain
# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
# to make it smaller, like logging for example.
on-chain-release-build = [ "sp-api/disable-logging", "pallet-funding/on-chain-release-build" ]
on-chain-release-build = [
"pallet-funding/on-chain-release-build",
"sp-api/disable-logging",
]

development-settings = [ "shared-configuration/development-settings" ]

0 comments on commit d9ac4a4

Please sign in to comment.