diff --git a/packages/inter-protocol/src/proposals/restorePSM.js b/packages/inter-protocol/src/proposals/restorePSM.js new file mode 100644 index 000000000000..f84167b4ea6d --- /dev/null +++ b/packages/inter-protocol/src/proposals/restorePSM.js @@ -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 {(amts: Amount[]) => Amount} */ +const amountSum = amts => amts.reduce((tot, a) => AmountMath.add(tot, a)); + +/** @template M @typedef {import('@agoric/governance/src/contractGovernance/typedParamManager.js').ParamRecordsFromTypes} ParamRecordsFromTypes */ +/** @typedef {import('../psm/psm.js').MetricsNotification} MetricsNotification */ + +/** + * @template M + * @template G + * @typedef {{ metrics: M, governance: ParamRecordsFromTypes }} GovernedContractState + */ + +/** + * @typedef {{ + * GiveMintedFee: 'ratio', + * WantMintedFee: 'ratio', + * MintLimit: 'amount', + * }} PSMParamTypesMap // TODO: factor out of psm.js + * + * @typedef {GovernedContractState} PSMPublishedState + * @typedef {Record>} PSMChainStorage + */ + +/** + * @param {ERef} stableIssuer + * @param {Amount<'nat'>[]} feeAmounts + * @param {ERef} zoe + * @param {ERef} 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 }, + // }), + // ), + } + } +};