Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ public(package) fun increase_supply_absolute(self: &mut State, amount: u64) {
}

/// Decrease the supply given an absolute amount. Used when the supply needs to be
/// decreased without decreasing shares.
/// decreased without decreasing shares. Clamped to `total_supply`: any excess
/// represents bad debt that exceeds the supplier pool's absorptive capacity and
/// is uncollectible by design (was never backed by vault cash).
public(package) fun decrease_supply_absolute(self: &mut State, amount: u64) {
self.total_supply = self.total_supply - amount;
let decrease = amount.min(self.total_supply);
self.total_supply = self.total_supply - decrease;
}

/// Increase the borrow given an amount. Return the individual borrow shares
Expand Down
19 changes: 19 additions & 0 deletions packages/deepbook_margin/tests/margin_pool/margin_state_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ fun margin_state_operations_work() {
test.end();
}

#[test]
fun decrease_supply_absolute_clamps_at_zero() {
let mut test = begin(test_constants::admin());
let clock = clock::create_for_testing(test.ctx());
let mut state = margin_state::default(&clock);

state.increase_supply_absolute(500);
assert_eq!(state.total_supply(), 500);

state.decrease_supply_absolute(600);
assert_eq!(state.total_supply(), 0);

state.decrease_supply_absolute(100);
assert_eq!(state.total_supply(), 0);

destroy(clock);
test.end();
}

#[test]
fun margin_state_with_supply_and_borrow_accrues_interest() {
let mut test = begin(test_constants::admin());
Expand Down
Loading