Skip to content
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

Add flashloan fee #273

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ Run the full test suite with `./scripts/test-program.sh <program_to_test>`
* e.g. `./scripts/test-program.sh all --sane`

Run a single test:
`./scripts/test-program.sh <program_to_test> <name_of_test>`
* e.g. `./scripts/test-program.sh marginfi configure_bank_success --verbose`
`./scripts/single-test.sh <program_to_test> <name_of_test>`
* e.g. `./scripts/single-test.sh marginfi flashloan_success_1op --verbose`

## Localnet Anchor Tests

Build the program with:

`anchor build -p marginfi -- --no-default-features`

You may also need to build the liquidity incentive program and mock program:
You may also need to build the liquidity incentive program, mock program, test_transfer_hook program, and brick program:

- `anchor build -p mocks`
- `anchor build -p liquidity_incentive_program -- --no-default-features`
- `anchor build -p mocks`
- `anchor build -p test_transfer_hook`
- `anchor build -p brick`

Remember to `yarn install`

Expand Down
8 changes: 8 additions & 0 deletions clients/rust/marginfi-cli/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ pub enum GroupCommand {
#[clap(long)]
bank_init_flat_sol_fee: u32,
#[clap(long)]
flashloan_flat_sol_fee: u32,
#[clap(long)]
program_fee_fixed: f64,
#[clap(long)]
program_fee_rate: f64,
Expand All @@ -179,6 +181,8 @@ pub enum GroupCommand {
#[clap(long)]
bank_init_flat_sol_fee: u32,
#[clap(long)]
flashloan_flat_sol_fee: u32,
#[clap(long)]
program_fee_fixed: f64,
#[clap(long)]
program_fee_rate: f64,
Expand Down Expand Up @@ -679,25 +683,29 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> {
admin,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
} => processor::initialize_fee_state(
config,
admin,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
),
GroupCommand::EditFeeState {
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
} => processor::edit_fee_state(
config,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
),
Expand Down
4 changes: 4 additions & 0 deletions clients/rust/marginfi-cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ pub fn initialize_fee_state(
admin: Pubkey,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: f64,
program_fee_rate: f64,
) -> Result<()> {
Expand All @@ -1019,6 +1020,7 @@ pub fn initialize_fee_state(
admin,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
})
Expand All @@ -1044,6 +1046,7 @@ pub fn edit_fee_state(
config: Config,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: f64,
program_fee_rate: f64,
) -> Result<()> {
Expand All @@ -1064,6 +1067,7 @@ pub fn edit_fee_state(
.args(marginfi::instruction::EditGlobalFeeState {
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
})
Expand Down
1 change: 1 addition & 0 deletions programs/marginfi/fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ fn initialize_fee_state<'a>(
// WARN: tests will fail at add_bank::system_program::transfer if this is non-zero because
// the fuzz suite does not yet support the system program.
0,
0,
I80F48!(0).into(),
I80F48!(0).into(),
)
Expand Down
1 change: 1 addition & 0 deletions programs/marginfi/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub const LIQUIDATION_INSURANCE_FEE: I80F48 = I80F48!(0.025);

/// The default fee, in native SOL in native decimals (i.e. lamports) used in testing
pub const INIT_BANK_ORIGINATION_FEE_DEFAULT: u32 = 10000;
pub const FLASHLOAN_FEE_DEFAULT: u32 = 100;

pub const SECONDS_PER_YEAR: I80F48 = I80F48!(31_536_000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use solana_program::{
use crate::{
check,
prelude::*,
state::fee_state::FeeState,
state::marginfi_account::{MarginfiAccount, RiskEngine, DISABLED_FLAG, IN_FLASHLOAN_FLAG},
};

Expand Down Expand Up @@ -129,6 +130,16 @@ pub fn lending_account_end_flashloan<'info>(
"End flashloan ix should not be in CPI"
);

// Transfer the flat sol flashloan fee to the global fee wallet
let fee_state = ctx.accounts.fee_state.load()?;
let flashloan_flat_sol_fee = fee_state.flashloan_flat_sol_fee;
if flashloan_flat_sol_fee > 0 {
anchor_lang::system_program::transfer(
ctx.accounts.transfer_flat_fee(),
flashloan_flat_sol_fee as u64,
)?;
}

let mut marginfi_account = ctx.accounts.marginfi_account.load_mut()?;

marginfi_account.unset_flag(IN_FLASHLOAN_FLAG);
Expand All @@ -144,4 +155,24 @@ pub struct LendingAccountEndFlashloan<'info> {
pub marginfi_account: AccountLoader<'info, MarginfiAccount>,
#[account(address = marginfi_account.load()?.authority)]
pub signer: Signer<'info>,
#[account(has_one = global_fee_wallet)]
pub fee_state: AccountLoader<'info, FeeState>,
/// CHECK: The fee admin's native SOL wallet, validated against fee state
#[account(mut)]
pub global_fee_wallet: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}

impl<'info> LendingAccountEndFlashloan<'info> {
fn transfer_flat_fee(
&self,
) -> CpiContext<'_, '_, '_, 'info, anchor_lang::system_program::Transfer<'info>> {
CpiContext::new(
self.system_program.to_account_info(),
anchor_lang::system_program::Transfer {
from: self.signer.to_account_info(),
to: self.global_fee_wallet.to_account_info(),
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pub fn edit_fee_state(
ctx: Context<EditFeeState>,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: WrappedI80F48,
program_fee_rate: WrappedI80F48,
) -> Result<()> {
let mut fee_state = ctx.accounts.fee_state.load_mut()?;
fee_state.global_fee_wallet = fee_wallet;
fee_state.bank_init_flat_sol_fee = bank_init_flat_sol_fee;
fee_state.flashloan_flat_sol_fee = flashloan_flat_sol_fee;
fee_state.program_fee_fixed = program_fee_fixed;
fee_state.program_fee_rate = program_fee_rate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn initialize_fee_state(
admin_key: Pubkey,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: WrappedI80F48,
program_fee_rate: WrappedI80F48,
) -> Result<()> {
Expand All @@ -26,6 +27,7 @@ pub fn initialize_fee_state(
fee_state.global_fee_wallet = fee_wallet;
fee_state.key = ctx.accounts.fee_state.key();
fee_state.bank_init_flat_sol_fee = bank_init_flat_sol_fee;
fee_state.flashloan_flat_sol_fee = flashloan_flat_sol_fee;
fee_state.bump_seed = ctx.bumps.fee_state;
fee_state.program_fee_fixed = program_fee_fixed;
fee_state.program_fee_rate = program_fee_rate;
Expand Down
4 changes: 4 additions & 0 deletions programs/marginfi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub mod marginfi {
admin: Pubkey,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: WrappedI80F48,
program_fee_rate: WrappedI80F48,
) -> MarginfiResult {
Expand All @@ -238,6 +239,7 @@ pub mod marginfi {
admin,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
)
Expand All @@ -248,13 +250,15 @@ pub mod marginfi {
ctx: Context<EditFeeState>,
fee_wallet: Pubkey,
bank_init_flat_sol_fee: u32,
flashloan_flat_sol_fee: u32,
program_fee_fixed: WrappedI80F48,
program_fee_rate: WrappedI80F48,
) -> MarginfiResult {
marginfi_group::edit_fee_state(
ctx,
fee_wallet,
bank_init_flat_sol_fee,
flashloan_flat_sol_fee,
program_fee_fixed,
program_fee_rate,
)
Expand Down
5 changes: 4 additions & 1 deletion programs/marginfi/src/state/fee_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ pub struct FeeState {
pub program_fee_fixed: WrappedI80F48,
/// Fee collected by the program owner from all groups
pub program_fee_rate: WrappedI80F48,
/// Flat fee assessed at flashloan end, in lamports.
/// * In SOL, in native decimals.
pub flashloan_flat_sol_fee: u32,
// Reserved for future use
_reserved0: [u8; 32],
_reserved0: [u8; 28],
_reserved1: [u8; 64],
}

Expand Down
Loading
Loading