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

fix(vaults): when reconstituting vaults, report correct shortfall #7752

Merged
merged 2 commits into from
May 16, 2023
Merged
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
80 changes: 47 additions & 33 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,7 +719,11 @@ export const prepareVaultManagerKit = (
vaultData.getSize(),
);

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

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

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

Expand All @@ -801,13 +813,13 @@ export const prepareVaultManagerKit = (
: AmountMath.makeEmptyFromAmount(collateralProceeds);

let collatRemaining = distributableCollateral;
let debtRemaining = totalDebt;
/** @type {import('@agoric/zoe/src/contractSupport/atomicTransfer.js').TransferPart[]} */
const transfers = [];
let liquidated = 0;
/** @type {MapStore<string, Vault>} */
const vaultsToReinstate = makeScalarMapStore();
let collateralReduction = AmountMath.makeEmpty(collateralBrand);
let shortfallToReserve = accounting.shortfall;

const reduceCollateral = amount =>
(collateralReduction = AmountMath.add(
Expand All @@ -825,13 +837,16 @@ export const prepareVaultManagerKit = (
if (
reconstituteVaults &&
AmountMath.isGTE(collatRemaining, collatPostDebt) &&
AmountMath.isGTE(debtRemaining, debtAmount)
AmountMath.isGTE(totalDebt, debtAmount)
) {
collatRemaining = AmountMath.subtract(
collatRemaining,
collatPostDebt,
);
debtRemaining = AmountMath.subtract(debtRemaining, debtAmount);
shortfallToReserve = AmountMath.subtract(
shortfallToReserve,
debtAmount,
);
const seat = vault.getVaultSeat();
const vaultId = vault.abortLiquidation();
liquidatingVaults.delete(vault);
Expand Down Expand Up @@ -869,13 +884,12 @@ export const prepareVaultManagerKit = (
transfers.length,
);

facets.helper.markRestoreDebt(
AmountMath.subtract(totalDebt, debtRemaining),
);
facets.helper.sendToReserve(collatRemaining, liqSeat);
facets.helper.markDoneLiquidating(totalDebt, totalCollateral);
facets.helper.markDoneLiquidating(totalDebt, totalCollateral, {
...accounting,
shortfall: shortfallToReserve,
});
}
facets.helper.recordShortfallAndProceeds(accounting);
return facets.helper.updateMetrics();
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ test('reinstate vault', async t => {
totalDebt: { value: 158n },
totalCollateral: { value: 44n },
totalProceedsReceived: { value: 34n },
totalShortfallReceived: { value: 224n },
totalShortfallReceived: { value: 66n },
totalCollateralSold: { value: 8n },
numLiquidatingVaults: 0,
numLiquidationsCompleted: 1,
Expand Down