Skip to content

Commit

Permalink
totalDebtTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 20, 2022
1 parent a6a4045 commit 23c4d30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
28 changes: 28 additions & 0 deletions packages/run-protocol/test/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,31 @@ export const metricsTracker = async (t, publicFacet) => {
const metricsSub = await E(publicFacet).getMetrics();
return subscriptionTracker(t, metricsSub);
};

/**
* For vaultManager
*
* @param {import('ava').ExecutionContext} t
* @param {{getMetrics?: () => Subscription<import('../src/vaultFactory/vaultManager').MetricsNotification>}} publicFacet
*/
export const totalDebtTracker = (t, publicFacet) => {
let totalDebtEver = 0n;
/** @param {bigint} delta */
const add = delta => {
totalDebtEver += delta;
};
const assertFullLiquidation = async () => {
const metricsSub = await E(publicFacet).getMetrics();
const metrics = makeNotifierFromAsyncIterable(metricsSub);
const { value: v } = await metrics.getUpdateSince();
const [p, o, s] = [v.totalProceeds, v.totalOverage, v.totalShortfall].map(
a => a.value,
);
t.is(
totalDebtEver,
p - o + s,
`Proceeds - overage + shortfall must equal total debt: ${totalDebtEver} != ${p} - ${o} + ${s}`,
);
};
return harden({ assertFullLiquidation, add });
};
17 changes: 12 additions & 5 deletions packages/run-protocol/test/vaultFactory/test-vaultFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
installGovernance,
} from '../supports.js';
import { unsafeMakeBundleCache } from '../bundleTool.js';
import { metricsTracker } from '../metrics.js';
import { metricsTracker, totalDebtTracker } from '../metrics.js';

/** @type {import('ava').TestInterface<any>} */
const test = unknownTest;
Expand Down Expand Up @@ -2399,7 +2399,7 @@ test('director notifiers', async t => {
t.pass();
});

test('manager notifiers', async t => {
test.only('manager notifiers', async t => {
const LOAN1 = 450n;
const DEBT1 = 473n; // with penalty
const LOAN2 = 50n;
Expand All @@ -2421,13 +2421,17 @@ test('manager notifiers', async t => {
const cm = await E(aethVaultManager).getPublicFacet();

const m = await metricsTracker(t, cm);
const td = totalDebtTracker(t, cm);

// 0. Creation
await m.assertInitial({
numLiquidations: 0,
// present
numVaults: 0,
totalCollateral: collAmount(0),
totalDebt: debtAmount(0),

// running
numLiquidations: 0,
totalOverage: debtAmount(0),
totalProceeds: debtAmount(0),
totalReclaimed: collAmount(0),
Expand All @@ -2448,6 +2452,7 @@ test('manager notifiers', async t => {
}),
);
const { vault: vault1 } = await E(vaultSeat).getOfferResult();
td.add(DEBT1);
await m.assertChange({
numVaults: 1,
totalCollateral: { value: collateralAmount.value },
Expand Down Expand Up @@ -2483,6 +2488,7 @@ test('manager notifiers', async t => {
numLiquidations: 1,
totalProceeds: { value: 613n },
});
await td.assertFullLiquidation();

// 3. Make another LOAN1 loan
vaultSeat = await E(services.zoe).offer(
Expand All @@ -2501,6 +2507,7 @@ test('manager notifiers', async t => {
totalCollateral: { value: collateralAmount.value },
totalDebt: { value: DEBT1 },
});
td.add(DEBT1);

// 4. Make a LOAN2 loan
vaultSeat = await E(services.zoe).offer(
Expand All @@ -2521,6 +2528,7 @@ test('manager notifiers', async t => {
totalCollateral: { value: AMPLE + ENOUGH },
totalDebt: { value: DEBT1 + DEBT2 },
});
td.add(DEBT2);

// 5. Liquidate all (2 loans)
await E(aethVaultManager).liquidateAll();
Expand All @@ -2539,6 +2547,5 @@ test('manager notifiers', async t => {
totalCollateral: { value: 0n },
totalProceeds: { value: 1528n },
});

t.pass(); // bc Ava doesn't detect the custom assertions above
await td.assertFullLiquidation();
});

0 comments on commit 23c4d30

Please sign in to comment.