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
use crate::fees::Fees;
use arrayref::{array_mut_ref, array_ref, array_refs, mut_array_refs};
use solana_program::{
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
pubkey::Pubkey,
};
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SwapInfo {
pub is_initialized: bool,
pub is_paused: bool,
pub nonce: u8,
pub initial_amp_factor: u64,
pub target_amp_factor: u64,
pub start_ramp_ts: i64,
pub stop_ramp_ts: i64,
pub future_admin_deadline: i64,
pub future_admin_key: Pubkey,
pub admin_key: Pubkey,
pub token_a: SwapTokenInfo,
pub token_b: SwapTokenInfo,
pub pool_mint: Pubkey,
pub fees: Fees,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SwapTokenInfo {
pub reserves: Pubkey,
pub mint: Pubkey,
pub admin_fees: Pubkey,
pub index: u8,
}
impl Sealed for SwapInfo {}
impl IsInitialized for SwapInfo {
fn is_initialized(&self) -> bool {
self.is_initialized
}
}
impl Pack for SwapInfo {
const LEN: usize = 395;
fn unpack_from_slice(input: &[u8]) -> Result<Self, ProgramError> {
let input = array_ref![input, 0, 395];
#[allow(clippy::ptr_offset_with_cast)]
let (
is_initialized,
is_paused,
nonce,
initial_amp_factor,
target_amp_factor,
start_ramp_ts,
stop_ramp_ts,
future_admin_deadline,
future_admin_key,
admin_key,
token_a,
token_b,
pool_mint,
token_a_mint,
token_b_mint,
admin_fee_key_a,
admin_fee_key_b,
fees,
) = array_refs![input, 1, 1, 1, 8, 8, 8, 8, 8, 32, 32, 32, 32, 32, 32, 32, 32, 32, 64];
Ok(Self {
is_initialized: match is_initialized {
[0] => false,
[1] => true,
_ => return Err(ProgramError::InvalidAccountData),
},
is_paused: match is_paused {
[0] => false,
[1] => true,
_ => return Err(ProgramError::InvalidAccountData),
},
nonce: nonce[0],
initial_amp_factor: u64::from_le_bytes(*initial_amp_factor),
target_amp_factor: u64::from_le_bytes(*target_amp_factor),
start_ramp_ts: i64::from_le_bytes(*start_ramp_ts),
stop_ramp_ts: i64::from_le_bytes(*stop_ramp_ts),
future_admin_deadline: i64::from_le_bytes(*future_admin_deadline),
future_admin_key: Pubkey::new_from_array(*future_admin_key),
admin_key: Pubkey::new_from_array(*admin_key),
token_a: SwapTokenInfo {
reserves: Pubkey::new_from_array(*token_a),
mint: Pubkey::new_from_array(*token_a_mint),
admin_fees: Pubkey::new_from_array(*admin_fee_key_a),
index: 0,
},
token_b: SwapTokenInfo {
reserves: Pubkey::new_from_array(*token_b),
mint: Pubkey::new_from_array(*token_b_mint),
admin_fees: Pubkey::new_from_array(*admin_fee_key_b),
index: 1,
},
pool_mint: Pubkey::new_from_array(*pool_mint),
fees: Fees::unpack_from_slice(fees)?,
})
}
fn pack_into_slice(&self, output: &mut [u8]) {
let output = array_mut_ref![output, 0, 395];
let (
is_initialized,
is_paused,
nonce,
initial_amp_factor,
target_amp_factor,
start_ramp_ts,
stop_ramp_ts,
future_admin_deadline,
future_admin_key,
admin_key,
token_a,
token_b,
pool_mint,
token_a_mint,
token_b_mint,
admin_fee_key_a,
admin_fee_key_b,
fees,
) = mut_array_refs![output, 1, 1, 1, 8, 8, 8, 8, 8, 32, 32, 32, 32, 32, 32, 32, 32, 32, 64];
is_initialized[0] = self.is_initialized as u8;
is_paused[0] = self.is_paused as u8;
nonce[0] = self.nonce;
*initial_amp_factor = self.initial_amp_factor.to_le_bytes();
*target_amp_factor = self.target_amp_factor.to_le_bytes();
*start_ramp_ts = self.start_ramp_ts.to_le_bytes();
*stop_ramp_ts = self.stop_ramp_ts.to_le_bytes();
*future_admin_deadline = self.future_admin_deadline.to_le_bytes();
future_admin_key.copy_from_slice(self.future_admin_key.as_ref());
admin_key.copy_from_slice(self.admin_key.as_ref());
token_a.copy_from_slice(self.token_a.reserves.as_ref());
token_b.copy_from_slice(self.token_b.reserves.as_ref());
pool_mint.copy_from_slice(self.pool_mint.as_ref());
token_a_mint.copy_from_slice(self.token_a.mint.as_ref());
token_b_mint.copy_from_slice(self.token_b.mint.as_ref());
admin_fee_key_a.copy_from_slice(self.token_a.admin_fees.as_ref());
admin_fee_key_b.copy_from_slice(self.token_b.admin_fees.as_ref());
self.fees.pack_into_slice(&mut fees[..]);
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn test_swap_info_packing() {
let nonce = 255;
let initial_amp_factor: u64 = 1;
let target_amp_factor: u64 = 1;
let start_ramp_ts: i64 = i64::MAX;
let stop_ramp_ts: i64 = i64::MAX;
let future_admin_deadline: i64 = i64::MAX;
let future_admin_key_raw = [1u8; 32];
let admin_key_raw = [2u8; 32];
let token_a_raw = [3u8; 32];
let token_b_raw = [4u8; 32];
let pool_mint_raw = [5u8; 32];
let token_a_mint_raw = [6u8; 32];
let token_b_mint_raw = [7u8; 32];
let admin_fee_key_a_raw = [8u8; 32];
let admin_fee_key_b_raw = [9u8; 32];
let admin_key = Pubkey::new_from_array(admin_key_raw);
let future_admin_key = Pubkey::new_from_array(future_admin_key_raw);
let token_a = Pubkey::new_from_array(token_a_raw);
let token_b = Pubkey::new_from_array(token_b_raw);
let pool_mint = Pubkey::new_from_array(pool_mint_raw);
let token_a_mint = Pubkey::new_from_array(token_a_mint_raw);
let token_b_mint = Pubkey::new_from_array(token_b_mint_raw);
let admin_fee_key_a = Pubkey::new_from_array(admin_fee_key_a_raw);
let admin_fee_key_b = Pubkey::new_from_array(admin_fee_key_b_raw);
let admin_trade_fee_numerator = 1;
let admin_trade_fee_denominator = 2;
let admin_withdraw_fee_numerator = 3;
let admin_withdraw_fee_denominator = 4;
let trade_fee_numerator = 5;
let trade_fee_denominator = 6;
let withdraw_fee_numerator = 7;
let withdraw_fee_denominator = 8;
let fees = Fees {
admin_trade_fee_numerator,
admin_trade_fee_denominator,
admin_withdraw_fee_numerator,
admin_withdraw_fee_denominator,
trade_fee_numerator,
trade_fee_denominator,
withdraw_fee_numerator,
withdraw_fee_denominator,
};
let is_initialized = true;
let is_paused = false;
let swap_info = SwapInfo {
is_initialized,
is_paused,
nonce,
initial_amp_factor,
target_amp_factor,
start_ramp_ts,
stop_ramp_ts,
future_admin_deadline,
future_admin_key,
admin_key,
token_a: SwapTokenInfo {
reserves: token_a,
mint: token_a_mint,
admin_fees: admin_fee_key_a,
index: 0,
},
token_b: SwapTokenInfo {
reserves: token_b,
mint: token_b_mint,
admin_fees: admin_fee_key_b,
index: 1,
},
pool_mint,
fees,
};
let mut packed = [0u8; SwapInfo::LEN];
SwapInfo::pack(swap_info, &mut packed).unwrap();
let unpacked = SwapInfo::unpack(&packed).unwrap();
assert_eq!(swap_info, unpacked);
let mut packed = vec![
1_u8,
0_u8,
nonce,
];
packed.extend_from_slice(&initial_amp_factor.to_le_bytes());
packed.extend_from_slice(&target_amp_factor.to_le_bytes());
packed.extend_from_slice(&start_ramp_ts.to_le_bytes());
packed.extend_from_slice(&stop_ramp_ts.to_le_bytes());
packed.extend_from_slice(&future_admin_deadline.to_le_bytes());
packed.extend_from_slice(&future_admin_key_raw);
packed.extend_from_slice(&admin_key_raw);
packed.extend_from_slice(&token_a_raw);
packed.extend_from_slice(&token_b_raw);
packed.extend_from_slice(&pool_mint_raw);
packed.extend_from_slice(&token_a_mint_raw);
packed.extend_from_slice(&token_b_mint_raw);
packed.extend_from_slice(&admin_fee_key_a_raw);
packed.extend_from_slice(&admin_fee_key_b_raw);
packed.extend_from_slice(&admin_trade_fee_numerator.to_le_bytes());
packed.extend_from_slice(&admin_trade_fee_denominator.to_le_bytes());
packed.extend_from_slice(&admin_withdraw_fee_numerator.to_le_bytes());
packed.extend_from_slice(&admin_withdraw_fee_denominator.to_le_bytes());
packed.extend_from_slice(&trade_fee_numerator.to_le_bytes());
packed.extend_from_slice(&trade_fee_denominator.to_le_bytes());
packed.extend_from_slice(&withdraw_fee_numerator.to_le_bytes());
packed.extend_from_slice(&withdraw_fee_denominator.to_le_bytes());
let unpacked = SwapInfo::unpack(&packed).unwrap();
assert_eq!(swap_info, unpacked);
}
}