Skip to content

Commit

Permalink
chore(liquidationVisibility): #4 test skeleton is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
anilhelvaci committed Feb 16, 2024
1 parent a39f547 commit b441bff
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
27 changes: 27 additions & 0 deletions packages/inter-protocol/test/liquidationVisibility/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ceilMultiplyBy,
makeRatio,
} from '@agoric/zoe/src/contractSupport/index.js';
import { TimeMath } from '@agoric/time';
import { headValue } from '../supports.js';
import { getDataFromVstorage } from './tools.js';

Expand Down Expand Up @@ -224,3 +225,29 @@ export const assertNodeInStorage = async ({
const [...storageData] = await getDataFromVstorage(rootNode, desiredNode);
t.is(storageData.length !== 0, expected);
};

// Currently supports only one collateral manager
export const assertLiqNodeForAuctionCreated = async ({
t,
rootNode,
auctioneerPF,
auctionType = 'next', // 'live' is the other option
expected = false,
}) => {
const schedules = await E(auctioneerPF).getSchedules();
const { startTime, startDelay } = schedules[`${auctionType}AuctionSchedule`];
const nominalStart = TimeMath.subtractAbsRel(startTime, startDelay);

await assertNodeInStorage({
t,
rootNode,
desiredNode: `vaultFactory.managers.manager0.liquidations.${nominalStart}`,
expected,
});
};

export const assertStorageData = async ({ t, path, storageRoot, expected }) => {
/** @type Array */
const [[, value]] = await getDataFromVstorage(storageRoot, path);
t.deepEqual(JSON.parse(value), expected);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
makeRatio,
makeRatioFromAmounts,
} from '@agoric/zoe/src/contractSupport/index.js';
import { documentStorageSchema } from '@agoric/governance/tools/storageDoc.js';
import {
defaultParamValues,
legacyOfferResult,
Expand Down Expand Up @@ -45,6 +46,9 @@ import {
assertVaultSeatExited,
assertVaultState,
assertMintedProceeds,
assertNodeInStorage,
assertLiqNodeForAuctionCreated,
assertStorageData,
} from './assertions.js';
import { Phase } from '../vaultFactory/driver.js';

Expand Down Expand Up @@ -227,6 +231,7 @@ test('liq-result-scenario-2', async t => {
aethTestPriceAuthority,
reserveKit: { reserveCreatorFacet, reservePublicFacet },
auctioneerKit,
chainStorage,
} = services;
await E(reserveCreatorFacet).addIssuer(aeth.issuer, 'Aeth');

Expand Down Expand Up @@ -317,6 +322,13 @@ test('liq-result-scenario-2', async t => {
totalCollateral: { value: 700n },
});

// TODO: UNCOMMENT THIS WHEN SOURCE IS READY
// await assertLiqNodeForAuctionCreated({
// t,
// rootNode: chainStorage,
// auctioneerPF: auctioneerKit.publicFacet,
// });

await E(aethTestPriceAuthority).setPrice(
makeRatio(70n, run.brand, 10n, aeth.brand),
);
Expand All @@ -327,10 +339,12 @@ test('liq-result-scenario-2', async t => {
const desired = aeth.make(700n);
const bidderSeat = await bid(t, zoe, auctioneerKit, aeth, bidAmount, desired);

const { startTime: start1, time: now1 } = await startAuctionClock(
auctioneerKit,
manualTimer,
);
const {
startTime: start1,
time: now1,
endTime,
} = await startAuctionClock(auctioneerKit, manualTimer);

let currentTime = now1;

await collateralManagerTracker.assertChange({
Expand All @@ -343,6 +357,22 @@ test('liq-result-scenario-2', async t => {
// expect Alice to be liquidated because her collateral is too low.
await assertVaultState(t, aliceNotifier, Phase.LIQUIDATING);

// TODO: Check vaults.preAuction here
// await assertStorageData({
// t,
// storageRoot: chainStorage,
// path: `vaultFactory.managers.manager0.liquidations.${now1}.preAuction`, // now1 is the nominal start time
// expected: [
// [
// 'vault0',
// {
// collateral: aeth.make(700n),
// debt: await E(aliceVault).getCurrentDebt(),
// },
// ],
// ],
// });

currentTime = await setClockAndAdvanceNTimes(manualTimer, 2, start1, 2n);

await assertVaultState(t, aliceNotifier, Phase.LIQUIDATED);
Expand Down Expand Up @@ -374,4 +404,42 @@ test('liq-result-scenario-2', async t => {

// Bidder bought 800 Aeth
await assertBidderPayout(t, bidderSeat, run, 115n, aeth, 700n);

// TODO: Check vaults.postAuction and auctionResults here
// await assertStorageData({
// t,
// storageRoot: chainStorage,
// path: `vaultFactory.managers.manager0.liquidations.${now1}.postAuction`, // now1 is the nominal start time
// expected: [
// [
// 'vault0',
// {
// collateral: aeth.makeEmpty(),
// debt: run.makeEmpty(),
// phase: Phase.LIQUIDATED,
// },
// ],
// ],
// });

// FIXME: https://github.com/Jorge-Lopes/agoric-sdk-liquidation-visibility/issues/3#issuecomment-1905488335
// await assertStorageData({
// t,
// storageRoot: chainStorage,
// path: `vaultFactory.managers.manager0.liquidations.${now1}.auctionResult`, // now1 is the nominal start time
// expected: {
// collateralForReserve: aeth.makeEmpty(),
// shortfallToReserve: run.make(2065n),
// mintedProceeds: run.make(3185n),
// collateralSold: aeth.make(700n),
// collateralRemaining: aeth.makeEmpty(),
// endTime,
// },
// });

// TODO: Snapshot here
// await documentStorageSchema(t, chainStorage, {
// note: 'Scenario 2 Liquidation Visibility Snapshot',
// node: `vaultFactory.managers.manager0.liquidations.${now1}`,
// });
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { E } from '@endo/far';
import { makeImportContext } from '@agoric/smart-wallet/src/marshal-contexts.js';
import { makeMockChainStorageRoot } from '../supports.js';
import { assertNodeInStorage } from './assertions.js';
import { assertNodeInStorage, assertStorageData } from './assertions.js';

const {
fromBoard: { toCapData },
Expand Down Expand Up @@ -34,3 +34,16 @@ test('storage-node-created', async t => {
expected: true,
});
});

test('storage-assert-data', async t => {
const storageRoot = makeMockChainStorageRoot();
const testNode = await E(storageRoot).makeChildNode('dummyNode');
await writeToStorage(testNode, { dummy: 'foo' });

await assertStorageData({
t,
path: 'dummyNode',
storageRoot,
expected: { dummy: 'foo' },
});
});
4 changes: 2 additions & 2 deletions packages/inter-protocol/test/liquidationVisibility/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ export const setClockAndAdvanceNTimes = async (
export const startAuctionClock = async (auctioneerKit, manualTimer) => {
const schedule = await E(auctioneerKit.creatorFacet).getSchedule();
const priceDelay = await E(auctioneerKit.publicFacet).getPriceLockPeriod();
const { startTime, startDelay } = schedule.nextAuctionSchedule;
const { startTime, startDelay, endTime } = schedule.nextAuctionSchedule;
const nominalStart = TimeMath.subtractAbsRel(startTime, startDelay);
const priceLockTime = TimeMath.subtractAbsRel(nominalStart, priceDelay);
await manualTimer.advanceTo(TimeMath.absValue(priceLockTime));
await eventLoopIteration();

await manualTimer.advanceTo(TimeMath.absValue(nominalStart));
await eventLoopIteration();
return { startTime, time: nominalStart };
return { startTime, time: nominalStart, endTime };
};

export const bid = async (t, zoe, auctioneerKit, aeth, bidAmount, desired) => {
Expand Down

0 comments on commit b441bff

Please sign in to comment.