Skip to content

Commit

Permalink
Changed vested_transfer extrinsic behavior. (#857)
Browse files Browse the repository at this point in the history
* Changed `vested_transfer` extrinsic behavior.
Fixed self-vesting case (`from` == `to`) when it was possible to self-freeze funds above the current account balance.

* added test for self-vesting case
  • Loading branch information
PraetorP authored Nov 30, 2022
1 parent 7e3c9ae commit 65da351
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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

0 comments on commit 65da351

Please sign in to comment.