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

Inspector lands function #243

Merged
merged 10 commits into from
Dec 14, 2024
12 changes: 10 additions & 2 deletions land_registry/src/interface/land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ pub trait ILandRegistry<TContractState> {
fn get_land_count(self: @TContractState) -> u256;
fn get_lands_by_owner(self: @TContractState, owner: ContractAddress) -> Span<u256>;
fn get_all_lands(self: @TContractState) -> Span<Land>;
fn update_land(ref self: TContractState, land_id: u256, area: u256, land_use: LandUse);
fn update_land(
ref self: TContractState,
land_id: u256,
area: u256,
land_use: LandUse,
land_status: LandStatus
);
fn approve_land(ref self: TContractState, land_id: u256);
fn reject_land(ref self: TContractState, land_id: u256);
fn is_inspector(self: @TContractState, inspector: ContractAddress) -> bool;
Expand All @@ -93,6 +99,7 @@ pub trait ILandRegistry<TContractState> {
fn add_inspector(ref self: TContractState, inspector: ContractAddress);
fn remove_inspector(ref self: TContractState, inspector: ContractAddress);
fn get_all_inspectors(self: @TContractState) -> Array<ContractAddress>;
fn inspector_lands(self: @TContractState, inspector: ContractAddress) -> Array<Land>;

fn get_user_type(self: @TContractState, userAddress: ContractAddress) -> felt252;

Expand Down Expand Up @@ -133,7 +140,8 @@ pub struct LandVerified {
pub struct LandUpdated {
pub land_id: u256,
pub land_use: Option<felt252>,
pub area: u256
pub area: u256,
pub status: LandStatus
}


Expand Down
41 changes: 38 additions & 3 deletions land_registry/src/land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,26 @@ pub mod LandRegistryContract {
lands.span()
}

fn update_land(ref self: ContractState, land_id: u256, area: u256, land_use: LandUse) {
fn update_land(
ref self: ContractState,
land_id: u256,
area: u256,
land_use: LandUse,
land_status: LandStatus
) {
assert(InternalFunctions::only_owner(@self, land_id), Errors::UPDATE_BY_LAND);
let mut land = self.lands.read(land_id);
land.area = area;
land.land_use = land_use;
land.status = land_status;
self.lands.write(land_id, land);

self.emit(LandUpdated { land_id: land_id, area: area, land_use: land_use.into() });
self
.emit(
LandUpdated {
land_id: land_id, area: area, land_use: land_use.into(), status: land_status
}
);
}

// Transfers land ownership to a new owner
Expand Down Expand Up @@ -377,8 +389,10 @@ pub mod LandRegistryContract {
}

fn set_land_inspector(ref self: ContractState, land_id: u256, inspector: ContractAddress) {
assert(InternalFunctions::only_owner(@self, land_id), Errors::OWNER_MK_INSPECTOR);
assert(self.only_owner(land_id), Errors::OWNER_MK_INSPECTOR);

let prev_land_count = self.lands_assigned_to_inspector.read(inspector);

self.land_inspectors.write(land_id, inspector);
self.lands_assigned_to_inspector.write(inspector, prev_land_count + 1);

Expand All @@ -389,6 +403,27 @@ pub mod LandRegistryContract {
self.emit(LandInspectorSet { land_id, inspector });
}

fn inspector_lands(self: @ContractState, inspector: ContractAddress) -> Array<Land> {
let mut inspector_lands: Array<Land> = array![];

let land_count = self.land_count.read();
let mut i: u256 = 1;

while i < land_count + 1 {
let land_registry: Land = self.lands_registry.read(i);
let land_inspector = self.land_inspectors.read(land_registry.land_id);

if land_inspector == inspector {
let land = self.lands.read(land_registry.land_id);
inspector_lands.append(land);
}

i += 1;
};

inspector_lands
}

fn get_land_inspector(self: @ContractState, land_id: u256) -> ContractAddress {
self.land_inspectors.read(land_id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ fn test_register_update_approve_and_transfer() {
let location: Location = Location { latitude: 1, longitude: 2 };
let area: u256 = 1000;
let land_use = LandUse::Residential;
let land_status = LandStatus::Pending;

// register and add inspector
start_cheat_caller_address(contract_address, owner);
Expand All @@ -222,7 +223,7 @@ fn test_register_update_approve_and_transfer() {
// update land
let new_area: u256 = 1000;
let new_land_use = LandUse::Residential;
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);

stop_cheat_caller_address(contract_address);

Expand Down Expand Up @@ -278,7 +279,8 @@ fn test_register_approve_update_and_transfer() {
start_cheat_caller_address(contract_address, owner);
let new_area: u256 = 1000;
let new_land_use = LandUse::Residential;
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
let land_status = LandStatus::Approved;
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);
stop_cheat_caller_address(contract_address);

let mut spy = spy_events();
Expand Down Expand Up @@ -312,6 +314,7 @@ fn test_register_approve_transfer_and_update_by_new_owner() {
let location: Location = Location { latitude: 1, longitude: 2 };
let area: u256 = 1000;
let land_use = LandUse::Residential;
let land_status = LandStatus::Approved;

// register and add inspector
start_cheat_caller_address(contract_address, owner);
Expand All @@ -333,7 +336,7 @@ fn test_register_approve_transfer_and_update_by_new_owner() {
start_cheat_caller_address(contract_address, new_owner);
let new_area: u256 = 2000;
let new_land_use = LandUse::Commercial;
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);
}

#[test]
Expand Down Expand Up @@ -369,6 +372,7 @@ fn previous_owner_tries_to_update_after_transfer_should_fail() {
let location: Location = Location { latitude: 1, longitude: 2 };
let area: u256 = 1000;
let land_use = LandUse::Residential;
let land_status = LandStatus::Approved;

// register and add inspector
start_cheat_caller_address(contract_address, owner);
Expand All @@ -389,7 +393,7 @@ fn previous_owner_tries_to_update_after_transfer_should_fail() {
// update land
let new_area: u256 = 2000;
let new_land_use = LandUse::Commercial;
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);
}

#[test]
Expand Down Expand Up @@ -449,6 +453,7 @@ fn test_try_to_update_a_not_existant_land_should_fail() {
let location: Location = Location { latitude: 1, longitude: 2 };
let area: u256 = 1000;
let land_use = LandUse::Residential;
let land_status = LandStatus::Approved;

// register and add inspector
start_cheat_caller_address(contract_address, owner);
Expand All @@ -457,5 +462,5 @@ fn test_try_to_update_a_not_existant_land_should_fail() {
start_cheat_caller_address(contract_address, not_owner);
let new_area = 2000;
let new_land_use = LandUse::Commercial;
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);
}
38 changes: 36 additions & 2 deletions land_registry/tests/test_land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ fn test_can_update_land() {
let location = Location { latitude: 1, longitude: 2 };
let initial_area = 1000;
let initial_land_use = LandUse::Residential;
let land_status = LandStatus::Approved;

// Register land as owner
start_cheat_caller_address(contract_address, owner_address);
Expand All @@ -434,12 +435,13 @@ fn test_can_update_land() {
let new_land_use = LandUse::Commercial;

// Update land
land_register_dispatcher.update_land(land_id, new_area, new_land_use);
land_register_dispatcher.update_land(land_id, new_area, new_land_use, land_status);

// Verify updates
let updated_land = land_register_dispatcher.get_land(land_id);
assert(updated_land.area == new_area, 'Area not updated correctly');
assert(updated_land.land_use == new_land_use, 'Land use not updated correctly');
assert(updated_land.status == land_status, 'Land status not updated');

stop_cheat_caller_address(contract_address);
}
Expand All @@ -464,7 +466,8 @@ fn test_update_land_by_unauthorized_user_will_fail() {

// Attempt to update land as unauthorized user
start_cheat_caller_address(contract_address, unauthorized_address);
land_register_dispatcher.update_land(land_id, 1500, LandUse::Commercial); // This should panic
land_register_dispatcher
.update_land(land_id, 1500, LandUse::Commercial, LandStatus::Approved); // This should panic
stop_cheat_caller_address(contract_address);
}

Expand Down Expand Up @@ -1244,3 +1247,34 @@ fn test_set_land_inspector() {
);
stop_cheat_caller_address(contract_address);
}

#[test]
fn test_inspector_lands() {
let contract_address = deploy("LandRegistryContract");

// Instance of LandRegistryContract
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

start_cheat_max_fee(contract_address, 10000000000000000000);

// Set up test data
let owner_address = starknet::contract_address_const::<0x123>();
let inspector_address = starknet::contract_address_const::<0x456>();
let location = Location { latitude: 1, longitude: 2 };
let area = 1000;
let land_use = LandUse::Residential;

// Register land as owner
start_cheat_caller_address(contract_address, owner_address);
let land_id = land_register_dispatcher.register_land(location, area, land_use);
stop_cheat_caller_address(contract_address);

// Set inspector as owner address
start_cheat_caller_address(contract_address, owner_address);
land_register_dispatcher.set_land_inspector(land_id, inspector_address);
stop_cheat_caller_address(contract_address);

let inspector_lands = land_register_dispatcher.inspector_lands(inspector_address);

assert(inspector_lands.len() == 1, 'Wrong inspector land count');
}
Loading