Skip to content

Commit

Permalink
Local integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 25, 2023
1 parent d47bcf4 commit 0197c95
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
15 changes: 12 additions & 3 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -135,9 +138,10 @@ pub mod pallet {
#[pallet::constant]
type MinimumLockedAmount: Get<Balance>;

/// 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<BlockNumberFor<Self>>;
type UnlockingPeriod: Get<EraNumber>;

/// Maximum amount of stake entries contract is allowed to have at once.
#[pallet::constant]
Expand Down Expand Up @@ -796,7 +800,7 @@ pub mod pallet {
ledger.subtract_lock_amount(amount_to_unlock);

let current_block = frame_system::Pallet::<T>::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::<T>::TooManyUnlockingChunks)?;
Expand Down Expand Up @@ -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> {
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.
Expand Down
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl pallet_dapp_staking::Config for Test {
type MaxNumberOfContracts = ConstU16<10>;
type MaxUnlockingChunks = ConstU32<5>;
type MinimumLockedAmount = ConstU128<MINIMUM_LOCK_AMOUNT>;
type UnlockingPeriod = ConstU64<20>;
type UnlockingPeriod = ConstU32<2>;
type MaxNumberOfStakedContracts = ConstU32<3>;
type MinimumStakeAmount = ConstU128<3>;
type NumberOfTiers = ConstU32<4>;
Expand Down
6 changes: 3 additions & 3 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Test as Config>::UnlockingPeriod::get();
let unlocking_blocks = DappStaking::unlock_period();

// Lock some amount in a few eras
let account = 2;
Expand Down Expand Up @@ -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 = <Test as Config>::UnlockingPeriod::get();
let unlocking_blocks = DappStaking::unlock_period();
run_for_blocks(unlocking_blocks - 1);
assert_noop!(
DappStaking::claim_unlocked(RuntimeOrigin::signed(account)),
Expand Down Expand Up @@ -769,7 +769,7 @@ fn relock_unlocking_insufficient_lock_amount_fails() {
});

// Make sure only one chunk is left
let unlocking_blocks: BlockNumber = <Test as Config>::UnlockingPeriod::get();
let unlocking_blocks = DappStaking::unlock_period();
run_for_blocks(unlocking_blocks - 1);
assert_claim_unlocked(account);

Expand Down
1 change: 1 addition & 0 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use sp_runtime::{
traits::{AtLeast32BitUnsigned, UniqueSaturatedInto, Zero},
FixedPointNumber, Permill, Saturating,
};
pub use sp_std::vec::Vec;

use astar_primitives::Balance;

Expand Down
4 changes: 4 additions & 0 deletions runtime/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
45 changes: 44 additions & 1 deletion runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -439,6 +440,47 @@ impl<AccountId: From<[u8; 32]>> From<[u8; 32]> for SmartContract<AccountId> {
}
}

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<AccountId>;
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
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<AST>;
type UnlockingPeriod = ConstU32<2>;
type MaxNumberOfStakedContracts = ConstU32<3>;
type MinimumStakeAmount = ConstU128<AST>;
type NumberOfTiers = ConstU32<4>;
}

impl pallet_utility::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0197c95

Please sign in to comment.