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: 4 additions & 0 deletions contracts/suilend/sources/cell.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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
121 changes: 113 additions & 8 deletions contracts/suilend/sources/obligation.move
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand Down Expand Up @@ -157,6 +169,30 @@ module suilend::obligation {
}
}

public(package) fun set_emode<P>(
obligation: &mut Obligation<P>,
reserves: &mut vector<Reserve<P>>,
clock: &Clock,
) {
assert!(!is_emode(obligation), EEModeAlreadySet);
assert!(vector::length(&obligation.borrows) <= 1, EEModeNotValidWithCrossMargin);
assert!(vector::length(&obligation.deposits) <= 1, EEModeNotValidWithCrossMargin);

if (vector::length(&obligation.deposits) == 1) {
0xLendlord marked this conversation as resolved.
Show resolved Hide resolved
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);

};

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

}

/// update the obligation's borrowed amounts and health values. this is
/// called by the lending market prior to any borrow, withdraw, or liquidate operation.
Expand All @@ -169,6 +205,7 @@ 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

while (i < vector::length(&obligation.deposits)) {
let deposit = vector::borrow_mut(&mut obligation.deposits, i);
Expand All @@ -189,19 +226,27 @@ module suilend::obligation {

deposit.market_value = market_value;
deposited_value_usd = add(deposited_value_usd, market_value);

let (open_ltv, close_ltv) = get_ltvs(
obligation,
deposit_reserve,
is_emode,
);

allowed_borrow_value_usd = add(
allowed_borrow_value_usd,
mul(
market_value_lower_bound,
open_ltv(config(deposit_reserve))
)
open_ltv,
),
);

unhealthy_borrow_value_usd = add(
unhealthy_borrow_value_usd,
mul(
market_value,
close_ltv(config(deposit_reserve))
)
close_ltv,
),
);

i = i + 1;
Expand Down Expand Up @@ -230,22 +275,29 @@ module suilend::obligation {
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);

let borrow_weight = if (is_emode) {
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 even if obligation is emode, if there's no corresponding emode entry, then the borrow weight has to be the one stored on the reserve (ie not 1).

example: say user is depositing usdc and borrowing FUD. fud has a borrow weight of 2. the (usdc, fud) pair does not have a corresponding emode entry. then, if the user sets their obligation to emode, then the borrow weight being used is 1, which lets them borrow more fud than we intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Refactored

decimal::from(1)
} else {
borrow_weight(config(borrow_reserve))
};

weighted_borrowed_value_usd = add(
weighted_borrowed_value_usd,
mul(
market_value,
borrow_weight(config(borrow_reserve))
borrow_weight
)
);
weighted_borrowed_value_upper_bound_usd = add(
weighted_borrowed_value_upper_bound_usd,
mul(
market_value_upper_bound,
borrow_weight(config(borrow_reserve))
borrow_weight
)
);

Expand Down Expand Up @@ -1221,4 +1273,57 @@ 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) {
if (is_emode) {
// check_normal_ltv_against_emode_ltvs
let borrow_reserve_index = get_single_borrow_array_reserve_if_any(obligation);

assert!(
Copy link
Contributor

Choose a reason for hiding this comment

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

we never want this assert to get tripped because then obligation::refresh can abort, which would be very bad.

Copy link
Contributor

Choose a reason for hiding this comment

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

in this case we still want to return the base ltvs, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's right! Refactored

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

if (option::is_none(&borrow_reserve_index)) {
// When obligation is emode but there is no borrows
(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(
config(deposit_reserve),
option::borrow(&borrow_reserve_index)
);

if (option::is_some(&emode_data)) {
// 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)))
} 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)))
}
}
} else {
// When obligation normal mode
(open_ltv(config(deposit_reserve)), close_ltv(config(deposit_reserve)))
}
}

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)
}
}
}
25 changes: 25 additions & 0 deletions contracts/suilend/sources/reserve.move
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,29 @@ 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);
0xLendlord marked this conversation as resolved.
Show resolved Hide resolved

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