System Calls (Syscalls)
System calls (syscalls) are the interface between Starknet smart contracts and the Starknet Operating System (OS). They provide essential functionalities for:
- Reading blockchain state and context
- Contract interactions (deployment, calls)
- Event emission
- Cross-layer communication
- Storage operations
All syscalls return a SyscallResult
type, which can be either Success
or Failure
, enabling proper error handling in your contracts.
Available Syscalls
Blockchain State
- get_execution_info - Get current execution context
- get_block_hash - Retrieve a block's hash
- get_class_hash_at - Get the class hash of a contract at a specific address
Contract Operations
- call_contract - Call another contract
- deploy - Deploy a new contract
- library_call - Make a delegate call
- replace_class - Upgrade contract code
Events and Messaging
- emit_event - Emit contract events
- send_message_to_l1 - Send messages to Ethereum (L1)
Storage Operations
- storage_read - Read from contract storage
- storage_write - Write to contract storage
Cryptographic Operations
- keccak - Compute Keccak hash
- sha256_process_block - Compute SHA256 hash
Detailed Reference
get_block_hash
fn get_block_hash_syscall(block_number: u64) -> SyscallResult<felt252>
Retrieves the hash of a specific block by its number. Only works for blocks within the range [first_v0_12_0_block, current_block - 10]
.
get_execution_info
fn get_execution_info_v2_syscall() -> SyscallResult<Box<starknet::info::v2::ExecutionInfo>>
fn get_execution_info_syscall() -> SyscallResult<Box<starknet::info::ExecutionInfo>>
Returns information about the current execution context.
get_class_hash_at
fn get_class_hash_at_syscall(contract_address: ContractAddress) -> SyscallResult<ClassHash>
Returns the class hash of a contract at a specific address.
call_contract
fn call_contract_syscall(
address: ContractAddress,
entry_point_selector: felt252,
calldata: Span<felt252>
) -> SyscallResult<Span<felt252>>
Calls a contract at the specified address. Failures cannot be caught and will revert the entire transaction.
deploy
fn deploy_syscall(
class_hash: ClassHash,
contract_address_salt: felt252,
calldata: Span<felt252>,
deploy_from_zero: bool,
) -> SyscallResult<(ContractAddress, Span::<felt252>)>
Deploys a new contract instance. Returns the deployed address and constructor result.
The Simple Factory uses the deploy
syscall under the hood:
fn create_counter_at(ref self: ContractState, init_value: u128) -> ContractAddress {
// Constructor arguments
let mut constructor_calldata: Array::<felt252> = array![init_value.into()];
// Contract deployment
let (deployed_address, _) = deploy_syscall(
self.counter_class_hash.read(), 0, constructor_calldata.span(), false,
)
.unwrap();
deployed_address
}
emit_event
fn emit_event_syscall(
keys: Span<felt252>,
data: Span<felt252>
) -> SyscallResult<()>
Emits an event with indexed keys and data values.
See the Events section for more information:
self.emit(Event::CounterIncreased(CounterIncreased { amount }));
self
.emit(
Event::UserIncreaseCounter(
UserIncreaseCounter {
user: get_caller_address(), new_value: self.counter.read(),
},
),
);
library_call
fn library_call_syscall(
class_hash: ClassHash,
function_selector: felt252,
calldata: Span<felt252>
) -> SyscallResult<Span<felt252>>
Makes a delegate call to execute code from another contract class within the current contract's context. Similar to Ethereum's delegatecall but limited to a single class.
send_message_to_l1
fn send_message_to_l1_syscall(
to_address: felt252,
payload: Span<felt252>
) -> SyscallResult<()>
Sends a message to an Ethereum (L1) contract.
replace_class
fn replace_class_syscall(
class_hash: ClassHash
) -> SyscallResult<()>
Upgrades the contract's code by replacing its class hash.
This syscall is used in Upgradeable Contract:
fn upgrade(ref self: ContractState, impl_hash: ClassHash) {
assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');
starknet::syscalls::replace_class_syscall(impl_hash).unwrap();
self.emit(Event::Upgraded(Upgraded { implementation: impl_hash }))
}
storage_read
fn storage_read_syscall(
address_domain: u32,
address: StorageAddress,
) -> SyscallResult<felt252>
Low-level storage read operation.
storage_write
fn storage_write_syscall(
address_domain: u32,
address: StorageAddress,
value: felt252
) -> SyscallResult<()>
Low-level storage write operation.
keccak
fn keccak_syscall(input: Span<u64>) -> SyscallResult<u256>
Computes the Keccak hash of the input data as a little-endian 256-bit number, where input
is a Span of 64-bits little-endian words.