Skip to content

Commit

Permalink
feat: add more native restaking test
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex committed Feb 5, 2025
1 parent 7dfbf12 commit 2e69ed3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pallets/multi-asset-delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub mod pallet {
use scale_info::TypeInfo;
use sp_core::H160;
use sp_runtime::traits::{MaybeSerializeDeserialize, Member, Zero};
use sp_staking::{OnStakingUpdate, SessionIndex, StakingInterface};
use sp_staking::{OnStakingUpdate, SessionIndex, Stake, StakingInterface};
use sp_std::{fmt::Debug, prelude::*, vec::Vec};
use tangle_primitives::traits::RewardsManager;
use tangle_primitives::types::rewards::LockMultiplier;
Expand Down Expand Up @@ -1231,5 +1231,9 @@ pub mod pallet {
}
}

impl<C: Config> OnStakingUpdate<C::AccountId, BalanceOf<C>> for Pallet<C> {}
impl<C: Config> OnStakingUpdate<C::AccountId, BalanceOf<C>> for Pallet<C> {
fn on_stake_update(_who: &C::AccountId, _prev_stake: Option<Stake<BalanceOf<C>>>) {
unimplemented!("Custom stake update logic not implemented")
}
}
}
50 changes: 50 additions & 0 deletions pallets/multi-asset-delegation/src/tests/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,53 @@ fn native_restaking_should_work() {
assert_eq!(locks[1].amount, delegate_amount);
});
}

#[test]
fn unbond_should_fail_if_delegated_nomination() {
new_test_ext().execute_with(|| {
// Arrange
let who: AccountId = Dave.into();
let validator = Staking::invulnerables()[0].clone();
let operator: AccountId = Alice.into();
let amount = 100_000;
let delegate_amount = amount / 2;
// Bond Some TNT
assert_ok!(Staking::bond(
RuntimeOrigin::signed(who.clone()),
amount,
pallet_staking::RewardDestination::Staked
));
// Nominate the validator
assert_ok!(Staking::nominate(RuntimeOrigin::signed(who.clone()), vec![validator.clone()]));

System::set_block_number(2);
Session::on_initialize(2);
Staking::on_initialize(2);
Session::on_finalize(2);
Staking::on_finalize(2);

assert_ok!(MultiAssetDelegation::join_operators(
RuntimeOrigin::signed(operator.clone()),
10_000
));

// Restake
assert_ok!(MultiAssetDelegation::delegate_nomination(
RuntimeOrigin::signed(who.clone()),
operator.clone(),
delegate_amount,
Default::default(),
));

// Try to unbond from the staking pallet
assert_ok!(Staking::unbond(RuntimeOrigin::signed(who.clone()), amount));

// Assert
let ledger = Staking::ledger(sp_staking::StakingAccount::Stash(who.clone())).unwrap();
assert_eq!(ledger.active, 0);
assert_eq!(ledger.total, amount);
assert_eq!(ledger.unlocking.len(), 1);

todo!("This test should fail but it is passing");
});
}

0 comments on commit 2e69ed3

Please sign in to comment.