From 0197c954f456a500a47b59669a2326cc95c82347 Mon Sep 17 00:00:00 2001 From: Dino Pacandi Date: Wed, 25 Oct 2023 14:56:49 +0200 Subject: [PATCH] Local integration --- Cargo.lock | 2 + Cargo.toml | 1 + pallets/dapp-staking-v3/src/lib.rs | 15 ++++++-- pallets/dapp-staking-v3/src/test/mock.rs | 2 +- pallets/dapp-staking-v3/src/test/tests.rs | 6 +-- pallets/dapp-staking-v3/src/types.rs | 1 + runtime/local/Cargo.toml | 4 ++ runtime/local/src/lib.rs | 45 ++++++++++++++++++++++- 8 files changed, 68 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 536ec19859..19c286fc40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5524,6 +5524,7 @@ dependencies = [ "pallet-contracts", "pallet-contracts-primitives", "pallet-custom-signatures", + "pallet-dapp-staking-v3", "pallet-dapps-staking", "pallet-democracy", "pallet-ethereum", @@ -5558,6 +5559,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", + "sp-arithmetic", "sp-block-builder", "sp-consensus-aura", "sp-core", diff --git a/Cargo.toml b/Cargo.toml index fd6f3ee412..91789f677a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -274,6 +274,7 @@ pallet-block-reward = { path = "./pallets/block-reward", default-features = fals pallet-collator-selection = { path = "./pallets/collator-selection", default-features = false } pallet-custom-signatures = { path = "./pallets/custom-signatures", default-features = false } pallet-dapps-staking = { path = "./pallets/dapps-staking", default-features = false } +pallet-dapp-staking-v3 = { path = "./pallets/dapp-staking-v3", default-features = false } pallet-xc-asset-config = { path = "./pallets/xc-asset-config", default-features = false } pallet-xvm = { path = "./pallets/xvm", default-features = false } pallet-xcm = { path = "./pallets/pallet-xcm", default-features = false } diff --git a/pallets/dapp-staking-v3/src/lib.rs b/pallets/dapp-staking-v3/src/lib.rs index 7596bfe0df..2bfcb74ace 100644 --- a/pallets/dapp-staking-v3/src/lib.rs +++ b/pallets/dapp-staking-v3/src/lib.rs @@ -50,10 +50,13 @@ use sp_runtime::{ traits::{BadOrigin, Saturating, Zero}, Perbill, }; +pub use sp_std::vec::Vec; use astar_primitives::Balance; use crate::types::*; +pub use crate::types::{PriceProvider, RewardPoolProvider}; + pub use pallet::*; #[cfg(test)] @@ -135,9 +138,10 @@ pub mod pallet { #[pallet::constant] type MinimumLockedAmount: Get; - /// Amount of blocks that need to pass before unlocking chunks can be claimed by the owner. + /// Number of standard eras that need to pass before unlocking chunk can be claimed. + /// Even though it's expressed in 'eras', it's actually measured in number of blocks. #[pallet::constant] - type UnlockingPeriod: Get>; + type UnlockingPeriod: Get; /// Maximum amount of stake entries contract is allowed to have at once. #[pallet::constant] @@ -796,7 +800,7 @@ pub mod pallet { ledger.subtract_lock_amount(amount_to_unlock); let current_block = frame_system::Pallet::::block_number(); - let unlock_block = current_block.saturating_add(T::UnlockingPeriod::get()); + let unlock_block = current_block.saturating_add(Self::unlock_period()); ledger .add_unlocking_chunk(amount_to_unlock, unlock_block) .map_err(|_| Error::::TooManyUnlockingChunks)?; @@ -1479,6 +1483,11 @@ pub mod pallet { current_period.saturating_sub(T::RewardRetentionInPeriods::get()) } + /// Unlocking period expressed in the number of blocks. + pub fn unlock_period() -> BlockNumberFor { + T::StandardEraLength::get().saturating_mul(T::UnlockingPeriod::get().into()) + } + /// Assign eligible dApps into appropriate tiers, and calculate reward for each tier. /// /// The returned object contains information about each dApp that made it into a tier. diff --git a/pallets/dapp-staking-v3/src/test/mock.rs b/pallets/dapp-staking-v3/src/test/mock.rs index 6de7dc0b61..e788349707 100644 --- a/pallets/dapp-staking-v3/src/test/mock.rs +++ b/pallets/dapp-staking-v3/src/test/mock.rs @@ -153,7 +153,7 @@ impl pallet_dapp_staking::Config for Test { type MaxNumberOfContracts = ConstU16<10>; type MaxUnlockingChunks = ConstU32<5>; type MinimumLockedAmount = ConstU128; - type UnlockingPeriod = ConstU64<20>; + type UnlockingPeriod = ConstU32<2>; type MaxNumberOfStakedContracts = ConstU32<3>; type MinimumStakeAmount = ConstU128<3>; type NumberOfTiers = ConstU32<4>; diff --git a/pallets/dapp-staking-v3/src/test/tests.rs b/pallets/dapp-staking-v3/src/test/tests.rs index ab33654c49..a823fc1abc 100644 --- a/pallets/dapp-staking-v3/src/test/tests.rs +++ b/pallets/dapp-staking-v3/src/test/tests.rs @@ -641,7 +641,7 @@ fn unlock_with_exceeding_unlocking_chunks_storage_limits_fails() { #[test] fn claim_unlocked_is_ok() { ExtBuilder::build().execute_with(|| { - let unlocking_blocks: BlockNumber = ::UnlockingPeriod::get(); + let unlocking_blocks = DappStaking::unlock_period(); // Lock some amount in a few eras let account = 2; @@ -691,7 +691,7 @@ fn claim_unlocked_no_eligible_chunks_fails() { // Cannot claim if unlock period hasn't passed yet let lock_amount = 103; assert_lock(account, lock_amount); - let unlocking_blocks: BlockNumber = ::UnlockingPeriod::get(); + let unlocking_blocks = DappStaking::unlock_period(); run_for_blocks(unlocking_blocks - 1); assert_noop!( DappStaking::claim_unlocked(RuntimeOrigin::signed(account)), @@ -769,7 +769,7 @@ fn relock_unlocking_insufficient_lock_amount_fails() { }); // Make sure only one chunk is left - let unlocking_blocks: BlockNumber = ::UnlockingPeriod::get(); + let unlocking_blocks = DappStaking::unlock_period(); run_for_blocks(unlocking_blocks - 1); assert_claim_unlocked(account); diff --git a/pallets/dapp-staking-v3/src/types.rs b/pallets/dapp-staking-v3/src/types.rs index fec4351457..834cb6ea34 100644 --- a/pallets/dapp-staking-v3/src/types.rs +++ b/pallets/dapp-staking-v3/src/types.rs @@ -26,6 +26,7 @@ use sp_runtime::{ traits::{AtLeast32BitUnsigned, UniqueSaturatedInto, Zero}, FixedPointNumber, Permill, Saturating, }; +pub use sp_std::vec::Vec; use astar_primitives::Balance; diff --git a/runtime/local/Cargo.toml b/runtime/local/Cargo.toml index 44012e1760..a1280f19d6 100644 --- a/runtime/local/Cargo.toml +++ b/runtime/local/Cargo.toml @@ -48,6 +48,7 @@ pallet-treasury = { workspace = true } pallet-utility = { workspace = true } pallet-vesting = { workspace = true } sp-api = { workspace = true } +sp-arithmetic = { workspace = true } sp-block-builder = { workspace = true } sp-consensus-aura = { workspace = true } sp-core = { workspace = true } @@ -70,6 +71,7 @@ pallet-block-reward = { workspace = true } pallet-chain-extension-dapps-staking = { workspace = true } pallet-chain-extension-xvm = { workspace = true } pallet-custom-signatures = { workspace = true } +pallet-dapp-staking-v3 = { workspace = true } pallet-dapps-staking = { workspace = true } pallet-evm-precompile-assets-erc20 = { workspace = true } pallet-evm-precompile-dapps-staking = { workspace = true } @@ -117,6 +119,7 @@ std = [ "pallet-chain-extension-xvm/std", "pallet-custom-signatures/std", "pallet-dapps-staking/std", + "pallet-dapp-staking-v3/std", "pallet-base-fee/std", "pallet-ethereum/std", "pallet-evm/std", @@ -150,6 +153,7 @@ std = [ "sp-offchain/std", "sp-runtime/std", "sp-session/std", + "sp-arithmetic/std", "sp-std/std", "sp-transaction-pool/std", "sp-version/std", diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index 40c1e7e9a3..addebe80e1 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -27,7 +27,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); use frame_support::{ construct_runtime, parameter_types, traits::{ - AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64, Currency, EitherOfDiverse, + AsEnsureOriginWithArg, ConstU128, ConstU16, ConstU32, ConstU64, Currency, EitherOfDiverse, EqualPrivilegeOnly, FindAuthor, Get, InstanceFilter, Nothing, OnFinalize, WithdrawReasons, }, weights::{ @@ -46,6 +46,7 @@ use pallet_evm_precompile_assets_erc20::AddressToAssetId; use pallet_grandpa::{fg_primitives, AuthorityList as GrandpaAuthorityList}; use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; use sp_api::impl_runtime_apis; +use sp_arithmetic::fixed_point::FixedU64; use sp_core::{crypto::KeyTypeId, ConstBool, OpaqueMetadata, H160, H256, U256}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, @@ -439,6 +440,47 @@ impl> From<[u8; 32]> for SmartContract { } } +pub struct DummyPriceProvider; +impl pallet_dapp_staking_v3::PriceProvider for DummyPriceProvider { + fn average_price() -> FixedU64 { + FixedU64::from_rational(1, 10) + } +} + +pub struct DummyRewardPoolProvider; +impl pallet_dapp_staking_v3::RewardPoolProvider for DummyRewardPoolProvider { + fn normal_reward_pools() -> (Balance, Balance) { + ( + Balance::from(1_000_000_000_000 * AST), + Balance::from(1_000_000_000 * AST), + ) + } + fn bonus_reward_pool() -> Balance { + Balance::from(3_000_000 * AST) + } +} + +impl pallet_dapp_staking_v3::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type SmartContract = SmartContract; + type ManagerOrigin = frame_system::EnsureRoot; + type NativePriceProvider = DummyPriceProvider; + type RewardPoolProvider = DummyRewardPoolProvider; + type StandardEraLength = ConstU32<30>; // should be 1 minute per standard era + type StandardErasPerVotingPeriod = ConstU32<2>; + type StandardErasPerBuildAndEarnPeriod = ConstU32<10>; + type EraRewardSpanLength = ConstU32<8>; + type RewardRetentionInPeriods = ConstU32<2>; + type MaxNumberOfContracts = ConstU16<10>; + type MaxUnlockingChunks = ConstU32<5>; + type MinimumLockedAmount = ConstU128; + type UnlockingPeriod = ConstU32<2>; + type MaxNumberOfStakedContracts = ConstU32<3>; + type MinimumStakeAmount = ConstU128; + type NumberOfTiers = ConstU32<4>; +} + impl pallet_utility::Config for Runtime { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; @@ -1005,6 +1047,7 @@ construct_runtime!( Balances: pallet_balances, Vesting: pallet_vesting, DappsStaking: pallet_dapps_staking, + DappStaking: pallet_dapp_staking_v3, BlockReward: pallet_block_reward, TransactionPayment: pallet_transaction_payment, EVM: pallet_evm,