diff --git a/assignments/assignment_3.md b/assignments/assignment_3.md index 0879663..4546220 100644 --- a/assignments/assignment_3.md +++ b/assignments/assignment_3.md @@ -12,3 +12,11 @@ 3. Deploy both `CounterV2` and `CounterV3` contracts and provide their corresponding following class_hash and contract addresses in a `solutions_3.md` file under the `Solutions` folder + +# CounterV2 +## 0x3ec3b11893ead66e0981f280d4a09303c9b8d2b51e7f92836a1c0a2e2b8b9c7 +## 0x2adfce772d97618bf8871bff3e8c0766147886da505a09dc3ce77777d52f398 - https://sepolia.starkscan.co/class/0xfae52025fdff77f2ab38dd5eaa9a23540fbf7131e896d8b5699e2642944e33 + +# CounterV3 +## 0xfae52025fdff77f2ab38dd5eaa9a23540fbf7131e896d8b5699e2642944e33 +## 0x1fa34f58aa881cb66e01a2156adca02e948a44351072d20d7d381783447bc2e - https://sepolia.starkscan.co/contract/0x1fa34f58aa881cb66e01a2156adca02e948a44351072d20d7d381783447bc2e \ No newline at end of file diff --git a/snfoundry.toml b/snfoundry.toml index 336b432..baf7d20 100644 --- a/snfoundry.toml +++ b/snfoundry.toml @@ -1,10 +1,10 @@ -[sncast.cohort_dev] -account = "cohort_dev" +[sncast.anonfedora] +account = "anonfedora" accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" url = "https://free-rpc.nethermind.io/sepolia-juno/" -[sncast.deploy_dev] -account = "deploy_dev" +[sncast.cohort_dev] +account = "cohort_dev" accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" url = "https://free-rpc.nethermind.io/sepolia-juno/" \ No newline at end of file diff --git a/src/counter_v2.cairo b/src/counter_v2.cairo index 0cf84b9..a7d9b62 100644 --- a/src/counter_v2.cairo +++ b/src/counter_v2.cairo @@ -19,6 +19,9 @@ trait ICounterV2 { // increase count by one fn increase_count_by_one(ref self: TContractState); + // decrease count by one + fn decrease_count_by_one(ref self: TContractState); + fn get_current_owner(self: @TContractState) -> ContractAddress; } @@ -72,6 +75,11 @@ mod CounterV2 { self.count.write(current_count + 1) } + // decrease count by one + fn decrease_count_by_one(ref self: ContractState) { + self.count.write(self.get_count() - 1) + } + // util function to get current owner fn get_current_owner(self: @ContractState) -> ContractAddress { self.owner.read() @@ -100,8 +108,8 @@ mod CounterV2 { } } } -// 0x65f0904d094297f08575291f2da8600b60c12e764b63fdfef8c1044a3eaa34b -// 0x6f2f6eb269f9741d5bb9cb633bfb632a0d71e0622b195ef4c4e66e8f1fee9fe +// 0x3ec3b11893ead66e0981f280d4a09303c9b8d2b51e7f92836a1c0a2e2b8b9c7 +// 0x2adfce772d97618bf8871bff3e8c0766147886da505a09dc3ce77777d52f398 // 0x000000000000000000000000000000000000000000000000000000000000000 diff --git a/src/counter_v3.cairo b/src/counter_v3.cairo new file mode 100644 index 0000000..682cb4e --- /dev/null +++ b/src/counter_v3.cairo @@ -0,0 +1,99 @@ +use starknet::ContractAddress; + +#[starknet::interface] +trait ICounterV3 { + // get count - retrieve the count from storage + // a read-only function + fn get_count(self: @TContractState) -> u32; + // set count + fn set_count(ref self: TContractState, amount: u32); + // add owner + fn add_new_owner(ref self: TContractState, new_owner: ContractAddress); + // increase count by one + fn increase_count_by_one(ref self: TContractState); + // decrease count by one + fn decrease_count_by_one(ref self: TContractState); + // get current owner + fn get_current_owner(self: @TContractState) -> ContractAddress; +} + +#[starknet::contract] +mod CounterV3 { + use core::num::traits::Zero; + use super::ICounterV3; + use starknet::{ContractAddress, get_caller_address}; + + #[storage] + struct Storage { + count: u32, + owner: ContractAddress + } + + #[constructor] + fn constructor(ref self: ContractState, _owner: ContractAddress) { + self.owner.write(_owner) + } + + #[abi(embed_v0)] + impl CounterV3Impl of super::ICounterV3 { + fn get_count(self: @ContractState) -> u32 { + self.count.read() + } + fn set_count(ref self: ContractState, amount: u32) { + // can only be executed by the contract deployer (state-changing) + self.only_owner(); + self.count.write(self.get_count() + amount) + } + fn add_new_owner(ref self: ContractState, new_owner: ContractAddress) { + self.only_owner(); + + assert(!self.is_zero_address(new_owner), '0 address'); + + assert(self.get_current_owner() != new_owner, 'same owner'); + + self.owner.write(new_owner); + } + + // increase count by one + fn increase_count_by_one(ref self: ContractState) { + // can only be executed by the contract deployer (state-changing) + assert(self.get_count() < 50, 'count too high'); + self.count.write(self.get_count() + 1) + } + + // decrease count by one + fn decrease_count_by_one(ref self: ContractState) { + // can only be executed by the contract deployer (state-changing) + self.count.write(self.get_count() - 1) + } + + // util function to get current owner + fn get_current_owner(self: @ContractState) -> ContractAddress { + self.owner.read() + } + } + + #[generate_trait] + impl Private of PrivateTrait { + fn only_owner(self: @ContractState) { + // get function caller + let caller = get_caller_address(); + + // get owner of CounterV2 contract + let current_owner: ContractAddress = self.owner.read(); + + // assertion logic + assert(current_owner == caller, 'caller is not owner'); + } + + fn is_zero_address(self: @ContractState, account: ContractAddress) -> bool { + if account.is_zero() { + return true; + } + return false; + } + } +} + +// 0xfae52025fdff77f2ab38dd5eaa9a23540fbf7131e896d8b5699e2642944e33 +// 0x1fa34f58aa881cb66e01a2156adca02e948a44351072d20d7d381783447bc2e \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index ca2e0ce..462909c 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,6 @@ mod counter_v1; mod counter_v2; +mod counter_v3; mod student_registry; pub mod student_struct; pub mod errors; diff --git a/src/student_registry.cairo b/src/student_registry.cairo index 9a405a3..75faa62 100644 --- a/src/student_registry.cairo +++ b/src/student_registry.cairo @@ -18,7 +18,7 @@ mod StudentRegistry { use starknet::{ContractAddress, get_caller_address}; use super::{IStudentRegistry, Student}; use core::num::traits::Zero; - + use starknet::storage::{ StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map };