Cheatcodes reference
Some tests require specific setup beyond what is possible using standard flow. Protostar exposes additional functions called cheatcodes that let you modify the state beyond what is normally possible.
Cheatcodes do not require any specific imports or use
declarations.
Error handling in cheatcodes
All cheatcodes return a Result
enum:
enum Result<T, felt252> {
Ok: T,
Err: E,
}
On successful cheatcode execution, a positive result is returned (OK
) - its exact type depends on the cheatcode.
Depending on cheatcode the type of Err
may also vary.
Failing test on cheatcode error
The simplest handling of Result
is to unwrap()
them. It either returns a cheatcode success value or causes the test
to
fail entirely:
use result::ResultTrait;
let prepared_contract = prepare(class_hash, @calldata).unwrap();
RevertedTransaction
Cheatcodes invoke
, call
, deploy
, deploy_contract
and deploy_contract_cairo0
and return an Err
of
type RevertedTransaction
in case of failure.
struct RevertedTransaction {
panic_data: Array::<felt252>,
}
It also implements a trait:
trait RevertedTransactionTrait {
fn first(self: @RevertedTransaction) -> felt252;
}
An example handling of RevertedTransaction
may look like this
match invoke(deployed_contract_address, 'panic_with', @panic_data) {
Result::Ok(x) => assert(false, 'Shouldnt have succeeded'),
Result::Err(x) => {
assert(x.first() == 'error', 'first datum doesnt match');
assert(*x.panic_data.at(1_u32) == 'data', 'second datum doesnt match');
}
}
Available cheatcodes
📄️ call
Calls a deployed contract. Unlike invoke, it does not mutate the blockchain state.
📄️ declare_cairo0
Declares a Cairo0 (old syntax) contract given a contract name defined in the protostar.toml configuration file.
📄️ declare
Declares a contract given its name defined in the protostar.toml configuration
📄️ deploy
Deploys a prepared contract.
📄️ deploy_contract
Declares and deploys a contract given its name defined in the protostar.toml
📄️ deploy_contract_cairo0
Declares and deploys a Cairo 0 contract given its name defined in the protostar.toml
📄️ invoke
Invokes a contract's function. function_name parameter should be provided as a short string. invoke can mutate the
📄️ prepare
Prepares contract for deployment.
📄️ start_prank
Changes a caller address returned by getcalleraddress() for the targeted contract until the prank is stopped
📄️ start_roll
Sets a block number for a deployed contract, until stoproll is called.
📄️ start_spoof
Changes TxInfo returned by gettxinfo() for the targeted contract until the spoof is stopped
📄️ start_warp
Sets a block timestamp for a deployed contract, until stopwarp is called.
📄️ stop_prank
Stops a prank started by startprank.
📄️ stop_roll
Stops a roll started by startroll.
📄️ stop_spoof
Stops a spoof started by startspoof.
📄️ stop_warp
Stops a warp started by startwarp.