Skip to content

Commit b431916

Browse files
committedMay 24, 2022
feat(liquidation): push penalties to reserve
1 parent f803755 commit b431916

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed
 

‎packages/run-protocol/src/vaultFactory/liquidateIncrementally.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,26 @@ const start = async zcf => {
8080
const asFloat = (numerator, denominator) =>
8181
Number(numerator) / Number(denominator);
8282

83-
// TODO(5467)) distribute penalties to the reserve
84-
assert(reservePublicFacet, 'Missing reservePublicFacet');
83+
// #region penalty distribution
8584
const { zcfSeat: penaltyPoolSeat } = zcf.makeEmptySeatKit();
85+
const drainPenaltyPool = async () => {
86+
const addCollateral = await E(
87+
reservePublicFacet,
88+
).makeAddCollateralInvitation();
89+
const proposal = harden({
90+
give: { Collateral: penaltyPoolSeat.getCurrentAllocation().Out },
91+
});
92+
const { deposited, userSeatPromise } = await offerTo(
93+
zcf,
94+
addCollateral,
95+
harden({ Out: 'Collateral' }),
96+
proposal,
97+
penaltyPoolSeat,
98+
);
99+
const [deposits] = await Promise.all([deposited, userSeatPromise]);
100+
trace('drainPenaltyPool deposited', deposits.Out);
101+
};
102+
// #endregion
86103

87104
/**
88105
* Compute the tranche size whose sale on the AMM would have
@@ -312,6 +329,10 @@ const start = async zcf => {
312329

313330
debtorSeat.exit();
314331
trace('exit seat');
332+
// trigger but don't await so it doesn't make liquidation itself fail
333+
drainPenaltyPool().catch(e =>
334+
console.error('🚨 error draining penalty pool', e),
335+
);
315336
};
316337

317338
/**

‎packages/run-protocol/test/bundleTool.js

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ export const makeBundleCache = (wr, cwd, readPowers, opts) => {
174174
};
175175

176176
const loaded = new Map();
177+
/**
178+
* @param {string} rootPath
179+
* @param {string} targetName
180+
* @param {(message: *) => void} [log]
181+
*/
177182
const load = async (rootPath, targetName, log = console.debug) => {
178183
const found = loaded.get(targetName);
179184
// console.log('load', { targetName, found: !!found, rootPath });

‎packages/run-protocol/test/vaultFactory/test-liquidator.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ import {
3434
} from '../supports.js';
3535
import { unsafeMakeBundleCache } from '../bundleTool.js';
3636

37-
/** @type {import('ava').TestInterface<any>} */
37+
/** @typedef {Record<string, any> & {
38+
* aethKit: IssuerKit,
39+
* reserveCreatorFacet: AssetReserveCreatorFacet,
40+
* runKit: IssuerKit,
41+
* }} Context */
42+
/** @type {import('ava').TestInterface<Context>} */
43+
// @ts-expect-error cast
3844
const test = unknownTest;
3945

4046
// #region Support
@@ -45,6 +51,7 @@ const contractRoots = {
4551
liquidate: './src/vaultFactory/liquidateIncrementally.js',
4652
VaultFactory: './src/vaultFactory/vaultFactory.js',
4753
amm: './src/vpool-xyk-amm/multipoolMarketMaker.js',
54+
reserve: './src/reserve/assetReserve.js',
4855
};
4956

5057
/** @typedef {import('../../src/vaultFactory/vaultFactory').VaultFactoryContract} VFC */
@@ -285,6 +292,7 @@ const setupServices = async (
285292
iProduce.reserve.resolve(t.context.installation.reserve);
286293
// produce the reserve instance in the space
287294
await setupReserve(space);
295+
t.context.reserveCreatorFacet = space.consume.reserveCreatorFacet;
288296
iProduce.VaultFactory.resolve(t.context.installation.VaultFactory);
289297
iProduce.liquidate.resolve(t.context.installation.liquidate);
290298
await startVaultFactory(space, { loanParams: loanTiming }, minInitialDebt);
@@ -818,3 +826,30 @@ test('amm stopAfter - want too much', async t => {
818826
Out: expectedRUN,
819827
});
820828
});
829+
830+
test('penalties to reserve', async t => {
831+
const {
832+
aethKit: { brand: aethBrand },
833+
runKit: { brand: runBrand },
834+
} = t.context;
835+
836+
const d = await makeDriver(
837+
t,
838+
AmountMath.make(runBrand, 1000n),
839+
AmountMath.make(aethBrand, 900n),
840+
);
841+
// Create a loan for 270 RUN with 400 aeth collateral
842+
const collateralAmount = AmountMath.make(aethBrand, 400n);
843+
const loanAmount = AmountMath.make(runBrand, 270n);
844+
await d.makeVaultDriver(collateralAmount, loanAmount);
845+
846+
// liquidate
847+
d.setPrice(AmountMath.make(runBrand, 636n));
848+
await waitForPromisesToSettle();
849+
850+
const { reserveCreatorFacet } = t.context;
851+
const reserveAllocations = await E(reserveCreatorFacet).getAllocations();
852+
t.deepEqual(reserveAllocations, {
853+
RUN: { brand: runBrand, value: 29n },
854+
});
855+
});

0 commit comments

Comments
 (0)
Please sign in to comment.