-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
toward #6645
- Loading branch information
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
// }), | ||
// ), | ||
} | ||
} | ||
}; |