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

feat(vault): econ metrics notifiers #5260

Merged
merged 44 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
9cedbdc
chore(vault): type VaultUIState -> VaultTitleState
turadg May 2, 2022
5334672
chore(zoe): type setupZCFTest
turadg May 3, 2022
af797d9
chore(vault): disambiguate VaultManager type (facet naming confusion)
turadg May 3, 2022
b4a5fcf
feat(vault): vaultDirector econ notifier
turadg Apr 29, 2022
696b651
feat(vault): vaultManager econNotifier
turadg May 2, 2022
8fc22d7
feat(vault): number of vaults per manager in EconState
turadg May 2, 2022
4a0d2d6
style: fix 'object' type casing
turadg May 9, 2022
a6f5d8b
fixup! chore(vault): type VaultUIState -> VaultTitleState
turadg May 9, 2022
ae03243
'econ' -> 'metrics' (will squash)
turadg May 9, 2022
d0c7612
fixup! chore(vault): disambiguate VaultManager type (facet naming con…
turadg May 9, 2022
c5bd54d
size -> count
turadg May 9, 2022
e4401c1
undo name change in test
turadg May 9, 2022
424776f
lint
turadg May 9, 2022
d9c0607
handleFooOffer style per https://github.com/Agoric/agoric-sdk/pull/5179
turadg May 10, 2022
024c205
chore(zoe): more typing offerTo and Invitation
turadg May 10, 2022
0cb7b4b
plan
turadg May 10, 2022
b13aba1
factored penalties into liquidation contracts
turadg May 10, 2022
58d8555
rm obsolete partitionProceeds
turadg May 11, 2022
276c94c
lint and comments
turadg May 12, 2022
c44cd1e
named parameters for makeGovernedTerms()
turadg May 12, 2022
6a3349e
wire reservePublicFacet into vaultFactory terms
turadg May 13, 2022
86dea15
rm obsolete penaltyPoolSeat
turadg May 13, 2022
db7194b
fixup! wire reservePublicFacet into vaultFactory terms
turadg May 13, 2022
db9e406
fixup! rm obsolete penaltyPoolSeat
turadg May 13, 2022
4c92859
consume reserve instance instead of facet
turadg May 13, 2022
fff80b0
typecheck liquidator,vaultFactory tests
turadg May 13, 2022
9e30183
fix test typo tsc caught
turadg May 13, 2022
d8fba65
fix invitation param and typecheck that would have caught it
turadg May 13, 2022
74460d2
update comment docs
turadg May 13, 2022
bbb4ea9
work around type resolution shortcomings
turadg May 16, 2022
01c3fe6
better comments
turadg May 16, 2022
96c9811
Merge branch 'master' into 5167-liq-penalty
turadg May 16, 2022
443a932
Merge branch 'master' into 4649-expose-vaultFactor-econ-metrics
turadg May 16, 2022
58f8d9d
fixup undo vaultFactoryCreator
turadg May 16, 2022
3fe00a4
getMetrics
turadg May 16, 2022
30a8eaf
use subscriptions
turadg May 16, 2022
9dd27d9
revert rename getNotifier (no ambiguity anymore)
turadg May 16, 2022
905e07c
fix lint my editor didn't show 🤷
turadg May 16, 2022
d1b2344
fixup! getMetrics
turadg May 16, 2022
41fa373
update metrics after each liquidation
turadg May 16, 2022
d3bc2d9
Merge branch '5167-liq-penalty' into 4649-expose-vaultFactor-econ-met…
turadg May 16, 2022
fc54589
fix test for manager notifiers
turadg May 17, 2022
cf8030d
Merge branch 'master' into 4649-expose-vaultFactor-econ-metrics
turadg May 17, 2022
38219e8
Merge branch 'master' into 4649-expose-vaultFactor-econ-metrics
mergify[bot] May 18, 2022
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
2 changes: 1 addition & 1 deletion packages/run-protocol/src/runStake/runStakeKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const helperBehavior = {
snapshotState: ({ state, facets }, newActive) => {
const { debtSnapshot: debt, interestSnapshot: interest, manager } = state;
const { helper } = facets;
/** @type {VaultUIState} */
/** @type {VaultNotification} */
const result = harden({
// TODO move manager state to a separate notifer https://github.com/Agoric/agoric-sdk/issues/4540
interestRate: manager.getInterestRate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const makePrioritizedVaults = (reschedulePriceCheck = () => {}) => {
addVault,
entries: vaults.entries,
entriesPrioritizedGTE,
getCount: vaults.getSize,
highestRatio: firstDebtRatio,
refreshVaultPriority,
removeVault,
Expand Down
3 changes: 2 additions & 1 deletion packages/run-protocol/src/vaultFactory/types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

/**
* @typedef {import('./vault').VaultUIState} VaultUIState
* @typedef {import('./vault').VaultNotification} VaultNotification
* @typedef {import('./vault').Vault} Vault
* @typedef {import('./vaultKit').VaultKit} VaultKit
* @typedef {import('./vaultManager').VaultManager} VaultManager
Expand Down Expand Up @@ -51,6 +51,7 @@
* @property {() => Allocation} getRewardAllocation
* @property {() => Instance} getContractGovernor
* @property {() => Promise<Invitation>} makeCollectFeesInvitation
* @property {() => void} updateMetrics
*/

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/run-protocol/src/vaultFactory/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ const validTransitions = {
/**
* @typedef {Phase[keyof typeof Phase]} TitlePhase
*
* @typedef {object} VaultUIState
Copy link
Member

Choose a reason for hiding this comment

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

We need a general term for the state snapshot from a notifier. We could remove "UI" from that with "vaultSnapshot"? but "vaultState" isn't terrible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Came up above and proposed Notification. I could see that being used for the wrapping object (with value and updateCount) but that's so generic as to not need a name.

* @typedef {object} VaultNotification
* @property {Amount<'nat'>} locked Amount of Collateral locked
* @property {{debt: Amount<'nat'>, interest: Ratio}} debtSnapshot 'debt' at the point the compounded interest was 'interest'
* @property {Ratio} interestRate Annual interest rate charge
* @property {Ratio} liquidationRatio
* @property {TitlePhase} vaultState
*/

// XXX masks typedef from types.js, but using that causes circular def problems
/**
* @typedef {object} VaultManager
* @property {() => Notifier<import('./vaultManager').AssetState>} getNotifier
Expand Down Expand Up @@ -101,7 +102,7 @@ const validTransitions = {
*
* @typedef {{
* interestSnapshot: Ratio,
* outerUpdater: IterationObserver<VaultUIState> | null,
* outerUpdater: IterationObserver<VaultNotification> | null,
* phase: VaultPhase,
* debtSnapshot: Amount<'nat'>,
* }} MutableState
Expand Down Expand Up @@ -302,7 +303,7 @@ const helperBehavior = {
*/
getStateSnapshot: ({ state, facets }, newPhase) => {
const { debtSnapshot: debt, interestSnapshot: interest } = state;
/** @type {VaultUIState} */
/** @type {VaultNotification} */
return harden({
// TODO move manager state to a separate notifer https://github.com/Agoric/agoric-sdk/issues/4540
interestRate: state.manager.getGovernedParams().getInterestRate(),
Expand Down
37 changes: 32 additions & 5 deletions packages/run-protocol/src/vaultFactory/vaultDirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Far } from '@endo/marshal';
import { AmountMath } from '@agoric/ertp';
import { assertKeywordName } from '@agoric/zoe/src/cleanProposal.js';
import { defineKindMulti } from '@agoric/vat-data';
import { observeIteration } from '@agoric/notifier';
import { makeSubscriptionKit, observeIteration } from '@agoric/notifier';
import { makeVaultManager } from './vaultManager.js';
import { makeMakeCollectFeesInvitation } from '../collectFees.js';
import {
Expand All @@ -31,11 +31,18 @@ import {
const { details: X } = assert;

/**
* @typedef {{
* collaterals: Brand[],
* rewardPoolAllocation: AmountKeywordRecord,
* }} MetricsNotification
*
* @typedef {Readonly<{
* debtMint: ZCFMint<'nat'>,
* collateralTypes: Store<Brand,VaultManager>,
* electionManager: Instance,
* directorParamManager: import('@agoric/governance/src/contractGovernance/typedParamManager').TypedParamManager<import('./params.js').VaultDirectorParams>,
* metricsPublication: IterationObserver<MetricsNotification>
* metricsSubscription: Subscription<MetricsNotification>
* mintSeat: ZCFSeat,
* rewardPoolSeat: ZCFSeat,
* vaultParamManagers: Store<Brand, import('./params.js').VaultParamManager>,
Expand Down Expand Up @@ -68,10 +75,15 @@ const initState = (zcf, directorParamManager, debtMint) => {

const vaultParamManagers = makeScalarMap('brand');

const { publication: metricsPublication, subscription: metricsSubscription } =
makeSubscriptionKit();

return {
collateralTypes,
debtMint,
directorParamManager,
metricsSubscription,
metricsPublication,
mintSeat,
rewardPoolSeat,
vaultParamManagers,
Expand Down Expand Up @@ -149,9 +161,8 @@ const getCollaterals = async ({ state }) => {
),
);
};

/**
* @param {import('@agoric/governance/src/contractGovernance/typedParamManager').TypedParamManager<import('./params.js').VaultDirectorParams>} directorParamManager
* @param {ImmutableState['directorParamManager']} directorParamManager
*/
const getLiquidationConfig = directorParamManager => ({
install: directorParamManager.getLiquidationInstall(),
Expand All @@ -160,7 +171,7 @@ const getLiquidationConfig = directorParamManager => ({

/**
*
* @param {*} govParams
* @param {ImmutableState['directorParamManager']} govParams
* @param {VaultManager} vaultManager
* @param {*} oldInstall
* @param {*} oldTerms
Expand Down Expand Up @@ -192,7 +203,7 @@ const machineBehavior = {
* @param {VaultManagerParamValues} initialParamValues
*/
addVaultType: async (
{ state },
{ state, facets },
collateralIssuer,
collateralKeyword,
initialParamValues,
Expand Down Expand Up @@ -256,6 +267,7 @@ const machineBehavior = {
}
// TODO add aggregate debt tracking at the vaultFactory level #4482
// totalDebt = AmountMath.add(totalDebt, toMint);
facets.machine.updateMetrics();
};

/**
Expand Down Expand Up @@ -291,6 +303,7 @@ const machineBehavior = {
const { install, terms } = getLiquidationConfig(directorParamManager);
await vm.setupLiquidator(install, terms);
watchGovernance(directorParamManager, vm, install, terms);
facets.machine.updateMetrics();
return vm;
},
getCollaterals,
Expand All @@ -306,6 +319,15 @@ const machineBehavior = {
},
/** @param {MethodContext} context */
getContractGovernor: ({ state }) => state.zcf.getTerms().electionManager,
/** @param {MethodContext} context */
updateMetrics: ({ state }) => {
/** @type {MetricsNotification} */
const metrics = harden({
collaterals: Array.from(state.collateralTypes.keys()),
rewardPoolAllocation: state.rewardPoolSeat.getCurrentAllocation(),
});
state.metricsPublication.updateState(metrics);
},

// XXX accessors for tests
/** @param {MethodContext} context */
Expand Down Expand Up @@ -356,6 +378,11 @@ const publicBehavior = {
/** @type {VaultManager} */
return collateralTypes.get(brandIn).getPublicFacet();
},
/**
* @param {MethodContext} context
*/
getMetrics: ({ state }) => state.metricsSubscription,

/** @deprecated use getCollateralManager and then makeVaultInvitation instead */
makeLoanInvitation: makeVaultInvitation,
/** @deprecated use getCollateralManager and then makeVaultInvitation instead */
Expand Down
Loading