Skip to content

Commit dce2442

Browse files
authored
Wphan/add liquidity (#1607)
* add add remove liquidity fees calc * add liquidity ix * fix init mint and lppool token account, refactor test fees * add removeLiquidity bankrun test * merge upstream * add LPPool.next_mint_redeem_id
1 parent b363288 commit dce2442

File tree

14 files changed

+1917
-63
lines changed

14 files changed

+1917
-63
lines changed

programs/drift/src/controller/token.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use anchor_spl::token_2022::spl_token_2022::extension::{
88
};
99
use anchor_spl::token_2022::spl_token_2022::state::Mint as MintInner;
1010
use anchor_spl::token_interface::{
11-
self, CloseAccount, Mint, TokenAccount, TokenInterface, Transfer, TransferChecked,
11+
self, Burn, CloseAccount, Mint, MintTo, TokenAccount, TokenInterface, Transfer, TransferChecked,
1212
};
1313

1414
pub fn send_from_program_vault<'info>(
@@ -106,6 +106,58 @@ pub fn close_vault<'info>(
106106
token_interface::close_account(cpi_context)
107107
}
108108

109+
pub fn mint_tokens<'info>(
110+
token_program: &Interface<'info, TokenInterface>,
111+
destination: &InterfaceAccount<'info, TokenAccount>,
112+
authority: &AccountInfo<'info>,
113+
nonce: u8,
114+
amount: u64,
115+
mint: &InterfaceAccount<'info, Mint>,
116+
) -> Result<()> {
117+
let signature_seeds = get_signer_seeds(&nonce);
118+
let signers = &[&signature_seeds[..]];
119+
120+
let mint_account_info = mint.to_account_info();
121+
122+
validate_mint_fee(&mint_account_info)?;
123+
124+
let cpi_accounts = MintTo {
125+
mint: mint_account_info,
126+
to: destination.to_account_info(),
127+
authority: authority.to_account_info(),
128+
};
129+
130+
let cpi_program = token_program.to_account_info();
131+
let cpi_context = CpiContext::new_with_signer(cpi_program, cpi_accounts, signers);
132+
token_interface::mint_to(cpi_context, amount)
133+
}
134+
135+
pub fn burn_tokens<'info>(
136+
token_program: &Interface<'info, TokenInterface>,
137+
destination: &InterfaceAccount<'info, TokenAccount>,
138+
authority: &AccountInfo<'info>,
139+
nonce: u8,
140+
amount: u64,
141+
mint: &InterfaceAccount<'info, Mint>,
142+
) -> Result<()> {
143+
let signature_seeds = get_signer_seeds(&nonce);
144+
let signers = &[&signature_seeds[..]];
145+
146+
let mint_account_info = mint.to_account_info();
147+
148+
validate_mint_fee(&mint_account_info)?;
149+
150+
let cpi_accounts = Burn {
151+
mint: mint_account_info,
152+
from: destination.to_account_info(),
153+
authority: authority.to_account_info(),
154+
};
155+
156+
let cpi_program = token_program.to_account_info();
157+
let cpi_context = CpiContext::new_with_signer(cpi_program, cpi_accounts, signers);
158+
token_interface::burn(cpi_context, amount)
159+
}
160+
109161
pub fn validate_mint_fee(account_info: &AccountInfo) -> Result<()> {
110162
let mint_data = account_info.try_borrow_data()?;
111163
let mint_with_extension = StateWithExtensions::<MintInner>::unpack(&mint_data)?;

programs/drift/src/instructions/admin.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,9 @@ pub fn handle_initialize_high_leverage_mode_config(
44804480
pub fn handle_initialize_lp_pool(
44814481
ctx: Context<InitializeLpPool>,
44824482
name: [u8; 32],
4483+
min_mint_fee: i64,
4484+
max_mint_fee: i64,
4485+
revenue_rebalance_period: u64,
44834486
max_aum: u128,
44844487
) -> Result<()> {
44854488
let mut lp_pool = ctx.accounts.lp_pool.load_init()?;
@@ -4497,8 +4500,13 @@ pub fn handle_initialize_lp_pool(
44974500
last_revenue_rebalance_ts: 0,
44984501
total_fees_received: 0,
44994502
total_fees_paid: 0,
4503+
total_mint_redeem_fees_paid: 0,
45004504
oldest_oracle_slot: 0,
45014505
bump: ctx.bumps.lp_pool,
4506+
min_mint_fee,
4507+
max_mint_fee_premium: max_mint_fee,
4508+
revenue_rebalance_period,
4509+
next_mint_redeem_id: 1,
45024510
_padding: [0; 12],
45034511
};
45044512

@@ -5763,13 +5771,17 @@ pub struct InitializeLpPool<'info> {
57635771
)]
57645772
pub lp_pool: AccountLoader<'info, LPPool>,
57655773

5774+
pub mint: Account<'info, anchor_spl::token::Mint>,
5775+
57665776
#[account(
57675777
init,
5778+
seeds = [b"LP_POOL_TOKEN_VAULT".as_ref(), lp_pool.key().as_ref()],
5779+
bump,
57685780
payer = admin,
5769-
mint::decimals = 6,
5770-
mint::authority = lp_pool.key(),
5781+
token::mint = mint,
5782+
token::authority = drift_signer
57715783
)]
5772-
pub mint: Account<'info, anchor_spl::token::Mint>,
5784+
pub lp_pool_token_vault: Box<InterfaceAccount<'info, TokenAccount>>,
57735785

57745786
#[account(
57755787
init,
@@ -5793,6 +5805,8 @@ pub struct InitializeLpPool<'info> {
57935805
has_one = admin
57945806
)]
57955807
pub state: Box<Account<'info, State>>,
5808+
/// CHECK: program signer
5809+
pub drift_signer: AccountInfo<'info>,
57965810

57975811
pub token_program: Program<'info, Token>,
57985812

0 commit comments

Comments
 (0)