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

Liquidation protocol fee #71

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add claim protocol fees
  • Loading branch information
ra authored and nope-finance committed Feb 17, 2022
commit da28ec974582591f39c5a41213c180c42bcb511c
35 changes: 35 additions & 0 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ pub enum LendingInstruction {
/// Reserve config to update to
config: ReserveConfig,
},

// 17
/// Claims unclaimed reserve protocol fees
ClaimProtocolFees,
}

impl LendingInstruction {
Expand Down Expand Up @@ -700,6 +704,9 @@ impl LendingInstruction {
buf.extend_from_slice(&config.fee_receiver.to_bytes());
buf.extend_from_slice(&config.protocol_liquidation_fee.to_le_bytes());
}
Self::ClaimProtocolFees => {
buf.push(17);
}
}
buf
}
Expand Down Expand Up @@ -1224,3 +1231,31 @@ pub fn update_reserve_config(
data: LendingInstruction::UpdateReserveConfig { config }.pack(),
}
}

/// Creates an 'ClaimProtocolFees' instruction
pub fn claim_protocol_fees(
program_id: Pubkey,
reserve_pubkey: Pubkey,
lending_market_pubkey: Pubkey,
lending_market_owner_pubkey: Pubkey,
reserve_liquidity_supply_pubkey: Pubkey,
reserve_liquidity_fee_receiver_pubkey: Pubkey,
) -> Instruction {
let (lending_market_authority_pubkey, _bump_seed) = Pubkey::find_program_address(
&[&lending_market_pubkey.to_bytes()[..PUBKEY_BYTES]],
&program_id,
);
let accounts = vec![
AccountMeta::new(reserve_pubkey, false),
AccountMeta::new_readonly(lending_market_pubkey, false),
AccountMeta::new_readonly(lending_market_authority_pubkey, false),
AccountMeta::new_readonly(lending_market_owner_pubkey, true),
AccountMeta::new(reserve_liquidity_supply_pubkey, false),
AccountMeta::new(reserve_liquidity_fee_receiver_pubkey, false),
];
Instruction {
program_id,
accounts,
data: LendingInstruction::ClaimProtocolFees.pack(),
}
}
84 changes: 84 additions & 0 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ pub fn process_instruction(
msg!("Instruction: UpdateReserveConfig");
process_update_reserve_config(program_id, config, accounts)
}
LendingInstruction::ClaimProtocolFees => {
msg!("Instruction: Update");
process_claim_protocol_fees(program_id, accounts)
}
}
}

Expand Down Expand Up @@ -2054,6 +2058,86 @@ fn process_update_reserve_config(
Ok(())
}

#[inline(never)] // avoid stack frame limit
fn process_claim_protocol_fees(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let reserve_info = next_account_info(account_info_iter)?;
let lending_market_info = next_account_info(account_info_iter)?;
let lending_market_authority_info = next_account_info(account_info_iter)?;
let lending_market_owner_info = next_account_info(account_info_iter)?;
let reserve_liquidity_supply_info = next_account_info(account_info_iter)?;
let reserve_liquidity_fee_receiver_info = next_account_info(account_info_iter)?;
let token_program_id = next_account_info(account_info_iter)?;

let mut reserve = Reserve::unpack(&reserve_info.data.borrow())?;
if reserve_info.owner != program_id {
msg!(
"Reserve provided is not owned by the lending program {} != {}",
&reserve_info.owner.to_string(),
&program_id.to_string(),
);
return Err(LendingError::InvalidAccountOwner.into());
}
if &reserve.lending_market != lending_market_info.key {
msg!("Reserve lending market does not match the lending market provided");
return Err(LendingError::InvalidAccountInput.into());
}

let lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
if lending_market_info.owner != program_id {
msg!(
"Lending market provided is not owned by the lending program {} != {}",
&lending_market_info.owner.to_string(),
&program_id.to_string(),
);
return Err(LendingError::InvalidAccountOwner.into());
}
if &lending_market.owner != lending_market_owner_info.key {
msg!("Lending market owner does not match the lending market owner provided");
return Err(LendingError::InvalidMarketOwner.into());
}
if !lending_market_owner_info.is_signer {
msg!("Lending market owner provided must be a signer");
return Err(LendingError::InvalidSigner.into());
}

let authority_signer_seeds = &[
lending_market_info.key.as_ref(),
&[lending_market.bump_seed],
];
let lending_market_authority_pubkey =
Pubkey::create_program_address(authority_signer_seeds, program_id)?;
if &lending_market_authority_pubkey != lending_market_authority_info.key {
msg!(
"Derived lending market authority does not match the lending market authority provided"
);
return Err(LendingError::InvalidMarketAuthority.into());
}

// TODO - add checks for the reserve liquidity supply, fee receiver, and token program

// transfer accumulated fees to fee receiver
let amount_to_transfer = reserve
.liquidity
.accumulated_protocol_fees
.try_floor_u64()?;
reserve.liquidity.accumulated_protocol_fees = reserve
.liquidity
.accumulated_protocol_fees
.try_sub(Decimal::from(amount_to_transfer))?;
spl_token_transfer(TokenTransferParams {
source: reserve_liquidity_supply_info.clone(),
destination: reserve_liquidity_fee_receiver_info.clone(),
amount: amount_to_transfer,
authority: lending_market_authority_info.clone(),
authority_signer_seeds,
token_program: token_program_id.clone(),
})?;

Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;
Ok(())
}

fn assert_rent_exempt(rent: &Rent, account_info: &AccountInfo) -> ProgramResult {
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) {
msg!(
Expand Down