Skip to content

Commit 9a23448

Browse files
authoredApr 13, 2023
Merge pull request #89 from azero-domains/feat/minor-functionality-changes
Feat/minor functionality changes
2 parents f4c6f41 + ac627a8 commit 9a23448

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed
 

‎azns_registry/lib.rs

+38-11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ mod azns_registry {
154154
RecordNotFound,
155155
/// Withdraw failed
156156
WithdrawFailed,
157+
/// Insufficient balance in the contract
158+
InsufficientBalance,
157159
/// No resolved address found
158160
NoResolvedAddress,
159161
/// A user can claim only one name during the whitelist-phase
@@ -223,7 +225,9 @@ mod azns_registry {
223225

224226
// Initializing reserved names
225227
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");
227231
}
228232

229233
// No Whitelist phase
@@ -700,12 +704,20 @@ mod azns_registry {
700704
/// (ADMIN-OPERATION)
701705
/// Transfers `value` amount of tokens to the caller.
702706
#[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<()> {
704712
self.ensure_admin()?;
705713

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());
707716

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() {
709721
return Err(Error::WithdrawFailed);
710722
}
711723

@@ -732,14 +744,21 @@ mod azns_registry {
732744
pub fn add_reserved_names(&mut self, set: Vec<(String, Option<AccountId>)>) -> Result<()> {
733745
self.ensure_admin()?;
734746

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+
736755
self.reserved_names.insert(&name, addr);
737756
self.env().emit_event(Reserve {
738757
name: name.clone(),
739758
account_id: *addr,
740759
action: true,
741760
});
742-
});
761+
}
743762
Ok(())
744763
}
745764

@@ -1352,7 +1371,7 @@ mod tests {
13521371

13531372
let balance_before =
13541373
get_account_balance::<DefaultEnvironment>(default_accounts.alice).unwrap();
1355-
assert_eq!(contract.withdraw(fees), Ok(()));
1374+
assert_eq!(contract.withdraw(None, Some(fees)), Ok(()));
13561375
let balance_after =
13571376
get_account_balance::<DefaultEnvironment>(default_accounts.alice).unwrap();
13581377

@@ -1373,10 +1392,7 @@ mod tests {
13731392
assert_eq!(contract.register(name, 1, None, None, false), Ok(()));
13741393

13751394
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));
13801396
}
13811397

13821398
#[ink::test]
@@ -1881,6 +1897,17 @@ mod tests {
18811897
vec![NameStatus::Reserved(Some(accounts.alice))],
18821898
);
18831899

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+
18841911
// Invocation from non-admin address fails
18851912
set_next_caller(accounts.bob);
18861913
assert_eq!(contract.add_reserved_names(vec![]), Err(Error::NotAdmin));

0 commit comments

Comments
 (0)