Skip to content

Commit

Permalink
fix(vaultFactory): don't prevent repayment based on DebtLimit (#9907)
Browse files Browse the repository at this point in the history
closes: #9837

## Description

DebtLimit should not impact a vault owner's ability to repay, so refine `checkDebtLimit` to succeed regardless of `debtLimit` if `toMint` is empty.

### Security / Scaling Considerations

none

### Testing Considerations

straightforward unit test that failed before the fix

### Upgrade / Documentation Considerations

Straightforward upgrade; no state changes. See:
 - #9887

This fix should be cited in release notes.
  • Loading branch information
mergify[bot] authored Aug 19, 2024
2 parents 46e77e4 + 1c94ac8 commit c4520c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/inter-protocol/src/contractSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ export const allEmpty = amounts => {
* @param {Amount<'nat'>} totalDebt
* @param {Amount<'nat'>} toMint
* @throws if minting would exceed total debt
*
* Note: Succeeds regardless of debtLimit if toMint is empty.
*/
export const checkDebtLimit = (debtLimit, totalDebt, toMint) => {
if (AmountMath.isEmpty(toMint)) {
return;
}
const debtPost = AmountMath.add(totalDebt, toMint);
AmountMath.isGTE(debtLimit, debtPost) ||
Fail`Minting ${q(toMint)} past ${q(
Expand Down
5 changes: 5 additions & 0 deletions packages/inter-protocol/test/contractSupport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ test('checkDebtLimit allows at limit', t => {
test('checkDebtLimit throws above', t => {
t.throws(() => checkDebtLimit(limit, prior, debt.make(3n)));
});

test('checkDebtLimit always succeeds if there is nothing to mint', t => {
const bigPrior = debt.make(5n);
t.notThrows(() => checkDebtLimit(limit, bigPrior, debt.make(0n)));
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ test('excessive loan', async t => {
);
});

test('repay works regardless of debtLimit', async t => {
const { aeth, run } = t.context;
const md = await makeManagerDriver(t);

// take debt that comes just shy of max
const vd = await md.makeVaultDriver(aeth.make(1000n), run.make(4500n));
t.deepEqual(await E(vd.vault()).getCurrentDebt(), run.make(4725n));

const MARGIN_HOP = 20n;

// we can take a loan and pay it back
await vd.giveCollateral(100n, aeth, MARGIN_HOP);
await vd.giveMinted(MARGIN_HOP, aeth, 100n);

// EC lowers mint limit
await md.setGovernedParam('DebtLimit', run.make(1000n), {
key: { collateralBrand: aeth.brand },
});
// we can still repay debt
await vd.giveMinted(MARGIN_HOP, aeth);
});

test('add debt to vault under LiquidationMarging + LiquidationPadding', async t => {
const { aeth, run } = t.context;
const md = await makeManagerDriver(t);
Expand Down

0 comments on commit c4520c5

Please sign in to comment.