Skip to content

Commit

Permalink
Merge pull request #5704 from Agoric/ta/vault-off-chain
Browse files Browse the repository at this point in the history
5346 replace uses of makeNotifierKit with makeNotifierFromSubscriber
  • Loading branch information
mergify[bot] authored Jul 18, 2022
2 parents 12e1527 + ec40f84 commit 00e3c5c
Show file tree
Hide file tree
Showing 31 changed files with 236 additions and 148 deletions.
19 changes: 17 additions & 2 deletions packages/run-protocol/src/contractSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ export const checkDebtLimit = (debtLimit, totalDebt, toMint) => {

/**
* @template T
* @param {ERef<StorageNode>} [storageNode]
* @param {ERef<Marshaller>} [marshaller]
* @param {ERef<StorageNode>} storageNode
* @param {ERef<Marshaller>} marshaller
* @returns {MetricsPublisherKit<T>}
*/
export const makeMetricsPublisherKit = (storageNode, marshaller) => {
assert(
storageNode && marshaller,
'makeMetricsPublisherKit missing storageNode or marshaller',
);
/** @type {import('@agoric/notifier').StoredPublisherKit<T>} */
const kit = makeStoredPublisherKit(storageNode, marshaller, 'metrics');
return {
Expand Down Expand Up @@ -129,3 +133,14 @@ export const makeEphemeraProvider = init => {
};
};
harden(makeEphemeraProvider);

/**
* @template {Record<string, unknown>} T
* @param {T} specimen
* @param {Array<keyof T>} keyList
*/
export const assertKeysDefined = (specimen, keyList) => {
for (const key of keyList) {
assert(specimen[key], X`Missing ${q(key)}`);
}
};
10 changes: 5 additions & 5 deletions packages/run-protocol/src/proposals/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '@agoric/governance/exported.js';
import '@agoric/vats/exported.js';
import '@agoric/vats/src/core/types.js';
import { CENTRAL_ISSUER_NAME } from '@agoric/vats/src/core/utils.js';
import { getChildNode } from '@agoric/vats/src/lib-chainStorage.js';
import { makeStorageNode } from '@agoric/vats/src/lib-chainStorage.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { E, Far } from '@endo/far';
import * as Collect from '../collect.js';
Expand Down Expand Up @@ -215,7 +215,7 @@ export const setupAmm = async (
AmountMath.make(runBrand, minInitialPoolLiquidity),
);

const storageNode = await getChildNode(chainStorage, AMM_STORAGE_PATH);
const storageNode = await makeStorageNode(chainStorage, AMM_STORAGE_PATH);
const marshaller = await E(board).getPublishingMarshaller();

const ammGovernorTerms = {
Expand Down Expand Up @@ -309,7 +309,7 @@ export const setupReserve = async ({

const feeMintAccess = await feeMintAccessP;

const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const storageNode = await makeStorageNode(chainStorage, STORAGE_PATH);
const marshaller = E(board).getReadonlyMarshaller();

const reserveGovernorTerms = {
Expand Down Expand Up @@ -439,7 +439,7 @@ export const startVaultFactory = async (
const reservePublicFacet = await E(zoe).getPublicFacet(reserveInstance);
const timer = await chainTimerService;

const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const storageNode = await makeStorageNode(chainStorage, STORAGE_PATH);
const marshaller = E(board).getReadonlyMarshaller();

const vaultFactoryTerms = makeGovernedTerms(
Expand Down Expand Up @@ -844,7 +844,7 @@ export const startRunStake = async (
},
);

const storageNode = await getChildNode(chainStorage, STORAGE_PATH);
const storageNode = await makeStorageNode(chainStorage, STORAGE_PATH);
const marshaller = await E(board).getReadonlyMarshaller();

/** @type {{ publicFacet: GovernorPublic, creatorFacet: GovernedContractFacetAccess<unknown>}} */
Expand Down
4 changes: 2 additions & 2 deletions packages/run-protocol/src/proposals/startPSM.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
import { AmountMath, AssetKind } from '@agoric/ertp';
import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
import { getChildNode } from '@agoric/vats/src/lib-chainStorage.js';
import { makeStorageNode } from '@agoric/vats/src/lib-chainStorage.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/far';
import { reserveThenGetNamePaths } from './utils.js';
Expand Down Expand Up @@ -108,7 +108,7 @@ export const startPSM = async (
},
};

const storageNode = await getChildNode(chainStorage, 'psm');
const storageNode = await makeStorageNode(chainStorage, 'psm');
const marshaller = E(board).getPublishingMarshaller();

const governorFacets = await E(zoe).startInstance(
Expand Down
4 changes: 2 additions & 2 deletions packages/run-protocol/src/reserve/assetReserve.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const nonalphanumeric = /[^A-Za-z0-9]/g;
* @param {{
* feeMintAccess: FeeMintAccess,
* initialPoserInvitation: Payment,
* marshaller?: ERef<Marshaller>,
* storageNode?: ERef<StorageNode>,
* marshaller: ERef<Marshaller>,
* storageNode: ERef<StorageNode>,
* }} privateArgs
*/
const start = async (zcf, privateArgs) => {
Expand Down
9 changes: 5 additions & 4 deletions packages/run-protocol/src/runStake/runStake.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const { values } = Object;
* feeMintAccess: FeeMintAccess,
* initialPoserInvitation: Invitation,
* lienBridge: ERef<StakingAuthority>,
* storageNode?: StorageNode,
* marshaller?: Marshaller,
* storageNode: StorageNode,
* marshaller: Marshaller,
* }} RunStakePrivateArgs
*
* The creator facet can make an `AttestationMaker` for each account, which
Expand Down Expand Up @@ -98,6 +98,7 @@ export const start = async (
} = zcf.getTerms();
assert.typeof(chargingPeriod, 'bigint', 'chargingPeriod must be a bigint');
assert.typeof(recordingPeriod, 'bigint', 'recordingPeriod must be a bigint');
assert(storageNode && marshaller, 'missing storageNode or marshaller');

/** @type {ZCFMint<'nat'>} */
const debtMint = await zcf.registerFeeMint(KW.Debt, feeMintAccess);
Expand Down Expand Up @@ -192,8 +193,8 @@ export const start = async (

return harden({
publicNotifiers: {
asset: manager.getAssetNotifier(),
vault: pot.getNotifier(),
asset: manager.getAssetSubscriber(),
vault: pot.getSubscriber(),
},
invitationMakers: Far('invitation makers', {
AdjustBalances: () =>
Expand Down
26 changes: 13 additions & 13 deletions packages/run-protocol/src/runStake/runStakeKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { AmountMath, AssetKind } from '@agoric/ertp';
import { assertProposalShape } from '@agoric/zoe/src/contractSupport/index.js';
import { ceilMultiplyBy } from '@agoric/zoe/src/contractSupport/ratio.js';
import { makeNotifierKit } from '@agoric/notifier';
import { makePublishKit } from '@agoric/notifier';
import { M, matches } from '@agoric/store';
import { defineKindMulti } from '@agoric/vat-data';
import { makeTracer } from '../makeTracer.js';
Expand Down Expand Up @@ -35,15 +35,15 @@ const calculateFee = (feeCoeff, currentDebt, giveAmount, wantAmount) => {
* collateralBrand: Brand,
* debtBrand: Brand,
* manager: import('./runStakeManager.js').RunStakeManager,
* notifier: NotifierRecord<unknown>['notifier'],
* subscriber: Subscriber<unknown>
* vaultSeat: ZCFSeat,
* zcf: ZCF,
* }>} ImmutableState
* @typedef {{
* open: boolean,
* debtSnapshot: Amount<'nat'>,
* interestSnapshot: Ratio,
* updater: NotifierRecord<unknown>['updater'] | null,
* publisher: Publisher<unknown> | null,
* }} MutableState
* @typedef {MutableState & ImmutableState} State
* @typedef {{
Expand Down Expand Up @@ -111,14 +111,14 @@ const initState = (zcf, startSeat, manager) => {
})();
manager.applyDebtDelta(emptyDebt, initialDebt);

const { notifier, updater } = makeNotifierKit();
const { publisher, subscriber } = makePublishKit();

/** @type {ImmutableState} */
const immutable = {
collateralBrand,
debtBrand,
manager,
notifier,
subscriber,
vaultSeat,
zcf,
};
Expand All @@ -128,7 +128,7 @@ const initState = (zcf, startSeat, manager) => {
// Two values from the same moment
interestSnapshot: manager.getCompoundedInterest(),
debtSnapshot: initialDebt,
updater,
publisher,
};
};
const helperBehavior = {
Expand Down Expand Up @@ -183,19 +183,19 @@ const helperBehavior = {
* @param {MethodContext} context
*/
updateUiState: async ({ state, facets }) => {
const { open: active, updater } = state;
if (!updater) {
console.warn('updateUiState called after ui.updater removed');
const { open: active, publisher } = state;
if (!publisher) {
console.warn('updateUiState called after ui.publisher removed');
return;
}
const uiState = facets.helper.snapshotState(active);
trace('updateUiState', uiState);

if (active) {
updater.updateState(uiState);
publisher.publish(uiState);
} else {
updater.finish(uiState);
state.updater = null;
publisher.finish(uiState);
state.publisher = null;
}
},

Expand Down Expand Up @@ -362,7 +362,7 @@ const helperBehavior = {

const potBehavior = {
/** @param {MethodContext} context */
getNotifier: ({ state }) => state.notifier,
getSubscriber: ({ state }) => state.subscriber,
/** @param {MethodContext} context */
makeAdjustBalancesInvitation: ({ state, facets }) => {
const { zcf } = state;
Expand Down
28 changes: 15 additions & 13 deletions packages/run-protocol/src/runStake/runStakeManager.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @ts-check
// @jessie-check
import { AmountMath } from '@agoric/ertp';
import { makePublishKit, observeNotifier } from '@agoric/notifier';
import { fit, getCopyBagEntries, M } from '@agoric/store';
import { defineKindMulti } from '@agoric/vat-data';
import { floorMultiplyBy } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { fit, getCopyBagEntries, M } from '@agoric/store';
import { makeNotifierKit, observeNotifier } from '@agoric/notifier';
import { E } from '@endo/far';
import { defineKindMulti } from '@agoric/vat-data';
import { makeTracer } from '../makeTracer.js';
import { checkDebtLimit } from '../contractSupport.js';
import { chargeInterest } from '../interest.js';
import { makeTracer } from '../makeTracer.js';
import { ManagerKW as KW } from './constants.js';
import { checkDebtLimit } from '../contractSupport.js';

const { details: X } = assert;

Expand All @@ -29,8 +29,8 @@ const trace = makeTracer('RSM', false);
* totalDebt: Amount<'nat'>,
* }} AssetState
* @typedef {Readonly<{
* assetNotifier: Notifier<AssetState>,
* assetUpdater: IterationObserver<AssetState>,
* assetPublisher: Publisher<AssetState>,
* assetSubscriber: Subscriber<AssetState>,
* brands: { debt: Brand<'nat'>, Attestation: Brand<'copyBag'>, Stake: Brand<'nat'> },
* mintPowers: { burnDebt: BurnDebt, getGovernedParams: () => ParamManager, mintAndReallocate: MintAndReallocate },
* chargingPeriod: bigint,
Expand Down Expand Up @@ -74,7 +74,9 @@ const initState = (
const compoundedInterest = makeRatio(100n, brands.debt); // starts at 1.0, no interest
const latestInterestUpdate = startTimeStamp;

const { updater: assetUpdater, notifier: assetNotifier } = makeNotifierKit(
const { publisher: assetPublisher, subscriber: assetSubscriber } =
makePublishKit();
assetPublisher.publish(
harden({
compoundedInterest,
interestRate: mintPowers.getGovernedParams().getInterestRate(),
Expand All @@ -86,8 +88,8 @@ const initState = (
const { zcfSeat: poolIncrementSeat } = zcf.makeEmptySeatKit();

return {
assetNotifier,
assetUpdater,
assetSubscriber,
assetPublisher,
brands,
chargingPeriod,
compoundedInterest,
Expand Down Expand Up @@ -172,8 +174,8 @@ const helper = {
latestInterestUpdate: state.latestInterestUpdate,
totalDebt: state.totalDebt,
});
const { assetUpdater } = state;
assetUpdater.updateState(payload);
const { assetPublisher: assetUpdater } = state;
assetUpdater.publish(payload);

trace('chargeAllVaults complete', payload);
},
Expand Down Expand Up @@ -278,7 +280,7 @@ const manager = {
/** @param {MethodContext} context */
getCompoundedInterest: ({ state }) => state.compoundedInterest,
/** @param {MethodContext} context */
getAssetNotifier: ({ state }) => state.assetNotifier,
getAssetSubscriber: ({ state }) => state.assetSubscriber,
};

const behavior = { helper, manager };
Expand Down
2 changes: 1 addition & 1 deletion packages/run-protocol/src/vaultFactory/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const makeVaultDirectorParamManager = async (
};

/**
* @param {{storageNode?: ERef<StorageNode>, marshaller?: ERef<Marshaller>}} caps
* @param {{storageNode: ERef<StorageNode>, marshaller: ERef<Marshaller>}} caps
* @param {{
* electorateInvitationAmount: Amount,
* minInitialDebt: Amount,
Expand Down
10 changes: 5 additions & 5 deletions packages/run-protocol/src/vaultFactory/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const validTransitions = {
// XXX masks typedef from types.js, but using that causes circular def problems
/**
* @typedef {object} VaultManager
* @property {() => Notifier<import('./vaultManager').AssetState>} getNotifier
* @property {() => Subscriber<import('./vaultManager').AssetState>} getAssetSubscriber
* @property {(collateralAmount: Amount) => ERef<Amount<'nat'>>} maxDebtFor
* @property {() => Brand} getCollateralBrand
* @property {() => Brand<'nat'>} getDebtBrand
Expand All @@ -101,7 +101,7 @@ const validTransitions = {
*
* @typedef {{
* interestSnapshot: Ratio,
* outerUpdater: IterationObserver<VaultNotification> | null,
* outerUpdater: Publisher<VaultNotification> | null,
* phase: VaultPhase,
* debtSnapshot: Amount<'nat'>,
* }} MutableState
Expand Down Expand Up @@ -335,7 +335,7 @@ const helperBehavior = {
case Phase.ACTIVE:
case Phase.LIQUIDATING:
case Phase.LIQUIDATED:
outerUpdater.updateState(uiState);
outerUpdater.publish(uiState);
break;
case Phase.CLOSED:
outerUpdater.finish(uiState);
Expand Down Expand Up @@ -528,7 +528,7 @@ const helperBehavior = {
helper.assertCloseable();
seat.exit();
// eslint-disable-next-line no-use-before-define
const vaultKit = makeVaultKit(self, state.manager.getNotifier());
const vaultKit = makeVaultKit(self, state.manager.getAssetSubscriber());
state.outerUpdater = vaultKit.vaultUpdater;
helper.updateUiState();

Expand Down Expand Up @@ -596,7 +596,7 @@ const selfBehavior = {
state.manager.mintAndReallocate(toMint, fee, seat, vaultSeat);
helper.updateDebtAccounting(normalizedDebtPre, collateralPre, newDebtPre);

const vaultKit = makeVaultKit(self, state.manager.getNotifier());
const vaultKit = makeVaultKit(self, state.manager.getAssetSubscriber());
state.outerUpdater = vaultKit.vaultUpdater;
helper.updateUiState();
return vaultKit;
Expand Down
8 changes: 4 additions & 4 deletions packages/run-protocol/src/vaultFactory/vaultDirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const { details: X, quote: q } = assert;
* zcf: import('./vaultFactory.js').VaultFactoryZCF,
* shortfallInvitation: Invitation,
* shortfallReporter: import('../reserve/assetReserve.js').ShortfallReporter,
* storageNode?: ERef<StorageNode>,
* marshaller?: ERef<Marshaller>,
* storageNode: ERef<StorageNode>,
* marshaller: ERef<Marshaller>,
* }>} ImmutableState
*
* @typedef {{
Expand Down Expand Up @@ -117,8 +117,8 @@ const metricsOf = state => {
* @param {State['zcf']} zcf
* @param {State['directorParamManager']} directorParamManager
* @param {State['debtMint']} debtMint
* @param {ERef<StorageNode>} [storageNode]
* @param {ERef<Marshaller>} [marshaller]
* @param {ERef<StorageNode>} storageNode
* @param {ERef<Marshaller>} marshaller
* @returns {State}
*/
const initState = (
Expand Down
Loading

0 comments on commit 00e3c5c

Please sign in to comment.