diff --git a/packages/coupons/sources/coupons.move b/packages/coupons/sources/coupons.move index 976617cd..88000b80 100644 --- a/packages/coupons/sources/coupons.move +++ b/packages/coupons/sources/coupons.move @@ -9,19 +9,12 @@ /// Each coupon is validated towards a list of rules. View `rules` module for explanation. /// The app is authorized on `SuiNS` to be able to claim names and add earnings to the registry. module coupons::coupons { - use std::string::{Self, String}; + use std::string::String; - use sui::table::{Self, Table}; - use sui::tx_context::{TxContext, sender}; - use sui::object::{Self, UID}; - use sui::transfer; - use sui::dynamic_field::{Self as df}; - use sui::clock::Clock; - use sui::sui::SUI; - use sui::coin::{Self, Coin}; + use sui::{table::{Self, Table}, dynamic_field::{Self as df}, clock::Clock, sui::SUI, coin::Coin}; - use coupons::rules::{Self, CouponRules}; - use coupons::constants; + use coupons::{rules::{Self, CouponRules}, constants}; + use suins::{domain, suins::{Self, AdminCap, SuiNS}, suins_registration::SuinsRegistration, config::Self, registry::Registry}; /// Coupon already exists const ECouponAlreadyExists: u64 = 0; @@ -41,12 +34,6 @@ module coupons::coupons { /// Our versioning of the coupons package. const VERSION: u8 = 1; - // use suins::config; - use suins::domain; - use suins::suins::{Self, AdminCap, SuiNS}; // re-use AdminCap for creating new coupons. - use suins::suins_registration::SuinsRegistration; - use suins::config::{Self, Config}; - use suins::registry::{Self, Registry}; // Authorization for the Coupons on SuiNS, to be able to register names on the app. public struct CouponsApp has drop {} @@ -99,37 +86,37 @@ module coupons::coupons { clock: &Clock, ctx: &mut TxContext ): SuinsRegistration { - assert_version_is_valid(self); + self.assert_version_is_valid(); // Validate that specified coupon is valid. - assert!(table::contains(&mut self.data.coupons, coupon_code), ECouponNotExists); + assert!(self.data.coupons.contains(coupon_code), ECouponNotExists); // Verify coupon house is authorized to buy names. - suins::assert_app_is_authorized(suins); + suins.assert_app_is_authorized(); // Validate registration years are in [0,5] range. assert!(no_years > 0 && no_years <= 5, EInvalidYearsArgument); - let config = suins::get_config(suins); + let config = suins.get_config(); let domain = domain::new(domain_name); - let label = domain::sld(&domain); + let label = domain.sld(); - let domain_length = (string::length(label) as u8); + let domain_length = (label.length() as u8); // Borrow coupon from the table. - let coupon = table::borrow_mut(&mut self.data.coupons, coupon_code); + let coupon = &mut self.data.coupons[coupon_code]; // We need to do a total of 5 checks, based on `CouponRules` // Our checks work with `AND`, all of the conditions must pass for a coupon to be used. // 1. Validate domain size. - rules::assert_coupon_valid_for_domain_size(&coupon.rules, domain_length); + coupon.rules.assert_coupon_valid_for_domain_size(domain_length); // 2. Decrease available claims. Will ABORT if the coupon doesn't have enough available claims. - rules::decrease_available_claims(&mut coupon.rules); + coupon.rules.decrease_available_claims(); // 3. Validate the coupon is valid for the specified user. - rules::assert_coupon_valid_for_address(&coupon.rules, sender(ctx)); + coupon.rules.assert_coupon_valid_for_address(ctx.sender()); // 4. Validate the coupon hasn't expired (Based on clock) - rules::assert_coupon_is_not_expired(&coupon.rules, clock); + coupon.rules.assert_coupon_is_not_expired(clock); // 5. Validate years are valid for the coupon. - rules::assert_coupon_valid_for_domain_years(&coupon.rules, no_years); + coupon.rules.assert_coupon_valid_for_domain_years(no_years); // Validate name can be registered (is main domain (no subdomain) and length is valid) config::assert_valid_user_registerable_domain(&domain); @@ -137,17 +124,17 @@ module coupons::coupons { let original_price = config::calculate_price(config, domain_length, no_years); let sale_price = internal_calculate_sale_price(original_price, coupon); - assert!(coin::value(&payment) == sale_price, EIncorrectAmount); - suins::app_add_balance(CouponsApp {}, suins, coin::into_balance(payment)); + assert!(payment.value() == sale_price, EIncorrectAmount); + suins::app_add_balance(CouponsApp {}, suins, payment.into_balance()); // Clean up our registry by removing the coupon if no more available claims! - if(!rules::has_available_claims(&coupon.rules)){ + if(!coupon.rules.has_available_claims()){ // remove the coupon, since it's no longer usable. - internal_remove_coupon(&mut self.data, coupon_code); + self.data.internal_remove_coupon(coupon_code); }; let registry = suins::app_registry_mut(CouponsApp {}, suins); - registry::add_record(registry, domain, no_years, clock, ctx) + registry.add_record(domain, no_years, clock, ctx) } // A convenient helper to calculate the price in a PTB. @@ -155,9 +142,9 @@ module coupons::coupons { // Nor does it calculate the original price. This is part of the Frontend anyways. public fun calculate_sale_price(self: &mut CouponHouse, price: u64, coupon_code: String): u64 { // Validate that specified coupon is valid. - assert!(table::contains(&mut self.data.coupons, coupon_code), ECouponNotExists); + assert!(self.data.coupons.contains(coupon_code), ECouponNotExists); // Borrow coupon from the table. - let coupon = table::borrow_mut(&mut self.data.coupons, coupon_code); + let coupon = &mut self.data.coupons[coupon_code]; internal_calculate_sale_price(price, coupon) } @@ -262,8 +249,8 @@ module coupons::coupons { code: String, coupon: Coupon ) { - assert!(!table::contains(&mut self.coupons, code), ECouponAlreadyExists); - table::add(&mut self.coupons, code, coupon); + assert!(!self.coupons.contains(code), ECouponAlreadyExists); + self.coupons.add(code, coupon); } /// An internal function to create a coupon object. @@ -282,7 +269,7 @@ module coupons::coupons { // A function to remove a coupon from the system. fun internal_remove_coupon(self: &mut Data, code: String) { - table::remove(&mut self.coupons, code); + self.coupons.remove(code); } // test only functions. diff --git a/packages/coupons/sources/range.move b/packages/coupons/sources/range.move index 4643fc86..a78b6c33 100644 --- a/packages/coupons/sources/range.move +++ b/packages/coupons/sources/range.move @@ -4,8 +4,6 @@ /// A module to introduce `range` checks for the rules. module coupons::range { - use std::vector; - /// Invalid [from, to] setup in the range! /// `to` parameter has to be >= `from` const EInvalidRange: u64 = 0; @@ -31,12 +29,12 @@ module coupons::range { /// Get floor limit for the range. public fun from(range: &Range): u8 { - *vector::borrow(&range.vec, 0) + range.vec[0] } /// Get upper limit for the range. public fun to(range: &Range): u8 { - *vector::borrow(&range.vec, 1) + range.vec[1] } } diff --git a/packages/coupons/sources/rules.move b/packages/coupons/sources/rules.move index 59504e23..5e0be1cd 100644 --- a/packages/coupons/sources/rules.move +++ b/packages/coupons/sources/rules.move @@ -4,14 +4,9 @@ // A module with a couple of helpers for validation of coupons // validation of names etc. module coupons::rules { + use sui::clock::Clock; - use std::vector; - use std::option::{Self, Option}; - - use sui::clock::{Self, Clock}; - - use coupons::constants; - use coupons::range::{Self, Range}; + use coupons::{constants, range::Range}; use suins::constants::{Self as suins_constants}; // Errors @@ -64,7 +59,7 @@ module coupons::rules { ): CouponRules { assert!(is_valid_years_range(&years), EInvalidYears); assert!(is_valid_length_range(&length), EInvalidLengthRule); - assert!(option::is_none(&available_claims) || (*option::borrow(&available_claims) > 0), EInvalidAvailableClaims); + assert!(available_claims.is_none() || (*available_claims.borrow() > 0), EInvalidAvailableClaims); CouponRules { length, available_claims, user, expiration, years } @@ -87,19 +82,19 @@ module coupons::rules { /// We shouldn't get here ever, as we're checking this on the coupon creation, but /// keeping it as a sanity check (e.g. created a coupon with 0 available claims). public fun decrease_available_claims(rules: &mut CouponRules) { - if(option::is_some(&rules.available_claims)){ + if(rules.available_claims.is_some()){ assert!(has_available_claims(rules), ENoMoreAvailableClaims); // Decrease available claims by 1. - let available_claims = *option::borrow(&rules.available_claims); - option::swap(&mut rules.available_claims, available_claims - 1); + let available_claims = *rules.available_claims.borrow(); + rules.available_claims.swap(available_claims - 1); } } // Checks whether a coupon has available claims. // Returns true if the rule is not set OR it has used all the available claims. public fun has_available_claims(rules: &CouponRules): bool { - if(option::is_none(&rules.available_claims)) return true; - *option::borrow(&rules.available_claims) > 0 + if(rules.available_claims.is_none()) return true; + *rules.available_claims.borrow() > 0 } // Assertion helper for the validity of years. @@ -113,13 +108,13 @@ module coupons::rules { // 1. Exact years (e.g. 2 years, by passing [2,2]) // 2. Range of years (e.g. [1,3]) public fun is_coupon_valid_for_domain_years(rules: &CouponRules, target: u8): bool { - if(option::is_none(&rules.years)) return true; + if(rules.years.is_none()) return true; - range::is_in_range(option::borrow(&rules.years), target) + rules.years.borrow().is_in_range(target) } public fun assert_is_valid_discount_type(`type`: u8) { - assert!(vector::contains(&constants::discount_rule_types(), &`type`), EInvalidType); + assert!(constants::discount_rule_types().contains(&`type`), EInvalidType); } // verify that we are creating the coupons correctly (based on amount & type). @@ -139,9 +134,9 @@ module coupons::rules { /// We check the length of the name based on the domain length rule public fun is_coupon_valid_for_domain_size(rules: &CouponRules, length: u8): bool { // If the vec is not set, we pass this rule test. - if(option::is_none(&rules.length)) return true; + if(rules.length.is_none()) return true; - range::is_in_range(option::borrow(&rules.length), length) + rules.length.borrow().is_in_range(length) } @@ -152,8 +147,8 @@ module coupons::rules { } /// Check that the domain is valid for the specified address public fun is_coupon_valid_for_address(rules: &CouponRules, user: address): bool { - if(option::is_none(&rules.user)) return true; - *option::borrow(&rules.user) == user + if(rules.user.is_none()) return true; + rules.user.borrow() == user } /// Simple assertion for the coupon expiration. @@ -164,23 +159,23 @@ module coupons::rules { /// Check whether a coupon has expired public fun is_coupon_expired(rules: &CouponRules, clock: &Clock): bool { - if(option::is_none(&rules.expiration)){ + if(rules.expiration.is_none()){ return false }; - clock::timestamp_ms(clock) > *option::borrow(&rules.expiration) + clock.timestamp_ms() > *rules.expiration.borrow() } fun is_valid_years_range(range: &Option): bool { - if(option::is_none(range)) return true; - let range = option::borrow(range); - range::from(range) >= 1 && range::to(range) <= 5 + if(range.is_none()) return true; + let range = range.borrow(); + range.from() >= 1 && range.to() <= 5 } fun is_valid_length_range(range: &Option): bool { - if(option::is_none(range)) return true; - let range = option::borrow(range); - range::from(range) >= suins_constants::min_domain_length() && range::to(range) <= suins_constants::max_domain_length() + if(range.is_none()) return true; + let range = range.borrow(); + range.from() >= suins_constants::min_domain_length() && range.to() <= suins_constants::max_domain_length() } } diff --git a/packages/coupons/tests/authorization_tests.move b/packages/coupons/tests/authorization_tests.move index 631e5ac7..0fd7611f 100644 --- a/packages/coupons/tests/authorization_tests.move +++ b/packages/coupons/tests/authorization_tests.move @@ -5,61 +5,69 @@ #[test_only] module coupons::app_authorization_tests { - // use std::string::{utf8, String}; - use sui::test_scenario::{Self}; + use sui::test_scenario::{return_shared, return_to_sender, end}; - use coupons::coupons::{Self, CouponHouse}; - use suins::suins::{AdminCap}; - use coupons::setup::{Self, UnauthorizedTestApp, TestApp, admin, user}; + use coupons::{ + coupons::{app_data_mut, deauthorize_app}, + setup::{ + Self, + UnauthorizedTestApp, + TestApp, + admin, + user, + test_init, + unauthorized_test_app + } + }; #[test] fun admin_get_app_success() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; // auth style as authorized app { - test_scenario::next_tx(scenario, user()); - let mut coupon_house = test_scenario::take_shared(scenario); - coupons::app_data_mut(setup::test_app(), &mut coupon_house); - test_scenario::return_shared(coupon_house); + scenario.next_tx(user()); + let mut coupon_house = scenario.take_shared(); + app_data_mut(setup::test_app(), &mut coupon_house); + return_shared(coupon_house); }; - test_scenario::end(scenario_val); + end(scenario_val); } #[test] fun authorized_app_get_app_success(){ - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; { - test_scenario::next_tx(scenario, admin()); + scenario.next_tx(admin()); - let mut coupon_house = test_scenario::take_shared(scenario); - let admin_cap = test_scenario::take_from_sender(scenario); + let mut coupon_house = scenario.take_shared(); + let admin_cap = scenario.take_from_sender(); // test app deauthorization. - coupons::deauthorize_app(&admin_cap, &mut coupon_house); + deauthorize_app(&admin_cap, &mut coupon_house); // test that the app is indeed non authorized - assert!(!coupons::is_app_authorized(&coupon_house), 0); + assert!(!coupon_house.is_app_authorized(), 0); - test_scenario::return_to_sender(scenario, admin_cap); - test_scenario::return_shared(coupon_house); + return_to_sender(scenario, admin_cap); + return_shared(coupon_house); }; - test_scenario::end(scenario_val); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::coupons::EAppNotAuthorized)] fun unauthorized_app_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; { - test_scenario::next_tx(scenario, user()); - let mut coupon_house = test_scenario::take_shared(scenario); - coupons::app_data_mut(setup::unauthorized_test_app(), &mut coupon_house); - test_scenario::return_shared(coupon_house); + scenario.next_tx(user()); + let mut coupon_house = scenario.take_shared(); + app_data_mut(unauthorized_test_app(), &mut coupon_house); + return_shared(coupon_house); }; - test_scenario::end(scenario_val); + end(scenario_val); } } diff --git a/packages/coupons/tests/coupons_tests.move b/packages/coupons/tests/coupons_tests.move index 3204a791..1c580ef2 100644 --- a/packages/coupons/tests/coupons_tests.move +++ b/packages/coupons/tests/coupons_tests.move @@ -3,69 +3,70 @@ #[test_only] module coupons::coupon_tests { - use std::option; use std::string::{utf8}; - use sui::test_scenario::{Self, Scenario, ctx}; + use sui::test_scenario::{Self, Scenario, ctx, return_shared, end}; // test dependencies. - use coupons::setup::{Self, TestApp, user, user_two, mist_per_sui}; - use coupons::coupons::{Self, CouponHouse}; - use coupons::constants::{Self}; - use coupons::rules; - use coupons::range; + use coupons::{ + setup::{Self, TestApp, user, user_two, mist_per_sui, test_app, admin_add_coupon, register_with_coupon, test_init}, + coupons::{Self, CouponHouse}, + constants::{Self}, + rules, + range + }; // populate a lot of coupons with different cases. // This populates the coupon as an authorized app fun populate_coupons(scenario: &mut Scenario) { - test_scenario::next_tx(scenario, user()); - let mut coupon_house = test_scenario::take_shared(scenario); + scenario.next_tx(user()); + let mut coupon_house = scenario.take_shared(); - let data_mut = coupons::app_data_mut(setup::test_app(), &mut coupon_house); + let data_mut = coupons::app_data_mut(test_app(), &mut coupon_house); setup::populate_coupons(data_mut, ctx(scenario)); - test_scenario::return_shared(coupon_house); + return_shared(coupon_house); } // Please look up at `setup` file to see all the coupon names and their respective logic. // Tests the e2e experience for coupons (a list of different coupons with different rules) #[test] fun test_e2e() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; // populate all coupons. populate_coupons(scenario); // 5 SUI discount coupon. - setup::register_with_coupon(utf8(b"5_SUI_DISCOUNT"), utf8(b"test.sui"), 1, 195 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"5_SUI_DISCOUNT"), utf8(b"test.sui"), 1, 195 * mist_per_sui(), 0, user(), scenario); // original price would be 400 (200*2 years). 25% discount should bring it down to 300. - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_MAX_2_YEARS"), utf8(b"jest.sui"), 2, 300 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_MAX_2_YEARS"), utf8(b"jest.sui"), 2, 300 * mist_per_sui(), 0, user(), scenario); // Test that this user-specific coupon works as expected - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"fest.sui"), 2, 300 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"fest.sui"), 2, 300 * mist_per_sui(), 0, user(), scenario); // 50% discount only on names 5+ digits - setup::register_with_coupon(utf8(b"50_PERCENT_5_PLUS_NAMES"), utf8(b"testo.sui"), 1, 25 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"50_PERCENT_5_PLUS_NAMES"), utf8(b"testo.sui"), 1, 25 * mist_per_sui(), 0, user(), scenario); // 50% discount only on names 3 digit names. - setup::register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"tes.sui"), 1, 600 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"tes.sui"), 1, 600 * mist_per_sui(), 0, user(), scenario); // 50% DISCOUNT, with all possible rules involved. - setup::register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"teso.sui"), 1, 100 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"teso.sui"), 1, 100 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + end(scenario_val); } #[test] fun zero_fee_purchase(){ - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; // populate all coupons. populate_coupons(scenario); // 5 SUI discount coupon. - setup::admin_add_coupon(utf8(b"100_SUI_OFF"), constants::fixed_price_discount_type(), 100 * mist_per_sui(), scenario); + admin_add_coupon(utf8(b"100_SUI_OFF"), constants::fixed_price_discount_type(), 100 * mist_per_sui(), scenario); // Buy a name for free using the 100 SUI OFF coupon! - setup::register_with_coupon(utf8(b"100_SUI_OFF"), utf8(b"testo.sui"), 1, 0 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"100_SUI_OFF"), utf8(b"testo.sui"), 1, 0 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test] fun specific_max_years(){ @@ -100,7 +101,7 @@ module coupons::coupon_tests { #[test] fun test_price_calculation(){ - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); { @@ -111,146 +112,146 @@ module coupons::coupon_tests { assert!(sale_price == 50, 1); test_scenario::return_shared(coupon_house); }; - test_scenario::end(scenario_val); + end(scenario_val); } // Tests the e2e experience for coupons (a list of different coupons with different rules) #[test, expected_failure(abort_code=::coupons::coupons::EIncorrectAmount)] fun test_invalid_coin_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; // populate all coupons. populate_coupons(scenario); // 5 SUI discount coupon. - setup::register_with_coupon(utf8(b"5_SUI_DISCOUNT"), utf8(b"test.sui"), 1, 200 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"5_SUI_DISCOUNT"), utf8(b"test.sui"), 1, 200 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::coupons::ECouponNotExists)] fun no_more_available_claims_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 0, user(), scenario); - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"tost.sui"), 1, 150 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 0, user(), scenario); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"tost.sui"), 1, 150 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::coupons::EInvalidYearsArgument)] fun invalid_years_claim_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 6, 150 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 6, 150 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::coupons::EInvalidYearsArgument)] fun invalid_years_claim_1_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 0, 150 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 0, 150 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidUser)] fun invalid_user_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 0, user_two(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"25_PERCENT_DISCOUNT_USER_ONLY"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 0, user_two(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::ECouponExpired)] fun coupon_expired_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"tes.sui"), 1, 150 * mist_per_sui(), 2, user_two(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"tes.sui"), 1, 150 * mist_per_sui(), 2, user_two(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::ENotValidYears)] fun coupon_not_valid_for_years_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"tes.sui"), 3, 150 * mist_per_sui(), 0, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"tes.sui"), 3, 150 * mist_per_sui(), 0, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidForDomainLength)] fun coupon_invalid_length_1_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 2, user_two(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"50_PERCENT_3_DIGITS"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 2, user_two(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidForDomainLength)] fun coupon_invalid_length_2_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); // Tries to use 5 digit name for a <=4 digit one. - setup::register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"testo.sui"), 1, 150 * mist_per_sui(), 2, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"50_DISCOUNT_SALAD"), utf8(b"testo.sui"), 1, 150 * mist_per_sui(), 2, user(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidForDomainLength)] fun coupon_invalid_length_3_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); // Tries to use 4 digit name for a 5+ chars coupon. - setup::register_with_coupon(utf8(b"50_PERCENT_5_PLUS_NAMES"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 2, user(), scenario); - test_scenario::end(scenario_val); + register_with_coupon(utf8(b"50_PERCENT_5_PLUS_NAMES"), utf8(b"test.sui"), 1, 150 * mist_per_sui(), 2, user(), scenario); + end(scenario_val); } #[test] fun add_coupon_as_admin() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); // add a no rule coupon as an admin - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::fixed_price_discount_type(), 100 * mist_per_sui(), scenario); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::fixed_price_discount_type(), 100 * mist_per_sui(), scenario); setup::admin_remove_coupon(utf8(b"TEST_SUCCESS_ADDITION"), scenario); - test_scenario::end(scenario_val); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidType)] fun add_coupon_invalid_type_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), 5, 100 * mist_per_sui(), scenario); - test_scenario::end(scenario_val); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), 5, 100 * mist_per_sui(), scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidAmount)] fun add_coupon_invalid_amount_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 101, scenario); - test_scenario::end(scenario_val); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 101, scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::rules::EInvalidAmount)] fun add_coupon_invalid_amount_2_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 0, scenario); - test_scenario::end(scenario_val); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 0, scenario); + end(scenario_val); } #[test, expected_failure(abort_code=::coupons::coupons::ECouponAlreadyExists)] fun add_coupon_twice_failure() { - let mut scenario_val = setup::test_init(); + let mut scenario_val = test_init(); let scenario = &mut scenario_val; populate_coupons(scenario); - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 100, scenario); - setup::admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 100, scenario); - test_scenario::end(scenario_val); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 100, scenario); + admin_add_coupon(utf8(b"TEST_SUCCESS_ADDITION"), constants::percentage_discount_type(), 100, scenario); + end(scenario_val); } } diff --git a/packages/coupons/tests/setup.move b/packages/coupons/tests/setup.move index fb04be92..d00edd23 100644 --- a/packages/coupons/tests/setup.move +++ b/packages/coupons/tests/setup.move @@ -3,20 +3,11 @@ #[test_only] module coupons::setup { - use std::option; use std::string::{utf8, String}; - use sui::clock::{Self, Clock}; - use sui::test_scenario::{Self, Scenario, ctx}; - use sui::tx_context::{TxContext}; - use sui::sui::SUI; - use sui::coin::{Self}; - use sui::transfer; + use sui::{clock::{Self, Clock}, test_scenario::{Self, Scenario, ctx}, sui::SUI, coin::{Self}}; - use coupons::coupons::{Self, CouponsApp,Data, CouponHouse}; - use coupons::rules::{Self}; - use coupons::constants; - use coupons::range; + use coupons::{coupons::{Self, CouponsApp,Data, CouponHouse}, rules::{Self}, constants, range}; public struct TestApp has drop {} @@ -44,12 +35,12 @@ module coupons::setup { clock::share_for_testing(clock); }; { - test_scenario::next_tx(scenario, ADMIN_ADDRESS); - let mut coupon_house = test_scenario::take_shared(scenario); + scenario.next_tx(ADMIN_ADDRESS); + let mut coupon_house = scenario.take_shared(); // get admin cap - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); // authorize TestApp to CouponHouse. coupons::authorize_app(&admin_cap, &mut coupon_house); @@ -194,9 +185,9 @@ module coupons::setup { // Adds a 0 rule coupon that gives 15% discount to test admin additions. public fun admin_add_coupon(code_name: String, `type`: u8, value: u64, scenario: &mut Scenario) { - test_scenario::next_tx(scenario, admin()); - let mut coupon_house = test_scenario::take_shared(scenario); - let cap = test_scenario::take_from_sender(scenario); + scenario.next_tx(admin()); + let mut coupon_house = scenario.take_shared(); + let cap = scenario.take_from_sender(); coupons::admin_add_coupon( &cap, &mut coupon_house, @@ -206,20 +197,20 @@ module coupons::setup { rules::new_empty_rules(), ctx(scenario) ); - test_scenario::return_to_sender(scenario, cap); + scenario.return_to_sender(cap); test_scenario::return_shared(coupon_house); } // Adds a 0 rule coupon that gives 15% discount to test admin additions. public fun admin_remove_coupon(code_name: String, scenario: &mut Scenario) { - test_scenario::next_tx(scenario, admin()); - let mut coupon_house = test_scenario::take_shared(scenario); - let cap = test_scenario::take_from_sender(scenario); + scenario.next_tx(admin()); + let mut coupon_house = scenario.take_shared(); + let cap = scenario.take_from_sender(); coupons::admin_remove_coupon( &cap, &mut coupon_house, code_name ); - test_scenario::return_to_sender(scenario, cap); + scenario.return_to_sender(cap); test_scenario::return_shared(coupon_house); } @@ -230,16 +221,15 @@ module coupons::setup { // 5 digit -> 50 // A helper to easily register a name with a coupon code. public fun register_with_coupon(coupon_code: String, domain_name: String, no_years: u8, amount: u64, clock_value: u64, user: address, scenario: &mut Scenario) { - test_scenario::next_tx(scenario, user); - let mut clock = test_scenario::take_shared(scenario); - clock::increment_for_testing(&mut clock, clock_value); - let mut coupon_house = test_scenario::take_shared(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(user); + let mut clock = scenario.take_shared(); + clock.increment_for_testing(clock_value); + let mut coupon_house = scenario.take_shared(); + let mut suins = scenario.take_shared(); let payment = coin::mint_for_testing(amount, ctx(scenario)); - let nft = coupons::register_with_coupon( - &mut coupon_house, + let nft = coupon_house.register_with_coupon( &mut suins, coupon_code, domain_name, diff --git a/packages/denylist/sources/denylist.move b/packages/denylist/sources/denylist.move index 97cfd89b..530bc311 100644 --- a/packages/denylist/sources/denylist.move +++ b/packages/denylist/sources/denylist.move @@ -3,9 +3,7 @@ module denylist::denylist { use std::string::String; - use std::vector; - use sui::tx_context::{TxContext}; use sui::table::{Self, Table}; use suins::suins::{Self, AdminCap, SuiNS}; @@ -35,12 +33,12 @@ module denylist::denylist { /// Check for a reserved name public fun is_reserved_name(suins: &SuiNS, name: String): bool { - table::contains(&denylist(suins).reserved, name) + denylist(suins).reserved.contains(name) } /// Checks for a blocked name. public fun is_blocked_name(suins: &SuiNS, name: String): bool { - table::contains(&denylist(suins).blocked, name) + denylist(suins).blocked.contains(name) } /// Add a list of reserved names to the list as admin. @@ -65,7 +63,7 @@ module denylist::denylist { /// Get immutable access to the registry. fun denylist(suins: &SuiNS): &Denylist { - suins::registry(suins) + suins.registry() } /// Internal helper to get access to the BlockedNames object @@ -75,27 +73,27 @@ module denylist::denylist { /// Internal helper to batch add words to a table. fun internal_add_names_to_list(table: &mut Table, words: vector) { - assert!(vector::length(&words) > 0, ENoWordsInList); + assert!(words.length() > 0, ENoWordsInList); - let mut i = vector::length(&words); + let mut i = words.length(); while (i > 0) { i = i - 1; - let word = *vector::borrow(&words, i); - table::add(table, word, true); + let word = words[i]; + table.add(word, true); }; } /// Internal helper to remove words from a table. fun internal_remove_names_from_list(table: &mut Table, words: vector) { - assert!(vector::length(&words) > 0, ENoWordsInList); + assert!(words.length() > 0, ENoWordsInList); - let mut i = vector::length(&words); + let mut i = words.length(); while (i > 0) { i = i - 1; - let word = *vector::borrow(&words, i); - table::remove(table, word); + let word = words[i]; + table.remove(word); }; } diff --git a/packages/denylist/tests/denylist_tests.move b/packages/denylist/tests/denylist_tests.move index a0c71c2a..0cf2a745 100644 --- a/packages/denylist/tests/denylist_tests.move +++ b/packages/denylist/tests/denylist_tests.move @@ -2,10 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 module denylist::denylist_tests { - use std::vector; use std::string::{utf8, String}; - use sui::test_scenario::{Self as ts, ctx, Scenario}; + use sui::test_scenario::{Self as ts, Scenario}; use suins::suins::{Self, SuiNS}; @@ -18,9 +17,9 @@ module denylist::denylist_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - ts::next_tx(scenario, ADDR); - let mut suins = ts::take_shared(scenario); - let cap = suins::create_admin_cap_for_testing(ctx(scenario)); + scenario.next_tx(ADDR); + let mut suins = scenario.take_shared(); + let cap = suins::create_admin_cap_for_testing(scenario.ctx()); denylist::add_reserved_names(&mut suins, &cap, some_reserved_names()); denylist::add_blocked_names(&mut suins, &cap, some_offensive_names()); @@ -38,7 +37,7 @@ module denylist::denylist_tests { suins::burn_admin_cap_for_testing(cap); ts::return_shared(suins); - ts::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::denylist::denylist::ENoWordsInList)] @@ -46,9 +45,9 @@ module denylist::denylist_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - ts::next_tx(scenario, ADDR); - let mut suins = ts::take_shared(scenario); - let cap = suins::create_admin_cap_for_testing(ctx(scenario)); + scenario.next_tx(ADDR); + let mut suins = scenario.take_shared(); + let cap = suins::create_admin_cap_for_testing(scenario.ctx()); denylist::add_reserved_names(&mut suins, &cap, vector[]); @@ -61,9 +60,9 @@ module denylist::denylist_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - ts::next_tx(scenario, ADDR); - let mut suins = ts::take_shared(scenario); - let cap = suins::create_admin_cap_for_testing(ctx(scenario)); + scenario.next_tx(ADDR); + let mut suins = scenario.take_shared(); + let cap = suins::create_admin_cap_for_testing(scenario.ctx()); denylist::add_blocked_names(&mut suins, &cap, vector[]); @@ -75,9 +74,9 @@ module denylist::denylist_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - ts::next_tx(scenario, ADDR); - let mut suins = ts::take_shared(scenario); - let cap = suins::create_admin_cap_for_testing(ctx(scenario)); + scenario.next_tx(ADDR); + let mut suins = scenario.take_shared(); + let cap = suins::create_admin_cap_for_testing(scenario.ctx()); denylist::add_blocked_names(&mut suins, &cap, some_offensive_names()); @@ -90,7 +89,7 @@ module denylist::denylist_tests { suins::burn_admin_cap_for_testing(cap); ts::return_shared(suins); - ts::end(scenario_val); + scenario_val.end(); } #[test] @@ -98,9 +97,9 @@ module denylist::denylist_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - ts::next_tx(scenario, ADDR); - let mut suins = ts::take_shared(scenario); - let cap = suins::create_admin_cap_for_testing(ctx(scenario)); + scenario.next_tx(ADDR); + let mut suins = scenario.take_shared(); + let cap = suins::create_admin_cap_for_testing(scenario.ctx()); denylist::add_reserved_names(&mut suins, &cap, some_reserved_names()); @@ -115,7 +114,7 @@ module denylist::denylist_tests { suins::burn_admin_cap_for_testing(cap); ts::return_shared(suins); - ts::end(scenario_val); + scenario_val.end(); } // data preparation @@ -123,15 +122,15 @@ module denylist::denylist_tests { public fun test_init(): (Scenario) { let mut scenario = ts::begin(ADDR); { - ts::next_tx(&mut scenario, ADDR); + scenario.next_tx(ADDR); - let (mut suins, cap) = suins::new_for_testing(ctx(&mut scenario)); + let (mut suins, cap) = suins::new_for_testing(scenario.ctx()); - suins::authorize_app_for_testing(&mut suins); + suins.authorize_app_for_testing(); - denylist::setup(&mut suins, &cap, ctx(&mut scenario)); + denylist::setup(&mut suins, &cap, scenario.ctx()); - suins::share_for_testing(suins); + suins.share_for_testing(); suins::burn_admin_cap_for_testing(cap); }; @@ -142,17 +141,17 @@ module denylist::denylist_tests { fun some_reserved_names(): vector { let mut vec: vector = vector::empty(); - vector::push_back(&mut vec, utf8(b"test")); - vector::push_back(&mut vec, utf8(b"test2")); - vector::push_back(&mut vec, utf8(b"test3")); + vec.push_back(utf8(b"test")); + vec.push_back(utf8(b"test2")); + vec.push_back(utf8(b"test3")); vec } fun some_offensive_names(): vector { let mut vec: vector = vector::empty(); - vector::push_back(&mut vec, utf8(b"bad_test")); - vector::push_back(&mut vec, utf8(b"bad_test2")); - vector::push_back(&mut vec, utf8(b"bad_test3")); + vec.push_back(utf8(b"bad_test")); + vec.push_back(utf8(b"bad_test2")); + vec.push_back(utf8(b"bad_test3")); vec } } diff --git a/packages/discounts/sources/discounts.move b/packages/discounts/sources/discounts.move index 24e71fae..caf77eb7 100644 --- a/packages/discounts/sources/discounts.move +++ b/packages/discounts/sources/discounts.move @@ -8,19 +8,11 @@ /// Can be called only when promotions are active for a specific type T. /// Activation / deactivation happens through PTBs. module discounts::discounts { - use std::option::{Option}; - use std::string::{Self, String}; - use std::type_name::{Self as `type`}; + use std::{string::String, type_name::{Self as `type`}}; - use sui::tx_context::{TxContext}; - use sui::dynamic_field::{Self as df}; - use sui::clock::{Clock}; - use sui::coin::{Self, Coin}; - use sui::sui::SUI; + use sui::{dynamic_field::{Self as df}, clock::{Clock}, coin::Coin, sui::SUI}; - use suins::domain::{Self}; - use suins::suins::{Self, AdminCap, SuiNS}; - use suins::suins_registration::SuinsRegistration; + use suins::{domain::{Self}, suins::{Self, AdminCap, SuiNS}, suins_registration::SuinsRegistration}; // The base shared object. use discounts::house::{Self, DiscountHouse}; @@ -105,7 +97,7 @@ module discounts::discounts { four_char_price: u64, five_plus_char_price: u64 ) { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); assert!(!df::exists_(house::uid_mut(self), DiscountKey {}), EConfigExists); df::add(house::uid_mut(self), DiscountKey{}, DiscountConfig { @@ -117,9 +109,9 @@ module discounts::discounts { /// An admin action to deauthorize type T from getting discounts. public fun deauthorize_type(_: &AdminCap, self: &mut DiscountHouse) { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); assert_config_exists(self); - df::remove, DiscountConfig>(house::uid_mut(self), DiscountKey{}); + df::remove, DiscountConfig>(self.uid_mut(), DiscountKey{}); } /// Internal helper to handle the registration process @@ -131,15 +123,15 @@ module discounts::discounts { clock: &Clock, ctx: &mut TxContext ): SuinsRegistration { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); // validate that there's a configuration for type T. assert_config_exists(self); let domain = domain::new(domain_name); - let price = calculate_price(df::borrow(house::uid_mut(self), DiscountKey{}), (string::length(domain::sld(&domain)) as u8)); + let price = calculate_price(df::borrow(self.uid_mut(), DiscountKey{}), (domain.sld().length() as u8)); - assert!(coin::value(&payment) == price, EIncorrectAmount); - suins::app_add_balance(house::suins_app_auth(), suins, coin::into_balance(payment)); + assert!(payment.value() == price, EIncorrectAmount); + suins::app_add_balance(house::suins_app_auth(), suins, payment.into_balance()); house::friend_add_registry_entry(suins, domain, clock, ctx) } diff --git a/packages/discounts/sources/free_claims.move b/packages/discounts/sources/free_claims.move index 89b94248..154889a6 100644 --- a/packages/discounts/sources/free_claims.move +++ b/packages/discounts/sources/free_claims.move @@ -9,22 +9,10 @@ /// Activation / deactivation happens through PTBs. module discounts::free_claims { - use std::vector; - use std::string; + use std::{string::{String}, type_name::{Self as `type`}}; + use sui::{dynamic_field::{Self as df}, clock::{Clock}, linked_table::{Self, LinkedTable}}; - use std::string::{String}; - use std::type_name::{Self as `type`}; - - use sui::object::{Self, ID}; - use sui::tx_context::{TxContext}; - - use sui::dynamic_field::{Self as df}; - use sui::clock::{Clock}; - use sui::linked_table::{Self, LinkedTable}; - - use suins::domain::{Self, Domain}; - use suins::suins::{AdminCap, SuiNS}; - use suins::suins_registration::SuinsRegistration; + use suins::{domain::{Self, Domain}, suins::{AdminCap, SuiNS}, suins_registration::SuinsRegistration}; use discounts::house::{Self, DiscountHouse}; @@ -97,7 +85,7 @@ module discounts::free_claims { object: &T, ctx: &mut TxContext ): SuinsRegistration { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); // validate that there's a configuration for type T. assert_config_exists(self); @@ -106,11 +94,11 @@ module discounts::free_claims { let id = object::id(object); // validate that the supplied object hasn't been used to claim a free name. - let config = df::borrow_mut, FreeClaimsConfig>(house::uid_mut(self), FreeClaimsKey{}); - assert!(!linked_table::contains(&config.used_objects, id), EAlreadyClaimed); + let config = df::borrow_mut, FreeClaimsConfig>(self.uid_mut(), FreeClaimsKey{}); + assert!(!config.used_objects.contains(id), EAlreadyClaimed); // add the supplied object's id to the used objects list. - linked_table::push_back(&mut config.used_objects, id, true); + config.used_objects.push_back(id, true); // Now validate the domain, and that the rule applies here. let domain = domain::new(domain_name); @@ -127,13 +115,13 @@ module discounts::free_claims { domain_length_range: vector, ctx: &mut TxContext ) { - house::assert_version_is_valid(self); - assert!(!df::exists_(house::uid_mut(self), FreeClaimsKey {}), EConfigExists); + self.assert_version_is_valid(); + assert!(!df::exists_(self.uid_mut(), FreeClaimsKey {}), EConfigExists); // validate the range is valid. assert_valid_length_setup(&domain_length_range); - df::add(house::uid_mut(self), FreeClaimsKey{}, FreeClaimsConfig { + df::add(self.uid_mut(), FreeClaimsKey{}, FreeClaimsConfig { domain_length_range, used_objects: linked_table::new(ctx) }); @@ -143,37 +131,37 @@ module discounts::free_claims { /// Deauthorization also brings storage rebates by destroying the table of used objects. /// If we re-authorize a type, objects can be re-used, but that's considered a separate promotion. public fun deauthorize_type(_: &AdminCap, self: &mut DiscountHouse) { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); assert_config_exists(self); - let FreeClaimsConfig { mut used_objects, domain_length_range: _ } = df::remove, FreeClaimsConfig>(house::uid_mut(self), FreeClaimsKey{}); + let FreeClaimsConfig { mut used_objects, domain_length_range: _ } = df::remove, FreeClaimsConfig>(self.uid_mut(), FreeClaimsKey{}); // parse each entry and remove it. Gives us storage rebates. - while(linked_table::length(&used_objects) > 0) { - linked_table::pop_front(&mut used_objects); + while(used_objects.length() > 0) { + used_objects.pop_front(); }; - linked_table::destroy_empty(used_objects); + used_objects.destroy_empty(); } /// Worried by the 1000 DFs load limit, I introduce a `drop_type` function now /// to make sure we can force-finish a promotion for type `T`. public fun force_deauthorize_type(_: &AdminCap, self: &mut DiscountHouse) { - house::assert_version_is_valid(self); + self.assert_version_is_valid(); assert_config_exists(self); - let FreeClaimsConfig { used_objects, domain_length_range: _ } = df::remove, FreeClaimsConfig>(house::uid_mut(self), FreeClaimsKey{}); - linked_table::drop(used_objects); + let FreeClaimsConfig { used_objects, domain_length_range: _ } = df::remove, FreeClaimsConfig>(self.uid_mut(), FreeClaimsKey{}); + used_objects.drop(); } // Validate that there is a config for `T` fun assert_config_exists(self: &mut DiscountHouse) { - assert!(df::exists_with_type, FreeClaimsConfig>(house::uid_mut(self), FreeClaimsKey {}), EConfigNotExists); + assert!(df::exists_with_type, FreeClaimsConfig>(self.uid_mut(), FreeClaimsKey {}), EConfigNotExists); } /// Validate that the domain length is valid for the passed configuration. fun assert_domain_length_eligible(domain: &Domain, config: &FreeClaimsConfig) { - let domain_length = (string::length(domain::sld(domain)) as u8); - let from = *vector::borrow(&config.domain_length_range, 0); - let to = *vector::borrow(&config.domain_length_range, 1); + let domain_length = (domain.sld().length() as u8); + let from = config.domain_length_range[0]; + let to = config.domain_length_range[1]; assert!(domain_length >= from && domain_length <= to, EInvalidCharacterRange); } @@ -181,10 +169,10 @@ module discounts::free_claims { // Validate that our range setup is right. fun assert_valid_length_setup(domain_length_range: &vector) { - assert!(vector::length(domain_length_range) == 2, EInvalidCharacterRange); + assert!(domain_length_range.length() == 2, EInvalidCharacterRange); - let from = *vector::borrow(domain_length_range, 0); - let to = *vector::borrow(domain_length_range, 1); + let from = domain_length_range[0]; + let to = domain_length_range[1]; assert!(to >= from, EInvalidCharacterRange); } diff --git a/packages/discounts/sources/house.move b/packages/discounts/sources/house.move index 6b475a78..1af58811 100644 --- a/packages/discounts/sources/house.move +++ b/packages/discounts/sources/house.move @@ -5,16 +5,15 @@ /// and exports some package utilities for the 2 systems to use. module discounts::house { - use sui::object::{Self, UID}; - use sui::tx_context::{TxContext}; - use sui::transfer; use sui::clock::{Clock}; - use suins::domain::{Domain}; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, AdminCap, SuiNS}; - use suins::config; - use suins::suins_registration::SuinsRegistration; + use suins::{ + domain::Domain, + registry::Registry, + suins::{Self, AdminCap, SuiNS}, + config, + suins_registration::SuinsRegistration + }; // The `free_claims` module can use the shared object to attach configuration & claim names. /* friend discounts::free_claims; */ @@ -68,13 +67,13 @@ module discounts::house { ctx: &mut TxContext ): SuinsRegistration { // Verify that app is authorized to register names. - suins::assert_app_is_authorized(suins); + suins.assert_app_is_authorized(); // Validate that the name can be registered. config::assert_valid_user_registerable_domain(&domain); let registry = suins::app_registry_mut(DiscountHouseApp {}, suins); - registry::add_record(registry, domain, REGISTRATION_YEARS, clock, ctx) + registry.add_record(domain, REGISTRATION_YEARS, clock, ctx) } /// Returns the UID of the shared object so we can add custom configuration. diff --git a/packages/discounts/tests/discount_tests.move b/packages/discounts/tests/discount_tests.move index cdfbeebd..5a42c274 100644 --- a/packages/discounts/tests/discount_tests.move +++ b/packages/discounts/tests/discount_tests.move @@ -3,20 +3,13 @@ #[test_only] module discounts::discount_tests { - use std::option; use std::string::{utf8, String}; - use sui::test_scenario::{Self as ts, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::coin::{Self, Coin}; - use sui::sui::SUI; - use sui::transfer; + use sui::{test_scenario::{Self as ts, Scenario, ctx}, clock::{Self, Clock}, coin::{Self, Coin}, sui::SUI}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::registry; + use suins::{suins::{Self, SuiNS, AdminCap}, registry}; - use discounts::house::{Self, DiscountHouse, DiscountHouseApp}; - use discounts::discounts; + use discounts::{house::{Self, DiscountHouse, DiscountHouseApp}, discounts}; use day_one::day_one::{Self, DayOne}; @@ -38,18 +31,18 @@ module discounts::discount_tests { let mut scenario_val = ts::begin(SUINS_ADDRESS); let scenario = &mut scenario_val; { - let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::share_for_testing(suins); - house::init_for_testing(ctx(scenario)); - let clock = clock::create_for_testing(ctx(scenario)); - clock::share_for_testing(clock); + let mut suins = suins::init_for_testing(scenario.ctx()); + suins.authorize_app_for_testing(); + suins.share_for_testing(); + house::init_for_testing(scenario.ctx()); + let clock = clock::create_for_testing(scenario.ctx()); + clock.share_for_testing(); }; { - ts::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = ts::take_from_sender(scenario); - let mut suins = ts::take_shared(scenario); - let mut discount_house = ts::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); + let mut discount_house = scenario.take_shared(); // a more expensive alternative. discounts::authorize_type(&admin_cap, &mut discount_house, 3*MIST_PER_SUI, 2*MIST_PER_SUI, 1*MIST_PER_SUI); @@ -57,7 +50,7 @@ module discounts::discount_tests { discounts::authorize_type(&admin_cap, &mut discount_house, MIST_PER_SUI / 20, MIST_PER_SUI / 10, MIST_PER_SUI / 5); discounts::authorize_type(&admin_cap, &mut discount_house, MIST_PER_SUI, MIST_PER_SUI, MIST_PER_SUI); - registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); + registry::init_for_testing(&admin_cap, &mut suins, scenario.ctx()); ts::return_shared(discount_house); ts::return_shared(suins); @@ -73,12 +66,12 @@ module discounts::discount_tests { payment: Coin, user: address ) { - ts::next_tx(scenario, user); - let mut suins = ts::take_shared(scenario); - let mut discount_house = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + scenario.next_tx(user); + let mut suins = scenario.take_shared(); + let mut discount_house = scenario.take_shared(); + let clock = scenario.take_shared(); - let name = discounts::register(&mut discount_house, &mut suins, item, domain_name, payment, &clock, option::none(), ctx(scenario)); + let name = discounts::register(&mut discount_house, &mut suins, item, domain_name, payment, &clock, option::none(), scenario.ctx()); transfer::public_transfer(name, user); @@ -94,12 +87,12 @@ module discounts::discount_tests { payment: Coin, user: address ) { - ts::next_tx(scenario, user); - let mut suins = ts::take_shared(scenario); - let mut discount_house = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + scenario.next_tx(user); + let mut suins = scenario.take_shared(); + let mut discount_house = scenario.take_shared(); + let clock = scenario.take_shared(); - let name = discounts::register_with_day_one(&mut discount_house, &mut suins, item, domain_name, payment, &clock, option::none(), ctx(scenario)); + let name = discounts::register_with_day_one(&mut discount_house, &mut suins, item, domain_name, payment, &clock, option::none(), scenario.ctx()); transfer::public_transfer(name, user); @@ -114,7 +107,7 @@ module discounts::discount_tests { let scenario = &mut scenario_val; let test_item = TestAuthorized {}; - let payment: Coin = coin::mint_for_testing(2*MIST_PER_SUI, ctx(scenario)); + let payment: Coin = coin::mint_for_testing(2*MIST_PER_SUI, scenario.ctx()); register_with_type( &test_item, @@ -124,7 +117,7 @@ module discounts::discount_tests { USER_ADDRESS ); - ts::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::discounts::discounts::EConfigNotExists)] @@ -133,7 +126,7 @@ module discounts::discount_tests { let scenario = &mut scenario_val; let test_item = TestUnauthorized {}; - let payment: Coin = coin::mint_for_testing(2*MIST_PER_SUI, ctx(scenario)); + let payment: Coin = coin::mint_for_testing(2*MIST_PER_SUI, scenario.ctx()); register_with_type( &test_item, @@ -142,7 +135,7 @@ module discounts::discount_tests { payment, USER_ADDRESS ); - ts::end(scenario_val); + scenario_val.end(); } #[test] @@ -150,9 +143,9 @@ module discounts::discount_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - let mut day_one = day_one::mint_for_testing(ctx(scenario)); + let mut day_one = day_one::mint_for_testing(scenario.ctx()); day_one::set_is_active_for_testing(&mut day_one, true); - let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, ctx(scenario)); + let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, scenario.ctx()); register_with_day_one( &day_one, @@ -162,8 +155,8 @@ module discounts::discount_tests { USER_ADDRESS ); - day_one::burn_for_testing(day_one); - ts::end(scenario_val); + day_one.burn_for_testing(); + scenario_val.end(); } #[test, expected_failure(abort_code = ::discounts::discounts::ENotValidForDayOne)] @@ -171,9 +164,9 @@ module discounts::discount_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - let mut day_one = day_one::mint_for_testing(ctx(scenario)); + let mut day_one = day_one::mint_for_testing(scenario.ctx()); day_one::set_is_active_for_testing(&mut day_one, true); - let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, ctx(scenario)); + let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, scenario.ctx()); register_with_type( &day_one, @@ -183,8 +176,8 @@ module discounts::discount_tests { USER_ADDRESS ); - day_one::burn_for_testing(day_one); - ts::end(scenario_val); + day_one.burn_for_testing(); + scenario_val.end(); } #[test, expected_failure(abort_code = ::discounts::discounts::ENotActiveDayOne)] @@ -192,8 +185,8 @@ module discounts::discount_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - let day_one = day_one::mint_for_testing(ctx(scenario)); - let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, ctx(scenario)); + let day_one = day_one::mint_for_testing(scenario.ctx()); + let payment: Coin = coin::mint_for_testing(MIST_PER_SUI, scenario.ctx()); register_with_day_one( &day_one, @@ -203,7 +196,7 @@ module discounts::discount_tests { USER_ADDRESS ); - day_one::burn_for_testing(day_one); - ts::end(scenario_val); + day_one.burn_for_testing(); + scenario_val.end(); } } diff --git a/packages/discounts/tests/free_claims_test.move b/packages/discounts/tests/free_claims_test.move index 5367fe9a..292ccb01 100644 --- a/packages/discounts/tests/free_claims_test.move +++ b/packages/discounts/tests/free_claims_test.move @@ -6,13 +6,9 @@ module discounts::free_claims_tests { use std::string::{utf8, String}; - use sui::object::{Self, UID}; - use sui::test_scenario::{Self as ts, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::transfer; + use sui::{test_scenario::{Self as ts, Scenario, ctx}, clock::{Self, Clock}}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::registry; + use suins::{suins::{Self, SuiNS, AdminCap}, registry}; use discounts::house::{Self, DiscountHouse, DiscountHouseApp}; use discounts::free_claims; @@ -32,23 +28,23 @@ module discounts::free_claims_tests { let mut scenario_val = ts::begin(SUINS_ADDRESS); let scenario = &mut scenario_val; { - let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::share_for_testing(suins); - house::init_for_testing(ctx(scenario)); - let clock = clock::create_for_testing(ctx(scenario)); - clock::share_for_testing(clock); + let mut suins = suins::init_for_testing(scenario.ctx()); + suins.authorize_app_for_testing(); + suins.share_for_testing(); + house::init_for_testing(scenario.ctx()); + let clock = clock::create_for_testing(scenario.ctx()); + clock.share_for_testing(); }; { ts::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = ts::take_from_sender(scenario); - let mut suins = ts::take_shared(scenario); - let mut discount_house = ts::take_shared(scenario); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); + let mut discount_house = scenario.take_shared(); // a more expensive alternative. - free_claims::authorize_type(&admin_cap, &mut discount_house, vector[10,63], ctx(scenario)); - free_claims::authorize_type(&admin_cap, &mut discount_house, vector[10,63], ctx(scenario)); - registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); + free_claims::authorize_type(&admin_cap, &mut discount_house, vector[10,63], scenario.ctx()); + free_claims::authorize_type(&admin_cap, &mut discount_house, vector[10,63], scenario.ctx()); + registry::init_for_testing(&admin_cap, &mut suins, scenario.ctx()); ts::return_shared(discount_house); ts::return_shared(suins); @@ -61,8 +57,8 @@ module discounts::free_claims_tests { let scenario = &mut scenario_val; { ts::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = ts::take_from_sender(scenario); - let mut discount_house = ts::take_shared(scenario); + let admin_cap = scenario.take_from_sender(); + let mut discount_house = scenario.take_shared(); free_claims::deauthorize_type(&admin_cap, &mut discount_house); free_claims::deauthorize_type(&admin_cap, &mut discount_house); ts::return_shared(discount_house); @@ -84,11 +80,11 @@ module discounts::free_claims_tests { user: address ) { ts::next_tx(scenario, user); - let mut suins = ts::take_shared(scenario); - let mut discount_house = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + let mut suins = scenario.take_shared(); + let mut discount_house = scenario.take_shared(); + let clock = scenario.take_shared(); - let name = free_claims::free_claim(&mut discount_house, &mut suins, item, domain_name, &clock, ctx(scenario)); + let name = free_claims::free_claim(&mut discount_house, &mut suins, item, domain_name, &clock, scenario.ctx()); transfer::public_transfer(name, user); @@ -108,7 +104,7 @@ module discounts::free_claims_tests { let mut discount_house = ts::take_shared(scenario); let clock = ts::take_shared(scenario); - let name = free_claims::free_claim_with_day_one(&mut discount_house, &mut suins, item, domain_name, &clock, ctx(scenario)); + let name = free_claims::free_claim_with_day_one(&mut discount_house, &mut suins, item, domain_name, &clock, scenario.ctx()); transfer::public_transfer(name, user); @@ -123,7 +119,7 @@ module discounts::free_claims_tests { let scenario = &mut scenario_val; let test_item = TestAuthorized { - id: object::new(ctx(scenario)) + id: object::new(scenario.ctx()) }; free_claim_with_type( @@ -142,8 +138,8 @@ module discounts::free_claims_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - let mut day_one = day_one::mint_for_testing(ctx(scenario)); - day_one::set_is_active_for_testing(&mut day_one, true); + let mut day_one = day_one::mint_for_testing(scenario.ctx()); + day_one.set_is_active_for_testing(true); free_claim_with_day_one( &day_one, @@ -152,7 +148,7 @@ module discounts::free_claims_tests { USER_ADDRESS ); - day_one::burn_for_testing(day_one); + day_one.burn_for_testing(); test_end(scenario_val); } @@ -162,7 +158,7 @@ module discounts::free_claims_tests { let scenario = &mut scenario_val; let test_item = TestAuthorized { - id: object::new(ctx(scenario)) + id: object::new(scenario.ctx()) }; free_claim_with_type( @@ -190,7 +186,7 @@ module discounts::free_claims_tests { let scenario = &mut scenario_val; let test_item = TestAuthorized { - id: object::new(ctx(scenario)) + id: object::new(scenario.ctx()) }; free_claim_with_type( @@ -210,7 +206,7 @@ module discounts::free_claims_tests { let scenario = &mut scenario_val; let test_item = TestUnauthorized { - id: object::new(ctx(scenario)) + id: object::new(scenario.ctx()) }; free_claim_with_type( diff --git a/packages/managed_names/sources/managed.move b/packages/managed_names/sources/managed.move index 5784d740..520d09a1 100644 --- a/packages/managed_names/sources/managed.move +++ b/packages/managed_names/sources/managed.move @@ -16,19 +16,11 @@ /// we're also using it to store the managed names (to avoid using separate shared objects). /// module managed_names::managed { - use std::vector; use std::string::{String}; - use std::option::{Self, Option}; - use sui::object::{Self, ID}; - use sui::table::{Self, Table}; - use sui::tx_context::{TxContext, sender}; - use sui::clock::Clock; - use sui::transfer; + use sui::{table::{Self, Table}, tx_context::sender, clock::Clock}; - use suins::domain::{Self, Domain}; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::suins::{Self, SuiNS, AdminCap}; + use suins::{domain::{Self, Domain}, suins_registration::SuinsRegistration, suins::{Self, SuiNS, AdminCap}}; /// Tries to add an NFT that has expired. const EExpiredNFT: u64 = 1; @@ -83,11 +75,11 @@ module managed_names::managed { allowed_addresses: vector
, ctx: &mut TxContext ) { - assert!(!suins_registration::has_expired(&nft, clock), EExpiredNFT); + assert!(!nft.has_expired(clock), EExpiredNFT); let managed_names = managed_names_mut(suins); - let domain = suins_registration::domain(&nft); + let domain = nft.domain(); // if the name exists. We check if it's expired, and return it to the owner. if(table::contains(&managed_names.names, domain)) { @@ -97,13 +89,13 @@ module managed_names::managed { let existing_nft = option::destroy_some(nft); - assert!(suins_registration::has_expired(&existing_nft, clock), EAlreadyExists); + assert!(existing_nft.has_expired(clock), EAlreadyExists); // transfer it back to the owner. transfer::public_transfer(existing_nft, owner); }; // add the name to the managed names list. - table::add(&mut managed_names.names, domain, ManagedName { + managed_names.names.add(domain, ManagedName { owner: sender(ctx), allowed_addresses, nft: option::some(nft) @@ -120,7 +112,7 @@ module managed_names::managed { let domain = domain::new(name); assert!(table::contains(&managed_names.names, domain), ENameNotExists); - let existing = table::remove(&mut managed_names.names, domain); + let existing = managed_names.names.remove(domain); assert!(is_owner(&existing, sender(ctx)), ENotAuthorized); @@ -140,11 +132,11 @@ module managed_names::managed { let existing = internal_get_managed_name(managed_names_mut(suins), domain::new(name)); assert!(is_owner(existing, sender(ctx)), ENotAuthorized); - while(vector::length(&addresses) > 0) { - let addr = vector::pop_back(&mut addresses); + while(addresses.length() > 0) { + let addr = addresses.pop_back(); - if(!vector::contains(&existing.allowed_addresses, &addr)) { - vector::push_back(&mut existing.allowed_addresses, addr); + if(!existing.allowed_addresses.contains(&addr)) { + existing.allowed_addresses.push_back(addr); } } } @@ -159,13 +151,13 @@ module managed_names::managed { let existing = internal_get_managed_name(managed_names_mut(suins), domain::new(name)); assert!(is_owner(existing, sender(ctx)), ENotAuthorized); - while(vector::length(&addresses) > 0) { - let addr = vector::pop_back(&mut addresses); + while(addresses.length() > 0) { + let addr = addresses.pop_back(); - let (has_address, index) = vector::index_of(&existing.allowed_addresses, &addr); + let (has_address, index) = existing.allowed_addresses.index_of(&addr); if (has_address) { - vector::remove(&mut existing.allowed_addresses, index); + existing.allowed_addresses.remove(index); } } } @@ -197,7 +189,7 @@ module managed_names::managed { let ReturnPromise { id } = promise; assert!(object::id(&nft) == id, EInvalidReturnedNFT); - let existing = internal_get_managed_name(managed_names_mut(suins), suins_registration::domain(&nft)); + let existing = internal_get_managed_name(managed_names_mut(suins), nft.domain()); // return the NFT back. option::fill(&mut existing.nft, nft) @@ -205,9 +197,9 @@ module managed_names::managed { fun internal_get_managed_name(managed_names: &mut ManagedNames, domain: Domain): &mut ManagedName { - assert!(table::contains(&managed_names.names, domain), ENameNotExists); + assert!(managed_names.names.contains(domain), ENameNotExists); - table::borrow_mut(&mut managed_names.names, domain) + &mut managed_names.names[domain] } @@ -216,13 +208,7 @@ module managed_names::managed { } /// Check if an address is authorized for borrowing. fun is_authorized_address(self: &ManagedName, addr: address): bool { - self.owner == addr || vector::contains(&self.allowed_addresses, &addr) - } - - - /// an immutable reference to the registry. - fun managed_names(self: &SuiNS): &ManagedNames { - suins::registry(self) + self.owner == addr || self.allowed_addresses.contains(&addr) } /// a mutable reference to the registry diff --git a/packages/managed_names/tests/managed_tests.move b/packages/managed_names/tests/managed_tests.move index 279367c4..ca8487d4 100644 --- a/packages/managed_names/tests/managed_tests.move +++ b/packages/managed_names/tests/managed_tests.move @@ -2,17 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 module managed_names::managed_tests { - use std::option; use std::string::{String, utf8}; - use sui::test_scenario::{Self as ts, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::transfer; - use sui::object; + use sui::{test_scenario::{Self as ts, Scenario, ctx}, clock::{Self, Clock}}; - use suins::suins_registration::{Self, new_for_testing, SuinsRegistration}; - use suins::suins::{Self, SuiNS}; - use suins::domain; + use suins::{suins_registration::{Self, new_for_testing, SuinsRegistration}, suins::{Self, SuiNS}, domain}; use managed_names::managed::{Self, ManagedNamesApp, ReturnPromise}; @@ -37,8 +31,7 @@ module managed_names::managed_tests { let nft = remove_attached_name(domain_name, USER, scenario); transfer::public_transfer(nft, USER); - - ts::end(scenario_val); + scenario_val.end(); } #[test] @@ -65,7 +58,7 @@ module managed_names::managed_tests { // Since we attached the re-registered version for the name, // the original `owner` should have received back the expired NFT. { - ts::next_tx(scenario, USER); + scenario.next_tx(USER); let mut nft_transferred_back = ts::most_recent_id_for_address(USER); assert!(option::is_some(&nft_transferred_back), 0); @@ -75,7 +68,7 @@ module managed_names::managed_tests { suins_registration::burn_for_testing(random_nft); - ts::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code=managed_names::managed::EExpiredNFT)] @@ -227,19 +220,18 @@ module managed_names::managed_tests { /// public fun test_init(): (Scenario) { let mut scenario = ts::begin(USER); - { - ts::next_tx(&mut scenario, USER); + scenario.next_tx(USER); - let clock = clock::create_for_testing(ctx(&mut scenario)); + let clock = clock::create_for_testing(scenario.ctx()); clock::share_for_testing(clock); - let (mut suins, cap) = suins::new_for_testing(ctx(&mut scenario)); + let (mut suins, cap) = suins::new_for_testing(scenario.ctx()); - suins::authorize_app_for_testing(&mut suins); + suins.authorize_app_for_testing(); managed::setup(&mut suins, &cap, ctx(&mut scenario)); - suins::share_for_testing(suins); + suins.share_for_testing(); suins::burn_admin_cap_for_testing(cap); }; @@ -248,35 +240,35 @@ module managed_names::managed_tests { } public fun attach_name(nft: SuinsRegistration, addresses: vector
, addr: address, scenario: &mut Scenario) { - ts::next_tx(scenario, addr); - let mut suins = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + scenario.next_tx(addr); + let mut suins = scenario.take_shared(); + let clock = scenario.take_shared(); - managed::attach_managed_name(&mut suins, nft, &clock, addresses, ctx(scenario)); + managed::attach_managed_name(&mut suins, nft, &clock, addresses, scenario.ctx()); ts::return_shared(clock); ts::return_shared(suins); } public fun remove_attached_name(domain_name: String, addr: address, scenario: &mut Scenario): SuinsRegistration { - ts::next_tx(scenario, addr); - let mut suins = ts::take_shared(scenario); + scenario.next_tx(addr); + let mut suins = scenario.take_shared(); - let nft = managed::remove_attached_name(&mut suins, domain_name, ctx(scenario)); + let nft = managed::remove_attached_name(&mut suins, domain_name, scenario.ctx()); ts::return_shared(suins); nft } public fun add_or_remove_addresses(name: String, addresses: vector
, add: bool, addr: address, scenario: &mut Scenario) { - ts::next_tx(scenario, addr); - let mut suins = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + scenario.next_tx(addr); + let mut suins = scenario.take_shared(); + let clock = scenario.take_shared(); if(add){ - managed::allow_addresses(&mut suins, name, addresses, ctx(scenario)); + managed::allow_addresses(&mut suins, name, addresses, scenario.ctx()); }else { - managed::revoke_addresses(&mut suins, name, addresses, ctx(scenario)); + managed::revoke_addresses(&mut suins, name, addresses, scenario.ctx()); }; ts::return_shared(clock); @@ -284,12 +276,12 @@ module managed_names::managed_tests { } public fun simulate_borrow(domain_name: String, addr: address, scenario: &mut Scenario): (SuinsRegistration, ReturnPromise) { - ts::next_tx(scenario, addr); - let mut suins = ts::take_shared(scenario); + scenario.next_tx(addr); + let mut suins = scenario.take_shared(); - let (name, promise) = managed::borrow_val(&mut suins, domain_name, ctx(scenario)); + let (name, promise) = managed::borrow_val(&mut suins, domain_name, scenario.ctx()); - assert!(suins_registration::domain(&name) == domain::new(domain_name), 0); + assert!(name.domain() == domain::new(domain_name), 0); ts::return_shared(suins); @@ -297,8 +289,8 @@ module managed_names::managed_tests { } public fun simulate_return(nft: SuinsRegistration, promise: ReturnPromise, scenario: &mut Scenario) { - ts::next_tx(scenario, USER); - let mut suins = ts::take_shared(scenario); + scenario.next_tx(USER); + let mut suins = scenario.take_shared(); managed::return_val(&mut suins, nft, promise); @@ -313,18 +305,18 @@ module managed_names::managed_tests { } public fun advance_clock_post_expiration_of_nft(nft: &SuinsRegistration, scenario: &mut Scenario) { - ts::next_tx(scenario, USER); - let mut clock = ts::take_shared(scenario); + scenario.next_tx(USER); + let mut clock = scenario.take_shared(); // expire name - clock::increment_for_testing(&mut clock, suins_registration::expiration_timestamp_ms(nft) + 1); + clock.increment_for_testing(nft.expiration_timestamp_ms() + 1); ts::return_shared(clock); } // generates a SuinsRegistration NFT for testing public fun get_nft(name: String, addr: address, scenario: &mut Scenario): SuinsRegistration { - ts::next_tx(scenario, addr); - let suins = ts::take_shared(scenario); - let clock = ts::take_shared(scenario); + scenario.next_tx(addr); + let suins = scenario.take_shared(); + let clock = scenario.take_shared(); let nft = new_for_testing( domain::new(name), 1, @@ -335,8 +327,4 @@ module managed_names::managed_tests { ts::return_shared(suins); nft } - - - - } diff --git a/packages/registration/sources/register.move b/packages/registration/sources/register.move index 1fe43d78..ba4b1ba9 100644 --- a/packages/registration/sources/register.move +++ b/packages/registration/sources/register.move @@ -2,17 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 module registration::register { - use std::string::{Self, String}; - use sui::coin::{Self, Coin}; - use sui::tx_context::TxContext; - use sui::clock::Clock; - use sui::sui::SUI; - - use suins::domain; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::config::{Self, Config}; - use suins::suins_registration::SuinsRegistration; + use std::string::String; + use sui::{coin::Coin, clock::Clock, sui::SUI}; + + use suins::{domain, registry::Registry, suins::{Self, SuiNS}, config::{Self, Config}, suins_registration::SuinsRegistration}; /// Number of years passed is not within [1-5] interval. const EInvalidYearsArgument: u64 = 0; @@ -38,22 +31,22 @@ module registration::register { clock: &Clock, ctx: &mut TxContext ): SuinsRegistration { - suins::assert_app_is_authorized(suins); + suins.assert_app_is_authorized(); - let config = suins::get_config(suins); + let config = suins.get_config(); let domain = domain::new(domain_name); config::assert_valid_user_registerable_domain(&domain); assert!(0 < no_years && no_years <= 5, EInvalidYearsArgument); - let label = domain::sld(&domain); - let price = config::calculate_price(config, (string::length(label) as u8), no_years); + let label = domain.sld(); + let price = config.calculate_price((label.length() as u8), no_years); - assert!(coin::value(&payment) == price, EIncorrectAmount); + assert!(payment.value() == price, EIncorrectAmount); - suins::app_add_balance(Register {}, suins, coin::into_balance(payment)); + suins::app_add_balance(Register {}, suins, payment.into_balance()); let registry = suins::app_registry_mut(Register {}, suins); - registry::add_record(registry, domain, no_years, clock, ctx) + registry.add_record(domain, no_years, clock, ctx) } } \ No newline at end of file diff --git a/packages/registration/tests/register_tests.move b/packages/registration/tests/register_tests.move index 9899714c..f33a3210 100644 --- a/packages/registration/tests/register_tests.move +++ b/packages/registration/tests/register_tests.move @@ -6,21 +6,20 @@ module registration::register_tests { use std::string::{utf8, String}; - use sui::test_scenario::{Self, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::coin; - use sui::sui::SUI; + use sui::{test_scenario::{Self, Scenario, ctx}, clock::{Self, Clock}, coin, sui::SUI}; use registration::register::{Self, Register, register}; - use suins::constants::{mist_per_sui, grace_period_ms, year_ms}; - use suins::suins::{Self, SuiNS, total_balance, AdminCap}; - use suins::suins_registration::SuinsRegistration; - use suins::suins_registration; - use suins::domain; - use suins::registry; - use suins::config; - use suins::auction_tests; - use suins::auction::{Self, App as AuctionApp}; + use suins::{ + constants::{mist_per_sui, grace_period_ms, year_ms}, + suins::{Self, SuiNS, total_balance, AdminCap}, + suins_registration::SuinsRegistration, + suins_registration, + domain, + registry, + config, + auction_tests, + auction::{Self, App as AuctionApp} + }; const SUINS_ADDRESS: address = @0xA001; const AUCTIONED_DOMAIN_NAME: vector = b"tes-t2.sui"; @@ -30,22 +29,22 @@ module registration::register_tests { let mut scenario_val = test_scenario::begin(SUINS_ADDRESS); let scenario = &mut scenario_val; { - let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::authorize_app_for_testing(&mut suins); - suins::share_for_testing(suins); - let clock = clock::create_for_testing(ctx(scenario)); + let mut suins = suins::init_for_testing(scenario.ctx()); + suins.authorize_app_for_testing(); + suins.authorize_app_for_testing(); + suins.share_for_testing(); + let clock = clock::create_for_testing(scenario.ctx()); clock::share_for_testing(clock); }; { test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); - registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); + registry::init_for_testing(&admin_cap, &mut suins, scenario.ctx()); test_scenario::return_shared(suins); - test_scenario::return_to_sender(scenario, admin_cap); + scenario.return_to_sender(admin_cap); }; scenario_val } @@ -57,13 +56,13 @@ module registration::register_tests { amount: u64, clock_tick: u64 ): SuinsRegistration { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let mut suins = test_scenario::take_shared(scenario); - let payment = coin::mint_for_testing(amount, ctx(scenario)); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let mut suins = scenario.take_shared(); + let payment = coin::mint_for_testing(amount, scenario.ctx()); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); - let nft = register(&mut suins, domain_name, no_years, payment, &clock, ctx(scenario)); + clock.increment_for_testing(clock_tick); + let nft = register(&mut suins, domain_name, no_years, payment, &clock, scenario.ctx()); test_scenario::return_shared(clock); test_scenario::return_shared(suins); @@ -73,19 +72,19 @@ module registration::register_tests { fun deauthorize_app_util(scenario: &mut Scenario) { test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); suins::deauthorize_app(&admin_cap, &mut suins); test_scenario::return_shared(suins); - test_scenario::return_to_sender(scenario, admin_cap); + scenario.return_to_sender(admin_cap); } public fun assert_balance(scenario: &mut Scenario, amount: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let auction_house = test_scenario::take_shared(scenario); - assert!(total_balance(&auction_house) == amount, 0); + scenario.next_tx(SUINS_ADDRESS); + let auction_house = scenario.take_shared(); + assert!(auction_house.total_balance() == amount, 0); test_scenario::return_shared(auction_house); } @@ -96,23 +95,23 @@ module registration::register_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(b"abcd.sui"), 2, 400 * mist_per_sui(), 20); assert_balance(scenario, 1600 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(b"abcd.sui")), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 2 * year_ms() + 30, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(b"abcd.sui")), 0); + assert!(nft.expiration_timestamp_ms() == 2 * year_ms() + 30, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(b"abce-f12.sui"), 3, 150 * mist_per_sui(), 30); assert_balance(scenario, 1750 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(b"abce-f12.sui")), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 3 * year_ms() + 60, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(b"abce-f12.sui")), 0); + assert!(nft.expiration_timestamp_ms() == 3 * year_ms() + 60, 0); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::EInvalidTld)] @@ -121,9 +120,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"abc.move"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EIncorrectAmount)] @@ -132,9 +131,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1210 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EIncorrectAmount)] @@ -143,9 +142,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 90 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EInvalidYearsArgument)] @@ -154,9 +153,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 6, 6 * 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EInvalidYearsArgument)] @@ -165,9 +164,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 0, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -177,9 +176,9 @@ module registration::register_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util( scenario, @@ -189,14 +188,14 @@ module registration::register_tests { year_ms() + grace_period_ms() + 20, ); assert_balance(scenario, 2400 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); assert!( - suins_registration::expiration_timestamp_ms(&nft) == 2 * year_ms() + grace_period_ms() + 30, + nft.expiration_timestamp_ms() == 2 * year_ms() + grace_period_ms() + 30, 0 ); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordNotExpired)] @@ -206,14 +205,14 @@ module registration::register_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 20); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -222,9 +221,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"-ab.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -233,9 +232,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"ab-.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -244,9 +243,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"Abc.com"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::ELabelTooShort)] @@ -255,9 +254,9 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"ab.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::EInvalidDomain)] @@ -266,29 +265,29 @@ module registration::register_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"abc.xyz.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordNotExpired)] fun test_register_aborts_if_domain_name_went_through_auction() { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - auction::init_for_testing(ctx(scenario)); + auction::init_for_testing(scenario.ctx()); auction_tests::normal_auction_flow(scenario); let nft = register_util(scenario, utf8(AUCTIONED_DOMAIN_NAME), 1, 50 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] fun test_register_works_if_auctioned_domain_name_expired() { let mut scenario_val = test_init(); let scenario = &mut scenario_val; - auction::init_for_testing(ctx(scenario)); + auction::init_for_testing(scenario.ctx()); auction_tests::normal_auction_flow(scenario); let nft = register_util( @@ -300,9 +299,9 @@ module registration::register_tests { ); assert_balance(scenario, 50 * mist_per_sui()); assert!(suins_registration::domain(&nft) == domain::new(utf8(AUCTIONED_DOMAIN_NAME)), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -312,8 +311,8 @@ module registration::register_tests { deauthorize_app_util(scenario); let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } } \ No newline at end of file diff --git a/packages/renewal/sources/renew.move b/packages/renewal/sources/renew.move index 0851f8bb..729b88de 100644 --- a/packages/renewal/sources/renew.move +++ b/packages/renewal/sources/renew.move @@ -6,22 +6,16 @@ /// /// The renewal is capped at 5 years. module renewal::renew { - use std::string; - use std::option; + use sui::{coin::{Self, Coin}, clock::Clock, sui::SUI}; - use sui::coin::{Self, Coin}; - - use sui::clock::{Self, Clock}; - use sui::sui::SUI; - use sui::object; - - use suins::constants; - use suins::domain::{Self, Domain}; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::config::{Self, Config}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::name_record; + use suins::{ + constants, + domain::Domain, + registry::Registry, + suins::{Self, SuiNS, AdminCap}, + config::{Self, Config}, + suins_registration::SuinsRegistration + }; /// Number of years passed is not within [1-5] interval. const EInvalidYearsArgument: u64 = 0; @@ -75,7 +69,7 @@ module renewal::renew { clock: &Clock ) { // authorization occurs inside the call. - let domain = nft::domain(nft); + let domain = nft.domain(); // check if the name is valid, for public registration // Also checks if the domain is not a subdomain, validates label lengths, TLD. config::assert_valid_user_registerable_domain(&domain); @@ -90,7 +84,7 @@ module renewal::renew { let target_expiration = target_expiration(registry, nft, domain, clock, no_years); // set the expiration of the NFT + the registry's name record. - registry::set_expiration_timestamp_ms(registry, nft, domain, target_expiration); + registry.set_expiration_timestamp_ms(nft, domain, target_expiration); sui::event::emit(NameRenewed { domain, amount: coin::value(&payment) }); suins::app_add_balance(Renew {}, suins, coin::into_balance(payment)); @@ -105,35 +99,35 @@ module renewal::renew { clock: &Clock, no_years: u8, ): u64 { - let name_record_option = registry::lookup(registry, domain); + let name_record_option = registry.lookup(domain); // validate that the name_record still exists in the registry. assert!(option::is_some(&name_record_option), ERecordNotFound); let name_record = option::destroy_some(name_record_option); // Validate that the name has not expired. If it has, we can only re-purchase (and that might involve different pricing). - assert!(!name_record::has_expired_past_grace_period(&name_record, clock), ERecordExpired); + assert!(!name_record.has_expired_past_grace_period(clock), ERecordExpired); // validate that the supplied NFT ID matches the NFT ID of the registry. - assert!(name_record::nft_id(&name_record) == object::id(nft), ERecordNftIDMismatch); + assert!(name_record.nft_id() == object::id(nft), ERecordNftIDMismatch); // Validate that the no_years supplied makes sense. (1-5). assert!(0 < no_years && no_years <= 5, EInvalidYearsArgument); // calcualate target expiration! - let target_expiration = name_record::expiration_timestamp_ms(&name_record) + (no_years as u64) * constants::year_ms(); + let target_expiration = name_record.expiration_timestamp_ms() + (no_years as u64) * constants::year_ms(); // validate that the target expiration is not more than 6 years in the future. - assert!(target_expiration < clock::timestamp_ms(clock) + (constants::year_ms() * 6), EMoreThanSixYears); + assert!(target_expiration < clock.timestamp_ms() + (constants::year_ms() * 6), EMoreThanSixYears); target_expiration } /// Validates that the payment Coin is correct for the domain + number of years fun validate_payment(suins: &SuiNS, payment: &Coin, domain: &Domain, no_years: u8){ - let config = suins::get_config(suins); - let label = domain::sld(domain); - let price = config::calculate_price(&config.config, (string::length(label) as u8), no_years); - assert!(coin::value(payment) == price, EIncorrectAmount); + let config = suins.get_config(); + let label = domain.sld(); + let price = config.config.calculate_price((label.length() as u8), no_years); + assert!(payment.value() == price, EIncorrectAmount); } } diff --git a/packages/renewal/tests/renew_tests.move b/packages/renewal/tests/renew_tests.move index 36e7e780..2a4d95ae 100644 --- a/packages/renewal/tests/renew_tests.move +++ b/packages/renewal/tests/renew_tests.move @@ -5,17 +5,16 @@ module renewal::renew_tests { use std::string::utf8; - use sui::coin; - use sui::sui::SUI; - use sui::clock::{Self, Clock}; - use sui::tx_context::{Self, TxContext}; - - use suins::constants::{mist_per_sui, year_ms, grace_period_ms}; - use suins::suins::{Self, SuiNS}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::registry; - use suins::domain; - use suins::config; + use sui::{coin, sui::SUI, clock::{Self, Clock}}; + + use suins::{ + constants::{mist_per_sui, year_ms, grace_period_ms}, + suins::{Self, SuiNS}, + suins_registration::{Self as nft, SuinsRegistration}, + registry, + domain, + config + }; use renewal::renew::{Self as renewal, Renew}; @@ -30,14 +29,14 @@ module renewal::renew_tests { let mut clock = clock::create_for_testing(&mut ctx); - clock::increment_for_testing(&mut clock, 10); + clock.increment_for_testing(10); renew_util(&mut suins, &mut nft, 5, &clock, &mut ctx); // our fresh domain with 5 years renewal is now 6 years - assert!(nft::expiration_timestamp_ms(&nft) == clock::timestamp_ms(&clock) + (6 * year_ms()) - 10, 0); + assert!(nft.expiration_timestamp_ms() == clock.timestamp_ms() + (6 * year_ms()) - 10, 0); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); wrapup(suins); wrapup_name(nft); @@ -143,11 +142,11 @@ module renewal::renew_tests { renewal::setup(&cap, &mut suins, config); - let nft = registry::add_record(&mut registry, domain, 1,&clock, ctx); + let nft = registry.add_record(domain, 1,&clock, ctx); suins::add_registry(&cap, &mut suins, registry); suins::burn_admin_cap_for_testing(cap); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); (suins, nft) } @@ -157,6 +156,6 @@ module renewal::renew_tests { } public fun wrapup(suins: SuiNS) { - suins::share_for_testing(suins); + suins.share_for_testing(); } } diff --git a/packages/subdomains/sources/config.move b/packages/subdomains/sources/config.move index 702b75f0..ba1fb792 100644 --- a/packages/subdomains/sources/config.move +++ b/packages/subdomains/sources/config.move @@ -2,11 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 module subdomains::config { - use std::string::{Self, String}; - use std::vector; + use std::string::String; - use suins::domain::{Self, Domain, is_parent_of}; - use suins::constants::sui_tld; + use suins::{domain::{Domain, is_parent_of}, constants::sui_tld}; /// the minimum size a subdomain label can have. const MIN_LABEL_SIZE: u8 = 3; @@ -71,7 +69,7 @@ module subdomains::config { /// Validate that the depth of the subdomain is with the allowed range. public fun has_valid_depth(domain: &Domain, config: &SubDomainConfig): bool { - domain::number_of_levels(domain) <= (config.max_depth as u64) + domain.number_of_levels() <= (config.max_depth as u64) } /// Validates that the TLD of the domain is supported for subdomains. @@ -80,8 +78,8 @@ module subdomains::config { /// (E.g., with `.move` service, we might want to restrict how subdomains are created) public fun is_valid_tld(domain: &Domain, config: &SubDomainConfig): bool { let mut i=0; - while (i < vector::length(&config.allowed_tlds)) { - if (domain::tld(domain) == vector::borrow(&config.allowed_tlds, i)) { + while (i < config.allowed_tlds.length()) { + if (domain.tld() == &config.allowed_tlds[i]) { return true }; i = i + 1; @@ -94,8 +92,8 @@ module subdomains::config { /// in the `Domain` construction. public fun is_valid_label(domain: &Domain, config: &SubDomainConfig): bool { // our label is the last vector element, as labels are stored in reverse order. - let label = domain::label(domain, domain::number_of_levels(domain) - 1); - string::length(label) >= (config.min_label_size as u64) + let label = domain.label(domain.number_of_levels() - 1); + label.length() >= (config.min_label_size as u64) } } diff --git a/packages/subdomains/sources/subdomains.move b/packages/subdomains/sources/subdomains.move index bddb2295..8dc90b16 100644 --- a/packages/subdomains/sources/subdomains.move +++ b/packages/subdomains/sources/subdomains.move @@ -25,22 +25,18 @@ /// OPEN TODOS: /// module subdomains::subdomains { - use std::option; use std::string::{String, utf8}; - use sui::object::{Self, ID}; - use sui::tx_context::{TxContext}; - use sui::clock::{Self, Clock}; - use sui::dynamic_field as df; - use sui::vec_map::{Self, VecMap}; + use sui::{clock::Clock, dynamic_field as df, vec_map::VecMap}; - use suins::domain::{Self, Domain, is_subdomain}; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::subdomain_registration::{Self, SubDomainRegistration}; - use suins::constants::{subdomain_allow_extension_key, subdomain_allow_creation_key}; - use suins::name_record; + use suins::{ + domain::{Self, Domain, is_subdomain}, + registry::Registry, + suins::{Self, SuiNS, AdminCap}, + suins_registration::SuinsRegistration, + subdomain_registration::SubDomainRegistration, + constants::{subdomain_allow_extension_key, subdomain_allow_creation_key} + }; use subdomains::config::{Self, SubDomainConfig}; @@ -97,7 +93,7 @@ module subdomains::subdomains { internal_validate_nft_can_manage_subdomain(suins, parent, clock, subdomain, true); // Aborts with `suins::registry::ERecordExists` if the subdomain already exists. - registry::add_leaf_record(registry_mut(suins), subdomain, clock, target, ctx) + registry_mut(suins).add_leaf_record(subdomain, clock, target, ctx) } /// Removes a `leaf` subdomain from the registry. @@ -115,7 +111,7 @@ module subdomains::subdomains { // we can still remove a leaf name (we just can't add a new one). internal_validate_nft_can_manage_subdomain(suins, parent, clock, subdomain, false); - registry::remove_leaf_record(registry_mut(suins), subdomain) + registry_mut(suins).remove_leaf_record(subdomain) } /// Creates a new `node` subdomain @@ -149,9 +145,9 @@ module subdomains::subdomains { internal_validate_nft_can_manage_subdomain(suins, parent, clock, subdomain, true); // Validate that the duration is at least the minimum duration. - assert!(expiration_timestamp_ms >= clock::timestamp_ms(clock) + config::minimum_duration(&app_config(suins).config), EInvalidExpirationDate); + assert!(expiration_timestamp_ms >= clock.timestamp_ms() + app_config(suins).config.minimum_duration(), EInvalidExpirationDate); // validate that the requested expiration timestamp is not greater than the parent's one. - assert!(expiration_timestamp_ms <= suins_registration::expiration_timestamp_ms(parent), EInvalidExpirationDate); + assert!(expiration_timestamp_ms <= parent.expiration_timestamp_ms(), EInvalidExpirationDate); // We register the subdomain (e.g. `subdomain.example.sui`) and return the SuinsRegistration object. // Aborts with `suins::registry::ERecordExists` if the subdomain already exists. @@ -178,29 +174,29 @@ module subdomains::subdomains { ) { let registry = registry(suins); - let nft = subdomain_registration::nft_mut(sub_nft); - let subdomain = suins_registration::domain(nft); - let parent_domain = domain::parent(&subdomain); + let nft = sub_nft.nft_mut(); + let subdomain = nft.domain(); + let parent_domain = subdomain.parent(); // Check if time extension is allowed for this subdomain. assert!(is_extension_allowed(&record_metadata(suins, subdomain)), EExtensionDisabledForSubDomain); - let existing_name_record = registry::lookup(registry, subdomain); - let parent_name_record = registry::lookup(registry, parent_domain); + let existing_name_record = registry.lookup(subdomain); + let parent_name_record = registry.lookup(parent_domain); // we need to make sure this name record exists (both child + parent), otherwise we don't have a valid object. assert!(option::is_some(&existing_name_record) && option::is_some(&parent_name_record), ESubdomainReplaced); // Validate that the parent of the name is the same as the actual parent // (to prevent cases where owner of the parent changed. When that happens, subdomains lose all abilities to renew / create subdomains) - assert!(parent(nft) == name_record::nft_id(option::borrow(&parent_name_record)), EParentChanged); + assert!(parent(nft) == option::borrow(&parent_name_record).nft_id(), EParentChanged); // validate that expiration date is > than the current. - assert!(expiration_timestamp_ms > suins_registration::expiration_timestamp_ms(nft), EInvalidExpirationDate); + assert!(expiration_timestamp_ms > nft.expiration_timestamp_ms(), EInvalidExpirationDate); // validate that the requested expiration timestamp is not greater than the parent's one. - assert!(expiration_timestamp_ms <= name_record::expiration_timestamp_ms(option::borrow(&parent_name_record)), EInvalidExpirationDate); + assert!(expiration_timestamp_ms <= option::borrow(&parent_name_record).expiration_timestamp_ms(), EInvalidExpirationDate); - registry::set_expiration_timestamp_ms(registry_mut(suins), nft, subdomain, expiration_timestamp_ms); + registry_mut(suins).set_expiration_timestamp_ms(nft, subdomain, expiration_timestamp_ms); } /// Called by the parent domain to edit a subdomain's settings. @@ -216,9 +212,9 @@ module subdomains::subdomains { allow_time_extension: bool ) { // validate that parent is a valid, non expired object. - registry::assert_nft_is_authorized(registry(suins), parent, clock); + registry(suins).assert_nft_is_authorized(parent, clock); - let parent_domain = suins_registration::domain(parent); + let parent_domain = parent.domain(); let subdomain = domain::new(subdomain_name); // validate that the subdomain is valid for the supplied parent @@ -237,12 +233,12 @@ module subdomains::subdomains { nft: SubDomainRegistration, clock: &Clock, ) { - registry::burn_subdomain_object(registry_mut(suins), nft, clock); + registry_mut(suins).burn_subdomain_object(nft, clock); } /// Parent ID of a subdomain public fun parent(subdomain: &SuinsRegistration): ID { - *df::borrow(suins_registration::uid(subdomain), ParentKey {}) + *df::borrow(subdomain.uid(), ParentKey {}) } // Sets/removes a (key,value) on the domain's NameRecord metadata (depending on cases). @@ -254,27 +250,27 @@ module subdomains::subdomains { enable: bool ) { let mut config = record_metadata(self, subdomain); - let is_enabled = vec_map::contains(&config, &key); + let is_enabled = config.contains(&key); if (enable && !is_enabled) { - vec_map::insert(&mut config, key, utf8(ACTIVE_METADATA_VALUE)); + config.insert(key, utf8(ACTIVE_METADATA_VALUE)); }; if(!enable && is_enabled) { - vec_map::remove(&mut config, &key); + config.remove(&key); }; - registry::set_data(registry_mut(self), subdomain, config); + registry_mut(self).set_data(subdomain, config); } /// Check if subdomain creation is allowed. fun is_creation_allowed(metadata: &VecMap): bool { - vec_map::contains(metadata, &subdomain_allow_creation_key()) + metadata.contains(&subdomain_allow_creation_key()) } /// Check if time extension is allowed. fun is_extension_allowed(metadata: &VecMap): bool { - vec_map::contains(metadata, &subdomain_allow_extension_key()) + metadata.contains(&subdomain_allow_extension_key()) } /// Get the name record's metadata for a subdomain. @@ -282,7 +278,7 @@ module subdomains::subdomains { self: &SuiNS, subdomain: Domain ): VecMap { - *registry::get_data(registry(self), subdomain) + *registry(self).get_data(subdomain) } /// Does all the regular checks for validating that a parent `SuinsRegistration` object @@ -301,15 +297,15 @@ module subdomains::subdomains { check_creation_auth: bool ) { // validate that parent is a valid, non expired object. - registry::assert_nft_is_authorized(registry(suins), parent, clock); + registry(suins).assert_nft_is_authorized(parent, clock); if (check_creation_auth) { // validate that the parent can create subdomains. - internal_assert_parent_can_create_subdomains(suins, suins_registration::domain(parent)); + internal_assert_parent_can_create_subdomains(suins, parent.domain()); }; // validate that the subdomain is valid for the supplied parent. - config::assert_is_valid_subdomain(&suins_registration::domain(parent), &subdomain, &app_config(suins).config); + config::assert_is_valid_subdomain(&parent.domain(), &subdomain, &app_config(suins).config); } /// Validate whether a `SuinsRegistration` object is eligible for creating a subdomain. @@ -341,21 +337,21 @@ module subdomains::subdomains { clock: &Clock, ctx: &mut TxContext, ): SubDomainRegistration { - let mut nft = registry::add_record_ignoring_grace_period(registry, subdomain, 1, clock, ctx); + let mut nft = registry.add_record_ignoring_grace_period(subdomain, 1, clock, ctx); // set the timestamp to the correct one. `add_record` only works with years but we can correct it easily here. - registry::set_expiration_timestamp_ms(registry, &mut nft, subdomain, expiration_timestamp_ms); + registry.set_expiration_timestamp_ms(&mut nft, subdomain, expiration_timestamp_ms); // attach the `ParentID` to the SuinsRegistration, so we validate that the parent who created this subdomain // is the same as the one currently holding the parent domain. - df::add(suins_registration::uid_mut(&mut nft), ParentKey {}, parent_nft_id); + df::add(nft.uid_mut(), ParentKey {}, parent_nft_id); - registry::wrap_subdomain(registry, nft, clock, ctx) + registry.wrap_subdomain(nft, clock, ctx) } // == Internal helper to access registry & app setup == fun registry(suins: &SuiNS): &Registry { - suins::registry(suins) + suins.registry() } fun registry_mut(suins: &mut SuiNS): &mut Registry { @@ -363,7 +359,7 @@ module subdomains::subdomains { } fun app_config(suins: &SuiNS): &App { - suins::registry(suins) + suins.registry() } #[test_only] diff --git a/packages/subdomains/tests/subdomain_tests.move b/packages/subdomains/tests/subdomain_tests.move index 08685260..5dcb4364 100644 --- a/packages/subdomains/tests/subdomain_tests.move +++ b/packages/subdomains/tests/subdomain_tests.move @@ -5,16 +5,17 @@ module subdomains::subdomain_tests { use std::string::{String, utf8}; - use sui::test_scenario::{Self as ts, Scenario, ctx}; - use sui::clock::{Self, Clock}; - - use suins::domain; - use suins::constants::{grace_period_ms, year_ms}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::registry::{Self, Registry}; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::subdomain_registration::{Self, SubDomainRegistration}; - use suins::registry_tests::{burn_nfts}; + use sui::{test_scenario::{Self as ts, Scenario, ctx}, clock::{Self, Clock}}; + + use suins::{ + domain, + constants::{grace_period_ms, year_ms}, + suins::{Self, SuiNS, AdminCap}, + registry::{Self, Registry}, + suins_registration::{Self, SuinsRegistration}, + subdomain_registration::{Self, SubDomainRegistration}, + registry_tests::{burn_nfts} + }; use denylist::denylist; diff --git a/packages/suins/sources/actions/update_image.move b/packages/suins/sources/actions/update_image.move index b3a882c4..84e550f2 100644 --- a/packages/suins/sources/actions/update_image.move +++ b/packages/suins/sources/actions/update_image.move @@ -2,17 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 module suins::update_image { - use std::vector; use std::string::{utf8, String}; - use sui::clock::Clock; - use sui::bcs; - use sui::ecdsa_k1; + use sui::{clock::Clock, bcs, ecdsa_k1}; - use suins::domain; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::config::{Self, Config}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; + use suins::{registry::Registry, suins::SuiNS, config::Config, suins_registration::SuinsRegistration}; /// Message data cannot be parsed. const EInvalidData: u64 = 0; @@ -26,29 +19,29 @@ module suins::update_image { /// Updates the image attached to a `SuinsRegistration`. entry fun update_image_url( - suins: &mut SuiNS, + suins: &SuiNS, nft: &mut SuinsRegistration, raw_msg: vector, signature: vector, clock: &Clock, ) { - suins::assert_app_is_authorized(suins); - let registry = suins::registry(suins); - registry::assert_nft_is_authorized(registry, nft, clock); + suins.assert_app_is_authorized(); + let registry = suins.registry(); + registry.assert_nft_is_authorized(nft, clock); - let config = suins::get_config(suins); + let config = suins.get_config(); assert!( - ecdsa_k1::secp256k1_verify(&signature, config::public_key(config), &raw_msg, 1), + ecdsa_k1::secp256k1_verify(&signature, config.public_key(), &raw_msg, 1), ESignatureNotMatch ); let (ipfs_hash, domain_name, expiration_timestamp_ms, _data) = image_data_from_bcs(raw_msg); - assert!(nft::expiration_timestamp_ms(nft) == expiration_timestamp_ms, EInvalidData); - assert!(domain::to_string(&nft::domain(nft)) == domain_name, EInvalidDomainData); + assert!(nft.expiration_timestamp_ms() == expiration_timestamp_ms, EInvalidData); + assert!(nft.domain().to_string() == domain_name, EInvalidDomainData); - nft::update_image_url(nft, ipfs_hash); + nft.update_image_url(ipfs_hash); // TODO emit an event // event::emit(ImageUpdatedEvent { @@ -71,13 +64,13 @@ module suins::update_image { fun image_data_from_bcs(msg_bytes: vector): (String, String, u64, String) { let mut bcs = bcs::new(msg_bytes); - let ipfs_hash = utf8(bcs::peel_vec_u8(&mut bcs)); - let domain_name = utf8(bcs::peel_vec_u8(&mut bcs)); - let expiration_timestamp_ms = bcs::peel_u64(&mut bcs); - let data = utf8(bcs::peel_vec_u8(&mut bcs)); + let ipfs_hash = utf8(bcs.peel_vec_u8()); + let domain_name = utf8(bcs.peel_vec_u8()); + let expiration_timestamp_ms = bcs.peel_u64(); + let data = utf8(bcs.peel_vec_u8()); - let remainder = bcs::into_remainder_bytes(bcs); - vector::destroy_empty(remainder); + let remainder = bcs.into_remainder_bytes(); + remainder.destroy_empty(); ( ipfs_hash, diff --git a/packages/suins/sources/admin.move b/packages/suins/sources/admin.move index e6960758..62fbd7e1 100644 --- a/packages/suins/sources/admin.move +++ b/packages/suins/sources/admin.move @@ -4,16 +4,10 @@ /// Admin features of the SuiNS application. Meant to be called directly /// by the suins admin. module suins::admin { - use std::vector; use std::string::String; - use sui::clock::Clock; - use sui::tx_context::{sender, TxContext}; + use sui::{clock::Clock, tx_context::{sender}}; - use suins::domain; - use suins::config; - use suins::suins::{Self, AdminCap, SuiNS}; - use suins::suins_registration::SuinsRegistration; - use suins::registry::{Self, Registry}; + use suins::{domain, config, suins::{Self, AdminCap, SuiNS}, suins_registration::SuinsRegistration, registry::Registry}; /// The authorization witness. public struct Admin has drop {} @@ -37,7 +31,7 @@ module suins::admin { let domain = domain::new(domain_name); config::assert_valid_user_registerable_domain(&domain); let registry = suins::app_registry_mut(Admin {}, suins); - registry::add_record(registry, domain, no_years, clock, ctx) + registry.add_record(domain, no_years, clock, ctx) } /// Reserve a list of domains. @@ -51,10 +45,10 @@ module suins::admin { ) { let sender = sender(ctx); let registry = suins::app_registry_mut(Admin {}, suins); - while (!vector::is_empty(&domains)) { - let domain = domain::new(vector::pop_back(&mut domains)); + while (!domains.is_empty()) { + let domain = domain::new(domains.pop_back()); config::assert_valid_user_registerable_domain(&domain); - let nft = registry::add_record(registry, domain, no_years, clock, ctx); + let nft = registry.add_record(domain, no_years, clock, ctx); sui::transfer::public_transfer(nft, sender); }; } diff --git a/packages/suins/sources/auction.move b/packages/suins/sources/auction.move index 8d97a320..bae81e83 100644 --- a/packages/suins/sources/auction.move +++ b/packages/suins/sources/auction.move @@ -4,24 +4,24 @@ /// Implementation of auction module. /// More information in: ../../../docs module suins::auction { - use std::option::{Self, Option, none, some, is_some}; - use std::string::{Self, String}; - - use sui::tx_context::{Self, TxContext}; - use sui::balance::{Self, Balance}; - use sui::transfer; - use sui::coin::{Self, Coin}; - use sui::clock::{Self, Clock}; - use sui::event; - use sui::object::{Self, UID}; - use sui::sui::SUI; - use sui::linked_table::{Self, LinkedTable}; - - use suins::config::{Self, Config}; - use suins::suins::{Self, AdminCap, SuiNS}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::registry::{Self, Registry}; - use suins::domain::{Self, Domain}; + use std::{option::{none, some, is_some}, string::String}; + + use sui::{ + balance::{Self, Balance}, + coin::{Self, Coin}, + clock::Clock, + event, + sui::SUI, + linked_table::{Self, LinkedTable} + }; + + use suins::{ + config::{Self, Config}, + suins::{Self, AdminCap, SuiNS}, + suins_registration::SuinsRegistration, + registry::Registry, + domain::{Self, Domain} + }; /// One year is the default duration for a domain. const DEFAULT_DURATION: u8 = 1; @@ -82,30 +82,30 @@ module suins::auction { clock: &Clock, ctx: &mut TxContext, ) { - suins::assert_app_is_authorized(suins); + suins.assert_app_is_authorized(); let domain = domain::new(domain_name); // make sure the domain is a .sui domain and not a subdomain config::assert_valid_user_registerable_domain(&domain); - assert!(!linked_table::contains(&self.auctions, domain), EAuctionStarted); + assert!(!self.auctions.contains(domain), EAuctionStarted); // The minimum price only applies to newly created auctions - let config = suins::get_config(suins); - let label = domain::sld(&domain); - let min_price = config::calculate_price(config, (string::length(label) as u8), DEFAULT_DURATION); - assert!(coin::value(&bid) >= min_price, EInvalidBidValue); + let config = suins.get_config(); + let label = domain.sld(); + let min_price = config.calculate_price((label.length() as u8), DEFAULT_DURATION); + assert!(bid.value() >= min_price, EInvalidBidValue); let registry = suins::app_registry_mut(App {}, suins); - let nft = registry::add_record(registry, domain, DEFAULT_DURATION, clock, ctx); - let starting_bid = coin::value(&bid); + let nft = registry.add_record(domain, DEFAULT_DURATION, clock, ctx); + let starting_bid = bid.value(); let auction = Auction { domain, - start_timestamp_ms: clock::timestamp_ms(clock), - end_timestamp_ms: clock::timestamp_ms(clock) + AUCTION_BIDDING_PERIOD_MS, - winner: tx_context::sender(ctx), + start_timestamp_ms: clock.timestamp_ms(), + end_timestamp_ms: clock.timestamp_ms() + AUCTION_BIDDING_PERIOD_MS, + winner: ctx.sender(), current_bid: bid, nft, }; @@ -118,7 +118,7 @@ module suins::auction { bidder: auction.winner, }); - linked_table::push_front(&mut self.auctions, domain, auction) + self.auctions.push_front(domain, auction) } /// #### Notice @@ -136,9 +136,9 @@ module suins::auction { ctx: &mut TxContext ) { let domain = domain::new(domain_name); - let bidder = tx_context::sender(ctx); + let bidder = ctx.sender(); - assert!(linked_table::contains(&self.auctions, domain), EAuctionNotStarted); + assert!(self.auctions.contains(domain), EAuctionNotStarted); let Auction { domain, @@ -147,16 +147,16 @@ module suins::auction { winner, current_bid, nft, - } = linked_table::remove(&mut self.auctions, domain); + } = self.auctions.remove(domain); // Ensure that the auction is not over - assert!(clock::timestamp_ms(clock) <= end_timestamp_ms, EAuctionEnded); + assert!(clock.timestamp_ms() <= end_timestamp_ms, EAuctionEnded); // Ensure the bidder isn't already the winner assert!(bidder != winner, EWinnerCannotPlaceBid); // get the current highest bid and ensure that the new bid is greater than the current winning bid - let current_winning_bid = coin::value(¤t_bid); - let bid_amount = coin::value(&bid); + let current_winning_bid = current_bid.value(); + let bid_amount = bid.value(); assert!(bid_amount > current_winning_bid, EBidAmountTooLow); // Return the previous winner their bid @@ -172,12 +172,12 @@ module suins::auction { // then extend the auction so that there is `AUCTION_MIN_QUIET_PERIOD_MS` left. // Auctions can't be finished until there is at least `AUCTION_MIN_QUIET_PERIOD_MS` // time where there are no bids. - if (end_timestamp_ms - clock::timestamp_ms(clock) < AUCTION_MIN_QUIET_PERIOD_MS) { - let new_end_timestamp_ms = clock::timestamp_ms(clock) + AUCTION_MIN_QUIET_PERIOD_MS; + if (end_timestamp_ms - clock.timestamp_ms() < AUCTION_MIN_QUIET_PERIOD_MS) { + let new_end_timestamp_ms = clock.timestamp_ms() + AUCTION_MIN_QUIET_PERIOD_MS; // Only extend the auction if the new auction end time is before // the NFT's expiration timestamp - if (new_end_timestamp_ms < nft::expiration_timestamp_ms(&nft)) { + if (new_end_timestamp_ms < nft.expiration_timestamp_ms()) { end_timestamp_ms = new_end_timestamp_ms; event::emit(AuctionExtendedEvent { @@ -196,7 +196,7 @@ module suins::auction { nft, }; - linked_table::push_front(&mut self.auctions, domain, auction); + self.auctions.push_front(domain, auction); } /// #### Notice @@ -219,13 +219,13 @@ module suins::auction { winner, current_bid, nft, - } = linked_table::remove(&mut self.auctions, domain); + } = self.auctions.remove(domain); // Ensure that the auction is over - assert!(clock::timestamp_ms(clock) > end_timestamp_ms, EAuctionNotEndedYet); + assert!(clock.timestamp_ms() > end_timestamp_ms, EAuctionNotEndedYet); // Ensure the sender is the winner - assert!(tx_context::sender(ctx) == winner, ENotWinner); + assert!(ctx.sender() == winner, ENotWinner); event::emit(AuctionFinalizedEvent { domain, @@ -237,7 +237,7 @@ module suins::auction { // Extract the NFT and their bid, returning the NFT to the user // and sending the proceeds of the auction to suins - balance::join(&mut self.balance, coin::into_balance(current_bid)); + self.balance.join(current_bid.into_balance()); nft } @@ -257,9 +257,9 @@ module suins::auction { ): (Option, Option, Option
, Option) { let domain = domain::new(domain_name); - if (linked_table::contains(&self.auctions, domain)) { - let auction = linked_table::borrow(&self.auctions, domain); - let highest_amount = coin::value(&auction.current_bid); + if (self.auctions.contains(domain)) { + let auction = &self.auctions[domain]; + let highest_amount = auction.current_bid.value(); return ( some(auction.start_timestamp_ms), some(auction.end_timestamp_ms), @@ -277,12 +277,12 @@ module suins::auction { ctx: &mut TxContext, ) { let domain = domain::new(domain_name); - let auction = linked_table::borrow_mut(&mut self.auctions, domain); + let auction = &mut self.auctions[domain]; // Ensure that the auction is over - assert!(clock::timestamp_ms(clock) > auction.end_timestamp_ms, EAuctionNotEndedYet); + assert!(clock.timestamp_ms() > auction.end_timestamp_ms, EAuctionNotEndedYet); - let amount = coin::value(&mut auction.current_bid); - balance::join(&mut self.balance, coin::into_balance(coin::split(&mut auction.current_bid, amount, ctx))); + let amount = auction.current_bid.value(); + self.balance.join(auction.current_bid.split(amount, ctx).into_balance()); } // === Admin Functions === @@ -292,7 +292,7 @@ module suins::auction { self: &mut AuctionHouse, ctx: &mut TxContext, ): Coin { - let amount = balance::value(&self.balance); + let amount = self.balance.value(); assert!(amount > 0, ENoProfits); coin::take(&mut self.balance, amount, ctx) } @@ -335,10 +335,10 @@ module suins::auction { winner, current_bid, nft, - } = linked_table::remove(&mut self.auctions, domain); + } = self.auctions.remove(domain); // Ensure that the auction is over - assert!(clock::timestamp_ms(clock) > end_timestamp_ms, EAuctionNotEndedYet); + assert!(clock.timestamp_ms() > end_timestamp_ms, EAuctionNotEndedYet); event::emit(AuctionFinalizedEvent { domain, @@ -348,7 +348,7 @@ module suins::auction { winner, }); - balance::join(&mut self.balance, coin::into_balance(current_bid)); + self.balance.join(current_bid.into_balance()); transfer::public_transfer(nft, winner); } @@ -364,7 +364,7 @@ module suins::auction { mut operation_limit: u64, clock: &Clock, ) { - let mut next_domain = *linked_table::back(&self.auctions); + let mut next_domain = *self.auctions.back(); while (is_some(&next_domain)) { if (operation_limit == 0) { @@ -373,12 +373,12 @@ module suins::auction { operation_limit = operation_limit - 1; let domain = option::extract(&mut next_domain); - next_domain = *linked_table::prev(&self.auctions, domain); + next_domain = *self.auctions.prev(domain); - let auction = linked_table::borrow(&self.auctions, domain); + let auction = &self.auctions[domain]; // If the auction has ended, then try to finalize it - if (clock::timestamp_ms(clock) > auction.end_timestamp_ms) { + if (clock.timestamp_ms() > auction.end_timestamp_ms) { admin_finalize_auction_internal( admin, self, @@ -431,6 +431,6 @@ module suins::auction { #[test_only] public fun total_balance(self: &AuctionHouse): u64 { - balance::value(&self.balance) + self.balance.value() } } diff --git a/packages/suins/sources/config.move b/packages/suins/sources/config.move index 1e93be3d..350f499b 100644 --- a/packages/suins/sources/config.move +++ b/packages/suins/sources/config.move @@ -17,10 +17,7 @@ /// and set again within the same Programmable Transaction Block (can only be /// performed by Admin) module suins::config { - use std::vector; - use std::string; - use suins::constants; - use suins::domain::{Self, Domain}; + use suins::{constants, domain::Domain}; /// A label is too short to be registered. const ELabelTooShort: u64 = 0; @@ -56,7 +53,7 @@ module suins::config { four_char_price: u64, five_plus_char_price: u64, ): Config { - assert!(vector::length(&public_key) == 33, EInvalidPublicKey); + assert!(public_key.length() == 33, EInvalidPublicKey); Config { public_key, @@ -70,7 +67,7 @@ module suins::config { /// Change the value of the `public_key` field. public fun set_public_key(self: &mut Config, value: vector) { - assert!(vector::length(&value) == 33, EInvalidPublicKey); + assert!(value.length() == 33, EInvalidPublicKey); self.public_key = value; } @@ -133,9 +130,9 @@ module suins::config { /// - only has 1 label, "name", other than the TLD /// - "name" is >= 3 characters long public fun assert_valid_user_registerable_domain(domain: &Domain) { - assert!(domain::number_of_levels(domain) == 2, EInvalidDomain); - assert!(domain::tld(domain) == &constants::sui_tld(), EInvalidTld); - let length = string::length(domain::sld(domain)); + assert!(domain.number_of_levels() == 2, EInvalidDomain); + assert!(domain.tld() == &constants::sui_tld(), EInvalidTld); + let length = domain.sld().length(); assert!(length >= (constants::min_domain_length() as u64), ELabelTooShort); assert!(length <= (constants::max_domain_length() as u64), ELabelTooLong); } diff --git a/packages/suins/sources/controller.move b/packages/suins/sources/controller.move index 12ba9073..c41181e4 100644 --- a/packages/suins/sources/controller.move +++ b/packages/suins/sources/controller.move @@ -2,17 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 module suins::controller { - use std::option::Option; use std::string::String; - use sui::tx_context::{sender, TxContext}; - use sui::clock::Clock; - use sui::vec_map; + use sui::{tx_context::{sender}, clock::Clock}; - use suins::domain; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use std::string; + use suins::{domain, registry::Registry, suins::{Self, SuiNS}, suins_registration::SuinsRegistration}; const AVATAR: vector = b"avatar"; const CONTENT_HASH: vector = b"content_hash"; @@ -32,23 +25,23 @@ module suins::controller { clock: &Clock, ) { let registry = suins::app_registry_mut(Controller {}, suins); - registry::assert_nft_is_authorized(registry, nft, clock); + registry.assert_nft_is_authorized(nft, clock); - let domain = nft::domain(nft); - registry::set_target_address(registry, domain, new_target); + let domain = nft.domain(); + registry.set_target_address(domain, new_target); } /// User-facing function (upgradable) - set the reverse lookup address for the domain. entry fun set_reverse_lookup(suins: &mut SuiNS, domain_name: String, ctx: &TxContext) { let domain = domain::new(domain_name); let registry = suins::app_registry_mut(Controller {}, suins); - registry::set_reverse_lookup(registry, sender(ctx), domain); + registry.set_reverse_lookup(sender(ctx), domain); } /// User-facing function (upgradable) - unset the reverse lookup address for the domain. entry fun unset_reverse_lookup(suins: &mut SuiNS, ctx: &TxContext) { let registry = suins::app_registry_mut(Controller {}, suins); - registry::unset_reverse_lookup(registry, sender(ctx)); + registry.unset_reverse_lookup(sender(ctx)); } /// User-facing function (upgradable) - add a new key-value pair to the name record's data. @@ -57,19 +50,19 @@ module suins::controller { ) { let registry = suins::app_registry_mut(Controller {}, suins); - let mut data = *registry::get_data(registry, nft::domain(nft)); - let domain = nft::domain(nft); + let mut data = *registry.get_data(nft.domain()); + let domain = nft.domain(); - registry::assert_nft_is_authorized(registry, nft, clock); - let key_bytes = *string::bytes(&key); + registry.assert_nft_is_authorized(nft, clock); + let key_bytes = *key.bytes(); assert!(key_bytes == AVATAR || key_bytes == CONTENT_HASH, EUnsupportedKey); - if (vec_map::contains(&data, &key)) { - vec_map::remove(&mut data, &key); + if (data.contains(&key)) { + data.remove(&key); }; - vec_map::insert(&mut data, key, value); - registry::set_data(registry, domain, data); + data.insert(key, value); + registry.set_data(domain, data); } /// User-facing function (upgradable) - remove a key from the name record's data. @@ -77,16 +70,16 @@ module suins::controller { suins: &mut SuiNS, nft: &SuinsRegistration, key: String, clock: &Clock ) { let registry = suins::app_registry_mut(Controller {}, suins); - let mut data = *registry::get_data(registry, nft::domain(nft)); - let domain = nft::domain(nft); + let mut data = *registry.get_data(nft.domain()); + let domain = nft.domain(); - registry::assert_nft_is_authorized(registry, nft, clock); + registry.assert_nft_is_authorized(nft, clock); - if (vec_map::contains(&data, &key)) { - vec_map::remove(&mut data, &key); + if (data.contains(&key)) { + data.remove(&key); }; - registry::set_data(registry, domain, data); + registry.set_data(domain, data); } // === Testing === diff --git a/packages/suins/sources/domain.move b/packages/suins/sources/domain.move index e28172de..9b9d30d4 100644 --- a/packages/suins/sources/domain.move +++ b/packages/suins/sources/domain.move @@ -8,7 +8,6 @@ /// https://en.wikipedia.org/wiki/Domain_name#Domain_name_syntax module suins::domain { use std::string::{Self, String, utf8}; - use std::vector; const EInvalidDomain: u64 = 0; @@ -30,11 +29,11 @@ module suins::domain { // Construct a `Domain` by parsing and validating the provided string public fun new(domain: String): Domain { - assert!(string::length(&domain) <= MAX_DOMAIN_LENGTH, EInvalidDomain); + assert!(domain.length() <= MAX_DOMAIN_LENGTH, EInvalidDomain); let mut labels = split_by_dot(domain); validate_labels(&labels); - vector::reverse(&mut labels); + labels.reverse(); Domain { labels } @@ -43,17 +42,17 @@ module suins::domain { /// Converts a domain into a fully-qualified string representation. public fun to_string(self: &Domain): String { let dot = utf8(b"."); - let len = vector::length(&self.labels); + let len = self.labels.length(); let mut i = 0; let mut out = string::utf8(vector::empty()); while (i < len) { - let part = vector::borrow(&self.labels, (len - i) - 1); - string::append(&mut out, *part); + let part = &self.labels[(len - i) - 1]; + out.append(*part); i = i + 1; if (i != len) { - string::append(&mut out, dot); + out.append(dot); } }; @@ -69,7 +68,7 @@ module suins::domain { /// /// This means that the TLD will always be at level `0`. public fun label(self: &Domain, level: u64): &String { - vector::borrow(&self.labels, level) + &self.labels[level] } /// Returns the TLD (Top-Level Domain) of a `Domain`. @@ -87,7 +86,7 @@ module suins::domain { } public fun number_of_levels(self: &Domain): u64 { - vector::length(&self.labels) + self.labels.length() } public fun is_subdomain(domain: &Domain): bool { @@ -99,7 +98,7 @@ module suins::domain { public fun parent(domain: &Domain): Domain { let mut labels = domain.labels; // we pop the last element and construct the parent from the remaining labels. - vector::pop_back(&mut labels); + labels.pop_back(); Domain { labels @@ -113,21 +112,21 @@ module suins::domain { } fun validate_labels(labels: &vector) { - assert!(!vector::is_empty(labels), EInvalidDomain); + assert!(!labels.is_empty(), EInvalidDomain); - let len = vector::length(labels); + let len = labels.length(); let mut index = 0; while (index < len) { - let label = vector::borrow(labels, index); + let label = &labels[index]; assert!(is_valid_label(label), EInvalidDomain); index = index + 1; } } fun is_valid_label(label: &String): bool { - let len = string::length(label); - let label_bytes = string::bytes(label); + let len = label.length(); + let label_bytes = label.bytes(); let mut index = 0; if (!(len >= MIN_LABEL_LENGTH && len <= MAX_LABEL_LENGTH)) { @@ -135,7 +134,7 @@ module suins::domain { }; while (index < len) { - let character = *vector::borrow(label_bytes, index); + let character = label_bytes[index]; let is_valid_character = (0x61 <= character && character <= 0x7A) // a-z || (0x30 <= character && character <= 0x39) // 0-9 @@ -155,19 +154,19 @@ module suins::domain { fun split_by_dot(mut s: String): vector { let dot = utf8(b"."); let mut parts: vector = vector[]; - while (!string::is_empty(&s)) { - let index_of_next_dot = string::index_of(&s, &dot); - let part = string::sub_string(&s, 0, index_of_next_dot); - vector::push_back(&mut parts, part); + while (!s.is_empty()) { + let index_of_next_dot = s.index_of(&dot); + let part = s.sub_string(0, index_of_next_dot); + parts.push_back(part); - let len = string::length(&s); + let len = s.length(); let start_of_next_part = if (index_of_next_dot == len) { len } else { index_of_next_dot + 1 }; - s = string::sub_string(&s, start_of_next_part, len); + s = s.sub_string(start_of_next_part, len); }; parts @@ -191,7 +190,7 @@ module suins::domain { let mut index = 0; while (index < len) { - let label = vector::borrow(&expected_labels, index); + let label = &expected_labels[index]; assert_eq(*label, *label(&domain, index)); index = index + 1; } @@ -200,9 +199,9 @@ module suins::domain { #[test_only] fun prep_expected_labels(mut labels: vector>): vector { let mut out = vector[]; - while (!vector::is_empty(&labels)) { - let label = vector::pop_back(&mut labels); - vector::push_back(&mut out, utf8(label)); + while (!labels.is_empty()) { + let label = labels.pop_back(); + out.push_back(utf8(label)); }; out } diff --git a/packages/suins/sources/name_record.move b/packages/suins/sources/name_record.move index 91f2383e..cd6a57a8 100644 --- a/packages/suins/sources/name_record.move +++ b/packages/suins/sources/name_record.move @@ -6,12 +6,9 @@ /// stored and managed. SuiNS has no direct and permanent dependency on this /// module. module suins::name_record { - use std::option::{Self, Option}; use std::string::String; - use sui::clock::{timestamp_ms, Clock}; - use sui::vec_map::{Self, VecMap}; - use sui::object::ID; + use sui::{clock::{timestamp_ms, Clock}, vec_map::{Self, VecMap}}; use suins::constants; diff --git a/packages/suins/sources/registry.move b/packages/suins/sources/registry.move index 1f8e680a..0e988f3b 100644 --- a/packages/suins/sources/registry.move +++ b/packages/suins/sources/registry.move @@ -2,20 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 module suins::registry { - use std::option::{Self, none, some, Option}; - use std::string::String; + use std::{option::{none, some}, string::String}; - use sui::tx_context::TxContext; - use sui::object; - use sui::table::{Self, Table}; - use sui::clock::Clock; - use sui::vec_map::VecMap; + use sui::{table::{Self, Table}, clock::Clock, vec_map::VecMap}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::name_record::{Self, NameRecord}; - use suins::domain::{Self, Domain}; - use suins::suins::AdminCap; - use suins::subdomain_registration::{Self, SubDomainRegistration}; + use suins::{ + suins_registration::{Self as nft, SuinsRegistration}, + name_record::{Self, NameRecord}, + domain::Domain, + suins::AdminCap, + subdomain_registration::{Self, SubDomainRegistration} + }; /// The `SuinsRegistration` has expired. const ENftExpired: u64 = 0; @@ -91,22 +88,22 @@ module suins::registry { clock: &Clock ) { // First we make sure that the SuinsRegistration object has expired. - assert!(nft::has_expired(&nft, clock), ERecordNotExpired); + assert!(nft.has_expired(clock), ERecordNotExpired); - let domain = nft::domain(&nft); + let domain = nft.domain(); // Then, if the registry still has a record for this domain and the NFT ID matches, we remove it. - if (table::contains(&self.registry, domain)) { + if (self.registry.contains(domain)) { - let record = table::borrow(&self.registry, domain); + let record = &self.registry[domain]; // We wanna remove the record only if the NFT ID matches. - if (name_record::nft_id(record) == object::id(&nft)) { - let record = table::remove(&mut self.registry, domain); - handle_invalidate_reverse_record(self, &domain, name_record::target_address(&record), none()); + if (record.nft_id() == object::id(&nft)) { + let record = self.registry.remove(domain); + handle_invalidate_reverse_record(self, &domain, record.target_address(), none()); } }; // burn the NFT. - nft::burn(nft); + nft.burn(); } /// Allow creation of subdomain wrappers only to authorized modules. @@ -126,7 +123,7 @@ module suins::registry { nft: SubDomainRegistration, clock: &Clock ) { - let nft = subdomain_registration::burn(nft, clock); + let nft = nft.burn(clock); burn_registration_object(self, nft, clock); } @@ -149,10 +146,10 @@ module suins::registry { target: address, _ctx: &mut TxContext ) { - assert!(domain::is_subdomain(&domain), EInvalidDepth); + assert!(domain.is_subdomain(), EInvalidDepth); // get the parent of the domain - let parent = domain::parent(&domain); + let parent = domain.parent(); let option_parent_name_record = lookup(self, parent); assert!(option::is_some(&option_parent_name_record), ERecordNotFound); @@ -162,13 +159,13 @@ module suins::registry { // Make sure that the parent isn't expired (because leaf record is invalid in that case). // Ignores grace period is it's only there so you don't accidently forget to renew your name. - assert!(!name_record::has_expired(parent_name_record, clock), ERecordExpired); + assert!(!parent_name_record.has_expired(clock), ERecordExpired); // Removes an existing record if it exists and is expired. remove_existing_record_if_exists_and_expired(self, domain, clock, false); // adds the `leaf` record to the registry. - table::add(&mut self.registry, domain, name_record::new_leaf(name_record::nft_id(parent_name_record), some(target))); + self.registry.add(domain, name_record::new_leaf(parent_name_record.nft_id(), some(target))); } /// Can be used to remove a leaf record. @@ -183,8 +180,8 @@ module suins::registry { // if it's a leaf record, there's no `SuinsRegistration` object. // We can just go ahead and remove the name_record, and invalidate the reverse record (if any). - let record = table::remove(&mut self.registry, domain); - let old_target_address = name_record::target_address(&record); + let record = self.registry.remove(domain); + let old_target_address = record.target_address(); handle_invalidate_reverse_record(self, &domain, old_target_address, none()); } @@ -194,15 +191,15 @@ module suins::registry { domain: Domain, new_target: Option
, ) { - let record = table::borrow_mut(&mut self.registry, domain); - let old_target = name_record::target_address(record); + let record = &mut self.registry[domain]; + let old_target = record.target_address(); - name_record::set_target_address(record, new_target); + record.set_target_address(new_target); handle_invalidate_reverse_record(self, &domain, old_target, new_target); } public fun unset_reverse_lookup(self: &mut Registry, address: address) { - table::remove(&mut self.reverse_registry, address); + self.reverse_registry.remove(address); } /// Reverse lookup can only be set for the record that has the target address. @@ -211,16 +208,16 @@ module suins::registry { address: address, domain: Domain, ) { - let record = table::borrow(&self.registry, domain); - let target = name_record::target_address(record); + let record = &self.registry[domain]; + let target = record.target_address(); assert!(option::is_some(&target), ETargetNotSet); assert!(some(address) == target, ERecordMismatch); - if (table::contains(&self.reverse_registry, address)) { - *table::borrow_mut(&mut self.reverse_registry, address) = domain; + if (self.reverse_registry.contains(address)) { + *self.reverse_registry.borrow_mut(address) = domain; } else { - table::add(&mut self.reverse_registry, address, domain); + self.reverse_registry.add(address, domain); }; } @@ -233,11 +230,11 @@ module suins::registry { domain: Domain, expiration_timestamp_ms: u64, ) { - let record = table::borrow_mut(&mut self.registry, domain); + let record = &mut self.registry[domain]; - assert!(object::id(nft) == name_record::nft_id(record), EIdMismatch); - name_record::set_expiration_timestamp_ms(record, expiration_timestamp_ms); - nft::set_expiration_timestamp_ms(nft, expiration_timestamp_ms); + assert!(object::id(nft) == record.nft_id(), EIdMismatch); + record.set_expiration_timestamp_ms(expiration_timestamp_ms); + nft.set_expiration_timestamp_ms(expiration_timestamp_ms); } /// Update the `data` of the given `NameRecord` using a `SuinsRegistration`. @@ -248,21 +245,21 @@ module suins::registry { domain: Domain, data: VecMap ) { - let record = table::borrow_mut(&mut self.registry, domain); - name_record::set_data(record, data); + let record = &mut self.registry[domain]; + record.set_data(data); } // === Reads === /// Check whether the given `domain` is registered in the `Registry`. public fun has_record(self: &Registry, domain: Domain): bool { - table::contains(&self.registry, domain) + self.registry.contains(domain) } /// Returns the `NameRecord` associated with the given domain or None. public fun lookup(self: &Registry, domain: Domain): Option { - if (table::contains(&self.registry, domain)) { - let record = table::borrow(&self.registry, domain); + if (self.registry.contains(domain)) { + let record = &self.registry[domain]; some(*record) } else { none() @@ -272,7 +269,7 @@ module suins::registry { /// Returns the `domain_name` associated with the given address or None. public fun reverse_lookup(self: &Registry, address: address): Option { if (table::contains(&self.reverse_registry, address)) { - some(*table::borrow(&self.reverse_registry, address)) + some(self.reverse_registry[address]) } else { none() } @@ -282,19 +279,19 @@ module suins::registry { /// 1. Matches the ID in the corresponding `Record` /// 2. Has not expired (does not take into account the grace period) public fun assert_nft_is_authorized(self: &Registry, nft: &SuinsRegistration, clock: &Clock) { - let domain = nft::domain(nft); - let record = table::borrow(&self.registry, domain); + let domain = nft.domain(); + let record = &self.registry[domain]; // The NFT does not - assert!(object::id(nft) == name_record::nft_id(record), EIdMismatch); - assert!(!name_record::has_expired(record, clock), ERecordExpired); - assert!(!nft::has_expired(nft, clock), ENftExpired); + assert!(object::id(nft) == record.nft_id(), EIdMismatch); + assert!(!record.has_expired(clock), ERecordExpired); + assert!(!nft.has_expired(clock), ENftExpired); } /// Returns the `data` associated with the given `Domain`. public fun get_data(self: &Registry, domain: Domain): &VecMap { - let record = table::borrow(&self.registry, domain); - name_record::data(record) + let record = &self.registry[domain]; + record.data() } // === Private Functions === @@ -309,7 +306,7 @@ module suins::registry { self: &Registry, domain: Domain ): bool { - if (!domain::is_subdomain(&domain)) { + if (!domain.is_subdomain()) { return false }; @@ -319,7 +316,7 @@ module suins::registry { return false }; - name_record::is_leaf_record(option::borrow(&option_name_record)) + option_name_record.borrow().is_leaf_record() } /// An internal helper to add a record @@ -336,8 +333,8 @@ module suins::registry { // If we've made it to this point then we know that we are able to // register an entry for this domain. let nft = nft::new(domain, no_years, clock, ctx); - let name_record = name_record::new(object::id(&nft), nft::expiration_timestamp_ms(&nft)); - table::add(&mut self.registry, domain, name_record); + let name_record = name_record::new(object::id(&nft), nft.expiration_timestamp_ms()); + self.registry.add(domain, name_record); nft } @@ -348,15 +345,15 @@ module suins::registry { with_grace_period: bool, ) { // if the domain is not part of the registry, we can override. - if (!table::contains(&self.registry, domain)) return; + if (!self.registry.contains(domain)) return; // Remove the record and assert that it has expired (past the grace period if applicable) - let record = table::remove(&mut self.registry, domain); + let record = self.registry.remove(domain); // Special case for leaf records, we can override them iff their parent has changed or has expired. - if (name_record::is_leaf_record(&record)) { + if (record.is_leaf_record()) { // find the parent of the leaf record. - let option_parent_name_record = lookup(self, domain::parent(&domain)); + let option_parent_name_record = lookup(self, domain.parent()); // if there's a parent (if not, we can just remove it), we need to check if the parent is valid. // -> If the parent is valid, we need to check if the parent is expired. @@ -366,17 +363,17 @@ module suins::registry { // If the parent is the same and hasn't expired, we can't override the leaf record like this. // We need to first remove + then call create (to protect accidental overrides). - if (name_record::nft_id(parent_name_record) == name_record::nft_id(&record)) { - assert!(name_record::has_expired(parent_name_record, clock), ERecordNotExpired); + if (parent_name_record.nft_id() == record.nft_id()) { + assert!(parent_name_record.has_expired(clock), ERecordNotExpired); }; } }else if (with_grace_period) { - assert!(name_record::has_expired_past_grace_period(&record, clock), ERecordNotExpired); + assert!(record.has_expired_past_grace_period(clock), ERecordNotExpired); } else { - assert!(name_record::has_expired(&record, clock), ERecordNotExpired); + assert!(record.has_expired(clock), ERecordNotExpired); }; - let old_target_address = name_record::target_address(&record); + let old_target_address = record.target_address(); handle_invalidate_reverse_record(self, &domain, old_target_address, none()); } @@ -398,9 +395,9 @@ module suins::registry { let reverse_registry = &mut self.reverse_registry; if (table::contains(reverse_registry, old_target_address)) { - let default_domain = table::borrow(reverse_registry, old_target_address); + let default_domain = &reverse_registry[old_target_address]; if (default_domain == domain) { - table::remove(reverse_registry, old_target_address); + reverse_registry.remove(old_target_address); } }; } @@ -427,7 +424,7 @@ module suins::registry { self: &mut Registry, domain: Domain, ): NameRecord { - table::remove(&mut self.registry, domain) + self.registry.remove(domain) } #[test_only] @@ -437,8 +434,8 @@ module suins::registry { reverse_registry, } = self; - table::destroy_empty(registry); - table::destroy_empty(reverse_registry); + registry.destroy_empty(); + reverse_registry.destroy_empty(); } #[test_only] @@ -448,7 +445,7 @@ module suins::registry { reverse_registry, } = self; - table::drop(registry); - table::drop(reverse_registry); + registry.drop(); + reverse_registry.drop(); } } diff --git a/packages/suins/sources/subdomain_registration.move b/packages/suins/sources/subdomain_registration.move index 5a28b601..9c8c25b8 100644 --- a/packages/suins/sources/subdomain_registration.move +++ b/packages/suins/sources/subdomain_registration.move @@ -8,13 +8,9 @@ /// /// We maintain all core functionality unchanged for registry, expiration etc. module suins::subdomain_registration { - use sui::object::{Self, UID}; - - use sui::tx_context::TxContext; use sui::clock::Clock; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::domain; + use suins::suins_registration::SuinsRegistration; /* friend suins::registry; */ /* #[test_only] */ /* friend suins::sub_name_tests; */ @@ -38,9 +34,9 @@ module suins::subdomain_registration { /// (as long as it's used for a subdomain). public(package) fun new(nft: SuinsRegistration, clock: &Clock, ctx: &mut TxContext): SubDomainRegistration { // Can't wrap a non-subdomain NFT. - assert!(domain::is_subdomain(&suins_registration::domain(&nft)), ENotSubdomain); + assert!(nft.domain().is_subdomain(), ENotSubdomain); // Can't wrap an expired NFT. - assert!(!suins_registration::has_expired(&nft, clock), EExpired); + assert!(!nft.has_expired(clock), EExpired); SubDomainRegistration { id: object::new(ctx), @@ -52,7 +48,7 @@ module suins::subdomain_registration { /// Fails if the subname is not expired. public(package) fun burn(name: SubDomainRegistration, clock: &Clock): SuinsRegistration { // tries to unwrap a non-expired subname. - assert!(suins_registration::has_expired(&name.nft, clock), ENameNotExpired); + assert!(name.nft.has_expired(clock), ENameNotExpired); let SubDomainRegistration { id, nft diff --git a/packages/suins/sources/suins.move b/packages/suins/sources/suins.move index 4b1e3993..7d29393e 100644 --- a/packages/suins/sources/suins.move +++ b/packages/suins/sources/suins.move @@ -21,13 +21,7 @@ /// - Any of the old modules can be deauthorized hence disabling its access to /// the registry and the balance. module suins::suins { - use sui::tx_context::{Self, TxContext}; - use sui::balance::{Self, Balance}; - use sui::coin::{Self, Coin}; - use sui::dynamic_field as df; - use sui::object::{Self, UID}; - use sui::transfer; - use sui::sui::SUI; + use sui::{balance::{Self, Balance}, coin::{Self, Coin}, dynamic_field as df, sui::SUI}; /// Trying to withdraw from an empty balance. const ENoProfits: u64 = 0; @@ -94,7 +88,7 @@ module suins::suins { /// transaction. This is useful for the admin to withdraw funds from the SuiNS /// and then send them somewhere specific or keep at the address. public fun withdraw(_: &AdminCap, self: &mut SuiNS, ctx: &mut TxContext): Coin { - let amount = balance::value(&self.balance); + let amount = self.balance.value(); assert!(amount > 0, ENoProfits); coin::take(&mut self.balance, amount, ctx) } @@ -134,7 +128,7 @@ module suins::suins { /// Adds balance to the SuiNS. public fun app_add_balance(_: App, self: &mut SuiNS, balance: Balance) { assert_app_is_authorized(self); - balance::join(&mut self.balance, balance); + self.balance.join(balance); } /// Get a mutable access to the `Registry` object. Can only be performed by authorized @@ -236,6 +230,6 @@ module suins::suins { #[test_only] public fun total_balance(self: &SuiNS): u64 { - balance::value(&self.balance) + self.balance.value() } } diff --git a/packages/suins/sources/suins_registration.move b/packages/suins/sources/suins_registration.move index a154753c..0a0b89b5 100644 --- a/packages/suins/sources/suins_registration.move +++ b/packages/suins/sources/suins_registration.move @@ -12,12 +12,9 @@ /// module suins::suins_registration { use std::string::{String}; - use sui::object::{Self, UID}; - use sui::tx_context::TxContext; use sui::clock::{timestamp_ms, Clock}; - use suins::constants; - use suins::domain::{Self, Domain}; + use suins::{constants, domain::Domain}; /* friend suins::registry; */ /* friend suins::update_image; */ @@ -47,7 +44,7 @@ module suins::suins_registration { ): SuinsRegistration { SuinsRegistration { id: object::new(ctx), - domain_name: domain::to_string(&domain), + domain_name: domain.to_string(), domain, expiration_timestamp_ms: timestamp_ms(clock) + ((no_years as u64) * constants::year_ms()), image_url: constants::default_image(), diff --git a/packages/suins/tests/auction_tests.move b/packages/suins/tests/auction_tests.move index 9a3d5ad6..b32f81cd 100644 --- a/packages/suins/tests/auction_tests.move +++ b/packages/suins/tests/auction_tests.move @@ -3,7 +3,6 @@ #[test_only] module suins::auction_tests { - use std::option; use std::string::{String, utf8}; use sui::test_scenario::{Self, Scenario, ctx}; @@ -11,16 +10,27 @@ module suins::auction_tests { use sui::clock::{Self, Clock}; use sui::coin::{Self, Coin}; - use suins::auction::{ - Self, App as AuctionApp, place_bid, claim, AuctionHouse, start_auction_and_place_bid, total_balance, - admin_finalize_auction, admin_try_finalize_auctions, admin_withdraw_funds, collect_winning_auction_fund + use suins::{ + auction::{ + Self, + App as AuctionApp, + place_bid, + claim, + AuctionHouse, + start_auction_and_place_bid, + total_balance, + admin_finalize_auction, + admin_try_finalize_auctions, + admin_withdraw_funds, + collect_winning_auction_fund + }, + suins_registration::SuinsRegistration, + config, + domain, + constants::{Self, mist_per_sui}, + suins::{Self, SuiNS, AdminCap}, + registry }; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::config; - use suins::domain; - use suins::constants::{Self, mist_per_sui}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::registry; const SUINS_ADDRESS: address = @0xA001; const FIRST_ADDRESS: address = @0xB001; @@ -29,23 +39,22 @@ module suins::auction_tests { const FIRST_DOMAIN_NAME: vector = b"tes-t2.sui"; const SECOND_DOMAIN_NAME: vector = b"tesq.sui"; const AUCTION_BIDDING_PERIOD_MS: u64 = 2 * 24 * 60 * 60 * 1000; - const AUCTION_MIN_QUIET_PERIOD_MS: u64 = 10 * 60 * 1000; // 10 minutes of quiet time public fun test_init(): Scenario { let mut scenario_val = test_scenario::begin(SUINS_ADDRESS); let scenario = &mut scenario_val; { let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::share_for_testing(suins); + suins.authorize_app_for_testing(); + suins.share_for_testing(); auction::init_for_testing(ctx(scenario)); let clock = clock::create_for_testing(ctx(scenario)); - clock::share_for_testing(clock); + clock.share_for_testing(); }; { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); @@ -61,11 +70,11 @@ module suins::auction_tests { domain_name: String, amount: u64 ) { - test_scenario::next_tx(scenario, sender); - let mut auction_house = test_scenario::take_shared(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut auction_house = scenario.take_shared(); + let mut suins = scenario.take_shared(); let payment = coin::mint_for_testing(amount, ctx(scenario)); - let clock = test_scenario::take_shared(scenario); + let clock = scenario.take_shared(); start_auction_and_place_bid( &mut auction_house, @@ -82,12 +91,12 @@ module suins::auction_tests { } fun place_bid_util(scenario: &mut Scenario, sender: address, domain_name: String, value: u64, clock_tick: u64) { - test_scenario::next_tx(scenario, sender); - let mut auction_house = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut auction_house = scenario.take_shared(); let payment = coin::mint_for_testing(value, ctx(scenario)); - let mut clock = test_scenario::take_shared(scenario); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); place_bid(&mut auction_house, domain_name, payment, &clock, ctx(scenario)); test_scenario::return_shared(clock); @@ -100,11 +109,11 @@ module suins::auction_tests { domain_name: String, clock_tick: u64 ): SuinsRegistration { - test_scenario::next_tx(scenario, sender); - let mut auction_house = test_scenario::take_shared(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut auction_house = scenario.take_shared(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); let nft = claim(&mut auction_house, domain_name, &clock, ctx(scenario)); test_scenario::return_shared(clock); @@ -113,17 +122,17 @@ module suins::auction_tests { } fun withdraw_util(scenario: &mut Scenario, sender: address): Coin { - test_scenario::next_tx(scenario, sender); + scenario.next_tx(sender); let returned_payment = test_scenario::take_from_sender>(scenario); returned_payment } fun admin_collect_fund_util(scenario: &mut Scenario, domain_name: String, clock_tick: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let mut auction_house = test_scenario::take_shared(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let mut auction_house = scenario.take_shared(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); collect_winning_auction_fund(&mut auction_house, domain_name, &clock, ctx(scenario)); test_scenario::return_shared(clock); @@ -135,12 +144,12 @@ module suins::auction_tests { domain: String, clock_tick: u64 ) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut auction_house = test_scenario::take_shared(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut auction_house = scenario.take_shared(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); admin_finalize_auction(&admin_cap, &mut auction_house, domain, &clock); test_scenario::return_shared(clock); @@ -149,12 +158,12 @@ module suins::auction_tests { } fun admin_try_finalize_auctions_util(scenario: &mut Scenario, operation_limit: u64, clock_tick: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut auction_house = test_scenario::take_shared(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut auction_house = scenario.take_shared(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); admin_try_finalize_auctions(&admin_cap, &mut auction_house, operation_limit, &clock); test_scenario::return_shared(clock); @@ -163,9 +172,9 @@ module suins::auction_tests { } fun admin_withdraw_funds_util(scenario: &mut Scenario): Coin { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut auction_house = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut auction_house = scenario.take_shared(); let funds = admin_withdraw_funds(&admin_cap, &mut auction_house, ctx(scenario)); @@ -175,9 +184,9 @@ module suins::auction_tests { } fun deauthorize_app_util(scenario: &mut Scenario) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); suins::deauthorize_app(&admin_cap, &mut suins); @@ -186,8 +195,8 @@ module suins::auction_tests { } fun assert_balance(scenario: &mut Scenario, amount: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let auction_house = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let auction_house = scenario.take_shared(); assert!(total_balance(&auction_house) == amount, 0); test_scenario::return_shared(auction_house); } @@ -200,9 +209,9 @@ module suins::auction_tests { expected_winner: address, expected_highest_amount: u64 ) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let auction_house = test_scenario::take_shared(scenario); - let (mut start_ms, mut end_ms, mut winner, mut highest_amount) = auction::get_auction_metadata(&auction_house, domain_name); + scenario.next_tx(SUINS_ADDRESS); + let auction_house = scenario.take_shared(); + let (mut start_ms, mut end_ms, mut winner, mut highest_amount) = auction_house.get_auction_metadata(domain_name); assert!(option::extract(&mut start_ms) == expected_start_ms, 0); assert!(option::extract(&mut end_ms) == expected_end_ms, 0); assert!(option::extract(&mut winner) == expected_winner, 0); @@ -236,9 +245,9 @@ module suins::auction_tests { ); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = withdraw_util(scenario, FIRST_ADDRESS); assert!(coin::value(&payment) == 1200 * mist_per_sui(), 0); @@ -256,7 +265,7 @@ module suins::auction_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; normal_auction_flow(scenario); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = sui::dynamic_field::EFieldDoesNotExist)] @@ -272,10 +281,10 @@ module suins::auction_tests { place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1210 * mist_per_sui(), 0); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - suins_registration::burn_for_testing(nft); - test_scenario::end(scenario_val); + nft.burn_for_testing(); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EWinnerCannotPlaceBid)] @@ -291,7 +300,7 @@ module suins::auction_tests { place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1210 * mist_per_sui(), 0); place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1210 * mist_per_sui(), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EBidAmountTooLow)] @@ -306,7 +315,7 @@ module suins::auction_tests { ); place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1200 * mist_per_sui(), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::ENotWinner)] @@ -322,8 +331,8 @@ module suins::auction_tests { place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1210 * mist_per_sui(), 0); let nft = claim_util(scenario, FIRST_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - suins_registration::burn_for_testing(nft); - test_scenario::end(scenario_val); + nft.burn_for_testing(); + scenario_val.end(); } #[test] @@ -343,9 +352,9 @@ module suins::auction_tests { assert_balance(scenario, 1220 * mist_per_sui()); let nft = test_scenario::take_from_address(scenario, THIRD_ADDRESS); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = test_scenario::take_from_address>(scenario, SECOND_ADDRESS); assert!(coin::value(&payment) == 1210 * mist_per_sui(), 0); @@ -356,7 +365,7 @@ module suins::auction_tests { coin::burn_for_testing(payment); assert_balance(scenario, 1220 * mist_per_sui()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -380,16 +389,16 @@ module suins::auction_tests { assert_balance(scenario, 2410 * mist_per_sui()); let nft = test_scenario::take_from_address(scenario, SECOND_ADDRESS); - assert!(suins_registration::domain(&nft) == domain::new(utf8(SECOND_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(SECOND_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let nft = test_scenario::take_from_address(scenario, FIRST_ADDRESS); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EAuctionNotEndedYet)] @@ -404,7 +413,7 @@ module suins::auction_tests { ); admin_try_finalize_auction_util(scenario, utf8(FIRST_DOMAIN_NAME), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -420,7 +429,7 @@ module suins::auction_tests { admin_try_finalize_auctions_util(scenario, 3, 0); assert_balance(scenario, 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EAuctionEnded)] @@ -440,7 +449,7 @@ module suins::auction_tests { 1210 * mist_per_sui(), AUCTION_BIDDING_PERIOD_MS + 1 ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::ENoProfits)] @@ -449,7 +458,7 @@ module suins::auction_tests { let scenario = &mut scenario_val; let funds = admin_withdraw_funds_util(scenario); coin::burn_for_testing(funds); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::EInvalidTld)] @@ -462,7 +471,7 @@ module suins::auction_tests { utf8(b"test.move"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::ELabelTooShort)] @@ -475,7 +484,7 @@ module suins::auction_tests { utf8(b"tt.sui"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -488,7 +497,7 @@ module suins::auction_tests { utf8(b"g2bst97onsyl8gwo5brfglcb-obh8i7p01lz5ccscd6zxx4qn7wnv8b1in5sectj8s.sui"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -501,7 +510,7 @@ module suins::auction_tests { utf8(b"-test.sui"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -514,7 +523,7 @@ module suins::auction_tests { utf8(b"test-.sui"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -527,7 +536,7 @@ module suins::auction_tests { utf8(b"ttABC.sui"), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EAuctionNotStarted)] @@ -535,7 +544,7 @@ module suins::auction_tests { let mut scenario_val = test_init(); let scenario = &mut scenario_val; place_bid_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), 1210 * mist_per_sui(), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EInvalidBidValue)] @@ -548,7 +557,7 @@ module suins::auction_tests { utf8(b"test.sui"), 10 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -573,9 +582,9 @@ module suins::auction_tests { assert_balance(scenario, 1210 * mist_per_sui()); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = withdraw_util(scenario, FIRST_ADDRESS); assert!(coin::value(&payment) == 1200 * mist_per_sui(), 0); @@ -587,7 +596,7 @@ module suins::auction_tests { assert_balance(scenario, 0); coin::burn_for_testing(funds); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = auction::EAuctionNotEndedYet)] @@ -601,7 +610,7 @@ module suins::auction_tests { 1200 * mist_per_sui() ); admin_collect_fund_util(scenario, utf8(FIRST_DOMAIN_NAME), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -615,7 +624,7 @@ module suins::auction_tests { utf8(FIRST_DOMAIN_NAME), 1200 * mist_per_sui() ); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -640,9 +649,9 @@ module suins::auction_tests { ); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = withdraw_util(scenario, FIRST_ADDRESS); assert!(coin::value(&payment) == 1200 * mist_per_sui(), 0); @@ -654,7 +663,7 @@ module suins::auction_tests { assert_balance(scenario, 0); coin::burn_for_testing(funds); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -675,9 +684,9 @@ module suins::auction_tests { assert_balance(scenario, 1220 * mist_per_sui()); let nft = test_scenario::take_from_address(scenario, THIRD_ADDRESS); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = test_scenario::take_from_address>(scenario, SECOND_ADDRESS); assert!(coin::value(&payment) == 1210 * mist_per_sui(), 0); @@ -688,7 +697,7 @@ module suins::auction_tests { coin::burn_for_testing(payment); assert_balance(scenario, 1220 * mist_per_sui()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -714,20 +723,20 @@ module suins::auction_tests { assert_balance(scenario, 1210 * mist_per_sui()); let nft = claim_util(scenario, SECOND_ADDRESS, utf8(FIRST_DOMAIN_NAME), AUCTION_BIDDING_PERIOD_MS + 1); - assert!(suins_registration::domain(&nft) == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == constants::year_ms(), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(FIRST_DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == constants::year_ms(), 0); + nft.burn_for_testing(); let payment = withdraw_util(scenario, FIRST_ADDRESS); - assert!(coin::value(&payment) == 1200 * mist_per_sui(), 0); - coin::burn_for_testing(payment); + assert!(payment.value() == 1200 * mist_per_sui(), 0); + payment.burn_for_testing(); assert_balance(scenario, 1210 * mist_per_sui()); let funds = admin_withdraw_funds_util(scenario); - assert!(coin::value(&funds) == 1210 * mist_per_sui(), 0); + assert!(funds.value() == 1210 * mist_per_sui(), 0); assert_balance(scenario, 0); - coin::burn_for_testing(funds); + funds.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } } diff --git a/packages/suins/tests/controller_tests.move b/packages/suins/tests/controller_tests.move index 11260b23..1a145267 100644 --- a/packages/suins/tests/controller_tests.move +++ b/packages/suins/tests/controller_tests.move @@ -3,30 +3,27 @@ #[test_only] module suins::controller_tests { - use std::string::{utf8, String}; - use std::option::{Option, extract, some, none}; - - use sui::test_scenario::{Self, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::transfer; - use sui::test_utils::assert_eq; - use sui::dynamic_field; - use sui::vec_map::{Self, VecMap}; - - use suins::register_sample::Register; - use suins::constants::{mist_per_sui, year_ms}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::suins_registration::SuinsRegistration; - use suins::register_sample_tests::register_util; - use suins::controller::{Self, Controller, set_target_address_for_testing, set_reverse_lookup_for_testing, unset_reverse_lookup_for_testing, set_user_data_for_testing, unset_user_data_for_testing}; - use suins::registry::{Self, Registry, lookup, reverse_lookup}; - use suins::name_record; - use suins::domain::{Self, Domain}; + use std::{string::{utf8, String}, option::{extract, some, none}}; + + use sui::{test_scenario::{Self, Scenario, ctx}, clock::{Self, Clock}, test_utils::assert_eq, dynamic_field, vec_map::VecMap}; + + use suins::{ + register_sample::Register, + constants::{mist_per_sui, year_ms}, + suins::{Self, SuiNS, AdminCap}, + suins_registration::SuinsRegistration, + register_sample_tests::register_util, + controller::{ + Self, Controller, set_target_address_for_testing, set_reverse_lookup_for_testing, + unset_reverse_lookup_for_testing, set_user_data_for_testing, unset_user_data_for_testing + }, + registry::{Self, Registry, lookup, reverse_lookup}, + domain::{Self, Domain} + }; const SUINS_ADDRESS: address = @0xA001; const FIRST_ADDRESS: address = @0xB001; const SECOND_ADDRESS: address = @0xB002; - const AUCTIONED_DOMAIN_NAME: vector = b"tes-t2.sui"; const DOMAIN_NAME: vector = b"abc.sui"; const AVATAR: vector = b"avatar"; const CONTENT_HASH: vector = b"content_hash"; @@ -36,16 +33,16 @@ module suins::controller_tests { let scenario = &mut scenario_val; { let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); + suins.authorize_app_for_testing(); suins::authorize_app_for_testing(&mut suins); suins::share_for_testing(suins); let clock = clock::create_for_testing(ctx(scenario)); - clock::share_for_testing(clock); + clock.share_for_testing(); }; { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); @@ -61,12 +58,12 @@ module suins::controller_tests { } public fun set_target_address_util(scenario: &mut Scenario, sender: address, target: Option
, clock_tick: u64) { - test_scenario::next_tx(scenario, sender); - let mut suins = test_scenario::take_shared(scenario); - let nft = test_scenario::take_from_sender(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut suins = scenario.take_shared(); + let nft = scenario.take_from_sender(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); set_target_address_for_testing(&mut suins, &nft, target, &clock); test_scenario::return_shared(clock); @@ -75,8 +72,8 @@ module suins::controller_tests { } public fun set_reverse_lookup_util(scenario: &mut Scenario, sender: address, domain_name: String) { - test_scenario::next_tx(scenario, sender); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut suins = scenario.take_shared(); set_reverse_lookup_for_testing(&mut suins, domain_name, ctx(scenario)); @@ -84,8 +81,8 @@ module suins::controller_tests { } public fun unset_reverse_lookup_util(scenario: &mut Scenario, sender: address) { - test_scenario::next_tx(scenario, sender); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut suins = scenario.take_shared(); unset_reverse_lookup_for_testing(&mut suins, ctx(scenario)); @@ -93,12 +90,12 @@ module suins::controller_tests { } public fun set_user_data_util(scenario: &mut Scenario, sender: address, key: String, value: String, clock_tick: u64) { - test_scenario::next_tx(scenario, sender); - let mut suins = test_scenario::take_shared(scenario); - let nft = test_scenario::take_from_sender(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut suins = scenario.take_shared(); + let nft = scenario.take_from_sender(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); set_user_data_for_testing(&mut suins, &nft, key, value, &clock); test_scenario::return_shared(clock); @@ -107,12 +104,12 @@ module suins::controller_tests { } public fun unset_user_data_util(scenario: &mut Scenario, sender: address, key: String, clock_tick: u64) { - test_scenario::next_tx(scenario, sender); - let mut suins = test_scenario::take_shared(scenario); - let nft = test_scenario::take_from_sender(scenario); - let mut clock = test_scenario::take_shared(scenario); + scenario.next_tx(sender); + let mut suins = scenario.take_shared(); + let nft = scenario.take_from_sender(); + let mut clock = scenario.take_shared(); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); unset_user_data_for_testing(&mut suins, &nft, key, &clock); test_scenario::return_shared(clock); @@ -121,33 +118,33 @@ module suins::controller_tests { } fun lookup_util(scenario: &mut Scenario, domain_name: String, expected_target_addr: Option
) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let suins = scenario.take_shared(); - let registry = suins::registry(&suins); + let registry = suins.registry(); let record = extract(&mut lookup(registry, domain::new(domain_name))); - assert_eq(name_record::target_address(&record), expected_target_addr); + assert_eq(record.target_address(), expected_target_addr); test_scenario::return_shared(suins); } fun get_user_data(scenario: &mut Scenario, domain_name: String): VecMap { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let suins = scenario.take_shared(); - let registry = suins::registry(&suins); + let registry = suins.registry(); let record = extract(&mut lookup(registry, domain::new(domain_name))); - let data = *name_record::data(&record); + let data = *record.data(); test_scenario::return_shared(suins); data } fun reverse_lookup_util(scenario: &mut Scenario, addr: address, expected_domain_name: Option) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let suins = scenario.take_shared(); - let registry = suins::registry(&suins); + let registry = suins.registry(); let domain_name = reverse_lookup(registry, addr); assert_eq(domain_name, expected_domain_name); @@ -155,9 +152,9 @@ module suins::controller_tests { } fun deauthorize_app_util(scenario: &mut Scenario) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); suins::deauthorize_app(&admin_cap, &mut suins); @@ -178,7 +175,7 @@ module suins::controller_tests { set_target_address_util(scenario, FIRST_ADDRESS, none(), 0); lookup_util(scenario, utf8(DOMAIN_NAME), none()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordExpired)] @@ -189,7 +186,7 @@ module suins::controller_tests { set_target_address_util(scenario, FIRST_ADDRESS, some(SECOND_ADDRESS), 2 * year_ms()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::EIdMismatch)] @@ -201,7 +198,7 @@ module suins::controller_tests { set_target_address_util(scenario, FIRST_ADDRESS, some(SECOND_ADDRESS), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -214,7 +211,7 @@ module suins::controller_tests { set_target_address_util(scenario, SECOND_ADDRESS, some(SECOND_ADDRESS), 0); lookup_util(scenario, utf8(DOMAIN_NAME), some(SECOND_ADDRESS)); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -226,7 +223,7 @@ module suins::controller_tests { deauthorize_app_util(scenario); set_target_address_util(scenario, FIRST_ADDRESS, some(SECOND_ADDRESS), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -247,7 +244,7 @@ module suins::controller_tests { reverse_lookup_util(scenario, FIRST_ADDRESS, some(domain::new(utf8(DOMAIN_NAME)))); reverse_lookup_util(scenario, SECOND_ADDRESS, none()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ETargetNotSet)] @@ -259,7 +256,7 @@ module suins::controller_tests { reverse_lookup_util(scenario, SECOND_ADDRESS, none()); set_reverse_lookup_util(scenario, SECOND_ADDRESS, utf8(DOMAIN_NAME)); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordMismatch)] @@ -272,7 +269,7 @@ module suins::controller_tests { reverse_lookup_util(scenario, SECOND_ADDRESS, none()); set_reverse_lookup_util(scenario, SECOND_ADDRESS, utf8(DOMAIN_NAME)); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -285,7 +282,7 @@ module suins::controller_tests { deauthorize_app_util(scenario); set_reverse_lookup_util(scenario, SECOND_ADDRESS, utf8(DOMAIN_NAME)); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -300,7 +297,7 @@ module suins::controller_tests { unset_reverse_lookup_util(scenario, SECOND_ADDRESS); reverse_lookup_util(scenario, SECOND_ADDRESS, none()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -314,7 +311,7 @@ module suins::controller_tests { deauthorize_app_util(scenario); unset_reverse_lookup_util(scenario, SECOND_ADDRESS); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = dynamic_field::EFieldDoesNotExist)] @@ -325,7 +322,7 @@ module suins::controller_tests { unset_reverse_lookup_util(scenario, SECOND_ADDRESS); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -335,19 +332,19 @@ module suins::controller_tests { setup(scenario, FIRST_ADDRESS, 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 0); + assert_eq(data.size(), 0); set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value_avatar"), 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 1); - assert_eq(*vec_map::get(data, &utf8(AVATAR)), utf8(b"value_avatar")); + assert_eq(data.size(), 1); + assert_eq(*data.get(&utf8(AVATAR)), utf8(b"value_avatar")); set_user_data_util(scenario, FIRST_ADDRESS, utf8(CONTENT_HASH), utf8(b"value_content_hash"), 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 2); - assert_eq(*vec_map::get(data, &utf8(AVATAR)), utf8(b"value_avatar")); - assert_eq(*vec_map::get(data, &utf8(CONTENT_HASH)), utf8(b"value_content_hash")); + assert_eq(data.size(), 2); + assert_eq(*data.get(&utf8(AVATAR)), utf8(b"value_avatar")); + assert_eq(*data.get(&utf8(CONTENT_HASH)), utf8(b"value_content_hash")); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = controller::EUnsupportedKey)] @@ -358,7 +355,7 @@ module suins::controller_tests { set_user_data_util(scenario, FIRST_ADDRESS, utf8(b"key"), utf8(b"value"), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordExpired)] @@ -369,7 +366,7 @@ module suins::controller_tests { set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value"), 2 * year_ms()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::EIdMismatch)] @@ -381,7 +378,7 @@ module suins::controller_tests { set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value"), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -393,10 +390,10 @@ module suins::controller_tests { set_user_data_util(scenario, SECOND_ADDRESS, utf8(AVATAR), utf8(b"value"), 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 1); - assert_eq(*vec_map::get(data, &utf8(AVATAR)), utf8(b"value")); + assert_eq(data.size(), 1); + assert_eq(*data.get(&utf8(AVATAR)), utf8(b"value")); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -408,7 +405,7 @@ module suins::controller_tests { deauthorize_app_util(scenario); set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value_avatar"), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -420,16 +417,16 @@ module suins::controller_tests { set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value_avatar"), 0); unset_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 0); + assert_eq(data.size(), 0); set_user_data_util(scenario, FIRST_ADDRESS, utf8(CONTENT_HASH), utf8(b"value_content_hash"), 0); set_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), utf8(b"value_avatar"), 0); unset_user_data_util(scenario, FIRST_ADDRESS, utf8(CONTENT_HASH), 0); let data = &get_user_data(scenario, utf8(DOMAIN_NAME)); - assert_eq(vec_map::size(data), 1); - assert_eq(*vec_map::get(data, &utf8(AVATAR)), utf8(b"value_avatar")); + assert_eq(data.size(), 1); + assert_eq(*data.get(&utf8(AVATAR)), utf8(b"value_avatar")); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -440,7 +437,7 @@ module suins::controller_tests { unset_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordExpired)] @@ -451,7 +448,7 @@ module suins::controller_tests { unset_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), 2 * year_ms()); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -463,6 +460,6 @@ module suins::controller_tests { deauthorize_app_util(scenario); unset_user_data_util(scenario, FIRST_ADDRESS, utf8(AVATAR), 0); - test_scenario::end(scenario_val); + scenario_val.end(); } } diff --git a/packages/suins/tests/register_tests.move b/packages/suins/tests/register_tests.move index 1ac3520f..de950606 100644 --- a/packages/suins/tests/register_tests.move +++ b/packages/suins/tests/register_tests.move @@ -6,21 +6,19 @@ module suins::register_sample_tests { use std::string::{utf8, String}; - use sui::test_scenario::{Self, Scenario, ctx}; - use sui::clock::{Self, Clock}; - use sui::coin; - use sui::sui::SUI; - - use suins::register_sample::{Self as register, Register, register}; - use suins::constants::{mist_per_sui, grace_period_ms, year_ms}; - use suins::suins::{Self, SuiNS, total_balance, AdminCap}; - use suins::suins_registration::SuinsRegistration; - use suins::suins_registration; - use suins::domain; - use suins::registry; - use suins::config; - use suins::auction_tests; - use suins::auction::{Self, App as AuctionApp}; + use sui::{test_scenario::{Self, Scenario, ctx}, clock::{Self, Clock}, coin, sui::SUI}; + + use suins::{ + register_sample::{Self as register, Register, register}, + constants::{mist_per_sui, grace_period_ms, year_ms}, + suins::{Self, SuiNS, total_balance, AdminCap}, + suins_registration::SuinsRegistration, + domain, + registry, + config, + auction_tests, + auction::{Self, App as AuctionApp}, + }; const SUINS_ADDRESS: address = @0xA001; const AUCTIONED_DOMAIN_NAME: vector = b"tes-t2.sui"; @@ -31,16 +29,16 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; { let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::authorize_app_for_testing(&mut suins); + suins.authorize_app_for_testing(); + suins.authorize_app_for_testing(); suins::share_for_testing(suins); let clock = clock::create_for_testing(ctx(scenario)); - clock::share_for_testing(clock); + clock.share_for_testing(); }; { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); @@ -57,12 +55,12 @@ module suins::register_sample_tests { amount: u64, clock_tick: u64 ): SuinsRegistration { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let mut suins = scenario.take_shared(); let payment = coin::mint_for_testing(amount, ctx(scenario)); let mut clock = test_scenario::take_shared(scenario); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); let nft = register(&mut suins, domain_name, no_years, payment, &clock, ctx(scenario)); test_scenario::return_shared(clock); @@ -72,9 +70,9 @@ module suins::register_sample_tests { } fun deauthorize_app_util(scenario: &mut Scenario) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); suins::deauthorize_app(&admin_cap, &mut suins); @@ -83,8 +81,8 @@ module suins::register_sample_tests { } public fun assert_balance(scenario: &mut Scenario, amount: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let auction_house = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let auction_house = scenario.take_shared(); assert!(total_balance(&auction_house) == amount, 0); test_scenario::return_shared(auction_house); } @@ -96,23 +94,23 @@ module suins::register_sample_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(b"abcd.sui"), 2, 400 * mist_per_sui(), 20); assert_balance(scenario, 1600 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(b"abcd.sui")), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 2 * year_ms() + 30, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(b"abcd.sui")), 0); + assert!(nft.expiration_timestamp_ms() == 2 * year_ms() + 30, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(b"abce-f12.sui"), 3, 150 * mist_per_sui(), 30); assert_balance(scenario, 1750 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(b"abce-f12.sui")), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 3 * year_ms() + 60, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(b"abce-f12.sui")), 0); + assert!(nft.expiration_timestamp_ms() == 3 * year_ms() + 60, 0); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::EInvalidTld)] @@ -121,9 +119,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"abc.move"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EIncorrectAmount)] @@ -132,9 +130,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1210 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EIncorrectAmount)] @@ -143,9 +141,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 90 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EInvalidYearsArgument)] @@ -154,9 +152,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 6, 6 * 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = register::EInvalidYearsArgument)] @@ -165,9 +163,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(DOMAIN_NAME), 0, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -177,9 +175,9 @@ module suins::register_sample_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util( scenario, @@ -189,14 +187,14 @@ module suins::register_sample_tests { year_ms() + grace_period_ms() + 20, ); assert_balance(scenario, 2400 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); assert!( - suins_registration::expiration_timestamp_ms(&nft) == 2 * year_ms() + grace_period_ms() + 30, + nft.expiration_timestamp_ms() == 2 * year_ms() + grace_period_ms() + 30, 0 ); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordNotExpired)] @@ -206,14 +204,14 @@ module suins::register_sample_tests { let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); assert_balance(scenario, 1200 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(DOMAIN_NAME)), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(DOMAIN_NAME)), 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); + nft.burn_for_testing(); let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 20); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -222,9 +220,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"-ab.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -233,9 +231,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"ab-.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = domain::EInvalidDomain)] @@ -244,9 +242,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"Abc.com"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::ELabelTooShort)] @@ -255,9 +253,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"ab.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = config::EInvalidDomain)] @@ -266,9 +264,9 @@ module suins::register_sample_tests { let scenario = &mut scenario_val; let nft = register_util(scenario, utf8(b"abc.xyz.sui"), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = registry::ERecordNotExpired)] @@ -279,9 +277,9 @@ module suins::register_sample_tests { auction_tests::normal_auction_flow(scenario); let nft = register_util(scenario, utf8(AUCTIONED_DOMAIN_NAME), 1, 50 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test] @@ -299,10 +297,10 @@ module suins::register_sample_tests { year_ms() + grace_period_ms() + 20, ); assert_balance(scenario, 50 * mist_per_sui()); - assert!(suins_registration::domain(&nft) == domain::new(utf8(AUCTIONED_DOMAIN_NAME)), 0); - suins_registration::burn_for_testing(nft); + assert!(nft.domain() == domain::new(utf8(AUCTIONED_DOMAIN_NAME)), 0); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -312,8 +310,8 @@ module suins::register_sample_tests { deauthorize_app_util(scenario); let nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } } diff --git a/packages/suins/tests/renew_tests.move b/packages/suins/tests/renew_tests.move index 3bbec113..71d52775 100644 --- a/packages/suins/tests/renew_tests.move +++ b/packages/suins/tests/renew_tests.move @@ -5,18 +5,22 @@ module suins::renew_tests { use std::string::utf8; - use sui::test_scenario::{Self, Scenario, ctx}; - use sui::coin; - use sui::sui::SUI; - use sui::clock::{Self, Clock}; - - use suins::renew::{Self, Renew, renew}; - use suins::register_sample::Register; - use suins::register_sample_tests::{register_util, assert_balance}; - use suins::constants::{mist_per_sui, year_ms}; - use suins::suins::{Self, SuiNS, AdminCap}; - use suins::suins_registration::{Self, SuinsRegistration}; - use suins::registry; + use sui::{ + test_scenario::{Self, Scenario, ctx}, + coin, + sui::SUI, + clock::{Self, Clock} + }; + + use suins::{ + renew::{Self, Renew, renew}, + register_sample::Register, + register_sample_tests::{register_util, assert_balance}, + constants::{mist_per_sui, year_ms}, + suins::{Self, SuiNS, AdminCap}, + suins_registration::SuinsRegistration, + registry, + }; const SUINS_ADDRESS: address = @0xA001; const DOMAIN_NAME: vector = b"abc.sui"; @@ -26,16 +30,16 @@ module suins::renew_tests { let scenario = &mut scenario_val; { let mut suins = suins::init_for_testing(ctx(scenario)); - suins::authorize_app_for_testing(&mut suins); - suins::authorize_app_for_testing(&mut suins); - suins::share_for_testing(suins); + suins.authorize_app_for_testing(); + suins.authorize_app_for_testing(); + suins.share_for_testing(); let clock = clock::create_for_testing(ctx(scenario)); clock::share_for_testing(clock); }; { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); registry::init_for_testing(&admin_cap, &mut suins, ctx(scenario)); @@ -46,12 +50,12 @@ module suins::renew_tests { } fun renew_util(scenario: &mut Scenario, nft: &mut SuinsRegistration, no_years: u8, amount: u64, clock_tick: u64) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let mut suins = scenario.take_shared(); let payment = coin::mint_for_testing(amount, ctx(scenario)); let mut clock = test_scenario::take_shared(scenario); - clock::increment_for_testing(&mut clock, clock_tick); + clock.increment_for_testing(clock_tick); renew(&mut suins, nft, no_years, payment, &clock); test_scenario::return_shared(clock); @@ -59,9 +63,9 @@ module suins::renew_tests { } fun deauthorize_app_util(scenario: &mut Scenario) { - test_scenario::next_tx(scenario, SUINS_ADDRESS); - let admin_cap = test_scenario::take_from_sender(scenario); - let mut suins = test_scenario::take_shared(scenario); + scenario.next_tx(SUINS_ADDRESS); + let admin_cap = scenario.take_from_sender(); + let mut suins = scenario.take_shared(); suins::deauthorize_app(&admin_cap, &mut suins); @@ -75,29 +79,29 @@ module suins::renew_tests { let scenario = &mut scenario_val; let mut nft = register_util(scenario, utf8(b"abcd.sui"), 1, 200 * mist_per_sui(), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms(), 0); + assert!(nft.expiration_timestamp_ms() == year_ms(), 0); renew_util(scenario, &mut nft, 1, 200 * mist_per_sui(), 0); assert_balance(scenario, 400 * mist_per_sui()); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); let mut nft = register_util(scenario, utf8(b"abcde.sui"), 1, 50 * mist_per_sui(), 0); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms(), 0); + assert!(nft.expiration_timestamp_ms() == year_ms(), 0); renew_util(scenario, &mut nft, 1, 50 * mist_per_sui(), 0); assert_balance(scenario, 500 * mist_per_sui()); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); - assert!(suins_registration::expiration_timestamp_ms(&nft) == year_ms() + 10, 0); + assert!(nft.expiration_timestamp_ms() == year_ms() + 10, 0); renew_util(scenario, &mut nft, 1, 1200 * mist_per_sui(), 0); assert_balance(scenario, 2900 * mist_per_sui()); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 2 * year_ms() + 10, 0); + assert!(nft.expiration_timestamp_ms() == 2 * year_ms() + 10, 0); renew_util(scenario, &mut nft, 2, 2400 * mist_per_sui(), 0); assert_balance(scenario, 5300 * mist_per_sui()); - assert!(suins_registration::expiration_timestamp_ms(&nft) == 4 * year_ms() + 10, 0); - suins_registration::burn_for_testing(nft); + assert!(nft.expiration_timestamp_ms() == 4 * year_ms() + 10, 0); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = renew::EIncorrectAmount)] @@ -107,9 +111,9 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); renew_util(scenario, &mut nft, 1, 1210 * mist_per_sui(), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = renew::EIncorrectAmount)] @@ -119,9 +123,9 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); renew_util(scenario, &mut nft, 1, 10 * mist_per_sui(), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = renew::EGracePeriodPassed)] @@ -131,9 +135,9 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 10); renew_util(scenario, &mut nft, 1, 1200 * mist_per_sui(), 2 * year_ms() + 20); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = renew::EInvalidYearsArgument)] @@ -143,9 +147,9 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 0); renew_util(scenario, &mut nft, 6, 7200 * mist_per_sui(), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = renew::EInvalidNewExpiredAt)] @@ -156,9 +160,9 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 0); renew_util(scenario, &mut nft, 2, 2400 * mist_per_sui(), 0); renew_util(scenario, &mut nft, 4, 4800 * mist_per_sui(), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] @@ -169,8 +173,8 @@ module suins::renew_tests { let mut nft = register_util(scenario, utf8(DOMAIN_NAME), 1, 1200 * mist_per_sui(), 0); deauthorize_app_util(scenario); renew_util(scenario, &mut nft, 2, 2400 * mist_per_sui(), 0); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - test_scenario::end(scenario_val); + scenario_val.end(); } } diff --git a/packages/suins/tests/unit/admin_tests.move b/packages/suins/tests/unit/admin_tests.move index b5c5a4c5..73feb054 100644 --- a/packages/suins/tests/unit/admin_tests.move +++ b/packages/suins/tests/unit/admin_tests.move @@ -10,19 +10,9 @@ /// module suins::admin_tests { use std::string::utf8; - use sui::tx_context; - use sui::clock; - use sui::test_utils::assert_eq; + use sui::{clock, test_utils::assert_eq}; - use suins::admin::{Self, Admin}; - use suins::suins_registration as nft; - use suins::constants; - use suins::domain; - use suins::suins; - use suins::registry; - - /// The admin account. - const ADMIN: address = @suins; + use suins::{admin::{Self, Admin}, constants, domain, suins, registry}; #[test, expected_failure(abort_code = ::suins::suins::EAppNotAuthorized)] fun try_unathorized_fail() { @@ -62,11 +52,11 @@ module suins::admin_tests { &mut ctx, ); - assert_eq(nft::domain(&nft), domain::new(utf8(b"test.sui"))); - assert_eq(nft::expiration_timestamp_ms(&nft), constants::year_ms()); + assert_eq(nft.domain(), domain::new(utf8(b"test.sui"))); + assert_eq(nft.expiration_timestamp_ms(), constants::year_ms()); - nft::burn_for_testing(nft); - clock::destroy_for_testing(clock); + nft.burn_for_testing(); + clock.destroy_for_testing(); suins::burn_admin_cap_for_testing(cap); suins::share_for_testing(suins); } diff --git a/packages/suins/tests/unit/config_tests.move b/packages/suins/tests/unit/config_tests.move index 193748d0..b8ae4fb8 100644 --- a/packages/suins/tests/unit/config_tests.move +++ b/packages/suins/tests/unit/config_tests.move @@ -23,22 +23,22 @@ module suins::config_tests { let mut config = default(); // check that the values are set correctly in the `new` function - assert!(config::public_key(&config) == &b"000000000000000000000000000000000", 0); - assert!(config::three_char_price(&config) == THREE, 0); - assert!(config::four_char_price(&config) == FOUR, 0); - assert!(config::five_plus_char_price(&config) == FIVE_PLUS, 0); + assert!(config.public_key() == &b"000000000000000000000000000000000", 0); + assert!(config.three_char_price() == THREE, 0); + assert!(config.four_char_price() == FOUR, 0); + assert!(config.five_plus_char_price() == FIVE_PLUS, 0); // update each of the values and make sure they are updated - config::set_public_key(&mut config, b"000000000000000000000000000000001"); - config::set_three_char_price(&mut config, 4_000_000_000); - config::set_four_char_price(&mut config, 3_000_000_000); - config::set_five_plus_char_price(&mut config, 1_000_000_000); + config.set_public_key(b"000000000000000000000000000000001"); + config.set_three_char_price(4_000_000_000); + config.set_four_char_price(3_000_000_000); + config.set_five_plus_char_price(1_000_000_000); // check that the updated values match the new ones - assert!(config::public_key(&config) == &b"000000000000000000000000000000001", 0); - assert!(config::three_char_price(&config) == 4_000_000_000, 0); - assert!(config::four_char_price(&config) == 3_000_000_000, 0); - assert!(config::five_plus_char_price(&config) == 1_000_000_000, 0); + assert!(config.public_key() == &b"000000000000000000000000000000001", 0); + assert!(config.three_char_price() == 4_000_000_000, 0); + assert!(config.four_char_price() == 3_000_000_000, 0); + assert!(config.five_plus_char_price() == 1_000_000_000, 0); } #[test] @@ -46,34 +46,34 @@ module suins::config_tests { let config = default(); // test each of the length ranges and 1 year duration - assert!(THREE == config::calculate_price(&config, 3, 1), 0); - assert!(FOUR == config::calculate_price(&config, 4, 1), 0); - assert!(FIVE_PLUS == config::calculate_price(&config, 5, 1), 0); - assert!(FIVE_PLUS == config::calculate_price(&config, 6, 1), 0); + assert!(THREE == config.calculate_price(3, 1), 0); + assert!(FOUR == config.calculate_price(4, 1), 0); + assert!(FIVE_PLUS == config.calculate_price(5, 1), 0); + assert!(FIVE_PLUS == config.calculate_price(6, 1), 0); // test each of the length ranges and 2 year duration - assert!(THREE * 2 == config::calculate_price(&config, 3, 2), 0); - assert!(FOUR * 2 == config::calculate_price(&config, 4, 2), 0); - assert!(FIVE_PLUS * 2 == config::calculate_price(&config, 5, 2), 0); - assert!(FIVE_PLUS * 2 == config::calculate_price(&config, 6, 2), 0); + assert!(THREE * 2 == config.calculate_price(3, 2), 0); + assert!(FOUR * 2 == config.calculate_price(4, 2), 0); + assert!(FIVE_PLUS * 2 == config.calculate_price(5, 2), 0); + assert!(FIVE_PLUS * 2 == config.calculate_price(6, 2), 0); } #[test] #[expected_failure(abort_code = suins::config::ENoYears)] fun calculate_price_years_fail() { - config::calculate_price(&default(), 3, 0); + default().calculate_price(3, 0); } #[test] #[expected_failure(abort_code = suins::config::ELabelTooShort)] fun calculate_price_length_min_fail() { - config::calculate_price(&default(), 2, 1); + default().calculate_price(2, 1); } #[test] #[expected_failure(abort_code = suins::config::ELabelTooLong)] fun calculate_price_length_max_fail() { - config::calculate_price(&default(), 255, 1); + default().calculate_price(255, 1); } #[test] @@ -89,7 +89,7 @@ module suins::config_tests { #[expected_failure(abort_code = suins::config::EInvalidPublicKey)] fun set_public_key_invalid_fail() { let mut config = default(); - config::set_public_key(&mut config, vector[]); + config.set_public_key(vector[]); } #[test] diff --git a/packages/suins/tests/unit/name_record_tests.move b/packages/suins/tests/unit/name_record_tests.move index 6f88bee7..e46d91cb 100644 --- a/packages/suins/tests/unit/name_record_tests.move +++ b/packages/suins/tests/unit/name_record_tests.move @@ -9,16 +9,10 @@ /// and the expiration timestamp are working correctly; /// module suins::name_record_tests { - use std::string::utf8; - use std::option::{none, some}; - use sui::object::{Self, ID}; - use sui::tx_context::{Self, TxContext}; - use sui::test_utils::assert_eq; - use sui::vec_map; - use sui::clock; + use std::{string::utf8, option::{none, some}}; + use sui::{test_utils::assert_eq, vec_map, clock}; - use suins::name_record as record; - use suins::constants; + use suins::{name_record as record, constants}; #[test] /// Make sure that the fields are empty by default. That they are updated @@ -29,25 +23,25 @@ module suins::name_record_tests { let mut record = record::new(nft_id, 0); // check default values - assert_eq(record::nft_id(&record), nft_id); - assert_eq(*record::data(&record), vec_map::empty()); - assert_eq(record::target_address(&record), none()); - assert_eq(record::expiration_timestamp_ms(&record), 0); + assert_eq(record.nft_id(), nft_id); + assert_eq(*record.data(), vec_map::empty()); + assert_eq(record.target_address(), none()); + assert_eq(record.expiration_timestamp_ms(), 0); let mut data = vec_map::empty(); - vec_map::insert(&mut data, utf8(b"user_name"), utf8(b"Brandon")); - vec_map::insert(&mut data, utf8(b"age"), utf8(b"forever young")); + data.insert(utf8(b"user_name"), utf8(b"Brandon")); + data.insert(utf8(b"age"), utf8(b"forever young")); // update values - record::set_data(&mut record, *&data); - record::set_target_address(&mut record, some(@suins)); - record::set_expiration_timestamp_ms(&mut record, 123456789); // 123456789 ms = 3.9 years + record.set_data(*&data); + record.set_target_address(some(@suins)); + record.set_expiration_timestamp_ms(123456789); // 123456789 ms = 3.9 years // check updated values - assert_eq(record::nft_id(&record), nft_id); - assert_eq(*record::data(&record), data); - assert_eq(record::target_address(&record), some(@suins)); - assert_eq(record::expiration_timestamp_ms(&record), 123456789); + assert_eq(record.nft_id(), nft_id); + assert_eq(*record.data(), data); + assert_eq(record.target_address(), some(@suins)); + assert_eq(record.expiration_timestamp_ms(), 123456789); } #[test] @@ -58,21 +52,21 @@ module suins::name_record_tests { let mut clock = clock::create_for_testing(&mut ctx); // clock is 0, record expires in 1 second with a 30 days (grace period) - assert_eq(record::has_expired(&record, &clock), false); - assert_eq(record::has_expired_past_grace_period(&record, &clock), false); + assert_eq(record.has_expired(&clock), false); + assert_eq(record.has_expired_past_grace_period(&clock), false); // increment time by 30 days to check if the grace period is working; // in just 1 second from that the record will expire - clock::increment_for_testing(&mut clock, constants::grace_period_ms()); - assert_eq(record::has_expired(&record, &clock), true); - assert_eq(record::has_expired_past_grace_period(&record, &clock), false); + clock.increment_for_testing(constants::grace_period_ms()); + assert_eq(record.has_expired(&clock), true); + assert_eq(record.has_expired_past_grace_period(&clock), false); // increment time by 1 second to check if record has expired - clock::increment_for_testing(&mut clock, constants::grace_period_ms() + 1000); - assert_eq(record::has_expired(&record, &clock), true); - assert_eq(record::has_expired_past_grace_period(&record, &clock), true); + clock.increment_for_testing(constants::grace_period_ms() + 1000); + assert_eq(record.has_expired(&clock), true); + assert_eq(record.has_expired_past_grace_period(&clock), true); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); } fun fresh_id(ctx: &mut TxContext): ID { diff --git a/packages/suins/tests/unit/registration_nft_tests.move b/packages/suins/tests/unit/registration_nft_tests.move index 8f98730a..6cd29789 100644 --- a/packages/suins/tests/unit/registration_nft_tests.move +++ b/packages/suins/tests/unit/registration_nft_tests.move @@ -10,13 +10,9 @@ /// module suins::registation_nft_tests { use std::string::{utf8, String}; - use sui::tx_context::{Self, TxContext}; - use sui::clock::{Self, Clock}; - use sui::test_utils::assert_eq; + use sui::{clock::{Self, Clock}, test_utils::assert_eq}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::constants; - use suins::domain; + use suins::{suins_registration::{Self as nft, SuinsRegistration}, constants, domain}; #[test] fun test_new() { @@ -25,27 +21,27 @@ module suins::registation_nft_tests { let nft = new(utf8(b"test.sui"), 1, &clock, &mut ctx); // expiration date for 1 year should be 365 days from now - assert_eq(nft::expiration_timestamp_ms(&nft), 365 * 24 * 60 * 60 * 1000); - assert_eq(nft::image_url(&nft), constants::default_image()); - assert_eq(nft::domain(&nft), domain::new(utf8(b"test.sui"))); + assert_eq(nft.expiration_timestamp_ms(), 365 * 24 * 60 * 60 * 1000); + assert_eq(nft.image_url(), constants::default_image()); + assert_eq(nft.domain(), domain::new(utf8(b"test.sui"))); // bump the clock value to 1 year from now // and create a new NFT with expiration in 2 years + 1 ms - clock::increment_for_testing(&mut clock, constants::year_ms() + 1); + clock.increment_for_testing(constants::year_ms() + 1); // test if the first NFT would have expired by then (but no grace period) - assert_eq(nft::has_expired(&nft, &clock), true); - assert_eq(nft::has_expired_past_grace_period(&nft, &clock), false); + assert_eq(nft.has_expired(&clock), true); + assert_eq(nft.has_expired_past_grace_period(&clock), false); burn(nft); // create a new NFT with expiration in 2 years let nft = new(utf8(b"test.sui"), 2, &clock, &mut ctx); // expiration timestamp for 2 years (1 off) should be 3 * 365 days (and 1 ms) from now - assert_eq(nft::expiration_timestamp_ms(&nft), 3 * constants::year_ms() + 1); + assert_eq(nft.expiration_timestamp_ms(), 3 * constants::year_ms() + 1); burn(nft); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); } #[test] @@ -54,13 +50,13 @@ module suins::registation_nft_tests { let clock = clock::create_for_testing(&mut ctx); let mut nft = new(utf8(b"test.sui"), 5, &clock, &mut ctx); - nft::set_expiration_timestamp_ms_for_testing(&mut nft, 0); - assert_eq(nft::expiration_timestamp_ms(&nft), 0); + nft.set_expiration_timestamp_ms_for_testing(0); + assert_eq(nft.expiration_timestamp_ms(), 0); - nft::update_image_url_for_testing(&mut nft, utf8(b"test_image_url")); - assert_eq(nft::image_url(&nft), utf8(b"test_image_url")); + nft.update_image_url_for_testing(utf8(b"test_image_url")); + assert_eq(nft.image_url(), utf8(b"test_image_url")); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); burn(nft); } @@ -81,6 +77,6 @@ module suins::registation_nft_tests { } fun burn(nft: SuinsRegistration) { - nft::burn_for_testing(nft) + nft.burn_for_testing() } } diff --git a/packages/suins/tests/unit/registry_tests.move b/packages/suins/tests/unit/registry_tests.move index 46e319e7..5eb7a335 100644 --- a/packages/suins/tests/unit/registry_tests.move +++ b/packages/suins/tests/unit/registry_tests.move @@ -3,19 +3,15 @@ #[test_only] module suins::registry_tests { - use std::string::utf8; - use std::option::{Self, some}; - use std::vector; - use sui::object; - use sui::tx_context::{Self, TxContext}; - use sui::clock::{Self, Clock}; - use sui::test_utils::assert_eq; - - use suins::suins_registration::{Self as nft, SuinsRegistration}; - use suins::name_record as record; - use suins::registry::{Self, Registry}; - use suins::domain::{Self, Domain}; - use suins::constants; + use std::{string::utf8, option::{some}}; + use sui::{clock::{Self, Clock}, test_utils::assert_eq}; + + use suins::{ + suins_registration::{Self as nft, SuinsRegistration}, + name_record as record, + registry::{Self, Registry}, + domain::{Self, Domain}, constants + }; // === Registry + Record Addition === @@ -25,15 +21,15 @@ module suins::registry_tests { let (mut registry, clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // make sure that the nft matches the domain - assert_eq(nft::domain(&nft), domain); - assert_eq(registry::has_record(®istry, nft::domain(&nft)), true); + assert_eq(nft.domain(), domain); + assert_eq(registry.has_record(nft.domain()), true); // take the record and compare it against the nft - let record = registry::remove_record_for_testing(&mut registry, domain); - assert_eq(record::expiration_timestamp_ms(&record), nft::expiration_timestamp_ms(&nft)); + let record = registry.remove_record_for_testing(domain); + assert_eq(record.expiration_timestamp_ms(), nft.expiration_timestamp_ms()); burn_nfts(vector[ nft ]); @@ -52,24 +48,24 @@ module suins::registry_tests { let subdomain_one = domain::new(utf8(b"test.hahaha.sui")); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // register a leaf record and set the target to @0x0 - registry::add_leaf_record(&mut registry, subdomain_one, &clock, @0x0, &mut ctx); + registry.add_leaf_record(subdomain_one, &clock, @0x0, &mut ctx); // set the reverse_Registry of @0x0 to be that leaf subdomain - registry::set_reverse_lookup(&mut registry, @0x0, subdomain_one); + registry.set_reverse_lookup(@0x0, subdomain_one); - let name_record = option::extract(&mut registry::lookup(®istry, subdomain_one)); + let name_record = option::extract(&mut registry.lookup(subdomain_one)); // validate that the parent nft_id is the same as the leaf's one. - assert_eq(object::id(&nft), record::nft_id(&name_record)); + assert_eq(object::id(&nft), name_record.nft_id()); // Reverse lookup should work as expected, since it's set. - let name = option::extract(&mut registry::reverse_lookup(®istry, @0x0)); + let name = option::extract(&mut registry.reverse_lookup(@0x0)); assert!(name == subdomain_one, 0); // remove leaf_record to test removal too - registry::remove_leaf_record(&mut registry, subdomain_one); + registry.remove_leaf_record(subdomain_one); // validate that now @0x0 doesn't have a reverse lookup anymore. let res = registry::reverse_lookup(®istry, @0x0); @@ -86,21 +82,21 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, mut clock, _domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); + let nft = registry.add_record(domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); // add 2 leaf records as nft - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test.test.sui")), &clock, @0x0, &mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test2.test.sui")), &clock, @0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test.test.sui")), &clock, @0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test2.test.sui")), &clock, @0x0, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + constants::grace_period_ms() + 1); // become a new owner, `new_oner_nft` - let new_owner_nft = registry::add_record(&mut registry, domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); + let new_owner_nft = registry.add_record(domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); // override both leaf records, one with a node subdomain, the other iwth a leaf subdomain - let normal_subdomain_override = registry::add_record_ignoring_grace_period(&mut registry, domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test2.test.sui")), &clock, @0x1, &mut ctx); + let normal_subdomain_override = registry.add_record_ignoring_grace_period(domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test2.test.sui")), &clock, @0x1, &mut ctx); burn_nfts(vector[ nft, new_owner_nft, normal_subdomain_override ]); wrapup_non_empty(registry, clock); @@ -115,14 +111,14 @@ module suins::registry_tests { let (mut registry, mut clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + constants::grace_period_ms() + 1); // override the record - let nft_2 = registry::add_record(&mut registry, domain, 2, &clock, &mut ctx); - let record = registry::remove_record_for_testing(&mut registry, domain); + let nft_2 = registry.add_record(domain, 2, &clock, &mut ctx); + let record = registry.remove_record_for_testing(domain); // make sure the old NFT is no longer matches to the domain assert!(object::id(&nft) != record::nft_id(&record), 0); @@ -143,14 +139,14 @@ module suins::registry_tests { let (mut registry, mut clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + 1); // override the record - let nft_2 = registry::add_record_ignoring_grace_period(&mut registry, domain, 2, &clock, &mut ctx); - let record = registry::remove_record_for_testing(&mut registry, domain); + let nft_2 = registry.add_record_ignoring_grace_period(domain, 2, &clock, &mut ctx); + let record = registry.remove_record_for_testing(domain); // make sure the old NFT is no longer matches to the domain assert!(object::id(&nft) != record::nft_id(&record), 0); @@ -171,10 +167,10 @@ module suins::registry_tests { let (mut registry, clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); // try to override the record and fail - not expired - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); abort 1337 } @@ -186,11 +182,11 @@ module suins::registry_tests { let (mut registry, mut clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + 1); // try to override the record and fail - not expired - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); abort 1337 } @@ -205,22 +201,22 @@ module suins::registry_tests { let (mut registry, mut clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + constants::grace_period_ms() + 1); // we re-register the same domain now that the other has expired. - let new_nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let new_nft = registry.add_record(domain, 1, &clock, &mut ctx); // we burn the first one as it is an expired name now. - registry::burn_registration_object(&mut registry, nft, &clock); + registry.burn_registration_object(nft, &clock); // we still have a registry entry though, it's not removed as the owner is different. - assert!(option::is_some(®istry::lookup(®istry, domain)), 1); + assert!(option::is_some(®istry.lookup(domain)), 1); // remove the record so we can wrap this up. - registry::remove_record_for_testing(&mut registry, domain); + registry.remove_record_for_testing(domain); wrapup(registry, clock); burn_nfts(vector[new_nft]); @@ -234,16 +230,16 @@ module suins::registry_tests { let (mut registry, mut clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + constants::grace_period_ms() + 1); // we burn the first one as it is an expired name now. - registry::burn_registration_object(&mut registry, nft, &clock); + registry.burn_registration_object(nft, &clock); // we still have a registry entry though, it's not removed as the owner is different. - assert!(option::is_none(®istry::lookup(®istry, domain)), 1); + assert!(option::is_none(®istry.lookup(domain)), 1); wrapup(registry, clock); } @@ -259,12 +255,12 @@ module suins::registry_tests { let (mut registry, clock, domain) = setup(&mut ctx); // create a record for the test domain with expiration set to 1 year - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); - registry::set_target_address(&mut registry, domain, some(@0x2)); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); + registry.set_target_address(domain, some(@0x2)); // try to find a record and then get a record - let mut search = registry::lookup(®istry, domain); - let record = registry::remove_record_for_testing(&mut registry, domain); + let mut search = registry.lookup(domain); + let record = registry.remove_record_for_testing(domain); // make sure the search is a success assert!(option::is_some(&search), 0); @@ -284,11 +280,11 @@ module suins::registry_tests { fun set_reverse_lookup() { let mut ctx = tx_context::dummy(); let (mut registry, clock, domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // set the `domain` points to @0x0; set the reverse lookup too - registry::set_target_address(&mut registry, domain, some(@0xB0B)); - registry::set_reverse_lookup(&mut registry, @0xB0B, domain); + registry.set_target_address(domain, some(@0xB0B)); + registry.set_reverse_lookup(@0xB0B, domain); // search for the reverse_lookup record let mut search = registry::reverse_lookup(®istry, @0xB0B); @@ -297,8 +293,8 @@ module suins::registry_tests { assert!(option::extract(&mut search) == domain, 0); // wrapup - registry::unset_reverse_lookup(&mut registry, @0xB0B); - let _ = registry::remove_record_for_testing(&mut registry, domain); + registry.unset_reverse_lookup(@0xB0B); + let _ = registry.remove_record_for_testing(domain); wrapup(registry, clock); burn_nfts(vector[ nft ]); @@ -309,13 +305,13 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, mut clock, _domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain::new(utf8(b"node.test.sui")), 1, &clock, &mut ctx); + let nft = registry.add_record(domain::new(utf8(b"node.test.sui")), 1, &clock, &mut ctx); - let subdomain = registry::wrap_subdomain(&mut registry, nft, &clock, &mut ctx); + let subdomain = registry.wrap_subdomain(nft, &clock, &mut ctx); clock::increment_for_testing(&mut clock, constants::year_ms() + 1); - registry::burn_subdomain_object(&mut registry, subdomain, &clock); + registry.burn_subdomain_object(subdomain, &clock); wrapup(registry, clock); } @@ -324,14 +320,14 @@ module suins::registry_tests { fun burn_expired_suins_registration() { let mut ctx = tx_context::dummy(); let (mut registry, mut clock, domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + 1); // burn the registration object - registry::burn_registration_object(&mut registry, nft, &clock); + registry.burn_registration_object(nft, &clock); - let name = registry::lookup(®istry, domain); + let name = registry.lookup(domain); assert!(option::is_none(&name), 0); wrapup(registry, clock); @@ -341,19 +337,19 @@ module suins::registry_tests { fun burn_expired_registration_without_overriding() { let mut ctx = tx_context::dummy(); let (mut registry, mut clock, domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // increment the clock to 1 years + grace period clock::increment_for_testing(&mut clock, constants::year_ms() + constants::grace_period_ms() + 1); // re-register - let new_nft_post_expiration = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let new_nft_post_expiration = registry.add_record(domain, 1, &clock, &mut ctx); // burn the expired object - registry::burn_registration_object(&mut registry, nft, &clock); + registry.burn_registration_object(nft, &clock); // Validate that the record still exists (no invalidation happened), // since the name was bought again after this. - let name = registry::lookup(®istry, domain); + let name = registry.lookup(domain); assert!(option::is_some(&name), 0); wrapup_non_empty(registry, clock); @@ -366,10 +362,10 @@ module suins::registry_tests { fun set_reverse_lookup_fail_target_not_set() { let mut ctx = tx_context::dummy(); let (mut registry, clock, domain) = setup(&mut ctx); - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); // set the `domain` points to @0x0 - registry::set_reverse_lookup(&mut registry, @0x0, domain); + registry.set_reverse_lookup(@0x0, domain); abort 1337 } @@ -381,11 +377,11 @@ module suins::registry_tests { fun set_reverse_lookup_fail_record_mismatch() { let mut ctx = tx_context::dummy(); let (mut registry, clock, domain) = setup(&mut ctx); - let _nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let _nft = registry.add_record(domain, 1, &clock, &mut ctx); // set the `domain` points to @0x0 - registry::set_target_address(&mut registry, domain, some(@0xB0B)); - registry::set_reverse_lookup(&mut registry, @0xA11CE, domain); + registry.set_target_address(domain, some(@0xB0B)); + registry.set_reverse_lookup(@0xA11CE, domain); abort 1337 } @@ -396,7 +392,7 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, clock, _domain) = setup(&mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test.sui")), &clock,@0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test.sui")), &clock,@0x0, &mut ctx); abort 1337 } @@ -407,7 +403,7 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, clock, _domain) = setup(&mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); abort 1337 } @@ -418,9 +414,9 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, clock, _domain) = setup(&mut ctx); - let _nft = registry::add_record(&mut registry, domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); + let _nft = registry.add_record(domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); - registry::remove_leaf_record(&mut registry, domain::new(utf8(b"test.test.sui"))); + registry.remove_leaf_record(domain::new(utf8(b"test.test.sui"))); abort 1337 } @@ -431,10 +427,10 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, clock, _domain) = setup(&mut ctx); - let _nft = registry::add_record(&mut registry, domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); - let _existing = registry::add_record(&mut registry, domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); + let _nft = registry.add_record(domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); + let _existing = registry.add_record(domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); abort 1337 } @@ -445,11 +441,11 @@ module suins::registry_tests { let mut ctx = tx_context::dummy(); let (mut registry, clock, _domain) = setup(&mut ctx); - let _nft = registry::add_record(&mut registry, domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); + let _nft = registry.add_record(domain::new(utf8(b"test.sui")), 1, &clock, &mut ctx); - registry::add_leaf_record(&mut registry, domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); + registry.add_leaf_record(domain::new(utf8(b"test.test.sui")), &clock,@0x0, &mut ctx); - let _existing = registry::add_record_ignoring_grace_period(&mut registry, domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); + let _existing = registry.add_record_ignoring_grace_period(domain::new(utf8(b"test.test.sui")), 1, &clock, &mut ctx); abort 1337 @@ -459,10 +455,10 @@ module suins::registry_tests { fun burn_non_expired_domain_failure() { let mut ctx = tx_context::dummy(); let (mut registry, clock, domain) = setup(&mut ctx); - let nft = registry::add_record(&mut registry, domain, 1, &clock, &mut ctx); + let nft = registry.add_record(domain, 1, &clock, &mut ctx); // burn the expired object - registry::burn_registration_object(&mut registry, nft, &clock); + registry.burn_registration_object(nft, &clock); abort 1337 } @@ -482,12 +478,12 @@ module suins::registry_tests { fun wrapup(registry: Registry, clock: Clock) { registry::destroy_empty_for_testing(registry); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); } fun wrapup_non_empty(registry: Registry, clock: Clock) { registry::destroy_for_testing(registry); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); } #[test_only] diff --git a/packages/suins/tests/unit/sub_name_tests.move b/packages/suins/tests/unit/sub_name_tests.move index b3ba253b..30533acd 100644 --- a/packages/suins/tests/unit/sub_name_tests.move +++ b/packages/suins/tests/unit/sub_name_tests.move @@ -5,12 +5,9 @@ module suins::sub_name_tests { use std::string::utf8; - use sui::tx_context; use sui::clock; - use suins::suins_registration; - use suins::subdomain_registration as subdomain; - use suins::domain; + use suins::{suins_registration, subdomain_registration as subdomain, domain}; #[test] fun test_wrap_and_destroy(){ @@ -24,16 +21,16 @@ module suins::sub_name_tests { // create subdomain from name let mut sub_nft = subdomain::new(nft, &clock, &mut ctx); - assert!(suins_registration::domain(subdomain::nft(&sub_nft)) == domain, 1); + assert!(sub_nft.nft().domain() == domain, 1); // destroy subdomain (added mut borrow for coverage) - clock::set_for_testing(&mut clock, suins_registration::expiration_timestamp_ms(subdomain::nft_mut(&mut sub_nft)) + 1); + clock.set_for_testing(sub_nft.nft_mut().expiration_timestamp_ms() + 1); - nft = subdomain::burn(sub_nft, &clock); + nft = sub_nft.burn(&clock); - suins_registration::burn_for_testing(nft); + nft.burn_for_testing(); - clock::destroy_for_testing(clock); + clock.destroy_for_testing(); } #[test, expected_failure(abort_code=suins::subdomain_registration::ENotSubdomain)] @@ -55,7 +52,7 @@ module suins::sub_name_tests { let mut clock = clock::create_for_testing(&mut ctx); let nft = suins_registration::new_for_testing(domain::new(utf8(b"sub.example.sui")), 1, &clock, &mut ctx); - clock::set_for_testing(&mut clock, suins_registration::expiration_timestamp_ms(&nft) + 1); + clock.set_for_testing(nft.expiration_timestamp_ms() + 1); // create subdomain from name let _sub_nft = subdomain::new(nft, &clock, &mut ctx); @@ -74,7 +71,7 @@ module suins::sub_name_tests { let sub_nft = subdomain::new(nft, &clock, &mut ctx); // try to destroy - let _nft = subdomain::burn(sub_nft, &clock); + let _nft = sub_nft.burn(&clock); abort 1337 } diff --git a/packages/suins/tests/unit/suins_tests.move b/packages/suins/tests/unit/suins_tests.move index 488af7bc..86173771 100644 --- a/packages/suins/tests/unit/suins_tests.move +++ b/packages/suins/tests/unit/suins_tests.move @@ -4,11 +4,7 @@ #[test_only] /// module suins::suins_tests { - use sui::coin; - use sui::balance; - use sui::sui::SUI; - use sui::tx_context; - use sui::test_utils::assert_eq; + use sui::{coin, balance, sui::SUI, test_utils::assert_eq}; use suins::suins::{Self, AdminCap, SuiNS}; // === Config management === @@ -22,7 +18,7 @@ module suins::suins_tests { let (mut suins, cap) = suins::new_for_testing(&mut ctx); suins::add_config(&cap, &mut suins, TestConfig { a: 1 }); - let _cfg = suins::get_config(&suins); + let _cfg = suins.get_config(); let cfg = suins::remove_config(&cap, &mut suins); assert_eq(cfg.a, 1); @@ -40,7 +36,7 @@ module suins::suins_tests { let (mut suins, cap) = suins::new_for_testing(&mut ctx); suins::add_registry(&cap, &mut suins, TestRegistry { counter: 1 }); - let reg = suins::registry(&suins); + let reg = suins.registry(); assert_eq(reg.counter, 1); wrapup(suins, cap); @@ -78,8 +74,8 @@ module suins::suins_tests { // authorize and check right away suins::authorize_app(&cap, &mut suins); - assert!(suins::is_app_authorized(&suins), 0); - suins::assert_app_is_authorized(&suins); + assert!(suins.is_app_authorized(), 0); + suins.assert_app_is_authorized(); // add balance and read registry suins::app_add_balance(TestApp {}, &mut suins, balance::zero()); @@ -87,11 +83,11 @@ module suins::suins_tests { registry.counter = 2; // now read the registry again - assert_eq(suins::registry(&suins).counter, 2); + assert_eq(suins.registry().counter, 2); // deauthorize application suins::deauthorize_app(&cap, &mut suins); - assert!(!suins::is_app_authorized(&suins), 0); + assert!(!suins.is_app_authorized(), 0); wrapup(suins, cap); } @@ -128,7 +124,7 @@ module suins::suins_tests { // for a softer and simpler wrapup we can just share the object fun wrapup(suins: SuiNS, cap: AdminCap) { - suins::share_for_testing(suins); + suins.share_for_testing(); suins::burn_admin_cap_for_testing(cap); } } diff --git a/packages/suins/tests/v2/register.move b/packages/suins/tests/v2/register.move index f53a19e6..2886e0ee 100644 --- a/packages/suins/tests/v2/register.move +++ b/packages/suins/tests/v2/register.move @@ -4,16 +4,9 @@ #[test_only] module suins::register_sample { use std::string::{Self, String}; - use sui::coin::{Self, Coin}; - use sui::tx_context::TxContext; - use sui::clock::Clock; - use sui::sui::SUI; - - use suins::domain; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::config::{Self, Config}; - use suins::suins_registration::SuinsRegistration; + use sui::{coin::{Self, Coin}, clock::Clock, sui::SUI}; + + use suins::{domain, registry::{Self, Registry}, suins::{Self, SuiNS}, config::{Self, Config}, suins_registration::SuinsRegistration}; /// Number of years passed is not within [1-5] interval. const EInvalidYearsArgument: u64 = 0; diff --git a/packages/suins/tests/v2/renew.move b/packages/suins/tests/v2/renew.move index 5c5d76af..275ef961 100644 --- a/packages/suins/tests/v2/renew.move +++ b/packages/suins/tests/v2/renew.move @@ -3,20 +3,18 @@ #[test_only] module suins::renew { - use std::option; use std::string; - use sui::coin::{Self, Coin}; - use sui::clock::{timestamp_ms, Clock}; - use sui::sui::SUI; - use sui::object; + use sui::{coin::{Self, Coin}, clock::{timestamp_ms, Clock}, sui::SUI}; - use suins::domain; - use suins::constants; - use suins::name_record; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::config::{Self, Config}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; + use suins::{ + domain, + constants, + name_record, + registry::{Self, Registry}, + suins::{Self, SuiNS}, + config::{Self, Config}, + suins_registration::{Self as nft, SuinsRegistration} + }; /// Number of years passed is not within [1-5] interval. const EInvalidYearsArgument: u64 = 0; diff --git a/packages/temp_subdomain_proxy/sources/subdomain_proxy.move b/packages/temp_subdomain_proxy/sources/subdomain_proxy.move index 1a5dbc4a..e00db9cd 100644 --- a/packages/temp_subdomain_proxy/sources/subdomain_proxy.move +++ b/packages/temp_subdomain_proxy/sources/subdomain_proxy.move @@ -11,11 +11,9 @@ module temp_subdomain_proxy::subdomain_proxy { use std::string::String; - use sui::tx_context::TxContext; use sui::clock::Clock; - use suins::suins::SuiNS; - use suins::subdomain_registration::{Self, SubDomainRegistration}; + use suins::{suins::SuiNS, subdomain_registration::SubDomainRegistration}; use subdomains::subdomains; use utils::direct_setup; @@ -32,7 +30,7 @@ module temp_subdomain_proxy::subdomain_proxy { ): SubDomainRegistration { subdomains::new( suins, - subdomain_registration::nft(subdomain), + subdomain.nft(), clock, subdomain_name, expiration_timestamp_ms, @@ -52,7 +50,7 @@ module temp_subdomain_proxy::subdomain_proxy { ){ subdomains::new_leaf( suins, - subdomain_registration::nft(subdomain), + subdomain.nft(), clock, subdomain_name, target, @@ -68,7 +66,7 @@ module temp_subdomain_proxy::subdomain_proxy { ) { subdomains::remove_leaf( suins, - subdomain_registration::nft(subdomain), + subdomain.nft(), clock, subdomain_name, ); @@ -82,7 +80,7 @@ module temp_subdomain_proxy::subdomain_proxy { ) { direct_setup::set_target_address( suins, - subdomain_registration::nft(subdomain), + subdomain.nft(), new_target, clock, ); diff --git a/packages/utils/sources/direct_setup.move b/packages/utils/sources/direct_setup.move index f589f222..7decf7d8 100644 --- a/packages/utils/sources/direct_setup.move +++ b/packages/utils/sources/direct_setup.move @@ -4,16 +4,11 @@ /// A simple package to allows us set a target address & default name in a single PTB in frontend. /// Unblocks better UX in the registration flow | easier adoption for non-technical users. module utils::direct_setup { - use std::option; use std::string::{String}; - use sui::tx_context::{TxContext, sender}; - use sui::clock::Clock; + use sui::{tx_context::{sender}, clock::Clock}; - use suins::domain; - use suins::registry::{Self, Registry}; - use suins::suins::{Self, SuiNS}; - use suins::suins_registration::{Self as nft, SuinsRegistration}; + use suins::{domain, registry::Registry, suins::{Self, SuiNS}, suins_registration::SuinsRegistration}; /// Authorization token for the controller. public struct DirectSetup has drop {} @@ -26,16 +21,16 @@ module utils::direct_setup { clock: &Clock, ) { let registry = suins::app_registry_mut(DirectSetup {}, suins); - registry::assert_nft_is_authorized(registry, nft, clock); + registry.assert_nft_is_authorized(nft, clock); - let domain = nft::domain(nft); - registry::set_target_address(registry, domain, option::some(new_target)); + let domain = nft.domain(); + registry.set_target_address(domain, option::some(new_target)); } /// Set the reverse lookup address for the domain public fun set_reverse_lookup(suins: &mut SuiNS, domain_name: String, ctx: &TxContext) { let domain = domain::new(domain_name); let registry = suins::app_registry_mut(DirectSetup {}, suins); - registry::set_reverse_lookup(registry, sender(ctx), domain); + registry.set_reverse_lookup(sender(ctx), domain); } }