Skip to content
Snippets Groups Projects
Unverified Commit 2a55e07b authored by Shaopeng Wang's avatar Shaopeng Wang Committed by GitHub
Browse files

Add VestedTransferOrigin. (#287)

parent b1311575
No related branches found
No related tags found
No related merge requests found
......@@ -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};
......@@ -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;
}
......@@ -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())?;
......
......@@ -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};
......@@ -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>;
......
......@@ -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};
......@@ -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(|| {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment