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

Publish Reserve econ data: mint & burn #5753

Merged
merged 3 commits into from
Jul 13, 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
12 changes: 12 additions & 0 deletions packages/run-protocol/src/reserve/assetReserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const nonalphanumeric = /[^A-Za-z0-9]/g;
*
* @property {AmountKeywordRecord} allocations
* @property {Amount<'nat'>} shortfallBalance shortfall from liquiditation that
* @property {Amount<'nat'>} totalFeeMinted total RUN minted to date
* @property {Amount<'nat'>} totalFeeBurned total RUN burned to date
* has not yet been compensated.
*/

Expand Down Expand Up @@ -222,6 +224,9 @@ const start = async (zcf, privateArgs) => {

const { brand: runBrand } = await E(runMint).getIssuerRecord();

let totalFeeMinted = AmountMath.makeEmpty(runBrand);
let totalFeeBurned = AmountMath.makeEmpty(runBrand);

// shortfall in Vaults due to liquidations less than debt. This value can be
// reduced by various actions which burn RUN.
let shortfallBalance = AmountMath.makeEmpty(runBrand);
Expand All @@ -236,6 +241,8 @@ const start = async (zcf, privateArgs) => {
const metrics = harden({
allocations: getAllocations(),
shortfallBalance,
totalFeeMinted,
totalFeeBurned,
});
metricsPublication.updateState(metrics);
};
Expand Down Expand Up @@ -270,6 +277,8 @@ const start = async (zcf, privateArgs) => {

// create the RUN
const offerToSeat = runMint.mintGains(harden({ RUN: runAmount }));
totalFeeMinted = AmountMath.add(totalFeeMinted, runAmount);

offerToSeat.incrementBy(
collateralSeat.decrementBy(
harden({
Expand Down Expand Up @@ -316,6 +325,7 @@ const start = async (zcf, privateArgs) => {
}),
);
zcf.reallocate(offerToSeat, collateralSeat);
updateMetrics();
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed here but not after burnedRUN change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is already called in burnRUNToReduceShortfall, because that calls increaseLiquidationShortfall. addLiquiditytoAmmPool mints, but doesn't adjust the shortfall. The shortfall is increased by other contracts that get a shortfallReportingInvitation.

};

const burnRUNToReduceShortfall = reduction => {
Expand All @@ -328,6 +338,8 @@ const start = async (zcf, privateArgs) => {
}

runMint.burnLosses(harden({ [runKeyword]: amountToBurn }), collateralSeat);
totalFeeBurned = AmountMath.add(totalFeeBurned, amountToBurn);

reduceLiquidationShortfall(amountToBurn);
};

Expand Down
6 changes: 6 additions & 0 deletions packages/run-protocol/test/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ export const vaultManagerMetricsTracker = async (t, publicFacet) => {
assertFullyLiquidated,
});
};
export const reserveInitialState = emptyRun => ({
allocations: {},
shortfallBalance: emptyRun,
totalFeeBurned: emptyRun,
totalFeeMinted: emptyRun,
});
25 changes: 16 additions & 9 deletions packages/run-protocol/test/reserve/test-reserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js';

import { setupReserveServices } from './setup.js';
import { unsafeMakeBundleCache } from '../bundleTool.js';
import { subscriptionTracker } from '../metrics.js';
import { reserveInitialState, subscriptionTracker } from '../metrics.js';
import { subscriptionKey } from '../supports.js';

const addLiquidPool = async (
Expand Down Expand Up @@ -173,6 +173,10 @@ test('governance add Liquidity to the AMM', async t => {
'should be 80K',
);

const metricsSub = await E(reserve.reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial(reserveInitialState(AmountMath.makeEmpty(runBrand)));

const invitation = await E(
reserve.reservePublicFacet,
).makeAddCollateralInvitation();
Expand Down Expand Up @@ -230,6 +234,14 @@ test('governance add Liquidity to the AMM', async t => {
}),
'should be 80K',
);

await m.assertChange({
totalFeeMinted: { value: 80_000n },
allocations: {
Rmoola: moola(10_000n),
RmoolaLiquidity: AmountMath.make(moolaLiquidityBrand, 85_622n),
},
});
});

test('request more collateral than available', async t => {
Expand Down Expand Up @@ -335,10 +347,7 @@ test('reserve track shortfall', async t => {

const metricsSub = await E(reserve.reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial({
allocations: {},
shortfallBalance: AmountMath.makeEmpty(runBrand),
});
await m.assertInitial(reserveInitialState(AmountMath.makeEmpty(runBrand)));
await m.assertChange({
shortfallBalance: { value: runningShortfall },
});
Expand Down Expand Up @@ -390,10 +399,7 @@ test('reserve burn IST', async t => {

const metricsSub = await E(reserve.reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial({
allocations: {},
shortfallBalance: AmountMath.makeEmpty(runBrand),
});
await m.assertInitial(reserveInitialState(AmountMath.makeEmpty(runBrand)));
await m.assertChange({
shortfallBalance: { value: runningShortfall },
});
Expand Down Expand Up @@ -454,6 +460,7 @@ test('reserve burn IST', async t => {
value: runningShortfall,
},
allocations: { RUN: AmountMath.makeEmpty(runBrand) },
totalFeeBurned: { value: 1000n },
});
});

Expand Down
17 changes: 4 additions & 13 deletions packages/run-protocol/test/vaultFactory/test-vaultFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
metricsTracker,
vaultManagerMetricsTracker,
subscriptionTracker,
reserveInitialState,
} from '../metrics.js';

/** @typedef {Record<string, any> & {
Expand Down Expand Up @@ -637,10 +638,7 @@ test('price drop', async t => {

const metricsSub = await E(reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial({
allocations: {},
shortfallBalance: run.makeEmpty(),
});
await m.assertInitial(reserveInitialState(run.makeEmpty()));

const debtAmountAfter = await E(vault).getCurrentDebt();
const finalNotification = await E(vaultNotifier).getUpdateSince();
Expand Down Expand Up @@ -764,11 +762,7 @@ test('price falls precipitously', async t => {

const metricsSub = await E(reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial({
allocations: {},
shortfallBalance: run.makeEmpty(),
});

await m.assertInitial(reserveInitialState(run.makeEmpty()));
await manualTimer.tick();
await assertDebtIs(debtAmount.value);

Expand Down Expand Up @@ -1741,10 +1735,7 @@ test('mutable liquidity triggers and interest', async t => {

const metricsSub = await E(reserveCreatorFacet).getMetrics();
const m = await subscriptionTracker(t, metricsSub);
await m.assertInitial({
allocations: {},
shortfallBalance: run.makeEmpty(),
});
await m.assertInitial(reserveInitialState(run.makeEmpty()));
let shortfallBalance = 0n;

// initial loans /////////////////////////////////////
Expand Down