Skip to content

Commit 2a55e07

Browse files
authored
Add VestedTransferOrigin. (#287)
1 parent b131157 commit 2a55e07

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

vesting/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use codec::{Decode, Encode, HasCompact};
3030
use frame_support::{
3131
decl_error, decl_event, decl_module, decl_storage, ensure,
32-
traits::{Currency, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
32+
traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons},
3333
weights::Weight,
3434
};
3535
use frame_system::{ensure_root, ensure_signed};
@@ -122,6 +122,9 @@ pub trait Trait: frame_system::Trait {
122122
/// The minimum amount transferred to call `vested_transfer`.
123123
type MinVestedTransfer: Get<BalanceOf<Self>>;
124124

125+
/// Required origin for vested transfer.
126+
type VestedTransferOrigin: EnsureOrigin<Self::Origin, Success = Self::AccountId>;
127+
125128
/// Weight information for extrinsics in this module.
126129
type WeightInfo: WeightInfo;
127130
}
@@ -220,7 +223,7 @@ decl_module! {
220223
dest: <T::Lookup as StaticLookup>::Source,
221224
schedule: VestingScheduleOf<T>,
222225
) {
223-
let from = ensure_signed(origin)?;
226+
let from = T::VestedTransferOrigin::ensure_origin(origin)?;
224227
let to = T::Lookup::lookup(dest)?;
225228
Self::do_vested_transfer(&from, &to, schedule.clone())?;
226229

vesting/src/mock.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![cfg(test)]
44

55
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
6+
use frame_system::RawOrigin;
67
use pallet_balances;
78
use sp_core::H256;
89
use sp_runtime::{testing::Header, traits::IdentityLookup, Perbill};
@@ -81,10 +82,29 @@ impl pallet_balances::Trait for Runtime {
8182
}
8283
pub type PalletBalances = pallet_balances::Module<Runtime>;
8384

85+
pub struct EnsureAliceOrBob;
86+
impl EnsureOrigin<Origin> for EnsureAliceOrBob {
87+
type Success = AccountId;
88+
89+
fn try_origin(o: Origin) -> Result<Self::Success, Origin> {
90+
Into::<Result<RawOrigin<AccountId>, Origin>>::into(o).and_then(|o| match o {
91+
RawOrigin::Signed(ALICE) => Ok(ALICE),
92+
RawOrigin::Signed(BOB) => Ok(BOB),
93+
r => Err(Origin::from(r)),
94+
})
95+
}
96+
97+
#[cfg(feature = "runtime-benchmarks")]
98+
fn successful_origin() -> Origin {
99+
Origin::from(RawOrigin::Signed(Default::default()))
100+
}
101+
}
102+
84103
impl Trait for Runtime {
85104
type Event = TestEvent;
86105
type Currency = PalletBalances;
87106
type MinVestedTransfer = MinVestedTransfer;
107+
type VestedTransferOrigin = EnsureAliceOrBob;
88108
type WeightInfo = ();
89109
}
90110
pub type Vesting = Module<Runtime>;

vesting/src/tests.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(test)]
44

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

@@ -188,6 +188,22 @@ fn vested_transfer_fails_if_overflow() {
188188
});
189189
}
190190

191+
#[test]
192+
fn vested_transfer_fails_if_bad_origin() {
193+
ExtBuilder::build().execute_with(|| {
194+
let schedule = VestingSchedule {
195+
start: 0u64,
196+
period: 10u64,
197+
period_count: 1u32,
198+
per_period: 100u64,
199+
};
200+
assert_noop!(
201+
Vesting::vested_transfer(Origin::signed(CHARLIE), BOB, schedule.clone()),
202+
BadOrigin
203+
);
204+
});
205+
}
206+
191207
#[test]
192208
fn claim_works() {
193209
ExtBuilder::build().execute_with(|| {

0 commit comments

Comments
 (0)