Simple contract that defines a count value in storage that can be incremented, decremented, and set to a specific number.
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2extern crate alloc;
3
4#[global_allocator]
5static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
6
7use stylus_sdk::{alloy_primitives::U256, prelude::*, storage::StorageU256};
8
9/// The solidity_storage macro allows this struct to be used in persistent
10/// storage. It accepts fields that implement the StorageType trait. Built-in
11/// storage types for Solidity ABI primitives are found under
12/// stylus_sdk::storage.
13#[solidity_storage]
14/// The entrypoint macro defines where Stylus execution begins. External methods
15/// are exposed by annotating an impl for this struct with #[external] as seen
16/// below.
17#[entrypoint]
18pub struct Counter {
19 count: StorageU256,
20}
21
22/// Define an implementation of the Counter struct, defining a set_count
23/// as well as inc and dec methods using the features of the Stylus SDK.
24#[external]
25impl Counter {
26 /// Gets the number from storage.
27 pub fn get(&self) -> Result<U256, Vec<u8>> {
28 Ok(self.count.get())
29 }
30
31 /// Sets the count in storage to a user-specified value.
32 pub fn set_count(&mut self, count: U256) -> Result<(), Vec<u8>> {
33 self.count.set(count);
34 Ok(())
35 }
36
37 /// Increments count by 1
38 pub fn inc(&mut self) -> Result<(), Vec<u8>> {
39 let count = self.count.get() + U256::from(1);
40 self.set_count(count)
41 }
42
43 /// Decrements count by 1
44 pub fn dec(&mut self) -> Result<(), Vec<u8>> {
45 let count = self.count.get() - U256::from(1);
46 self.set_count(count)
47 }
48}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2extern crate alloc;
3
4#[global_allocator]
5static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
6
7use stylus_sdk::{alloy_primitives::U256, prelude::*, storage::StorageU256};
8
9/// The solidity_storage macro allows this struct to be used in persistent
10/// storage. It accepts fields that implement the StorageType trait. Built-in
11/// storage types for Solidity ABI primitives are found under
12/// stylus_sdk::storage.
13#[solidity_storage]
14/// The entrypoint macro defines where Stylus execution begins. External methods
15/// are exposed by annotating an impl for this struct with #[external] as seen
16/// below.
17#[entrypoint]
18pub struct Counter {
19 count: StorageU256,
20}
21
22/// Define an implementation of the Counter struct, defining a set_count
23/// as well as inc and dec methods using the features of the Stylus SDK.
24#[external]
25impl Counter {
26 /// Gets the number from storage.
27 pub fn get(&self) -> Result<U256, Vec<u8>> {
28 Ok(self.count.get())
29 }
30
31 /// Sets the count in storage to a user-specified value.
32 pub fn set_count(&mut self, count: U256) -> Result<(), Vec<u8>> {
33 self.count.set(count);
34 Ok(())
35 }
36
37 /// Increments count by 1
38 pub fn inc(&mut self) -> Result<(), Vec<u8>> {
39 let count = self.count.get() + U256::from(1);
40 self.set_count(count)
41 }
42
43 /// Decrements count by 1
44 pub fn dec(&mut self) -> Result<(), Vec<u8>> {
45 let count = self.count.get() - U256::from(1);
46 self.set_count(count)
47 }
48}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3#[cfg(feature = "export-abi")]
4fn main() {
5 stylus_counter::main();
6}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3#[cfg(feature = "export-abi")]
4fn main() {
5 stylus_counter::main();
6}
1[package]
2name = "stylus-counter"
3version = "0.1.5"
4edition = "2021"
5
6[dependencies]
7alloy-primitives = "0.3.1"
8alloy-sol-types = "0.3.1"
9stylus-sdk = "0.4.1"
10hex = "0.4.3"
11wee_alloc = "0.4.5"
12
13[dev-dependencies]
14tokio = { version = "1.12.0", features = ["full"] }
15ethers = "2.0"
16eyre = "0.6.8"
17
18[features]
19export-abi = ["stylus-sdk/export-abi"]
20
21[[bin]]
22name = "stylus-counter"
23path = "src/main.rs"
24
25[lib]
26crate-type = ["lib", "cdylib"]
1[package]
2name = "stylus-counter"
3version = "0.1.5"
4edition = "2021"
5
6[dependencies]
7alloy-primitives = "0.3.1"
8alloy-sol-types = "0.3.1"
9stylus-sdk = "0.4.1"
10hex = "0.4.3"
11wee_alloc = "0.4.5"
12
13[dev-dependencies]
14tokio = { version = "1.12.0", features = ["full"] }
15ethers = "2.0"
16eyre = "0.6.8"
17
18[features]
19export-abi = ["stylus-sdk/export-abi"]
20
21[[bin]]
22name = "stylus-counter"
23path = "src/main.rs"
24
25[lib]
26crate-type = ["lib", "cdylib"]