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

EMode (1) #37

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions contracts/suilend/sources/cell.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ module suilend::cell {
option::borrow(&cell.element)
}

public fun get_mut<Element>(cell: &mut Cell<Element>): &mut Element {
option::borrow_mut(&mut cell.element)
}

public fun destroy<Element>(cell: Cell<Element>): Element {
let Cell { element } = cell;
option::destroy_some(element)
Expand Down
20 changes: 20 additions & 0 deletions contracts/suilend/sources/lending_market.move
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,26 @@ module suilend::lending_market {
coin::from_balance(receive_balance, ctx)
}

/// Set emode for obligation - T is the deposit coin type
public fun set_emode<P>(
lending_market: &mut LendingMarket<P>,
obligation_owner_cap: &ObligationOwnerCap<P>,
clock: &Clock
Copy link
Contributor

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

) {
assert!(lending_market.version == CURRENT_VERSION, EIncorrectVersion);

let obligation = object_table::borrow_mut(
&mut lending_market.obligations,
obligation_owner_cap.obligation_id
);

obligation::set_emode(
obligation,
&mut lending_market.reserves,
clock,
);
}

public fun withdraw_ctokens<P, T>(
lending_market: &mut LendingMarket<P>,
reserve_array_index: u64,
Expand Down
127 changes: 112 additions & 15 deletions contracts/suilend/sources/obligation.move
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,7 @@ module suilend::obligation {
assert!(vector::length(&obligation.borrows) <= 1, EEModeNotValidWithCrossMargin);
assert!(vector::length(&obligation.deposits) <= 1, EEModeNotValidWithCrossMargin);

if (vector::length(&obligation.deposits) == 1) {
let reserve_array_index = vector::borrow(&obligation.deposits, 0).reserve_array_index;
let deposit_reserve = vector::borrow(reserves, reserve_array_index);

assert!(
reserve_config::has_emode_config(config(deposit_reserve)),
ENoEmodeConfigForGivenDepositReserve,
);

refresh(obligation, reserves, clock);

};
refresh(obligation, reserves, clock);

df::add(&mut obligation.id, EModeFlag {}, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to refresh after adding the flag for this to be correct

}
Expand All @@ -206,6 +195,114 @@ module suilend::obligation {
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just return immediately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assigned it to a variable to avoid borrow-checker error

Copy link
Contributor

@0xripleys 0xripleys Nov 8, 2024

Choose a reason for hiding this comment

The 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);

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;
deposited_value_usd = add(deposited_value_usd, market_value);
allowed_borrow_value_usd = add(
allowed_borrow_value_usd,
mul(
market_value_lower_bound,
open_ltv(config(deposit_reserve))
)
);
unhealthy_borrow_value_usd = add(
unhealthy_borrow_value_usd,
mul(
market_value,
close_ltv(config(deposit_reserve))
)
);

i = i + 1;
};

obligation.deposited_value_usd = deposited_value_usd;
obligation.allowed_borrow_value_usd = allowed_borrow_value_usd;
obligation.unhealthy_borrow_value_usd = unhealthy_borrow_value_usd;

let mut i = 0;
let mut unweighted_borrowed_value_usd = decimal::from(0);
let mut weighted_borrowed_value_usd = decimal::from(0);
let mut weighted_borrowed_value_upper_bound_usd = decimal::from(0);
let mut borrowing_isolated_asset = false;

while (i < vector::length(&obligation.borrows)) {
let borrow = vector::borrow_mut(&mut obligation.borrows, i);

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;
unweighted_borrowed_value_usd = add(unweighted_borrowed_value_usd, market_value);
weighted_borrowed_value_usd = add(
weighted_borrowed_value_usd,
mul(
market_value,
borrow_weight(config(borrow_reserve))
)
);
weighted_borrowed_value_upper_bound_usd = add(
weighted_borrowed_value_upper_bound_usd,
mul(
market_value_upper_bound,
borrow_weight(config(borrow_reserve))
)
);

if (isolated(config(borrow_reserve))) {
borrowing_isolated_asset = true;
};

i = i + 1;
};

obligation.unweighted_borrowed_value_usd = unweighted_borrowed_value_usd;
obligation.weighted_borrowed_value_usd = weighted_borrowed_value_usd;
obligation.weighted_borrowed_value_upper_bound_usd = weighted_borrowed_value_upper_bound_usd;

obligation.borrowing_isolated_asset = borrowing_isolated_asset;
};
}

fun refresh_emode<P>(
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
) {
let mut i = 0;
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);

while (i < vector::length(&obligation.deposits)) {
let deposit = vector::borrow_mut(&mut obligation.deposits, i);
Expand Down Expand Up @@ -1293,14 +1390,14 @@ module suilend::obligation {
(open_ltv(config(deposit_reserve)), close_ltv(config(deposit_reserve)))
} else {
// When obligation is emode and there is a borrow
let emode_data = reserve_config::try_get_emode_data(
let emode_entry = reserve_config::try_get_emode_entry(
config(deposit_reserve),
option::borrow(&borrow_reserve_index)
);

if (option::is_some(&emode_data)) {
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_data)), close_ltv_emode(option::borrow(&emode_data)))
(open_ltv_emode(option::borrow(&emode_entry)), close_ltv_emode(option::borrow(&emode_entry)))
} 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)))
Expand Down
25 changes: 0 additions & 25 deletions contracts/suilend/sources/reserve.move
Original file line number Diff line number Diff line change
Expand Up @@ -1031,29 +1031,4 @@ module suilend::reserve {

reserve
}

#[test_only]
public fun set_emode_for_pair<P>(
reserve: &mut Reserve<P>,
reserve_array_index: u64,
open_ltv_pct: u8,
close_ltv_pct: u8,
ctx: &mut TxContext,
) {
let config = cell::get_mut(&mut reserve.config);

let mut builder = reserve_config::from(config, ctx);

let emode_data = reserve_config::create_emode_data(
reserve_array_index,
open_ltv_pct,
close_ltv_pct,
);

reserve_config::set_emode_ltv_for_borrow(&mut builder, emode_data);

reserve_config::destroy(
cell::set(&mut reserve.config, reserve_config::build(builder, ctx))
);
}
}
Loading
Loading