Skip to content

Commit

Permalink
4602 simpler vault uinotify 2 (#4610)
Browse files Browse the repository at this point in the history
* fix: remove `collateralizationRatio`

* remove the operation from `vault.uiUpdater`
* delete many tests that looked at its specific structure
* change some tests to check the debt to ensure
  that the intended change has been made.

Co-authored-by: Dean Tribble <[email protected]>
  • Loading branch information
dtribble and dtribble authored Feb 22, 2022
1 parent a4f297c commit 85228b7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 121 deletions.
1 change: 0 additions & 1 deletion packages/run-protocol/src/vaultFactory/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
* @typedef {Object} BaseUIState
* @property {Amount<NatValue>} locked Amount of Collateral locked
* @property {Amount<NatValue>} debt Amount of Loan (including accrued interest)
* @property {Ratio} collateralizationRatio
*/

/**
Expand Down
31 changes: 3 additions & 28 deletions packages/run-protocol/src/vaultFactory/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { makeNotifierKit, observeNotifier } from '@agoric/notifier';

import {
invertRatio,
makeRatio,
multiplyRatios,
} from '@agoric/zoe/src/contractSupport/ratio.js';
import { AmountMath } from '@agoric/ertp';
Expand Down Expand Up @@ -240,25 +239,7 @@ export const makeInnerVault = (
: getCollateralAllocated(vaultSeat);
};

/**
* @returns {Promise<Ratio>} Collateral over actual debt
*/
const getCollateralizationRatio = async () => {
const collateralAmount = getCollateralAmount();
const quoteAmount = await E(priceAuthority).quoteGiven(
collateralAmount,
runBrand,
);

// TODO: allow Ratios to represent X/0.
if (AmountMath.isEmpty(debtSnapshot.run)) {
return makeRatio(collateralAmount.value, runBrand, 1n);
}
const collateralValueInRun = getAmountOut(quoteAmount);
return makeRatioFromAmounts(collateralValueInRun, getDebtAmount());
};

const snapshotState = (vstate, collateralizationRatio) => {
const snapshotState = vstate => {
/** @type {VaultUIState} */
return harden({
// TODO move manager state to a separate notifer https://github.com/Agoric/agoric-sdk/issues/4540
Expand All @@ -267,25 +248,19 @@ export const makeInnerVault = (
debtSnapshot,
locked: getCollateralAmount(),
debt: getDebtAmount(),
collateralizationRatio,
// TODO state distinct from CLOSED https://github.com/Agoric/agoric-sdk/issues/4539
liquidated: vaultState === VaultState.CLOSED,
vaultState: vstate,
});
};

// call this whenever anything changes!
const updateUiState = async () => {
const updateUiState = () => {
if (!outerUpdater) {
return;
}
// TODO(123): track down all calls and ensure that they all update a
// lastKnownCollateralizationRatio (since they all know) so we don't have to
// await quoteGiven() here
// [https://github.com/Agoric/dapp-token-economy/issues/123]
const collateralizationRatio = await getCollateralizationRatio();
/** @type {VaultUIState} */
const uiState = snapshotState(vaultState, collateralizationRatio);
const uiState = snapshotState(vaultState);
trace('updateUiState', uiState);

switch (vaultState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const expectedVaultFactoryLog = [
'after vote on (InterestRate), InterestRate numerator is 4321',
'at 3 days: vote closed',
'at 3 days: Alice owes {"brand":"[Alleged: RUN brand]","value":"[510105n]"}',
'at 3 days: 1 day after votes cast, uiNotifier update #4 has interestRate.numerator 250',
'at 4 days: 2 days after votes cast, uiNotifier update #5 has interestRate.numerator 4321',
'at 3 days: 1 day after votes cast, uiNotifier update #5 has interestRate.numerator 250',
'at 4 days: 2 days after votes cast, uiNotifier update #6 has interestRate.numerator 4321',
'at 4 days: Alice owes {"brand":"[Alleged: RUN brand]","value":"[510608n]"}',
];

Expand Down
95 changes: 5 additions & 90 deletions packages/run-protocol/test/vaultFactory/test-vaultFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ test('price drop', async t => {
const { vault, uiNotifier } = await E(loanSeat).getOfferResult();
trace('offer result', vault);
const debtAmount = await E(vault).getDebtAmount();
const fee = ceilMultiplyBy(AmountMath.make(runBrand, 270n), rates.loanFee);
const fee = ceilMultiplyBy(loanAmount, rates.loanFee);
t.deepEqual(
debtAmount,
AmountMath.add(loanAmount, fee),
Expand All @@ -509,10 +509,7 @@ test('price drop', async t => {
trace('got notificaation', notification);

t.falsy(notification.value.liquidated);
t.deepEqual(
await notification.value.collateralizationRatio,
makeRatio(444n, runBrand, 284n),
);
t.deepEqual(await notification.value.debt, AmountMath.add(loanAmount, fee));
const { RUN: lentAmount } = await E(loanSeat).getCurrentAllocation();
t.truthy(AmountMath.isEqual(lentAmount, loanAmount), 'received 470 RUN');
t.deepEqual(
Expand All @@ -538,10 +535,6 @@ test('price drop', async t => {
const debtAmountAfter = await E(vault).getDebtAmount();
const finalNotification = await E(uiNotifier).getUpdateSince();
t.truthy(finalNotification.value.liquidated);
t.deepEqual(
await finalNotification.value.collateralizationRatio,
makeRatio(0n, runBrand, 1n),
);
t.truthy(AmountMath.isEmpty(debtAmountAfter));

t.deepEqual(await E(vaultFactory).getRewardAllocation(), {
Expand Down Expand Up @@ -877,11 +870,6 @@ test('interest on multiple vaults', async t => {
bobUpdate.value.liquidationRatio,
makeRatio(105n, runBrand, 100n),
);
const bobCollateralization = bobUpdate.value.collateralizationRatio;
t.truthy(
bobCollateralization.numerator.value >
bobCollateralization.denominator.value,
);

// 236 is the initial fee. Interest is ~3n/week
const aliceAddedDebt = 236n + 3n;
Expand All @@ -897,16 +885,6 @@ test('interest on multiple vaults', async t => {
});
t.deepEqual(aliceUpdate.value.interestRate, interestRate);
t.deepEqual(aliceUpdate.value.liquidationRatio, makeRatio(105n, runBrand));
const aliceCollateralization = aliceUpdate.value.collateralizationRatio;
t.truthy(
aliceCollateralization.numerator.value >
aliceCollateralization.denominator.value,
);
t.is(
aliceCollateralization.denominator.value,
aliceUpdate.value.debt.value,
`Debt in collateralizationRatio should match actual debt`,
);

const rewardAllocation = await E(vaultFactory).getRewardAllocation();
const rewardRunCount = aliceAddedDebt + bobAddedDebt + 1n; // +1 due to rounding
Expand Down Expand Up @@ -1001,7 +979,6 @@ test('adjust balances', async t => {
} = services;
const { vaultFactory, lender } = services.vaultFactory;

const priceConversion = makeRatio(15n, runBrand, 1n, aethBrand);
const rates = makeRates(runBrand);
await E(vaultFactory).addVaultType(aethIssuer, 'AEth', rates);

Expand Down Expand Up @@ -1044,9 +1021,6 @@ test('adjust balances', async t => {

let aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebtLevel);
const aliceCollateralization1 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(aliceCollateralization1.numerator.value, 15000n);
t.deepEqual(aliceCollateralization1.denominator.value, runDebtLevel.value);
t.deepEqual(aliceUpdate.value.debtSnapshot, {
run: AmountMath.make(runBrand, 5250n),
interest: makeRatio(100n, runBrand),
Expand Down Expand Up @@ -1096,12 +1070,6 @@ test('adjust balances', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebtLevel);
const aliceCollateralization2 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization2.numerator,
ceilMultiplyBy(collateralLevel, priceConversion),
);
t.deepEqual(aliceCollateralization2.denominator, runDebtLevel);

// increase collateral 2 ////////////////////////////////// (want:s, give:c)

Expand Down Expand Up @@ -1149,12 +1117,6 @@ test('adjust balances', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebtLevel);
const aliceCollateralization3 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization3.numerator,
ceilMultiplyBy(collateralLevel, priceConversion),
);
t.deepEqual(aliceCollateralization3.denominator, runDebtLevel);
t.deepEqual(aliceUpdate.value.debtSnapshot, {
run: AmountMath.make(runBrand, 5253n),
interest: makeRatio(100n, runBrand),
Expand Down Expand Up @@ -1183,6 +1145,7 @@ test('adjust balances', async t => {

debtAmount = await E(aliceVault).getDebtAmount();
t.deepEqual(debtAmount, runDebtLevel);
t.deepEqual(collateralLevel, await E(aliceVault).getCollateralAmount());

const { RUN: lentAmount4 } = await E(
aliceReduceCollateralSeat,
Expand All @@ -1207,12 +1170,6 @@ test('adjust balances', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebtLevel);
const aliceCollateralization4 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization4.numerator,
ceilMultiplyBy(collateralLevel, priceConversion),
);
t.deepEqual(aliceCollateralization4.denominator, runDebtLevel);

// NSF ///////////////////////////////////// (want too much of both)

Expand All @@ -1224,7 +1181,6 @@ test('adjust balances', async t => {
runDebtLevel,
AmountMath.add(withdrawRunAmount, withdrawRun3WithFees),
);
collateralLevel = AmountMath.subtract(collateralLevel, collateralDecr2);
const aliceReduceCollateralSeat2 = await E(zoe).offer(
E(aliceVault).makeAdjustBalancesInvitation(),
harden({
Expand Down Expand Up @@ -1454,9 +1410,7 @@ test('overdeposit', async t => {

let aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebt);
const aliceCollateralization1 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(aliceCollateralization1.numerator.value, 15000n);
t.deepEqual(aliceCollateralization1.denominator.value, runDebt.value);
t.deepEqual(aliceUpdate.value.locked, collateralAmount);

// Bob's loan /////////////////////////////////////

Expand Down Expand Up @@ -1512,15 +1466,6 @@ test('overdeposit', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, AmountMath.makeEmpty(runBrand));
const aliceCollateralization5 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization5.numerator,
AmountMath.make(runBrand, 1000n),
);
t.deepEqual(
aliceCollateralization5.denominator,
AmountMath.make(runBrand, 1n),
);

const collectFeesSeat = await E(zoe).offer(
E(vaultFactory).makeCollectFeesInvitation(),
Expand Down Expand Up @@ -1609,7 +1554,6 @@ test('mutable liquidity triggers and interest', async t => {
const aliceDebtAmount = await E(aliceVault).getDebtAmount();
const fee = ceilMultiplyBy(aliceLoanAmount, rates.loanFee);
const aliceRunDebtLevel = AmountMath.add(aliceLoanAmount, fee);
let aliceCollateralLevel = AmountMath.make(aethBrand, 1000n);

t.deepEqual(aliceDebtAmount, aliceRunDebtLevel, 'vault lent 5000 RUN + fees');
const { RUN: aliceLentAmount } = await E(
Expand Down Expand Up @@ -1671,10 +1615,6 @@ test('mutable liquidity triggers and interest', async t => {
// Alice reduce collateral by 300. That leaves her at 700 * 10 > 1.05 * 5000.
// Prices will drop from 10 to 7, she'll be liquidated: 700 * 7 < 1.05 * 5000.
const collateralDecrement = AmountMath.make(aethBrand, 300n);
aliceCollateralLevel = AmountMath.subtract(
aliceCollateralLevel,
collateralDecrement,
);
const aliceReduceCollateralSeat = await E(zoe).offer(
E(aliceVault).makeAdjustBalancesInvitation(),
harden({
Expand All @@ -1700,15 +1640,6 @@ test('mutable liquidity triggers and interest', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, aliceRunDebtLevel);
const aliceCollateralization4 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization4.numerator,
ceilMultiplyBy(
aliceCollateralLevel,
makeRatio(10n, runBrand, 1n, aethBrand),
),
);
t.deepEqual(aliceCollateralization4.denominator, aliceRunDebtLevel);

await manualTimer.tick();
// price levels changed and interest was charged.
Expand Down Expand Up @@ -1931,9 +1862,7 @@ test('close loan', async t => {

const aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, runDebtLevel);
const aliceCollateralization1 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(aliceCollateralization1.numerator.value, 15000n);
t.deepEqual(aliceCollateralization1.denominator.value, runDebtLevel.value);
t.deepEqual(aliceUpdate.value.locked, collateralAmount);

// Create a loan for Bob for 1000 RUN with 200 aeth collateral
const bobCollateralAmount = AmountMath.make(aethBrand, 200n);
Expand Down Expand Up @@ -2129,7 +2058,6 @@ test('mutable liquidity triggers and interest sensitivity', async t => {
const aliceDebtAmount = await E(aliceVault).getDebtAmount();
const fee = ceilMultiplyBy(aliceLoanAmount, rates.loanFee);
const aliceRunDebtLevel = AmountMath.add(aliceLoanAmount, fee);
let aliceCollateralLevel = AmountMath.make(aethBrand, 1000n);

t.deepEqual(aliceDebtAmount, aliceRunDebtLevel, 'vault lent 5000 RUN + fees');
const { RUN: aliceLentAmount } = await E(
Expand Down Expand Up @@ -2191,10 +2119,6 @@ test('mutable liquidity triggers and interest sensitivity', async t => {
// Alice reduce collateral by 300. That leaves her at 700 * 10 > 1.05 * 5000.
// Prices will drop from 10 to 7, she'll be liquidated: 700 * 7 < 1.05 * 5000.
const collateralDecrement = AmountMath.make(aethBrand, 211n);
aliceCollateralLevel = AmountMath.subtract(
aliceCollateralLevel,
collateralDecrement,
);
const aliceReduceCollateralSeat = await E(zoe).offer(
E(aliceVault).makeAdjustBalancesInvitation(),
harden({
Expand All @@ -2218,15 +2142,6 @@ test('mutable liquidity triggers and interest sensitivity', async t => {

aliceUpdate = await E(aliceNotifier).getUpdateSince();
t.deepEqual(aliceUpdate.value.debt, aliceRunDebtLevel);
const aliceCollateralization4 = aliceUpdate.value.collateralizationRatio;
t.deepEqual(
aliceCollateralization4.numerator,
ceilMultiplyBy(
aliceCollateralLevel,
makeRatio(10n, runBrand, 1n, aethBrand),
),
);
t.deepEqual(aliceCollateralization4.denominator, aliceRunDebtLevel);

await manualTimer.tick();
// price levels changed and interest was charged.
Expand Down

0 comments on commit 85228b7

Please sign in to comment.