Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vault test refactor #5103

Merged
merged 8 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/run-protocol/src/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ export const setupAmm = async ({
governorInstallation,
{},
ammGovernorTerms,
{
electorateCreatorFacet: committeeCreator,
},
{ electorateCreatorFacet: committeeCreator },
);

const [creatorFacet, ammPublicFacet, instance] = await Promise.all([
Expand Down Expand Up @@ -525,7 +523,7 @@ harden(startRewardDistributor);
*/

/**
* @typedef {EconomyBootstrapPowers & WellKnownSpaces & PromiseMarket<{
* @typedef {EconomyBootstrapPowers & PromiseMarket<{
* runStakeBundle: SourceBundle,
* client: ClientManager,
* lienBridge: StakingAuthority,
Expand Down
8 changes: 6 additions & 2 deletions packages/run-protocol/src/makeTracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ const makeTracer = (name, enable = true) => {
return infoTick;
}
default: {
const debugTick = (...args) => {
console.log(key, (debugCount += 1), ...args);
const debugTick = (optLog, ...args) => {
if (optLog.log) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope we find time after MN-1 to rationalize logging. I don't think we have a ticket yet for that per se. #1318 is to do research.

optLog.log(key, (debugCount += 1), ...args);
} else {
console.info(key, (debugCount += 1), optLog, ...args);
}
};
return debugTick;
}
Expand Down
9 changes: 3 additions & 6 deletions packages/run-protocol/src/vaultFactory/liquidateMinimum.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ const start = async zcf => {
// check whether swapIn liquidated assets until getOfferResult() returns.
// Of course, we also can't exit the seat until one or the other
// liquidation takes place.
const [offerResult, amounts, _deposited] = await Promise.all([
E(liqSeat).getOfferResult(),
E(liqSeat).getCurrentAllocation(),
deposited,
]);
trace('offerResult', offerResult, amounts);
const amounts = await deposited;
await E(liqSeat).getOfferResult();
trace('exact sell result', amounts);

// if swapOut failed to make the trade, we'll sell it all
const sellAllIfUnsold = async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/run-protocol/src/vaultFactory/vaultManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ const helperBehavior = {
const debtPost = AmountMath.add(totalDebt, toMint);
const limit = factoryPowers.getGovernedParams().getDebtLimit();
if (AmountMath.isGTE(debtPost, limit)) {
assert.fail(X`Minting would exceed total debt limit ${q(limit)}`);
assert.fail(
X`Minting ${q(toMint)} would exceed total debt limit ${q(limit)}`,
);
}
},

Expand Down
26 changes: 4 additions & 22 deletions packages/run-protocol/test/psm/test-psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,22 @@ import '../../src/vaultFactory/types.js';

import path from 'path';
import { E } from '@endo/eventual-send';
import bundleSource from '@endo/bundle-source';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import { makeZoeKit } from '@agoric/zoe';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { makeLoopback } from '@endo/captp';
import {
makeRatio,
floorDivideBy,
floorMultiplyBy,
natSafeMath as NatMath,
} from '@agoric/zoe/src/contractSupport/index.js';
import { makeTracer } from '../../src/makeTracer.js';
import { makeBundle, setUpZoeForTest } from '../supports.js';

const pathname = new URL(import.meta.url).pathname;
const dirname = path.dirname(pathname);

const psmRoot = `${dirname}/../../src/psm/psm.js`;
const trace = makeTracer('TestPSM', false);

const makeBundle = async sourceRoot => {
const url = await importMetaResolve(sourceRoot, import.meta.url);
const contractBundle = await bundleSource(new URL(url).pathname);
console.log(`makeBundle ${sourceRoot}`);
return contractBundle;
};

const BASIS_POINTS = 10000n;
const WantStableFeeBP = 1n;
const GiveStableFeeBP = 3n;
Expand Down Expand Up @@ -88,16 +77,9 @@ test.before(async t => {
// makeBundle is slow, so we bundle each contract once and reuse in all tests.
const psmBundle = await makeBundle(psmRoot);
t.context.bundles = { psmBundle };
const { makeFar, makeNear: makeRemote } = makeLoopback('zoeTest');
const { zoeService, feeMintAccess: nonFarFeeMintAccess } = makeZoeKit(
makeFakeVatAdmin(setJig, makeRemote).admin,
);
/** @type {ERef<ZoeService>} */
const zoe = makeFar(zoeService);
t.context.zoe = zoe;
trace('makeZoe');
const feeMintAccess = await makeFar(nonFarFeeMintAccess);
t.context.feeMintAccess = feeMintAccess;
const { zoe, feeMintAccess } = setUpZoeForTest(setJig);
t.context.zoe = await zoe;
t.context.feeMintAccess = await feeMintAccess;

const runIssuer = await E(zoe).getFeeIssuer();
const runBrand = await E(runIssuer).getBrand();
Expand Down
19 changes: 2 additions & 17 deletions packages/run-protocol/test/runStake/test-runStake.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
economyBundles,
} from '../../src/importedBundles.js';
import * as Collect from '../../src/collect.js';
import { setUpZoeForTest } from '../supports.js';
import { KW } from '../../src/runStake/params.js';

// 8 Partial repayment from reward stream - TODO
Expand Down Expand Up @@ -68,29 +69,13 @@ test.before(async t => {
*/
const theBundles = t => /** @type { any } */ (t.context).bundles;

export const setUpZoeForTest = async () => {
const { makeFar } = makeLoopback('zoeTest');

const { zoeService, feeMintAccess: nonFarFeeMintAccess } = makeZoeKit(
makeFakeVatAdmin(() => {}).admin,
);
/** @type {ERef<ZoeService>} */
const zoe = makeFar(zoeService);
const feeMintAccess = await makeFar(nonFarFeeMintAccess);
return {
zoe,
feeMintAccess,
};
};
harden(setUpZoeForTest);

export const setupBootstrap = async (
bundles,
timer = buildManualTimer(console.log),
zoe,
) => {
if (!zoe) {
({ zoe } = await setUpZoeForTest());
zoe = await setUpZoeForTest().zoe;
}

const space = /** @type {any} */ (makePromiseSpace());
Expand Down
93 changes: 89 additions & 4 deletions packages/run-protocol/test/supports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// @ts-check
/* global setImmediate */

import { AmountMath } from '@agoric/ertp';
import { Far } from '@endo/marshal';
import bundleSource from '@endo/bundle-source';
import { makeLoopback } from '@endo/captp';

import { resolve as importMetaResolve } from 'import-meta-resolve';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import { makeZoeKit } from '@agoric/zoe';
import {
makeAgoricNamesAccess,
makePromiseSpace,
} from '@agoric/vats/src/core/utils.js';
import buildManualTimer from '@agoric/zoe/tools/manualTimer.js';
import { governanceBundles, economyBundles } from '../src/importedBundles.js';

/**
*
Expand All @@ -8,11 +23,11 @@ import { Far } from '@endo/marshal';
* @param {Amount} initCollateral
* @returns {InnerVault & {setDebt: (Amount) => void}}
*/
export function makeFakeInnerVault(
export const makeFakeInnerVault = (
vaultId,
initDebt,
initCollateral = AmountMath.make(initDebt.brand, 100n),
) {
) => {
let debt = initDebt;
let collateral = initCollateral;
const vault = Far('Vault', {
Expand All @@ -24,6 +39,76 @@ export function makeFakeInnerVault(
getIdInManager: () => vaultId,
liquidate: () => {},
});
// @ts-expect-error pretend this is compatible with VaultKit
// @ts-expect-error cast
return vault;
}
};

/**
*
* @param {string} sourceRoot
* @returns {Promise<SourceBundle>}
*/
export const makeBundle = async sourceRoot => {
const url = await importMetaResolve(sourceRoot, import.meta.url);
const path = new URL(url).pathname;
const contractBundle = await bundleSource(path);
console.log(`makeBundle ${sourceRoot}`);
return contractBundle;
};
harden(makeBundle);

// Some notifier updates aren't propagating sufficiently quickly for
// the tests. This invocation waits for all promises that can fire to
// have all their callbacks run
export const waitForPromisesToSettle = async () =>
new Promise(resolve => setImmediate(resolve));
harden(waitForPromisesToSettle);

/**
* Returns promises for `zoe` and the `feeMintAccess`.
*
* @param {() => void} setJig
*/
export const setUpZoeForTest = (setJig = () => {}) => {
const { makeFar } = makeLoopback('zoeTest');

const { zoeService, feeMintAccess: nonFarFeeMintAccess } = makeZoeKit(
makeFakeVatAdmin(setJig).admin,
);
/** @type {ERef<ZoeService>} */
const zoe = makeFar(zoeService);
const feeMintAccess = makeFar(nonFarFeeMintAccess);
return {
zoe,
feeMintAccess,
};
};
harden(setUpZoeForTest);

export const setupBootstrap = (t, optTimer = undefined) => {
const space = /** @type {any} */ (makePromiseSpace(t.log));
const { produce, consume } = /** @type {EconomyBootstrapPowers} */ (space);

const timer = optTimer || buildManualTimer(t.log);
produce.chainTimerService.resolve(timer);

const {
zoe,
feeMintAccess,
runKit: { brand: runBrand, issuer: runIssuer },
} = t.context;
produce.zoe.resolve(zoe);
produce.feeMintAccess.resolve(feeMintAccess);

const { agoricNames, spaces } = makeAgoricNamesAccess();
produce.agoricNames.resolve(agoricNames);

const { brand, issuer } = spaces;
brand.produce.RUN.resolve(runBrand);
issuer.produce.RUN.resolve(runIssuer);

produce.governanceBundles.resolve(governanceBundles);
produce.centralSupplyBundle.resolve(economyBundles.centralSupply);

return { produce, consume, ...spaces };
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,26 @@ import '../../src/vaultFactory/types.js';

import path from 'path';
import { E } from '@endo/eventual-send';
import bundleSource from '@endo/bundle-source';
import { makeFakeVatAdmin } from '@agoric/zoe/tools/fakeVatAdmin.js';
import { makeZoeKit } from '@agoric/zoe';
import { AmountMath } from '@agoric/ertp';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { makeLoopback } from '@endo/captp';
import { makeBundle, setUpZoeForTest } from '../supports.js';

const pathname = new URL(import.meta.url).pathname;
const dirname = path.dirname(pathname);

const centralSupplyRoot = `${dirname}/../../src/centralSupply.js`;

const makeBundle = async sourceRoot => {
const url = await importMetaResolve(sourceRoot, import.meta.url);
const contractBundle = await bundleSource(new URL(url).pathname);
console.log(`makeBundle ${sourceRoot}`);
return contractBundle;
};

// makeBundle is slow, so we bundle each contract once and reuse in all tests.
const [centralSupplyBundle] = await Promise.all([
makeBundle(centralSupplyRoot),
]);

const installBundle = (zoe, contractBundle) => E(zoe).install(contractBundle);

const setUpZoeForTest = async setJig => {
const { makeFar } = makeLoopback('zoeTest');
const { zoeService, feeMintAccess: nonFarFeeMintAccess } = makeZoeKit(
makeFakeVatAdmin(setJig).admin,
);
/** @type {ERef<ZoeService>} */
const zoe = makeFar(zoeService);
const feeMintAccess = await makeFar(nonFarFeeMintAccess);
return {
zoe,
feeMintAccess,
};
};

const startContract = async bootstrapPaymentValue => {
const { zoe, feeMintAccess } = await setUpZoeForTest(() => {});
const { zoe, feeMintAccess: feeMintAccessP } = setUpZoeForTest();
const runIssuer = E(zoe).getFeeIssuer();
const runBrand = await E(runIssuer).getBrand();
const feeMintAccess = await feeMintAccessP;

/** @type {import('@agoric/zoe/src/zoeService/utils').Installation<import('../../src/centralSupply.js').CentralSupplyContract>} */
const centralSupplyInstall = await installBundle(zoe, centralSupplyBundle);
Expand Down
Loading