Skip to content

Commit

Permalink
feat: add option to set primary-name in claim_reserved_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimish Agrawal authored and wottpal committed Sep 4, 2023
1 parent 7789a76 commit 779ed69
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions azns_registry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ mod azns_registry {

/// Allows users to claim their reserved name at zero cost
#[ink(message)]
pub fn claim_reserved_name(&mut self, name: String) -> Result<()> {
pub fn claim_reserved_name(
&mut self,
name: String,
set_as_primary_name: bool,
) -> Result<()> {
let caller = self.env().caller();

let Some(user) = self.reserved_names.get(&name) else {
Expand All @@ -463,6 +467,10 @@ mod azns_registry {
let expiry_time = self.env().block_timestamp() + YEAR;
self.register_name(&name, &caller, expiry_time)
.and_then(|_| {
if set_as_primary_name {
self.set_primary_name(Some(name.clone()))?;
}

// Remove the name from the list once claimed
self.reserved_names.remove(&name);
self.env().emit_event(Reserve {
Expand Down Expand Up @@ -2884,19 +2892,19 @@ mod tests {

// Non-reserved name cannot be claimed
assert_eq!(
contract.claim_reserved_name("abcd".to_string()),
contract.claim_reserved_name("abcd".to_string(), false),
Err(Error::NotReservedName),
);

// Non-authorised user cannot claim reserved name
assert_eq!(
contract.claim_reserved_name(name.clone()),
contract.claim_reserved_name(name.clone(), false),
Err(Error::NotAuthorised),
);

// Authorised user can claim name reserved for them
set_next_caller(accounts.bob);
assert!(contract.claim_reserved_name(name.clone()).is_ok());
assert!(contract.claim_reserved_name(name.clone(), false).is_ok());

let address_dict = AddressDict::new(accounts.bob);
assert_eq!(
Expand Down

0 comments on commit 779ed69

Please sign in to comment.