1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! Assertion helpers for testing account states
//!
//! This module provides convenient assertion methods for verifying
//! account states in tests.
use crate::AnchorContext;
use solana_program::pubkey::Pubkey;
use litesvm_token::spl_token;
use solana_program_pack::Pack;
/// Assertion helper methods for AnchorContext
pub trait AssertionHelpers {
/// Assert that an account is closed (doesn't exist or has 0 lamports and 0 data)
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let account = Pubkey::new_unique();
/// ctx.assert_account_closed(&account);
/// ```
fn assert_account_closed(&self, pubkey: &Pubkey);
/// Assert that an account exists
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let account = Pubkey::new_unique();
/// ctx.assert_account_exists(&account);
/// ```
fn assert_account_exists(&self, pubkey: &Pubkey);
/// Assert token account balance
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let token_account = Pubkey::new_unique();
/// ctx.assert_token_balance(&token_account, 1_000_000_000); // 1 token with 9 decimals
/// ```
fn assert_token_balance(&self, token_account: &Pubkey, expected: u64);
/// Assert token account balance with custom message
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let token_account = Pubkey::new_unique();
/// ctx.assert_token_balance_with_msg(
/// &token_account,
/// 1_000_000_000,
/// "Maker should have 1 token"
/// );
/// ```
fn assert_token_balance_with_msg(&self, token_account: &Pubkey, expected: u64, msg: &str);
/// Assert account lamports balance
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let account = Pubkey::new_unique();
/// ctx.assert_account_lamports(&account, 1_000_000_000);
/// ```
fn assert_account_lamports(&self, pubkey: &Pubkey, expected: u64);
/// Assert account owner
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let account = Pubkey::new_unique();
/// # let expected_owner = Pubkey::new_unique();
/// ctx.assert_account_owner(&account, &expected_owner);
/// ```
fn assert_account_owner(&self, pubkey: &Pubkey, expected_owner: &Pubkey);
/// Assert that multiple accounts are closed
///
/// # Example
/// ```no_run
/// # use anchor_litesvm::{AnchorContext, AssertionHelpers};
/// # use litesvm::LiteSVM;
/// # use solana_program::pubkey::Pubkey;
/// # let ctx = AnchorContext::new(LiteSVM::new(), Pubkey::new_unique());
/// # let account1 = Pubkey::new_unique();
/// # let account2 = Pubkey::new_unique();
/// ctx.assert_accounts_closed(&[&account1, &account2]);
/// ```
fn assert_accounts_closed(&self, pubkeys: &[&Pubkey]);
}
impl AssertionHelpers for AnchorContext {
fn assert_account_closed(&self, pubkey: &Pubkey) {
match self.svm.get_account(pubkey) {
None => {
// Account doesn't exist - that's fine, it's closed
}
Some(account) => {
// In LiteSVM, closed accounts might still exist with 0 lamports and empty data
assert!(
account.lamports == 0 && account.data.is_empty(),
"Account {} should be closed but has {} lamports and {} bytes of data",
pubkey,
account.lamports,
account.data.len()
);
}
}
}
fn assert_account_exists(&self, pubkey: &Pubkey) {
assert!(
self.svm.get_account(pubkey).is_some(),
"Account {} should exist but was not found",
pubkey
);
}
fn assert_token_balance(&self, token_account: &Pubkey, expected: u64) {
self.assert_token_balance_with_msg(
token_account,
expected,
&format!("Token balance mismatch for {}", token_account),
);
}
fn assert_token_balance_with_msg(&self, token_account: &Pubkey, expected: u64, msg: &str) {
match self.svm.get_account(token_account) {
None => {
if expected == 0 {
// Account doesn't exist and we expect 0 balance - that's fine
return;
}
panic!(
"{}: Account {} doesn't exist but expected {} tokens",
msg, token_account, expected
);
}
Some(account) => {
let token_state = spl_token::state::Account::unpack(&account.data)
.expect(&format!("Failed to unpack token account {}", token_account));
assert_eq!(
token_state.amount, expected,
"{}: expected {} tokens, got {}",
msg, expected, token_state.amount
);
}
}
}
fn assert_account_lamports(&self, pubkey: &Pubkey, expected: u64) {
let account = self
.svm
.get_account(pubkey)
.expect(&format!("Account {} should exist", pubkey));
assert_eq!(
account.lamports, expected,
"Account {} lamports mismatch: expected {}, got {}",
pubkey, expected, account.lamports
);
}
fn assert_account_owner(&self, pubkey: &Pubkey, expected_owner: &Pubkey) {
let account = self
.svm
.get_account(pubkey)
.expect(&format!("Account {} should exist", pubkey));
assert_eq!(
account.owner, *expected_owner,
"Account {} owner mismatch: expected {}, got {}",
pubkey, expected_owner, account.owner
);
}
fn assert_accounts_closed(&self, pubkeys: &[&Pubkey]) {
for pubkey in pubkeys {
self.assert_account_closed(pubkey);
}
}
}
/// Additional assertion functions that don't require self
pub mod assertions {
use super::*;
/// Assert that two pubkeys are equal with a custom message
pub fn assert_pubkey_eq(actual: &Pubkey, expected: &Pubkey, msg: &str) {
assert_eq!(
actual, expected,
"{}: expected {}, got {}",
msg, expected, actual
);
}
/// Assert that a value is within a range
pub fn assert_in_range(value: u64, min: u64, max: u64, msg: &str) {
assert!(
value >= min && value <= max,
"{}: value {} is not in range [{}, {}]",
msg,
value,
min,
max
);
}
/// Assert that a token amount matches expected value within some tolerance
/// Useful for dealing with rounding in token calculations
pub fn assert_token_amount_approx(actual: u64, expected: u64, tolerance: u64, msg: &str) {
let diff = if actual > expected {
actual - expected
} else {
expected - actual
};
assert!(
diff <= tolerance,
"{}: expected {} ± {}, got {} (diff: {})",
msg,
expected,
tolerance,
actual,
diff
);
}
}