forked from otter-sec/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
405 lines (338 loc) · 11.1 KB
/
Copy pathlib.rs
File metadata and controls
405 lines (338 loc) · 11.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Misc optional example is a catchall program for testing unrelated features.
//! It's not too instructive/coherent by itself, so please see other examples.
use account::*;
use anchor_lang::prelude::*;
use context::*;
use event::*;
mod account;
mod context;
mod event;
declare_id!("FNqz6pqLAwvMSds2FYjR4nKV3moVpPNtvkfGFrqLKrgG");
#[constant]
pub const BASE: u128 = 1_000_000;
#[constant]
pub const DECIMALS: u8 = 6;
pub const NO_IDL: u16 = 55;
#[program]
pub mod misc_optional {
use super::*;
pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().udata = udata;
ctx.accounts.data.as_mut().unwrap().idata = idata;
Ok(())
}
pub fn initialize_no_rent_exempt(_ctx: Context<InitializeNoRentExempt>) -> Result<()> {
Ok(())
}
pub fn initialize_skip_rent_exempt(_ctx: Context<InitializeSkipRentExempt>) -> Result<()> {
Ok(())
}
pub fn test_owner(_ctx: Context<TestOwner>) -> Result<()> {
Ok(())
}
pub fn test_executable(_ctx: Context<TestExecutable>) -> Result<()> {
Ok(())
}
pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> Result<()> {
emit!(E1 { data });
emit!(E2 { data: 1234 });
emit!(E3 { data: 9 });
emit!(E5 {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
});
emit!(E6 {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
});
Ok(())
}
pub fn test_const_array_size(ctx: Context<TestConstArraySize>, data: u8) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data[0] = data;
Ok(())
}
pub fn test_const_ix_data_size(
ctx: Context<TestConstIxDataSize>,
data: [u8; MAX_SIZE],
) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
}
pub fn test_close(_ctx: Context<TestClose>) -> Result<()> {
Ok(())
}
pub fn test_close_twice(ctx: Context<TestCloseTwice>) -> Result<()> {
let data_account = &ctx.accounts.data.as_ref().unwrap();
let sol_dest_info = ctx.accounts.sol_dest.as_ref().unwrap().to_account_info();
data_account.close(sol_dest_info)?;
let data_account_info: &AccountInfo = data_account.as_ref();
require_keys_eq!(*data_account_info.owner, System::id());
Ok(())
}
pub fn test_close_mut(ctx: Context<TestCloseMut>) -> Result<()> {
let data_account = &ctx.accounts.data.as_ref().unwrap();
let sol_dest_info = ctx.accounts.sol_dest.as_ref().unwrap().to_account_info();
data_account.close(sol_dest_info)?;
let data_account_info: &AccountInfo = data_account.as_ref();
require_keys_eq!(*data_account_info.owner, System::id());
Ok(())
}
pub fn test_instruction_constraint(
_ctx: Context<TestInstructionConstraint>,
_nonce: u8,
) -> Result<()> {
Ok(())
}
pub fn test_pda_init(
ctx: Context<TestPdaInit>,
_domain: String,
_seed: Vec<u8>,
_bump: u8,
) -> Result<()> {
ctx.accounts.my_pda.as_mut().unwrap().data = 6;
Ok(())
}
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>) -> Result<()> {
let mut acc = ctx.accounts.my_pda.as_ref().unwrap().load_init()?;
acc.data = 9;
acc.bump = ctx.bumps.my_pda.unwrap();
Ok(())
}
pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> Result<()> {
let mut acc = ctx.accounts.my_pda.as_mut().unwrap().load_mut()?;
acc.data = 1234;
Ok(())
}
pub fn test_token_seeds_init(_ctx: Context<TestTokenSeedsInit>) -> Result<()> {
Ok(())
}
pub fn default<'info>(
_program_id: &Pubkey,
_accounts: &[AccountInfo<'info>],
_data: &[u8],
) -> Result<()> {
Err(ProgramError::Custom(1234).into())
}
pub fn test_init(ctx: Context<TestInit>) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = 3;
Ok(())
}
pub fn test_init_zero_copy(ctx: Context<TestInitZeroCopy>) -> Result<()> {
let mut data = ctx.accounts.data.as_ref().unwrap().load_init()?;
data.data = 10;
data.bump = 2;
Ok(())
}
pub fn test_init_mint(ctx: Context<TestInitMint>) -> Result<()> {
assert!(ctx.accounts.mint.as_ref().unwrap().decimals == 6);
Ok(())
}
pub fn test_init_mint_with_token_program(
_ctx: Context<TestInitMintWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn test_init_token(ctx: Context<TestInitToken>) -> Result<()> {
assert!(
ctx.accounts.token.as_ref().unwrap().mint == ctx.accounts.mint.as_ref().unwrap().key()
);
Ok(())
}
pub fn test_init_token_with_token_program(
_ctx: Context<TestInitTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> Result<()> {
ctx.accounts.composite.data.as_mut().unwrap().data = 1;
ctx.accounts.data.as_mut().unwrap().udata = 2;
ctx.accounts.data.as_mut().unwrap().idata = 3;
Ok(())
}
pub fn test_init_associated_token(ctx: Context<TestInitAssociatedToken>) -> Result<()> {
assert!(
ctx.accounts.token.as_ref().unwrap().mint == ctx.accounts.mint.as_ref().unwrap().key()
);
Ok(())
}
pub fn test_init_associated_token_with_token_program(
_ctx: Context<TestInitAssociatedTokenWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn test_validate_associated_token(
_ctx: Context<TestValidateAssociatedToken>,
) -> Result<()> {
Ok(())
}
pub fn test_fetch_all(ctx: Context<TestFetchAll>, filterable: Pubkey) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().authority =
ctx.accounts.authority.as_ref().unwrap().key();
ctx.accounts.data.as_mut().unwrap().filterable = filterable;
Ok(())
}
pub fn test_init_with_empty_seeds(_ctx: Context<TestInitWithEmptySeeds>) -> Result<()> {
Ok(())
}
pub fn test_empty_seeds_constraint(_ctx: Context<TestEmptySeedsConstraint>) -> Result<()> {
Ok(())
}
pub fn test_init_if_needed(ctx: Context<TestInitIfNeeded>, data: u16) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
}
pub fn test_init_if_needed_checks_owner(
_ctx: Context<TestInitIfNeededChecksOwner>,
) -> Result<()> {
Ok(())
}
pub fn test_init_if_needed_checks_seeds(
_ctx: Context<TestInitIfNeededChecksSeeds>,
_seed_data: String,
) -> Result<()> {
Ok(())
}
pub fn test_init_mint_if_needed(
_ctx: Context<TestInitMintIfNeeded>,
_decimals: u8,
) -> Result<()> {
Ok(())
}
pub fn test_init_mint_if_needed_with_token_program(
_ctx: Context<TestInitMintIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn test_init_token_if_needed(_ctx: Context<TestInitTokenIfNeeded>) -> Result<()> {
Ok(())
}
pub fn test_init_token_if_needed_with_token_program(
_ctx: Context<TestInitTokenIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn test_init_associated_token_if_needed(
_ctx: Context<TestInitAssociatedTokenIfNeeded>,
) -> Result<()> {
Ok(())
}
pub fn test_init_associated_token_if_needed_with_token_program(
_ctx: Context<TestInitAssociatedTokenIfNeededWithTokenProgram>,
) -> Result<()> {
Ok(())
}
pub fn init_with_space(_ctx: Context<InitWithSpace>, _data: u16) -> Result<()> {
Ok(())
}
pub fn test_multidimensional_array(
ctx: Context<TestMultidimensionalArray>,
data: [[u8; 10]; 10],
) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
}
pub fn test_multidimensional_array_const_sizes(
ctx: Context<TestMultidimensionalArrayConstSizes>,
data: [[u8; 11]; 10],
) -> Result<()> {
ctx.accounts.data.as_mut().unwrap().data = data;
Ok(())
}
pub fn test_no_rent_exempt(_ctx: Context<NoRentExempt>) -> Result<()> {
Ok(())
}
pub fn test_enforce_rent_exempt(_ctx: Context<EnforceRentExempt>) -> Result<()> {
Ok(())
}
pub fn init_decrease_lamports(ctx: Context<InitDecreaseLamports>) -> Result<()> {
**ctx
.accounts
.data
.as_mut()
.unwrap()
.try_borrow_mut_lamports()? -= 1;
**ctx
.accounts
.user
.as_mut()
.unwrap()
.try_borrow_mut_lamports()? += 1;
Ok(())
}
pub fn init_if_needed_checks_rent_exemption(
_ctx: Context<InitIfNeededChecksRentExemption>,
) -> Result<()> {
Ok(())
}
pub fn test_program_id_constraint(
_ctx: Context<TestProgramIdConstraint>,
_bump: u8,
_second_bump: u8,
) -> Result<()> {
Ok(())
}
pub fn test_program_id_constraint_find_pda(
_ctx: Context<TestProgramIdConstraintUsingFindPda>,
) -> Result<()> {
Ok(())
}
pub fn test_token_constraint(_ctx: Context<TestConstraintToken>) -> Result<()> {
Ok(())
}
pub fn test_token_auth_constraint(_ctx: Context<TestAuthorityConstraint>) -> Result<()> {
Ok(())
}
pub fn test_only_auth_constraint(_ctx: Context<TestOnlyAuthorityConstraint>) -> Result<()> {
Ok(())
}
pub fn test_only_mint_constraint(_ctx: Context<TestOnlyMintConstraint>) -> Result<()> {
Ok(())
}
pub fn test_only_token_program_constraint(
_ctx: Context<TestOnlyTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}
pub fn test_mint_constraint(_ctx: Context<TestMintConstraint>, _decimals: u8) -> Result<()> {
Ok(())
}
pub fn test_mint_only_decimals_constraint(
_ctx: Context<TestMintOnlyDecimalsConstraint>,
_decimals: u8,
) -> Result<()> {
Ok(())
}
pub fn test_mint_only_auth_constraint(
_ctx: Context<TestMintAuthorityConstraint>,
) -> Result<()> {
Ok(())
}
pub fn test_mint_only_one_auth_constraint(
_ctx: Context<TestMintOneAuthorityConstraint>,
) -> Result<()> {
Ok(())
}
pub fn test_mint_miss_mint_auth_constraint(
_ctx: Context<TestMintMissMintAuthConstraint>,
_decimals: u8,
) -> Result<()> {
Ok(())
}
pub fn test_mint_only_token_program_constraint(
_ctx: Context<TestMintOnlyTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}
pub fn test_associated_constraint(_ctx: Context<TestAssociatedToken>) -> Result<()> {
Ok(())
}
pub fn test_associated_token_with_token_program_constraint(
_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>,
) -> Result<()> {
Ok(())
}
#[allow(unused_variables)]
pub fn test_init_many_associated_token_accounts(
_ctx: Context<InitManyAssociatedTokenAccounts>,
) -> Result<()> {
Ok(())
}
}