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

feat: Implement Error Handling Module #108

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions land_registry/src/errors.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/////////////////
//CUSTOM ERRORS
/////////////////
mod Errors {
const MINT_NFT: felt252 = 'Only land registry can mint';
const TRANSFER_NFT: felt252 = 'Only land registry can transfer';
const UPDATE_BY_OWNER: felt252 = 'Only owner can update land';
const ONLY_OWNER_TRNF: felt252 = 'Only owner can transfer';
const LAND_APPROVAL: felt252 = 'Land must be approved';
const NO_LAND: felt252 = 'Land not found';
const INSPECTOR_APPROVE: felt252 = 'Only inspector can approve';
const INSPECTOR_REJECT: felt252 = 'Only inspector can reject';
}
11 changes: 4 additions & 7 deletions land_registry/src/land_nft.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet::ContractAddress;
use openzeppelin::token::erc721::ERC721Component;
use openzeppelin::introspection::src5::SRC5Component;
use crate::land_registry::errors::Errors;

#[starknet::interface]
pub trait ILandNFT<TContractState> {
Expand All @@ -17,7 +18,7 @@ pub mod LandNFT {
use openzeppelin::token::erc721::{ERC721Component, ERC721Component::InternalTrait};
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::token::erc721::ERC721HooksEmptyImpl;

use crate::land_registry::errors::Errors;

component!(path: ERC721Component, storage: erc721, event: ERC721Event);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
Expand Down Expand Up @@ -56,10 +57,7 @@ pub mod LandNFT {
impl LandNFTImpl of ILandNFT<ContractState> {
fn mint(ref self: ContractState, to: ContractAddress, token_id: u256) {
// Only the land registry contract can mint NFTs
assert(
starknet::get_caller_address() == self.land_registry.read(),
'Only land registry can mint'
);
assert(starknet::get_caller_address() == self.land_registry.read(), Errors::MINT_NFT);
self.erc721.mint(to, token_id);
}

Expand All @@ -68,8 +66,7 @@ pub mod LandNFT {
) {
// Only the land registry contract can transfer NFTs
assert(
starknet::get_caller_address() == self.land_registry.read(),
'Only land registry can transfer'
starknet::get_caller_address() == self.land_registry.read(), Errors::TRANSFER_NFT
);
self.erc721.transfer(from, to, token_id);
}
Expand Down
13 changes: 7 additions & 6 deletions land_registry/src/land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod LandRegistryContract {
use land_registry::utils::utils::{create_land_id, LandUseIntoOptionFelt252};
use core::array::ArrayTrait;
use starknet::storage::{Map, StorageMapWriteAccess, StorageMapReadAccess};
use crate::land_registry::errors::Errors;


#[storage]
Expand Down Expand Up @@ -122,7 +123,7 @@ pub mod LandRegistryContract {
}

fn update_land(ref self: ContractState, land_id: u256, area: u256, land_use: LandUse) {
assert(InternalFunctions::only_owner(@self, land_id), 'Only owner can update land');
assert(InternalFunctions::only_owner(@self, land_id), Errors::UPDATE_BY_OWNER);
let mut land = self.lands.read(land_id);
land.area = area;
land.land_use = land_use;
Expand All @@ -132,8 +133,8 @@ pub mod LandRegistryContract {
}

fn transfer_land(ref self: ContractState, land_id: u256, new_owner: ContractAddress) {
assert(InternalFunctions::only_owner(@self, land_id), 'Only owner can transfer');
assert(self.approved_lands.read(land_id), 'Land must be approved');
assert(InternalFunctions::only_owner(@self, land_id), Errors::ONLY_OWNER_TRNF);
assert(self.approved_lands.read(land_id), Errors::LAND_APPROVAL);

let mut land = self.lands.read(land_id);
let old_owner = land.owner;
Expand All @@ -155,7 +156,7 @@ pub mod LandRegistryContract {
i += 1;
};

assert(index_to_remove < old_owner_land_count, 'Land not found');
assert(index_to_remove < old_owner_land_count, Errors::NO_LAND);

if index_to_remove < old_owner_land_count - 1 {
let last_land = self.owner_lands.read((old_owner, old_owner_land_count - 1));
Expand All @@ -182,7 +183,7 @@ pub mod LandRegistryContract {
}

fn approve_land(ref self: ContractState, land_id: u256) {
assert(InternalFunctions::only_inspector(@self), 'Only inspector can approve');
assert(InternalFunctions::only_inspector(@self), Errors::INSPECTOR_APPROVE);
self.approved_lands.write(land_id, true);

// Mint NFT
Expand All @@ -195,7 +196,7 @@ pub mod LandRegistryContract {
}

fn reject_land(ref self: ContractState, land_id: u256) {
assert(InternalFunctions::only_inspector(@self), 'Only inspector can reject');
assert(InternalFunctions::only_inspector(@self), Errors::INSPECTOR_REJECT);
let mut land = self.lands.read(land_id);
land.is_approved = false;
self.lands.write(land_id, land);
Expand Down
2 changes: 1 addition & 1 deletion land_registry/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod interface;
pub mod land_register;
pub mod utils;
pub mod land_nft;

pub mod errors;