Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change_main_username function and test #292

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion onchain/cairo/.snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1 +1 @@
afk::tests::launchpad_tests::launchpad_tests::test_buy_coin_with_different_supply
afk::tests::launchpad_tests::launchpad_tests::test_buy_coin_with_different_supply
34 changes: 28 additions & 6 deletions onchain/cairo/src/afk_id/nameservice.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,15 @@ pub mod Nameservice {
self.usernames.read(key)
}

fn get_token_quote(self: @ContractState) -> ContractAddress {
self.token_quote.read()
}

fn get_is_payment_enabled(self: @ContractState) -> bool {
self.is_payment_enabled.read()
}

fn get_subscription_price(self: @ContractState) -> u256 {
self.subscription_price.read()

fn get_token_quote(self: @ContractState) -> ContractAddress {
self.token_quote.read()
}

// Users call
Expand Down Expand Up @@ -316,8 +315,31 @@ pub mod Nameservice {
);
}

// TODO change main username
fn change_main_username(ref self: ContractState, new_username: felt252) {}
fn change_main_username(ref self: ContractState, new_username: felt252) {
let caller_address = get_caller_address();

// Check for username belongs to user
let username_storage = self.username_storage.entry(new_username);
let owner = username_storage.owner.read();
assert(
owner == caller_address,
UserNameClaimErrors::USER_DOESNT_HAVE_USERNAME
);

let old_username = self.user_to_username.read(caller_address);
self.usernames.entry(new_username).write(caller_address);
self.user_to_username.entry(caller_address).write(new_username);

self
.emit(
UsernameChanged {
old_username: old_username,
new_username: new_username,
address: caller_address
}
);
}

// TODO
fn create_auction_for_username(
ref self: ContractState,
Expand Down
71 changes: 71 additions & 0 deletions onchain/cairo/src/tests/nameservice_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ mod nameservice_tests {
fn CALLER() -> ContractAddress {
starknet::contract_address_const::<2>()
}
fn NEW_CALLER() -> ContractAddress {
starknet::contract_address_const::<3>()
}
const ADMIN_ROLE: felt252 = selector!("ADMIN_ROLE");
const YEAR_IN_SECONDS: u64 = 31536000_u64;
const PAYMENT_AMOUNT: felt252 = 10;
Expand Down Expand Up @@ -251,4 +254,72 @@ mod nameservice_tests {
assert(subscription_price == 10_u256, 'Price is not correct');
assert(new_subscription_price == 30_u256, 'Price is not correct');
}

#[test]
#[should_panic(expected: 'Username already claimed')]
fn test_username_already_claimed() {
let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup();

let MINTER_ROLE: felt252 = selector!("MINTER_ROLE");

start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN());
payment_token_mintable_dispatcher.set_role(recipient: ADMIN(), role: MINTER_ROLE, is_enable: true);
payment_token_mintable_dispatcher.mint(CALLER(), 20_u256); // Reduced amount
stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, CALLER());
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, ADMIN());
nameservice_dispatcher.set_is_payment_enabled(true);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let username = selector!("test");
start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.claim_username(username);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

start_cheat_caller_address(nameservice_dispatcher.contract_address, NEW_CALLER());
nameservice_dispatcher.claim_username(username);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);
}

#[test]
fn test_change_main_username() {
let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) =
setup();
let MINTER_ROLE: felt252 = selector!("MINTER_ROLE");

start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN());
payment_token_mintable_dispatcher
.set_role(recipient: ADMIN(), role: MINTER_ROLE, is_enable: true);
payment_token_mintable_dispatcher.mint(CALLER(), 20_u256);
stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address);

start_cheat_caller_address(payment_token_dispatcher.contract_address, CALLER());
payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256);
stop_cheat_caller_address(payment_token_dispatcher.contract_address);

let username = selector!("first username");
let second_username = selector!("second username");
let third_username = selector!("third username");

start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.claim_username(username);
nameservice_dispatcher.claim_username(second_username);
nameservice_dispatcher.claim_username(third_username);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);


let stored_username = nameservice_dispatcher.get_username(CALLER());
assert(stored_username == third_username, 'Must be last claimed username');

start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER());
nameservice_dispatcher.change_main_username(second_username);
stop_cheat_caller_address(nameservice_dispatcher.contract_address);

let stored_username = nameservice_dispatcher.get_username(CALLER());
assert(stored_username == second_username, 'New username Incorrect');
}
}
Loading