Skip to content

Commit

Permalink
Add VestedTransferOrigin. (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Sep 17, 2020
1 parent b131157 commit 2a55e07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 5 additions & 2 deletions vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use codec::{Decode, Encode, HasCompact};
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, ensure,
traits::{Currency, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
weights::Weight,
};
use frame_system::{ensure_root, ensure_signed};
Expand Down Expand Up @@ -122,6 +122,9 @@ pub trait Trait: frame_system::Trait {
/// The minimum amount transferred to call `vested_transfer`.
type MinVestedTransfer: Get<BalanceOf<Self>>;

/// Required origin for vested transfer.
type VestedTransferOrigin: EnsureOrigin<Self::Origin, Success = Self::AccountId>;

/// Weight information for extrinsics in this module.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -220,7 +223,7 @@ decl_module! {
dest: <T::Lookup as StaticLookup>::Source,
schedule: VestingScheduleOf<T>,
) {
let from = ensure_signed(origin)?;
let from = T::VestedTransferOrigin::ensure_origin(origin)?;
let to = T::Lookup::lookup(dest)?;
Self::do_vested_transfer(&from, &to, schedule.clone())?;

Expand Down
20 changes: 20 additions & 0 deletions vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg(test)]

use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
use frame_system::RawOrigin;
use pallet_balances;
use sp_core::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup, Perbill};
Expand Down Expand Up @@ -81,10 +82,29 @@ impl pallet_balances::Trait for Runtime {
}
pub type PalletBalances = pallet_balances::Module<Runtime>;

pub struct EnsureAliceOrBob;
impl EnsureOrigin<Origin> for EnsureAliceOrBob {
type Success = AccountId;

fn try_origin(o: Origin) -> Result<Self::Success, Origin> {
Into::<Result<RawOrigin<AccountId>, Origin>>::into(o).and_then(|o| match o {
RawOrigin::Signed(ALICE) => Ok(ALICE),
RawOrigin::Signed(BOB) => Ok(BOB),
r => Err(Origin::from(r)),
})
}

#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> Origin {
Origin::from(RawOrigin::Signed(Default::default()))
}
}

impl Trait for Runtime {
type Event = TestEvent;
type Currency = PalletBalances;
type MinVestedTransfer = MinVestedTransfer;
type VestedTransferOrigin = EnsureAliceOrBob;
type WeightInfo = ();
}
pub type Vesting = Module<Runtime>;
Expand Down
18 changes: 17 additions & 1 deletion vesting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![cfg(test)]

use super::*;
use frame_support::{assert_err, assert_ok, traits::WithdrawReason};
use frame_support::{assert_err, assert_noop, assert_ok, error::BadOrigin, traits::WithdrawReason};
use mock::{ExtBuilder, Origin, PalletBalances, Runtime, System, TestEvent, Vesting, ALICE, BOB, CHARLIE};
use pallet_balances::{BalanceLock, Reasons};

Expand Down Expand Up @@ -188,6 +188,22 @@ fn vested_transfer_fails_if_overflow() {
});
}

#[test]
fn vested_transfer_fails_if_bad_origin() {
ExtBuilder::build().execute_with(|| {
let schedule = VestingSchedule {
start: 0u64,
period: 10u64,
period_count: 1u32,
per_period: 100u64,
};
assert_noop!(
Vesting::vested_transfer(Origin::signed(CHARLIE), BOB, schedule.clone()),
BadOrigin
);
});
}

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

0 comments on commit 2a55e07

Please sign in to comment.