Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ pub mod module {
) -> DispatchResult {
let from = T::VestedTransferOrigin::ensure_origin(origin)?;
let to = T::Lookup::lookup(dest)?;

if to == from {
ensure!(
T::Currency::free_balance(&from) >= schedule.total_amount().ok_or(ArithmeticError::Overflow)?,
Error::<T>::InsufficientBalanceToLock,
);
}

Self::do_vested_transfer(&from, &to, schedule.clone())?;

Self::deposit_event(Event::VestingScheduleAdded {
Expand Down
5 changes: 4 additions & 1 deletion vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const CHARLIE: AccountId = 3;

pub const ALICE_BALANCE: u64 = 100;
pub const CHARLIE_BALANCE: u64 = 50;

#[derive(Default)]
pub struct ExtBuilder;

Expand All @@ -126,7 +129,7 @@ impl ExtBuilder {
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, 100), (CHARLIE, 50)],
balances: vec![(ALICE, ALICE_BALANCE), (CHARLIE, CHARLIE_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
39 changes: 39 additions & 0 deletions vesting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ fn vested_transfer_works() {
});
}

#[test]
fn self_vesting() {
ExtBuilder::build().execute_with(|| {
System::set_block_number(1);

let schedule = VestingSchedule {
start: 0u64,
period: 10u64,
period_count: 1u32,
per_period: ALICE_BALANCE,
};

let bad_schedule = VestingSchedule {
start: 0u64,
period: 10u64,
period_count: 1u32,
per_period: 10 * ALICE_BALANCE,
};

assert_noop!(
Vesting::vested_transfer(RuntimeOrigin::signed(ALICE), ALICE, bad_schedule),
crate::Error::<Runtime>::InsufficientBalanceToLock
);

assert_ok!(Vesting::vested_transfer(
RuntimeOrigin::signed(ALICE),
ALICE,
schedule.clone()
));

assert_eq!(Vesting::vesting_schedules(&ALICE), vec![schedule.clone()]);
System::assert_last_event(RuntimeEvent::Vesting(crate::Event::VestingScheduleAdded {
from: ALICE,
to: ALICE,
vesting_schedule: schedule,
}));
});
}

#[test]
fn add_new_vesting_schedule_merges_with_current_locked_balance_and_until() {
ExtBuilder::build().execute_with(|| {
Expand Down