From d6337cc0c267246a70c1896910fb67c255406b8b Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 5 Oct 2024 13:14:39 +0100 Subject: [PATCH 1/4] feat: add profile --- Scarb.toml | 2 +- account.md | 40 ++++++++++++++++++++++++++++++++++++++++ snfoundry.toml | 4 ++++ src/lib.cairo | 4 ++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 account.md create mode 100644 snfoundry.toml diff --git a/Scarb.toml b/Scarb.toml index e06eb45..4edc192 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -7,7 +7,7 @@ edition = "2023_11" [dependencies] -starknet = "2.8.2" +starknet = "2.8.0" [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.27.0" } diff --git a/account.md b/account.md new file mode 100644 index 0000000..2f7e51c --- /dev/null +++ b/account.md @@ -0,0 +1,40 @@ +intro_contract +intro_contract +sncast account create --url https://free-rpc.nethermind.io/sepolia-juno/ --name intro_contract +command: account create +add_profile: --add-profile flag was not set. No profile added to snfoundry.toml +address: 0x14e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 +max_fee: 6909344028795 +message: Account successfully created. Prefund generated address with at least STRK tokens or an equivalent amount of ETH tokens. It is good to send more in the case of higher demand. + +To see account creation details, visit: +account: https://sepolia.starkscan.co/contract/0x14e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 +0x014e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 + +sncast --profile intro_contract account deploy --name intro_contract --fee-token eth +command: account deploy +transaction_hash: 0x32e93bcd074e49bb9b4b728c89f1655dd4db57f741f773c9b30425df5fca699 + +To see invocation details, visit: +transaction: https://sepolia.starkscan.co/tx/0x32e93bcd074e49bb9b4b728c89f1655dd4db57f741f773c9b30425df5fca699 + +sncast --account intro_contract declare --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --contract-name CounterV2 +Compiling cairo_bootcamp_3 v0.1.0 (/Users/user/Documents/dev/web3/cairo-bootcamp-3/Scarb.toml) + Finished release target(s) in 6 seconds +command: declare +class_hash: 0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 +transaction_hash: 0x1cf5c43dd7122e23f5247f408d4152659b036250697bc3f89eed8f5de813fc6 + +To see declaration details, visit: +class: https://sepolia.starkscan.co/class/0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 +transaction: https://sepolia.starkscan.co/tx/0x1cf5c43dd7122e23f5247f408d4152659b036250697bc3f89eed8f5de813fc6 + + sncast --account intro_contract deploy --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --class-hash 0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 +command: deploy +contract_address: 0x4e7e5c55a8f11831bc6d9c570d00812ddd80c3f7476f5cde64e8a0f395a931f +transaction_hash: 0x3bdee78c8923a765011a4eea27c102978fe1a9cf8c5500024bb991ffe21d3ad + +To see deployment details, visit: +contract: https://sepolia.starkscan.co/contract/0x4e7e5c55a8f11831bc6d9c570d00812ddd80c3f7476f5cde64e8a0f395a931f +transaction: https://sepolia.starkscan.co/tx/0x3bdee78c8923a765011a4eea27c102978fe1a9cf8c5500024bb991ffe21d3ad + diff --git a/snfoundry.toml b/snfoundry.toml new file mode 100644 index 0000000..dcd7595 --- /dev/null +++ b/snfoundry.toml @@ -0,0 +1,4 @@ +[sncast.intro_contract] +account = "intro_contract" +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/lib.cairo b/src/lib.cairo index e305469..e2afbe9 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -34,3 +34,7 @@ mod Counter { } } } + + + + From fee333d8aed86914998bf3ba2a21866d5d6378f0 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 5 Oct 2024 13:16:47 +0100 Subject: [PATCH 2/4] conflict --- src/lib.cairo | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.cairo b/src/lib.cairo index e305469..3e4898c 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -3,7 +3,8 @@ // module - which houses: // - storage Struct // - impl block - +mod counter_v1; +mod counter_v2; #[starknet::interface] trait ICounter { // get count - retrieve the count from storage From 077a472db42b8e3fc57d9e41bcad898b01b22217 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 5 Oct 2024 16:42:13 +0100 Subject: [PATCH 3/4] account --- account.md | 40 ---------------------------------------- snfoundry.toml | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 40 deletions(-) delete mode 100644 account.md diff --git a/account.md b/account.md deleted file mode 100644 index 2f7e51c..0000000 --- a/account.md +++ /dev/null @@ -1,40 +0,0 @@ -intro_contract -intro_contract -sncast account create --url https://free-rpc.nethermind.io/sepolia-juno/ --name intro_contract -command: account create -add_profile: --add-profile flag was not set. No profile added to snfoundry.toml -address: 0x14e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 -max_fee: 6909344028795 -message: Account successfully created. Prefund generated address with at least STRK tokens or an equivalent amount of ETH tokens. It is good to send more in the case of higher demand. - -To see account creation details, visit: -account: https://sepolia.starkscan.co/contract/0x14e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 -0x014e27cf3cadb8d9354cb6a7e57ddfb04dc07bd422aa77234611ac8bea989f20 - -sncast --profile intro_contract account deploy --name intro_contract --fee-token eth -command: account deploy -transaction_hash: 0x32e93bcd074e49bb9b4b728c89f1655dd4db57f741f773c9b30425df5fca699 - -To see invocation details, visit: -transaction: https://sepolia.starkscan.co/tx/0x32e93bcd074e49bb9b4b728c89f1655dd4db57f741f773c9b30425df5fca699 - -sncast --account intro_contract declare --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --contract-name CounterV2 -Compiling cairo_bootcamp_3 v0.1.0 (/Users/user/Documents/dev/web3/cairo-bootcamp-3/Scarb.toml) - Finished release target(s) in 6 seconds -command: declare -class_hash: 0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 -transaction_hash: 0x1cf5c43dd7122e23f5247f408d4152659b036250697bc3f89eed8f5de813fc6 - -To see declaration details, visit: -class: https://sepolia.starkscan.co/class/0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 -transaction: https://sepolia.starkscan.co/tx/0x1cf5c43dd7122e23f5247f408d4152659b036250697bc3f89eed8f5de813fc6 - - sncast --account intro_contract deploy --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --class-hash 0x62348eca978957b7d06841b55cde76b2b01cf99483c6d2c947cb3bbcaff5d86 -command: deploy -contract_address: 0x4e7e5c55a8f11831bc6d9c570d00812ddd80c3f7476f5cde64e8a0f395a931f -transaction_hash: 0x3bdee78c8923a765011a4eea27c102978fe1a9cf8c5500024bb991ffe21d3ad - -To see deployment details, visit: -contract: https://sepolia.starkscan.co/contract/0x4e7e5c55a8f11831bc6d9c570d00812ddd80c3f7476f5cde64e8a0f395a931f -transaction: https://sepolia.starkscan.co/tx/0x3bdee78c8923a765011a4eea27c102978fe1a9cf8c5500024bb991ffe21d3ad - diff --git a/snfoundry.toml b/snfoundry.toml index e69de29..947cab3 100644 --- a/snfoundry.toml +++ b/snfoundry.toml @@ -0,0 +1,16 @@ +[sncast.cohort_dev] +account = "cohort_dev" +accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" +url = "https://free-rpc.nethermind.io/sepolia-juno/" + + +[sncast.deploy_dev] +account = "deploy_dev" +accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" +url = "https://free-rpc.nethermind.io/sepolia-juno/" + +[sncast.steph] +account = "steph" +accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" +url = "https://free-rpc.nethermind.io/sepolia-juno/" + From e30686c04f2c73cc4a68fdd02150d425d805eb85 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Mon, 7 Oct 2024 09:38:00 +0100 Subject: [PATCH 4/4] feat(StudentRegistry) add events, errors, and checks --- src/errors.cairo | 8 ++++++++ src/events.cairo | 17 +++++++++++++++++ src/lib.cairo | 32 +------------------------------- src/student_registry.cairo | 26 ++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 src/events.cairo diff --git a/src/errors.cairo b/src/errors.cairo index 524d2ad..ebb9003 100644 --- a/src/errors.cairo +++ b/src/errors.cairo @@ -4,4 +4,12 @@ pub mod Errors { pub const ZERO_ADDRESS: felt252 = 'ZERO ADDRESS!'; pub const SAME_ADDRESS: felt252 = 'CANNOT BE SAME ADDRESS!'; pub const ZERO_VALUE: felt252 = 'CANNOT BE ZERO_VALUE!'; + pub const EMPTY_NAME: felt252 = 'NAME CAN NOT BE EMPTY!'; + pub const ADMIN_NOT_ALLOWED: felt252 = 'ADMIN CAN NOT BE A STUDENT'; } +// sncast --profile intro account deploy --name intro --fee-token eth +// sncast --account intro declare --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --contract-name Counter +// sncast --account intro deploy --url https://free-rpc.nethermind.io/sepolia-juno --fee-token eth --class-hash 0x2ed891bb2417107e425e9c1d1f3ef13bec730d21d3340099de685c52679eaea + + +// 0x6067f56674b7b566c299d2e076d452cc0d6d28bde699247138ca09d125a6926 \ No newline at end of file diff --git a/src/events.cairo b/src/events.cairo new file mode 100644 index 0000000..b875937 --- /dev/null +++ b/src/events.cairo @@ -0,0 +1,17 @@ +use starknet::ContractAddress; +#[event] +#[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] +pub enum Event { + + StudentAdded: StudentAdded, +} + +#[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] +pub struct StudentAdded { + #[key] + pub name: felt252, + pub account: ContractAddress, + pub age: u8, + pub xp: u16, + pub is_active: bool +} \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index 7faeaf9..bcdc295 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -5,37 +5,7 @@ // - impl block mod counter_v1; mod counter_v2; - -#[starknet::interface] -trait ICounter { - // 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); -} - - -#[starknet::contract] -mod Counter { - #[storage] - struct Storage { - count: u32, - } - - #[abi(embed_v0)] - impl CounterImpl of super::ICounter { - fn get_count(self: @ContractState) -> u32 { - self.count.read() - } - - fn set_count(ref self: ContractState, amount: u32) { - let current_count: u32 = self.get_count(); - self.count.write(current_count + amount); - } - } -} mod student_registry; pub mod student_struct; pub mod errors; +pub mod events; \ No newline at end of file diff --git a/src/student_registry.cairo b/src/student_registry.cairo index 9a405a3..1a4ffde 100644 --- a/src/student_registry.cairo +++ b/src/student_registry.cairo @@ -1,5 +1,6 @@ use starknet::ContractAddress; use crate::student_struct::Student; +use crate::events::{Event, StudentAdded}; #[starknet::interface] trait IStudentRegistry { @@ -10,6 +11,8 @@ trait IStudentRegistry { // read-only function to get student fn get_student(self: @T, account: ContractAddress) -> (felt252, ContractAddress, u8, u16, bool); + + fn update_student(ref self: T, _name: felt252, _account: ContractAddress, _age: u8); } @@ -18,11 +21,13 @@ mod StudentRegistry { use starknet::{ContractAddress, get_caller_address}; use super::{IStudentRegistry, Student}; use core::num::traits::Zero; - + #[event] + use crate::events::{Event, StudentAdded}; + use starknet::storage::{ StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map }; - use crate::errors::Errors::{NOT_ADMIN, ZERO_ADDRESS}; + use crate::errors::Errors::{NOT_ADMIN, ZERO_ADDRESS, EMPTY_NAME, ADMIN_NOT_ALLOWED}; #[storage] struct Storage { @@ -50,12 +55,16 @@ mod StudentRegistry { _is_active: bool ) { // validation to check if student account is valid address and not a 0 address + let admin = self.admin.read(); assert(!self.is_zero_address(_account), ZERO_ADDRESS); + assert(_account != admin, ADMIN_NOT_ALLOWED); + assert(_name != '', EMPTY_NAME); assert(_age > 0, 'age cannot be 0'); let student = Student { name: _name, account: _account, age: _age, xp: _xp, is_active: _is_active }; self.students_map.entry(_account).write(student); + //TODO: student is not surposed to input _is_active } // read-only function to get student @@ -67,6 +76,19 @@ mod StudentRegistry { let student = self.students_map.entry(account).read(); (student.name, student.account, student.age, student.xp, student.is_active) } + + fn update_student( + ref self: ContractState, + _name: felt252, + _account: ContractAddress, + _age: u8, + ) { + let admin = self.admin.read(); + assert(!self.is_zero_address(_account), ZERO_ADDRESS); + + + } + }