From 6383adb2da8d46c01f71e4389cf838a1a3ec8c71 Mon Sep 17 00:00:00 2001 From: RichoKD Date: Sat, 5 Oct 2024 13:51:48 +0100 Subject: [PATCH] counter V2 and V3 --- Scarb.toml | 2 + assignments/solutions/solutions_3.md | 13 ++++ snfoundry.toml | 9 +++ src/counterV2.cairo | 55 ++++++++++++++++ src/counterV3.cairo | 96 ++++++++++++++++++++++++++++ src/intro_to_felt.cairo | 2 +- src/is_even.cairo | 8 +-- src/is_odd.cairo | 4 +- src/lib.cairo | 4 ++ src/utils.cairo | 2 +- 10 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 assignments/solutions/solutions_3.md create mode 100644 snfoundry.toml create mode 100644 src/counterV2.cairo create mode 100644 src/counterV3.cairo diff --git a/Scarb.toml b/Scarb.toml index d60f88b..b4d7134 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -14,3 +14,5 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v [[target.starknet-contract]] sierra = true +[scripts] +test = "snforge test" \ No newline at end of file diff --git a/assignments/solutions/solutions_3.md b/assignments/solutions/solutions_3.md new file mode 100644 index 0000000..0bd05a2 --- /dev/null +++ b/assignments/solutions/solutions_3.md @@ -0,0 +1,13 @@ +### Counter Details + +``` +CounterV2: +class_hash: 0x3a609dd24a6ecef2d8614e4885d9b7923c9db38d83fb9a0134ecfbcc1df8ada +contract: 0x3a07a1e0e8baf948c19b719996fb22dd7d42c5f53dc347c255f81e865885fed +``` + +``` +CounterV3: +class_hash: 0x55152fc1bda27047634c9ca4a3b8a1a8001e8ce1a042337b399df66bf02caf4 +contract: 0xd7e5df67691fdbddc8ac1719361ac9e50f50f28eab0255a8b35caa9db29a85 +``` \ No newline at end of file diff --git a/snfoundry.toml b/snfoundry.toml new file mode 100644 index 0000000..a2df463 --- /dev/null +++ b/snfoundry.toml @@ -0,0 +1,9 @@ +# Visit https://foundry-rs.github.io/starknet-foundry/appendix/snfoundry-toml.html for more information + +[sncast.stark1] # Define a profile name +url = "https://free-rpc.nethermind.io/sepolia-juno/" # Url of the RPC provider +accounts_file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json" # Path to the file with the account data +account = "stark1" # Account from `accounts_file` or default account file that will be used for the transactions +# keystore = "~/keystore" # Path to the keystore file +# wait_params = { timeout = 500, retry_interval = 10 } # Wait for submitted transaction parameters +block_explorer = "StarkScan" # Block explorer service used to display links to transaction details diff --git a/src/counterV2.cairo b/src/counterV2.cairo new file mode 100644 index 0000000..386efb0 --- /dev/null +++ b/src/counterV2.cairo @@ -0,0 +1,55 @@ +#[starknet::interface] +trait ICounterV2 { + fn get_count(self: @TContractState) -> u32; + fn set_count(ref self: TContractState, amount: u32); + + fn increment(ref self: TContractState); + fn decrement(ref self: TContractState); +} + + +#[starknet::contract] +mod CounterV2 { + use starknet::storage::StoragePointerWriteAccess; + use starknet::storage::StoragePointerReadAccess; + use starknet::get_caller_address; + use starknet::ContractAddress; + + #[storage] + struct Storage { + owner: ContractAddress, + count: u32 + } + + #[constructor] + fn constructor(ref self: ContractState) { + self.owner.write(get_caller_address()); + self.count.write(0); + } + + #[abi(embed_v0)] + impl CounterV2Impl of super::ICounterV2 { + fn get_count(self: @ContractState) -> u32 { + self.count.read() + } + + fn set_count(ref self: ContractState, amount: u32) { + if amount != 0 { + self.count.write(amount); + } + } + + fn increment(ref self: ContractState) { + self.count.write(self.count.read() + 1); + } + + fn decrement(ref self: ContractState) { + if self.count.read() == 0 { + return; + } + + self.count.write(self.count.read() - 1); + } + } +} + diff --git a/src/counterV3.cairo b/src/counterV3.cairo new file mode 100644 index 0000000..1ad37cb --- /dev/null +++ b/src/counterV3.cairo @@ -0,0 +1,96 @@ +#[starknet::interface] +trait ICounterV3 { + fn get_count(self: @TContractState) -> u32; + fn set_count(ref self: TContractState, amount: u32); + + fn increment(ref self: TContractState); + fn decrement(ref self: TContractState); +} + + +#[starknet::contract] +mod CounterV3 { + use starknet::storage::StoragePointerWriteAccess; + use starknet::storage::StoragePointerReadAccess; + use starknet::get_caller_address; + use starknet::{ContractAddress, contract_address_const}; + + #[storage] + struct Storage { + owner: ContractAddress, + count: u32 + } + + #[constructor] + fn constructor(ref self: ContractState, _owner: ContractAddress) { + self.owner.write(_owner); + } + + + fn set_new_owner(ref self: ContractState, new_owner: ContractAddress) { + self.only_owner(); + + self.not_address_zero(new_owner); + + self.owner.write(new_owner); + } + + #[generate_trait] + impl Private of PrivateTrait { + fn only_owner(ref self: ContractState) { + if get_caller_address() != self.owner.read() { + return; + } + } + + fn not_address_zero(ref self: ContractState, address: ContractAddress) { + let zero_address: ContractAddress = contract_address_const(); + + if (address == zero_address) { + return; + } + } + } + + #[abi(embed_v0)] + impl CounterV3Impl of super::ICounterV3 { + fn get_count(self: @ContractState) -> u32 { + self.count.read() + } + + // state functions + + fn set_count(ref self: ContractState, amount: u32) { + self.only_owner(); + + assert(amount <= 50, 'exceeds 50'); + + if amount > 50 { + return; + } + + if amount > 0 { + self.count.write(amount); + } + } + + fn increment(ref self: ContractState) { + self.only_owner(); + if self.count.read() == 50 { + return; + } + + self.count.write(self.count.read() + 1); + } + + fn decrement(ref self: ContractState) { + self.only_owner(); + if self.count.read() == 0 { + return; + } + + self.count.write(self.count.read() - 1); + } + } +} + diff --git a/src/intro_to_felt.cairo b/src/intro_to_felt.cairo index d3ca719..cbc929a 100644 --- a/src/intro_to_felt.cairo +++ b/src/intro_to_felt.cairo @@ -5,4 +5,4 @@ pub fn run() -> felt252 { let x = 'SAY GM!'; println!("x value here: {}", x); x -} \ No newline at end of file +} diff --git a/src/is_even.cairo b/src/is_even.cairo index 254be75..cb580b1 100644 --- a/src/is_even.cairo +++ b/src/is_even.cairo @@ -3,7 +3,7 @@ pub fn run(x: u8, y: u8) -> bool { let mymod = sum_u8::run(x, y) % 2; if mymod == 0 { true - } else { - false - } -} \ No newline at end of file + } else { + false + } +} diff --git a/src/is_odd.cairo b/src/is_odd.cairo index 5f6638f..5fa3851 100644 --- a/src/is_odd.cairo +++ b/src/is_odd.cairo @@ -1,8 +1,8 @@ pub fn run(x: u8) -> bool { let mut mymod = x % 2; if mymod != 0 { - true + true } else { false } -} \ No newline at end of file +} diff --git a/src/lib.cairo b/src/lib.cairo index 6670f9f..7c289bc 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -5,6 +5,10 @@ mod intro_to_bytearray; mod is_odd; pub mod utils; +mod counterV2; +mod counterV3; + + fn main() { intro_to_felt::run(); intro_to_u8::run(5, 5); diff --git a/src/utils.cairo b/src/utils.cairo index cf4285f..773b0d5 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -10,4 +10,4 @@ pub fn high_to_low(x: u16) -> u8 { pub fn sum_u8(x: u8, y: u8) -> u8 { x + y -} \ No newline at end of file +}