diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 34534e8e6f..6291be0f5f 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -46,6 +46,7 @@ * [Cheatcodes Reference](appendix/cheatcodes.md) * [CheatTarget](appendix/cheatcodes/cheat_target.md) * [CheatSpan](appendix/cheatcodes/cheat_span.md) + * [block_timestamp](appendix/cheatcodes/block_timestamp.md) * [mock_call](appendix/cheatcodes/mock_call.md) * [caller_address](appendix/cheatcodes/caller_address/README.md) * [prank](appendix/cheatcodes/caller_address/prank.md) @@ -59,10 +60,6 @@ * [roll](appendix/cheatcodes/block_number/roll.md) * [start_roll](appendix/cheatcodes/block_number/start_roll.md) * [stop_roll](appendix/cheatcodes/block_number/stop_roll.md) - * [block_timestamp](appendix/cheatcodes/block_timestamp/README.md) - * [warp](appendix/cheatcodes/block_timestamp/warp.md) - * [start_warp](appendix/cheatcodes/block_timestamp/start_warp.md) - * [stop_warp](appendix/cheatcodes/block_timestamp/stop_warp.md) * [tx_info](appendix/cheatcodes/tx_info/README.md) * [spoof](appendix/cheatcodes/tx_info/spoof.md) * [start_spoof](appendix/cheatcodes/tx_info/start_spoof.md) diff --git a/docs/src/appendix/cheatcodes.md b/docs/src/appendix/cheatcodes.md index 1dd3455b8f..39e69afab4 100644 --- a/docs/src/appendix/cheatcodes.md +++ b/docs/src/appendix/cheatcodes.md @@ -8,9 +8,9 @@ - [`roll`](cheatcodes/block_number/roll.md) - changes the block number for contracts, for a number of calls - [`start_roll`](cheatcodes/block_number/start_roll.md) - changes the block number for contracts - [`stop_roll`](cheatcodes/block_number/stop_roll.md) - cancels the `roll` / `start_roll` for contracts -- [`warp`](cheatcodes/block_timestamp/warp.md) - changes the block timestamp for contracts, for a number of calls -- [`start_warp`](cheatcodes/block_timestamp/start_warp.md) - changes the block timestamp for contracts -- [`stop_warp`](cheatcodes/block_timestamp/stop_warp.md) - cancels the `warp` / `start_warp` for contracts +- [`warp`](cheatcodes/block_timestamp#warp) - changes the block timestamp for contracts, for a number of calls +- [`start_warp`](cheatcodes/block_timestamp#start_warp) - changes the block timestamp for contracts +- [`stop_warp`](cheatcodes/block_timestamp#stop_warp) - cancels the `warp` / `start_warp` for contracts - [`elect`](cheatcodes/sequencer_address/elect.md) - changes the sequencer address for contracts, for a number of calls - [`start_elect`](cheatcodes/sequencer_address/start_elect.md) - changes the sequencer address for contracts - [`stop_elect`](cheatcodes/sequencer_address/stop_elect.md) - cancels the `elect` / `start_elect` for contracts diff --git a/docs/src/appendix/cheatcodes/block_timestamp.md b/docs/src/appendix/cheatcodes/block_timestamp.md new file mode 100644 index 0000000000..0bcc8f80b2 --- /dev/null +++ b/docs/src/appendix/cheatcodes/block_timestamp.md @@ -0,0 +1,20 @@ +# `block_timestamp` + +Cheatcodes modifying `block_timestamp`: + +## `warp` + +> `fn warp(target: CheatTarget, block_timestamp: u64, span: CheatSpan)` + +Changes the block timestamp for the given target and span. + +## `start_warp` +> `fn start_warp(target: CheatTarget, block_timestamp: u64)` + +Changes the block timestamp for the given target. + +## `stop_warp` + +> `fn stop_warp(target: CheatTarget)` + +Cancels the `warp` / `start_warp` for the given target. diff --git a/docs/src/appendix/cheatcodes/block_timestamp/README.md b/docs/src/appendix/cheatcodes/block_timestamp/README.md deleted file mode 100644 index 6e08a5b93c..0000000000 --- a/docs/src/appendix/cheatcodes/block_timestamp/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# `block_timestamp` - -Cheatcodes modifying `block_timestamp`: - -* [`warp`](./warp.md) - changes the block timestamp for the given target and span -* [`start_warp`](./start_warp.md) - changes the block timestamp for the given target until [`stop_warp`](./stop_warp.md) is called -* [`stop_warp`](./stop_warp.md) - cancels the [`warp`](./warp.md) / [`start_warp`](./start_warp.md) for the given target diff --git a/docs/src/appendix/cheatcodes/block_timestamp/start_warp.md b/docs/src/appendix/cheatcodes/block_timestamp/start_warp.md deleted file mode 100644 index bf35732f1f..0000000000 --- a/docs/src/appendix/cheatcodes/block_timestamp/start_warp.md +++ /dev/null @@ -1,49 +0,0 @@ -# `start_warp` - -> `fn start_warp(target: CheatTarget, block_timestamp: u64)` - -Changes the block timestamp for the given target. -The change can be canceled with [`stop_warp`](./stop_warp.md). - -- `target` - instance of [`CheatTarget`](../cheat_target.md) specifying which contracts to warp -- `block_timestamp` - block timestamp to be set - -For contract implementation: - -```rust -// ... -#[storage] -struct Storage { - // ... - stored_block_timestamp: u64 -} - -#[abi(embed_v0)] -impl IContractImpl of IContract { - fn set_block_timestamp(ref self: ContractState) { - self.stored_block_timestamp.write(starknet::get_block_timestamp()); - } - - fn get_block_timestamp(self: @ContractState) -> u64 { - self.stored_block_timestamp.read() - } -} -// ... -``` - -We can use `start_warp` in a test to change the block timestamp for contracts: - -```rust -use snforge_std::{start_warp, CheatTarget}; - -#[test] -fn test_warp() { - // ... - - start_warp(CheatTarget::One(contract_address), 1000); - - dispatcher.set_block_timestamp(); - let new_timestamp = dispatcher.get_block_timestamp(); - assert(new_timestamp == 1000, 'Wrong timestamp'); -} -``` diff --git a/docs/src/appendix/cheatcodes/block_timestamp/stop_warp.md b/docs/src/appendix/cheatcodes/block_timestamp/stop_warp.md deleted file mode 100644 index 507b15f0b6..0000000000 --- a/docs/src/appendix/cheatcodes/block_timestamp/stop_warp.md +++ /dev/null @@ -1,20 +0,0 @@ -# `stop_warp` - -> `fn stop_warp(target: CheatTarget)` - -Cancels the [`warp`](./warp.md) / [`start_warp`](./start_warp.md) for the given target. - -- `target` - instance of [`CheatTarget`](../cheat_target.md) specifying which contracts to stop warping. - -```rust -use snforge_std::stop_warp; - -#[test] -fn test_warp() { - // ... - - stop_warp(CheatTarget::One(contract_address)); - - // ... -} -``` diff --git a/docs/src/appendix/cheatcodes/block_timestamp/warp.md b/docs/src/appendix/cheatcodes/block_timestamp/warp.md deleted file mode 100644 index 1907e2cec4..0000000000 --- a/docs/src/appendix/cheatcodes/block_timestamp/warp.md +++ /dev/null @@ -1,10 +0,0 @@ -# `warp` - -> `fn warp(target: CheatTarget, block_timestamp: u64, span: CheatSpan)` - -Changes the block timestamp for the given target and span. -This change can be canceled with [`stop_warp`](./stop_warp.md). - -- `target` - instance of [`CheatTarget`](../cheat_target.md) specifying which contracts to warp -- `block_timestamp` - block timestamp to be set -- `span` - instance of [`CheatSpan`](../cheat_span.md) specifying the number of target calls with the cheat applied diff --git a/snforge_std/src/cheatcodes.cairo b/snforge_std/src/cheatcodes.cairo index 9c123a0f56..882f46e083 100644 --- a/snforge_std/src/cheatcodes.cairo +++ b/snforge_std/src/cheatcodes.cairo @@ -70,6 +70,10 @@ fn stop_prank(target: CheatTarget) { handle_cheatcode(cheatcode::<'stop_prank'>(inputs.span())); } +/// Changes the block timestamp for the given target and span. +/// - `target` - instance of `CheatTarget` specifying which contracts to warp +/// - `block_timestamp` - block timestamp to be set +/// - `span` - instance of `CheatSpan` specifying the number of target calls with the cheat applied fn warp(target: CheatTarget, block_timestamp: u64, span: CheatSpan) { validate_cheat_target_and_span(@target, @span); @@ -80,10 +84,15 @@ fn warp(target: CheatTarget, block_timestamp: u64, span: CheatSpan) { handle_cheatcode(cheatcode::<'warp'>(inputs.span())); } +/// Changes the block timestamp for the given target. +/// - `target` - instance of `CheatTarget` specifying which contracts to warp +/// - `block_timestamp` - block timestamp to be set fn start_warp(target: CheatTarget, block_timestamp: u64) { warp(target, block_timestamp, CheatSpan::Indefinite); } +/// Cancels the `warp` / `start_warp` for the given target. +/// - `target` - instance of `CheatTarget` specifying which contracts to stop warping fn stop_warp(target: CheatTarget) { let mut inputs = array![]; target.serialize(ref inputs);