Skip to main content

Cheatcodes

Most of the time, testing smart contracts with assertions only is not enough. Some test cases require manipulating the state of the blockchain, as well as checking for reverts and events. For that reason, Protostar provides a set of cheatcodes.

Cheatcodes are available inside of Cairo hints. You don't have to import anything. When Protostar runs tests, it replaces some core elements in cairo-lang library and injects cheatcodes to the hint scope.

note

If you are familiar with Foundry, you will recognize most cheatcodes.

Available cheatcodes

Data Transformer

What is a Data Transformer

Data Transformer converts inputs and outputs of Cairo functions to Python friendly representation. Cairo internally operates on a list of integers, which readability and maintenance become problematic for complex data structures. You can read more about:

Using Data Transformer in cheatcodes

Cheatcodes accept arguments representing input or output of a Cairo function as:

  • List[int] — a list of integers
  • Dict[DataTransformer.ArgumentName, DataTransformer.SupportedType] — Data Transformer friendly dictionary

Example

The following example demonstrates usage on the deploy_contract.

./src/main.cairo
%lang starknet
from starkware.cairo.common.uint256 import Uint256

@constructor
func constructor(initial_balance: Uint256, contract_id: felt) {
// ...
return ();
}
Passing constructor data as a dictionary
deploy_contract("./src/main.cairo", { "initial_balance": 42, "contract_id": 123 })
Passing constructor data as a list of integers
deploy_contract("./src/main.cairo", [42, 0, 123])