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
1 change: 1 addition & 0 deletions starknet_contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.11.4
2 changes: 1 addition & 1 deletion starknet_contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
]

[[package]]
name = "Starknet_contracts"
name = "starknet_contracts"
version = "0.1.0"
dependencies = [
"snforge_std",
Expand Down
6 changes: 3 additions & 3 deletions starknet_contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[package]
name = "Starknet_contracts"
name = "starknet_contracts"
version = "0.1.0"
edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
Starknet = "2.11.4"
starknet = "2.11.4"

[dev-dependencies]
snforge_std = "0.43.1"
assert_macros = "2.11.4"

[[target.Starknet-contract]]
[[target.starknet-contract]]
sierra = true

[scripts]
Expand Down
71 changes: 48 additions & 23 deletions starknet_contracts/src/contracts/HelloStarknet.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/// Simple contract for managing balance.
#[Starknet::contract]
#[starknet::contract]
pub mod HelloStarknet {

use Starknet_contracts::interfaces::IHelloStarknet::IHelloStarknet;
// use Starknet::storage::{StoragePointerReadAccess, StoragePathEntry, StoragePointerWriteAccess, Map };
use Starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess, StoragePointerWriteAccess };
use Starknet::{ContractAddress, get_caller_address};
use starknet::storage::*;
use starknet::{ContractAddress, get_caller_address};
use crate::interfaces::IHelloStarknet::IHelloStarknet; // <--- correct import

#[storage]
struct Storage {
Expand All @@ -14,15 +11,16 @@ pub mod HelloStarknet {
}

#[event]
#[derive(Drop, Starknet::Event)]
#[derive(Drop, starknet::Event)]
pub enum Event {
Balance : BalanceIncreased,
Balance: BalanceUpdated,
}

#[derive(Drop, Starknet::Event)]
pub struct BalanceIncreased {
#[derive(Drop, starknet::Event)]
pub struct BalanceUpdated {
pub caller: ContractAddress,
pub amount: felt252,
pub old_amount: felt252,
pub new_amount: felt252,
}

#[abi(embed_v0)]
Expand All @@ -31,22 +29,49 @@ pub mod HelloStarknet {
assert(amount != 0, 'Amount cannot be 0');
let caller = get_caller_address();

let updated_amount = self.balance.read() + amount;
self.balance.write(updated_amount);
let old_total = self.balance.read();
let new_total = old_total + amount;
self.balance.write(new_total);

// let unique_balance = self.balances.entry(caller).read();
let old_unique = self.balances.read(caller);
let new_unique = old_unique + amount;
self.balances.write(caller, new_unique);

let unique_balance = self.balances.read(caller);
// self.balances.entry(caller).write(unique_balance + amount);
self.balances.write(caller, unique_balance + amount);

// self.balance.write(self.balance.read() + amount);

self.emit(BalanceIncreased{caller, amount});
self.emit(Event::Balance(BalanceUpdated { caller, old_amount: old_unique, new_amount: new_unique }));
}

fn get_balance(self: @ContractState) -> felt252 {
self.balance.read()
}

fn get_unique_balance(self: @ContractState, addr: ContractAddress) -> felt252 {
self.balances.read(addr)
}

fn set_balance(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
let caller = get_caller_address();

let old_unique = self.balances.read(caller);
self.balances.write(caller, amount);

let old_total = self.balance.read();
let new_total = old_total + amount - old_unique;
self.balance.write(new_total);

self.emit(Event::Balance(BalanceUpdated { caller, old_amount: old_unique, new_amount: amount }));
}

fn reset_balance(ref self: ContractState) {
let caller = get_caller_address();
let old_unique = self.balances.read(caller);

let total = self.balance.read();
self.balance.write(total - old_unique);

self.balances.write(caller, 0);

self.emit(Event::Balance(BalanceUpdated { caller, old_amount: old_unique, new_amount: 0 }));
}
}
}
}
88 changes: 44 additions & 44 deletions starknet_contracts/src/contracts/counter.cairo
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
#[Starknet::contract]
pub mod Counter {
// use Starknet::ContractAddress;
// use Starknet::get_caller_address;
use Starknet_contracts::interfaces::ICounter::ICounter;
use Starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
// #[Starknet::contract]
// pub mod Counter {
// use starknet::ContractAddress;
// use starknet::get_caller_address;
// use starknet_contracts::interfaces::ICounter::ICounter;
// use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

#[storage]
struct Storage {
count: u32,
}
// #[storage]
// struct Storage {
// count: u32,
// }

#[event]
#[derive(Drop, Starknet::Event)]
pub enum Event {
CountUpdated : CountUpdated,
}
// #[event]
// #[derive(Drop, Starknet::Event)]
// pub enum Event {
// CountUpdated : CountUpdated,
// }

#[derive(Drop, Starknet::Event)]
struct CountUpdated {
old_value: u32,
new_value: u32,
}
// #[derive(Drop, Starknet::Event)]
// struct CountUpdated {
// old_value: u32,
// new_value: u32,
// }

#[constructor]
fn constructor(ref self: ContractState) {
self.count.write(0);
}
// #[constructor]
// fn constructor(ref self: ContractState) {
// self.count.write(0);
// }

#[abi(embed_v0)]
impl CounterImpl of ICounter<ContractState> {
fn get_count(self: @ContractState) -> u32 {
self.count.read()
}
// #[abi(embed_v0)]
// impl CounterImpl of ICounter<ContractState> {
// fn get_count(self: @ContractState) -> u32 {
// self.count.read()
// }

fn increment(ref self: ContractState) {
let old_value = self.count.read();
let new_value = old_value + 1;
self.count.write(new_value);
self.emit(CountUpdated { old_value, new_value });
}
// fn increment(ref self: ContractState) {
// let old_value = self.count.read();
// let new_value = old_value + 1;
// self.count.write(new_value);
// self.emit(CountUpdated { old_value, new_value });
// }

fn decrement(ref self: ContractState) {
let old_value = self.count.read();
assert(old_value > 0, 'Count cannot be negative');
let new_value = old_value - 1;
self.count.write(new_value);
self.emit(CountUpdated { old_value, new_value });
}
}
}
// fn decrement(ref self: ContractState) {
// let old_value = self.count.read();
// assert(old_value > 0, 'Count cannot be negative');
// let new_value = old_value - 1;
// self.count.write(new_value);
// self.emit(CountUpdated { old_value, new_value });
// }
// }
// }
12 changes: 6 additions & 6 deletions starknet_contracts/src/interfaces/ICounter.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[Starknet::interface]
pub trait ICounter<TContractState> {
fn get_count(self: @TContractState) -> u32;
fn increment(ref self: TContractState);
fn decrement(ref self: TContractState);
}
// #[Starknet::interface]
// pub trait ICounter<TContractState> {
// fn get_count(self: @TContractState) -> u32;
// fn increment(ref self: TContractState);
// fn decrement(ref self: TContractState);
// }
12 changes: 7 additions & 5 deletions starknet_contracts/src/interfaces/IHelloStarknet.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use starknet::ContractAddress; // <--- this was missing

