stable_swap_anchor/
accounts.rs

1//! Accounts structs for StableSwap.
2
3use anchor_lang::prelude::*;
4
5/// Accounts for an [crate::initialize] instruction.
6#[derive(Accounts, Clone)]
7pub struct Initialize<'info> {
8    /// The swap.
9    #[account(signer)]
10    pub swap: AccountInfo<'info>,
11    /// The authority of the swap.
12    pub swap_authority: AccountInfo<'info>,
13    /// The admin of the swap.
14    pub admin: AccountInfo<'info>,
15    /// The A token of the swap.
16    pub token_a: InitToken<'info>,
17    /// The B token of the swap.
18    pub token_b: InitToken<'info>,
19    /// The pool mint of the swap.
20    pub pool_mint: AccountInfo<'info>,
21    /// The output account for LP tokens.
22    pub output_lp: AccountInfo<'info>,
23    /// The spl_token program.
24    pub token_program: AccountInfo<'info>,
25}
26
27/// Accounts for a [crate::deposit] instruction.
28#[derive(Accounts, Clone)]
29pub struct Deposit<'info> {
30    /// The context of the user.
31    pub user: SwapUserContext<'info>,
32    /// The A token of the swap.
33    pub input_a: SwapToken<'info>,
34    /// The B token of the swap.
35    pub input_b: SwapToken<'info>,
36    /// The pool mint of the swap.
37    pub pool_mint: AccountInfo<'info>,
38    /// The output account for LP tokens.
39    pub output_lp: AccountInfo<'info>,
40}
41
42/// Accounts for a [crate::swap] instruction.
43#[derive(Accounts, Clone)]
44pub struct Swap<'info> {
45    /// The context of the user.
46    pub user: SwapUserContext<'info>,
47    /// Accounts for input tokens.
48    pub input: SwapToken<'info>,
49    /// Accounts for output tokens.
50    pub output: SwapOutput<'info>,
51}
52
53/// Accounts for a [crate::withdraw_one] instruction.
54#[derive(Accounts, Clone)]
55pub struct WithdrawOne<'info> {
56    /// The context of the user.
57    pub user: SwapUserContext<'info>,
58    /// The pool mint of the swap.
59    pub pool_mint: AccountInfo<'info>,
60    /// The input (user)'s LP token account
61    pub input_lp: AccountInfo<'info>,
62    /// The TokenAccount holding the swap's reserves of quote tokens; i.e., the token not being withdrawn.
63    ///
64    /// - If withdrawing token A, this is `swap_info.token_b.reserves`.
65    /// - If withdrawing token B, this is `swap_info.token_a.reserves`.
66    ///
67    /// These reserves are needed for the withdraw_one instruction since the
68    /// StableSwap `D` invariant requires both the base and quote reserves
69    /// to determine how many tokens are paid out to users withdrawing from
70    /// the swap.
71    ///
72    /// *For more info, see [stable_swap_client::state::SwapTokenInfo::reserves].*
73    pub quote_reserves: AccountInfo<'info>,
74    /// Accounts for output tokens.
75    pub output: SwapOutput<'info>,
76}
77
78/// Accounts for a [crate::withdraw] instruction.
79#[derive(Accounts, Clone)]
80pub struct Withdraw<'info> {
81    /// The context of the user.
82    pub user: SwapUserContext<'info>,
83    /// The input account for LP tokens.
84    pub input_lp: AccountInfo<'info>,
85    /// The pool mint of the swap.
86    pub pool_mint: AccountInfo<'info>,
87    /// The A token of the swap.
88    pub output_a: SwapOutput<'info>,
89    /// The B token of the swap.
90    pub output_b: SwapOutput<'info>,
91}
92
93/// Accounts for a [crate::set_fee_account] instruction.
94#[derive(Accounts, Clone)]
95pub struct SetFeeAccount<'info> {
96    /// The context of the admin user
97    pub admin_ctx: AdminUserContext<'info>,
98    /// The new token account for fees
99    pub fee_account: AccountInfo<'info>,
100}
101
102/// Accounts for a [crate::apply_new_admin] instruction.
103#[derive(Accounts, Clone)]
104pub struct CommitNewAdmin<'info> {
105    /// The context of the admin user.
106    pub admin_ctx: AdminUserContext<'info>,
107    /// The account of the new admin.
108    pub new_admin: AccountInfo<'info>,
109}
110
111// --------------------------------
112// Various accounts
113// --------------------------------
114
115/// Token accounts for initializing a [crate::SwapInfo].
116#[derive(Accounts, Clone)]
117pub struct InitToken<'info> {
118    /// The token account for the pool's reserves of this token.
119    pub reserve: AccountInfo<'info>,
120    /// The token account for the fees associated with the token.
121    pub fees: AccountInfo<'info>,
122    /// The mint of the token.
123    pub mint: AccountInfo<'info>,
124}
125
126/// Token accounts for a [crate::swap] instruction.
127#[derive(Accounts, Clone)]
128pub struct SwapToken<'info> {
129    /// The token account associated with the user.
130    pub user: AccountInfo<'info>,
131    /// The token account for the pool's reserves of this token.
132    pub reserve: AccountInfo<'info>,
133}
134
135/// Token accounts for the output of a StableSwap instruction.
136#[derive(Accounts, Clone)]
137pub struct SwapOutput<'info> {
138    /// The token accounts of the user and the token.
139    pub user_token: SwapToken<'info>,
140    /// The token account for the fees associated with the token.
141    pub fees: AccountInfo<'info>,
142}
143
144/// Accounts for an instruction that interacts with the swap.
145#[derive(Accounts, Clone)]
146pub struct SwapUserContext<'info> {
147    /// The spl_token program.
148    pub token_program: AccountInfo<'info>,
149    /// The authority of the swap.
150    pub swap_authority: AccountInfo<'info>,
151    /// The authority of the user.
152    #[account(signer)]
153    pub user_authority: AccountInfo<'info>,
154    /// The swap.
155    pub swap: AccountInfo<'info>,
156}
157
158/// Accounts for an instruction that requires admin permission.
159#[derive(Accounts, Clone)]
160pub struct AdminUserContext<'info> {
161    /// The public key of the admin account.
162    ///
163    /// *Note: must be a signer.*
164    #[account(signer)]
165    pub admin: AccountInfo<'info>,
166    /// The swap.
167    pub swap: AccountInfo<'info>,
168}