Skip to content

Commit

Permalink
manager notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 4, 2022
1 parent 4e576c1 commit db362c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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 @@ -38,6 +38,7 @@ const trace = makeTracer('VM', true);
* }} AssetState
*
* @typedef {{
* numVaults: number,
* totalCollateral: Amount<'nat'>,
* totalDebt: Amount<'nat'>,
* }} EconState
Expand Down Expand Up @@ -158,6 +159,7 @@ const initState = (

const { updater: econUpdater, notifier: econNotifier } = makeNotifierKit(
harden({
numVaults: 0,
totalCollateral,
totalDebt,
}),
Expand Down Expand Up @@ -476,7 +478,7 @@ const collateralBehavior = {
/** @param {MethodContext} context */
getAssetNotifier: ({ state }) => state.assetNotifier,
/** @param {MethodContext} context */
getEconNotifier: ({ state }) => state.econNotifier,
getPublicNotifiers: ({ state }) => ({ econ: state.econNotifier }),
/** @param {MethodContext} context */
getCompoundedInterest: ({ state }) => state.compoundedInterest,
};
Expand Down
50 changes: 48 additions & 2 deletions packages/run-protocol/test/vaultFactory/test-vaultFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,12 @@ async function setupServices(
);

// Add a vault that will lend on aeth collateral
/** @type {Promise<VaultManager>} */
const aethVaultManagerP = E(vaultFactoryCreatorFacetP).addVaultType(
aethIssuer,
'AEth',
rates,
);
/** @type {[any, VaultFactory, VFC['publicFacet']]} */
// @ts-expect-error cast
const [
governorInstance,
vaultFactoryCreator,
Expand Down Expand Up @@ -2345,3 +2344,50 @@ test('director notifiers', async t => {
// Not testing penaltyPoolAllocation and rewardPoolAllocation contents because those are simply those values.
// We could refactor the tests of those allocations to use the data now exposed by a notifier.
});

test('manager notifiers', async t => {
const { aethKit, runKit } = t.context;
const services = await setupServices(
t,
[500n, 15n],
AmountMath.make(aethKit.brand, 900n),
undefined,
undefined,
500n,
);

const { aethVaultManager, lender } = services.vaultFactory;
const cm = await E(aethVaultManager).getPublicFacet();

const { econ } = await E(cm).getPublicNotifiers();
let state = await E(econ).getUpdateSince();
t.deepEqual(state.value, {
numVaults: 0,
totalCollateral: AmountMath.makeEmpty(aethKit.brand),
totalDebt: AmountMath.makeEmpty(t.context.runKit.brand),
});

// Create a loan for 470 RUN with 1100 aeth collateral
const collateralAmount = AmountMath.make(aethKit.brand, 1100n);
const loanAmount = AmountMath.make(runKit.brand, 470n);
/** @type {UserSeat<VaultKit>} */
const vaultSeat = await E(services.zoe).offer(
await E(lender).makeVaultInvitation(),
harden({
give: { Collateral: collateralAmount },
want: { RUN: loanAmount },
}),
harden({
Collateral: t.context.aethKit.mint.mintPayment(collateralAmount),
}),
);

await E(vaultSeat).getOfferResult();

state = await E(econ).getUpdateSince(state.updateCount);
t.deepEqual(state.value, {
numVaults: 1,
totalCollateral: collateralAmount,
totalDebt: AmountMath.make(runKit.brand, 494n), // 470 plus fee
});
});

0 comments on commit db362c6

Please sign in to comment.