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 (
IContractDispatcher
andIContractDispatcherTrait
) - 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