Run the Example
First go through the Setup process to get your development environment configured, then clone thev23.0.0 tag of soroban-examples repository:
increment directory, and use cargo test.
Code
increment/src/lib.rs
How it Works
This contract will get a counter value from storage (or use the value 0 if no value has been stored yet), and increment this counter every time it’s called. Open theincrement/src/lib.rs file or see the code above to follow along.
Contract Data Key
Contract data is associated with a key, which can be used at a later time to look up the value.Symbol is a short (up to 32 characters long) string type with limited character space (only a-zA-Z0-9_ characters are allowed). Identifiers like contract function names and contract data keys are represented by Symbols.
The symbol_short!() macro is a convenient way to pre-compute short symbols up to 9 characters in length at compile time using Symbol::short. It generates a compile-time constant that adheres to the valid character set of letters (a-zA-Z), numbers (0-9), and underscores (_). If a symbol exceeds the 9-character limit, Symbol::new should be utilized for creating symbols at runtime.
Contract Data Access
env.storage() function is used to access and update contract data. The executing contract is the only contract that can query or modify contract data that it has stored. The data stored is viewable on ledger anywhere the ledger is viewable, but contracts executing within the Soroban environment are restricted to their own data.
The get() function gets the current value associated with the counter key.
If no value is currently stored, the value given to unwrap_or(...) is returned instead.
Values stored as contract data and retrieved are transmitted from the environment and expanded into the type specified. In this case a u32. If the value can be expanded, the type returned will be a u32. Otherwise, if a developer cast it to be some other type, a panic would occur at the unwrap.
set() function stores the new count value against the key, replacing the existing value.
Managing Contract Data TTLs with extend_ttl()
Persistent, Temporary, and Instance. This contract only uses Instance storage: env.storage().instance(). Every time the counter is incremented, this storage’s TTL gets extended by 100 ledgers, or about 500 seconds.
Tests
Open theincrement/src/test.rs file to follow along.
increment/src/test.rs
Env, which is the Soroban environment that the contract will run in.
impl block that is annotated with the #[contractimpl] attribute have a corresponding function generated in a generated client type. The client type will be named the same as the contract type with Client appended.
Build the Contracts
To build the contract into a.wasm file, use the stellar contract build command.
.wasm file should be found in the contract target directory after building the contract:
Run the Contract
If you havestellar-cli installed, you can deploy the contract, and invoke the contract function.
Deploy
- macOS/Linux
- Windows (PowerShell)
Invoke
- macOS/Linux
- Windows (PowerShell)