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

Ft username store contract #32

Merged
merged 9 commits into from
Apr 16, 2024
6 changes: 5 additions & 1 deletion onchain/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod art_peace;
pub mod username_store;
pub mod interface;
use art_peace::ArtPeace;
use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel};
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait};
use username_store::UsernameStore;


mod quests {
pub mod interfaces;
Expand All @@ -23,4 +26,5 @@ mod templates {
#[cfg(test)]
mod tests {
mod art_peace;
mod username_store;
}
72 changes: 72 additions & 0 deletions onchain/src/tests/username_store.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use username_store::UsernameStore;
use username_store::{IUsernameStoreDispatcher, IUsernameStoreDispatcherTrait};
use username::UserNameClaimErrors;



#[cfg(test)]
mod tests {

b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
// 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::<'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
}


#[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");


}


}
89 changes: 89 additions & 0 deletions onchain/src/username_store.cairo
Original file line number Diff line number Diff line change
@@ -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<TContractState> {
fn claim_username(ref self: TContractState, key: felt252);
fn transfer_username(ref self: TContractState, key: felt252, new_Address: ContractAddress);
}

#[starknet::contract]
pub mod UsernameStore {
use starknet::{get_caller_address, ContractAddress, contract_address_const};

use super::UserNameClaimErrors;

#[storage]
struct Storage {
usernames: LegacyMap::<felt252, ContractAddress>
}

#[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<ContractState> {

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()
}
)
)
}

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
}
)
)

}
}
}