@@ -154,6 +154,8 @@ mod azns_registry {
154
154
RecordNotFound ,
155
155
/// Withdraw failed
156
156
WithdrawFailed ,
157
+ /// Insufficient balance in the contract
158
+ InsufficientBalance ,
157
159
/// No resolved address found
158
160
NoResolvedAddress ,
159
161
/// A user can claim only one name during the whitelist-phase
@@ -223,7 +225,9 @@ mod azns_registry {
223
225
224
226
// Initializing reserved names
225
227
if let Some ( set) = reserved_names {
226
- contract. add_reserved_names ( set) . expect ( "Infallible" ) ;
228
+ contract
229
+ . add_reserved_names ( set)
230
+ . expect ( "Invalid reserve name detected" ) ;
227
231
}
228
232
229
233
// No Whitelist phase
@@ -700,12 +704,20 @@ mod azns_registry {
700
704
/// (ADMIN-OPERATION)
701
705
/// Transfers `value` amount of tokens to the caller.
702
706
#[ ink( message) ]
703
- pub fn withdraw ( & mut self , value : Balance ) -> Result < ( ) > {
707
+ pub fn withdraw (
708
+ & mut self ,
709
+ beneficiary : Option < AccountId > ,
710
+ value : Option < Balance > ,
711
+ ) -> Result < ( ) > {
704
712
self . ensure_admin ( ) ?;
705
713
706
- assert ! ( value <= Self :: env( ) . balance( ) , "insufficient funds!" ) ;
714
+ let beneficiary = beneficiary. unwrap_or ( self . env ( ) . caller ( ) ) ;
715
+ let value = value. unwrap_or ( self . env ( ) . balance ( ) ) ;
707
716
708
- if Self :: env ( ) . transfer ( Self :: env ( ) . caller ( ) , value) . is_err ( ) {
717
+ if value > self . env ( ) . balance ( ) {
718
+ return Err ( Error :: InsufficientBalance ) ;
719
+ }
720
+ if self . env ( ) . transfer ( beneficiary, value) . is_err ( ) {
709
721
return Err ( Error :: WithdrawFailed ) ;
710
722
}
711
723
@@ -732,14 +744,21 @@ mod azns_registry {
732
744
pub fn add_reserved_names ( & mut self , set : Vec < ( String , Option < AccountId > ) > ) -> Result < ( ) > {
733
745
self . ensure_admin ( ) ?;
734
746
735
- set. iter ( ) . for_each ( |( name, addr) | {
747
+ for ( name, addr) in set. iter ( ) {
748
+ if name. is_empty ( ) {
749
+ return Err ( Error :: NameEmpty ) ;
750
+ }
751
+ if self . has_name_expired ( name) == Ok ( false ) {
752
+ return Err ( Error :: NameAlreadyExists ) ;
753
+ }
754
+
736
755
self . reserved_names . insert ( & name, addr) ;
737
756
self . env ( ) . emit_event ( Reserve {
738
757
name : name. clone ( ) ,
739
758
account_id : * addr,
740
759
action : true ,
741
760
} ) ;
742
- } ) ;
761
+ }
743
762
Ok ( ( ) )
744
763
}
745
764
@@ -1352,7 +1371,7 @@ mod tests {
1352
1371
1353
1372
let balance_before =
1354
1373
get_account_balance :: < DefaultEnvironment > ( default_accounts. alice ) . unwrap ( ) ;
1355
- assert_eq ! ( contract. withdraw( fees) , Ok ( ( ) ) ) ;
1374
+ assert_eq ! ( contract. withdraw( None , Some ( fees) ) , Ok ( ( ) ) ) ;
1356
1375
let balance_after =
1357
1376
get_account_balance :: < DefaultEnvironment > ( default_accounts. alice ) . unwrap ( ) ;
1358
1377
@@ -1373,10 +1392,7 @@ mod tests {
1373
1392
assert_eq ! ( contract. register( name, 1 , None , None , false ) , Ok ( ( ) ) ) ;
1374
1393
1375
1394
set_next_caller ( default_accounts. bob ) ;
1376
- assert_eq ! (
1377
- contract. withdraw( 160_u128 * 10_u128 . pow( 12 ) ) ,
1378
- Err ( Error :: NotAdmin )
1379
- ) ;
1395
+ assert_eq ! ( contract. withdraw( None , None ) , Err ( Error :: NotAdmin ) ) ;
1380
1396
}
1381
1397
1382
1398
#[ ink:: test]
@@ -1881,6 +1897,17 @@ mod tests {
1881
1897
vec![ NameStatus :: Reserved ( Some ( accounts. alice) ) ] ,
1882
1898
) ;
1883
1899
1900
+ // Cannot reserve already registered-name
1901
+ let name = "alice" . to_string ( ) ;
1902
+ set_value_transferred :: < DefaultEnvironment > ( 160_u128 * 10_u128 . pow ( 12 ) ) ;
1903
+ contract
1904
+ . register ( name. clone ( ) , 1 , None , None , false )
1905
+ . unwrap ( ) ;
1906
+ assert_eq ! (
1907
+ contract. add_reserved_names( vec![ ( name, None ) ] ) ,
1908
+ Err ( Error :: NameAlreadyExists )
1909
+ ) ;
1910
+
1884
1911
// Invocation from non-admin address fails
1885
1912
set_next_caller ( accounts. bob ) ;
1886
1913
assert_eq ! ( contract. add_reserved_names( vec![ ] ) , Err ( Error :: NotAdmin ) ) ;
0 commit comments