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
//! Accounts structs for StableSwap.

use anchor_lang::prelude::*;

/// Accounts for an [crate::initialize] instruction.
#[derive(Accounts, Clone)]
pub struct Initialize<'info> {
    /// The swap.
    #[account(signer)]
    pub swap: AccountInfo<'info>,
    /// The authority of the swap.
    pub swap_authority: AccountInfo<'info>,
    /// The admin of the swap.
    pub admin: AccountInfo<'info>,
    /// The A token of the swap.
    pub token_a: InitToken<'info>,
    /// The B token of the swap.
    pub token_b: InitToken<'info>,
    /// The pool mint of the swap.
    pub pool_mint: AccountInfo<'info>,
    /// The output account for LP tokens.
    pub output_lp: AccountInfo<'info>,
    /// The spl_token program.
    pub token_program: AccountInfo<'info>,
}

/// Accounts for a [crate::deposit] instruction.
#[derive(Accounts, Clone)]
pub struct Deposit<'info> {
    /// The context of the user.
    pub user: SwapUserContext<'info>,
    /// The A token of the swap.
    pub input_a: SwapToken<'info>,
    /// The B token of the swap.
    pub input_b: SwapToken<'info>,
    /// The pool mint of the swap.
    pub pool_mint: AccountInfo<'info>,
    /// The output account for LP tokens.
    pub output_lp: AccountInfo<'info>,
}

/// Accounts for a [crate::swap] instruction.
#[derive(Accounts, Clone)]
pub struct Swap<'info> {
    /// The context of the user.
    pub user: SwapUserContext<'info>,
    /// Accounts for input tokens.
    pub input: SwapToken<'info>,
    /// Accounts for output tokens.
    pub output: SwapOutput<'info>,
}

/// Accounts for a [crate::withdraw_one] instruction.
#[derive(Accounts, Clone)]
pub struct WithdrawOne<'info> {
    /// The context of the user.
    pub user: SwapUserContext<'info>,
    /// The pool mint of the swap.
    pub pool_mint: AccountInfo<'info>,
    /// The input (user)'s LP token account
    pub input_lp: AccountInfo<'info>,
    /// The TokenAccount holding the swap's reserves of quote tokens; i.e., the token not being withdrawn.
    ///
    /// - If withdrawing token A, this is `swap_info.token_b.reserves`.
    /// - If withdrawing token B, this is `swap_info.token_a.reserves`.
    ///
    /// These reserves are needed for the withdraw_one instruction since the
    /// StableSwap `D` invariant requires both the base and quote reserves
    /// to determine how many tokens are paid out to users withdrawing from
    /// the swap.
    ///
    /// *For more info, see [stable_swap_client::state::SwapTokenInfo::reserves].*
    pub quote_reserves: AccountInfo<'info>,
    /// Accounts for output tokens.
    pub output: SwapOutput<'info>,
}

/// Accounts for a [crate::withdraw] instruction.
#[derive(Accounts, Clone)]
pub struct Withdraw<'info> {
    /// The context of the user.
    pub user: SwapUserContext<'info>,
    /// The input account for LP tokens.
    pub input_lp: AccountInfo<'info>,
    /// The pool mint of the swap.
    pub pool_mint: AccountInfo<'info>,
    /// The A token of the swap.
    pub output_a: SwapOutput<'info>,
    /// The B token of the swap.
    pub output_b: SwapOutput<'info>,
}

/// Accounts for a [crate::set_fee_account] instruction.
#[derive(Accounts, Clone)]
pub struct SetFeeAccount<'info> {
    /// The context of the admin user
    pub admin_ctx: AdminUserContext<'info>,
    /// The new token account for fees
    pub fee_account: AccountInfo<'info>,
}

/// Accounts for a [crate::apply_new_admin] instruction.
#[derive(Accounts, Clone)]
pub struct CommitNewAdmin<'info> {
    /// The context of the admin user.
    pub admin_ctx: AdminUserContext<'info>,
    /// The account of the new admin.
    pub new_admin: AccountInfo<'info>,
}

// --------------------------------
// Various accounts
// --------------------------------

/// Token accounts for initializing a [crate::SwapInfo].
#[derive(Accounts, Clone)]
pub struct InitToken<'info> {
    /// The token account for the pool's reserves of this token.
    pub reserve: AccountInfo<'info>,
    /// The token account for the fees associated with the token.
    pub fees: AccountInfo<'info>,
    /// The mint of the token.
    pub mint: AccountInfo<'info>,
}

/// Token accounts for a [crate::swap] instruction.
#[derive(Accounts, Clone)]
pub struct SwapToken<'info> {
    /// The token account associated with the user.
    pub user: AccountInfo<'info>,
    /// The token account for the pool's reserves of this token.
    pub reserve: AccountInfo<'info>,
}

/// Token accounts for the output of a StableSwap instruction.
#[derive(Accounts, Clone)]
pub struct SwapOutput<'info> {
    /// The token accounts of the user and the token.
    pub user_token: SwapToken<'info>,
    /// The token account for the fees associated with the token.
    pub fees: AccountInfo<'info>,
}

/// Accounts for an instruction that interacts with the swap.
#[derive(Accounts, Clone)]
pub struct SwapUserContext<'info> {
    /// The spl_token program.
    pub token_program: AccountInfo<'info>,
    /// The authority of the swap.
    pub swap_authority: AccountInfo<'info>,
    /// The authority of the user.
    #[account(signer)]
    pub user_authority: AccountInfo<'info>,
    /// The swap.
    pub swap: AccountInfo<'info>,
}

/// Accounts for an instruction that requires admin permission.
#[derive(Accounts, Clone)]
pub struct AdminUserContext<'info> {
    /// The public key of the admin account.
    ///
    /// *Note: must be a signer.*
    #[account(signer)]
    pub admin: AccountInfo<'info>,
    /// The swap.
    pub swap: AccountInfo<'info>,
}