From 9d063f40ef6f0c27dec6d4f63590e69c5722ba6e Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Tue, 27 Jun 2023 12:39:33 -0400 Subject: [PATCH 1/3] docs(counter): add doc + summary --- src/SUMMARY.md | 3 ++- src/ch00-11-counter.md | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/ch00-11-counter.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 113a2061..869aa2e1 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) \ No newline at end of file + - [Defi Vault](./ch01-02-simple_vault.md) diff --git a/src/ch00-11-counter.md b/src/ch00-11-counter.md new file mode 100644 index 00000000..7ad35c70 --- /dev/null +++ b/src/ch00-11-counter.md @@ -0,0 +1,14 @@ +# Simple Counter + +This is the Cairo adaptation of the [Solidity by example First Application](https://solidity-by-example.org/first-app/). +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}} +``` From ff366cd8fac7974c045199b7181dec845d21ebdb Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Tue, 27 Jun 2023 12:59:51 -0400 Subject: [PATCH 2/3] feat(counter): add counter pkg --- listings/ch00-introduction/counter/.gitignore | 1 + listings/ch00-introduction/counter/Scarb.toml | 8 +++++++ .../counter/src/counter.cairo | 24 +++++++++++++++++++ .../ch00-introduction/counter/src/lib.cairo | 1 + 4 files changed, 34 insertions(+) create mode 100644 listings/ch00-introduction/counter/.gitignore create mode 100644 listings/ch00-introduction/counter/Scarb.toml create mode 100644 listings/ch00-introduction/counter/src/counter.cairo create mode 100644 listings/ch00-introduction/counter/src/lib.cairo diff --git a/listings/ch00-introduction/counter/.gitignore b/listings/ch00-introduction/counter/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/listings/ch00-introduction/counter/.gitignore @@ -0,0 +1 @@ +target diff --git a/listings/ch00-introduction/counter/Scarb.toml b/listings/ch00-introduction/counter/Scarb.toml new file mode 100644 index 00000000..2e5938be --- /dev/null +++ b/listings/ch00-introduction/counter/Scarb.toml @@ -0,0 +1,8 @@ +[package] +name = "counter" +version = "0.1.0" + +[dependencies] +starknet = "1.1.0" + +[[target.starknet-contract]] diff --git a/listings/ch00-introduction/counter/src/counter.cairo b/listings/ch00-introduction/counter/src/counter.cairo new file mode 100644 index 00000000..e230fb8e --- /dev/null +++ b/listings/ch00-introduction/counter/src/counter.cairo @@ -0,0 +1,24 @@ +#[contract] +mod SimpleCounter { + struct Storage { + // Counter variable + _counter: u256, + } + + #[constructor] + fn constructor() {} + + #[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); + } +} diff --git a/listings/ch00-introduction/counter/src/lib.cairo b/listings/ch00-introduction/counter/src/lib.cairo new file mode 100644 index 00000000..ba8e24da --- /dev/null +++ b/listings/ch00-introduction/counter/src/lib.cairo @@ -0,0 +1 @@ +mod counter; \ No newline at end of file From 9d44e1dfa45afbd9e097be8d897fa020590af704 Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Tue, 27 Jun 2023 16:45:46 -0400 Subject: [PATCH 3/3] fix(counter): address pr review changes --- listings/ch00-introduction/counter/src/counter.cairo | 5 +++++ src/ch00-11-counter.md | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/listings/ch00-introduction/counter/src/counter.cairo b/listings/ch00-introduction/counter/src/counter.cairo index e230fb8e..822965ce 100644 --- a/listings/ch00-introduction/counter/src/counter.cairo +++ b/listings/ch00-introduction/counter/src/counter.cairo @@ -8,6 +8,11 @@ mod SimpleCounter { #[constructor] fn constructor() {} + #[view] + fn get_current_count() -> u256 { + return _counter::read(); + } + #[external] fn increment() { // Store counter value + 1 diff --git a/src/ch00-11-counter.md b/src/ch00-11-counter.md index 7ad35c70..b67017c2 100644 --- a/src/ch00-11-counter.md +++ b/src/ch00-11-counter.md @@ -1,6 +1,7 @@ # Simple Counter -This is the Cairo adaptation of the [Solidity by example First Application](https://solidity-by-example.org/first-app/). +This is a smiple counter contract. + Here's how it works: - The contract has a state variable called 'counter' that is initialized to 0.