-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathlib.rs
326 lines (296 loc) · 7.98 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
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
use anchor_lang::prelude::*;
use some_external_program;
use std::str::FromStr;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[constant]
pub const FOO_CONST: u128 = 1_000_000;
#[constant]
pub const BAR_CONST: u8 = 6;
/// This is an example program used for testing
#[program]
pub mod example_program {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.state.set_inner(State::default());
Ok(())
}
/// Initializes an account with specified values
pub fn initialize_with_values(
ctx: Context<Initialize>,
bool_field: bool,
u8_field: u8,
i8_field: i8,
u16_field: u16,
i16_field: i16,
u32_field: u32,
i32_field: i32,
f32_field: f32,
u64_field: u64,
i64_field: i64,
f64_field: f64,
u128_field: u128,
i128_field: i128,
bytes_field: Vec<u8>,
string_field: String,
pubkey_field: Pubkey,
vec_field: Vec<u64>,
vec_struct_field: Vec<FooStruct>,
option_field: Option<bool>,
option_struct_field: Option<FooStruct>,
struct_field: FooStruct,
array_field: [bool; 3],
enum_field_1: FooEnum,
enum_field_2: FooEnum,
enum_field_3: FooEnum,
enum_field_4: FooEnum,
) -> Result<()> {
ctx.accounts.state.set_inner(State {
bool_field,
u8_field,
i8_field,
u16_field,
i16_field,
u32_field,
i32_field,
f32_field,
u64_field,
i64_field,
f64_field,
u128_field,
i128_field,
bytes_field,
string_field,
pubkey_field,
vec_field,
vec_struct_field,
option_field,
option_struct_field,
struct_field,
array_field,
enum_field_1,
enum_field_2,
enum_field_3,
enum_field_4,
});
Ok(())
}
/// a separate instruction due to initialize_with_values having too many arguments
/// https://github.com/solana-labs/solana/issues/23978
pub fn initialize_with_values2(
ctx: Context<Initialize2>,
vec_of_option: Vec<Option<u64>>,
box_field: Box<bool>,
) -> Result<SomeRetStruct> {
ctx.accounts.state.set_inner(State2 { vec_of_option, box_field });
Ok(SomeRetStruct { some_field: 3})
}
pub fn cause_error(_ctx: Context<CauseError>) -> Result<()> {
return Err(error!(ErrorCode::SomeError));
}
}
/// Enum type
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub enum FooEnum {
/// Tuple kind
Unnamed(bool, u8, BarStruct),
UnnamedSingle(BarStruct),
Named {
/// A bool field inside a struct tuple kind
bool_field: bool,
u8_field: u8,
nested: BarStruct,
},
Struct(BarStruct),
OptionStruct(Option<BarStruct>),
VecStruct(Vec<BarStruct>),
NoFields,
}
/// Bar struct type
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct BarStruct {
/// Some field
some_field: bool,
other_field: u8,
}
impl Default for BarStruct {
fn default() -> Self {
return BarStruct {
some_field: true,
other_field: 10,
};
}
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct FooStruct {
field1: u8,
field2: u16,
nested: BarStruct,
vec_nested: Vec<BarStruct>,
option_nested: Option<BarStruct>,
enum_field: FooEnum,
}
impl Default for FooStruct {
fn default() -> Self {
return FooStruct {
field1: 123,
field2: 999,
nested: BarStruct::default(),
vec_nested: vec![BarStruct::default()],
option_nested: Some(BarStruct::default()),
enum_field: FooEnum::Named {
bool_field: true,
u8_field: 15,
nested: BarStruct::default(),
},
};
}
}
/// An account containing various fields
#[account]
pub struct State {
/// A boolean field
bool_field: bool,
u8_field: u8,
i8_field: i8,
u16_field: u16,
i16_field: i16,
u32_field: u32,
i32_field: i32,
f32_field: f32,
u64_field: u64,
i64_field: i64,
f64_field: f64,
u128_field: u128,
i128_field: i128,
bytes_field: Vec<u8>,
string_field: String,
pubkey_field: Pubkey,
vec_field: Vec<u64>,
vec_struct_field: Vec<FooStruct>,
option_field: Option<bool>,
option_struct_field: Option<FooStruct>,
struct_field: FooStruct,
array_field: [bool; 3],
enum_field_1: FooEnum,
enum_field_2: FooEnum,
enum_field_3: FooEnum,
enum_field_4: FooEnum,
}
impl Default for State {
fn default() -> Self {
// some arbitrary default values
return State {
bool_field: true,
u8_field: 234,
i8_field: -123,
u16_field: 62345,
i16_field: -31234,
u32_field: 1234567891,
i32_field: -1234567891,
f32_field: 123456.5,
u64_field: u64::MAX / 2 + 10,
i64_field: i64::MIN / 2 - 10,
f64_field: 1234567891.345,
u128_field: u128::MAX / 2 + 10,
i128_field: i128::MIN / 2 - 10,
bytes_field: vec![1, 2, 255, 254],
string_field: String::from("hello"),
pubkey_field: Pubkey::from_str("EPZP2wrcRtMxrAPJCXVEQaYD9eH7fH7h12YqKDcd4aS7").unwrap(),
vec_field: vec![1, 2, 100, 1000, u64::MAX],
vec_struct_field: vec![FooStruct::default()],
option_field: None,
option_struct_field: Some(FooStruct::default()),
struct_field: FooStruct::default(),
array_field: [true, false, true],
enum_field_1: FooEnum::Unnamed(false, 10, BarStruct::default()),
enum_field_2: FooEnum::Named {
bool_field: true,
u8_field: 20,
nested: BarStruct::default(),
},
enum_field_3: FooEnum::Struct(BarStruct::default()),
enum_field_4: FooEnum::NoFields,
};
}
}
#[account]
pub struct State2 {
vec_of_option: Vec<Option<u64>>,
box_field: Box<bool>,
}
impl Default for State2 {
fn default() -> Self {
return State2 {
vec_of_option: vec![None, Some(10)],
box_field: Box::new(true),
};
}
}
#[derive(Accounts)]
pub struct NestedAccounts<'info> {
/// Sysvar clock
clock: Sysvar<'info, Clock>,
rent: Sysvar<'info, Rent>,
}
#[derive(Accounts)]
pub struct Initialize<'info> {
/// State account
#[account(
init,
space = 8 + 1000, // TODO: use exact space required
payer = payer,
)]
state: Account<'info, State>,
nested: NestedAccounts<'info>,
zc_account: AccountLoader<'info, SomeZcAccount>,
#[account(mut)]
payer: Signer<'info>,
system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Initialize2<'info> {
#[account(
init,
space = 8 + 1000, // TODO: use exact space required
payer = payer,
)]
state: Account<'info, State2>,
#[account(mut)]
payer: Signer<'info>,
system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct CauseError {}
#[error_code]
pub enum ErrorCode {
#[msg("Example error.")]
SomeError,
#[msg("Another error.")]
OtherError,
ErrorWithoutMsg,
}
mod some_other_module {
use super::*;
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct Baz {
some_u8: u8,
}
}
#[event]
pub struct SomeEvent {
bool_field: bool,
external_baz: some_external_program::Baz,
other_module_baz: some_other_module::Baz,
}
#[zero_copy]
pub struct ZcStruct {
pub some_field: u16,
}
#[account(zero_copy)]
pub struct SomeZcAccount {
field: ZcStruct,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct SomeRetStruct {
pub some_field: u8,
}