-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add manta-vesting pallet Co-authored-by: stechu <[email protected]> Co-authored-by: ghzlatarev <[email protected]>
- Loading branch information
1 parent
0273415
commit b3f646d
Showing
15 changed files
with
1,360 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[package] | ||
authors = ['Manta Network'] | ||
name = "calamari-vesting" | ||
version = '3.1.0' | ||
edition = "2018" | ||
homepage = 'https://manta.network' | ||
license = 'GPL-3.0' | ||
repository = 'https://github.com/Manta-Network/Manta/' | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "2.3.1", default-features = false, features = ["derive"] } | ||
scale-info = { version = "1.0", default-features = false, features = ["derive"] } | ||
frame-benchmarking = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12", optional = true } | ||
pallet-timestamp = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12", optional = true } | ||
pallet-balances = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12", optional = true } | ||
frame-support = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12" } | ||
frame-system = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12" } | ||
sp-runtime = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12" } | ||
sp-std = { git = 'https://github.com/paritytech/substrate.git', default-features = false, branch = "polkadot-v0.9.12" } | ||
|
||
[dev-dependencies] | ||
chrono = "0.4" | ||
pallet-balances = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.12" } | ||
pallet-timestamp = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.12" } | ||
sp-core = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.12" } | ||
sp-io = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.12" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"scale-info/std", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
] | ||
runtime-benchmarks = [ | ||
"frame-benchmarking", | ||
'frame-support/runtime-benchmarks', | ||
'frame-system/runtime-benchmarks', | ||
"pallet-timestamp/runtime-benchmarks", | ||
"pallet-balances/runtime-benchmarks", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2020-2021 Manta Network. | ||
// This file is part of Manta. | ||
// | ||
// Manta is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Manta is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Manta. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg(feature = "runtime-benchmarks")] | ||
|
||
extern crate alloc; | ||
|
||
use super::*; | ||
#[allow(unused_imports)] | ||
use crate::Pallet as CalamariVesting; | ||
use core::{convert::TryInto, ops::Div, time::Duration}; | ||
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller}; | ||
use frame_support::assert_ok; | ||
use frame_system::RawOrigin; | ||
use sp_runtime::{traits::AtLeast32BitUnsigned, SaturatedConversion}; | ||
|
||
const SEED: u32 = 0; | ||
// existential deposit multiplier | ||
const ED_MULTIPLIER: u32 = 100; | ||
|
||
fn assert_has_event<T: Config>(generic_event: <T as Config>::Event) { | ||
frame_system::Pallet::<T>::assert_has_event(generic_event.into()); | ||
} | ||
|
||
fn init_setup< | ||
T: Config + pallet_timestamp::Config<Moment = u64> + pallet_balances::Config<I>, | ||
I: 'static, | ||
>( | ||
caller: &T::AccountId, | ||
) where | ||
<T as pallet_balances::Config>::Balance: TryFrom<u128>, | ||
T: pallet_balances::Config, | ||
{ | ||
let now = Duration::from_secs(1636329600 - 1) | ||
.as_millis() | ||
.saturated_into::<u64>() | ||
+ 1; | ||
pallet_timestamp::Pallet::<T>::set_timestamp(now); | ||
|
||
let existential_deposit = <T as pallet_balances::Config<I>>::ExistentialDeposit::get(); | ||
let amount = existential_deposit.saturating_mul(ED_MULTIPLIER.into()); | ||
let source_caller = T::Lookup::unlookup(caller.clone()); | ||
let _ = pallet_balances::Pallet::<T, I>::make_free_balance_be(&caller, amount); | ||
|
||
assert_ok!(pallet_balances::Pallet::<T, I>::set_balance( | ||
RawOrigin::Root.into(), | ||
source_caller, | ||
amount, | ||
amount | ||
)); | ||
assert_eq!( | ||
pallet_balances::Pallet::<T, I>::free_balance(caller), | ||
amount | ||
); | ||
} | ||
|
||
benchmarks! { | ||
where_clause { | ||
where | ||
T: Config + pallet_timestamp::Config<Moment = u64> + pallet_balances::Config, | ||
BalanceOf<T>: AtLeast32BitUnsigned, | ||
<T as pallet_balances::Config>::Balance: From<u128>, | ||
} | ||
|
||
update_vesting_schedule { | ||
let new_schedule = BoundedVec::try_from( | ||
crate::Pallet::<T>::vesting_schedule() | ||
.iter() | ||
.map(|(_, s)| s + 1) | ||
.collect::<sp_std::vec::Vec<u64>>(), | ||
) | ||
.unwrap_or_default(); | ||
}: _(RawOrigin::Root, new_schedule.clone()) | ||
verify { | ||
assert_has_event::<T>(Event::VestingScheduleUpdated(new_schedule).into()); | ||
} | ||
|
||
vest { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
let recipient: T::AccountId = account("receiver", 0, SEED); | ||
let source_recipient = T::Lookup::unlookup(recipient.clone()); | ||
|
||
init_setup::<T, ()>(&caller); | ||
let existential_deposit = <T as pallet_balances::Config<()>>::ExistentialDeposit::get(); | ||
let unvested = existential_deposit.saturating_mul(ED_MULTIPLIER.div(10u32).into()).saturated_into::<u128>().try_into().ok().unwrap(); | ||
assert_ok!(crate::Pallet::<T>::vested_transfer(RawOrigin::Signed(caller.clone()).into(), source_recipient, unvested)); | ||
assert!(crate::Pallet::<T>::vesting_balance(&recipient).is_some()); | ||
|
||
let now = Duration::from_secs(1660694400) | ||
.as_millis() | ||
.saturated_into::<u64>() | ||
+ 1; | ||
pallet_timestamp::Pallet::<T>::set_timestamp(now); | ||
}: _(RawOrigin::Signed(recipient.clone())) | ||
verify { | ||
assert_has_event::<T>(Event::VestingCompleted(recipient).into()); | ||
} | ||
|
||
vested_transfer { | ||
let caller: T::AccountId = whitelisted_caller(); | ||
init_setup::<T, ()>(&caller); | ||
let existential_deposit = <T as pallet_balances::Config<()>>::ExistentialDeposit::get(); | ||
let unvested = existential_deposit.saturating_mul(ED_MULTIPLIER.div(10u32).into()).saturated_into::<u128>().try_into().ok().unwrap(); | ||
let recipient: T::AccountId = account("receiver", 0, SEED); | ||
let source_recipient = T::Lookup::unlookup(recipient.clone()); | ||
}: _(RawOrigin::Signed(caller.clone()), source_recipient, unvested) | ||
verify { | ||
assert_eq!(crate::Pallet::<T>::vesting_balance(&recipient), Some(unvested)); | ||
assert_has_event::<T>(Event::VestingUpdated(recipient, unvested).into()); | ||
} | ||
} | ||
|
||
impl_benchmark_test_suite!( | ||
CalamariVesting, | ||
crate::mock::ExtBuilder::default() | ||
.existential_deposit(1) | ||
.build(), | ||
crate::mock::Test, | ||
); |
Oops, something went wrong.