diff --git a/land_registry/src/errors.cairo b/land_registry/src/errors.cairo new file mode 100644 index 0000000..2e27e60 --- /dev/null +++ b/land_registry/src/errors.cairo @@ -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'; +} diff --git a/land_registry/src/land_nft.cairo b/land_registry/src/land_nft.cairo index 4d6f946..0a60a84 100644 --- a/land_registry/src/land_nft.cairo +++ b/land_registry/src/land_nft.cairo @@ -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 { @@ -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); @@ -56,10 +57,7 @@ pub mod LandNFT { impl LandNFTImpl of ILandNFT { 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); } @@ -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); } diff --git a/land_registry/src/land_register.cairo b/land_registry/src/land_register.cairo index 0b23c0d..ac5307f 100644 --- a/land_registry/src/land_register.cairo +++ b/land_registry/src/land_register.cairo @@ -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] @@ -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; @@ -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; @@ -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)); @@ -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 @@ -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); diff --git a/land_registry/src/lib.cairo b/land_registry/src/lib.cairo index 4b4a797..c78245d 100644 --- a/land_registry/src/lib.cairo +++ b/land_registry/src/lib.cairo @@ -2,4 +2,4 @@ pub mod interface; pub mod land_register; pub mod utils; pub mod land_nft; - +pub mod errors;