From 34c0eb56120d7cd4d6bc80eb722afceca475ae41 Mon Sep 17 00:00:00 2001 From: PoulavBhowmick03 Date: Wed, 30 Oct 2024 00:04:29 +0530 Subject: [PATCH] changes --- .../cairo/src/tests/nameservice_tests.cairo | 242 +++++++----------- 1 file changed, 94 insertions(+), 148 deletions(-) diff --git a/onchain/cairo/src/tests/nameservice_tests.cairo b/onchain/cairo/src/tests/nameservice_tests.cairo index fe381758..b08bdded 100644 --- a/onchain/cairo/src/tests/nameservice_tests.cairo +++ b/onchain/cairo/src/tests/nameservice_tests.cairo @@ -15,46 +15,50 @@ mod nameservice_tests { }; fn ADMIN() -> ContractAddress { - starknet::contract_address_const::<1>() // Using 1 instead of 123 + starknet::contract_address_const::<1>() } fn CALLER() -> ContractAddress { - starknet::contract_address_const::<2>() // Using 2 instead of 5 + starknet::contract_address_const::<2>() } const ADMIN_ROLE: felt252 = selector!("ADMIN_ROLE"); const YEAR_IN_SECONDS: u64 = 31536000_u64; + const PAYMENT_AMOUNT: felt252 = 10; fn setup() -> (INameserviceDispatcher, IERC20Dispatcher, IERC20MintableDispatcher) { let erc20_mintable_class = declare("ERC20Mintable").unwrap().contract_class(); + let (payment_token_dispatcher, payment_token_mintable_dispatcher) = deploy_erc20_mint( - *erc20_mintable_class, "PaymentToken", "PAY", ADMIN(), 1_000_000_u256, + *erc20_mintable_class, + selector!("PaymentToken"), + selector!("PAY"), + ADMIN(), + 50_u256, ); let nameservice_class = declare("Nameservice").unwrap().contract_class(); let mut calldata = array![]; - ADMIN().serialize(ref calldata); // owner - ADMIN().serialize(ref calldata); // admin + ADMIN().serialize(ref calldata); + ADMIN().serialize(ref calldata); let (nameservice_address, _) = nameservice_class.deploy(@calldata).unwrap(); let nameservice_dispatcher = INameserviceDispatcher { contract_address: nameservice_address }; - // Set the token_quote in the nameservice contract start_cheat_caller_address(nameservice_dispatcher.contract_address, ADMIN()); nameservice_dispatcher.set_token_quote(payment_token_dispatcher.contract_address); - nameservice_dispatcher.update_subscription_price(100_u256); + nameservice_dispatcher.update_subscription_price(10_u256); // Reduced price stop_cheat_caller_address(nameservice_dispatcher.contract_address); (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) } - fn deploy_erc20_mint( class: ContractClass, - name: ByteArray, - symbol: ByteArray, + name: felt252, + symbol: felt252, owner: ContractAddress, initial_supply: u256, ) -> (IERC20Dispatcher, IERC20MintableDispatcher) { @@ -72,195 +76,137 @@ mod nameservice_tests { (erc20_dispatcher, erc20_mintable_dispatcher) } - + #[test] fn test_claim_username() { let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); - // Mint tokens to CALLER start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); - payment_token_mintable_dispatcher.mint(CALLER(), 1000_u256); + payment_token_mintable_dispatcher.mint(CALLER(), 20_u256); // Reduced amount stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address); - - // Approve Nameservice contract to spend CALLER's tokens + start_cheat_caller_address(payment_token_dispatcher.contract_address, CALLER()); - payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 1000_u256); + payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 20_u256); stop_cheat_caller_address(payment_token_dispatcher.contract_address); - // CALLER claims a username - let username = "test".try_into().unwrap(); + 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); - // Verify username mapping let stored_username = nameservice_dispatcher.get_username(CALLER()); assert(stored_username == username, 'Username was not set correctly'); let stored_address = nameservice_dispatcher.get_username_address(username); assert(stored_address == CALLER(), 'Address was not set correctly'); - // Verify subscription expiry let expiry = nameservice_dispatcher.get_subscription_expiry(CALLER()); let current_time = get_block_timestamp(); assert(expiry > current_time, 'Subscription expiry not set correctly'); - // Verify token balances let caller_balance = payment_token_dispatcher.balance_of(CALLER()); - assert(caller_balance == 900_u256, 'Payment token balance incorrect'); + assert(caller_balance == 10_u256, 'Payment token balance incorrect'); let contract_balance = payment_token_dispatcher.balance_of(nameservice_dispatcher.contract_address); - assert(contract_balance == 100_u256, 'Contract token balance incorrect'); + assert(contract_balance == 10_u256, 'Contract token balance incorrect'); } - - // #[test] - // fn test_change_username() { - // let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); - - // // Mint and approve tokens - // start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); - // payment_token_mintable_dispatcher.mint(CALLER(), 1000_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, 1000_u256); - // stop_cheat_caller_address(payment_token_dispatcher.contract_address); - - // // Claim initial username - // let username = 'test'.try_into().unwrap(); - - // start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); - // nameservice_dispatcher.claim_username(username); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); - - // // Change username - // let new_username = 'new'.try_into().unwrap(); - - // start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); - // nameservice_dispatcher.change_username(new_username); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); - - // // Verify changes - // let stored_username = nameservice_dispatcher.get_username(CALLER()); - // assert(stored_username == new_username, 'Username was not changed correctly'); - - // let old_username_address = nameservice_dispatcher.get_username_address(username); - // assert(old_username_address == starknet::contract_address_const::<0>(), 'Old username still mapped'); - - // let new_username_address = nameservice_dispatcher.get_username_address(new_username); - // assert(new_username_address == CALLER(), 'New username not mapped correctly'); - // } - - // #[test] - // fn test_renew_subscription() { - // let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); - - // // Mint and approve tokens - // start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); - // payment_token_mintable_dispatcher.mint(CALLER(), 1000_u256); - // stop_cheat_caller_address(payment_token_mintable_dispatcher.contract_address); + #[test] + fn test_change_username() { + let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); - // start_cheat_caller_address(payment_token_dispatcher.contract_address, CALLER()); - // payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 1000_u256); - // stop_cheat_caller_address(payment_token_dispatcher.contract_address); + start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); + 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); - // // Claim username - // let username = 'test'.try_into().unwrap(); + 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, CALLER()); + nameservice_dispatcher.claim_username(username); + stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // // Get current expiry - // let current_expiry = nameservice_dispatcher.get_subscription_expiry(CALLER()); + let new_username = selector!("new"); - // // Advance time by half a year - // let half_year = 15768000_u64; - // let new_timestamp = get_block_timestamp() + half_year; - // start_warp(nameservice_dispatcher.contract_address, new_timestamp); + start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); + nameservice_dispatcher.change_username(new_username); + stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // // Renew subscription - // start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); - // nameservice_dispatcher.renew_subscription(); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); + let stored_username = nameservice_dispatcher.get_username(CALLER()); + assert(stored_username == new_username, 'Username was not changed correctly'); - // // Verify new expiry - // let new_expiry = nameservice_dispatcher.get_subscription_expiry(CALLER()); - // assert(new_expiry == current_expiry + YEAR_IN_SECONDS, 'Subscription not renewed correctly'); + let old_username_address = nameservice_dispatcher.get_username_address(username); + assert(old_username_address == starknet::contract_address_const::<0>(), 'Old username still mapped'); - // // Verify token balance - // let caller_balance = payment_token_dispatcher.balance_of(CALLER()); - // assert(caller_balance == 800_u256, 'Payment token balance incorrect after renewal'); - // } + let new_username_address = nameservice_dispatcher.get_username_address(new_username); + assert(new_username_address == CALLER(), 'New username not mapped correctly'); + } - // #[test] - // fn test_withdraw_fees() { - // let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); + #[test] + fn test_renew_subscription() { + let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); - // // Mint and approve tokens - // start_cheat_caller_address(payment_token_dispatcher.contract_address, ADMIN()); - // payment_token_dispatcher.mint(CALLER(), 1000_u256); - // stop_cheat_caller_address(payment_token_dispatcher.contract_address); + start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); + 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, 1000_u256); - // stop_cheat_caller_address(payment_token_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); - // // Claim username - // let username = 'test'.try_into().unwrap(); + 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, CALLER()); + nameservice_dispatcher.claim_username(username); + stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // // Withdraw fees - // start_cheat_caller_address(nameservice_dispatcher.contract_address, ADMIN()); - // nameservice_dispatcher.withdraw_fees(100_u256); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); + let current_expiry = nameservice_dispatcher.get_subscription_expiry(CALLER()); - // // Verify ADMIN balance - // let admin_balance = payment_token_dispatcher.balance_of(ADMIN()); - // assert(admin_balance == 1_000_000_u256 + 100_u256, 'Admin did not receive fees'); + let half_year = 15768000_u64; + let new_timestamp = get_block_timestamp() + half_year; + start_warp(nameservice_dispatcher.contract_address, new_timestamp); - // // Verify contract balance - // let contract_balance = payment_token_dispatcher.balance_of(nameservice_dispatcher.contract_address); - // assert(contract_balance == 0_u256, 'Contract balance not zero after withdrawal'); - // } + start_cheat_caller_address(nameservice_dispatcher.contract_address, CALLER()); + nameservice_dispatcher.renew_subscription(); + stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // #[test] - // #[should_panic(expected: ('Username already claimed',))] - // fn test_claim_username_already_claimed() { - // let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); + let new_expiry = nameservice_dispatcher.get_subscription_expiry(CALLER()); + assert(new_expiry == current_expiry + YEAR_IN_SECONDS, 'Subscription not renewed correctly'); - // let other_user: ContractAddress = 6.try_into().unwrap(); + let caller_balance = payment_token_dispatcher.balance_of(CALLER()); + assert(caller_balance == 0_u256, 'Payment token balance incorrect after renewal'); + } - // // Mint and approve tokens for CALLER and other_user - // start_cheat_caller_address(payment_token_dispatcher.contract_address, ADMIN()); - // payment_token_dispatcher.mint(CALLER(), 1000_u256); - // payment_token_dispatcher.mint(other_user, 1000_u256); - // stop_cheat_caller_address(payment_token_dispatcher.contract_address); + #[test] + fn test_withdraw_fees() { + let (nameservice_dispatcher, payment_token_dispatcher, payment_token_mintable_dispatcher) = setup(); + + start_cheat_caller_address(payment_token_mintable_dispatcher.contract_address, ADMIN()); + 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, 1000_u256); - // stop_cheat_caller_address(payment_token_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(payment_token_dispatcher.contract_address, other_user); - // payment_token_dispatcher.approve(nameservice_dispatcher.contract_address, 1000_u256); - // stop_cheat_caller_address(payment_token_dispatcher.contract_address); + let username = selector!("test"); - // // CALLER claims a username - // let username = 'test'.try_into().unwrap(); + 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, CALLER()); - // nameservice_dispatcher.claim_username(username); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); + start_cheat_caller_address(nameservice_dispatcher.contract_address, ADMIN()); + nameservice_dispatcher.withdraw_fees(10_u256); + stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // // other_user tries to claim the same username - // start_cheat_caller_address(nameservice_dispatcher.contract_address, other_user); - // nameservice_dispatcher.claim_username(username); - // stop_cheat_caller_address(nameservice_dispatcher.contract_address); - // } + let admin_balance = payment_token_dispatcher.balance_of(ADMIN()); + assert(admin_balance == 60_u256, 'Admin did not receive fees'); // 50 initial + 10 withdrawn -} + let contract_balance = payment_token_dispatcher.balance_of(nameservice_dispatcher.contract_address); + assert(contract_balance == 0_u256, 'Contract balance not zero after withdrawal'); + } +} \ No newline at end of file