System calls
Starknet smart contracts are written in Cairo. However, Cairo is a general purpose language that you can use for much more than just contract language. For details, see Cairo – a Turing-complete STARK-friendly CPU architecture.
So writing smart contracts requires some operations, such as calling another contract or accessing the contract’s storage, that standalone programs do not require.
The Starknet contract language supports these operations by using system calls. System calls enable a contract to require services from the Starknet OS.
You can use system calls in a function to get information that depends on the broader state of Starknet, which would otherwise be inaccessible, rather than local variables that appear in the function’s scope.
getters (Cairo 0)
Below we describe the collection of system calls that can be used to get information regarding the block, transaction or execution context during runtime (e.g. block number, account address, and caller address).
get_block_number
func get_block_number{syscall_ptr : felt*}() -> (block_number : felt)
Gets the number of the block in which the transaction is executed.
None.
block_number
|
The number of the block in which the transaction is executed. |
get_block_timestamp
func get_block_timestamp{syscall_ptr : felt*}() -> (block_timestamp : felt)
Gets the timestamp of the block in which the transaction is executed.
None.
'block_timestamp' |
The timestamp of the block in which the transaction is executed |
get_caller_address
func get_caller_address{syscall_ptr : felt*}() -> (caller_address : felt)
Returns the address of the calling contract, or 0 if the call was not initiated by another contract.
None.
caller_address
|
The address of the calling contract, or 0 if the call was not initiated by another contract. |
get_contract_address
func get_contract_address{syscall_ptr : felt*}() -> (contract_address : felt)
Gets the address of the contract who raised the system call.
None.
contract_address
|
The address of the contract who raised the system call. |
get_sequencer_address
func get_sequencer_address{syscall_ptr : felt*}() -> (sequencer_address : felt)
Returns the address of the sequencer that generated the current block.
None.
sequencer_address
|
The address of the sequencer that generated the current block. |
===get_transaction_info
Gets information about the original transaction.
func get_tx_info{syscall_ptr : felt*}() -> (tx_info : TxInfo*)
Gets information about the original transaction.
None.
tx_info
|
The following information about the original transaction:
|
getters (Cairo 1.0)
In Cairo 1.0, all block/transaction/execution context getters are batched into a single system call: get_execution_info
.
get_execution_info
extern fn get_execution_info_syscall() -> SyscallResult<Box<starknet::info::ExecutionInfo>> implicits(
GasBuiltin, System
) nopanic;
Gets information about the original transaction.
None.
ExecutionInfo
|
A struct containing the execution info |
call_contract
Calls a given contract. This system call expects the address of the called contract, a selector for a function within that contract, and call arguments.
-
Cairo 0
-
Cairo 1.0
func call_contract{syscall_ptr : felt*}(
contract_address : felt, function_selector : felt, calldata_size : felt, calldata : felt*
) -> (retdata_size : felt, retdata : felt*)
extern fn call_contract_syscall(
address: ContractAddress, entry_point_selector: felt252, calldata: Span<felt252>
) -> SyscallResult<Span<felt252>> implicits(GasBuiltin, System) nopanic;
contract_address
|
The address of the contract you want to call. |
function_selector
|
A selector for a function within that contract. |
calldata_size
|
The size, in number of felts, of the calldata. |
calldata
|
The calldata. |
retdata_size
|
The size, in number of felts, of the return data. |
retdata
|
The return data. |
This is considered a lower level syntax for calling contracts. If the interface of the called contract is available, then you can use a more straightforward syntax. |
deploy
-
Cairo 0
-
Cairo 1.0
func deploy{syscall_ptr : felt*}(
class_hash : felt,
contract_address_salt : felt,
constructor_calldata_size : felt,
constructor_calldata : felt*,
deploy_from_zero: felt,
) -> (contract_address : felt)
extern fn deploy_syscall(
class_hash: ClassHash,
contract_address_salt: felt252,
calldata: Span<felt252>,
deploy_from_zero: bool,
) -> SyscallResult<(ContractAddress, Span::<felt252>)> implicits(GasBuiltin, System) nopanic;
Deploys a new instance of a previously declared class.
class_hash
|
The class hash of the contract to be deployed |
contract_address_salt
|
The salt, an arbitrary value provided by the sender, used in the computation of the contract’s address. |
constructor_calldata_size
|
The number of arguments to pass to the constructor, equal to the number of felts in |
constructor_calldata
|
The constructor’s calldata. An array of felts. |
deploy_from_zero
|
A flag used for the contract address computation. If not set, the caller address will be used as the new contract’s deployer address, otherwise 0 is used. |
contract_address
|
The address of the deployed contract. |
emit_event
-
Cairo 0
-
Cairo 1.0
func emit_event{syscall_ptr : felt*}(keys_len : felt, keys : felt*, data_len : felt, data : felt*)
extern fn emit_event_syscall(
keys: Span<felt252>, data: Span<felt252>
) -> SyscallResult<()> implicits(GasBuiltin, System) nopanic;
Emits an event with a given set of keys and data.
For more information, and for a higher level syntax for emitting events, see Starknet events.
keys_len
|
The number of keys in the event. Analogous to Ethereum’s event topics, you can use the starknet_getEvents method to filter by these keys. |
keys
|
The event’s keys |
data_len
|
The number of data elements in the event. |
data
|
The event’s data |
None.
The following example emits an event with two keys, the strings status
and deposit
and three data elements: 1
, 2
, and 3
.
-
Cairo 0
-
Cairo 1
let (keys : felt*) = alloc()
assert keys[0] = 'status'
assert keys[1] = 'deposit'
let (data : felt*) = alloc()
assert data[0] = 1
assert data[1] = 2
assert data[2] = 3
emit_event(2, keys, 3, data)
let keys = ArrayTrait::new();
keys.append('key');
keys.append('deposit');
let values = ArrayTrait::new();
values.append(1);
values.append(2);
values.append(3);
emit_event_syscall(keys, values).unwrap_syscall();
library_call
-
Cairo 0
-
Cairo 1.0
func library_call{syscall_ptr : felt*}(
class_hash : felt, function_selector : felt, calldata_size : felt, calldata : felt*
) -> (retdata_size : felt, retdata : felt*)
extern fn library_call_syscall(
class_hash: ClassHash, function_selector: felt252, calldata: Span<felt252>
) -> SyscallResult<Span<felt252>> implicits(GasBuiltin, System) nopanic;
Calls the requested function in any previously declared class.
This system call replaces the known delegate call functionality from Ethereum, with the important difference that there is only one contract involved.
The class is only used for its logic.
class_hash
|
The hash of the class you want to use. |
function_selector
|
A selector for a function within that class. |
calldata_size
|
The size, in number of felts, of the calldata. |
calldata
|
The calldata. |
retdata_size
|
The size, in number of felts, of the return data. |
retdata
|
The return data. |
library_call_l1_handler
This system call is not currently not supported in Cairo 1.0.
In practice, this was only used for proxy contracts, which in Cairo 1.0 can be implemented instead via the |
func library_call_l1_handler{syscall_ptr : felt*}(
class_hash : felt, function_selector : felt, calldata_size : felt, calldata : felt*
) -> (retdata_size : felt, retdata : felt*)
Calls the requested L1 handler in any previously declared class.
Same as the library_call
system call, but also enables you to call an L1 handler that cannot otherwise be called directly. For more information, see Starknet’s messaging mechanism.
When you invoke an L1 handler with this system call, the sequencer does not consume an L1→L2 message.
This system call enables an L1 handler to use the logic inside an L1 handler in a different class.
It is recommended to raise this system call only inside an L1 handler. |
class_hash
|
The hash of the class you want to use. |
function_selector
|
A selector for an L1 handler function within that class. |
calldata_size
|
The size, in number of felts, of the calldata. |
calldata
|
The calldata. |
retdata_size
|
The size, in number of felts, of the return data. |
retdata
|
The return data. |
send_message_to_L1
-
Cairo 0
-
Cairo 1.0
func send_message_to_l1{syscall_ptr : felt*}(
to_address : felt, payload_size : felt, payload : felt*
)
extern fn send_message_to_l1_syscall(
to_address: felt252, payload: Span<felt252>
) -> SyscallResult<()> implicits(GasBuiltin, System) nopanic;
Sends a message to L1.
This system call includes the message parameters as part of the proof’s output, and exposes these parameters to the Starknet Core contract on L1 once the state update, including the transaction, is received.
For more information, see Starknet’s messaging mechanism.
to_address
|
The recipient’s L1 address. |
payload_size
|
The size of the message payload. |
payload
|
A pointer to an array containing the contents of the message. |
None.
The following example sends a message whose content is (1,2)
to the L1 contract whose address is 3423542542364363
.
-
Cairo 0
-
Cairo 1
let payload = alloc()
payload[0] = 1
payload[1] = 2
send_message_to_l1(3423542542364363,2,payload)
let payload = ArrayTrait::new();
payload.append(1);
payload.append(2);
send_message_to_l1_syscall(payload).unwrap_syscall();
replace_class
-
Cairo 0
-
Cairo 1.0
replace_class(class_hash: felt)
extern fn replace_class_syscall(
class_hash: ClassHash
) -> SyscallResult<()> implicits(GasBuiltin, System) nopanic;
Once replace_class
is called, the class of the calling contract (i.e. the contract whose
address is returned by get_contract_address
at the time the syscall is called) will be replaced
by the class whose hash is given by the class_hash argument .
After calling The new class will be used from the next transaction onwards or if the contract is called via the call_contract syscall in the same transaction (after the replacement). |
class_hash
|
The hash of the class you want to use as a replacement. |
None
storage_read
-
Cairo 0
-
Cairo 1.0
func storage_read{syscall_ptr : felt*}(address : felt) -> (value : felt)
extern fn storage_read_syscall(
address_domain: u32, address: StorageAddress,
) -> SyscallResult<felt252> implicits(GasBuiltin, System) nopanic;
Gets the value of a key in the storage of the calling contract.
This system call provides direct access to any possible key in storage, in contrast with balance.read()
, which enables you to read storage variables that are defined explicitly in the contract.
For information on accessing storage by using the storage variables, see storage variables.
address
|
The address of the storage key you want to read. |
value
|
The value of the key. |
-
Cairo 0
-
Cairo 1.0
let value = storage_read(3534535754756246375475423547453)
use starknet::storage_access::storage_base_address_from_felt252;
...
let storage_address = storage_base_address_from_felt252(3534535754756246375475423547453)
storage_read_syscall(0, storage_address).unwrap_syscall()
storage_write
Sets the value of a key in the storage of the calling contract.
-
Cairo 0
-
Cairo 1.0
func storage_write{syscall_ptr : felt*}(address : felt, value : felt)
extern fn storage_write_syscall(
address_domain: u32, address: StorageAddress, value: felt252
) -> SyscallResult<()> implicits(GasBuiltin, System) nopanic;
Sets the value of a key in the storage of the calling contract.
This system call provides direct access to any possible key in storage, in contrast with balance.write()
, which enables you to write to storage variables that are defined explicitly in the contract.
For information on accessing storage by using the storage variables, see storage variables.
address
|
The address of the storage key to which you want to write. |
.value
|
The value to write to the key. |
None.