Skip to content

Commit

Permalink
Remove max uint functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
0xkiplet committed Feb 17, 2022
1 parent a14e6b3 commit aff68d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 81 deletions.
20 changes: 2 additions & 18 deletions token-lending/program/src/processor.rs
Expand Up @@ -686,13 +686,7 @@ fn _flash_borrow_reserve_liquidity<'a>(
return Err(LendingError::FlashLoansDisabled.into());
}

// @FIXME: if u64::MAX is flash loaned, fees should be inclusive as with ordinary borrows
let flash_loan_amount = if liquidity_amount == u64::MAX {
reserve.liquidity.available_amount
} else {
liquidity_amount
};

let flash_loan_amount = liquidity_amount;
let flash_loan_amount_decimal = Decimal::from(flash_loan_amount);

// Make sure this isnt a cpi call
Expand Down Expand Up @@ -738,9 +732,6 @@ fn _flash_borrow_reserve_liquidity<'a>(
}
}

reserve
.liquidity
.set_flash_borrow_amount(flash_loan_amount)?;
reserve.liquidity.borrow(flash_loan_amount_decimal)?;
reserve.borrowing = true;
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;
Expand Down Expand Up @@ -841,11 +832,7 @@ fn _flash_repay_reserve_liquidity<'a>(
}

// @FIXME: if u64::MAX is flash loaned, fees should be inclusive as with ordinary borrows
let flash_loan_amount = if liquidity_amount == u64::MAX {
reserve.liquidity.flash_borrowed_amount
} else {
liquidity_amount
};
let flash_loan_amount = liquidity_amount;

let flash_loan_amount_decimal = Decimal::from(flash_loan_amount);
let (origination_fee, host_fee) = reserve
Expand All @@ -865,9 +852,6 @@ fn _flash_repay_reserve_liquidity<'a>(
return Err(LendingError::FlashRepayCpi.into());
}