/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract balance.
#[Starknet::interface]
#[starknet::interface]
pub trait IHelloStarknet<TContractState> {
/// Increase contract balance.
fn increase_balance(ref self: TContractState, amount: felt252);
/// Retrieve contract balance.
fn get_balance(self: @TContractState) -> felt252;
}
fn get_unique_balance(self: @TContractState, addr: ContractAddress) -> felt252;
fn set_balance(ref self: TContractState, amount: felt252);
fn reset_balance(ref self: TContractState);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions tasks/Task-5-b/sncast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
![Contract Deployment](./screenshots/deployment.png)

![Call Function](./screenshots/deployment.png)

![Increment1](./screenshots/increment.png)

![Increment2](./screenshots/increment2.png)

![Get Count1](./screenshots/get_count.png)

![Decrement](./screenshots/get_count.png)

![Get Count2](./screenshots/get_count.png)
3 changes: 3 additions & 0 deletions tasks/session-5-task-a.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ sncast invoke \
--arguments '<PEER_WALLET_ADDRESS>, 10000000000000000000'
```

#### MY TRANSACTION HASH
https://sepolia.starkscan.co/tx/0x04178df11f3e300623d54366db9afe7b5c10cdd1f739a64529fe710251c59ca0


##### Submission

Expand Down