@@ -52,8 +52,8 @@ pub use sp_std::vec::Vec;
52
52
53
53
use astar_primitives:: {
54
54
dapp_staking:: {
55
- CycleConfiguration , DAppId , EraNumber , Observer as DAppStakingObserver , PeriodNumber ,
56
- SmartContractHandle , StakingRewardHandler , TierId ,
55
+ AccountCheck , CycleConfiguration , DAppId , EraNumber , Observer as DAppStakingObserver ,
56
+ PeriodNumber , SmartContractHandle , StakingRewardHandler , TierId ,
57
57
} ,
58
58
oracle:: PriceProvider ,
59
59
Balance , BlockNumber ,
@@ -144,6 +144,9 @@ pub mod pallet {
144
144
/// dApp staking event observers, notified when certain events occur.
145
145
type Observers : DAppStakingObserver ;
146
146
147
+ /// Used to check whether an account is allowed to participate in dApp staking.
148
+ type AccountCheck : AccountCheck < Self :: AccountId > ;
149
+
147
150
/// Maximum length of a single era reward span length entry.
148
151
#[ pallet:: constant]
149
152
type EraRewardSpanLength : Get < u32 > ;
@@ -307,6 +310,8 @@ pub mod pallet {
307
310
ZeroAmount ,
308
311
/// Total locked amount for staker is below minimum threshold.
309
312
LockedAmountBelowThreshold ,
313
+ /// Account is not allowed to participate in dApp staking due to some external reason (e.g. account is already a collator).
314
+ AccountNotAvailableForDappStaking ,
310
315
/// Cannot add additional unlocking chunks due to capacity limit.
311
316
TooManyUnlockingChunks ,
312
317
/// Remaining stake prevents entire balance of starting the unlocking process.
@@ -765,16 +770,30 @@ pub mod pallet {
765
770
///
766
771
/// Locked amount can immediately be used for staking.
767
772
#[ pallet:: call_index( 7 ) ]
768
- #[ pallet:: weight( T :: WeightInfo :: lock( ) ) ]
769
- pub fn lock ( origin : OriginFor < T > , #[ pallet:: compact] amount : Balance ) -> DispatchResult {
773
+ #[ pallet:: weight( T :: WeightInfo :: lock_new_account( ) . max( T :: WeightInfo :: lock_existing_account( ) ) ) ]
774
+ pub fn lock (
775
+ origin : OriginFor < T > ,
776
+ #[ pallet:: compact] amount : Balance ,
777
+ ) -> DispatchResultWithPostInfo {
770
778
Self :: ensure_pallet_enabled ( ) ?;
771
779
let account = ensure_signed ( origin) ?;
772
780
773
781
let mut ledger = Ledger :: < T > :: get ( & account) ;
774
782
783
+ // Only do the check for new accounts.
784
+ // External logic should ensure that accounts which are already participating in dApp staking aren't
785
+ // allowed to participate elsewhere where they shouldn't.
786
+ let is_new_account = ledger. is_empty ( ) ;
787
+ if is_new_account {
788
+ ensure ! (
789
+ T :: AccountCheck :: allowed_to_stake( & account) ,
790
+ Error :: <T >:: AccountNotAvailableForDappStaking
791
+ ) ;
792
+ }
793
+
775
794
// Calculate & check amount available for locking
776
795
let available_balance =
777
- T :: Currency :: balance ( & account) . saturating_sub ( ledger. active_locked_amount ( ) ) ;
796
+ T :: Currency :: total_balance ( & account) . saturating_sub ( ledger. active_locked_amount ( ) ) ;
778
797
let amount_to_lock = available_balance. min ( amount) ;
779
798
ensure ! ( !amount_to_lock. is_zero( ) , Error :: <T >:: ZeroAmount ) ;
780
799
@@ -795,7 +814,12 @@ pub mod pallet {
795
814
amount : amount_to_lock,
796
815
} ) ;
797
816
798
- Ok ( ( ) )
817
+ Ok ( Some ( if is_new_account {
818
+ T :: WeightInfo :: lock_new_account ( )
819
+ } else {
820
+ T :: WeightInfo :: lock_existing_account ( )
821
+ } )
822
+ . into ( ) )
799
823
}
800
824
801
825
/// Attempts to start the unlocking process for the specified amount.
0 commit comments