reserve
.liquidity
.verify_and_reset_flash_borrow_amount(flash_loan_amount)?;
reserve
.liquidity
.repay(flash_loan_amount, flash_loan_amount_decimal)?;
Expand Down
35 changes: 2 additions & 33 deletions token-lending/program/src/state/reserve.rs
Expand Up @@ -366,8 +366,6 @@ pub struct ReserveLiquidity {
pub available_amount: u64,
/// Reserve liquidity borrowed
pub borrowed_amount_wads: Decimal,
/// Amount of borrowed_amount_wads which is currently being flash borrowed
pub flash_borrowed_amount: u64,
/// Reserve liquidity cumulative borrow rate
pub cumulative_borrow_rate_wads: Decimal,
/// Reserve liquidity market price in quote currency
Expand All @@ -385,7 +383,6 @@ impl ReserveLiquidity {
switchboard_oracle_pubkey: params.switchboard_oracle_pubkey,
available_amount: 0,
borrowed_amount_wads: Decimal::zero(),
flash_borrowed_amount: 0,
cumulative_borrow_rate_wads: Decimal::one(),
market_price: params.market_price,
}
Expand Down Expand Up @@ -447,28 +444,6 @@ impl ReserveLiquidity {
Ok(())
}

/// Set the amount being flash borrowed
pub fn set_flash_borrow_amount(&mut self, flash_borrow_amount: u64) -> ProgramResult {
self.flash_borrowed_amount = flash_borrow_amount;

Ok(())
}

/// Verify and reset the flash borrow amount
pub fn verify_and_reset_flash_borrow_amount(
&mut self,
flash_borrow_amount: u64,
) -> ProgramResult {
if flash_borrow_amount != self.flash_borrowed_amount {
msg!("Repay amount does not match flash borrowed amount");
return Err(LendingError::InvalidFlashRepay.into());
}

self.flash_borrowed_amount = 0;

Ok(())
}

/// Calculate the liquidity utilization rate of the reserve
pub fn utilization_rate(&self) -> Result<Rate, ProgramError> {
let total_supply = self.total_supply()?;
Expand Down Expand Up @@ -792,7 +767,6 @@ impl Pack for Reserve {
config_borrow_limit,
config_fee_receiver,
borrowing,
liquidity_flash_borrowed_amount,
_padding,
) = mut_array_refs![
output,
Expand Down Expand Up @@ -826,8 +800,7 @@ impl Pack for Reserve {
8,
PUBKEY_BYTES,
1,
8,
239
247
];

// reserve
Expand Down Expand Up @@ -876,7 +849,6 @@ impl Pack for Reserve {

// borrowing flag
pack_bool(self.borrowing, borrowing);
*liquidity_flash_borrowed_amount = self.liquidity.flash_borrowed_amount.to_le_bytes();
}

/// Unpacks a byte buffer into a [ReserveInfo](struct.ReserveInfo.html).
Expand Down Expand Up @@ -914,7 +886,6 @@ impl Pack for Reserve {
config_borrow_limit,
config_fee_receiver,
borrowing,
liquidity_flash_borrowed_amount,
_padding,
) = array_refs![
input,
Expand Down Expand Up @@ -948,8 +919,7 @@ impl Pack for Reserve {
8,
PUBKEY_BYTES,
1,
8,
239
247
];

let version = u8::from_le_bytes(*version);
Expand All @@ -975,7 +945,6 @@ impl Pack for Reserve {
),
available_amount: u64::from_le_bytes(*liquidity_available_amount),
borrowed_amount_wads: unpack_decimal(liquidity_borrowed_amount_wads),
flash_borrowed_amount: u64::from_le_bytes(*liquidity_flash_borrowed_amount),
cumulative_borrow_rate_wads: unpack_decimal(liquidity_cumulative_borrow_rate_wads),
market_price: unpack_decimal(liquidity_market_price),
},
Expand Down
41 changes: 11 additions & 30 deletions token-lending/program/tests/flash_borrow_repay.rs
Expand Up @@ -261,7 +261,7 @@ async fn test_success_multiple_borrow_repays() {
}

#[tokio::test]
async fn test_success_max_uint() {
async fn test_fail_max_uint() {
let mut test = ProgramTest::new(
"spl_token_lending",
spl_token_lending::id(),
Expand All @@ -274,7 +274,6 @@ async fn test_success_max_uint() {
const FLASH_LOAN_AMOUNT: u64 = u64::MAX;
const LIQUIDITY_AMOUNT: u64 = 1_000 * FRACTIONAL_TO_USDC;
const FEE_AMOUNT: u64 = 3_000_000;
const HOST_FEE_AMOUNT: u64 = 600_000;

test.prefer_bpf(false);

Expand Down Expand Up @@ -341,37 +340,19 @@ async fn test_success_max_uint() {
);

transaction.sign(&[&payer, &user_accounts_owner], recent_blockhash);
assert!(banks_client.process_transaction(transaction).await.is_ok());

let usdc_reserve = usdc_test_reserve.get_state(&mut banks_client).await;
// check that transaction fails
assert_eq!(
usdc_reserve.liquidity.available_amount,
initial_available_amount
banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(
0,
InstructionError::Custom(LendingError::InsufficientLiquidity as u32)
)
);

let (total_fee, host_fee) = usdc_reserve
.config
.fees
.calculate_flash_loan_fees(LIQUIDITY_AMOUNT.into())
.unwrap();
assert_eq!(total_fee, FEE_AMOUNT);
assert_eq!(host_fee, HOST_FEE_AMOUNT);

let liquidity_supply =
get_token_balance(&mut banks_client, usdc_test_reserve.liquidity_supply_pubkey).await;
assert_eq!(liquidity_supply, initial_liquidity_supply);

let token_balance =
get_token_balance(&mut banks_client, usdc_test_reserve.user_liquidity_pubkey).await;
assert_eq!(token_balance, initial_token_balance - FEE_AMOUNT);

let fee_balance =
get_token_balance(&mut banks_client, usdc_test_reserve.config.fee_receiver).await;
assert_eq!(fee_balance, FEE_AMOUNT - HOST_FEE_AMOUNT);

let host_fee_balance =
get_token_balance(&mut banks_client, usdc_test_reserve.liquidity_host_pubkey).await;
assert_eq!(host_fee_balance, HOST_FEE_AMOUNT);
}

#[tokio::test]
Expand Down

0 comments on commit aff68d4

Please sign in to comment.