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
388
389
#![deny(rustdoc::all)]
#![allow(rustdoc::missing_doc_code_examples)]
#![deny(clippy::unwrap_used)]
mod macros;
use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, Token, TokenAccount};
use stable_swap_anchor::{StableSwap, SwapInfo};
use vipers::prelude::*;
mod account_validators;
mod cpi_helpers;
mod import_pool;
mod state;
pub use state::*;
declare_id!("SMANK4F5osjfVpKFH5LPzE6HPpbzSPu5iHPBhuor5xU");
#[program]
pub mod pools {
use anchor_spl::token;
use super::*;
pub fn new_pool_manager(ctx: Context<NewPoolManager>, _bump: u8) -> Result<()> {
let pool_manager = &mut ctx.accounts.pool_manager;
pool_manager.base = ctx.accounts.base.key();
pool_manager.bump = *unwrap_int!(ctx.bumps.get("pool_manager"));
pool_manager.num_pools = 0;
pool_manager.admin = ctx.accounts.admin.key();
pool_manager.pending_admin = Pubkey::default();
pool_manager.initial_fees = SwapFees {
trade_fee_numerator: 4,
withdraw_fee_numerator: 0,
admin_trade_fee_numerator: 0,
admin_withdraw_fee_numerator: 0,
trade_fee_denominator: 10_000,
withdraw_fee_denominator: 10_000,
admin_trade_fee_denominator: 10_000,
admin_withdraw_fee_denominator: 10_000,
};
pool_manager.min_permissionless_amp_factor = 10;
pool_manager.max_permissionless_amp_factor = 200;
pool_manager.operator = ctx.accounts.operator.key();
pool_manager.beneficiary = ctx.accounts.beneficiary.key();
Ok(())
}
#[access_control(ctx.accounts.validate())]
pub fn import_pool_permissionless(
ctx: Context<ImportPoolPermissionless>,
_bump: u8,
) -> Result<()> {
ctx.accounts.validate_initial_parameters()?;
let bump = *unwrap_int!(ctx.bumps.get("pool"));
import_pool::import_pool_unchecked(ctx.accounts, bump, true)
}
#[access_control(ctx.accounts.validate())]
pub fn import_pool_as_operator(ctx: Context<ImportPoolAsOperator>, _bump: u8) -> Result<()> {
let bump = *unwrap_int!(ctx.bumps.get("pool"));
import_pool::import_pool_unchecked(&mut ctx.accounts.import_pool, bump, false)
}
#[access_control(ctx.accounts.validate())]
pub fn ramp_a(ctx: Context<SwapContext>, target_amp: u64, stop_ramp_ts: i64) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::ramp_a(cpi_ctx, target_amp, stop_ramp_ts)
}
#[access_control(ctx.accounts.validate())]
pub fn stop_ramp_a(ctx: Context<SwapContext>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::stop_ramp_a(cpi_ctx)
}
#[access_control(ctx.accounts.validate())]
pub fn pause_swap(ctx: Context<SwapContext>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::pause(cpi_ctx)
}
#[access_control(ctx.accounts.validate())]
pub fn unpause_swap(ctx: Context<SwapContext>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::unpause(cpi_ctx)
}
#[access_control(ctx.accounts.validate())]
pub fn commit_new_admin(ctx: Context<CommitNewAdmin>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let admin_user_context = cpi_helpers::create_pool_admin_user_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
);
let cpi_ctx = CpiContext::new_with_signer(
ctx.accounts.swap_program.to_account_info(),
stable_swap_anchor::CommitNewAdmin {
admin_ctx: admin_user_context,
new_admin: ctx.accounts.new_admin.to_account_info(),
},
seeds,
);
stable_swap_anchor::commit_new_admin(cpi_ctx)
}
#[access_control(ctx.accounts.validate())]
pub fn apply_new_admin(ctx: Context<SwapContext>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::apply_new_admin(cpi_ctx)
}
#[access_control(ctx.accounts.validate())]
pub fn set_new_fees(ctx: Context<SwapContext>, new_fees: SwapFees) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
let cpi_ctx = cpi_helpers::pool_admin_cpi_context(
&ctx.accounts.pool,
ctx.accounts.swap.to_account_info(),
ctx.accounts.swap_program.to_account_info(),
)
.with_signer(seeds);
stable_swap_anchor::set_new_fees(cpi_ctx, new_fees.into())
}
#[access_control(ctx.accounts.validate())]
pub fn send_fees_to_beneficiary(ctx: Context<SendFeesToBeneficiary>) -> Result<()> {
let seeds: &[&[&[u8]]] = gen_pool_signer_seeds!(ctx.accounts.pool);
token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.fee_account.to_account_info(),
to: ctx.accounts.beneficiary_account.to_account_info(),
authority: ctx.accounts.pool.to_account_info(),
},
)
.with_signer(seeds),
ctx.accounts.fee_account.amount,
)
}
#[access_control(ctx.accounts.validate())]
pub fn set_operator(ctx: Context<SetOperator>) -> Result<()> {
let pool_manager = &mut ctx.accounts.pool_manager;
pool_manager.operator = ctx.accounts.operator.key();
Ok(())
}
#[access_control(ctx.accounts.validate())]
pub fn set_beneficiary(ctx: Context<SetBeneficiary>) -> Result<()> {
let pool_manager = &mut ctx.accounts.pool_manager;
pool_manager.beneficiary = ctx.accounts.beneficiary.key();
Ok(())
}
}
#[derive(Accounts)]
pub struct NewPoolManager<'info> {
#[account(
init,
seeds = [
b"SaberPoolManager".as_ref(),
base.key().to_bytes().as_ref()
],
bump,
space = 8 + PoolManager::LEN,
payer = payer
)]
pub pool_manager: Account<'info, PoolManager>,
pub base: Signer<'info>,
pub admin: UncheckedAccount<'info>,
pub operator: UncheckedAccount<'info>,
pub beneficiary: UncheckedAccount<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct ImportPoolPermissionless<'info> {
#[account(mut)]
pub pool_manager: Box<Account<'info, PoolManager>>,
pub swap: Box<Account<'info, SwapInfo>>,
#[account(
init,
seeds = [
b"SaberPool".as_ref(),
pool_manager.key().to_bytes().as_ref(),
swap.sorted_mints().0.to_bytes().as_ref(),
swap.sorted_mints().1.to_bytes().as_ref()
],
bump,
space = 8 + Pool::LEN,
payer = payer
)]
pub pool: Box<Account<'info, Pool>>,
pub token_a_fees: Box<Account<'info, TokenAccount>>,
pub token_b_fees: Box<Account<'info, TokenAccount>>,
pub lp_mint: Box<Account<'info, Mint>>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct ImportPoolAsOperator<'info> {
pub admin_or_operator: Signer<'info>,
pub import_pool: ImportPoolPermissionless<'info>,
}
#[derive(Accounts)]
pub struct SwapContext<'info> {
pub pool_manager: Account<'info, PoolManager>,
#[account(mut)]
pub swap: Account<'info, SwapInfo>,
pub pool: Account<'info, Pool>,
pub swap_program: Program<'info, StableSwap>,
pub admin: Signer<'info>,
}
#[derive(Accounts)]
pub struct CommitNewAdmin<'info> {
pub pool_manager: Account<'info, PoolManager>,
#[account(mut)]
pub swap: Account<'info, SwapInfo>,
pub pool: Account<'info, Pool>,
pub admin: Signer<'info>,
pub new_admin: UncheckedAccount<'info>,
pub swap_program: Program<'info, StableSwap>,
}
#[derive(Accounts)]
pub struct SendFeesToBeneficiary<'info> {
pub pool_manager: Account<'info, PoolManager>,
pub pool: Account<'info, Pool>,
#[account(mut)]
pub fee_account: Account<'info, TokenAccount>,
#[account(mut)]
pub beneficiary_account: Account<'info, TokenAccount>,
pub token_program: Program<'info, Token>,
}
#[derive(Accounts)]
pub struct SetOperator<'info> {
#[account(mut)]
pub pool_manager: Account<'info, PoolManager>,
pub admin: Signer<'info>,
pub operator: UncheckedAccount<'info>,
}
#[derive(Accounts)]
pub struct SetBeneficiary<'info> {
#[account(mut)]
pub pool_manager: Account<'info, PoolManager>,
pub admin: Signer<'info>,
pub beneficiary: UncheckedAccount<'info>,
}
#[error_code]
pub enum ErrorCode {
#[msg("Must be admin to perform this action.")]
NotAdmin,
#[msg("Must be admin or operator to perform this action.")]
NotAdminOrOperator,
#[msg("Initial amp factor out of range.")]
InitialAmpOutOfRange,
#[msg("Swap fees do not match the configured initial parameters.")]
InitialFeesMismatch,
#[msg("Swap's token mints must be sorted.")]
SwapTokensNotSorted,
#[msg("Swap's token mints cannot be the same.")]
SwapTokensCannotBeEqual,
#[msg("Specified fee account invalid.")]
InvalidFeeAccount,
}