From e8f07379f6fa2ef2f385e84fddf8bebf17667ee7 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Wed, 10 Apr 2024 19:27:35 +0100 Subject: [PATCH 1/6] username store contract --- onchain/src/username_store.cairo | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 onchain/src/username_store.cairo diff --git a/onchain/src/username_store.cairo b/onchain/src/username_store.cairo new file mode 100644 index 00000000..54475c68 --- /dev/null +++ b/onchain/src/username_store.cairo @@ -0,0 +1,89 @@ +use starknet::ContractAddress; + +pub mod UserNameClaimErrors { + pub const USERNAME_CLAIMED: felt252 = 'username_claimed'; + pub const USERNAME_CANNOT_BE_TRANSFER : felt252 = 'username_cannot_be_transferred'; +} + +#[starknet::interface] +pub trait IUsernameStore { + fn claimUsername(ref self: TContractState, key: felt252); + fn transferUsername(ref self: TContractState, key: felt252, new_Address: ContractAddress); +} + +#[starknet::contract] +pub mod usernameStore { + use starknet::{get_caller_address, ContractAddress}; + + use super::UserNameClaimErrors; + + #[storage] + struct Storage { + username: LegacyMap:: + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + UserNameClaimed: UserNameClaimed, + UserNameTransferred: UserNameTransferred + } + + #[derive(Drop, starknet::Event)] + struct UserNameClaimed { + #[key] + username: felt252, + address: ContractAddress + } + + #[derive(Drop, starknet::Event)] + struct UserNameTransferred { + #[key] + username: felt252, + address: ContractAddress + } + + #[abi(embed_v0)] + impl UsernameStore of super::IUsernameStore { + + fn claimUsername(ref self: ContractState, key: felt252) { + let mut username = self.username.read(key); + + if username == get_caller_address() { + core::panic_with_felt252(UserNameClaimErrors::USERNAME_CLAIMED); + } + + self.username.write(key, get_caller_address()); + + + self.emit( + Event::UserNameClaimed( + UserNameClaimed { + username: key, address: get_caller_address() + } + ) + ) + } + + fn transferUsername(ref self: ContractState, key: felt252, new_Address: ContractAddress) { + let usernameAddress = self.username.read(key); + + if usernameAddress != get_caller_address() { + core::panic_with_felt252(UserNameClaimErrors::USERNAME_CANNOT_BE_TRANSFER); + } + + self.username.write(key, new_Address); + + + self.emit( + Event::UserNameTransferred( + UserNameTransferred { + username: key, address: new_Address + } + ) + ) + + } + } +} + From 164b147bd27bae1e1eb2055032b94d21fec3031e Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Thu, 11 Apr 2024 17:50:17 +0100 Subject: [PATCH 2/6] write test for username store contract --- onchain/src/tests/username_store.cairo | 72 ++++++++++++++++++++++++++ onchain/src/username_store.cairo | 10 ++-- 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 onchain/src/tests/username_store.cairo diff --git a/onchain/src/tests/username_store.cairo b/onchain/src/tests/username_store.cairo new file mode 100644 index 00000000..197b59dd --- /dev/null +++ b/onchain/src/tests/username_store.cairo @@ -0,0 +1,72 @@ +use username_store::UsernameStore; +use username_store::{IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait}; +use username::UserNameClaimErrors; + + + +#[cfg(test)] +mod tests { + + // Import the deploy syscall to be able to deploy the contract. + use starknet::{ + ContractAddress, get_caller_address, get_contract_address, contract_address_const + }; + + use starknet::syscalls::deploy_syscall; + use starknet::{ContractAddress, contract_address_const}; + + // Use starknet test utils to fake the transaction context. + use starknet::testing::{claim_username, transfer_username}; + + fn USERNAME_STORE_CONTRACT() -> ContractAddress { + contract_address_const::<'UsernameStores'>() +} + + // Deploy the contract and return its dispatcher. + fn deploy() -> ContractAddress { + // Set up constructor arguments. + let contract = snf::declare('UsernameStores'); + + + let contract_addr = contract.deploy_at(@calldata, USERNAME_STORE_CONTRACT()).unwrap(); + + contract_addr + } + + + #[test] + #[available_gas(2000000000)] + fn test_claimUsername() { + + // Fake the caller address to address 1 + let owner = contract_address_const::<1>(); + claim_username(owner, 'demo_name'); + + let contract = deploy(10); + assert(contract_state.username.read('transfer_username(&contract_state, "demo_name", new_address)') == get_caller_address(), 'Test case 1 failed: Username not claimed correctly'); + + + assert(core::panic_with_felt252(UserNameClaimErrors::USERNAME_CLAIMED), 'Test case 2 failed: Username claimed erroneously'); + + } + + #[test] + #[available_gas(2000000000)] + fn test_transfer_username() { + + // Fake the caller address to address 1 + let original_owner = contract_address_const::<1>(); + let new_owner = contract_address_const::<2>(); + + transfer_username(&contract_state, 'demo_name', new_address); + + let contract = deploy(10); + assert(contract_state.username.read('demo_name') == new_owner, "Test case 1 failed: Username not transferred correctly"); + + assert(contract_state.username.read('demo_name') == original_owner, "Test case 1 failed: Username not transferred correctly"); + + + } + + +} \ No newline at end of file diff --git a/onchain/src/username_store.cairo b/onchain/src/username_store.cairo index 54475c68..8b9d8bca 100644 --- a/onchain/src/username_store.cairo +++ b/onchain/src/username_store.cairo @@ -7,12 +7,12 @@ pub mod UserNameClaimErrors { #[starknet::interface] pub trait IUsernameStore { - fn claimUsername(ref self: TContractState, key: felt252); - fn transferUsername(ref self: TContractState, key: felt252, new_Address: ContractAddress); + fn claim_username(ref self: TContractState, key: felt252); + fn transfer_username(ref self: TContractState, key: felt252, new_Address: ContractAddress); } #[starknet::contract] -pub mod usernameStore { +pub mod UsernameStore { use starknet::{get_caller_address, ContractAddress}; use super::UserNameClaimErrors; @@ -46,7 +46,7 @@ pub mod usernameStore { #[abi(embed_v0)] impl UsernameStore of super::IUsernameStore { - fn claimUsername(ref self: ContractState, key: felt252) { + fn claim_username(ref self: ContractState, key: felt252) { let mut username = self.username.read(key); if username == get_caller_address() { @@ -65,7 +65,7 @@ pub mod usernameStore { ) } - fn transferUsername(ref self: ContractState, key: felt252, new_Address: ContractAddress) { + fn transfer_username(ref self: ContractState, key: felt252, new_Address: ContractAddress) { let usernameAddress = self.username.read(key); if usernameAddress != get_caller_address() { From 69a451e5041c479f37f5129127d3c66381b956a5 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Fri, 12 Apr 2024 10:45:18 +0100 Subject: [PATCH 3/6] implement changes base on the review recieved on the PR --- onchain/src/lib.cairo | 4 ++++ onchain/src/tests/username_store.cairo | 4 ++-- onchain/src/username_store.cairo | 16 ++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/onchain/src/lib.cairo b/onchain/src/lib.cairo index 5af0a2d6..deb9fd74 100644 --- a/onchain/src/lib.cairo +++ b/onchain/src/lib.cairo @@ -1,7 +1,10 @@ pub mod art_peace; +pub mod username_store; pub mod interface; use art_peace::ArtPeace; use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait}; +use username_store::UsernameStore; + mod quests { pub mod interface; @@ -13,4 +16,5 @@ mod quests { #[cfg(test)] mod tests { mod art_peace; + mod username_store; } diff --git a/onchain/src/tests/username_store.cairo b/onchain/src/tests/username_store.cairo index 197b59dd..6b8618f6 100644 --- a/onchain/src/tests/username_store.cairo +++ b/onchain/src/tests/username_store.cairo @@ -19,13 +19,13 @@ mod tests { use starknet::testing::{claim_username, transfer_username}; fn USERNAME_STORE_CONTRACT() -> ContractAddress { - contract_address_const::<'UsernameStores'>() + contract_address_const::<'UsernameStore'>() } // Deploy the contract and return its dispatcher. fn deploy() -> ContractAddress { // Set up constructor arguments. - let contract = snf::declare('UsernameStores'); + let contract = snf::declare('UsernameStore'); let contract_addr = contract.deploy_at(@calldata, USERNAME_STORE_CONTRACT()).unwrap(); diff --git a/onchain/src/username_store.cairo b/onchain/src/username_store.cairo index 8b9d8bca..58b84e48 100644 --- a/onchain/src/username_store.cairo +++ b/onchain/src/username_store.cairo @@ -13,13 +13,13 @@ pub trait IUsernameStore { #[starknet::contract] pub mod UsernameStore { - use starknet::{get_caller_address, ContractAddress}; + use starknet::{get_caller_address, ContractAddress, contract_address_const}; use super::UserNameClaimErrors; #[storage] struct Storage { - username: LegacyMap:: + usernames: LegacyMap:: } #[event] @@ -47,13 +47,13 @@ pub mod UsernameStore { impl UsernameStore of super::IUsernameStore { fn claim_username(ref self: ContractState, key: felt252) { - let mut username = self.username.read(key); + let mut username_address = self.usernames.read(key); - if username == get_caller_address() { + if username_address == contract_address_const::<0>() { core::panic_with_felt252(UserNameClaimErrors::USERNAME_CLAIMED); } - self.username.write(key, get_caller_address()); + self.usernames.write(key, get_caller_address()); self.emit( @@ -66,13 +66,13 @@ pub mod UsernameStore { } fn transfer_username(ref self: ContractState, key: felt252, new_Address: ContractAddress) { - let usernameAddress = self.username.read(key); + let username_address = self.usernames.read(key); - if usernameAddress != get_caller_address() { + if username_address != get_caller_address() { core::panic_with_felt252(UserNameClaimErrors::USERNAME_CANNOT_BE_TRANSFER); } - self.username.write(key, new_Address); + self.usernames.write(key, new_Address); self.emit( From 421abe4f4d4dbb8d99024aab98e1691e56237eb5 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Sun, 14 Apr 2024 15:29:29 +0100 Subject: [PATCH 4/6] fix unit test for claim_username and tranfer_username --- onchain/src/lib.cairo | 2 +- onchain/src/tests/username_store.cairo | 64 +++++++++++++------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/onchain/src/lib.cairo b/onchain/src/lib.cairo index f06bd345..eee5f7c2 100644 --- a/onchain/src/lib.cairo +++ b/onchain/src/lib.cairo @@ -2,7 +2,7 @@ pub mod art_peace; pub mod username_store; pub mod interface; use art_peace::ArtPeace; -use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait}; +use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel}; use username_store::UsernameStore; diff --git a/onchain/src/tests/username_store.cairo b/onchain/src/tests/username_store.cairo index 6b8618f6..57c7258e 100644 --- a/onchain/src/tests/username_store.cairo +++ b/onchain/src/tests/username_store.cairo @@ -2,6 +2,8 @@ use username_store::UsernameStore; use username_store::{IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait}; use username::UserNameClaimErrors; +use starknet::{ContractAddress}; +use snforge_std::{declare, cheatcodes::contract_class::ContractClassTrait}; #[cfg(test)] @@ -16,54 +18,52 @@ mod tests { use starknet::{ContractAddress, contract_address_const}; // Use starknet test utils to fake the transaction context. - use starknet::testing::{claim_username, transfer_username}; + // use starknet::testing::{claim_username, transfer_username}; - fn USERNAME_STORE_CONTRACT() -> ContractAddress { - contract_address_const::<'UsernameStore'>() -} - - // Deploy the contract and return its dispatcher. - fn deploy() -> ContractAddress { - // Set up constructor arguments. - let contract = snf::declare('UsernameStore'); - - - let contract_addr = contract.deploy_at(@calldata, USERNAME_STORE_CONTRACT()).unwrap(); - - contract_addr + // Deploy the contract and return its dispatcher. + fn deploy_contract() -> ContractAddress { + let contract = declare("UsernameStore"); + // contract.deploy().unwrap(); + return contract.deploy(@ArrayTrait::new()).unwrap(); } - - #[test] + + #[test] #[available_gas(2000000000)] - fn test_claimUsername() { + fn check_claimed_username(){ + // deploy the contract + let contract_address = deploy_contract(); + let dispatcher = IUsernameStoreDispatcher { contract_address }; - // Fake the caller address to address 1 - let owner = contract_address_const::<1>(); - claim_username(owner, 'demo_name'); + // call the claimed username + let claim_username = dispatcher.claim_username('demo_name') + + let original_owner = contract_address_const::<0>(); - let contract = deploy(10); - assert(contract_state.username.read('transfer_username(&contract_state, "demo_name", new_address)') == get_caller_address(), 'Test case 1 failed: Username not claimed correctly'); + assert(contract_state.usernames.read('demo_name') == original_owner, Errors::NOT_EQUAL); - - assert(core::panic_with_felt252(UserNameClaimErrors::USERNAME_CLAIMED), 'Test case 2 failed: Username claimed erroneously'); - } + #[test] #[available_gas(2000000000)] fn test_transfer_username() { + // deploy the contract + let contract_address = deploy_contract(); + let dispatcher = IUsernameStoreDispatcher { contract_address }; + + // call the claimed username + let claim_username = dispatcher.claim_username('demo_name') // Fake the caller address to address 1 - let original_owner = contract_address_const::<1>(); - let new_owner = contract_address_const::<2>(); + let original_owner = contract_address_const::<0>(); - transfer_username(&contract_state, 'demo_name', new_address); + let new_owner = contract_address_const::<1>(); - let contract = deploy(10); - assert(contract_state.username.read('demo_name') == new_owner, "Test case 1 failed: Username not transferred correctly"); - - assert(contract_state.username.read('demo_name') == original_owner, "Test case 1 failed: Username not transferred correctly"); + dispatcher.transfer_usernames('demo_name', new_owner); + + + assert(contract_state.usernames.read('demo_name') == new_owner, "Test case 1 failed: Username not transferred correctly"); } From 27b49c504480d49f54bcb9c275a6d204a2869666 Mon Sep 17 00:00:00 2001 From: mubarak23 Date: Tue, 16 Apr 2024 12:37:09 +0100 Subject: [PATCH 5/6] fix merge conflict --- onchain/src/lib.cairo | 4 +- onchain/src/tests/username_store.cairo | 82 +++++++++----------------- onchain/src/username_store.cairo | 65 ++++++++++---------- 3 files changed, 63 insertions(+), 88 deletions(-) diff --git a/onchain/src/lib.cairo b/onchain/src/lib.cairo index df4a0caf..13ab50df 100644 --- a/onchain/src/lib.cairo +++ b/onchain/src/lib.cairo @@ -36,6 +36,7 @@ mod nfts { }; } + mod mocks { pub mod erc20_mock; } @@ -43,6 +44,7 @@ mod mocks { #[cfg(test)] mod tests { mod art_peace; - mod username_store; + pub mod username_store; pub(crate) mod utils; } + diff --git a/onchain/src/tests/username_store.cairo b/onchain/src/tests/username_store.cairo index 57c7258e..ba9253d1 100644 --- a/onchain/src/tests/username_store.cairo +++ b/onchain/src/tests/username_store.cairo @@ -1,72 +1,46 @@ -use username_store::UsernameStore; -use username_store::{IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait}; -use username::UserNameClaimErrors; +use art_peace::username_store::IUsernameStoreDispatcherTrait; -use starknet::{ContractAddress}; -use snforge_std::{declare, cheatcodes::contract_class::ContractClassTrait}; +use snforge_std::{declare, ContractClassTrait}; -#[cfg(test)] -mod tests { - // Import the deploy syscall to be able to deploy the contract. - use starknet::{ - ContractAddress, get_caller_address, get_contract_address, contract_address_const - }; - - use starknet::syscalls::deploy_syscall; - use starknet::{ContractAddress, contract_address_const}; - - // Use starknet test utils to fake the transaction context. - // use starknet::testing::{claim_username, transfer_username}; +use art_peace::username_store::IUsernameStoreDispatcher; - // Deploy the contract and return its dispatcher. - fn deploy_contract() -> ContractAddress { - let contract = declare("UsernameStore"); - // contract.deploy().unwrap(); - return contract.deploy(@ArrayTrait::new()).unwrap(); - } +use starknet::{ContractAddress, get_caller_address, get_contract_address, contract_address_const}; - - #[test] - #[available_gas(2000000000)] - fn check_claimed_username(){ - // deploy the contract - let contract_address = deploy_contract(); - let dispatcher = IUsernameStoreDispatcher { contract_address }; - // call the claimed username - let claim_username = dispatcher.claim_username('demo_name') - - let original_owner = contract_address_const::<0>(); - assert(contract_state.usernames.read('demo_name') == original_owner, Errors::NOT_EQUAL); - - } +fn deploy_contract() -> ContractAddress { + let contract = declare("UsernameStore"); + + return contract.deploy(@ArrayTrait::new()).unwrap(); +} - #[test] - #[available_gas(2000000000)] - fn test_transfer_username() { - // deploy the contract - let contract_address = deploy_contract(); - let dispatcher = IUsernameStoreDispatcher { contract_address }; +#[test] +fn test_claim_username() { + + let contract_address = deploy_contract(); + let dispatcher = IUsernameStoreDispatcher { contract_address }; + dispatcher.claim_username('deal'); - // call the claimed username - let claim_username = dispatcher.claim_username('demo_name') + let username_address = dispatcher.get_username('deal'); + + assert(contract_address != username_address, 'Username not claimed'); - // Fake the caller address to address 1 - let original_owner = contract_address_const::<0>(); +} +#[test] +fn test_transfer_username() { + let contract_address = deploy_contract(); + let dispatcher = IUsernameStoreDispatcher { contract_address }; + dispatcher.claim_username('devsweet'); - let new_owner = contract_address_const::<1>(); + let second_contract_address = contract_address_const::<1>(); - dispatcher.transfer_usernames('demo_name', new_owner); + dispatcher.transfer_username('devsweet', second_contract_address); - - assert(contract_state.usernames.read('demo_name') == new_owner, "Test case 1 failed: Username not transferred correctly"); - - - } + let username_address = dispatcher.get_username('devsweet'); + assert( username_address == second_contract_address, 'Username not Transferred'); } \ No newline at end of file diff --git a/onchain/src/username_store.cairo b/onchain/src/username_store.cairo index 58b84e48..2f9201f6 100644 --- a/onchain/src/username_store.cairo +++ b/onchain/src/username_store.cairo @@ -1,21 +1,23 @@ use starknet::ContractAddress; -pub mod UserNameClaimErrors { - pub const USERNAME_CLAIMED: felt252 = 'username_claimed'; - pub const USERNAME_CANNOT_BE_TRANSFER : felt252 = 'username_cannot_be_transferred'; -} - #[starknet::interface] pub trait IUsernameStore { fn claim_username(ref self: TContractState, key: felt252); fn transfer_username(ref self: TContractState, key: felt252, new_Address: ContractAddress); + fn get_username(ref self: TContractState, key: felt252) -> ContractAddress; +} + +pub mod UserNameClaimErrors { + pub const USERNAME_CLAIMED: felt252 = 'username_claimed'; + pub const USERNAME_CANNOT_BE_TRANSFER: felt252 = 'username_cannot_be_transferred'; } + #[starknet::contract] pub mod UsernameStore { use starknet::{get_caller_address, ContractAddress, contract_address_const}; - - use super::UserNameClaimErrors; + + use super::{IUsernameStore, UserNameClaimErrors}; #[storage] struct Storage { @@ -44,46 +46,43 @@ pub mod UsernameStore { } #[abi(embed_v0)] - impl UsernameStore of super::IUsernameStore { - - fn claim_username(ref self: ContractState, key: felt252) { + pub impl UsernameStore of IUsernameStore { + fn claim_username(ref self: ContractState, key: felt252) { let mut username_address = self.usernames.read(key); - - if username_address == contract_address_const::<0>() { - core::panic_with_felt252(UserNameClaimErrors::USERNAME_CLAIMED); - } - - self.usernames.write(key, get_caller_address()); - self.emit( - Event::UserNameClaimed( - UserNameClaimed { - username: key, address: get_caller_address() - } + + assert(username_address == contract_address_const::<0>(), UserNameClaimErrors::USERNAME_CLAIMED); + + self.usernames.write(key, get_caller_address()); + + self + .emit( + Event::UserNameClaimed( + UserNameClaimed { username: key, address: get_caller_address() } + ) ) - ) } fn transfer_username(ref self: ContractState, key: felt252, new_Address: ContractAddress) { let username_address = self.usernames.read(key); - + if username_address != get_caller_address() { core::panic_with_felt252(UserNameClaimErrors::USERNAME_CANNOT_BE_TRANSFER); } - + self.usernames.write(key, new_Address); - - self.emit( - Event::UserNameTransferred( - UserNameTransferred { - username: key, address: new_Address - } + self + .emit( + Event::UserNameTransferred( + UserNameTransferred { username: key, address: new_Address } + ) ) - ) - + } + fn get_username(ref self: ContractState, key: felt252) -> ContractAddress { + let username_address = self.usernames.read(key); + return username_address; } } } - From 47ba97d67a843b9d41ac956c160459778d89fb6f Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Tue, 16 Apr 2024 14:44:49 -0500 Subject: [PATCH 6/6] Refactor --- onchain/src/lib.cairo | 13 ++++++--- onchain/src/tests/username_store.cairo | 24 +++++---------- onchain/src/username_store/interfaces.cairo | 8 +++++ .../{ => username_store}/username_store.cairo | 29 +++++++------------ 4 files changed, 34 insertions(+), 40 deletions(-) create mode 100644 onchain/src/username_store/interfaces.cairo rename onchain/src/{ => username_store}/username_store.cairo (72%) diff --git a/onchain/src/lib.cairo b/onchain/src/lib.cairo index 13ab50df..5c6a3325 100644 --- a/onchain/src/lib.cairo +++ b/onchain/src/lib.cairo @@ -1,9 +1,7 @@ pub mod art_peace; -pub mod username_store; pub mod interfaces; use art_peace::ArtPeace; -use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel}; -use username_store::UsernameStore; +use interfaces::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel}; mod quests { @@ -36,6 +34,13 @@ mod nfts { }; } +mod username_store { + pub mod interfaces; + pub mod username_store; + + use interfaces::{IUsernameStore, IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait}; + use username_store::UsernameStore; +} mod mocks { pub mod erc20_mock; @@ -44,7 +49,7 @@ mod mocks { #[cfg(test)] mod tests { mod art_peace; - pub mod username_store; + mod username_store; pub(crate) mod utils; } diff --git a/onchain/src/tests/username_store.cairo b/onchain/src/tests/username_store.cairo index ba9253d1..f4f4ee38 100644 --- a/onchain/src/tests/username_store.cairo +++ b/onchain/src/tests/username_store.cairo @@ -1,33 +1,24 @@ -use art_peace::username_store::IUsernameStoreDispatcherTrait; - - use snforge_std::{declare, ContractClassTrait}; - - -use art_peace::username_store::IUsernameStoreDispatcher; - +use art_peace::username_store::interfaces::{ + IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait +}; use starknet::{ContractAddress, get_caller_address, get_contract_address, contract_address_const}; - - fn deploy_contract() -> ContractAddress { let contract = declare("UsernameStore"); - + return contract.deploy(@ArrayTrait::new()).unwrap(); } - #[test] fn test_claim_username() { - let contract_address = deploy_contract(); let dispatcher = IUsernameStoreDispatcher { contract_address }; dispatcher.claim_username('deal'); let username_address = dispatcher.get_username('deal'); - - assert(contract_address != username_address, 'Username not claimed'); + assert(contract_address != username_address, 'Username not claimed'); } #[test] fn test_transfer_username() { @@ -41,6 +32,5 @@ fn test_transfer_username() { let username_address = dispatcher.get_username('devsweet'); - assert( username_address == second_contract_address, 'Username not Transferred'); - -} \ No newline at end of file + assert(username_address == second_contract_address, 'Username not Transferred'); +} diff --git a/onchain/src/username_store/interfaces.cairo b/onchain/src/username_store/interfaces.cairo new file mode 100644 index 00000000..04ea5850 --- /dev/null +++ b/onchain/src/username_store/interfaces.cairo @@ -0,0 +1,8 @@ +use starknet::ContractAddress; + +#[starknet::interface] +pub trait IUsernameStore { + fn claim_username(ref self: TContractState, key: felt252); + fn transfer_username(ref self: TContractState, key: felt252, new_Address: ContractAddress); + fn get_username(ref self: TContractState, key: felt252) -> ContractAddress; +} diff --git a/onchain/src/username_store.cairo b/onchain/src/username_store/username_store.cairo similarity index 72% rename from onchain/src/username_store.cairo rename to onchain/src/username_store/username_store.cairo index 2f9201f6..309a5fe2 100644 --- a/onchain/src/username_store.cairo +++ b/onchain/src/username_store/username_store.cairo @@ -1,23 +1,13 @@ -use starknet::ContractAddress; - -#[starknet::interface] -pub trait IUsernameStore { - fn claim_username(ref self: TContractState, key: felt252); - fn transfer_username(ref self: TContractState, key: felt252, new_Address: ContractAddress); - fn get_username(ref self: TContractState, key: felt252) -> ContractAddress; -} - pub mod UserNameClaimErrors { pub const USERNAME_CLAIMED: felt252 = 'username_claimed'; pub const USERNAME_CANNOT_BE_TRANSFER: felt252 = 'username_cannot_be_transferred'; } - #[starknet::contract] pub mod UsernameStore { use starknet::{get_caller_address, ContractAddress, contract_address_const}; - - use super::{IUsernameStore, UserNameClaimErrors}; + use art_peace::username_store::IUsernameStore; + use super::UserNameClaimErrors; #[storage] struct Storage { @@ -46,13 +36,14 @@ pub mod UsernameStore { } #[abi(embed_v0)] - pub impl UsernameStore of IUsernameStore { - fn claim_username(ref self: ContractState, key: felt252) { + pub impl UsernameStore of IUsernameStore { + fn claim_username(ref self: ContractState, key: felt252) { let mut username_address = self.usernames.read(key); - - - assert(username_address == contract_address_const::<0>(), UserNameClaimErrors::USERNAME_CLAIMED); + assert( + username_address == contract_address_const::<0>(), + UserNameClaimErrors::USERNAME_CLAIMED + ); self.usernames.write(key, get_caller_address()); @@ -80,9 +71,9 @@ pub mod UsernameStore { ) ) } + fn get_username(ref self: ContractState, key: felt252) -> ContractAddress { - let username_address = self.usernames.read(key); - return username_address; + self.usernames.read(key) } } }