@@ -3,7 +3,7 @@ use polkadot_sdk::{
33 frame_support:: { CloneNoBound , DebugNoBound , EqNoBound , PartialEqNoBound } ,
44 frame_system:: pallet_prelude:: BlockNumberFor ,
55 sp_runtime:: {
6- traits:: { One , Saturating } ,
6+ traits:: { One , Saturating , Zero } ,
77 FixedPointNumber , FixedU128 ,
88 } ,
99} ;
@@ -112,3 +112,42 @@ pub struct NamespaceMetadata<T: Config> {
112112 /// Storage deposit paid for this namespace
113113 pub deposit : BalanceOf < T > ,
114114}
115+
116+ pub fn find_missing_paths < T : Config > (
117+ owner : & T :: AccountId ,
118+ path : & NamespacePath ,
119+ ) -> Vec < NamespacePath > {
120+ let mut paths_to_create = path. parents ( ) ;
121+ paths_to_create. insert ( 0 , path. clone ( ) ) ;
122+
123+ for ( i, segment) in paths_to_create. iter ( ) . enumerate ( ) . rev ( ) {
124+ if !Namespaces :: < T > :: contains_key ( owner, segment) {
125+ return paths_to_create. get ( ..=i) . unwrap_or_default ( ) . to_vec ( ) ;
126+ }
127+ }
128+
129+ Default :: default ( )
130+ }
131+
132+ /// Calculates the total cost for registering, (Fee, Deposit)
133+ pub fn calculate_cost < T : Config > (
134+ owner : & T :: AccountId ,
135+ missing_paths : & [ NamespacePath ] ,
136+ ) -> Result < ( BalanceOf < T > , BalanceOf < T > ) , DispatchError > {
137+ let current_count = NamespaceCount :: < T > :: get ( owner) ;
138+
139+ let pricing_config = crate :: NamespacePricingConfig :: < T > :: get ( ) ;
140+ let mut total_fee = BalanceOf :: < T > :: zero ( ) ;
141+ let mut total_deposit = BalanceOf :: < T > :: zero ( ) ;
142+
143+ for ( index, path) in missing_paths. iter ( ) . enumerate ( ) {
144+ let count = current_count. saturating_add ( index as u32 ) ;
145+ let fee = pricing_config. namespace_fee ( count) ?;
146+ let deposit = pricing_config. namespace_deposit ( path) ;
147+
148+ total_fee = total_fee. saturating_add ( fee) ;
149+ total_deposit = total_deposit. saturating_add ( deposit) ;
150+ }
151+
152+ Ok ( ( total_fee, total_deposit) )
153+ }
0 commit comments