-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathinstruction.rs
387 lines (367 loc) · 12.9 KB
/
instruction.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
use crate::error::VestingError;
use solana_program::{
instruction::{AccountMeta, Instruction},
msg,
program_error::ProgramError,
pubkey::Pubkey
};
use std::convert::TryInto;
use std::mem::size_of;
#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
#[cfg(feature = "fuzz")]
impl Arbitrary for VestingInstruction {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let seeds: [u8; 32] = u.arbitrary()?;
let choice = u.choose(&[0, 1, 2, 3])?;
match choice {
0 => {
let number_of_schedules = u.arbitrary()?;
return Ok(Self::Init {
seeds,
number_of_schedules,
});
}
1 => {
let schedules: [Schedule; 10] = u.arbitrary()?;
let key_bytes: [u8; 32] = u.arbitrary()?;
let mint_address: Pubkey = Pubkey::new(&key_bytes);
let key_bytes: [u8; 32] = u.arbitrary()?;
let destination_token_address: Pubkey = Pubkey::new(&key_bytes);
return Ok(Self::Create {
seeds,
mint_address,
destination_token_address,
schedules: schedules.to_vec(),
});
}
2 => return Ok(Self::Unlock { seeds }),
_ => return Ok(Self::ChangeDestination { seeds }),
}
}
}
#[cfg_attr(feature = "fuzz", derive(Arbitrary))]
#[repr(C)]
#[derive(Clone, Debug, PartialEq)]
pub struct Schedule {
// Schedule release time in unix timestamp
pub release_time: u64,
pub amount: u64,
}
pub const SCHEDULE_SIZE: usize = 16;
#[repr(C)]
#[derive(Clone, Debug, PartialEq)]
pub enum VestingInstruction {
/// Initializes an empty program account for the token_vesting program
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The system program account
/// 1. `[]` The sysvar Rent account
/// 1. `[signer]` The fee payer account
/// 1. `[]` The vesting account
Init {
// The seed used to derive the vesting accounts address
seeds: [u8; 32],
// The number of release schedules for this contract to hold
number_of_schedules: u32,
},
/// Creates a new vesting schedule contract
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The spl-token program account
/// 1. `[writable]` The vesting account
/// 2. `[writable]` The vesting spl-token account
/// 3. `[signer]` The source spl-token account owner
/// 4. `[writable]` The source spl-token account
Create {
seeds: [u8; 32],
mint_address: Pubkey,
destination_token_address: Pubkey,
schedules: Vec<Schedule>,
},
/// Unlocks a simple vesting contract (SVC) - can only be invoked by the program itself
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The spl-token program account
/// 1. `[]` The clock sysvar account
/// 1. `[writable]` The vesting account
/// 2. `[writable]` The vesting spl-token account
/// 3. `[writable]` The destination spl-token account
Unlock { seeds: [u8; 32] },
/// Change the destination account of a given simple vesting contract (SVC)
/// - can only be invoked by the present destination address of the contract.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[]` The vesting account
/// 1. `[]` The current destination token account
/// 2. `[signer]` The destination spl-token account owner
/// 3. `[]` The new destination spl-token account
ChangeDestination { seeds: [u8; 32] },
}
impl VestingInstruction {
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
use VestingError::InvalidInstruction;
let (&tag, rest) = input.split_first().ok_or(InvalidInstruction)?;
Ok(match tag {
0 => {
let seeds: [u8; 32] = rest
.get(..32)
.and_then(|slice| slice.try_into().ok())
.unwrap();
let number_of_schedules = rest
.get(32..36)
.and_then(|slice| slice.try_into().ok())
.map(u32::from_le_bytes)
.ok_or(InvalidInstruction)?;
Self::Init {
seeds,
number_of_schedules,
}
}
1 => {
let seeds: [u8; 32] = rest
.get(..32)
.and_then(|slice| slice.try_into().ok())
.unwrap();
let mint_address = rest
.get(32..64)
.and_then(|slice| slice.try_into().ok())
.map(Pubkey::new)
.ok_or(InvalidInstruction)?;
let destination_token_address = rest
.get(64..96)
.and_then(|slice| slice.try_into().ok())
.map(Pubkey::new)
.ok_or(InvalidInstruction)?;
let number_of_schedules = rest[96..].len() / SCHEDULE_SIZE;
let mut schedules: Vec<Schedule> = Vec::with_capacity(number_of_schedules);
let mut offset = 96;
for _ in 0..number_of_schedules {
let release_time = rest
.get(offset..offset + 8)
.and_then(|slice| slice.try_into().ok())
.map(u64::from_le_bytes)
.ok_or(InvalidInstruction)?;
let amount = rest
.get(offset + 8..offset + 16)
.and_then(|slice| slice.try_into().ok())
.map(u64::from_le_bytes)
.ok_or(InvalidInstruction)?;
offset += SCHEDULE_SIZE;
schedules.push(Schedule {
release_time,
amount,
})
}
Self::Create {
seeds,
mint_address,
destination_token_address,
schedules,
}
}
2 | 3 => {
let seeds: [u8; 32] = rest
.get(..32)
.and_then(|slice| slice.try_into().ok())
.unwrap();
match tag {
2 => Self::Unlock { seeds },
_ => Self::ChangeDestination { seeds },
}
}
_ => {
msg!("Unsupported tag");
return Err(InvalidInstruction.into());
}
})
}
pub fn pack(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(size_of::<Self>());
match self {
&Self::Init {
seeds,
number_of_schedules,
} => {
buf.push(0);
buf.extend_from_slice(&seeds);
buf.extend_from_slice(&number_of_schedules.to_le_bytes())
}
Self::Create {
seeds,
mint_address,
destination_token_address,
schedules,
} => {
buf.push(1);
buf.extend_from_slice(seeds);
buf.extend_from_slice(&mint_address.to_bytes());
buf.extend_from_slice(&destination_token_address.to_bytes());
for s in schedules.iter() {
buf.extend_from_slice(&s.release_time.to_le_bytes());
buf.extend_from_slice(&s.amount.to_le_bytes());
}
}
&Self::Unlock { seeds } => {
buf.push(2);
buf.extend_from_slice(&seeds);
}
&Self::ChangeDestination { seeds } => {
buf.push(3);
buf.extend_from_slice(&seeds);
}
};
buf
}
}
// Creates a `Init` instruction to create and initialize the vesting token account.
pub fn init(
system_program_id: &Pubkey,
rent_program_id: &Pubkey,
vesting_program_id: &Pubkey,
payer_key: &Pubkey,
vesting_account: &Pubkey,
seeds: [u8; 32],
number_of_schedules: u32,
) -> Result<Instruction, ProgramError> {
let data = VestingInstruction::Init {
seeds,
number_of_schedules,
}
.pack();
let accounts = vec![
AccountMeta::new_readonly(*system_program_id, false),
AccountMeta::new_readonly(*rent_program_id, false),
AccountMeta::new(*payer_key, true),
AccountMeta::new(*vesting_account, false),
];
Ok(Instruction {
program_id: *vesting_program_id,
accounts,
data,
})
}
// Creates a `CreateSchedule` instruction
pub fn create(
vesting_program_id: &Pubkey,
token_program_id: &Pubkey,
vesting_account_key: &Pubkey,
vesting_token_account_key: &Pubkey,
source_token_account_owner_key: &Pubkey,
source_token_account_key: &Pubkey,
destination_token_account_key: &Pubkey,
mint_address: &Pubkey,
schedules: Vec<Schedule>,
seeds: [u8; 32],
) -> Result<Instruction, ProgramError> {
let data = VestingInstruction::Create {
mint_address: *mint_address,
seeds,
destination_token_address: *destination_token_account_key,
schedules,
}
.pack();
let accounts = vec![
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new(*vesting_account_key, false),
AccountMeta::new(*vesting_token_account_key, false),
AccountMeta::new_readonly(*source_token_account_owner_key, true),
AccountMeta::new(*source_token_account_key, false),
];
Ok(Instruction {
program_id: *vesting_program_id,
accounts,
data,
})
}
// Creates an `Unlock` instruction
pub fn unlock(
vesting_program_id: &Pubkey,
token_program_id: &Pubkey,
clock_sysvar_id: &Pubkey,
vesting_account_key: &Pubkey,
vesting_token_account_key: &Pubkey,
destination_token_account_key: &Pubkey,
seeds: [u8; 32],
) -> Result<Instruction, ProgramError> {
let data = VestingInstruction::Unlock { seeds }.pack();
let accounts = vec![
AccountMeta::new_readonly(*token_program_id, false),
AccountMeta::new_readonly(*clock_sysvar_id, false),
AccountMeta::new(*vesting_account_key, false),
AccountMeta::new(*vesting_token_account_key, false),
AccountMeta::new(*destination_token_account_key, false),
];
Ok(Instruction {
program_id: *vesting_program_id,
accounts,
data,
})
}
pub fn change_destination(
vesting_program_id: &Pubkey,
vesting_account_key: &Pubkey,
current_destination_token_account_owner: &Pubkey,
current_destination_token_account: &Pubkey,
target_destination_token_account: &Pubkey,
seeds: [u8; 32],
) -> Result<Instruction, ProgramError> {
let data = VestingInstruction::ChangeDestination { seeds }.pack();
let accounts = vec![
AccountMeta::new(*vesting_account_key, false),
AccountMeta::new_readonly(*current_destination_token_account, false),
AccountMeta::new_readonly(*current_destination_token_account_owner, true),
AccountMeta::new_readonly(*target_destination_token_account, false),
];
Ok(Instruction {
program_id: *vesting_program_id,
accounts,
data,
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_instruction_packing() {
let mint_address = Pubkey::new_unique();
let destination_token_address = Pubkey::new_unique();
let original_create = VestingInstruction::Create {
seeds: [50u8; 32],
schedules: vec![Schedule {
amount: 42,
release_time: 250,
}],
mint_address: mint_address.clone(),
destination_token_address,
};
let packed_create = original_create.pack();
let unpacked_create = VestingInstruction::unpack(&packed_create).unwrap();
assert_eq!(original_create, unpacked_create);
let original_unlock = VestingInstruction::Unlock { seeds: [50u8; 32] };
assert_eq!(
original_unlock,
VestingInstruction::unpack(&original_unlock.pack()).unwrap()
);
let original_init = VestingInstruction::Init {
number_of_schedules: 42,
seeds: [50u8; 32],
};
assert_eq!(
original_init,
VestingInstruction::unpack(&original_init.pack()).unwrap()
);
let original_change = VestingInstruction::ChangeDestination { seeds: [50u8; 32] };
assert_eq!(
original_change,
VestingInstruction::unpack(&original_change.pack()).unwrap()
);
}
}