-
Notifications
You must be signed in to change notification settings - Fork 7
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
EMode (1) #37
base: devel
Are you sure you want to change the base?
EMode (1) #37
Changes from 5 commits
9d8275b
9950159
6dad8d5
d7cae35
09bb04d
7d592c1
881f350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,18 @@ module suilend::obligation { | |
use sui::tx_context::{TxContext}; | ||
use suilend::reserve::{Self, Reserve, config}; | ||
use suilend::reserve_config::{ | ||
Self, | ||
open_ltv, | ||
close_ltv, | ||
borrow_weight, | ||
liquidation_bonus, | ||
protocol_liquidation_fee, | ||
isolated, | ||
open_ltv_emode, | ||
close_ltv_emode, | ||
}; | ||
use sui::clock::{Clock}; | ||
use sui::dynamic_field::{Self as df}; | ||
use suilend::decimal::{Self, Decimal, mul, add, sub, div, gt, lt, min, floor, le, eq, saturating_sub}; | ||
use suilend::liquidity_mining::{Self, UserRewardManager, PoolRewardManager}; | ||
|
||
|
@@ -29,12 +33,20 @@ module suilend::obligation { | |
const ETooManyBorrows: u64 = 6; | ||
const EObligationIsNotForgivable: u64 = 7; | ||
const ECannotDepositAndBorrowSameAsset: u64 = 8; | ||
const EEModeNotValidWithCrossMargin: u64 = 9; | ||
const ENoEmodeConfigForGivenDepositReserve : u64 = 10; | ||
const EInvalidEModeDeposit: u64 = 13; | ||
const EInvalidEModeBorrow: u64 = 14; | ||
const EEModeAlreadySet: u64 = 15; | ||
|
||
// === Constants === | ||
const CLOSE_FACTOR_PCT: u8 = 20; | ||
const MAX_DEPOSITS: u64 = 5; | ||
const MAX_BORROWS: u64 = 5; | ||
|
||
// === Dynamic Field Keys === | ||
public struct EModeFlag has store, copy, drop {} | ||
|
||
// === public structs === | ||
public struct Obligation<phantom P> has key, store { | ||
id: UID, | ||
|
@@ -157,6 +169,21 @@ module suilend::obligation { | |
} | ||
} | ||
|
||
public(package) fun toggle_emode<P>( | ||
obligation: &mut Obligation<P>, | ||
reserves: &mut vector<Reserve<P>>, | ||
clock: &Clock, | ||
) { | ||
if (is_emode(obligation)) { | ||
df::remove<EModeFlag, bool>(&mut obligation.id, EModeFlag {}); | ||
} else { | ||
assert!(vector::length(&obligation.borrows) <= 1, EEModeNotValidWithCrossMargin); | ||
assert!(vector::length(&obligation.deposits) <= 1, EEModeNotValidWithCrossMargin); | ||
df::add(&mut obligation.id, EModeFlag {}, true); | ||
}; | ||
|
||
refresh(obligation, reserves, clock); | ||
} | ||
|
||
/// update the obligation's borrowed amounts and health values. this is | ||
/// called by the lending market prior to any borrow, withdraw, or liquidate operation. | ||
|
@@ -169,7 +196,10 @@ module suilend::obligation { | |
let mut deposited_value_usd = decimal::from(0); | ||
let mut allowed_borrow_value_usd = decimal::from(0); | ||
let mut unhealthy_borrow_value_usd = decimal::from(0); | ||
|
||
let is_emode = is_emode(obligation); | ||
0xLendlord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (is_emode) { | ||
obligation.refresh_emode(reserves, is_emode, clock) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: just return immediately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assigned it to a variable to avoid borrow-checker error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (is_emode) {
obligation.refresh_emode(reserves, is_emode, clock)
return;
} |
||
} else { | ||
while (i < vector::length(&obligation.deposits)) { | ||
let deposit = vector::borrow_mut(&mut obligation.deposits, i); | ||
|
||
|
@@ -261,6 +291,99 @@ module suilend::obligation { | |
obligation.weighted_borrowed_value_upper_bound_usd = weighted_borrowed_value_upper_bound_usd; | ||
|
||
obligation.borrowing_isolated_asset = borrowing_isolated_asset; | ||
}; | ||
} | ||
|
||
fun refresh_emode<P>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think you can simplify this function to not use a while loops at all, and the variables on 303-305 aren't necessary either because the while loop will only iterate at most 1 time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplified |
||
obligation: &mut Obligation<P>, | ||
reserves: &mut vector<Reserve<P>>, | ||
is_emode: bool, | ||
clock: &Clock | ||
) { | ||
// Deposit | ||
|
||
if (obligation.deposits.length() == 0) { | ||
return | ||
}; | ||
|
||
assert!(vector::length(&obligation.deposits) == 1, 0); | ||
|
||
let deposit = obligation.deposits.borrow_mut(0); | ||
let deposit_reserve = vector::borrow_mut(reserves, deposit.reserve_array_index); | ||
|
||
reserve::compound_interest(deposit_reserve, clock); | ||
reserve::assert_price_is_fresh(deposit_reserve, clock); | ||
|
||
let market_value = reserve::ctoken_market_value( | ||
deposit_reserve, | ||
deposit.deposited_ctoken_amount | ||
); | ||
let market_value_lower_bound = reserve::ctoken_market_value_lower_bound( | ||
deposit_reserve, | ||
deposit.deposited_ctoken_amount | ||
); | ||
|
||
deposit.market_value = market_value; | ||
obligation.deposited_value_usd = market_value; | ||
|
||
let (open_ltv, close_ltv, is_emode_ltvs) = get_ltvs( | ||
obligation, | ||
deposit_reserve, | ||
is_emode, | ||
); | ||
|
||
obligation.allowed_borrow_value_usd = mul( | ||
market_value_lower_bound, | ||
open_ltv, | ||
); | ||
|
||
obligation.unhealthy_borrow_value_usd = mul( | ||
market_value, | ||
close_ltv, | ||
); | ||
|
||
// Borrow | ||
|
||
if (obligation.borrows.length() == 0) { | ||
return | ||
}; | ||
|
||
assert!(vector::length(&obligation.borrows) == 1, 0); | ||
|
||
let borrow = obligation.borrows.borrow_mut(0); | ||
|
||
let borrow_reserve = vector::borrow_mut(reserves, borrow.reserve_array_index); | ||
reserve::compound_interest(borrow_reserve, clock); | ||
reserve::assert_price_is_fresh(borrow_reserve, clock); | ||
|
||
compound_debt(borrow, borrow_reserve); | ||
|
||
let market_value = reserve::market_value(borrow_reserve, borrow.borrowed_amount); | ||
let market_value_upper_bound = reserve::market_value_upper_bound( | ||
borrow_reserve, | ||
borrow.borrowed_amount | ||
); | ||
|
||
borrow.market_value = market_value; | ||
obligation.unweighted_borrowed_value_usd = market_value; | ||
|
||
let borrow_weight = if (is_emode_ltvs) { | ||
decimal::from(1) | ||
} else { | ||
borrow_weight(config(borrow_reserve)) | ||
}; | ||
|
||
obligation.weighted_borrowed_value_usd = mul( | ||
market_value, | ||
borrow_weight | ||
); | ||
|
||
obligation.weighted_borrowed_value_upper_bound_usd = mul( | ||
market_value_upper_bound, | ||
borrow_weight | ||
); | ||
|
||
obligation.borrowing_isolated_asset = isolated(config(borrow_reserve)); | ||
} | ||
|
||
/// Process a deposit action | ||
|
@@ -1221,4 +1344,50 @@ module suilend::obligation { | |
user_reward_manager_index | ||
} | ||
} | ||
} | ||
|
||
fun get_ltvs<P>( | ||
0xripleys marked this conversation as resolved.
Show resolved
Hide resolved
|
||
obligation: &Obligation<P>, | ||
deposit_reserve: &Reserve<P>, | ||
is_emode: bool, | ||
): (Decimal, Decimal, bool) { | ||
if (!is_emode) { | ||
// When obligation normal mode | ||
return (open_ltv(config(deposit_reserve)), close_ltv(config(deposit_reserve)), false) | ||
}; | ||
|
||
let borrow_reserve_index = get_single_borrow_array_reserve_if_any(obligation); | ||
if (borrow_reserve_index.is_none()) { | ||
// When obligation is emode but there is no borrows | ||
return (open_ltv(config(deposit_reserve)), close_ltv(config(deposit_reserve)), false) | ||
}; | ||
|
||
// When obligation is emode and there is a borrow | ||
let emode_entry = reserve_config::try_get_emode_entry( | ||
config(deposit_reserve), | ||
option::borrow(&borrow_reserve_index) | ||
); | ||
|
||
if (option::is_some(&emode_entry)) { | ||
// When obligation is emode and there is a borrow AND there is a matching emode config | ||
(open_ltv_emode(option::borrow(&emode_entry)), close_ltv_emode(option::borrow(&emode_entry)), true) | ||
} else { | ||
// When obligation is emode and there is a borrow BUT there is no matching emode config | ||
(open_ltv(config(deposit_reserve)), close_ltv(config(deposit_reserve)), false) | ||
} | ||
} | ||
|
||
public(package) fun is_emode<P>(obligation: &Obligation<P>): bool { | ||
df::exists_(&obligation.id, EModeFlag {}) | ||
} | ||
|
||
fun get_single_borrow_array_reserve_if_any<P>( | ||
obligation: &Obligation<P>, | ||
): Option<u64> { | ||
let len = vector::length(&obligation.borrows); | ||
if (len != 1) { | ||
option::none() | ||
} else { | ||
option::some(vector::borrow(&obligation.borrows, 0).reserve_array_index) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ability to toggle emode is still missing