forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.rs
403 lines (358 loc) · 11.2 KB
/
context.rs
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
use crate::account::*;
use anchor_lang::prelude::*;
use anchor_lang::accounts::loader::Loader;
use anchor_lang::accounts::cpi_state::CpiState;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
use misc2::misc2::MyState as Misc2State;
use std::mem::size_of;
#[derive(Accounts)]
#[instruction(token_bump: u8, mint_bump: u8)]
pub struct TestTokenSeedsInit<'info> {
#[account(
init,
seeds = [b"my-mint-seed".as_ref()],
bump = mint_bump,
payer = authority,
mint::decimals = 6,
mint::authority = authority,
)]
pub mint: Account<'info, Mint>,
#[account(
init,
seeds = [b"my-token-seed".as_ref(),],
bump = token_bump,
payer = authority,
token::mint = mint,
token::authority = authority,
)]
pub my_pda: Account<'info, TokenAccount>,
pub authority: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub token_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitAssociatedToken<'info> {
#[account(
init,
payer = payer,
associated_token::mint = mint,
associated_token::authority = payer,
)]
pub token: Account<'info, TokenAccount>,
pub mint: Account<'info, Mint>,
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
}
#[derive(Accounts)]
pub struct TestValidateAssociatedToken<'info> {
#[account(
associated_token::mint = mint,
associated_token::authority = wallet,
)]
pub token: Account<'info, TokenAccount>,
pub mint: Account<'info, Mint>,
pub wallet: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(nonce: u8)]
pub struct TestInstructionConstraint<'info> {
#[account(
seeds = [b"my-seed", my_account.key.as_ref()],
bump = nonce,
)]
pub my_pda: AccountInfo<'info>,
pub my_account: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(domain: String, seed: Vec<u8>, bump: u8)]
pub struct TestPdaInit<'info> {
#[account(
init,
seeds = [b"my-seed", domain.as_bytes(), foo.key.as_ref(), &seed],
bump = bump,
payer = my_payer,
)]
pub my_pda: Account<'info, DataU16>,
pub my_payer: AccountInfo<'info>,
pub foo: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct TestPdaInitZeroCopy<'info> {
#[account(
init,
seeds = [b"my-seed".as_ref()],
bump = bump,
payer = my_payer,
)]
pub my_pda: Loader<'info, DataZeroCopy>,
pub my_payer: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestPdaMutZeroCopy<'info> {
#[account(
mut,
seeds = [b"my-seed".as_ref()],
bump = my_pda.load()?.bump,
)]
pub my_pda: Loader<'info, DataZeroCopy>,
pub my_payer: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Ctor {}
#[derive(Accounts)]
pub struct RemainingAccounts {}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(zero)]
pub data: Account<'info, Data>,
}
#[derive(Accounts)]
pub struct InitializeSkipRentExempt<'info> {
#[account(zero, rent_exempt = skip)]
pub data: Account<'info, Data>,
}
#[derive(Accounts)]
pub struct InitializeNoRentExempt<'info> {
pub data: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestOwner<'info> {
#[account(owner = *misc.key)]
pub data: AccountInfo<'info>,
pub misc: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestExecutable<'info> {
#[account(executable)]
pub program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestStateCpi<'info> {
#[account(signer)]
pub authority: AccountInfo<'info>,
#[account(mut, state = misc2_program)]
pub cpi_state: CpiState<'info, Misc2State>,
#[account(executable)]
pub misc2_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestClose<'info> {
#[account(mut, close = sol_dest)]
pub data: Account<'info, Data>,
sol_dest: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestU16<'info> {
#[account(zero)]
pub my_account: Account<'info, DataU16>,
}
#[derive(Accounts)]
pub struct TestI16<'info> {
#[account(zero)]
pub data: Account<'info, DataI16>,
}
#[derive(Accounts)]
pub struct TestSimulate {}
#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(zero)]
pub data: Account<'info, DataI8>,
}
#[derive(Accounts)]
pub struct TestInit<'info> {
#[account(init, payer = payer)]
pub data: Account<'info, DataI8>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitZeroCopy<'info> {
#[account(init, payer = payer, space = 8 + size_of::<DataZeroCopy>())]
pub data: Loader<'info, DataZeroCopy>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitMint<'info> {
#[account(init, mint::decimals = 6, mint::authority = payer, mint::freeze_authority = payer, payer = payer)]
pub mint: Account<'info, Mint>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitToken<'info> {
#[account(init, token::mint = mint, token::authority = payer, payer = payer)]
pub token: Account<'info, TokenAccount>,
pub mint: Account<'info, Mint>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestCompositePayer<'info> {
pub composite: TestInit<'info>,
#[account(init, payer = composite.payer, space = 8 + size_of::<Data>())]
pub data: Account<'info, Data>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TestFetchAll<'info> {
#[account(init, payer = authority)]
pub data: Account<'info, DataWithFilter>,
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TestInitWithEmptySeeds<'info> {
#[account(init, seeds = [], bump, payer = authority, space = 8 + size_of::<Data>())]
pub pda: Account<'info, Data>,
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TestEmptySeedsConstraint<'info> {
#[account(seeds = [], bump)]
pub pda: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitWithSpace<'info> {
#[account(init, payer = payer)]
pub data: Account<'info, DataU16>,
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TestInitIfNeeded<'info> {
#[account(init_if_needed, payer = payer, space = 500)]
pub data: Account<'info, DataU16>,
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TestInitIfNeededChecksOwner<'info> {
#[account(init_if_needed, payer = payer, space = 100, owner = *owner.key, seeds = [b"hello"], bump)]
pub data: UncheckedAccount<'info>,
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
pub owner: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(seed_data: String)]
pub struct TestInitIfNeededChecksSeeds<'info> {
#[account(init_if_needed, payer = payer, space = 100, seeds = [seed_data.as_bytes()], bump)]
pub data: UncheckedAccount<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(decimals: u8)]
pub struct TestInitMintIfNeeded<'info> {
#[account(init_if_needed, mint::decimals = decimals, mint::authority = mint_authority, mint::freeze_authority = freeze_authority, payer = payer)]
pub mint: Account<'info, Mint>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub mint_authority: AccountInfo<'info>,
pub freeze_authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitTokenIfNeeded<'info> {
#[account(init_if_needed, token::mint = mint, token::authority = authority, payer = payer)]
pub token: Account<'info, TokenAccount>,
pub mint: Account<'info, Mint>,
#[account(signer)]
pub payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestInitAssociatedTokenIfNeeded<'info> {
#[account(
init_if_needed,
payer = payer,
associated_token::mint = mint,
associated_token::authority = authority,
)]
pub token: Account<'info, TokenAccount>,
pub mint: Account<'info, Mint>,
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestMultidimensionalArray<'info> {
#[account(zero)]
pub data: Account<'info, DataMultidimensionalArray>,
}
#[derive(Accounts)]
pub struct TestConstArraySize<'info> {
#[account(zero)]
pub data: Account<'info, DataConstArraySize>,
}
#[derive(Accounts)]
pub struct NoRentExempt<'info> {
pub data: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct EnforceRentExempt<'info> {
#[account(rent_exempt = enforce)]
pub data: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitDecreaseLamports<'info> {
#[account(init, payer = user, space = 1000)]
pub data: AccountInfo<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
}
#[derive(Accounts)]
pub struct InitIfNeededChecksRentExemption<'info> {
#[account(init_if_needed, payer = user, space = 1000)]
pub data: AccountInfo<'info>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>
}
#[derive(Accounts)]
#[instruction(bump: u8, second_bump: u8)]
pub struct TestProgramIdConstraint<'info> {
// not a real associated token account
// just deriving like this for testing purposes
#[account(seeds = [b"seed"], bump = bump, seeds::program = anchor_spl::associated_token::ID)]
first: AccountInfo<'info>,
#[account(seeds = [b"seed"], bump = second_bump, seeds::program = crate::ID)]
second: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TestProgramIdConstraintUsingFindPda<'info> {
// not a real associated token account
// just deriving like this for testing purposes
#[account(seeds = [b"seed"], bump, seeds::program = anchor_spl::associated_token::ID)]
first: AccountInfo<'info>,
#[account(seeds = [b"seed"], bump, seeds::program = crate::ID)]
second: AccountInfo<'info>,
}