Skip to content

Commit

Permalink
chore(liquidationVisibility): #4 improve testing tools
Browse files Browse the repository at this point in the history
  • Loading branch information
anilhelvaci committed Jan 22, 2024
1 parent 5dfa289 commit bbd2e48
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 34 deletions.
12 changes: 4 additions & 8 deletions packages/inter-protocol/test/liquidationVisibility/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,21 @@ export const assertBidderPayout = async (
));
};

export const assertReserveState = async (t, metricsTopic, method, expected) => {
const m = await subscriptionTracker(t, metricsTopic);

export const assertReserveState = async (metricTracker, method, expected) => {
switch (method) {
case 'initial':
await m.assertInitial(expected);
await metricTracker.assertInitial(expected);
break;
case 'like':
await m.assertLike(expected);
await metricTracker.assertLike(expected);
break;
case 'state':
await m.assertState(expected);
await metricTracker.assertState(expected);
break;
default:
console.log('Default');
break;
}

return m;
};

export const assertVaultCurrentDebt = async (t, vault, debt) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
startAuctionClock,
getDataFromVstorage,
openVault,
} from './tools.js';
getMetricTrackers, adjustVault
} from "./tools.js";
import {
assertBidderPayout,
assertCollateralProceeds,
Expand Down Expand Up @@ -235,21 +236,20 @@ test('Auction sells all collateral w/shortfall', async t => {
} = services;
await E(reserveCreatorFacet).addIssuer(aeth.issuer, 'Aeth');

const metricsTopic = await E.get(E(reservePublicFacet).getPublicTopics())
.metrics;
const m = await assertReserveState(
const { reserveTracker, collateralManagerTracker } = await getMetricTrackers({
t,
metricsTopic,
collateralManager: aethCollateralManager,
reservePublicFacet,
});

await assertReserveState(
reserveTracker,
'initial',
reserveInitialState(run.makeEmpty()),
);
let shortfallBalance = 0n;

const aethVaultMetrics = await vaultManagerMetricsTracker(
t,
aethCollateralManager,
);
await aethVaultMetrics.assertInitial({
await collateralManagerTracker.assertInitial({
// present
numActiveVaults: 0,
numLiquidatingVaults: 0,
Expand Down Expand Up @@ -293,7 +293,7 @@ test('Auction sells all collateral w/shortfall', async t => {
await assertVaultDebtSnapshot(t, aliceNotifier, aliceWantMinted);

let totalDebt = 5250n;
await aethVaultMetrics.assertChange({
await collateralManagerTracker.assertChange({
numActiveVaults: 1,
totalCollateral: { value: 1000n },
totalDebt: { value: totalDebt },
Expand All @@ -306,20 +306,21 @@ test('Auction sells all collateral w/shortfall', async t => {
// Alice reduce collateral by 300. That leaves her at 700 * 10 > 1.05 * 5000.
// Prices will drop from 10 to 7, she'll be liquidated: 700 * 7 < 1.05 * 5000.
const collateralDecrement = aeth.make(300n);
const aliceReduceCollateralSeat = await E(zoe).offer(
E(aliceVault).makeAdjustBalancesInvitation(),
harden({
const aliceReduceCollateralSeat = await adjustVault({
t,
vault: aliceVault,
proposal: {
want: { Collateral: collateralDecrement },
}),
);
},
});
await E(aliceReduceCollateralSeat).getOfferResult();

trace('alice ');
await assertCollateralProceeds(t, aliceReduceCollateralSeat, aeth.make(300n));

await assertVaultDebtSnapshot(t, aliceNotifier, aliceWantMinted);
trace(t, 'alice reduce collateral');
await aethVaultMetrics.assertChange({
await collateralManagerTracker.assertChange({
totalCollateral: { value: 700n },
});

Expand All @@ -339,7 +340,7 @@ test('Auction sells all collateral w/shortfall', async t => {
);
let currentTime = now1;

await aethVaultMetrics.assertChange({
await collateralManagerTracker.assertChange({
lockedQuote: makeRatioFromAmounts(
aeth.make(1_000_000n),
run.make(7_000_000n),
Expand All @@ -353,8 +354,7 @@ test('Auction sells all collateral w/shortfall', async t => {

await assertVaultState(t, aliceNotifier, Phase.LIQUIDATED);
trace(t, 'alice liquidated', currentTime);
totalDebt += 30n;
await aethVaultMetrics.assertChange({
await collateralManagerTracker.assertChange({
numActiveVaults: 0,
numLiquidatingVaults: 1,
liquidatingCollateral: { value: 700n },
Expand All @@ -363,11 +363,11 @@ test('Auction sells all collateral w/shortfall', async t => {
});

shortfallBalance += 2065n;
await m.assertChange({
await reserveTracker.assertChange({
shortfallBalance: { value: shortfallBalance },
});

await aethVaultMetrics.assertChange({
await collateralManagerTracker.assertChange({
liquidatingDebt: { value: 0n },
liquidatingCollateral: { value: 0n },
totalCollateral: { value: 0n },
Expand Down
55 changes: 51 additions & 4 deletions packages/inter-protocol/test/liquidationVisibility/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { TimeMath } from '@agoric/time';
import { subscribeEach } from '@agoric/notifier';
import '../../src/vaultFactory/types.js';
import exp from 'constants';
import { withAmountUtils } from '../supports.js';
import {
getRunFromFaucet,
Expand Down Expand Up @@ -285,6 +286,52 @@ export const openVault = async ({
);
};

/**
* @typedef {object} AdjustVaultParams
* @property {object} t
* @property {Vault} vault
* @property {{
* want: [
* {
* Collateral: Amount<'nat'>;
* Minted: Amount<'nat'>;
* },
* ];
* give: [
* {
* Collateral: Amount<'nat'>;
* Minted: Amount<'nat'>;
* },
* ];
* }} proposal
* @property {{
* want: [
* {
* Collateral: Payment;
* Minted: Payment;
* },
* ];
* give: [
* {
* Collateral: Payment;
* Minted: Payment;
* },
* ];
* }} [payment]
*/

/**
* @param {AdjustVaultParams} adjustVaultParams
* @returns {Promise<UserSeat>}
*/
export const adjustVault = async ({ t, vault, proposal, payment }) => {
return E(t.context.zoe).offer(
E(vault).makeAdjustBalancesInvitation(),
harden(proposal),
payment,
);
};

/**
* @typedef {object} GetTrackerParams
* @property {any} t
Expand All @@ -294,8 +341,8 @@ export const openVault = async ({

/**
* @typedef {object} Trackers
* @property {object | undefined} reserveTracker
* @property {object | undefined} collateralManagerTracker
* @property {object} [reserveTracker]
* @property {object} [collateralManagerTracker]
*/

/**
Expand All @@ -308,7 +355,7 @@ export const getMetricTrackers = async ({
reservePublicFacet,
}) => {
/** @type {Trackers} */
const trackers = harden({});
const trackers = {};
if (reservePublicFacet) {
const metricsTopic = await E.get(E(reservePublicFacet).getPublicTopics())
.metrics;
Expand All @@ -322,7 +369,7 @@ export const getMetricTrackers = async ({
);
}

return trackers;
return harden(trackers);
};

export const getBookDataTracker = async (t, auctioneerPublicFacet, brand) => {
Expand Down

0 comments on commit bbd2e48

Please sign in to comment.