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

Origination fees on borrow #239

Merged
merged 18 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ Integration tests for the on-chain marginfi programs are located under
`/programs/marginfi/tests`. To run the tests, use `cargo test-bpf`. Be sure to
use an x86 toolchain when compiling and running the tests.

Run the full test suite with `.scripts/test-program.sh <program_to_test>`
* e.g. `.scripts/test-program.sh all --sane`
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/test-program.sh <program_to_test> <name_of_test>`
* e.g. `./scripts/test-program.sh marginfi configure_bank_success --verbose`

## Footguns

Expand Down
3 changes: 2 additions & 1 deletion programs/marginfi/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub struct LendingAccountBorrowEvent {
pub header: AccountEventHeader,
pub bank: Pubkey,
pub mint: Pubkey,
pub amount: u64,
pub amount_pre_fee: u64,
jkbpvsc marked this conversation as resolved.
Show resolved Hide resolved
pub fee: u64
}

#[event]
Expand Down
45 changes: 42 additions & 3 deletions programs/marginfi/src/instructions/marginfi_account/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
bank_signer, check,
constants::{LIQUIDITY_VAULT_AUTHORITY_SEED, LIQUIDITY_VAULT_SEED},
constants::{FEE_VAULT_SEED, LIQUIDITY_VAULT_AUTHORITY_SEED, LIQUIDITY_VAULT_SEED},
events::{AccountEventHeader, LendingAccountBorrowEvent},
math_error,
prelude::{MarginfiError, MarginfiGroup, MarginfiResult},
state::{
marginfi_account::{BankAccountWrapper, MarginfiAccount, RiskEngine, DISABLED_FLAG},
Expand Down Expand Up @@ -32,6 +33,7 @@ pub fn lending_account_borrow<'info>(
token_program,
bank_liquidity_vault_authority,
bank: bank_loader,
fee_vault,
..
} = ctx.accounts;
let clock = Clock::get()?;
Expand All @@ -58,6 +60,11 @@ pub fn lending_account_borrow<'info>(
let mut bank = bank_loader.load_mut()?;

let liquidity_vault_authority_bump = bank.liquidity_vault_authority_bump;
let origination_fee_rate: I80F48 = bank
.config
.interest_rate_config
.protocol_origination_fee // <- a new configurable fee...
.into();

let mut bank_account = BankAccountWrapper::find_or_create(
&bank_loader.key(),
Expand All @@ -77,8 +84,13 @@ pub fn lending_account_borrow<'info>(
})
.transpose()?
.unwrap_or(amount);
let origination_fee: I80F48 = I80F48::from_num(amount_pre_fee)
.checked_mul(origination_fee_rate)
.ok_or_else(math_error!())?;
let origination_fee_u64: u64 = origination_fee.checked_to_num().ok_or_else(math_error!())?;

bank_account.borrow(I80F48::from_num(amount_pre_fee))?;
// Incurs a borrow that includes the origination fee, but withdraws just the amt
bank_account.borrow(I80F48::from_num(amount_pre_fee) + origination_fee)?;
bank_account.withdraw_spl_transfer(
amount_pre_fee,
bank_liquidity_vault.to_account_info(),
Expand All @@ -94,6 +106,22 @@ pub fn lending_account_borrow<'info>(
ctx.remaining_accounts,
)?;

// The bank fee account gains the origination fee
bank_account.withdraw_spl_transfer(
origination_fee_u64,
bank_liquidity_vault.to_account_info(),
fee_vault.to_account_info(),
bank_liquidity_vault_authority.to_account_info(),
maybe_bank_mint.as_ref(),
token_program.to_account_info(),
bank_signer!(
BankVaultType::Liquidity,
bank_loader.key(),
liquidity_vault_authority_bump
),
ctx.remaining_accounts,
)?;

emit!(LendingAccountBorrowEvent {
header: AccountEventHeader {
signer: Some(ctx.accounts.signer.key()),
Expand All @@ -103,7 +131,8 @@ pub fn lending_account_borrow<'info>(
},
bank: bank_loader.key(),
mint: bank.mint,
amount: amount_pre_fee,
amount_pre_fee: amount_pre_fee,
fee: origination_fee_u64
});
}

Expand Down Expand Up @@ -159,5 +188,15 @@ pub struct LendingAccountBorrow<'info> {
)]
pub bank_liquidity_vault: InterfaceAccount<'info, TokenAccount>,

#[account(
mut,
seeds = [
FEE_VAULT_SEED.as_bytes(),
bank.key().as_ref(),
],
bump,
)]
pub fee_vault: InterfaceAccount<'info, TokenAccount>,

jgur-psyops marked this conversation as resolved.
Show resolved Hide resolved
pub token_program: Interface<'info, TokenInterface>,
}
10 changes: 8 additions & 2 deletions programs/marginfi/src/state/marginfi_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub struct InterestRateConfigCompact {
pub insurance_ir_fee: WrappedI80F48,
pub protocol_fixed_fee_apr: WrappedI80F48,
pub protocol_ir_fee: WrappedI80F48,
pub protocol_origination_fee: WrappedI80F48,
}

impl From<InterestRateConfigCompact> for InterestRateConfig {
Expand All @@ -110,7 +111,9 @@ impl From<InterestRateConfigCompact> for InterestRateConfig {
insurance_ir_fee: ir_config.insurance_ir_fee,
protocol_fixed_fee_apr: ir_config.protocol_fixed_fee_apr,
protocol_ir_fee: ir_config.protocol_ir_fee,
_padding: [[0; 2]; 8],
protocol_origination_fee: ir_config.protocol_origination_fee,
_padding0: [0; 16],
_padding1: [[0; 32]; 3],
}
}
}
Expand All @@ -125,6 +128,7 @@ impl From<InterestRateConfig> for InterestRateConfigCompact {
insurance_ir_fee: ir_config.insurance_ir_fee,
protocol_fixed_fee_apr: ir_config.protocol_fixed_fee_apr,
protocol_ir_fee: ir_config.protocol_ir_fee,
protocol_origination_fee: ir_config.protocol_origination_fee,
}
}
}
Expand All @@ -147,8 +151,10 @@ pub struct InterestRateConfig {
pub insurance_ir_fee: WrappedI80F48,
pub protocol_fixed_fee_apr: WrappedI80F48,
pub protocol_ir_fee: WrappedI80F48,
pub protocol_origination_fee: WrappedI80F48,

pub _padding: [[u64; 2]; 8], // 16 * 8 = 128 bytes
pub _padding0: [u8; 16],
pub _padding1: [[u8; 32]; 3]
}

impl InterestRateConfig {
Expand Down
1 change: 1 addition & 0 deletions test-utils/src/marginfi_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl MarginfiAccountFixture {
destination_token_account: destination_account,
bank_liquidity_vault: bank.get_vault(BankVaultType::Liquidity).0,
bank_liquidity_vault_authority: bank.get_vault_authority(BankVaultType::Liquidity).0,
fee_vault: bank.get_vault(BankVaultType::Fee).0,
token_program: bank.get_token_program(),
}
.to_account_metas(Some(true));
Expand Down
Loading