Skip to content

Commit

Permalink
Furnic/docs counter example (FuelLabs#958)
Browse files Browse the repository at this point in the history
* docs:add counter example to sway book

* docs:add counter to examples

* style: forc fmt

* docs: actually add new page
  • Loading branch information
nfurfaro authored Mar 17, 2022
1 parent da11f37 commit d357815
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [The Sway Toolchain](./introduction/sway-toolchain.md)
- [A Forc Project](./introduction/forc_project.md)
- [Examples](./examples/index.md)
- [Counter](./examples/counter.md)
- [Subcurrency](./examples/subcurrency.md)
- [FizzBuzz](./examples/fizzbuzz.md)
- [Wallet Smart Contract](./examples/wallet_smart_contract.md)
Expand Down
7 changes: 7 additions & 0 deletions docs/src/examples/counter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Counter

The following is a simple example of a contract which implements a counter. Both the `initialize()` and `increment()` functions return the currently set value.

```sway
{{#include ../../../examples/counter/src/main.sw}}
```
1 change: 1 addition & 0 deletions docs/src/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Some basic example contracts to see how Sway and Forc work.

- [Counter](./counter.md)
- [Subcurrency](./subcurrency.md)
- [FizzBuzz](./fizzbuzz.md)
- [Wallet Smart Contract](./wallet_smart_contract.md)
2 changes: 2 additions & 0 deletions examples/counter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
21 changes: 21 additions & 0 deletions examples/counter/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[[package]]
name = 'core'
source = 'git+http://github.com/FuelLabs/sway-lib-core?reference=master#30274cf817c1848e28f984c2e8703eb25e7a3a44'
dependencies = []

[[package]]
name = 'core'
source = 'git+https://github.com/FuelLabs/sway-lib-core?reference=master#30274cf817c1848e28f984c2e8703eb25e7a3a44'
dependencies = []

[[package]]
name = 'counter'
dependencies = [
'core git+http://github.com/FuelLabs/sway-lib-core?reference=master#30274cf817c1848e28f984c2e8703eb25e7a3a44',
'std git+http://github.com/FuelLabs/sway-lib-std?reference=master#6a8f5bf588df5d0679fb834f05c900f2e54de426',
]

[[package]]
name = 'std'
source = 'git+http://github.com/FuelLabs/sway-lib-std?reference=master#6a8f5bf588df5d0679fb834f05c900f2e54de426'
dependencies = ['core git+https://github.com/FuelLabs/sway-lib-core?reference=master#30274cf817c1848e28f984c2e8703eb25e7a3a44']
9 changes: 9 additions & 0 deletions examples/counter/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "counter"

[dependencies]
core = { git = "http://github.com/FuelLabs/sway-lib-core" }
std = { git = "http://github.com/FuelLabs/sway-lib-std" }
22 changes: 22 additions & 0 deletions examples/counter/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
contract;

use std::storage::{get, store};

const KEY = 0x0000000000000000000000000000000000000000000000000000000000000000;

abi Incrementor {
fn initialize(initial_value: u64) -> u64;
fn increment(initial_value: u64) -> u64;
}

impl Incrementor for Contract {
fn initialize(initial_value: u64) -> u64 {
store(KEY, initial_value);
initial_value
}
fn increment(increment_by: u64) -> u64 {
let new_val = get::<u64>(KEY) + 1;
store(KEY, new_val);
new_val
}
}

0 comments on commit d357815

Please sign in to comment.