Defining Call Messages in Starknet.js
In Starknet.js, a call message is the structured description of a contract function invocation: which contract to target, which entrypoint to call, and what calldata to pass. Defining call messages correctly is essential for reading contract state, preparing transactions, and composing multicalls. This guide explains the core types and patterns for defining call messages in your Starknet.js applications.
What Is a Call Message?
A call message is an object that represents a single function call on a Starknet contract. It is used by Starknet.js providers and account abstractions to encode the call into the format expected by the network. The two primary shapes you will encounter are:
- Call — a plain object with
contractAddress,entrypoint, andcalldatafields. This is the low-level representation used by providers and account methods. - CallData — a utility for assembling and parsing calldata arrays, often used when you need to encode arguments manually.
Understanding these types helps you build calls that are both correct and readable, whether you are querying a view function or preparing a state-changing transaction.
The Call Type
The Call type is the fundamental call message. It is a TypeScript interface (or a plain JavaScript object) with the following properties:
contractAddress: the address of the target contract as a hex string.entrypoint: the name of the function to invoke (for example,"balanceOf").calldata: an array of felt values (strings or numbers) representing the function arguments.
Here is a minimal example of a call message that reads an ERC-20 balance:
const call = {
contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
entrypoint: 'balanceOf',
calldata: ['0x1234...'], // address whose balance you want to read
};
You can pass an array of such calls to provider methods like callContract or to account methods such as execute and estimateFee.
Building Calldata with CallData
Manually constructing the calldata array can be error-prone, especially for functions with multiple arguments or complex types. Starknet.js provides the CallData class to help you encode and decode calldata in a type-aware way.
To use it, import CallData from the library and create an instance, then call its compile method with the function arguments in order:
import { CallData } from 'starknet';
const calldata = new CallData(abi).compile('transfer', [recipient, amount]);
The abi parameter is the contract's ABI, which tells CallData how to serialize each argument. This approach reduces mistakes and makes your call messages easier to maintain.
Using Call Messages with Providers and Accounts
Once defined, call messages are consumed by different parts of Starknet.js:
- Read-only calls: use
provider.callContract(call)to invoke a view function without submitting a transaction. - Transactions: pass an array of calls to
account.execute(calls)to submit one or more state-changing invocations. - Fee estimation: use
account.estimateFee(calls)to preview the cost of your calls before sending them. - Multicall: combine multiple call messages into a single array to batch operations atomically.
Because the same Call structure is used across these methods, you can define your call messages once and reuse them in different contexts.
Best Practices for Defining Call Messages
Keep the following guidelines in mind to avoid common pitfalls:
- Validate addresses: ensure contract addresses are properly formatted hex strings. Use Starknet.js utilities to normalize addresses when needed.
- Match the ABI: the
entrypointname and the order ofcalldatavalues must match the contract's ABI exactly. - Use CallData for complex types: for arrays, structs, or multiple arguments, prefer
CallData.compileover manual array construction. - Keep calls immutable: treat call message objects as read-only once defined, especially when reusing them in multiple transactions.
- Test with read calls first: before executing a state-changing call, verify your calldata by making a read-only call to the same entrypoint if possible.
Frequently Asked Questions
What is the difference between a Call and a CallData?
A Call is the complete message object (contract address, entrypoint, calldata). CallData is a helper class for encoding and decoding the calldata array itself.
Can I use the same call message for reading and writing?
Yes, the Call structure is the same. However, read-only calls use provider.callContract, while state-changing calls are passed to an account's execute method.
How do I handle functions with no arguments?
Set calldata to an empty array: calldata: [].
Do I need the contract ABI to define a call message?
Not strictly—you can build the calldata array manually. But using the ABI with CallData is recommended to avoid encoding errors.
Where can I find the ABI for a contract?
You can obtain it from the contract's source, from a block explorer, or by compiling the Cairo contract. Starknet.js does not fetch ABIs automatically.