From a5815ae490c2c2a3b828688f7bedad6263612d09 Mon Sep 17 00:00:00 2001 From: anilhelvaci Date: Tue, 23 Jan 2024 11:17:32 +0300 Subject: [PATCH] chore(liquidationVisibility): #4 test skeleton is ready --- .../test/liquidationVisibility/assertions.js | 27 +++++++ .../test-liquidationVisibility.js | 76 ++++++++++++++++++- .../test-visibilityAssertions.js | 15 +++- .../test/liquidationVisibility/tools.js | 4 +- 4 files changed, 115 insertions(+), 7 deletions(-) diff --git a/packages/inter-protocol/test/liquidationVisibility/assertions.js b/packages/inter-protocol/test/liquidationVisibility/assertions.js index 90ddc0ae16c..2102de65f18 100644 --- a/packages/inter-protocol/test/liquidationVisibility/assertions.js +++ b/packages/inter-protocol/test/liquidationVisibility/assertions.js @@ -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'; @@ -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); +}; diff --git a/packages/inter-protocol/test/liquidationVisibility/test-liquidationVisibility.js b/packages/inter-protocol/test/liquidationVisibility/test-liquidationVisibility.js index 66779a77b83..97ed6885e9c 100644 --- a/packages/inter-protocol/test/liquidationVisibility/test-liquidationVisibility.js +++ b/packages/inter-protocol/test/liquidationVisibility/test-liquidationVisibility.js @@ -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, @@ -45,6 +46,9 @@ import { assertVaultSeatExited, assertVaultState, assertMintedProceeds, + assertNodeInStorage, + assertLiqNodeForAuctionCreated, + assertStorageData, } from './assertions.js'; import { Phase } from '../vaultFactory/driver.js'; @@ -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'); @@ -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), ); @@ -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({ @@ -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); @@ -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}`, + // }); }); diff --git a/packages/inter-protocol/test/liquidationVisibility/test-visibilityAssertions.js b/packages/inter-protocol/test/liquidationVisibility/test-visibilityAssertions.js index 7321a9d24ed..6738c0fd1c1 100644 --- a/packages/inter-protocol/test/liquidationVisibility/test-visibilityAssertions.js +++ b/packages/inter-protocol/test/liquidationVisibility/test-visibilityAssertions.js @@ -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 }, @@ -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' }, + }); +}); diff --git a/packages/inter-protocol/test/liquidationVisibility/tools.js b/packages/inter-protocol/test/liquidationVisibility/tools.js index 7b8c1ea7c78..13d8f06bc3e 100644 --- a/packages/inter-protocol/test/liquidationVisibility/tools.js +++ b/packages/inter-protocol/test/liquidationVisibility/tools.js @@ -236,7 +236,7 @@ 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)); @@ -244,7 +244,7 @@ export const startAuctionClock = async (auctioneerKit, manualTimer) => { 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) => {