Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counter V2 and V3 #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions assignments/solutions/solutions_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Counter Details

```
CounterV2:
class_hash: 0x3a609dd24a6ecef2d8614e4885d9b7923c9db38d83fb9a0134ecfbcc1df8ada
contract: 0x3a07a1e0e8baf948c19b719996fb22dd7d42c5f53dc347c255f81e865885fed
```

```
CounterV3:
class_hash: 0x55152fc1bda27047634c9ca4a3b8a1a8001e8ce1a042337b399df66bf02caf4
contract: 0xd7e5df67691fdbddc8ac1719361ac9e50f50f28eab0255a8b35caa9db29a85
```
9 changes: 9 additions & 0 deletions snfoundry.toml
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions src/counterV2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[starknet::interface]
trait ICounterV2<TContractState> {
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<ContractState> {
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);
}
}
}

96 changes: 96 additions & 0 deletions src/counterV3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#[starknet::interface]
trait ICounterV3<TContractState> {
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<ContractState> {
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);
}
}
}

2 changes: 1 addition & 1 deletion src/intro_to_felt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub fn run() -> felt252 {
let x = 'SAY GM!';
println!("x value here: {}", x);
x
}
}
8 changes: 4 additions & 4 deletions src/is_even.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
} else {
false
}
}
4 changes: 2 additions & 2 deletions src/is_odd.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub fn run(x: u8) -> bool {
let mut mymod = x % 2;
if mymod != 0 {
true
true
} else {
false
}
}
}
4 changes: 4 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub fn high_to_low(x: u16) -> u8 {

pub fn sum_u8(x: u8, y: u8) -> u8 {
x + y
}
}