Skip to content
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
8 changes: 8 additions & 0 deletions assignments/assignment_3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions snfoundry.toml
Original file line number Diff line number Diff line change
@@ -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/"
12 changes: 10 additions & 2 deletions src/counter_v2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ trait ICounterV2<TContractState> {
// 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;
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -100,8 +108,8 @@ mod CounterV2 {
}
}
}
// 0x65f0904d094297f08575291f2da8600b60c12e764b63fdfef8c1044a3eaa34b
// 0x6f2f6eb269f9741d5bb9cb633bfb632a0d71e0622b195ef4c4e66e8f1fee9fe
// 0x3ec3b11893ead66e0981f280d4a09303c9b8d2b51e7f92836a1c0a2e2b8b9c7
// 0x2adfce772d97618bf8871bff3e8c0766147886da505a09dc3ce77777d52f398
// 0x000000000000000000000000000000000000000000000000000000000000000


99 changes: 99 additions & 0 deletions src/counter_v3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use starknet::ContractAddress;

#[starknet::interface]
trait ICounterV3<TContractState> {
// 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<ContractState> {
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
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod counter_v1;
mod counter_v2;
mod counter_v3;
mod student_registry;
pub mod student_struct;
pub mod errors;
2 changes: 1 addition & 1 deletion src/student_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down