In Starknet, contracts can interact with each other through contract calls. The recommended way to make these calls is using the dispatcher pattern, which provides type safety and better error handling.Documentation Index
Fetch the complete documentation index at: https://docs.starknet.io/llms.txt
Use this file to discover all available pages before exploring further.
Understanding Dispatchers
A dispatcher is an automatically generated struct that handles the serialization and deserialization of contract calls. To use dispatchers:- Define the target contract’s interface as a trait with
#[starknet::interface](IContract) - Import the generated dispatcher types (
IContractDispatcherandIContractDispatcherTrait) - Create a dispatcher instance with the target contract’s address
Caller) interacts with another (Callee). The Callee contract stores a value that can be set and retrieved:
Caller contract demonstrates how to use the dispatcher to interact with Callee:
Key Points:
- The
#[starknet::interface]attribute automatically generates the dispatcher types - Dispatchers handle all the low-level details of contract interaction
- Contract calls are type-safe and checked at compile time
- Each contract maintains its own storage and state
While you can use the low-level
call_contract_syscall directly, it’s not recommended as it:- Requires manual serialization/deserialization
- Lacks compile-time type checking
- Is more easy to make mistakes