Skip to content

Commit

Permalink
Add primary name functionality to router
Browse files Browse the repository at this point in the history
  • Loading branch information
BriungRi committed Aug 10, 2023
1 parent 9daf00b commit e829fdc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 4 deletions.
104 changes: 104 additions & 0 deletions router/sources/primary_name_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#[test_only]
module router::primary_name_tests {
use router::router;
use router::test_helper;
use std::option;
use std::signer::address_of;
use std::string::utf8;
use std::vector;

const SECONDS_PER_YEAR: u64 = 60 * 60 * 24 * 365;

#[test(
router = @router,
aptos_names = @aptos_names,
aptos_names_v2 = @aptos_names_v2,
user1 = @0x077,
user2 = @0x266f,
aptos = @0x1,
foundation = @0xf01d
)]
fun test_set_primary_name(
router: &signer,
aptos_names: &signer,
aptos_names_v2: &signer,
user1: signer,
user2: signer,
aptos: signer,
foundation: signer
) {
router::init_module_for_test(router);
let users = test_helper::e2e_test_setup(aptos_names, aptos_names_v2, user1, &aptos, user2, &foundation);
let user = vector::borrow(&users, 0);
let user_addr = address_of(user);
let domain_name = utf8(b"test");
let subdomain_name = utf8(b"test");
let subdomain_name_opt = option::some(subdomain_name);

// Register with v1
router::register_domain(user, domain_name, SECONDS_PER_YEAR);
router::register_subdomain(user, domain_name, subdomain_name, SECONDS_PER_YEAR, 0);

// Set domain as primary
router::set_primary_name(user, domain_name, option::none());
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(*option::borrow(&primary_domain_name) == domain_name, 1);
assert!(option::is_none(&primary_subdomain_name), 2);
};

// Set subdomain as primary
router::set_primary_name(user, domain_name, subdomain_name_opt);
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(*option::borrow(&primary_domain_name) == domain_name, 3);
assert!(*option::borrow(&primary_subdomain_name) == subdomain_name, 4);
};

// Clear primary name
router::clear_primary_name(user);
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(option::is_none(&primary_domain_name), 5);
assert!(option::is_none(&primary_subdomain_name), 6);
};

// Bump mode
router::set_mode(router, 1);

// Primary name should still be cleared after version bump
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(option::is_none(&primary_domain_name), 5);
assert!(option::is_none(&primary_subdomain_name), 6);
};

// Migrate domain and subdomain
router::migrate_name(user, domain_name, option::none());
router::migrate_name(user, domain_name, subdomain_name_opt);

// Set domain as primary
router::set_primary_name(user, domain_name, option::none());
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(*option::borrow(&primary_domain_name) == domain_name, 7);
assert!(option::is_none(&primary_subdomain_name), 8);
};

// Set subdomain as primary
router::set_primary_name(user, domain_name, subdomain_name_opt);
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(*option::borrow(&primary_domain_name) == domain_name, 9);
assert!(*option::borrow(&primary_subdomain_name) == subdomain_name, 10);
};

// Clear primary name
router::clear_primary_name(user);
{
let (primary_subdomain_name, primary_domain_name) = router::get_primary_name(user_addr);
assert!(option::is_none(&primary_domain_name), 11);
assert!(option::is_none(&primary_subdomain_name), 12);
};
}
}
52 changes: 48 additions & 4 deletions router/sources/router.move
Original file line number Diff line number Diff line change
Expand Up @@ -356,22 +356,57 @@ module router::router {

// ==== EXPIRATION ====

// Not available in MODE_V1
public entry fun renew_domain(
_user: &signer,
_domain_name: String
) {}
) acquires RouterConfig {
let mode = get_mode();
if (mode == MODE_V1) {
// Will not be implemented in v1
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else if (mode == MODE_V1_AND_V2) {
// TODO: Implement
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else {
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
}
}

// Not available in MODE_V1
public entry fun set_subdomain_expiration_policy(
_user: &signer,
_domain_name: String,
_subdomain_name: String,
) {}
) acquires RouterConfig {
let mode = get_mode();
if (mode == MODE_V1) {
// Will not be implemented in v1
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else if (mode == MODE_V1_AND_V2) {
// TODO: Implement
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else {
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
}
}

public entry fun set_subdomain_expiration(
_user: &signer,
_subdomain_name: String,
_domain_name: String,
) {}
) acquires RouterConfig {
let mode = get_mode();
if (mode == MODE_V1) {
// Will not be implemented in v1
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else if (mode == MODE_V1_AND_V2) {
// TODO: Implement
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
} else {
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
}
}

// ==== REVERSE REGISTRATION ====

Expand All @@ -398,7 +433,16 @@ module router::router {
}
}

public entry fun clear_primary_name(_user: &signer) {}
public entry fun clear_primary_name(user: &signer) acquires RouterConfig {
let mode = get_mode();
if (mode == MODE_V1) {
aptos_names::domains::clear_reverse_lookup(user);
} else if (mode == MODE_V1_AND_V2) {
aptos_names_v2::domains::clear_reverse_lookup(user);
} else {
abort error::not_implemented(ENOT_IMPLEMENTED_IN_MODE)
}
}

// ==== METADATA ====

Expand Down

0 comments on commit e829fdc

Please sign in to comment.