Soroban provides a set of specialized types optimized for smart contract development. These types are designed to be efficient in the WASM environment and integrate seamlessly with the host environment.
use soroban_sdk::{Address, Env};pub fn use_addresses(env: Env) { // From strkey (G... for accounts, C... for contracts) let addr = Address::from_str(&env, "GABC123..."); // Get current contract address let self_addr = env.current_contract_address();}
pub fn address_operations(env: Env, addr: Address) { // Require authorization addr.require_auth(); // Convert to string let strkey = addr.to_string(); // Check if contract/account exists let exists = addr.exists(); // Get executable type (Wasm, StellarAsset, or Account) let executable = addr.executable();}
In tests, use Address::generate(&env) to create random addresses for testing.
use soroban_sdk::{Symbol, symbol_short};// Compile-time symbol (≤ 9 chars, more efficient)let short_sym = symbol_short!("balance");// Runtime symbol (up to 32 chars)let long_sym = Symbol::new(&env, "very_long_symbol_name_here");
use soroban_sdk::{Bytes, bytes};pub fn bytes_example(env: Env) { // Create from array let b1 = Bytes::from_array(&env, &[1, 2, 3, 4]); // Create from hex literal let b2 = bytes!(&env, 0xdeadbeef); // Operations let len = b1.len(); let first = b1.get(0).unwrap(); let sliced = b1.slice(1..3); // Append let mut b3 = b1.clone(); b3.append(&b2);}
use soroban_sdk::{vec, Vec, Symbol};pub fn vec_example(env: Env) { // Create with vec! macro let v1 = vec![&env, 1, 2, 3, 4, 5]; // Create and push let mut v2 = Vec::new(&env); v2.push_back(10); v2.push_back(20); // Access elements let first = v1.get(0).unwrap(); // Iterate for item in v1.iter() { // Process item } // Length and operations let len = v1.len(); let sliced = v1.slice(1..4);}
use soroban_sdk::String;pub fn string_example(env: Env) { // Create from str let s1 = String::from_str(&env, "Hello, Soroban!"); // Length let len = s1.len(); // Convert to Symbol (if short enough) let sym = Symbol::new(&env, "short");}
String should be used sparingly in contracts as strings are expensive to process. Prefer Symbol for identifiers.
use soroban_sdk::{U256, I256};pub fn large_numbers(env: Env) { let big_num = U256::from_u128(&env, 1000000000000000000u128); let signed = I256::from_i128(&env, -500000000000000000i128);}
use soroban_sdk::{Timepoint, Duration};pub fn time_example(env: Env) { // Get current timestamp let now = env.ledger().timestamp(); // Create duration (in seconds) let one_day = Duration::from_u64(&env, 86400); // Calculate future timepoint let tomorrow = now + one_day;}
Val is the internal representation of all Soroban values. You rarely work with Val directly, but it’s used for:
Low-level conversions with IntoVal and FromVal
Generic storage operations
Cross-contract calls
use soroban_sdk::{IntoVal, Val, Vec};pub fn val_usage(env: Env, val: Val) { // Convert to specific type let number: u32 = val.try_into_val(&env).unwrap(); // Store as generic Val let args: Vec<Val> = vec![&env, val];}
use soroban_sdk::{IntoVal, FromVal};pub fn conversions(env: Env) { // IntoVal - convert TO Val let val = 42u32.into_val(&env); // FromVal - convert FROM Val let num = u32::from_val(&env, &val);}