From 8db56ad5ec0326bc832ab601d936f3c936083815 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 16 Sep 2024 20:43:02 +0300 Subject: [PATCH] Remove vesting pallet --- Cargo.lock | 18 - Dockerfile-bootstrap-node | 1 - Dockerfile-bootstrap-node.aarch64 | 1 - Dockerfile-farmer | 1 - Dockerfile-farmer.aarch64 | 1 - Dockerfile-node | 1 - Dockerfile-node.aarch64 | 1 - Dockerfile-runtime | 1 - README.md | 1 - .../src/chain_spec.rs | 6 +- crates/subspace-node/src/chain_spec.rs | 132 +--- crates/subspace-runtime/Cargo.toml | 5 +- crates/subspace-runtime/src/lib.rs | 19 +- orml/README.md | 3 - orml/vesting/Cargo.toml | 46 -- orml/vesting/README.md | 13 - orml/vesting/src/default_weight.rs | 27 - orml/vesting/src/lib.rs | 411 ------------ orml/vesting/src/mock.rs | 129 ---- orml/vesting/src/tests.rs | 603 ------------------ orml/vesting/src/weights.rs | 58 -- rustfmt.toml | 4 - test/subspace-test-client/src/chain_spec.rs | 9 +- test/subspace-test-runtime/Cargo.toml | 2 - test/subspace-test-runtime/src/lib.rs | 18 - 25 files changed, 8 insertions(+), 1503 deletions(-) delete mode 100644 orml/README.md delete mode 100644 orml/vesting/Cargo.toml delete mode 100644 orml/vesting/README.md delete mode 100644 orml/vesting/src/default_weight.rs delete mode 100644 orml/vesting/src/lib.rs delete mode 100644 orml/vesting/src/mock.rs delete mode 100644 orml/vesting/src/tests.rs delete mode 100644 orml/vesting/src/weights.rs diff --git a/Cargo.lock b/Cargo.lock index ceda2ba65eb..a14b89a5346 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7548,22 +7548,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" -[[package]] -name = "orml-vesting" -version = "0.9.1" -dependencies = [ - "frame-support", - "frame-system", - "pallet-balances", - "parity-scale-codec", - "scale-info", - "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", -] - [[package]] name = "overload" version = "0.1.1" @@ -12989,7 +12973,6 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "hex-literal", - "orml-vesting", "pallet-balances", "pallet-collective", "pallet-democracy", @@ -13198,7 +13181,6 @@ dependencies = [ "frame-support", "frame-system", "frame-system-rpc-runtime-api", - "orml-vesting", "pallet-balances", "pallet-domains", "pallet-messenger", diff --git a/Dockerfile-bootstrap-node b/Dockerfile-bootstrap-node index 088ee5af071..0d04e63ebe8 100644 --- a/Dockerfile-bootstrap-node +++ b/Dockerfile-bootstrap-node @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-bootstrap-node.aarch64 b/Dockerfile-bootstrap-node.aarch64 index 9b715e14aba..97c5f29c561 100644 --- a/Dockerfile-bootstrap-node.aarch64 +++ b/Dockerfile-bootstrap-node.aarch64 @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-farmer b/Dockerfile-farmer index 81c554f9c6a..3ae020411c2 100644 --- a/Dockerfile-farmer +++ b/Dockerfile-farmer @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-farmer.aarch64 b/Dockerfile-farmer.aarch64 index 0fc6d467cbf..46dfbff8e2a 100644 --- a/Dockerfile-farmer.aarch64 +++ b/Dockerfile-farmer.aarch64 @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-node b/Dockerfile-node index a72596b9409..1b110cb1624 100644 --- a/Dockerfile-node +++ b/Dockerfile-node @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-node.aarch64 b/Dockerfile-node.aarch64 index 620e955d36a..d3d5034ee03 100644 --- a/Dockerfile-node.aarch64 +++ b/Dockerfile-node.aarch64 @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/Dockerfile-runtime b/Dockerfile-runtime index b0afc621fa3..cad151932f1 100644 --- a/Dockerfile-runtime +++ b/Dockerfile-runtime @@ -33,7 +33,6 @@ COPY rust-toolchain.toml /code/rust-toolchain.toml COPY crates /code/crates COPY domains /code/domains -COPY orml /code/orml COPY shared /code/shared COPY test /code/test diff --git a/README.md b/README.md index 5a75a79c8c1..399f9fca8e2 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ The structure of this repository is the following: - `subspace-node` is an implementation of the node for Subspace protocol - `subspace-farmer` is a CLI farmer app - `domains` contains client and runtime code for decoupled execution and domains -- `orml` contains a fork of orml vesting pallet with modified dependencies ## How to run diff --git a/crates/subspace-malicious-operator/src/chain_spec.rs b/crates/subspace-malicious-operator/src/chain_spec.rs index b90bc008d01..2782aef3d26 100644 --- a/crates/subspace-malicious-operator/src/chain_spec.rs +++ b/crates/subspace-malicious-operator/src/chain_spec.rs @@ -15,7 +15,7 @@ use std::num::NonZeroU32; use subspace_runtime::{ AllowAuthoringBy, CouncilConfig, DemocracyConfig, DomainsConfig, EnableRewardsAt, HistorySeedingConfig, MaxDomainBlockSize, MaxDomainBlockWeight, RewardsConfig, - RuntimeConfigsConfig, SubspaceConfig, VestingConfig, + RuntimeConfigsConfig, SubspaceConfig, }; use subspace_runtime_primitives::{ AccountId, Balance, BlockNumber, CouncilDemocracyConfigParams, SSC, @@ -172,7 +172,6 @@ pub fn dev_config() -> Result { (get_account_id_from_seed("Alice//stash"), 1_000 * SSC), (get_account_id_from_seed("Bob//stash"), 1_000 * SSC), ], - vec![], GenesisParams { enable_rewards_at: EnableRewardsAt::Manually, allow_authoring_by: AllowAuthoringBy::Anyone, @@ -206,8 +205,6 @@ pub fn dev_config() -> Result { fn subspace_genesis_config( sudo_account: AccountId, balances: Vec<(AccountId, Balance)>, - // who, start, period, period_count, per_period - vesting: Vec<(AccountId, BlockNumber, BlockNumber, u32, Balance)>, genesis_params: GenesisParams, genesis_domain_params: GenesisDomainParams, ) -> subspace_runtime::RuntimeGenesisConfig { @@ -238,7 +235,6 @@ fn subspace_genesis_config( phantom: PhantomData, }, rewards: rewards_config, - vesting: VestingConfig { vesting }, council: CouncilConfig::default(), democracy: DemocracyConfig::default(), runtime_configs: RuntimeConfigsConfig { diff --git a/crates/subspace-node/src/chain_spec.rs b/crates/subspace-node/src/chain_spec.rs index 58f0ba4767c..a2bda440bd9 100644 --- a/crates/subspace-node/src/chain_spec.rs +++ b/crates/subspace-node/src/chain_spec.rs @@ -35,62 +35,14 @@ use subspace_runtime::{ AllowAuthoringBy, BalancesConfig, CouncilConfig, DemocracyConfig, DomainsConfig, EnableRewardsAt, HistorySeedingConfig, MaxDomainBlockSize, MaxDomainBlockWeight, RewardsConfig, RuntimeConfigsConfig, RuntimeGenesisConfig, SubspaceConfig, SudoConfig, SystemConfig, - VestingConfig, WASM_BINARY, + WASM_BINARY, }; -use subspace_runtime_primitives::time::MILLISECS_PER_BLOCK; use subspace_runtime_primitives::{ AccountId, Balance, BlockNumber, CouncilDemocracyConfigParams, SSC, }; const SUBSPACE_TELEMETRY_URL: &str = "wss://telemetry.subspace.network/submit/"; -/// List of accounts which should receive token grants, amounts are specified in SSC. -const TOKEN_GRANTS: &[(&str, u128)] = &[ - ( - "5Dns1SVEeDqnbSm2fVUqHJPCvQFXHVsgiw28uMBwmuaoKFYi", - 2_000_000, - ), - ( - "5DxtHHQL9JGapWCQARYUAWj4yDcwuhg9Hsk5AjhEzuzonVyE", - 1_000_000, - ), - ("5EHhw9xuQNdwieUkNoucq2YcateoMVJQdN8EZtmRy3roQkVK", 69_427), - ("5GBWVfJ253YWVPHzWDTos1nzYZpa9TemP7FpQT9RnxaFN6Sz", 167_708), - ("5F9tEPid88uAuGbjpyegwkrGdkXXtaQ9sGSWEnYrfVCUCsen", 111_111), - ("5DkJFCv3cTBsH5y1eFT94DXMxQ3EmVzYojEA88o56mmTKnMp", 244_444), - ("5G23o1yxWgVNQJuL4Y9UaCftAFvLuMPCRe7BCARxCohjoHc9", 174_994), - ("5D9pNnGCiZ9UqhBQn5n71WFVaRLvZ7znsMvcZ7PHno4zsiYa", 337_500), - ("5H2Kq1qWrisf7aXUvdGrQB9j9zhiGt6MdaGSSBpFCwynBT9p", 13_834), - ("5Ci12WM1YqPjSAMNubucNejuSqwChfRSKDpFfFhtshomNSG1", 250_000), - ("5FAS1mdyp1yomAzJaJ74ZgJbzicQmZ8ajRyxPZ2x4wseGkY2", 104_175), - ("5E4vk2Ant4y6KiKoGMezrhhFwSanspjh8Fxa9HmWmjWrFyry", 66_700), - ("5GsCx12U1zMu7bMZHXjb1rhMFR8YK9VUj6hQHWyaw1ReYt8D", 33_333), - ("5F72mz79TjkWQEjuefPCMabFarGVLvW4haPTYsrzewxrbuD7", 12_222), - ("5Fn9BF7pyiefhAwanXFyW4T5sXNQGJ9kzLAR1DpF8iYmc7aw", 6_667), - ("5CdMyLvrxdTNTVZYAgN9NCQbNmwYW32vojsBZZfkEcxYbjUR", 33_333), - ("5Fgjk1nMYCEcoP9QvjMDVzDjBzJfo5X2ZssSbWn5PesfyPJh", 100_000), - ("5CUutLkRAMr14dsqFzRFgByD6gv9U8iqL67CZ7huxXtoXKdB", 22_222), - ("5EqPLjjBob7Y6FCUMKMPfgQyb2BZ8y2CcNVQrZ5wSF3aDpTX", 3_333), - ("5HKZUKYjQQ8H47z1HchLgLWZ8EfguFDDqh2KJqxBLoUggtCp", 9_167), - ("5D7E29Ut5P5RDczpakVSVvTV3vEh6v5B3oofEzcJ2xKUks78", 12_473), - ("5DRUS33oYrkPjM8SpLDKPiNG8R4sHvvZ8R2QZTcSgSCByjyR", 17_778), - ("5H6d5Wh5tmbrksPbHyoaonYCF7u71YuBRL7e8a8mHsphxxbT", 10_417), - ("5CXYUjQv42aYhbdCL98QgKP82RyxPGEvHJZw9yBEJ5CE53um", 100_000), - ("5FsxXZCHHcRUhek6whnEMkgXuumeWVVn8SFeUVhGNGdKzq6e", 9_583), - ("5GhHwuJoK1b7uUg5oi8qUXxWHdfgzv6P5CQSdJ3ffrnPRgKM", 567_378), - ("5DydwBX2uLjnVKjg1zAWS3z27ukbr99PiXteQSg96bb1k6p7", 190_000), - ("5FND87MkPEVvwMP3sq88n1MFxHuLDrHkBdCNeuc23ibjHME4", 288_889), - ("5G4BCrTj6xZHkTwFtPmK4sjNEXc8w12ZjLLU8awsb5CDBz4d", 260_000), - ("5GW7F86K47JArVGB5eSoUHoA9WADAxwts7P9yicAmQnf6cmK", 137_500), - ("5DXfPcXUcP4BG8LBSkJDrfFNApxjWySR6ARfgh3v27hdYr5S", 440_000), - ("5CXSdDJgzRTj54f9raHN2Z5BNPSMa2ETjqCTUmpaw3ECmwm4", 330_000), - ("5DqKxL7bQregQmUfFgzTMfRKY4DSvA1KgHuurZWYmxYSCmjY", 200_000), - ("5CfixiS93yTwHQbzzfn8P2tMxhKXdTx7Jam9htsD7XtiMFtn", 27_800), - ("5FZe9YzXeEXe7sK5xLR8yCmbU8bPJDTZpNpNbToKvSJBUiEo", 18_067), - ("5FZwEgsvZz1vpeH7UsskmNmTpbfXvAcojjgVfShgbRqgC1nx", 27_800), - ("5EqBwtqrCV427xCtTsxnb9X2Qay39pYmKNk9wD9Kd62jLS97", 75_000), -]; - /// Additional subspace specific genesis parameters. struct GenesisParams { enable_rewards_at: EnableRewardsAt, @@ -136,48 +88,11 @@ pub fn gemini_3h_compiled() -> Result { AccountId::from_ss58check("5DNwQTHfARgKoa2NdiUM51ZUow7ve5xG9S2yYdSbVQcnYxBA") .expect("Wrong root account address"); - let mut balances = vec![(sudo_account.clone(), 1_000 * SSC)]; - let vesting_schedules = TOKEN_GRANTS - .iter() - .flat_map(|&(account_address, amount)| { - let account_id = AccountId::from_ss58check(account_address) - .expect("Wrong vesting account address"); - let amount: Balance = amount * SSC; - - // TODO: Adjust start block to real value before mainnet launch - let start_block = 100_000_000; - let one_month_in_blocks = - u32::try_from(3600 * 24 * 30 * MILLISECS_PER_BLOCK / 1000) - .expect("One month of blocks always fits in u32; qed"); - - // Add balance so it can be locked - balances.push((account_id.clone(), amount)); - - [ - // 1/4 of tokens are released after 1 year. - ( - account_id.clone(), - start_block, - one_month_in_blocks * 12, - 1, - amount / 4, - ), - // 1/48 of tokens are released every month after that for 3 more years. - ( - account_id, - start_block + one_month_in_blocks * 12, - one_month_in_blocks, - 36, - amount / 48, - ), - ] - }) - .collect::>(); + let balances = vec![(sudo_account.clone(), 1_000 * SSC)]; patch_domain_runtime_version( serde_json::to_value(subspace_genesis_config( sudo_account.clone(), balances, - vesting_schedules, GenesisParams { enable_rewards_at: EnableRewardsAt::Manually, allow_authoring_by: AllowAuthoringBy::RootFarmer( @@ -257,48 +172,11 @@ pub fn devnet_config_compiled() -> Result { AccountId::from_ss58check("5CXTmJEusve5ixyJufqHThmy4qUrrm6FyLCR7QfE4bbyMTNC") .expect("Wrong root account address"); - let mut balances = vec![(sudo_account.clone(), Balance::MAX / 2)]; - let vesting_schedules = TOKEN_GRANTS - .iter() - .flat_map(|&(account_address, amount)| { - let account_id = AccountId::from_ss58check(account_address) - .expect("Wrong vesting account address"); - let amount: Balance = amount * SSC; - - // TODO: Adjust start block to real value before mainnet launch - let start_block = 100_000_000; - let one_month_in_blocks = - u32::try_from(3600 * 24 * 30 * MILLISECS_PER_BLOCK / 1000) - .expect("One month of blocks always fits in u32; qed"); - - // Add balance so it can be locked - balances.push((account_id.clone(), amount)); - - [ - // 1/4 of tokens are released after 1 year. - ( - account_id.clone(), - start_block, - one_month_in_blocks * 12, - 1, - amount / 4, - ), - // 1/48 of tokens are released every month after that for 3 more years. - ( - account_id, - start_block + one_month_in_blocks * 12, - one_month_in_blocks, - 36, - amount / 48, - ), - ] - }) - .collect::>(); + let balances = vec![(sudo_account.clone(), Balance::MAX / 2)]; patch_domain_runtime_version( serde_json::to_value(subspace_genesis_config( sudo_account.clone(), balances, - vesting_schedules, GenesisParams { enable_rewards_at: EnableRewardsAt::Manually, allow_authoring_by: AllowAuthoringBy::FirstFarmer, @@ -362,7 +240,6 @@ pub fn dev_config() -> Result { (get_account_id_from_seed("Alice//stash"), 1_000 * SSC), (get_account_id_from_seed("Bob//stash"), 1_000 * SSC), ], - vec![], GenesisParams { enable_rewards_at: EnableRewardsAt::Manually, allow_authoring_by: AllowAuthoringBy::Anyone, @@ -399,8 +276,6 @@ pub fn dev_config() -> Result { fn subspace_genesis_config( sudo_account: AccountId, balances: Vec<(AccountId, Balance)>, - // who, start, period, period_count, per_period - vesting: Vec<(AccountId, BlockNumber, BlockNumber, u32, Balance)>, genesis_params: GenesisParams, genesis_domain_params: GenesisDomainParams, council_democracy_config_params: CouncilDemocracyConfigParams, @@ -463,7 +338,6 @@ fn subspace_genesis_config( phantom: PhantomData, }, rewards: rewards_config, - vesting: VestingConfig { vesting }, council: CouncilConfig::default(), democracy: DemocracyConfig::default(), runtime_configs: RuntimeConfigsConfig { diff --git a/crates/subspace-runtime/Cargo.toml b/crates/subspace-runtime/Cargo.toml index e38f128da68..647d0641bf7 100644 --- a/crates/subspace-runtime/Cargo.toml +++ b/crates/subspace-runtime/Cargo.toml @@ -24,12 +24,11 @@ frame-support = { default-features = false, git = "https://github.com/subspace/p frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } frame-system-benchmarking = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", optional = true } frame-system-rpc-runtime-api = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -orml-vesting = { version = "0.9.1", default-features = false, path = "../../orml/vesting" } pallet-balances = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } pallet-collective = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } pallet-democracy = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } pallet-domains = { version = "0.1.0", default-features = false, path = "../pallet-domains" } -pallet-history-seeding = { version = "0.1.0", default-features = false,path = "../pallet-history-seeding" } +pallet-history-seeding = { version = "0.1.0", default-features = false, path = "../pallet-history-seeding" } pallet-messenger = { version = "0.1.0", path = "../../domains/pallets/messenger", default-features = false } pallet-mmr = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } pallet-offences-subspace = { version = "0.1.0", default-features = false, path = "../pallet-offences-subspace" } @@ -90,7 +89,6 @@ std = [ "frame-system/std", "frame-system-benchmarking?/std", "frame-system-rpc-runtime-api/std", - "orml-vesting/std", "pallet-balances/std", "pallet-collective/std", "pallet-democracy/std", @@ -146,7 +144,6 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "frame-system-benchmarking", "frame-system-benchmarking/runtime-benchmarks", - "orml-vesting/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-collective/runtime-benchmarks", "pallet-domains/runtime-benchmarks", diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index afac99018cb..b51d6177969 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -52,7 +52,7 @@ use frame_support::weights::constants::ParityDbWeight; use frame_support::weights::{ConstantMultiplier, Weight}; use frame_support::{construct_runtime, parameter_types, PalletId}; use frame_system::limits::{BlockLength, BlockWeights}; -use frame_system::{EnsureNever, EnsureRoot}; +use frame_system::EnsureRoot; use pallet_collective::{EnsureMember, EnsureProportionAtLeast}; pub use pallet_rewards::RewardPoint; pub use pallet_subspace::{AllowAuthoringBy, EnableRewardsAt}; @@ -887,21 +887,6 @@ impl pallet_runtime_configs::Config for Runtime { type WeightInfo = pallet_runtime_configs::weights::SubstrateWeight; } -parameter_types! { - // This value doesn't matter, we don't use it (`VestedTransferOrigin = EnsureNever` below). - pub const MinVestedTransfer: Balance = 0; -} - -impl orml_vesting::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Currency = Balances; - type MinVestedTransfer = MinVestedTransfer; - type VestedTransferOrigin = EnsureNever; - type WeightInfo = (); - type MaxVestingSchedules = ConstU32<2>; - type BlockNumberProvider = System; -} - mod mmr { use super::Runtime; pub use pallet_mmr::primitives::*; @@ -954,8 +939,6 @@ construct_runtime!( Domains: pallet_domains = 12, RuntimeConfigs: pallet_runtime_configs = 14, - Vesting: orml_vesting = 13, - Mmr: pallet_mmr = 30, SubspaceMmr: pallet_subspace_mmr = 31, diff --git a/orml/README.md b/orml/README.md deleted file mode 100644 index 0dea4e40915..00000000000 --- a/orml/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This directory contains forks of crates from [orml](https://github.com/subspace/open-runtime-module-library) to simplify Substrate upgrades for Subspace. - -The only changes are paths to dependencies. diff --git a/orml/vesting/Cargo.toml b/orml/vesting/Cargo.toml deleted file mode 100644 index 387e7a4419a..00000000000 --- a/orml/vesting/Cargo.toml +++ /dev/null @@ -1,46 +0,0 @@ -[package] -name = "orml-vesting" -description = "Provides scheduled balance locking mechanism, in a *graded vesting* way." -repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/vesting" -license = "Apache-2.0" -version = "0.9.1" -authors = ["Laminar Developers "] -edition = "2021" - -[dependencies] -parity-scale-codec = { version = "3.6.12", default-features = false, features = ["max-encoded-len"] } -scale-info = { version = "2.11.2", default-features = false, features = ["derive"] } -serde = { version = "1.0.206", optional = true } - -frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -sp-io = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } - -[dev-dependencies] -pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -sp-core = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } - -[features] -default = ["std"] -std = [ - "frame-support/std", - "frame-system/std", - "parity-scale-codec/std", - "scale-info/std", - "serde", - "sp-io/std", - "sp-runtime/std", - "sp-std/std", -] -runtime-benchmarks = [ - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", -] -try-runtime = [ - "frame-support/try-runtime", - "frame-system/try-runtime", - "sp-runtime/try-runtime", -] diff --git a/orml/vesting/README.md b/orml/vesting/README.md deleted file mode 100644 index 1ec1fcc8d11..00000000000 --- a/orml/vesting/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Vesting Module - -## Overview - -Vesting module provides a means of scheduled balance lock on an account. It uses the *graded vesting* way, which unlocks a specific amount of balance every period of time, until all balance unlocked. - -### Vesting Schedule - -The schedule of a vesting is described by data structure `VestingSchedule`: from the block number of `start`, for every `period` amount of blocks, `per_period` amount of balance would unlocked, until number of periods `period_count` reached. Note in vesting schedules, *time* is measured by block number. All `VestingSchedule`s under an account could be queried in chain state. - -### Locks - -The implementation uses locks which allow tokens to be locked by other pallets that's also using locks, for example, the conviction-voting pallet. diff --git a/orml/vesting/src/default_weight.rs b/orml/vesting/src/default_weight.rs deleted file mode 100644 index d5cffd1aecf..00000000000 --- a/orml/vesting/src/default_weight.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0 - -#![allow(unused_parens)] -#![allow(unused_imports)] -#![allow(clippy::unnecessary_cast)] - -use frame_support::weights::{constants::ParityDbWeight as DbWeight, Weight}; - -impl crate::WeightInfo for () { - fn vested_transfer() -> Weight { - Weight::from_parts(310_862_000, 0) - .saturating_add(DbWeight::get().reads(4 as u64)) - .saturating_add(DbWeight::get().writes(4 as u64)) - } - fn claim(i: u32) -> Weight { - Weight::from_parts(158_614_000, 0) - .saturating_add(Weight::from_parts(958_000, 0).saturating_mul(i as u64)) - .saturating_add(DbWeight::get().reads(3 as u64)) - .saturating_add(DbWeight::get().writes(3 as u64)) - } - fn update_vesting_schedules(i: u32) -> Weight { - Weight::from_parts(119_811_000, 0) - .saturating_add(Weight::from_parts(2_320_000, 0).saturating_mul(i as u64)) - .saturating_add(DbWeight::get().reads(2 as u64)) - .saturating_add(DbWeight::get().writes(3 as u64)) - } -} diff --git a/orml/vesting/src/lib.rs b/orml/vesting/src/lib.rs deleted file mode 100644 index 0490be5edf3..00000000000 --- a/orml/vesting/src/lib.rs +++ /dev/null @@ -1,411 +0,0 @@ -//! # Vesting Module -//! -//! ## Overview -//! -//! Vesting module provides a means of scheduled balance lock on an account. It -//! uses the *graded vesting* way, which unlocks a specific amount of balance -//! every period of time, until all balance unlocked. -//! -//! ### Vesting Schedule -//! -//! The schedule of a vesting is described by data structure `VestingSchedule`: -//! from the block number of `start`, for every `period` amount of blocks, -//! `per_period` amount of balance would unlocked, until number of periods -//! `period_count` reached. Note in vesting schedules, *time* is measured by -//! block number. All `VestingSchedule`s under an account could be queried in -//! chain state. -//! -//! ## Interface -//! -//! ### Dispatchable Functions -//! -//! - `vested_transfer` - Add a new vesting schedule for an account. -//! - `claim` - Claim unlocked balances. -//! - `update_vesting_schedules` - Update all vesting schedules under an -//! account, `root` origin required. - -#![cfg_attr(not(feature = "std"), no_std)] -#![allow(clippy::unused_unit)] - -use frame_support::{ - ensure, - pallet_prelude::*, - traits::{Currency, EnsureOrigin, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, WithdrawReasons}, - BoundedVec, -}; -use frame_system::{ensure_root, ensure_signed, pallet_prelude::*}; -use parity_scale_codec::{HasCompact, MaxEncodedLen}; -use scale_info::TypeInfo; -use sp_runtime::{ - traits::{AtLeast32Bit, BlockNumberProvider, CheckedAdd, Saturating, StaticLookup, Zero}, - ArithmeticError, DispatchResult, RuntimeDebug, -}; -use sp_std::{ - cmp::{Eq, PartialEq}, - vec::Vec, -}; - -mod mock; -mod tests; -mod weights; - -pub use module::*; -pub use weights::WeightInfo; - -pub const VESTING_LOCK_ID: LockIdentifier = *b"ormlvest"; - -/// The vesting schedule. -/// -/// Benefits would be granted gradually, `per_period` amount every `period` -/// of blocks after `start`. -#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)] -pub struct VestingSchedule { - /// Vesting starting block - pub start: BlockNumber, - /// Number of blocks between vest - pub period: BlockNumber, - /// Number of vest - pub period_count: u32, - /// Amount of tokens to release per vest - #[codec(compact)] - pub per_period: Balance, -} - -impl - VestingSchedule -{ - /// Returns the end of all periods, `None` if calculation overflows. - pub fn end(&self) -> Option { - // period * period_count + start - self.period - .checked_mul(&self.period_count.into())? - .checked_add(&self.start) - } - - /// Returns all locked amount, `None` if calculation overflows. - pub fn total_amount(&self) -> Option { - self.per_period.checked_mul(&self.period_count.into()) - } - - /// Returns locked amount for a given `time`. - /// - /// Note this func assumes schedule is a valid one(non-zero period and - /// non-overflow total amount), and it should be guaranteed by callers. - pub fn locked_amount(&self, time: BlockNumber) -> Balance { - // full = (time - start) / period - // unrealized = period_count - full - // per_period * unrealized - let full = time - .saturating_sub(self.start) - .checked_div(&self.period) - .expect("ensured non-zero period; qed"); - let unrealized = self.period_count.saturating_sub(full.unique_saturated_into()); - self.per_period - .checked_mul(&unrealized.into()) - .expect("ensured non-overflow total amount; qed") - } -} - -#[frame_support::pallet] -pub mod module { - use super::*; - - pub(crate) type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; - pub(crate) type VestingScheduleOf = VestingSchedule, BalanceOf>; - pub type ScheduledItem = ( - ::AccountId, - BlockNumberFor, - BlockNumberFor, - u32, - BalanceOf, - ); - - #[pallet::config] - pub trait Config: frame_system::Config { - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - - type Currency: LockableCurrency>; - - #[pallet::constant] - /// The minimum amount transferred to call `vested_transfer`. - type MinVestedTransfer: Get>; - - /// Required origin for vested transfer. - type VestedTransferOrigin: EnsureOrigin; - - /// Weight information for extrinsics in this module. - type WeightInfo: WeightInfo; - - /// The maximum vesting schedules - type MaxVestingSchedules: Get; - - // The block number provider - type BlockNumberProvider: BlockNumberProvider>; - } - - #[pallet::error] - pub enum Error { - /// Vesting period is zero - ZeroVestingPeriod, - /// Number of vests is zero - ZeroVestingPeriodCount, - /// Insufficient amount of balance to lock - InsufficientBalanceToLock, - /// This account have too many vesting schedules - TooManyVestingSchedules, - /// The vested transfer amount is too low - AmountLow, - /// Failed because the maximum vesting schedules was exceeded - MaxVestingSchedulesExceeded, - } - - #[pallet::event] - #[pallet::generate_deposit(fn deposit_event)] - pub enum Event { - /// Added new vesting schedule. - VestingScheduleAdded { - from: T::AccountId, - to: T::AccountId, - vesting_schedule: VestingScheduleOf, - }, - /// Claimed vesting. - Claimed { who: T::AccountId, amount: BalanceOf }, - /// Updated vesting schedules. - VestingSchedulesUpdated { who: T::AccountId }, - } - - /// Vesting schedules of an account. - /// - /// VestingSchedules: map AccountId => Vec - #[pallet::storage] - #[pallet::getter(fn vesting_schedules)] - pub type VestingSchedules = StorageMap< - _, - Blake2_128Concat, - T::AccountId, - BoundedVec, T::MaxVestingSchedules>, - ValueQuery, - >; - - #[pallet::genesis_config] - pub struct GenesisConfig { - pub vesting: Vec>, - } - - impl Default for GenesisConfig { - fn default() -> Self { - GenesisConfig { - vesting: Default::default(), - } - } - } - - #[pallet::genesis_build] - impl BuildGenesisConfig for GenesisConfig { - fn build(&self) { - self.vesting - .iter() - .for_each(|(who, start, period, period_count, per_period)| { - let mut bounded_schedules = VestingSchedules::::get(who); - bounded_schedules - .try_push(VestingSchedule { - start: *start, - period: *period, - period_count: *period_count, - per_period: *per_period, - }) - .expect("Max vesting schedules exceeded"); - let total_amount = bounded_schedules - .iter() - .try_fold::<_, _, Result, DispatchError>>(Zero::zero(), |acc_amount, schedule| { - let amount = ensure_valid_vesting_schedule::(schedule)?; - acc_amount - .checked_add(&amount) - .ok_or_else(|| ArithmeticError::Overflow.into()) - }) - .expect("Invalid vesting schedule"); - - assert!( - T::Currency::free_balance(who) >= total_amount, - "Account do not have enough balance" - ); - - T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, WithdrawReasons::all()); - VestingSchedules::::insert(who, bounded_schedules); - }); - } - } - - #[pallet::pallet] - pub struct Pallet(_); - - #[pallet::hooks] - impl Hooks> for Pallet {} - - #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::claim(::MaxVestingSchedules::get() / 2))] - pub fn claim(origin: OriginFor) -> DispatchResult { - let who = ensure_signed(origin)?; - let locked_amount = Self::do_claim(&who); - - Self::deposit_event(Event::Claimed { - who, - amount: locked_amount, - }); - Ok(()) - } - - #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::vested_transfer())] - pub fn vested_transfer( - origin: OriginFor, - dest: ::Source, - schedule: VestingScheduleOf, - ) -> 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::::InsufficientBalanceToLock, - ); - } - - Self::do_vested_transfer(&from, &to, schedule.clone())?; - - Self::deposit_event(Event::VestingScheduleAdded { - from, - to, - vesting_schedule: schedule, - }); - Ok(()) - } - - #[pallet::call_index(2)] - #[pallet::weight(T::WeightInfo::update_vesting_schedules(vesting_schedules.len() as u32))] - pub fn update_vesting_schedules( - origin: OriginFor, - who: ::Source, - vesting_schedules: Vec>, - ) -> DispatchResult { - ensure_root(origin)?; - - let account = T::Lookup::lookup(who)?; - Self::do_update_vesting_schedules(&account, vesting_schedules)?; - - Self::deposit_event(Event::VestingSchedulesUpdated { who: account }); - Ok(()) - } - - #[pallet::call_index(3)] - #[pallet::weight(T::WeightInfo::claim(::MaxVestingSchedules::get() / 2))] - pub fn claim_for(origin: OriginFor, dest: ::Source) -> DispatchResult { - let _ = ensure_signed(origin)?; - let who = T::Lookup::lookup(dest)?; - let locked_amount = Self::do_claim(&who); - - Self::deposit_event(Event::Claimed { - who, - amount: locked_amount, - }); - Ok(()) - } - } -} - -impl Pallet { - fn do_claim(who: &T::AccountId) -> BalanceOf { - let locked = Self::locked_balance(who); - if locked.is_zero() { - // cleanup the storage and unlock the fund - >::remove(who); - T::Currency::remove_lock(VESTING_LOCK_ID, who); - } else { - T::Currency::set_lock(VESTING_LOCK_ID, who, locked, WithdrawReasons::all()); - } - locked - } - - /// Returns locked balance based on current block number. - fn locked_balance(who: &T::AccountId) -> BalanceOf { - let now = T::BlockNumberProvider::current_block_number(); - >::mutate_exists(who, |maybe_schedules| { - let total = if let Some(schedules) = maybe_schedules.as_mut() { - let mut total: BalanceOf = Zero::zero(); - schedules.retain(|s| { - let amount = s.locked_amount(now); - total = total.saturating_add(amount); - !amount.is_zero() - }); - total - } else { - Zero::zero() - }; - if total.is_zero() { - *maybe_schedules = None; - } - total - }) - } - - fn do_vested_transfer(from: &T::AccountId, to: &T::AccountId, schedule: VestingScheduleOf) -> DispatchResult { - let schedule_amount = ensure_valid_vesting_schedule::(&schedule)?; - - let total_amount = Self::locked_balance(to) - .checked_add(&schedule_amount) - .ok_or(ArithmeticError::Overflow)?; - - T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?; - T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all()); - >::try_append(to, schedule).map_err(|_| Error::::MaxVestingSchedulesExceeded)?; - Ok(()) - } - - fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec>) -> DispatchResult { - let bounded_schedules: BoundedVec, T::MaxVestingSchedules> = schedules - .try_into() - .map_err(|_| Error::::MaxVestingSchedulesExceeded)?; - - // empty vesting schedules cleanup the storage and unlock the fund - if bounded_schedules.len().is_zero() { - >::remove(who); - T::Currency::remove_lock(VESTING_LOCK_ID, who); - return Ok(()); - } - - let total_amount = bounded_schedules - .iter() - .try_fold::<_, _, Result, DispatchError>>(Zero::zero(), |acc_amount, schedule| { - let amount = ensure_valid_vesting_schedule::(schedule)?; - acc_amount - .checked_add(&amount) - .ok_or_else(|| ArithmeticError::Overflow.into()) - })?; - ensure!( - T::Currency::free_balance(who) >= total_amount, - Error::::InsufficientBalanceToLock, - ); - - T::Currency::set_lock(VESTING_LOCK_ID, who, total_amount, WithdrawReasons::all()); - >::insert(who, bounded_schedules); - - Ok(()) - } -} - -/// Returns `Ok(total_total)` if valid schedule, or error. -fn ensure_valid_vesting_schedule(schedule: &VestingScheduleOf) -> Result, DispatchError> { - ensure!(!schedule.period.is_zero(), Error::::ZeroVestingPeriod); - ensure!(!schedule.period_count.is_zero(), Error::::ZeroVestingPeriodCount); - ensure!(schedule.end().is_some(), ArithmeticError::Overflow); - - let total_total = schedule.total_amount().ok_or(ArithmeticError::Overflow)?; - - ensure!(total_total >= T::MinVestedTransfer::get(), Error::::AmountLow); - - Ok(total_total) -} diff --git a/orml/vesting/src/mock.rs b/orml/vesting/src/mock.rs deleted file mode 100644 index 8bd5be12ee6..00000000000 --- a/orml/vesting/src/mock.rs +++ /dev/null @@ -1,129 +0,0 @@ -//! Mocks for the vesting module. - -#![cfg(test)] - -use super::*; -use frame_support::{ - construct_runtime, derive_impl, parameter_types, - traits::{ConstU64}, -}; -use frame_system::RawOrigin; -use sp_runtime::{traits::IdentityLookup, BuildStorage}; - -use crate as vesting; - -pub type AccountId = u128; - -#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] -impl frame_system::Config for Runtime { - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Block = Block; - type AccountData = pallet_balances::AccountData; -} - -type Balance = u64; - -impl pallet_balances::Config for Runtime { - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ConstU64<1>; - type AccountStore = frame_system::Pallet; - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type WeightInfo = (); - type RuntimeHoldReason = RuntimeHoldReason; - type RuntimeFreezeReason = RuntimeFreezeReason; - type FreezeIdentifier = [u8; 8]; - type MaxFreezes = (); -} - -pub struct EnsureAliceOrBob; -impl EnsureOrigin for EnsureAliceOrBob { - type Success = AccountId; - - fn try_origin(o: RuntimeOrigin) -> Result { - Into::, RuntimeOrigin>>::into(o).and_then(|o| match o { - RawOrigin::Signed(ALICE) => Ok(ALICE), - RawOrigin::Signed(BOB) => Ok(BOB), - r => Err(RuntimeOrigin::from(r)), - }) - } - - #[cfg(feature = "runtime-benchmarks")] - fn try_successful_origin() -> Result { - let zero_account_id = AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()) - .expect("infinite length input; no invalid inputs for type; qed"); - Ok(RuntimeOrigin::from(RawOrigin::Signed(zero_account_id))) - } -} - -parameter_types! { - pub static MockBlockNumberProvider: u64 = 0; -} - -impl BlockNumberProvider for MockBlockNumberProvider { - type BlockNumber = u64; - - fn current_block_number() -> BlockNumberFor { - Self::get() - } -} - -impl Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Currency = PalletBalances; - type MinVestedTransfer = ConstU64<5>; - type VestedTransferOrigin = EnsureAliceOrBob; - type WeightInfo = (); - type MaxVestingSchedules = ConstU32<2>; - type BlockNumberProvider = MockBlockNumberProvider; -} - -type Block = frame_system::mocking::MockBlock; - -construct_runtime!( - pub enum Runtime { - System: frame_system, - Vesting: vesting, - PalletBalances: pallet_balances, - } -); - -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; - -impl ExtBuilder { - pub fn build() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .unwrap(); - - pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, ALICE_BALANCE), (CHARLIE, CHARLIE_BALANCE)], - } - .assimilate_storage(&mut t) - .unwrap(); - - vesting::GenesisConfig:: { - vesting: vec![ - // who, start, period, period_count, per_period - (CHARLIE, 2, 3, 1, 5), - (CHARLIE, 2 + 3, 3, 3, 5), - ], - } - .assimilate_storage(&mut t) - .unwrap(); - - t.into() - } -} diff --git a/orml/vesting/src/tests.rs b/orml/vesting/src/tests.rs deleted file mode 100644 index 4d1af5fc2cf..00000000000 --- a/orml/vesting/src/tests.rs +++ /dev/null @@ -1,603 +0,0 @@ -//! Unit tests for the vesting module. - -#![cfg(test)] - -use super::*; -use frame_support::error::BadOrigin; -use frame_support::{assert_noop, assert_ok}; -use mock::*; -use pallet_balances::{BalanceLock, Reasons}; -use sp_runtime::traits::Dispatchable; -use sp_runtime::TokenError; - -#[test] -fn vesting_from_chain_spec_works() { - ExtBuilder::build().execute_with(|| { - assert_ok!(PalletBalances::ensure_can_withdraw( - &CHARLIE, - 10, - WithdrawReasons::TRANSFER, - 20 - )); - assert!( - PalletBalances::ensure_can_withdraw(&CHARLIE, 11, WithdrawReasons::TRANSFER, 19) - .is_err() - ); - - assert_eq!( - Vesting::vesting_schedules(CHARLIE), - vec![ - VestingSchedule { - start: 2u64, - period: 3u64, - period_count: 1u32, - per_period: 5u64, - }, - VestingSchedule { - start: 2u64 + 3u64, - period: 3u64, - period_count: 3u32, - per_period: 5u64, - } - ] - ); - - MockBlockNumberProvider::set(13); - - assert_ok!(Vesting::claim(RuntimeOrigin::signed(CHARLIE))); - - assert_ok!(PalletBalances::ensure_can_withdraw( - &CHARLIE, - 25, - WithdrawReasons::TRANSFER, - 5 - )); - assert!( - PalletBalances::ensure_can_withdraw(&CHARLIE, 26, WithdrawReasons::TRANSFER, 4) - .is_err() - ); - - MockBlockNumberProvider::set(14); - - assert_ok!(Vesting::claim(RuntimeOrigin::signed(CHARLIE))); - - assert_ok!(PalletBalances::ensure_can_withdraw( - &CHARLIE, - 30, - WithdrawReasons::TRANSFER, - 0 - )); - }); -} - -#[test] -fn vested_transfer_works() { - ExtBuilder::build().execute_with(|| { - System::set_block_number(1); - - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 1u32, - per_period: 100u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule.clone() - )); - assert_eq!(Vesting::vesting_schedules(BOB), vec![schedule.clone()]); - System::assert_last_event(RuntimeEvent::Vesting(crate::Event::VestingScheduleAdded { - from: ALICE, - to: BOB, - vesting_schedule: schedule, - })); - }); -} - -#[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::::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(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule - )); - - MockBlockNumberProvider::set(12); - - let another_schedule = VestingSchedule { - start: 10u64, - period: 13u64, - period_count: 1u32, - per_period: 7u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - another_schedule - )); - - assert_eq!( - PalletBalances::locks(BOB).first(), - Some(&BalanceLock { - id: VESTING_LOCK_ID, - amount: 17u64, - reasons: Reasons::All, - }) - ); - }); -} - -#[test] -fn cannot_use_fund_if_not_claimed() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 10u64, - period: 10u64, - period_count: 1u32, - per_period: 50u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule - )); - assert!( - PalletBalances::ensure_can_withdraw(&BOB, 1, WithdrawReasons::TRANSFER, 49).is_err() - ); - }); -} - -#[test] -fn vested_transfer_fails_if_zero_period_or_count() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 1u64, - period: 0u64, - period_count: 1u32, - per_period: 100u64, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(ALICE), BOB, schedule), - Error::::ZeroVestingPeriod - ); - - let schedule = VestingSchedule { - start: 1u64, - period: 1u64, - period_count: 0u32, - per_period: 100u64, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(ALICE), BOB, schedule), - Error::::ZeroVestingPeriodCount - ); - }); -} - -#[test] -fn vested_transfer_fails_if_transfer_err() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 1u64, - period: 1u64, - period_count: 1u32, - per_period: 100u64, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(BOB), ALICE, schedule), - TokenError::FundsUnavailable, - ); - }); -} - -#[test] -fn vested_transfer_fails_if_overflow() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 1u64, - period: 1u64, - period_count: 2u32, - per_period: u64::MAX, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(ALICE), BOB, schedule), - ArithmeticError::Overflow, - ); - - let another_schedule = VestingSchedule { - start: u64::MAX, - period: 1u64, - period_count: 2u32, - per_period: 1u64, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(ALICE), BOB, another_schedule), - ArithmeticError::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(RuntimeOrigin::signed(CHARLIE), BOB, schedule), - BadOrigin - ); - }); -} - -#[test] -fn claim_works() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule - )); - - MockBlockNumberProvider::set(11); - // remain locked if not claimed - assert!( - PalletBalances::transfer(&BOB, &ALICE, 10, ExistenceRequirement::AllowDeath).is_err() - ); - // unlocked after claiming - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert!(VestingSchedules::::contains_key(BOB)); - assert_ok!(PalletBalances::transfer( - &BOB, - &ALICE, - 10, - ExistenceRequirement::AllowDeath - )); - // more are still locked - assert!( - PalletBalances::transfer(&BOB, &ALICE, 1, ExistenceRequirement::AllowDeath).is_err() - ); - - MockBlockNumberProvider::set(21); - // claim more - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert!(!VestingSchedules::::contains_key(BOB)); - assert_ok!(PalletBalances::transfer( - &BOB, - &ALICE, - 10, - ExistenceRequirement::AllowDeath - )); - // all used up - assert_eq!(PalletBalances::free_balance(BOB), 0); - - // no locks anymore - assert_eq!(PalletBalances::locks(BOB), vec![]); - }); -} - -#[test] -fn claim_for_works() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule - )); - - assert_ok!(Vesting::claim_for(RuntimeOrigin::signed(ALICE), BOB)); - - assert_eq!( - PalletBalances::locks(BOB).first(), - Some(&BalanceLock { - id: VESTING_LOCK_ID, - amount: 20u64, - reasons: Reasons::All, - }) - ); - assert!(VestingSchedules::::contains_key(BOB)); - - MockBlockNumberProvider::set(21); - - assert_ok!(Vesting::claim_for(RuntimeOrigin::signed(ALICE), BOB)); - - // no locks anymore - assert_eq!(PalletBalances::locks(BOB), vec![]); - assert!(!VestingSchedules::::contains_key(BOB)); - }); -} - -#[test] -fn update_vesting_schedules_works() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule - )); - - let updated_schedule = VestingSchedule { - start: 0u64, - period: 20u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::update_vesting_schedules( - RuntimeOrigin::root(), - BOB, - vec![updated_schedule] - )); - - MockBlockNumberProvider::set(11); - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert!( - PalletBalances::transfer(&BOB, &ALICE, 1, ExistenceRequirement::AllowDeath).is_err() - ); - - MockBlockNumberProvider::set(21); - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert_ok!(PalletBalances::transfer( - &BOB, - &ALICE, - 10, - ExistenceRequirement::AllowDeath - )); - - // empty vesting schedules cleanup the storage and unlock the fund - assert!(VestingSchedules::::contains_key(BOB)); - assert_eq!( - PalletBalances::locks(BOB).first(), - Some(&BalanceLock { - id: VESTING_LOCK_ID, - amount: 10u64, - reasons: Reasons::All, - }) - ); - assert_ok!(Vesting::update_vesting_schedules( - RuntimeOrigin::root(), - BOB, - vec![] - )); - assert!(!VestingSchedules::::contains_key(BOB)); - assert_eq!(PalletBalances::locks(BOB), vec![]); - }); -} - -#[test] -fn update_vesting_schedules_fails_if_unexpected_existing_locks() { - ExtBuilder::build().execute_with(|| { - assert_ok!(PalletBalances::transfer( - &ALICE, - &BOB, - 1, - ExistenceRequirement::AllowDeath - )); - PalletBalances::set_lock(*b"prelocks", &BOB, 0u64, WithdrawReasons::all()); - }); -} - -#[test] -fn vested_transfer_check_for_min() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 1u64, - period: 1u64, - period_count: 1u32, - per_period: 3u64, - }; - assert_noop!( - Vesting::vested_transfer(RuntimeOrigin::signed(BOB), ALICE, schedule), - Error::::AmountLow - ); - }); -} - -#[test] -fn multiple_vesting_schedule_claim_works() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule.clone() - )); - - let schedule2 = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 3u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule2.clone() - )); - - assert_eq!( - Vesting::vesting_schedules(BOB), - vec![schedule, schedule2.clone()] - ); - - MockBlockNumberProvider::set(21); - - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - - assert_eq!(Vesting::vesting_schedules(BOB), vec![schedule2]); - - MockBlockNumberProvider::set(31); - - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - - assert!(!VestingSchedules::::contains_key(BOB)); - - assert_eq!(PalletBalances::locks(BOB), vec![]); - }); -} - -#[test] -fn exceeding_maximum_schedules_should_fail() { - ExtBuilder::build().execute_with(|| { - let schedule = VestingSchedule { - start: 0u64, - period: 10u64, - period_count: 2u32, - per_period: 10u64, - }; - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule.clone() - )); - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - schedule.clone() - )); - - let create = RuntimeCall::Vesting(crate::Call::::vested_transfer { - dest: BOB, - schedule: schedule.clone(), - }); - assert_noop!( - create.dispatch(RuntimeOrigin::signed(ALICE)), - Error::::MaxVestingSchedulesExceeded - ); - - let schedules = vec![schedule.clone(), schedule.clone(), schedule]; - - assert_noop!( - Vesting::update_vesting_schedules(RuntimeOrigin::root(), BOB, schedules), - Error::::MaxVestingSchedulesExceeded - ); - }); -} - -#[test] -fn cliff_vesting_works() { - const VESTING_AMOUNT: u64 = 12; - const VESTING_PERIOD: u64 = 20; - - ExtBuilder::build().execute_with(|| { - let cliff_schedule = VestingSchedule { - start: VESTING_PERIOD - 1, - period: 1, - period_count: 1, - per_period: VESTING_AMOUNT, - }; - - let balance_lock = BalanceLock { - id: VESTING_LOCK_ID, - amount: VESTING_AMOUNT, - reasons: Reasons::All, - }; - - assert_eq!(PalletBalances::free_balance(BOB), 0); - assert_ok!(Vesting::vested_transfer( - RuntimeOrigin::signed(ALICE), - BOB, - cliff_schedule - )); - assert_eq!(PalletBalances::free_balance(BOB), VESTING_AMOUNT); - assert_eq!(PalletBalances::locks(BOB), vec![balance_lock.clone()]); - - for i in 1..VESTING_PERIOD { - MockBlockNumberProvider::set(i); - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert_eq!(PalletBalances::free_balance(BOB), VESTING_AMOUNT); - assert_eq!(PalletBalances::locks(BOB), vec![balance_lock.clone()]); - assert_noop!( - PalletBalances::transfer( - &BOB, - &CHARLIE, - VESTING_AMOUNT, - ExistenceRequirement::AllowDeath - ), - TokenError::Frozen, - ); - } - - MockBlockNumberProvider::set(VESTING_PERIOD); - assert_ok!(Vesting::claim(RuntimeOrigin::signed(BOB))); - assert!(PalletBalances::locks(BOB).is_empty()); - assert_ok!(PalletBalances::transfer( - &BOB, - &CHARLIE, - VESTING_AMOUNT, - ExistenceRequirement::AllowDeath, - )); - }); -} diff --git a/orml/vesting/src/weights.rs b/orml/vesting/src/weights.rs deleted file mode 100644 index 543cad9f584..00000000000 --- a/orml/vesting/src/weights.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! Autogenerated weights for orml_vesting -//! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-05-04, STEPS: [50, ], REPEAT: 20, LOW RANGE: [], HIGH RANGE: [] -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 - -// Executed Command: -// /Users/xiliangchen/projects/acala/target/release/acala -// benchmark -// --chain=dev -// --steps=50 -// --repeat=20 -// --pallet=orml_vesting -// --extrinsic=* -// --wasm-execution=compiled -// --heap-pages=4096 -// --output=./vesting/src/weights.rs -// --template -// ../templates/orml-weight-template.hbs - - -#![cfg_attr(rustfmt, rustfmt_skip)] -#![allow(unused_parens)] -#![allow(unused_imports)] -#![allow(clippy::unnecessary_cast)] - -use frame_support::{traits::Get, weights::{Weight, constants::ParityDbWeight}}; -use sp_std::marker::PhantomData; - -/// Weight functions needed for orml_vesting. -pub trait WeightInfo { - fn vested_transfer() -> Weight; - fn claim(i: u32, ) -> Weight; - fn update_vesting_schedules(i: u32, ) -> Weight; -} - -/// Default weights. -impl WeightInfo for () { - fn vested_transfer() -> Weight { - Weight::from_parts(69_000_000, 0) - .saturating_add(ParityDbWeight::get().reads(4 as u64)) - .saturating_add(ParityDbWeight::get().writes(4 as u64)) - } - fn claim(i: u32, ) -> Weight { - Weight::from_parts(31_747_000, 0) - // Standard Error: 4_000 - .saturating_add(Weight::from_parts(63_000, 0).saturating_mul(i as u64)) - .saturating_add(ParityDbWeight::get().reads(2 as u64)) - .saturating_add(ParityDbWeight::get().writes(2 as u64)) - } - fn update_vesting_schedules(i: u32, ) -> Weight { - Weight::from_parts(29_457_000, 0) - // Standard Error: 4_000 - .saturating_add(Weight::from_parts(117_000, 0).saturating_mul(i as u64)) - .saturating_add(ParityDbWeight::get().reads(2 as u64)) - .saturating_add(ParityDbWeight::get().writes(3 as u64)) - } -} diff --git a/rustfmt.toml b/rustfmt.toml index c03016f32ec..c7e69dea017 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,6 +1,2 @@ edition = "2021" -ignore = [ - # This is where Substrate sources with custom formatting are, don't touch them - "/orml", -] imports_granularity = "Module" diff --git a/test/subspace-test-client/src/chain_spec.rs b/test/subspace-test-client/src/chain_spec.rs index 3320cfe1545..96058be781b 100644 --- a/test/subspace-test-client/src/chain_spec.rs +++ b/test/subspace-test-client/src/chain_spec.rs @@ -5,11 +5,10 @@ use sp_core::{sr25519, Pair, Public}; use sp_runtime::traits::{IdentifyAccount, Verify}; use std::marker::PhantomData; use std::num::NonZeroU32; -use subspace_runtime_primitives::{AccountId, Balance, BlockNumber, Signature}; +use subspace_runtime_primitives::{AccountId, Balance, Signature}; use subspace_test_runtime::{ AllowAuthoringBy, BalancesConfig, DomainsConfig, EnableRewardsAt, RewardsConfig, - RuntimeGenesisConfig, SubspaceConfig, SudoConfig, SystemConfig, VestingConfig, SSC, - WASM_BINARY, + RuntimeGenesisConfig, SubspaceConfig, SudoConfig, SystemConfig, SSC, WASM_BINARY, }; /// Generate a crypto pair from seed. @@ -55,7 +54,6 @@ pub fn subspace_local_testnet_config() -> Result { (get_account_id_from_seed("Eve//stash"), 1_000 * SSC), (get_account_id_from_seed("Ferdie//stash"), 1_000 * SSC), ], - vec![], )?) .map_err(|error| format!("Failed to serialize genesis config: {error}"))?, )) @@ -67,8 +65,6 @@ pub fn subspace_local_testnet_config() -> Result { fn create_genesis_config( sudo_account: AccountId, balances: Vec<(AccountId, Balance)>, - // who, start, period, period_count, per_period - vesting: Vec<(AccountId, BlockNumber, BlockNumber, u32, Balance)>, ) -> Result { Ok(RuntimeGenesisConfig { system: SystemConfig::default(), @@ -89,7 +85,6 @@ fn create_genesis_config( proposer_subsidy_points: Default::default(), voter_subsidy_points: Default::default(), }, - vesting: VestingConfig { vesting }, domains: DomainsConfig { permissioned_action_allowed_by: Some(sp_domains::PermissionedActionAllowedBy::Anyone), genesis_domains: vec![ diff --git a/test/subspace-test-runtime/Cargo.toml b/test/subspace-test-runtime/Cargo.toml index f7bf1946bed..bc9ca2e02fd 100644 --- a/test/subspace-test-runtime/Cargo.toml +++ b/test/subspace-test-runtime/Cargo.toml @@ -21,7 +21,6 @@ domain-runtime-primitives = { version = "0.1.0", default-features = false, path frame-executive = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } -orml-vesting = { version = "0.9.1", default-features = false, path = "../../orml/vesting" } pallet-balances = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } pallet-domains = { version = "0.1.0", default-features = false, path = "../../crates/pallet-domains" } pallet-messenger = { version = "0.1.0", path = "../../domains/pallets/messenger", default-features = false } @@ -79,7 +78,6 @@ std = [ "frame-support/std", "frame-system/std", "frame-system-rpc-runtime-api/std", - "orml-vesting/std", "pallet-balances/std", "pallet-domains/std", "pallet-messenger/std", diff --git a/test/subspace-test-runtime/src/lib.rs b/test/subspace-test-runtime/src/lib.rs index 67899a3aef1..8a15246cb3f 100644 --- a/test/subspace-test-runtime/src/lib.rs +++ b/test/subspace-test-runtime/src/lib.rs @@ -42,7 +42,6 @@ use frame_support::weights::constants::{ParityDbWeight, WEIGHT_REF_TIME_PER_SECO use frame_support::weights::{ConstantMultiplier, Weight}; use frame_support::{construct_runtime, parameter_types, PalletId}; use frame_system::limits::{BlockLength, BlockWeights}; -use frame_system::EnsureNever; use pallet_balances::NegativeImbalance; pub use pallet_rewards::RewardPoint; pub use pallet_subspace::{AllowAuthoringBy, EnableRewardsAt}; @@ -805,21 +804,6 @@ impl pallet_rewards::Config for Runtime { type OnReward = (); } -parameter_types! { - // This value doesn't matter, we don't use it (`VestedTransferOrigin = EnsureNever` below). - pub const MinVestedTransfer: Balance = 0; -} - -impl orml_vesting::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Currency = Balances; - type MinVestedTransfer = MinVestedTransfer; - type VestedTransferOrigin = EnsureNever; - type WeightInfo = (); - type MaxVestingSchedules = ConstU32<2>; - type BlockNumberProvider = System; -} - mod mmr { use super::Runtime; pub use pallet_mmr::primitives::*; @@ -877,8 +861,6 @@ construct_runtime!( Domains: pallet_domains = 11, RuntimeConfigs: pallet_runtime_configs = 14, - Vesting: orml_vesting = 7, - Mmr: pallet_mmr = 30, SubspaceMmr: pallet_subspace_mmr = 31,