Skip to content

Commit d342f2b

Browse files
Add tests for cheat_account_contract_address cheatcode (#3722)
<!-- Reference any GitHub issues resolved by this PR --> Advances #2145 ## Introduced changes <!-- A brief description of the changes --> - Adds tests for `cheat_account_contract_address` cheatcode ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [ ] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md` --------- Co-authored-by: ddoktorski <[email protected]>
1 parent ae5da24 commit d342f2b

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::{
2+
runtime_extensions::forge_runtime_extension::cheatcodes::cheat_execution_info::{
3+
CheatArguments, ExecutionInfoMockOperations, Operation, TxInfoMockOperations,
4+
},
5+
state::{CheatSpan, CheatnetState},
6+
};
7+
use starknet_api::core::ContractAddress;
8+
9+
impl CheatnetState {
10+
pub fn cheat_account_contract_address(
11+
&mut self,
12+
target: ContractAddress,
13+
account_contract_address: ContractAddress,
14+
span: CheatSpan,
15+
) {
16+
self.cheat_execution_info(ExecutionInfoMockOperations {
17+
tx_info: TxInfoMockOperations {
18+
account_contract_address: Operation::Start(CheatArguments {
19+
value: account_contract_address.into(),
20+
span,
21+
target,
22+
}),
23+
..Default::default()
24+
},
25+
..Default::default()
26+
});
27+
}
28+
29+
pub fn start_cheat_account_contract_address(
30+
&mut self,
31+
target: ContractAddress,
32+
account_contract_address: ContractAddress,
33+
) {
34+
self.cheat_account_contract_address(
35+
target,
36+
account_contract_address,
37+
CheatSpan::Indefinite,
38+
);
39+
}
40+
41+
pub fn stop_cheat_account_contract_address(&mut self, target: ContractAddress) {
42+
self.cheat_execution_info(ExecutionInfoMockOperations {
43+
tx_info: TxInfoMockOperations {
44+
account_contract_address: Operation::Stop(target),
45+
..Default::default()
46+
},
47+
..Default::default()
48+
});
49+
}
50+
51+
pub fn start_cheat_account_contract_address_global(
52+
&mut self,
53+
account_contract_address: ContractAddress,
54+
) {
55+
self.cheat_execution_info(ExecutionInfoMockOperations {
56+
tx_info: TxInfoMockOperations {
57+
account_contract_address: Operation::StartGlobal(account_contract_address.into()),
58+
..Default::default()
59+
},
60+
..Default::default()
61+
});
62+
}
63+
64+
pub fn stop_cheat_account_contract_address_global(
65+
&mut self,
66+
account_contract_address: ContractAddress,
67+
) {
68+
self.cheat_execution_info(ExecutionInfoMockOperations {
69+
tx_info: TxInfoMockOperations {
70+
account_contract_address: Operation::StartGlobal(account_contract_address.into()),
71+
..Default::default()
72+
},
73+
..Default::default()
74+
});
75+
}
76+
}

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/cheatcodes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use cairo_vm::vm::errors::hint_errors::HintError;
33
use runtime::EnhancedHintError;
44
use starknet_types_core::felt::Felt;
55

6+
pub mod cheat_account_contract_address;
67
pub mod cheat_block_hash;
78
pub mod cheat_block_number;
89
pub mod cheat_block_timestamp;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::num::NonZeroUsize;
2+
3+
use crate::{cheatcodes::test_environment::TestEnvironment, common::assertions::assert_success};
4+
use cheatnet::state::CheatSpan;
5+
use starknet_api::core::ContractAddress;
6+
use starknet_types_core::felt::Felt;
7+
8+
trait CheatAccountContractAddressTrait {
9+
fn cheat_account_contract_address(
10+
&mut self,
11+
target: ContractAddress,
12+
new_address: u128,
13+
span: CheatSpan,
14+
);
15+
fn start_cheat_account_contract_address(&mut self, target: ContractAddress, new_address: u128);
16+
fn stop_cheat_account_contract_address(&mut self, target: ContractAddress);
17+
}
18+
19+
impl CheatAccountContractAddressTrait for TestEnvironment {
20+
fn cheat_account_contract_address(
21+
&mut self,
22+
target: ContractAddress,
23+
new_address: u128,
24+
span: CheatSpan,
25+
) {
26+
self.cheatnet_state.cheat_account_contract_address(
27+
target,
28+
ContractAddress::from(new_address),
29+
span,
30+
);
31+
}
32+
33+
fn start_cheat_account_contract_address(&mut self, target: ContractAddress, new_address: u128) {
34+
self.cheatnet_state
35+
.start_cheat_account_contract_address(target, ContractAddress::from(new_address));
36+
}
37+
38+
fn stop_cheat_account_contract_address(&mut self, target: ContractAddress) {
39+
self.cheatnet_state
40+
.stop_cheat_account_contract_address(target);
41+
}
42+
}
43+
44+
#[test]
45+
fn cheat_account_contract_address_simple() {
46+
let mut test_env = TestEnvironment::new();
47+
48+
let contract_address = test_env.deploy("CheatTxInfoChecker", &[]);
49+
50+
let output = test_env.call_contract(&contract_address, "get_account_contract_address", &[]);
51+
assert_success(output, &[Felt::from(0)]);
52+
53+
test_env.start_cheat_account_contract_address(contract_address, 123);
54+
55+
let output = test_env.call_contract(&contract_address, "get_account_contract_address", &[]);
56+
assert_success(output, &[Felt::from(123)]);
57+
}
58+
59+
#[test]
60+
fn cheat_account_contract_address_stop() {
61+
let mut test_env = TestEnvironment::new();
62+
63+
let contract_address = test_env.deploy("CheatTxInfoChecker", &[]);
64+
65+
test_env.start_cheat_account_contract_address(contract_address, 123);
66+
67+
assert_success(
68+
test_env.call_contract(&contract_address, "get_account_contract_address", &[]),
69+
&[Felt::from(123)],
70+
);
71+
72+
test_env.stop_cheat_account_contract_address(contract_address);
73+
74+
assert_success(
75+
test_env.call_contract(&contract_address, "get_account_contract_address", &[]),
76+
&[Felt::from(0)],
77+
);
78+
}
79+
80+
#[test]
81+
fn cheat_account_contract_address_simple_with_span() {
82+
let mut test_env = TestEnvironment::new();
83+
84+
let contract_address = test_env.deploy("CheatTxInfoChecker", &[]);
85+
86+
test_env.cheat_account_contract_address(
87+
contract_address,
88+
123,
89+
CheatSpan::TargetCalls(NonZeroUsize::new(2).unwrap()),
90+
);
91+
92+
assert_success(
93+
test_env.call_contract(&contract_address, "get_account_contract_address", &[]),
94+
&[Felt::from(123)],
95+
);
96+
assert_success(
97+
test_env.call_contract(&contract_address, "get_account_contract_address", &[]),
98+
&[Felt::from(123)],
99+
);
100+
assert_success(
101+
test_env.call_contract(&contract_address, "get_account_contract_address", &[]),
102+
&[Felt::from(0)],
103+
);
104+
}

crates/cheatnet/tests/cheatcodes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod test_environment;
22

3+
mod cheat_account_contract_address;
34
mod cheat_block_hash;
45
mod cheat_block_number;
56
mod cheat_block_timestamp;

crates/cheatnet/tests/contracts/src/cheat_tx_info/tx_info_checker.cairo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
use starknet::ContractAddress;
2+
13
#[starknet::interface]
24
trait ICheatTxInfoChecker<TContractState> {
5+
fn get_account_contract_address(ref self: TContractState) -> ContractAddress;
36
fn get_transaction_hash(self: @TContractState) -> felt252;
47
fn get_tx_hash_and_emit_event(ref self: TContractState) -> felt252;
58
fn get_tx_info(self: @TContractState) -> starknet::TxInfo;
69
}
710

811
#[starknet::contract]
912
mod CheatTxInfoChecker {
10-
use starknet::SyscallResultTrait;
1113
use starknet::syscalls::get_execution_info_v2_syscall;
14+
use starknet::{ContractAddress, SyscallResultTrait, get_tx_info};
1215

1316
#[storage]
1417
struct Storage {}
@@ -40,5 +43,10 @@ mod CheatTxInfoChecker {
4043
let execution_info = get_execution_info_v2_syscall().unwrap_syscall().unbox();
4144
execution_info.tx_info.unbox()
4245
}
46+
47+
fn get_account_contract_address(ref self: ContractState) -> ContractAddress {
48+
let tx_info = get_tx_info();
49+
tx_info.account_contract_address
50+
}
4351
}
4452
}

0 commit comments

Comments
 (0)