forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
285 lines (234 loc) · 7.61 KB
/
lib.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
//! Misc example is a catchall program for testing unrelated features.
//! It's not too instructive/coherent by itself, so please see other examples.
use anchor_lang::prelude::*;
use context::*;
use event::*;
use misc2::Auth;
mod account;
mod context;
mod event;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[constant]
pub const BASE: u128 = 1_000_000;
#[constant]
pub const DECIMALS: u8 = 6;
pub const NO_IDL: u16 = 55;
#[program]
pub mod misc {
use super::*;
pub const SIZE: u64 = 99;
#[state(SIZE)]
pub struct MyState {
pub v: Vec<u8>,
}
impl MyState {
pub fn new(_ctx: Context<Ctor>) -> Result<Self, ProgramError> {
Ok(Self { v: vec![] })
}
pub fn remaining_accounts(&mut self, ctx: Context<RemainingAccounts>) -> ProgramResult {
if ctx.remaining_accounts.len() != 1 {
return Err(ProgramError::Custom(1)); // Arbitrary error.
}
Ok(())
}
}
pub fn initialize(ctx: Context<Initialize>, udata: u128, idata: i128) -> ProgramResult {
ctx.accounts.data.udata = udata;
ctx.accounts.data.idata = idata;
Ok(())
}
pub fn initialize_no_rent_exempt(ctx: Context<InitializeNoRentExempt>) -> ProgramResult {
Ok(())
}
pub fn initialize_skip_rent_exempt(ctx: Context<InitializeSkipRentExempt>) -> ProgramResult {
Ok(())
}
pub fn test_owner(_ctx: Context<TestOwner>) -> ProgramResult {
Ok(())
}
pub fn test_executable(_ctx: Context<TestExecutable>) -> ProgramResult {
Ok(())
}
pub fn test_state_cpi(ctx: Context<TestStateCpi>, data: u64) -> ProgramResult {
let cpi_program = ctx.accounts.misc2_program.clone();
let cpi_accounts = Auth {
authority: ctx.accounts.authority.clone(),
};
let ctx = ctx.accounts.cpi_state.context(cpi_program, cpi_accounts);
misc2::cpi::state::set_data(ctx, data)
}
pub fn test_u16(ctx: Context<TestU16>, data: u16) -> ProgramResult {
ctx.accounts.my_account.data = data;
Ok(())
}
pub fn test_simulate(_ctx: Context<TestSimulate>, data: u32) -> ProgramResult {
emit!(E1 { data });
emit!(E2 { data: 1234 });
emit!(E3 { data: 9 });
Ok(())
}
pub fn test_i8(ctx: Context<TestI8>, data: i8) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}
pub fn test_i16(ctx: Context<TestI16>, data: i16) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}
pub fn test_const_array_size(ctx: Context<TestConstArraySize>, data: u8) -> ProgramResult {
ctx.accounts.data.data[0] = data;
Ok(())
}
pub fn test_close(_ctx: Context<TestClose>) -> ProgramResult {
Ok(())
}
pub fn test_instruction_constraint(
_ctx: Context<TestInstructionConstraint>,
_nonce: u8,
) -> ProgramResult {
Ok(())
}
pub fn test_pda_init(
ctx: Context<TestPdaInit>,
_domain: String,
_seed: Vec<u8>,
_bump: u8,
) -> ProgramResult {
ctx.accounts.my_pda.data = 6;
Ok(())
}
pub fn test_pda_init_zero_copy(ctx: Context<TestPdaInitZeroCopy>, bump: u8) -> ProgramResult {
let mut acc = ctx.accounts.my_pda.load_init()?;
acc.data = 9;
acc.bump = bump;
Ok(())
}
pub fn test_pda_mut_zero_copy(ctx: Context<TestPdaMutZeroCopy>) -> ProgramResult {
let mut acc = ctx.accounts.my_pda.load_mut()?;
acc.data = 1234;
Ok(())
}
pub fn test_token_seeds_init(
_ctx: Context<TestTokenSeedsInit>,
_token_bump: u8,
_mint_bump: u8,
) -> ProgramResult {
Ok(())
}
pub fn default<'info>(
_program_id: &Pubkey,
_accounts: &[AccountInfo<'info>],
_data: &[u8],
) -> ProgramResult {
Err(ProgramError::Custom(1234))
}
pub fn test_init(ctx: Context<TestInit>) -> ProgramResult {
ctx.accounts.data.data = 3;
Ok(())
}
pub fn test_init_zero_copy(ctx: Context<TestInitZeroCopy>) -> ProgramResult {
let mut data = ctx.accounts.data.load_init()?;
data.data = 10;
data.bump = 2;
Ok(())
}
pub fn test_init_mint(ctx: Context<TestInitMint>) -> ProgramResult {
assert!(ctx.accounts.mint.decimals == 6);
Ok(())
}
pub fn test_init_token(ctx: Context<TestInitToken>) -> ProgramResult {
assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
Ok(())
}
pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> ProgramResult {
ctx.accounts.composite.data.data = 1;
ctx.accounts.data.udata = 2;
ctx.accounts.data.idata = 3;
Ok(())
}
pub fn test_init_associated_token(ctx: Context<TestInitAssociatedToken>) -> ProgramResult {
assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
Ok(())
}
pub fn test_validate_associated_token(
_ctx: Context<TestValidateAssociatedToken>,
) -> ProgramResult {
Ok(())
}
pub fn test_fetch_all(ctx: Context<TestFetchAll>, filterable: Pubkey) -> ProgramResult {
ctx.accounts.data.authority = ctx.accounts.authority.key();
ctx.accounts.data.filterable = filterable;
Ok(())
}
pub fn test_init_with_empty_seeds(ctx: Context<TestInitWithEmptySeeds>) -> ProgramResult {
Ok(())
}
pub fn test_empty_seeds_constraint(ctx: Context<TestEmptySeedsConstraint>) -> ProgramResult {
Ok(())
}
pub fn test_init_if_needed(ctx: Context<TestInitIfNeeded>, data: u16) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}
pub fn test_init_if_needed_checks_owner(
ctx: Context<TestInitIfNeededChecksOwner>,
) -> ProgramResult {
Ok(())
}
pub fn test_init_if_needed_checks_seeds(
ctx: Context<TestInitIfNeededChecksSeeds>,
seed_data: String,
) -> ProgramResult {
Ok(())
}
pub fn test_init_mint_if_needed(
ctx: Context<TestInitMintIfNeeded>,
decimals: u8,
) -> ProgramResult {
Ok(())
}
pub fn test_init_token_if_needed(ctx: Context<TestInitTokenIfNeeded>) -> ProgramResult {
Ok(())
}
pub fn test_init_associated_token_if_needed(
ctx: Context<TestInitAssociatedTokenIfNeeded>,
) -> ProgramResult {
Ok(())
}
pub fn init_with_space(ctx: Context<InitWithSpace>, data: u16) -> ProgramResult {
Ok(())
}
pub fn test_multidimensional_array(
ctx: Context<TestMultidimensionalArray>,
data: [[u8; 10]; 10],
) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}
pub fn test_no_rent_exempt(ctx: Context<NoRentExempt>) -> ProgramResult {
Ok(())
}
pub fn test_enforce_rent_exempt(ctx: Context<EnforceRentExempt>) -> ProgramResult {
Ok(())
}
pub fn init_decrease_lamports(ctx: Context<InitDecreaseLamports>) -> ProgramResult {
**ctx.accounts.data.try_borrow_mut_lamports()? -= 1;
**ctx.accounts.user.try_borrow_mut_lamports()? += 1;
Ok(())
}
pub fn init_if_needed_checks_rent_exemption(_ctx: Context<InitIfNeededChecksRentExemption>) -> ProgramResult {
Ok(())
}
pub fn test_program_id_constraint(
_ctx: Context<TestProgramIdConstraint>,
_bump: u8,
_second_bump: u8
) -> ProgramResult {
Ok(())
}
pub fn test_program_id_constraint_find_pda(
_ctx: Context<TestProgramIdConstraintUsingFindPda>,
) -> ProgramResult {
Ok(())
}
}