From 941b8223a391af2f8f1162ca200ce3257a71b079 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 9 Aug 2024 15:16:19 -0500 Subject: [PATCH] test: the way I'd _like_ to write the test; not working WIP --- .../vault-collateralization.test.js | 90 ++++++------------- 1 file changed, 29 insertions(+), 61 deletions(-) diff --git a/packages/inter-protocol/test/vaultFactory/vault-collateralization.test.js b/packages/inter-protocol/test/vaultFactory/vault-collateralization.test.js index 9cdac08df87..5a0703c76ef 100644 --- a/packages/inter-protocol/test/vaultFactory/vault-collateralization.test.js +++ b/packages/inter-protocol/test/vaultFactory/vault-collateralization.test.js @@ -4,7 +4,7 @@ import { makeTracer } from '@agoric/internal'; import { E } from '@endo/eventual-send'; import { makeNotifierFromSubscriber } from '@agoric/notifier'; import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js'; -import { makeDriverContext, makeManagerDriver } from './driver.js'; +import { AT_NEXT, makeDriverContext, makeManagerDriver } from './driver.js'; /** @typedef {import('./driver.js').DriverContext & {}} Context */ /** @type {import('ava').TestFn} */ @@ -19,74 +19,42 @@ test.before(async t => { test('totalDebt does not include compoundedInterest', async t => { const { aeth, run } = t.context; - const md = await makeManagerDriver(t); // TODO: thread timer step + const md = await makeManagerDriver(t); const timer = md.timer(); - const vfactory = md.getVaultDirectorPublic(); - - t.log( - '1. Have a large vault with some debt in a system in which the VaultManager.compoundedInterest is large', - ); - // TODO: think about interest rate a bit... trying 720% - // so 1 day is 2% - await md.setGovernedParam('InterestRate', run.makeRatio(720n, 100n), { - key: { collateralBrand: aeth.brand }, - }); - // plausible debt limit: $500k - await md.setGovernedParam('DebtLimit', run.units(500000), { - key: { collateralBrand: aeth.brand }, - }); + t.log('charge 2% per day to simulate lower rates over longer times'); + const aethKey = { collateralBrand: aeth.brand }; + const twoPctPerDay = run.makeRatio(2n * 360n, 100n); + await md.setGovernedParam('InterestRate', twoPctPerDay, { key: aethKey }); + const plausibleDebtLimit = run.units(500000); + await md.setGovernedParam('DebtLimit', plausibleDebtLimit, { key: aethKey }); + t.log('mint 100 IST against 25 AETH'); const vd = await md.makeVaultDriver(aeth.units(25), run.units(100)); - t.deepEqual(await E(vd.vault()).getCurrentDebt(), run.make(105_000_000n)); + const v1debtAfterMint = await E(vd.vault()).getCurrentDebt(); + t.deepEqual(v1debtAfterMint, run.make(105_000_000n)); + // totalDebt is debit of this 1 vault + await md.metricsNotified({ totalDebt: v1debtAfterMint }, AT_NEXT); await E(timer).tickN(40, 'interest accumulates over 40hrs'); - await eventLoopIteration(); /// ???? - t.deepEqual(await E(vd.vault()).getCurrentDebt(), run.make(106008000n)); - - // check total debt - const aethMgr = await E(vfactory).getCollateralManager(aeth.brand); - t.log({ aethMgr }); - const topics = await E(aethMgr).getPublicTopics(); - t.log({ topics }); - - const metricsNotifier = await makeNotifierFromSubscriber( - topics.metrics.subscriber, - ); - let updateCount; - const managerNotification1 = - await E(metricsNotifier).getUpdateSince(updateCount); - t.like(managerNotification1.value.totalDebt, { value: 105000000n }, 'XXX'); - ({ updateCount } = managerNotification1); - // const managerNotification2 = - // await E(metricsNotifier).getUpdateSince(updateCount); - // t.like(managerNotification2.value.totalDebt, { value: 105_000_000n }); - // ({ updateCount } = managerNotification2); - - await md.managerNotified({ - compoundedInterest: { - denominator: { - value: 100000000000000000000n, - }, - numerator: { - value: 100960000000000000000n, - }, - }, - }); - - t.log('change the collateral of the vault with adjustBalance'); + await eventLoopIteration(); // let all timer-induced promises settle + const v1debtAfterDay = await E(vd.vault()).getCurrentDebt(); + t.deepEqual(v1debtAfterDay, run.make(106_008_000n)); + await md.metricsNotified({ totalDebt: v1debtAfterDay }, AT_NEXT); + + const expectedCompoundedInterest = { + denominator: { value: 100000000000000000000n }, + numerator: { value: 100960000000000000000n }, + }; + await md.managerNotified({ compoundedInterest: expectedCompoundedInterest }); + + t.log('give some collateral (adjustBalances); no change to debt'); await E(vd).giveCollateral(50n, aeth); + const v1debtAfterGive = await E(vd.vault()).getCurrentDebt(); + t.deepEqual(v1debtAfterGive, v1debtAfterDay); + await eventLoopIteration(); // let notifications settle - t.log('vault debt now', await E(vd.vault()).getCurrentDebt()); - t.deepEqual(await E(vd.vault()).getCurrentDebt(), run.make(106008000n)); - - const managerNotification3 = - await E(metricsNotifier).getUpdateSince(updateCount); - t.like( - managerNotification3.value.totalDebt, - { value: 106_008_000n }, - 'total debt stays the same', - ); + await md.metricsNotified({ totalDebt: v1debtAfterDay }, AT_NEXT); }); test('excessive loan', async t => {