Silex Standard Library (Built-in Functions)
This document describes the built-in functions available in Silex. These functions are automatically available in your Silex code and can be invoked on the appropriate types (Arrays, Strings, Maps, etc.) or as static functions (e.g., Block::current()).
Please note:
- Gas Cost: Each function consumes a certain amount of gas when invoked. The gas cost is indicated below next to each function. But some functions can apply an additional dynamic gas.
- Optional Return: Some functions can return a value or
nullwhen the value is absent. - Type Parameters: Some functions use a type variable
T(0),T(1)(like in arrays or maps) simply meaning “whatever element type” your array or map uses.
Global Utility Functions
println
Signature:
println(value: Any)Gas Cost: 1
Description: Prints a string representation of value (only effective if in debug mode).
debug
Signature:
debug(value: Any)Gas Cost: 1
Description: Prints a debug-style (developer-friendly) representation of value (only effective if in debug mode).
panic
Signature:
panic(value: Any) -> AnyReturn: whatever type you need (but code will never return)
Gas Cost: 1
Description: Aborts execution with the provided value as a message. It never returns normally.
assert
Signature:
assert(condition: bool)Gas Cost: 1
Description: If condition is false, execution aborts with an assertion error.
require
Signature:
require(condition: bool, msg: string)Gas Cost: 1
Description: If condition is false, execution aborts with the provided message, msg.
is_same_ptr
Signature:
is_same_ptr(value1: Any, value2: Any) -> boolGas Cost: 5
Description: Checks if value1 and value2 refer to the exact same internal reference.
Array Functions
Let arr be of type T[].
len
Signature:
arr.len() -> u32Return: u32
Gas Cost: 1
Description: Returns the number of elements in arr.
push
Signature:
arr.push(value: T)Gas Cost: 2
Description: Appends value to the end of arr.
remove
Signature:
arr.remove(index: u32) -> TGas Cost: 5
Description: Removes and returns the element at index, shifting subsequent elements to the left.
Throws an “OutOfBounds” error if index is invalid.
pop
Signature:
arr.pop() -> optional<T>Gas Cost: 1
Description: Removes and returns the last element if it exists, or null if arr is empty.
slice
Signature:
arr.slice(range: range<u32>) -> T[]Gas Cost: 5
Description: Returns a new array containing the elements in the given half-open range [start, end).
Throws an “InvalidRange” error if out of bounds or invalid.
contains
Signature:
arr.contains(value: T) -> boolGas Cost: 10
Description: Returns true if value is in arr.
get
Signature:
arr.get(index: u32) -> optional<T>Gas Cost: 1
Description: Returns the element at index, or null if out of bounds.
first
Signature:
arr.first() -> optional<T>Gas Cost: 1
Description: Returns the first element of arr, or null if empty.
last
Signature:
arr.last() -> optional<T>Gas Cost: 1
Description: Returns the last element of arr, or null if empty.
Optional (T or null) Functions
These functions are invoked on a value that might be T or null.
is_none
Signature:
value.is_none() -> boolGas Cost: 1
Description: Returns true if value is null.
is_some
Signature:
value.is_some() -> boolGas Cost: 1
Description: Returns true if value is not null.
unwrap
Signature:
value.unwrap() -> TGas Cost: 1
Description: Returns the underlying value if not null. Throws an error otherwise.
unwrap_or
Signature:
value.unwrap_or(default_value: T) -> TGas Cost: 1
Description: Returns the underlying value if not null, or returns default_value if null.
expect
Signature:
value.expect(msg: string) -> TGas Cost: 15
Description: Returns the underlying value if not null. If null, the execution panics with the provided message, msg.
Map Functions
Let map be of type Map<K, V>.
len
Signature:
map.len() -> u32Gas Cost: 1
Description: Returns the number of key-value pairs in map.
contains_key
Signature:
map.contains_key(key: K) -> boolGas Cost: 15
Description: Returns true if map contains key.
get
Signature:
map.get(key: K) -> optional<V>Gas Cost: 15
Description: Returns the value for key or null if the key is not present.
insert
Signature:
map.insert(key: K, value: V) -> optional<V>Gas Cost: 30
Description: Inserts or updates the key-value pair. Returns the old value if key was present, otherwise null.
remove
Signature:
map.remove(key: K) -> optional<V>Gas Cost: 15
Description: Removes the key-value pair and returns the old value, or null if the key was not present.
clear
Signature:
map.clear()Gas Cost: 5
Description: Removes all key-value pairs.
keys
Signature:
map.keys() -> [K]Gas Cost: 20
Description: Returns an array of all keys.
values
Signature:
map.values() -> [V]Gas Cost: 20
Description: Returns an array of all values.
string Functions
All are invoked on a string variable, e.g. my_string.len().
len
Signature:
str.len() -> u32Gas Cost: 1
Description: Returns the length of the string in bytes (UTF-8 length).
trim
Signature:
str.trim() -> stringGas Cost: 1
Description: Returns a new string with leading and trailing whitespace removed.
contains
Signature:
str.contains(value: string) -> boolGas Cost: 1
Description: Checks if value is a substring of str.
contains_ignore_case
Signature:
str.contains_ignore_case(value: string) -> boolGas Cost: 1
Description: Case-insensitive version of contains.
to_uppercase
Signature:
str.to_uppercase() -> stringGas Cost: 1
Description: Returns str converted to uppercase.
to_lowercase
Signature:
str.to_lowercase() -> stringGas Cost: 1
Description: Returns str converted to lowercase.
to_bytes
Signature:
str.to_bytes() -> u8[]Gas Cost: 5
Description: Converts str into an array of its UTF-8 bytes.
index_of
Signature:
str.index_of(value: string) -> optional<u32>Gas Cost: 3
Description: Finds the first index where value occurs. If not found, returns null.
last_index_of
Signature:
str.last_index_of(value: string) -> optional<u32>Gas Cost: 3
Description: Finds the last index where value occurs. If not found, returns null.
replace
Signature:
str.replace(from: string, to: string) -> stringGas Cost: 5
Description: Returns a copy of str with all occurrences of from replaced by to.
starts_with
Signature:
str.starts_with(value: string) -> boolGas Cost: 3
Description: Returns true if str begins with value.
ends_with
Signature:
str.ends_with(value: string) -> boolGas Cost: 3
Description: Returns true if str ends with value.
split
Signature:
str.split(at: string) -> [string]Gas Cost: 5
Description: Splits str by the separator at and returns an array of substrings.
char_at
Signature:
str.char_at(index: u32) -> optional<string>Gas Cost: 1
Description: Returns the character (as a 1-char string) at position index, or null if out of range.
is_empty
Signature:
str.is_empty() -> boolGas Cost: 1
Description: Returns true if str is empty.
matches
Signature:
str.matches(pattern: string) -> [string]Gas Cost: 50
Description: Returns all non-overlapping matches of pattern within str as an array of strings.
substring (1 param)
Signature:
str.substring(start: u32) -> optional<string>Gas Cost: 3
Description: Returns the substring from start index to the end, or null if out of range.
substring (2 params)
Signature:
str.substring(start: u32, end: u32) -> optional<string>Gas Cost: 3
Description: Returns the substring from start (inclusive) up to end (exclusive), or null if out of range.
Range Functions
Silex supports a range type range<T> (e.g. range<u32>). These functions are methods on such range variables.
contains
Signature:
range.contains(value: T) -> boolGas Cost: 5
Description: Checks if value is within [start, end).
collect
Signature:
range.collect() -> T[]Gas Cost: 20 (plus additional usage proportional to the size of the range)
Description: Creates an array of all values in [start, end).
max
Signature:
range.max() -> TGas Cost: 1
Description: Returns the end value of the range.
min
Signature:
range.min() -> TGas Cost: 1
Description: Returns the start value of the range.
count
Signature:
range.count() -> TGas Cost: 5
Description: Returns the number of elements in [start, end) as a T.
Integer Functions & Constants
Checked Arithmetic
For each unsigned integer type (u8, u16, u32, u64, u128, u256), the following methods exist:
checked_add(other: T) -> optional<T>checked_sub(other: T) -> optional<T>checked_mul(other: T) -> optional<T>checked_div(other: T) -> optional<T>checked_rem(other: T) -> optional<T>
Gas Cost: 2 (for each operation)
Description: Performs the specified arithmetic operation. If an overflow occurs, returns null; otherwise returns the resulting value.
MIN, MAX constants
For each of the unsigned integer types, the built-in constants MIN and MAX are available. You can reference them as u8::MIN, u8::MAX, u16::MIN, etc.
Example:
let min_u32 = u32::MIN; // 0
let max_u32 = u32::MAX; // 4294967295to_be_bytes / to_le_bytes
For each of u16, u32, u64, u128, u256, you have:
value.to_be_bytes() -> u8[]value.to_le_bytes() -> u8[]
Gas Cost: 15
Description: Converts the integer into a big-endian or little-endian byte array. Typically, this returns the array (non-null).
Transaction Functions
Use these via the Transaction opaque type:
Transaction::current()
Signature:
Transaction::current() -> TransactionGas Cost: 5
Description: Returns the current transaction object in which the smart contract is running.
Once you have a Transaction (e.g. let tx: Transaction = Transaction::current();), you can call:
nonce
tx.nonce() -> u64Gas Cost: 5
Description: Returns the nonce field of the current transaction.
hash
tx.hash() -> HashGas Cost: 5
Description: Returns the cryptographic hash of the transaction.
source
tx.source() -> AddressGas Cost: 5
Description: Returns the Address that originated the transaction.
fee
tx.fee() -> u64Gas Cost: 5
Description: Returns the transaction’s fee.
signature
tx.signature() -> SignatureGas Cost: 5
Description: Returns the signature object of the transaction.
Block Functions
Use these via the Block opaque type:
Block::current()
Block::current() -> BlockGas Cost: 5
Description: Returns the current block object in which the contract is executing.
After obtaining a Block (e.g., let blk = Block::current();), you can call:
nonce
blk.nonce() -> u64Gas Cost: 5
Description: Returns the block’s nonce.
timestamp
blk.timestamp() -> u64Gas Cost: 5
Description: Returns the block’s timestamp.
height
blk.height() -> u64Gas Cost: 5
Description: Returns the chain height at this block.
extra_nonce
blk.extra_nonce() -> u8[]Gas Cost: 5
Description: Returns the block’s extra nonce field as a byte array.
hash
blk.hash() -> HashGas Cost: 5
Description: Returns this block’s hash.
miner
blk.miner() -> AddressGas Cost: 5
Description: Returns the miner’s address for the block.
version
blk.version() -> u8Gas Cost: 5
Description: Returns the version number of the block.
tips
blk.tips() -> Hash[]Gas Cost: 5
Description: Returns an array of hash references this block depends on (the block tips).
transactions_hashes
blk.transactions_hashes() -> Hash[]Gas Cost: 50
Description: Returns an array of all transaction hashes included in this block.
transactions
blk.transactions() -> Transaction[]Gas Cost: 250
Description: Returns an array of all Transaction objects in this block.
transactions_count
blk.transactions_count() -> u32Gas Cost: 1
Description: Returns how many transactions are in this block.
Storage Functions
Storage::new()
Storage::new() -> StorageGas Cost: 5
Description: Creates a Storage object referring to this contract’s persistent storage. Typically you just use this once.
When you have a Storage, you can call:
load
storage.load(key: Any) -> optional<Any>Gas Cost: 50
Description: Loads the stored value for key. Returns the value or null if not found.
has
storage.has(key: Any) -> boolGas Cost: 25
Description: Returns true if a value for key exists.
store
storage.store(key: Any, value: Any) -> optional<Any>Gas Cost: 50
Description: Stores value for key, returning the old value if it existed, otherwise null.
delete
storage.delete(key: Any) -> optional<Any>Gas Cost: 50
Description: Removes the key from storage, returning the old value if present, or null.
ReadOnlyStorage Functions
ReadOnlyStorage::new(contract_hash)
ReadOnlyStorage::new(contract: Hash) -> optional<ReadOnlyStorage>Gas Cost: 15
Description: Returns a read-only view of another contract’s storage if it exists, otherwise null.
When you have a ReadOnlyStorage, you can call:
load
readonly_storage.load(key: Any) -> optional<Any>Gas Cost: 50
Description: Reads the value for key in that contract’s storage, or null if not found.
has
readonly_storage.has(key: Any) -> boolGas Cost: 25
Description: Returns true if a value for key exists in the read-only storage.
MemoryStorage Functions
MemoryStorage::new()
MemoryStorage::new() -> MemoryStorageGas Cost: 5
Description: Creates a temporary in-memory storage (non-persistent).
For a MemoryStorage, you can call:
load
mem.load(key: Any) -> optional<Any>Gas Cost: 50
Description: Loads a value for key from an in-memory map. Returns the value or null if not present.
has
mem.has(key: Any) -> boolGas Cost: 25
Description: Returns true if key is in the in-memory storage.
store
mem.store(key: Any, value: Any) -> optional<Any>Gas Cost: 50
Description: Stores value, returning the old value if it existed, otherwise null.
delete
mem.delete(key: Any) -> optional<Any>Gas Cost: 50
Description: Removes key, returning the old value if present, or null.
Address Functions
is_mainnet
addr.is_mainnet() -> boolGas Cost: 5
Description: Returns true if addr is a mainnet address.
is_normal
addr.is_normal() -> boolGas Cost: 5
Description: Returns true if addr is a “normal” address (not integrated, subaddress, etc.).
to_public_key_bytes
addr.to_public_key_bytes() -> BytesGas Cost: 10
Description: Extracts the public key (32 bytes) as a Bytes object.
Address::from_string
Address::from_string(address_str: string) -> AddressGas Cost: 75
Description: Constructs an Address from the given string representation. (If invalid, behavior may error or produce an invalid address.)
Hash Functions
to_bytes
hash_value.to_bytes() -> BytesGas Cost: 5
Description: Converts the Hash to a 32-byte Bytes.
to_array
hash_value.to_array() -> u8[]Gas Cost: 5
Description: Converts the Hash to an array of 32 u8.
to_u256
hash_value.to_u256() -> u256Gas Cost: 5
Description: Interprets the Hash as a u256.
to_hex
hash_value.to_hex() -> stringGas Cost: 20
Description: Returns a hex-encoded string of the hash (64 hex chars).
Hash::from_bytes
Hash::from_bytes(bytes: Bytes) -> HashGas Cost: 75
Description: Constructs a Hash from a 32-byte Bytes.
Hash::from_array
Hash::from_array(bytes: u8[]) -> HashGas Cost: 75
Description: Constructs a Hash from an array of 32 u8. Typically errors if the length is not 32.
Hash::from_u256
Hash::from_u256(value: u256) -> HashGas Cost: 75
Description: Constructs a 32-byte Hash from a u256.
Hash::from_hex
Hash::from_hex(hex: string) -> HashGas Cost: 75
Description: Parses a 64-char hex string into a 32-byte hash.
Hash::blake3
Hash::blake3(input: u8[]) -> HashGas Cost: 3000
Description: Computes the BLAKE3 hash of input.
Hash::sha256
Hash::sha256(input: u8[]) -> HashGas Cost: 7500
Description: Computes the SHA-256 hash of input.
Hash::zero
Hash::zero() -> HashGas Cost: 1
Description: Returns the zeroed 32-byte hash.
Hash::max
Hash::max() -> HashGas Cost: 1
Description: Returns a hash of all 0xFF bytes (the “maximum” possible hash).
Random Functions
Random::new()
Random::new() -> RandomGas Cost: 5
Description: Creates a deterministic random generator object (seeded by the execution context).
The following methods can be called on a Random object:
next_u8 / next_u16 / next_u32 / next_u64 / next_u128 / next_u256
rng.next_u8()-> u8
rng.next_u16() -> u16
rng.next_u32() -> u32
rng.next_u64() -> u64
rng.next_u128()-> u128
rng.next_u256()-> u256Gas Cost: 5 each
Description: Generates the next random number of the given width.
next_bool
rng.next_bool() -> boolGas Cost: 5
Description: Generates a random boolean.
Signature Functions
verify
Signature:
sig.verify(data: u8[], address: Address) -> boolGas Cost: 500
Description: Verifies that the signature sig is valid for the byte array data signed by address. Returns true if valid, false if invalid.
Signature::from_bytes
Signature::from_bytes(bytes: u8[]) -> SignatureGas Cost: 75
Description: Creates a Signature from a byte array (usually 64 bytes).
Asset Functions
Asset::get_by_id
Asset::get_by_id(id: u64) -> optional<Asset>Gas Cost: 1000
Description: Looks up the asset by a numeric ID. Returns the asset or null if not found.
Asset::create
Asset::create(
id: u64,
name: string,
ticker: string,
decimals: u8,
max_supply: optional<u64>
) -> optional<Asset>Gas Cost: 2500, with an additional 1 XEL of burned gas
Description: Creates a new asset with the given parameters. max_supply can be null if unlimited. Returns the newly created asset or null if creation fails.
Asset::get_by_hash
Asset::get_by_hash(hash: Hash) -> optional<Asset>Gas Cost: 500
Description: Looks up the asset by its Hash. Returns the asset or null if not found.
Once you have an Asset, you can call:
get_max_supply
asset.get_max_supply() -> optional<u64>Gas Cost: 5
Description: Returns the maximum supply if any, otherwise null.
get_supply
asset.get_supply() -> u64Gas Cost: 15
Description: Returns the current supply of the asset.
get_name
asset.get_name() -> stringGas Cost: 5
Description: Returns the asset’s full name (human-readable).
get_ticker
asset.get_ticker() -> stringGas Cost: 5
Description: Returns the short ticker symbol.
get_hash
asset.get_hash() -> HashGas Cost: 5
Description: Returns the unique Hash representing this asset.
mint
asset.mint(amount: u64) -> boolGas Cost: 500
Description: Increases the asset’s supply by amount, if allowed. Returns true if successful, false otherwise.
is_read_only
asset.is_read_only() -> boolGas Cost: 5
Description: Returns true if the asset cannot be modified (e.g. a read-only context).
get_contract_hash
asset.get_contract_hash() -> HashGas Cost: 5
Description: If the asset is contract-scoped, returns the owning contract hash. Otherwise null.
get_id
asset.get_id() -> optional<u64>Gas Cost: 5
Description: If the asset is numeric-based, returns its numeric ID, otherwise null.
transfer_ownership
asset.transfer_ownership(contract: Hash) -> boolGas Cost: 250
Description: If the asset is owned by the current contract, transfers ownership to the contract hash passed in parameter. Returns true if successful.
Misc. Built-ins
get_contract_hash()
get_contract_hash() -> HashGas Cost: 5
Description: Returns the hash of the contract that is currently executing.
get_deposit_for_asset()
get_deposit_for_asset(asset: Hash) -> optional<u64>Gas Cost: 5
Description: Returns how much of asset was deposited with the current contract call, or null if none.
get_balance_for_asset()
get_balance_for_asset(asset: Hash) -> u64Gas Cost: 25
Description: Returns the contract’s current balance of asset.
transfer()
transfer(destination: Address, amount: u64, asset: Hash) -> boolGas Cost: 500
Description: Moves amount of asset out of the contract to destination. Returns true if successful, false otherwise.
burn()
burn(amount: u64, asset: Hash) -> boolGas Cost: 500
Description: Permanently removes amount of asset from the contract’s balance. Returns true if successful.
fire_event()
fire_event(id: u64, data: Any)Gas Cost: 250 + an additional cost proportional to the size of data
Description: Fires an event with the given id and data. Events can be tracked externally.