Skip to content

Commit

Permalink
feat: add event for main id updates (#6)
Browse files Browse the repository at this point in the history
* feat: add event for main id updates

* feat: flatten events
  • Loading branch information
Th0rgal authored Nov 15, 2023
1 parent e86ebe5 commit 9aa4596
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/identity/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ mod Identity {
ExtendedVerifierDataUpdate: ExtendedVerifierDataUpdate,
UserDataUpdate: UserDataUpdate,
ExtendedUserDataUpdate: ExtendedUserDataUpdate,
MainIdUpdate: MainIdUpdate,
// components
#[flat]
CustomUriEvent: custom_uri_component::Event,
#[flat]
StorageReadEvent: storage_read_component::Event,
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
ERC721Event: ERC721Component::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
}

Expand Down Expand Up @@ -136,6 +143,13 @@ mod Identity {
_data: Span<felt252>,
}

#[derive(Drop, starknet::Event)]
struct MainIdUpdate {
#[key]
owner: ContractAddress,
id: u128,
}

#[constructor]
fn constructor(
ref self: ContractState, owner: ContractAddress, token_uri_base: Span<felt252>,
Expand All @@ -160,7 +174,13 @@ mod Identity {
}

fn get_main_id(self: @ContractState, user: ContractAddress) -> u128 {
self.main_id_by_addr.read(user)
let main_id = self.main_id_by_addr.read(user);
if self.erc721._owner_of(main_id.into()) == user {
main_id
} else {
// if you transfer your main_id to someone, it is no longer your main_id
0
}
}

fn get_user_data(self: @ContractState, id: u128, field: felt252, domain: u32) -> felt252 {
Expand Down Expand Up @@ -234,15 +254,19 @@ mod Identity {
}

fn set_main_id(ref self: ContractState, id: u128) {
// todo: add event
let caller = get_caller_address();
assert(caller == self.erc721._owner_of(id.into()), 'you don\'t own this id');
self.main_id_by_addr.write(caller, id);
self.emit(Event::MainIdUpdate(MainIdUpdate { id, owner: caller }));
}

fn reset_main_id(ref self: ContractState) {
// todo: add event
let id = self.main_id_by_addr.read(get_caller_address());
self.main_id_by_addr.write(get_caller_address(), 0);
self
.emit(
Event::MainIdUpdate(MainIdUpdate { id, owner: ContractAddressZeroable::zero() })
);
}

fn set_user_data(
Expand Down

0 comments on commit 9aa4596

Please sign in to comment.