Arbitrum Stylus logo

Stylus by Example

First App

Simple contract that defines a count value in storage that can be incremented, decremented, and set to a specific number.

Learn More

src/lib.rs

1#![cfg_attr(not(feature = "export-abi"), no_main)]
2extern crate alloc;
3
4use stylus_sdk::{alloy_primitives::U256, prelude::*, storage::StorageU256};
5
6/// The solidity_storage macro allows this struct to be used in persistent
7/// storage. It accepts fields that implement the StorageType trait. Built-in
8/// storage types for Solidity ABI primitives are found under
9/// stylus_sdk::storage.
10#[storage]
11/// The entrypoint macro defines where Stylus execution begins. External methods
12/// are exposed by annotating an impl for this struct with #[external] as seen
13/// below.
14#[entrypoint]
15pub struct Counter {
16    count: StorageU256,
17}
18
19/// Define an implementation of the Counter struct, defining a set_count
20/// as well as inc and dec methods using the features of the Stylus SDK.
21#[public]
22impl Counter {
23    /// Gets the number from storage.
24    pub fn get(&self) -> Result<U256, Vec<u8>> {
25        Ok(self.count.get())
26    }
27
28    /// Sets the count in storage to a user-specified value.
29    pub fn set_count(&mut self, count: U256) -> Result<(), Vec<u8>> {
30        self.count.set(count);
31        Ok(())
32    }
33
34    /// Increments count by 1
35    pub fn inc(&mut self) -> Result<(), Vec<u8>> {
36        let count = self.count.get() + U256::from(1);
37        self.set_count(count)
38    }
39
40    /// Decrements count by 1
41    pub fn dec(&mut self) -> Result<(), Vec<u8>> {
42        let count = self.count.get() - U256::from(1);
43        self.set_count(count)
44    }
45}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2extern crate alloc;
3
4use stylus_sdk::{alloy_primitives::U256, prelude::*, storage::StorageU256};
5
6/// The solidity_storage macro allows this struct to be used in persistent
7/// storage. It accepts fields that implement the StorageType trait. Built-in
8/// storage types for Solidity ABI primitives are found under
9/// stylus_sdk::storage.
10#[storage]
11/// The entrypoint macro defines where Stylus execution begins. External methods
12/// are exposed by annotating an impl for this struct with #[external] as seen
13/// below.
14#[entrypoint]
15pub struct Counter {
16    count: StorageU256,
17}
18
19/// Define an implementation of the Counter struct, defining a set_count
20/// as well as inc and dec methods using the features of the Stylus SDK.
21#[public]
22impl Counter {
23    /// Gets the number from storage.
24    pub fn get(&self) -> Result<U256, Vec<u8>> {
25        Ok(self.count.get())
26    }
27
28    /// Sets the count in storage to a user-specified value.
29    pub fn set_count(&mut self, count: U256) -> Result<(), Vec<u8>> {
30        self.count.set(count);
31        Ok(())
32    }
33
34    /// Increments count by 1
35    pub fn inc(&mut self) -> Result<(), Vec<u8>> {
36        let count = self.count.get() + U256::from(1);
37        self.set_count(count)
38    }
39
40    /// Decrements count by 1
41    pub fn dec(&mut self) -> Result<(), Vec<u8>> {
42        let count = self.count.get() - U256::from(1);
43        self.set_count(count)
44    }
45}

src/main.rs

1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3#[cfg(feature = "export-abi")]
4fn main() {
5    stylus_counter::print_abi("MIT-OR-APACHE-2.0", "pragma solidity ^0.8.23;");
6}
1#![cfg_attr(not(feature = "export-abi"), no_main)]
2
3#[cfg(feature = "export-abi")]
4fn main() {
5    stylus_counter::print_abi("MIT-OR-APACHE-2.0", "pragma solidity ^0.8.23;");
6}

Cargo.toml

1[package]
2name = "stylus-counter"
3version = "0.1.7"
4edition = "2021"
5
6[dependencies]
7alloy-primitives = "=0.7.6"
8alloy-sol-types = "=0.7.6"
9stylus-sdk = "0.6.0"
10hex = "0.4.3"
11
12[dev-dependencies]
13tokio = { version = "1.12.0", features = ["full"] }
14ethers = "2.0"
15eyre = "0.6.8"
16
17[features]
18export-abi = ["stylus-sdk/export-abi"]
19
20[[bin]]
21name = "stylus-counter"
22path = "src/main.rs"
23
24[lib]
25crate-type = ["lib", "cdylib"]
1[package]
2name = "stylus-counter"
3version = "0.1.7"
4edition = "2021"
5
6[dependencies]
7alloy-primitives = "=0.7.6"
8alloy-sol-types = "=0.7.6"
9stylus-sdk = "0.6.0"
10hex = "0.4.3"
11
12[dev-dependencies]
13tokio = { version = "1.12.0", features = ["full"] }
14ethers = "2.0"
15eyre = "0.6.8"
16
17[features]
18export-abi = ["stylus-sdk/export-abi"]
19
20[[bin]]
21name = "stylus-counter"
22path = "src/main.rs"
23
24[lib]
25crate-type = ["lib", "cdylib"]