Skip to content

Commit

Permalink
feat: store registration timestamp as well
Browse files Browse the repository at this point in the history
  • Loading branch information
realnimish committed Apr 12, 2023
1 parent da7620b commit 0fc7c39
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions azns_registry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ mod azns_registry {

/// Mapping from name to addresses associated with it
name_to_address_dict: Mapping<String, AddressDict, ManualKey<200>>,
/// Mapping from name to its expiry timestamp
name_to_expiry: Mapping<String, u64>,
/// Mapping from name to its registration period (timestamp)
name_to_period: Mapping<String, (u64, u64)>,
/// Metadata
metadata: Mapping<String, Vec<(String, String)>, ManualKey<201>>,
metadata_size_limit: Option<u32>,
Expand Down Expand Up @@ -226,7 +226,7 @@ mod azns_registry {
name_checker,
fee_calculator,
name_to_address_dict: Mapping::default(),
name_to_expiry: Mapping::default(),
name_to_period: Mapping::default(),
owner_to_names: Default::default(),
metadata: Default::default(),
address_to_primary_name: Default::default(),
Expand Down Expand Up @@ -574,9 +574,16 @@ mod azns_registry {
self.get_address_dict_ref(&name).map(|x| x.resolved)
}

#[ink(message)]
pub fn get_registration_date(&self, name: String) -> Result<u64> {
self.get_registration_period_ref(&name)
.map(|(registration, _)| registration)
}

#[ink(message)]
pub fn get_expiry_date(&self, name: String) -> Result<u64> {
self.name_to_expiry.get(&name).ok_or(Error::NameDoesntExist)
self.get_registration_period_ref(&name)
.map(|(_, expiry)| expiry)
}

/// Gets all records
Expand Down Expand Up @@ -854,9 +861,11 @@ mod azns_registry {
_ => (), // Name is available
}

let registration = self.env().block_timestamp();

let address_dict = AddressDict::new(recipient.clone());
self.name_to_address_dict.insert(name, &address_dict);
self.name_to_expiry.insert(name, &expiry);
self.name_to_period.insert(name, &(registration, expiry));

/* Update convenience mapping for owned names */
self.add_name_to_owner(recipient, name);
Expand Down Expand Up @@ -890,7 +899,7 @@ mod azns_registry {
};

self.name_to_address_dict.remove(name);
self.name_to_expiry.remove(name);
self.name_to_period.remove(name);
self.metadata.remove(name);

self.remove_name_from_owner(&address_dict.owner, &name);
Expand Down Expand Up @@ -1159,9 +1168,13 @@ mod azns_registry {
.unwrap_or_default()
}

fn get_registration_period_ref(&self, name: &str) -> Result<(u64, u64)> {
self.name_to_period.get(name).ok_or(Error::NameDoesntExist)
}

fn has_name_expired(&self, name: &str) -> Result<bool> {
match self.name_to_expiry.get(name) {
Some(expiry) => Ok(expiry <= self.env().block_timestamp()),
match self.name_to_period.get(name) {
Some((_, expiry)) => Ok(expiry <= self.env().block_timestamp()),
None => Err(Error::NameDoesntExist),
}
}
Expand Down Expand Up @@ -1350,8 +1363,14 @@ mod azns_registry {
.map(|key| match key.as_str() {
"TLD" => self.tld.clone(),
"Length" => name.chars().count().to_string(),
"Registration" => "".to_string(),
"Expiration" => "".to_string(),
"Registration" => match self.get_registration_period_ref(&name) {
Ok(period) => period.0.to_string(),
_ => String::new(),
},
"Expiration" => match self.get_registration_period_ref(&name) {
Ok(period) => period.1.to_string(),
_ => String::new(),
},
_ => "".to_string(),
})
.collect()
Expand Down

0 comments on commit 0fc7c39

Please sign in to comment.