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 #47

Merged
merged 3 commits into from
Jun 28, 2023
Merged
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 listings/ch00-introduction/counter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
8 changes: 8 additions & 0 deletions listings/ch00-introduction/counter/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "counter"
version = "0.1.0"

[dependencies]
starknet = "1.1.0"

[[target.starknet-contract]]
29 changes: 29 additions & 0 deletions listings/ch00-introduction/counter/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#[contract]
mod SimpleCounter {
struct Storage {
// Counter variable
_counter: u256,
}

#[constructor]
fn constructor() {}

#[view]
fn get_current_count() -> u256 {
return _counter::read();
}

#[external]
fn increment() {
// Store counter value + 1
let counter: u256 = _counter::read() + 1;
_counter::write(counter);
}

#[external]
fn decrement() {
// Store counter value - 1
let counter: u256 = _counter::read() - 1;
_counter::write(counter);
}
HansBhatia marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions listings/ch00-introduction/counter/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod counter;
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Summary
- [Function Attributes](./ch00-08-function_attributes.md)
- [If statements](./ch00-09-if_statements.md)
- [Events](./ch00-10-events.md)
- [Counter](./ch00-11-counter.md)

- [Applications](./ch01-00-applications.md)
- [Upgradeable Contract](./ch01-01-upgradeable_contract.md)
- [Defi Vault](./ch01-02-simple_vault.md)
- [Defi Vault](./ch01-02-simple_vault.md)
15 changes: 15 additions & 0 deletions src/ch00-11-counter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Simple Counter

This is a smiple counter contract.

Here's how it works:

- The contract has a state variable called 'counter' that is initialized to 0.

- When a user calls 'increment', the contract increments the counter by 1.

- When a user calls 'decrement', the contract decrements the counter by 1.

```rust
{{#include ../listings/ch00-introduction/counter/src/counter.cairo}}
```