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/snfoundry.toml b/snfoundry.toml index 336b432..947cab3 100644 --- a/snfoundry.toml +++ b/snfoundry.toml @@ -7,4 +7,10 @@ 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/" \ No newline at end of file +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/" + 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 ca2e0ce..bcdc295 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,11 @@ + +// trait - blueprint which specifies the function signatures we intend to build +// module - which houses: +// - storage Struct +// - impl block mod counter_v1; mod counter_v2; 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); + + + } + }