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

sliding down borrow limit and 1 percent close factor #91

Merged
merged 3 commits into from
Jun 22, 2022
Merged
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
36 changes: 33 additions & 3 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ use solana_program::{
};
use spl_token::solana_program::instruction::AccountMeta;
use spl_token::state::{Account, Mint};
use std::{convert::TryInto, result::Result};
use std::{
cmp::{max, min},
convert::TryInto,
result::Result,
};
use switchboard_program::{
get_aggregator, get_aggregator_result, AggregatorState, RoundResult, SwitchboardAccountType,
};
Expand Down Expand Up @@ -897,8 +901,34 @@ fn process_refresh_obligation(program_id: &Pubkey, accounts: &[AccountInfo]) ->

obligation.deposited_value = deposited_value;
obligation.borrowed_value = borrowed_value;
obligation.allowed_borrow_value = allowed_borrow_value;
obligation.unhealthy_borrow_value = unhealthy_borrow_value;

// Wednesday, June 22, 2022 12:00:00 PM GMT
let start_timestamp = 1655899200u64;
// Wednesday, June 28, 2022 8:00:00 AM GMT
let end_timestamp = 1656403200u64;
let current_timestamp = clock.unix_timestamp as u64;
let current_timestamp_in_range = min(max(start_timestamp, current_timestamp), end_timestamp);
let numerator = end_timestamp
.checked_sub(current_timestamp_in_range)
.ok_or(LendingError::MathOverflow)?;
let denominator = end_timestamp
.checked_sub(start_timestamp)
.ok_or(LendingError::MathOverflow)?;

let start_global_unhealthy_borrow_value = Decimal::from(120000000u64);
let end_global_unhealthy_borrow_value = Decimal::from(50000000u64);

let global_unhealthy_borrow_value = end_global_unhealthy_borrow_value.try_add(
start_global_unhealthy_borrow_value
.try_sub(end_global_unhealthy_borrow_value)?
.try_mul(numerator)?
solendvega marked this conversation as resolved.
Show resolved Hide resolved
.try_div(denominator)?,
)?;
let global_allowed_borrow_value =
global_unhealthy_borrow_value.try_sub(Decimal::from(5000000u64))?;

obligation.allowed_borrow_value = min(allowed_borrow_value, global_allowed_borrow_value);
obligation.unhealthy_borrow_value = min(unhealthy_borrow_value, global_unhealthy_borrow_value);

obligation.last_update.update_slot(clock.slot);
Obligation::pack(obligation, &mut obligation_info.data.borrow_mut())?;
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
};

/// Percentage of an obligation that can be repaid during each liquidation call
pub const LIQUIDATION_CLOSE_FACTOR: u8 = 20;
pub const LIQUIDATION_CLOSE_FACTOR: u8 = 1;

/// Obligation borrow amount that is small enough to close out
pub const LIQUIDATION_CLOSE_AMOUNT: u64 = 2;
Expand Down
7 changes: 4 additions & 3 deletions token-lending/program/tests/liquidate_obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use solana_sdk::{
use solend_program::{
instruction::{liquidate_obligation, refresh_obligation},
processor::process_instruction,
state::INITIAL_COLLATERAL_RATIO,
state::{INITIAL_COLLATERAL_RATIO, LIQUIDATION_CLOSE_FACTOR},
};
use spl_token::instruction::approve;

Expand All @@ -32,10 +32,11 @@ async fn test_success() {
// 100 SOL * 80% LTV -> 80 SOL * 20 USDC -> 1600 USDC borrow
const USDC_BORROW_AMOUNT_FRACTIONAL: u64 = 1_600 * FRACTIONAL_TO_USDC;
// 1600 USDC * 20% -> 320 USDC liquidation
const USDC_LIQUIDATION_AMOUNT_FRACTIONAL: u64 = USDC_BORROW_AMOUNT_FRACTIONAL / 5;
const USDC_LIQUIDATION_AMOUNT_FRACTIONAL: u64 =
USDC_BORROW_AMOUNT_FRACTIONAL * (LIQUIDATION_CLOSE_FACTOR as u64) / 100;
// 320 USDC / 20 USDC per SOL -> 16 SOL + 10% bonus -> 17.6 SOL (88/5)
const SOL_LIQUIDATION_AMOUNT_LAMPORTS: u64 =
LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO * 88 / 5;
LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO * 88 * (LIQUIDATION_CLOSE_FACTOR as u64) / 100;

const SOL_RESERVE_COLLATERAL_LAMPORTS: u64 = 2 * SOL_DEPOSIT_AMOUNT_LAMPORTS;
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 2 * USDC_BORROW_AMOUNT_FRACTIONAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use solana_sdk::{
use solend_program::{
instruction::{liquidate_obligation_and_redeem_reserve_collateral, refresh_obligation},
processor::process_instruction,
state::INITIAL_COLLATERAL_RATIO,
state::{INITIAL_COLLATERAL_RATIO, LIQUIDATION_CLOSE_FACTOR},
};
use std::cmp::max;

Expand All @@ -32,10 +32,11 @@ async fn test_success() {
// 100 SOL * 80% LTV -> 80 SOL * 20 USDC -> 1600 USDC borrow
const USDC_BORROW_AMOUNT_FRACTIONAL: u64 = 1_600 * FRACTIONAL_TO_USDC;
// 1600 USDC * 20% -> 320 USDC liquidation
const USDC_LIQUIDATION_AMOUNT_FRACTIONAL: u64 = USDC_BORROW_AMOUNT_FRACTIONAL / 5;
const USDC_LIQUIDATION_AMOUNT_FRACTIONAL: u64 =
USDC_BORROW_AMOUNT_FRACTIONAL * (LIQUIDATION_CLOSE_FACTOR as u64) / 100;
// 320 USDC / 20 USDC per SOL -> 16 SOL + 10% bonus -> 17.6 SOL (88/5)
const SOL_LIQUIDATION_AMOUNT_LAMPORTS: u64 =
LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO * 88 / 5;
LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO * 88 * (LIQUIDATION_CLOSE_FACTOR as u64) / 100;

const SOL_RESERVE_COLLATERAL_LAMPORTS: u64 = 2 * SOL_DEPOSIT_AMOUNT_LAMPORTS;
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 2 * USDC_BORROW_AMOUNT_FRACTIONAL;
Expand Down