Skip to content

Commit

Permalink
Merge pull request #107 from No-bodyq/main
Browse files Browse the repository at this point in the history
Implement and test the is_land_approved, get_pending_approvals and get_land_transaction_history
  • Loading branch information
fishonamos authored Oct 28, 2024
2 parents c3c9fb7 + 450534e commit ad1a948
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions land_registry/src/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ pub trait ILandRegistry<TContractState> {
fn is_inspector(self: @TContractState, inspector: ContractAddress) -> bool;
fn add_inspector(ref self: TContractState, inspector: ContractAddress);
fn remove_inspector(ref self: TContractState, inspector: ContractAddress);
fn is_land_approved(self: @TContractState, land_id: u256) -> bool;
fn get_pending_approvals(self: @TContractState) -> Array<u256>;
fn get_land_transaction_history(
self: @TContractState, land_id: u256
) -> Array<(ContractAddress, u64)>;
fn get_land_status(self: @TContractState, land_id: u256) -> LandStatus;
}
43 changes: 43 additions & 0 deletions land_registry/src/land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod LandRegistryContract {
approved_lands: Map::<u256, bool>,
land_count: u256,
nft_contract: ContractAddress,
land_transaction_history: Map::<(u256, u256), (ContractAddress, u64)>,
land_transaction_count: Map::<u256, u256>,
land_inspector_assignments: Map::<u256, ContractAddress>,
}

Expand Down Expand Up @@ -77,6 +79,7 @@ pub mod LandRegistryContract {
let caller = get_caller_address();
let timestamp = get_block_timestamp();
let land_id = create_land_id(caller, timestamp, location);
let transaction_count = self.land_transaction_count.read(land_id);

let new_land = Land {
owner: caller,
Expand All @@ -95,6 +98,11 @@ pub mod LandRegistryContract {
self.owner_lands.write((caller, owner_land_count), land_id);
self.owner_land_count.write(caller, owner_land_count + 1);

self.land_transaction_history.write((land_id, transaction_count), (caller, timestamp));
self
.land_transaction_count
.write(land_id, self.land_transaction_count.read(land_id) + 1);

self
.emit(
LandRegistered {
Expand Down Expand Up @@ -227,6 +235,41 @@ pub mod LandRegistryContract {
self.land_inspectors.write(inspector, false);
}


fn is_land_approved(self: @ContractState, land_id: u256) -> bool {
let land = self.lands.read(land_id);
land.status == LandStatus::Approved
}


fn get_pending_approvals(self: @ContractState) -> Array<u256> {
let mut pending_approvals = array![];
let owner = get_caller_address();
let owner_land_count = self.owner_land_count.read(owner);
let mut i = 0;
while i < owner_land_count {
let land_id = self.owner_lands.read((owner, i));
if (!self.approved_lands.read(land_id)) {
pending_approvals.append(land_id);
}
i += 1;
};
pending_approvals
}

fn get_land_transaction_history(
self: @ContractState, land_id: u256
) -> Array<(ContractAddress, u64)> {
let mut land_history = array![];
let transaction_count = self.land_transaction_count.read(land_id);
let mut i = 0;
while i < transaction_count {
land_history.append(self.land_transaction_history.read((land_id, i)));
i += 1;
};

land_history
}
fn get_land_status(self: @ContractState, land_id: u256) -> LandStatus {
let land = self.lands.read(land_id);
land.status
Expand Down
85 changes: 85 additions & 0 deletions land_registry/tests/test_land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ fn test_can_get_land_count() {
.register_land(Location { latitude: 1, longitude: 2 }, 1000, LandUse::Residential);
// Assert land_count is equal to one after registration
assert(land_register_dispatcher.get_land_count() == 1, 'Should be equal to one');

stop_cheat_caller_address(contract_address);
}

#[test]
Expand All @@ -160,6 +162,89 @@ fn test_can_get_lands_by_owner() {
land_register_dispatcher.get_lands_by_owner(owner_address),
array![land_id1, land_id2, land_id3].span(),
);

stop_cheat_caller_address(contract_address);
}

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

// Get an instance of the deployed Counter contract
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

// Set up test data
let caller_address = starknet::contract_address_const::<0x123>();
let location: Location = Location { latitude: 12, longitude: 34 };
let area: u256 = 1234;
let land_use = LandUse::Commercial;

// Start cheating the caller address
start_cheat_caller_address(contract_address, caller_address);

// Register the land
let land_id = land_register_dispatcher.register_land(location, area, land_use);

assert(
land_register_dispatcher.is_land_approved(land_id) == false, 'Land should not be approved'
);

stop_cheat_caller_address(contract_address);
}

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

// Get an instance of the deployed Counter contract
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

// Set up test data
let caller_address = starknet::contract_address_const::<0x123>();
let location: Location = Location { latitude: 12, longitude: 34 };
let area: u256 = 1234;
let land_use = LandUse::Residential;

// Start cheating the caller address
start_cheat_caller_address(contract_address, caller_address);

// Register the land
let land_id = land_register_dispatcher.register_land(location, area, land_use);

assert(
land_register_dispatcher.get_pending_approvals() == array![land_id],
'Not enough pending approvals'
);
}

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

// Get an instance of the deployed Counter contract
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

// Set up test data
let caller_address = starknet::contract_address_const::<0x123>();
let location: Location = Location { latitude: 12, longitude: 34 };
let area: u256 = 1234;
let land_use = LandUse::Residential;

// Start cheating the caller address
start_cheat_caller_address(contract_address, caller_address);
start_cheat_block_timestamp(contract_address, 1);

// Register the land
let land_id = land_register_dispatcher.register_land(location, area, land_use);

assert(
land_register_dispatcher
.get_land_transaction_history(land_id) == array![(caller_address, 1)],
'Inaccurate land history'
);

stop_cheat_caller_address(contract_address);
stop_cheat_block_timestamp(contract_address);
}

#[test]
Expand Down

0 comments on commit ad1a948

Please sign in to comment.