-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
instruction.rs
306 lines (288 loc) · 10.7 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
//! Instruction types
use {
solana_program::{
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
system_program,
},
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
spl_pod::{bytemuck::pod_slice_to_bytes, slice::PodSlice},
spl_tlv_account_resolution::account::ExtraAccountMeta,
std::convert::TryInto,
};
/// Instructions supported by the transfer hook interface.
#[repr(C)]
#[derive(Clone, Debug, PartialEq)]
pub enum TransferHookInstruction {
/// Runs additional transfer logic.
///
/// Accounts expected by this instruction:
///
/// 0. `[]` Source account
/// 1. `[]` Token mint
/// 2. `[]` Destination account
/// 3. `[]` Source account's owner/delegate
/// 4. `[]` Validation account
/// 5..5+M `[]` `M` additional accounts, written in validation account
/// data
Execute {
/// Amount of tokens to transfer
amount: u64,
},
/// Initializes the extra account metas on an account, writing into the
/// first open TLV space.
///
/// Accounts expected by this instruction:
///
/// 0. `[w]` Account with extra account metas
/// 1. `[]` Mint
/// 2. `[s]` Mint authority
/// 3. `[]` System program
InitializeExtraAccountMetaList {
/// List of `ExtraAccountMeta`s to write into the account
extra_account_metas: Vec<ExtraAccountMeta>,
},
/// Updates the extra account metas on an account by overwriting the
/// existing list.
///
/// Accounts expected by this instruction:
///
/// 0. `[w]` Account with extra account metas
/// 1. `[]` Mint
/// 2. `[s]` Mint authority
UpdateExtraAccountMetaList {
/// The new list of `ExtraAccountMetas` to overwrite the existing entry
/// in the account.
extra_account_metas: Vec<ExtraAccountMeta>,
},
}
/// TLV instruction type only used to define the discriminator. The actual data
/// is entirely managed by `ExtraAccountMetaList`, and it is the only data
/// contained by this type.
#[derive(SplDiscriminate)]
#[discriminator_hash_input("spl-transfer-hook-interface:execute")]
pub struct ExecuteInstruction;
/// TLV instruction type used to initialize extra account metas
/// for the transfer hook
#[derive(SplDiscriminate)]
#[discriminator_hash_input("spl-transfer-hook-interface:initialize-extra-account-metas")]
pub struct InitializeExtraAccountMetaListInstruction;
/// TLV instruction type used to update extra account metas
/// for the transfer hook
#[derive(SplDiscriminate)]
#[discriminator_hash_input("spl-transfer-hook-interface:update-extra-account-metas")]
pub struct UpdateExtraAccountMetaListInstruction;
impl TransferHookInstruction {
/// Unpacks a byte buffer into a
/// [TransferHookInstruction](enum.TransferHookInstruction.html).
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
if input.len() < ArrayDiscriminator::LENGTH {
return Err(ProgramError::InvalidInstructionData);
}
let (discriminator, rest) = input.split_at(ArrayDiscriminator::LENGTH);
Ok(match discriminator {
ExecuteInstruction::SPL_DISCRIMINATOR_SLICE => {
let amount = rest
.get(..8)
.and_then(|slice| slice.try_into().ok())
.map(u64::from_le_bytes)
.ok_or(ProgramError::InvalidInstructionData)?;
Self::Execute { amount }
}
InitializeExtraAccountMetaListInstruction::SPL_DISCRIMINATOR_SLICE => {
let pod_slice = PodSlice::<ExtraAccountMeta>::unpack(rest)?;
let extra_account_metas = pod_slice.data().to_vec();
Self::InitializeExtraAccountMetaList {
extra_account_metas,
}
}
UpdateExtraAccountMetaListInstruction::SPL_DISCRIMINATOR_SLICE => {
let pod_slice = PodSlice::<ExtraAccountMeta>::unpack(rest)?;
let extra_account_metas = pod_slice.data().to_vec();
Self::UpdateExtraAccountMetaList {
extra_account_metas,
}
}
_ => return Err(ProgramError::InvalidInstructionData),
})
}
/// Packs a [TokenInstruction](enum.TokenInstruction.html) into a byte
/// buffer.
pub fn pack(&self) -> Vec<u8> {
let mut buf = vec![];
match self {
Self::Execute { amount } => {
buf.extend_from_slice(ExecuteInstruction::SPL_DISCRIMINATOR_SLICE);
buf.extend_from_slice(&amount.to_le_bytes());
}
Self::InitializeExtraAccountMetaList {
extra_account_metas,
} => {
buf.extend_from_slice(
InitializeExtraAccountMetaListInstruction::SPL_DISCRIMINATOR_SLICE,
);
buf.extend_from_slice(&(extra_account_metas.len() as u32).to_le_bytes());
buf.extend_from_slice(pod_slice_to_bytes(extra_account_metas));
}
Self::UpdateExtraAccountMetaList {
extra_account_metas,
} => {
buf.extend_from_slice(
UpdateExtraAccountMetaListInstruction::SPL_DISCRIMINATOR_SLICE,
);
buf.extend_from_slice(&(extra_account_metas.len() as u32).to_le_bytes());
buf.extend_from_slice(pod_slice_to_bytes(extra_account_metas));
}
};
buf
}
}
/// Creates an `Execute` instruction, provided all of the additional required
/// account metas
#[allow(clippy::too_many_arguments)]
pub fn execute_with_extra_account_metas(
program_id: &Pubkey,
source_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
authority_pubkey: &Pubkey,
validate_state_pubkey: &Pubkey,
additional_accounts: &[AccountMeta],
amount: u64,
) -> Instruction {
let mut instruction = execute(
program_id,
source_pubkey,
mint_pubkey,
destination_pubkey,
authority_pubkey,
validate_state_pubkey,
amount,
);
instruction.accounts.extend_from_slice(additional_accounts);
instruction
}
/// Creates an `Execute` instruction, without the additional accounts
#[allow(clippy::too_many_arguments)]
pub fn execute(
program_id: &Pubkey,
source_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
destination_pubkey: &Pubkey,
authority_pubkey: &Pubkey,
validate_state_pubkey: &Pubkey,
amount: u64,
) -> Instruction {
let data = TransferHookInstruction::Execute { amount }.pack();
let accounts = vec![
AccountMeta::new_readonly(*source_pubkey, false),
AccountMeta::new_readonly(*mint_pubkey, false),
AccountMeta::new_readonly(*destination_pubkey, false),
AccountMeta::new_readonly(*authority_pubkey, false),
AccountMeta::new_readonly(*validate_state_pubkey, false),
];
Instruction {
program_id: *program_id,
accounts,
data,
}
}
/// Creates a `InitializeExtraAccountMetaList` instruction.
pub fn initialize_extra_account_meta_list(
program_id: &Pubkey,
extra_account_metas_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
authority_pubkey: &Pubkey,
extra_account_metas: &[ExtraAccountMeta],
) -> Instruction {
let data = TransferHookInstruction::InitializeExtraAccountMetaList {
extra_account_metas: extra_account_metas.to_vec(),
}
.pack();
let accounts = vec![
AccountMeta::new(*extra_account_metas_pubkey, false),
AccountMeta::new_readonly(*mint_pubkey, false),
AccountMeta::new_readonly(*authority_pubkey, true),
AccountMeta::new_readonly(system_program::id(), false),
];
Instruction {
program_id: *program_id,
accounts,
data,
}
}
/// Creates a `UpdateExtraAccountMetaList` instruction.
pub fn update_extra_account_meta_list(
program_id: &Pubkey,
extra_account_metas_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
authority_pubkey: &Pubkey,
extra_account_metas: &[ExtraAccountMeta],
) -> Instruction {
let data = TransferHookInstruction::UpdateExtraAccountMetaList {
extra_account_metas: extra_account_metas.to_vec(),
}
.pack();
let accounts = vec![
AccountMeta::new(*extra_account_metas_pubkey, false),
AccountMeta::new_readonly(*mint_pubkey, false),
AccountMeta::new_readonly(*authority_pubkey, true),
];
Instruction {
program_id: *program_id,
accounts,
data,
}
}
#[cfg(test)]
mod test {
use {super::*, crate::NAMESPACE, solana_program::hash, spl_pod::bytemuck::pod_from_bytes};
#[test]
fn validate_packing() {
let amount = 111_111_111;
let check = TransferHookInstruction::Execute { amount };
let packed = check.pack();
// Please use ExecuteInstruction::SPL_DISCRIMINATOR in your program, the
// following is just for test purposes
let preimage = hash::hashv(&[format!("{NAMESPACE}:execute").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
let mut expect = vec![];
expect.extend_from_slice(discriminator.as_ref());
expect.extend_from_slice(&amount.to_le_bytes());
assert_eq!(packed, expect);
let unpacked = TransferHookInstruction::unpack(&expect).unwrap();
assert_eq!(unpacked, check);
}
#[test]
fn initialize_validation_pubkeys_packing() {
let extra_meta_len_bytes = &[
1, 0, 0, 0, // `1u32`
];
let extra_meta_bytes = &[
0, // `AccountMeta`
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, // pubkey
0, // is_signer
0, // is_writable
];
let extra_account_metas =
vec![*pod_from_bytes::<ExtraAccountMeta>(extra_meta_bytes).unwrap()];
let check = TransferHookInstruction::InitializeExtraAccountMetaList {
extra_account_metas,
};
let packed = check.pack();
// Please use INITIALIZE_EXTRA_ACCOUNT_METAS_DISCRIMINATOR in your program,
// the following is just for test purposes
let preimage =
hash::hashv(&[format!("{NAMESPACE}:initialize-extra-account-metas").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
let mut expect = vec![];
expect.extend_from_slice(discriminator.as_ref());
expect.extend_from_slice(extra_meta_len_bytes);
expect.extend_from_slice(extra_meta_bytes);
assert_eq!(packed, expect);
let unpacked = TransferHookInstruction::unpack(&expect).unwrap();
assert_eq!(unpacked, check);
}
}