Arbitrum Stylus logo

Stylus by Example

Constants

Constants are values that are bound to a name and cannot change. They are always immutable. In Rust, constants are declared with the const keyword. Unlike variables declared with the let keyword, constants must be annotated with their type.

Constants are valid for the entire length of the transaction. They are essentially inlined wherever they are used, meaning that their value is copied directly into whatever context invokes them.

Since their value is hardcoded, they can save on gas cost as their value does not need to be fetched from storage.

Learn More

src/lib.rs

1// Only run this as a WASM if the export-abi feature is not set.
2#![cfg_attr(not(any(feature = "export-abi", test)), no_main)]
3extern crate alloc;
4
5use alloc::vec;
6use alloc::vec::Vec;
7
8use stylus_sdk::alloy_primitives::Address;
9use stylus_sdk::prelude::*;
10use stylus_sdk::storage::StorageAddress;
11
12const OWNER: &str = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
13
14#[storage]
15#[entrypoint]
16pub struct Contract {
17    owner: StorageAddress,
18}
19
20#[public]
21impl Contract {
22    pub fn init(&mut self) -> Result<(), Vec<u8>> {
23        // Parse the const &str as a local Address variable
24        let owner_address = Address::parse_checksummed(OWNER, None).expect("Invalid address");
25
26        // Save the result as the owner
27        self.owner.set(owner_address);
28
29        Ok(())
30    }
31    pub fn owner(&self) -> Result<Address, Vec<u8>> {
32        let owner_address = self.owner.get();
33
34        Ok(owner_address)
35    }
36}
1// Only run this as a WASM if the export-abi feature is not set.
2#![cfg_attr(not(any(feature = "export-abi", test)), no_main)]
3extern crate alloc;
4
5use alloc::vec;
6use alloc::vec::Vec;
7
8use stylus_sdk::alloy_primitives::Address;
9use stylus_sdk::prelude::*;
10use stylus_sdk::storage::StorageAddress;
11
12const OWNER: &str = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
13
14#[storage]
15#[entrypoint]
16pub struct Contract {
17    owner: StorageAddress,
18}
19
20#[public]
21impl Contract {
22    pub fn init(&mut self) -> Result<(), Vec<u8>> {
23        // Parse the const &str as a local Address variable
24        let owner_address = Address::parse_checksummed(OWNER, None).expect("Invalid address");
25
26        // Save the result as the owner
27        self.owner.set(owner_address);
28
29        Ok(())
30    }
31    pub fn owner(&self) -> Result<Address, Vec<u8>> {
32        let owner_address = self.owner.get();
33
34        Ok(owner_address)
35    }
36}

Cargo.toml

1[package]
2name = "stylus_constants_example"
3version = "0.1.7"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
7
8[dependencies]
9alloy-primitives = "=0.7.6"
10alloy-sol-types = "=0.7.6"
11mini-alloc = "0.4.2"
12stylus-sdk = "0.6.0"
13hex = "0.4.3"
14
15[dev-dependencies]
16tokio = { version = "1.12.0", features = ["full"] }
17ethers = "2.0"
18eyre = "0.6.8"
19
20[features]
21export-abi = ["stylus-sdk/export-abi"]
22
23[lib]
24crate-type = ["lib", "cdylib"]
25
26[profile.release]
27codegen-units = 1
28strip = true
29lto = true
30panic = "abort"
31opt-level = "s"
1[package]
2name = "stylus_constants_example"
3version = "0.1.7"
4edition = "2021"
5license = "MIT OR Apache-2.0"
6keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
7
8[dependencies]
9alloy-primitives = "=0.7.6"
10alloy-sol-types = "=0.7.6"
11mini-alloc = "0.4.2"
12stylus-sdk = "0.6.0"
13hex = "0.4.3"
14
15[dev-dependencies]
16tokio = { version = "1.12.0", features = ["full"] }
17ethers = "2.0"
18eyre = "0.6.8"
19
20[features]
21export-abi = ["stylus-sdk/export-abi"]
22
23[lib]
24crate-type = ["lib", "cdylib"]
25
26[profile.release]
27codegen-units = 1
28strip = true
29lto = true
30panic = "abort"
31opt-level = "s"