Skip to content

Commit

Permalink
Publish Reserve econ data: mint & burn (#5753)
Browse files Browse the repository at this point in the history
* feature: publish Reserve econ data: mint & burn

closes: #4651

* chore: standardize names

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and turadg committed Jul 14, 2022
1 parent 6d21650 commit 6a9a978
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
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();
};

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

0 comments on commit 6a9a978

Please sign in to comment.