Skip to content

Commit

Permalink
refactor(vaultManager): inline recordShortfallAndProceeds
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 16, 2023
1 parent 3470475 commit aa76acf
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions packages/inter-protocol/src/vaultFactory/vaultManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,28 +496,6 @@ export const prepareVaultManagerKit = (
proceeds,
);
},

/** @type {(accounting: { overage: Amount<'nat'>, shortfall: Amount<'nat'> }) => void} */
recordShortfallAndProceeds(accounting) {
trace('recordShortfallAndProceeds', accounting);
const { state } = this;

const { overage, shortfall } = accounting;
// cumulative values
state.totalOverageReceived = AmountMath.add(
state.totalOverageReceived,
overage,
);
state.totalShortfallReceived = AmountMath.add(
state.totalShortfallReceived,
shortfall,
);
state.totalDebt = AmountMath.subtract(state.totalDebt, shortfall);

void E.when(factoryPowers.getShortfallReporter(), reporter =>
E(reporter).increaseLiquidationShortfall(shortfall),
);
},
sendToReserve(penalty, seat, seatKeyword = 'Collateral') {
const invitation =
E(reservePublicFacet).makeAddCollateralInvitation();
Expand Down Expand Up @@ -555,9 +533,17 @@ export const prepareVaultManagerKit = (

state.liquidatingDebt = AmountMath.add(state.liquidatingDebt, debt);
},
markDoneLiquidating(debt, collateral) {
/**
*
* @param {Amount<'nat'>} debt
* @param {Amount<'nat'>} collateral
* @param {{ overage: Amount<'nat'>, shortfall: Amount<'nat'> }} accounting
*/
markDoneLiquidating(debt, collateral, accounting) {
const { state } = this;

// update liquidation state

state.liquidatingCollateral = AmountMath.subtract(
state.liquidatingCollateral,
collateral,
Expand All @@ -566,6 +552,24 @@ export const prepareVaultManagerKit = (
state.liquidatingDebt,
debt,
);

// record shortfall and proceeds

const { overage, shortfall } = accounting;
// cumulative values
state.totalOverageReceived = AmountMath.add(
state.totalOverageReceived,
overage,
);
state.totalShortfallReceived = AmountMath.add(
state.totalShortfallReceived,
shortfall,
);
state.totalDebt = AmountMath.subtract(state.totalDebt, shortfall);

void E.when(factoryPowers.getShortfallReporter(), reporter =>
E(reporter).increaseLiquidationShortfall(shortfall),
);
},
/**
* If interest was charged between liquidating and liquidated, erase it.
Expand Down Expand Up @@ -715,8 +719,11 @@ export const prepareVaultManagerKit = (
vaultData.getSize(),
);

facets.helper.recordShortfallAndProceeds(accounting);
facets.helper.markDoneLiquidating(totalDebt, totalCollateral);
facets.helper.markDoneLiquidating(
totalDebt,
totalCollateral,
accounting,
);
} else if (AmountMath.isEmpty(collateralProceeds)) {
// Flow #2a

Expand Down Expand Up @@ -783,8 +790,11 @@ export const prepareVaultManagerKit = (
vaultData.getSize(),
);

facets.helper.recordShortfallAndProceeds(accounting);
facets.helper.markDoneLiquidating(totalDebt, totalCollateral);
facets.helper.markDoneLiquidating(
totalDebt,
totalCollateral,
accounting,
);
} else {
// Flow #2b: There's unsold collateral; some vaults may be revived.

Expand Down Expand Up @@ -875,9 +885,8 @@ export const prepareVaultManagerKit = (
);

facets.helper.sendToReserve(collatRemaining, liqSeat);
facets.helper.markDoneLiquidating(totalDebt, totalCollateral);
facets.helper.recordShortfallAndProceeds({
overage: accounting.overage,
facets.helper.markDoneLiquidating(totalDebt, totalCollateral, {
...accounting,
shortfall: shortfallToReserve,
});
}
Expand Down

0 comments on commit aa76acf

Please sign in to comment.