-
Notifications
You must be signed in to change notification settings - Fork 2
[LST-284] WithdrawProtocolFeesV2
#141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jereldlimjy
merged 10 commits into
v2
from
lst-284-new-instructionwithdrawprotocolfeesv2
Dec 4, 2025
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a202472
feat: add withdrawprotocolfeesv2 impl
jereldlimjy b3ae7eb
feat: add correct strat proptest
jereldlimjy c3aecf3
proptest finally working
jereldlimjy 76b278b
fix: clippy
jereldlimjy 662ade0
fix: review comments
jereldlimjy aaa9f1a
Merge branch 'v2' of https://github.com/igneous-labs/inf-1.5 into lst…
jereldlimjy 94eb0e4
chore: remove regression
jereldlimjy 3316436
fix: verify
jereldlimjy dc94a48
fix: review comments
jereldlimjy 7d89d28
chore: remove unused
jereldlimjy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| pub mod set_protocol_fee; | ||
| pub mod set_protocol_fee_beneficiary; | ||
| pub mod withdraw_protocol_fees; | ||
| pub mod v1; | ||
| pub mod v2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod set_protocol_fee; | ||
| pub mod set_protocol_fee_beneficiary; | ||
| pub mod withdraw_protocol_fees; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod withdraw_protocol_fees; |
55 changes: 55 additions & 0 deletions
55
controller/core/src/instructions/protocol_fee/v2/withdraw_protocol_fees.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use generic_array_struct::generic_array_struct; | ||
|
|
||
| use crate::instructions::generic::DiscmOnlyIxData; | ||
|
|
||
| // Accounts | ||
|
|
||
| #[generic_array_struct(builder pub)] | ||
| #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] | ||
| #[repr(transparent)] | ||
| pub struct WithdrawProtocolFeesV2IxAccs<T> { | ||
| /// The pool's state singleton PDA | ||
| pub pool_state: T, | ||
|
|
||
| /// The pool's protocol fee beneficiary | ||
| pub beneficiary: T, | ||
|
|
||
| /// INF token account to withdraw unclaimed protocol fees to | ||
| pub withdraw_to: T, | ||
|
|
||
| /// INF token mint | ||
| pub inf_mint: T, | ||
|
|
||
| /// INF token program | ||
| pub token_program: T, | ||
| } | ||
|
|
||
| impl<T: Copy> WithdrawProtocolFeesV2IxAccs<T> { | ||
| #[inline] | ||
| pub const fn memset(val: T) -> Self { | ||
| Self([val; WITHDRAW_PROTOCOL_FEES_V2_IX_ACCS_LEN]) | ||
| } | ||
| } | ||
|
|
||
| pub type WithdrawProtocolFeesV2IxKeys<'a> = WithdrawProtocolFeesV2IxAccs<&'a [u8; 32]>; | ||
|
|
||
| pub type WithdrawProtocolFeesV2IxKeysOwned = WithdrawProtocolFeesV2IxAccs<[u8; 32]>; | ||
|
|
||
| pub type WithdrawProtocolFeesV2IxAccFlags = WithdrawProtocolFeesV2IxAccs<bool>; | ||
|
|
||
| pub const WITHDRAW_PROTOCOL_FEES_V2_IX_IS_WRITER: WithdrawProtocolFeesV2IxAccFlags = | ||
| WithdrawProtocolFeesV2IxAccFlags::memset(false) | ||
| .const_with_pool_state(true) | ||
| .const_with_withdraw_to(true) | ||
| .const_with_inf_mint(true); | ||
|
|
||
| pub const WITHDRAW_PROTOCOL_FEES_V2_IX_IS_SIGNER: WithdrawProtocolFeesV2IxAccFlags = | ||
| WithdrawProtocolFeesV2IxAccFlags::memset(false).const_with_beneficiary(true); | ||
|
|
||
| // Data | ||
|
|
||
| pub const WITHDRAW_PROTOCOL_FEES_V2_IX_DISCM: u8 = 25; | ||
|
|
||
| pub type WithdrawProtocolFeesV2IxData = DiscmOnlyIxData<WITHDRAW_PROTOCOL_FEES_V2_IX_DISCM>; | ||
|
|
||
| pub const WITHDRAW_PROTOCOL_FEES_V2_IX_DATA_LEN: usize = WithdrawProtocolFeesV2IxData::DATA_LEN; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| pub mod set_protocol_fee; | ||
| pub mod set_protocol_fee_beneficiary; | ||
| pub mod withdraw_protocol_fee; | ||
| pub mod v1; | ||
| pub mod v2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod set_protocol_fee; | ||
| pub mod set_protocol_fee_beneficiary; | ||
| pub mod withdraw_protocol_fees; |
2 changes: 1 addition & 1 deletion
2
...ructions/protocol_fee/set_protocol_fee.rs → ...tions/protocol_fee/v1/set_protocol_fee.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tocol_fee/set_protocol_fee_beneficiary.rs → ...ol_fee/v1/set_protocol_fee_beneficiary.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ons/protocol_fee/withdraw_protocol_fee.rs → ...protocol_fee/v1/withdraw_protocol_fees.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod withdraw_protocol_fees; |
121 changes: 121 additions & 0 deletions
121
controller/program/src/instructions/protocol_fee/v2/withdraw_protocol_fees.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| use inf1_ctl_jiminy::{ | ||
| account_utils::{pool_state_v2_checked, pool_state_v2_checked_mut}, | ||
| err::Inf1CtlErr, | ||
| instructions::protocol_fee::v2::withdraw_protocol_fees::{ | ||
| NewWithdrawProtocolFeesV2IxAccsBuilder, WithdrawProtocolFeesV2IxAccs, | ||
| WITHDRAW_PROTOCOL_FEES_V2_IX_IS_SIGNER, | ||
| }, | ||
| keys::POOL_STATE_ID, | ||
| pda_onchain::POOL_STATE_SIGNER, | ||
| program_err::Inf1CtlCustomProgErr, | ||
| svc::InfCalc, | ||
| typedefs::pool_sv::PoolSvLamports, | ||
| }; | ||
| use jiminy_cpi::{ | ||
| account::{Abr, AccountHandle}, | ||
| program_error::{ProgramError, INVALID_ACCOUNT_DATA, NOT_ENOUGH_ACCOUNT_KEYS}, | ||
| Cpi, | ||
| }; | ||
| use jiminy_sysvar_clock::Clock; | ||
| use sanctum_spl_token_jiminy::{ | ||
| instructions::mint_to::mint_to_ix_account_handle_perms, | ||
| sanctum_spl_token_core::{ | ||
| instructions::mint_to::{MintToIxData, NewMintToIxAccsBuilder}, | ||
| state::mint::{Mint, RawMint}, | ||
| }, | ||
| }; | ||
|
|
||
| use crate::verify::{ | ||
| verify_not_rebalancing_and_not_disabled_v2, verify_pks, verify_signers, | ||
| verify_tokenkeg_or_22_mint, | ||
| }; | ||
|
|
||
| type WithdrawProtocolFeesV2IxAccounts<'acc> = WithdrawProtocolFeesV2IxAccs<AccountHandle<'acc>>; | ||
|
|
||
| #[inline] | ||
| pub fn withdraw_protocol_fees_v2_checked<'acc>( | ||
| abr: &Abr, | ||
| accs: &[AccountHandle<'acc>], | ||
| ) -> Result<WithdrawProtocolFeesV2IxAccounts<'acc>, ProgramError> { | ||
| let accs = accs.first_chunk().ok_or(NOT_ENOUGH_ACCOUNT_KEYS)?; | ||
| let accs = WithdrawProtocolFeesV2IxAccs(*accs); | ||
|
|
||
| let pool = pool_state_v2_checked(abr.get(*accs.pool_state()))?; | ||
| let mint_acc = abr.get(*accs.inf_mint()); | ||
|
|
||
| let expected_pks = NewWithdrawProtocolFeesV2IxAccsBuilder::start() | ||
| .with_pool_state(&POOL_STATE_ID) | ||
| .with_beneficiary(&pool.protocol_fee_beneficiary) | ||
| .with_inf_mint(&pool.lp_token_mint) | ||
| .with_token_program(mint_acc.owner()) | ||
| // Free: the beneficiary is free to specify whatever INF token account to withdraw to | ||
| // In the case of an invalid INF token acc, token prog mint_to CPI will fail | ||
| .with_withdraw_to(abr.get(*accs.withdraw_to()).key()) | ||
| .build(); | ||
| verify_pks(abr, &accs.0, &expected_pks.0)?; | ||
|
|
||
| verify_signers(abr, &accs.0, &WITHDRAW_PROTOCOL_FEES_V2_IX_IS_SIGNER.0)?; | ||
|
|
||
| verify_not_rebalancing_and_not_disabled_v2(pool)?; | ||
|
|
||
| verify_tokenkeg_or_22_mint(mint_acc)?; | ||
jereldlimjy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Ok(accs) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn process_withdraw_protocol_fees_v2( | ||
| abr: &mut Abr, | ||
| cpi: &mut Cpi, | ||
| accs: WithdrawProtocolFeesV2IxAccounts, | ||
| clock: &Clock, | ||
| ) -> Result<(), ProgramError> { | ||
| let pool = pool_state_v2_checked_mut(abr.get_mut(*accs.pool_state()))?; | ||
| pool.release_yield(clock.slot) | ||
| .map_err(Inf1CtlCustomProgErr)?; | ||
|
|
||
| let protocol_fee_lamports = pool.protocol_fee_lamports; | ||
|
|
||
| if protocol_fee_lamports == 0 { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let pool_lamports = PoolSvLamports::from_pool_state_v2(pool); | ||
| let inf_mint_data = abr.get(*accs.inf_mint()).data(); | ||
| let inf_token_supply = RawMint::of_acc_data(inf_mint_data) | ||
| .and_then(Mint::try_from_raw) | ||
| .map(|a| a.supply()) | ||
| .ok_or(INVALID_ACCOUNT_DATA)?; | ||
jereldlimjy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let inf_calc = InfCalc { | ||
| pool_lamports, | ||
| mint_supply: inf_token_supply, | ||
| }; | ||
|
|
||
| let inf_to_mint = inf_calc | ||
| .sol_to_inf(protocol_fee_lamports) | ||
| .ok_or(Inf1CtlCustomProgErr(Inf1CtlErr::MathError))?; | ||
|
|
||
| if inf_to_mint == 0 { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| cpi.invoke_signed_handle( | ||
| abr, | ||
| *accs.token_program(), | ||
| MintToIxData::new(inf_to_mint).as_buf(), | ||
| mint_to_ix_account_handle_perms( | ||
| NewMintToIxAccsBuilder::start() | ||
| .with_auth(*accs.pool_state()) | ||
| .with_mint(*accs.inf_mint()) | ||
| .with_to(*accs.withdraw_to()) | ||
| .build(), | ||
| ), | ||
| &[POOL_STATE_SIGNER], | ||
| )?; | ||
|
|
||
| let pool = pool_state_v2_checked_mut(abr.get_mut(*accs.pool_state()))?; | ||
| pool.protocol_fee_lamports = 0; | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| mod set_protocol_fee; | ||
| mod set_protocol_fee_beneficiary; | ||
| mod withdraw_protocol_fees; | ||
| pub mod v1; | ||
| pub mod v2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| mod set_protocol_fee; | ||
| mod set_protocol_fee_beneficiary; | ||
| mod withdraw_protocol_fees; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tocol_fee/set_protocol_fee_beneficiary.rs → ...ol_fee/v1/set_protocol_fee_beneficiary.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| mod withdraw_protocol_fees; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.