Skip to content

Commit

Permalink
WIP: restorePSM
Browse files Browse the repository at this point in the history
toward #6645
  • Loading branch information
dckc committed Apr 24, 2023
1 parent fcebad4 commit 328c248
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions packages/inter-protocol/src/proposals/restorePSM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// @ts-check
import { AmountMath } from '@agoric/ertp';
import { Stable } from '@agoric/vats/src/tokens.js';
import { E } from '@endo/far';

const { entries, values } = Object;

/** @type {<K extends AssetKind>(amts: Amount<K>[]) => Amount<K>} */
const amountSum = amts => amts.reduce((tot, a) => AmountMath.add(tot, a));

/** @template M @typedef {import('@agoric/governance/src/contractGovernance/typedParamManager.js').ParamRecordsFromTypes<M>} ParamRecordsFromTypes<M> */
/** @typedef {import('../psm/psm.js').MetricsNotification} MetricsNotification */

/**
* @template M
* @template G
* @typedef {{ metrics: M, governance: ParamRecordsFromTypes<G> }} GovernedContractState
*/

/**
* @typedef {{
* GiveMintedFee: 'ratio',
* WantMintedFee: 'ratio',
* MintLimit: 'amount',
* }} PSMParamTypesMap // TODO: factor out of psm.js
*
* @typedef {GovernedContractState<MetricsNotification, PSMParamTypesMap>} PSMPublishedState
* @typedef {Record<string, Record<string, PSMPublishedState>>} PSMChainStorage
*/

/**
* @param {ERef<Issuer>} stableIssuer
* @param {Amount<'nat'>[]} feeAmounts
* @param {ERef<ZoeService>} zoe
* @param {ERef<Installation>} centralSupply
* @param {unknown} feeMintAccess
*/
const mintTotalFees = async (
stableIssuer,
feeAmounts,
zoe,
centralSupply,
feeMintAccess,
) => {
const feePurse = E(stableIssuer).makeEmptyPurse();

const { creatorFacet: supplier } = await E(zoe).startInstance(
centralSupply,
{ [Stable.symbol]: stableIssuer },
{ bootstrapPaymentValue: amountSum(feeAmounts) },
{ feeMintAccess },
);
await E(supplier)
.getBootstrapPayment()
.then(pmt => E(feePurse).deposit(pmt));
return feePurse;
};

/**
* @param {PSMChainStorage} before
* @param {BootstrapPowers} powers
*/
export const restorePSM = async (before, powers) => {
const {
consume: { feeMintAccess, zoe },
installation: {
consume: { centralSupply },
},
issuer: {
consume: { [Stable.symbol]: stableIssuer },
},
brand: {
consume: { [Stable.symbol]: stableBrandP },
},
} = powers;

const stableBrand = await stableBrandP;
const feeAmounts = values(before).flatMap(a =>
values(a).map(({ metrics }) => metrics.feePoolBalance),
);

const feePurse = await mintTotalFees(
stableIssuer,
feeAmounts,
zoe,
centralSupply,
feeMintAccess,
);

for await (const [keyStable, anchors] of entries(before)) {
for await (const [keyAnchor, { metrics, governance }] of entries(anchors)) {
console.log('restoring', { keyStable, keyAnchor, metrics, governance });

const feePoolPmt = await E(feePurse).withdraw(metrics.feePoolBalance);
metrics.mintedPoolBalance;
/**
*
mint the balance of the anchor asset (e.g. USDC) - (note Jan 18 design sketch below)
*/
// from packages/vats/src/core/boot-psm.js
//
// ...anchorAssets.map(anchorOptions =>
// makeAnchorAsset(powersFor('makeAnchorAsset'), {
// options: { anchorOptions },
// }),
// ),
// ...anchorAssets.map(anchorOptions =>
// startPSM(powersFor('startPSM'), {
// options: { anchorOptions },
// }),
// ),
}
}
};

0 comments on commit 328c248

Please sign in to comment.