@agoric/inter-protocol 0.13.3 → 0.13.4-dev-d32a66a.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +26 -20
  2. package/bundles/bundle-auctioneer-js-meta.json +722 -0
  3. package/bundles/bundle-auctioneer.js +1 -0
  4. package/bundles/bundle-econCommitteeCharter-js-meta.json +514 -0
  5. package/bundles/bundle-econCommitteeCharter.js +1 -0
  6. package/bundles/bundle-feeDistributor-js-meta.json +458 -0
  7. package/bundles/bundle-feeDistributor.js +1 -1
  8. package/bundles/bundle-fluxAggregatorKit-js-meta.json +674 -0
  9. package/bundles/bundle-fluxAggregatorKit.js +1 -0
  10. package/bundles/bundle-psm-js-meta.json +666 -0
  11. package/bundles/bundle-psm.js +1 -1
  12. package/bundles/bundle-reserve-js-meta.json +662 -0
  13. package/bundles/bundle-reserve.js +1 -1
  14. package/bundles/bundle-scaledPriceAuthority-js-meta.json +542 -0
  15. package/bundles/bundle-scaledPriceAuthority.js +1 -1
  16. package/bundles/bundle-vaultFactory-js-meta.json +786 -0
  17. package/bundles/bundle-vaultFactory.js +1 -1
  18. package/exported.js +0 -2
  19. package/package.json +34 -31
  20. package/scripts/add-collateral-core.js +14 -4
  21. package/scripts/build-bundles.js +9 -2
  22. package/scripts/deploy-contracts.js +1 -4
  23. package/scripts/init-core.js +20 -38
  24. package/scripts/invite-committee-core.js +1 -1
  25. package/scripts/manual-price-feed.js +2 -2
  26. package/scripts/price-feed-core.js +7 -6
  27. package/scripts/start-local-chain.sh +84 -0
  28. package/src/auction/auctionBook.js +755 -0
  29. package/src/auction/auctioneer.js +720 -0
  30. package/src/auction/offerBook.js +221 -0
  31. package/src/auction/params.js +174 -0
  32. package/src/auction/scheduleMath.js +167 -0
  33. package/src/auction/scheduler.js +370 -0
  34. package/src/auction/sortedOffers.js +136 -0
  35. package/src/auction/util.js +55 -0
  36. package/src/clientSupport.js +324 -0
  37. package/src/collectFees.js +8 -14
  38. package/src/contractSupport.js +24 -68
  39. package/src/econCommitteeCharter.js +161 -77
  40. package/src/feeDistributor.js +60 -20
  41. package/src/index.js +2 -0
  42. package/src/interest-math.js +1 -4
  43. package/src/interest.js +20 -13
  44. package/src/price/README.md +57 -0
  45. package/src/price/fluxAggregatorContract.js +190 -0
  46. package/src/price/fluxAggregatorKit.js +375 -0
  47. package/src/price/priceOracleKit.js +127 -0
  48. package/src/price/roundsManager.js +726 -0
  49. package/src/proposals/addAssetToVault.js +89 -94
  50. package/src/proposals/committee-proposal.js +141 -40
  51. package/src/proposals/core-proposal.js +100 -167
  52. package/src/proposals/econ-behaviors.js +320 -564
  53. package/src/proposals/price-feed-proposal.js +175 -78
  54. package/src/proposals/startEconCommittee.js +25 -12
  55. package/src/proposals/startPSM.js +231 -211
  56. package/src/proposals/utils.js +31 -17
  57. package/src/psm/README.md +71 -0
  58. package/src/psm/psm.js +272 -160
  59. package/src/psm/types.js +2 -0
  60. package/src/reserve/assetReserve.js +56 -413
  61. package/src/reserve/assetReserveKit.js +303 -0
  62. package/src/reserve/doc.md +1 -3
  63. package/src/reserve/params.js +2 -5
  64. package/src/typeGuards.js +13 -0
  65. package/src/vaultFactory/Liquidation.md +100 -0
  66. package/src/vaultFactory/burn.js +2 -1
  67. package/src/vaultFactory/liquidation.js +270 -108
  68. package/src/vaultFactory/math.js +95 -0
  69. package/src/vaultFactory/orderedVaultStore.js +0 -1
  70. package/src/vaultFactory/params.js +133 -112
  71. package/src/vaultFactory/prioritizedVaults.js +36 -79
  72. package/src/vaultFactory/storeUtils.js +67 -9
  73. package/src/vaultFactory/type-imports.js +2 -0
  74. package/src/vaultFactory/types.js +22 -26
  75. package/src/vaultFactory/vault.js +772 -709
  76. package/src/vaultFactory/vaultDirector.js +480 -483
  77. package/src/vaultFactory/vaultFactory.js +99 -60
  78. package/src/vaultFactory/vaultHolder.js +121 -95
  79. package/src/vaultFactory/vaultKit.js +39 -31
  80. package/src/vaultFactory/vaultManager.js +1249 -868
  81. package/bundles/bundle-amm-meta.js +0 -618
  82. package/bundles/bundle-amm.js +0 -1
  83. package/bundles/bundle-feeDistributor-meta.js +0 -346
  84. package/bundles/bundle-interchainPool-meta.js +0 -534
  85. package/bundles/bundle-interchainPool.js +0 -1
  86. package/bundles/bundle-liquidateIncrementally-meta.js +0 -442
  87. package/bundles/bundle-liquidateIncrementally.js +0 -1
  88. package/bundles/bundle-liquidateMinimum-meta.js +0 -442
  89. package/bundles/bundle-liquidateMinimum.js +0 -1
  90. package/bundles/bundle-psm-meta.js +0 -538
  91. package/bundles/bundle-psmCharter-meta.js +0 -418
  92. package/bundles/bundle-psmCharter.js +0 -1
  93. package/bundles/bundle-reserve-meta.js +0 -542
  94. package/bundles/bundle-scaledPriceAuthority-meta.js +0 -442
  95. package/bundles/bundle-stakeFactory-meta.js +0 -574
  96. package/bundles/bundle-stakeFactory.js +0 -1
  97. package/bundles/bundle-vaultFactory-meta.js +0 -598
  98. package/scripts/add-initial-liquidity.js +0 -180
  99. package/src/collect.js +0 -13
  100. package/src/interchainPool.js +0 -135
  101. package/src/makeTracer.js +0 -36
  102. package/src/my-lien.js +0 -85
  103. package/src/proposals/demoIssuers.js +0 -685
  104. package/src/proposals/sim-behaviors.js +0 -40
  105. package/src/psm/psmCharter.js +0 -139
  106. package/src/stakeFactory/README.md +0 -62
  107. package/src/stakeFactory/attestation.js +0 -267
  108. package/src/stakeFactory/attestationTool.js +0 -66
  109. package/src/stakeFactory/constants.js +0 -16
  110. package/src/stakeFactory/params.js +0 -31
  111. package/src/stakeFactory/stakeFactory.js +0 -236
  112. package/src/stakeFactory/stakeFactoryKit.js +0 -455
  113. package/src/stakeFactory/stakeFactoryManager.js +0 -296
  114. package/src/stakeFactory/types.js +0 -42
  115. package/src/vaultFactory/liquidateIncrementally.js +0 -350
  116. package/src/vaultFactory/liquidateMinimum.js +0 -100
  117. package/src/vpool-xyk-amm/AttackersGuide.md +0 -89
  118. package/src/vpool-xyk-amm/addLiquidity.js +0 -222
  119. package/src/vpool-xyk-amm/addPool.js +0 -197
  120. package/src/vpool-xyk-amm/constantProduct/AMM-Trade.jpeg +0 -0
  121. package/src/vpool-xyk-amm/constantProduct/README.md +0 -204
  122. package/src/vpool-xyk-amm/constantProduct/calcFees.js +0 -99
  123. package/src/vpool-xyk-amm/constantProduct/calcSwapPrices.js +0 -85
  124. package/src/vpool-xyk-amm/constantProduct/core.js +0 -153
  125. package/src/vpool-xyk-amm/constantProduct/defaults.js +0 -5
  126. package/src/vpool-xyk-amm/constantProduct/getXY.js +0 -52
  127. package/src/vpool-xyk-amm/constantProduct/internal-types.js +0 -116
  128. package/src/vpool-xyk-amm/constantProduct/invariants.js +0 -43
  129. package/src/vpool-xyk-amm/constantProduct/swap.js +0 -272
  130. package/src/vpool-xyk-amm/constantProduct/types.js +0 -32
  131. package/src/vpool-xyk-amm/doublePool.js +0 -194
  132. package/src/vpool-xyk-amm/estimate.js +0 -155
  133. package/src/vpool-xyk-amm/multipoolMarketMaker.js +0 -520
  134. package/src/vpool-xyk-amm/params.js +0 -61
  135. package/src/vpool-xyk-amm/pool.js +0 -396
  136. package/src/vpool-xyk-amm/priceAuthority.js +0 -94
  137. package/src/vpool-xyk-amm/removeLiquidity.js +0 -31
  138. package/src/vpool-xyk-amm/singlePool.js +0 -73
  139. package/src/vpool-xyk-amm/swap.js +0 -69
  140. package/src/vpool-xyk-amm/types.js +0 -154
@@ -1,32 +1,14 @@
1
- // @ts-check
1
+ // @jessie-check
2
2
 
3
- import { E, Far } from '@endo/far';
4
- import { AmountMath } from '@agoric/ertp';
5
- import { handleParamGovernance, ParamTypes } from '@agoric/governance';
6
- import { offerTo } from '@agoric/zoe/src/contractSupport/index.js';
7
- import { provideDurableMapStore, vivifyKindMulti } from '@agoric/vat-data';
3
+ import { handleParamGovernance } from '@agoric/governance';
4
+ import { makeTracer } from '@agoric/internal';
5
+ import {
6
+ prepareRecorderKitMakers,
7
+ provideAll,
8
+ } from '@agoric/zoe/src/contractSupport/index.js';
9
+ import { prepareAssetReserveKit } from './assetReserveKit.js';
8
10
 
9
- import { AMM_INSTANCE } from './params.js';
10
- import { makeTracer } from '../makeTracer.js';
11
- import { makeMetricsPublisherKit } from '../contractSupport.js';
12
-
13
- const { details: X, quote: q } = assert;
14
-
15
- const trace = makeTracer('Reserve', false);
16
-
17
- const makeLiquidityKeyword = keyword => `${keyword}Liquidity`;
18
-
19
- const nonalphanumeric = /[^A-Za-z0-9]/g;
20
-
21
- /**
22
- * @typedef {object} MetricsNotification
23
- *
24
- * @property {AmountKeywordRecord} allocations
25
- * @property {Amount<'nat'>} shortfallBalance shortfall from liquidation that
26
- * has not yet been compensated.
27
- * @property {Amount<'nat'>} totalFeeMinted total Fee tokens minted to date
28
- * @property {Amount<'nat'>} totalFeeBurned total Fee tokens burned to date
29
- */
11
+ const trace = makeTracer('AR', true);
30
12
 
31
13
  /**
32
14
  * @typedef {{
@@ -35,73 +17,41 @@ const nonalphanumeric = /[^A-Za-z0-9]/g;
35
17
  * }} ShortfallReportingFacet
36
18
  */
37
19
 
38
- /**
39
- * @typedef {{
40
- * state: {
41
- * totalFeeMinted: Amount<'nat'>,
42
- * },
43
- * facets: {
44
- * publicFacet: {},
45
- * creatorFacet: {},
46
- * shortfallReportingFacet: ShortfallReportingFacet,
47
- * limitedCreatorFacet: {},
48
- * governedApis: {},
49
- * },
50
- * }} MethodContext
51
- */
52
-
53
20
  /** @typedef {import('@agoric/vat-data').Baggage} Baggage */
54
21
 
55
22
  /**
56
23
  * Asset Reserve holds onto assets for the Inter Protocol, and can
57
- * dispense it for various purposes under governance control. It currently
58
- * supports governance decisions to add liquidity to an AMM pool.
24
+ * dispense it for various purposes under governance control.
59
25
  *
60
26
  * This contract has the ability to mint Fee tokens, granted through its private
61
- * arguments. When adding liquidity to an AMM pool, it mints new Fee tokens and
62
- * merges them with the specified amount of collateral on hand. It then deposits
63
- * both into an AMM pool by using the AMM's method that allows the pool balance
64
- * to be determined based on the contributed funds.
27
+ * arguments.
65
28
  *
66
- * @param {ZCF<GovernanceTerms<{AmmInstance: 'instance'}> &
29
+ * @param {ZCF<GovernanceTerms<{}> &
67
30
  * {
68
- * governedApis: ['addLiquidityToAmmPool'],
31
+ * governedApis: ['burnFeesToReduceShortfall'],
69
32
  * }
70
33
  * >} zcf
71
34
  * @param {{
72
35
  * feeMintAccess: FeeMintAccess,
73
- * initialPoserInvitation: Payment,
36
+ * initialPoserInvitation: Invitation,
74
37
  * marshaller: ERef<Marshaller>,
75
38
  * storageNode: ERef<StorageNode>,
76
39
  * }} privateArgs
77
40
  * @param {Baggage} baggage
78
41
  */
79
- const start = async (zcf, privateArgs, baggage) => {
42
+ export const prepare = async (zcf, privateArgs, baggage) => {
43
+ trace('prepare', Object.keys(privateArgs), [...baggage.keys()]);
80
44
  // This contract mixes two styles of access to durable state. durableStores
81
45
  // are declared at the top level and referenced lexically. local state is
82
46
  // accessed via the `state` object. The latter means updates are made directly
83
47
  // to state and don't require reference to baggage.
84
48
 
85
- /**
86
- * Used to look up the unique keyword for each brand, including Fee brand.
87
- *
88
- * @type {MapStore<Brand, Keyword>}
89
- */
90
- const keywordForBrand = provideDurableMapStore(baggage, 'keywordForBrand');
91
- /**
92
- * Used to look up the brands for keywords, excluding Fee because it's a special case.
93
- *
94
- * @type {MapStore<Keyword, Brand>}
95
- */
96
- const brandForKeyword = provideDurableMapStore(baggage, 'brandForKeyword');
97
- /**
98
- * @type {MapStore<Brand, Brand>}
99
- */
100
- const liquidityBrandForBrand = provideDurableMapStore(
49
+ const { makeRecorderKit } = prepareRecorderKitMakers(
101
50
  baggage,
102
- 'liquidityBrandForBrand',
51
+ privateArgs.marshaller,
103
52
  );
104
53
 
54
+ /** @type {() => Promise<ZCFMint<'nat'>>} */
105
55
  const takeFeeMint = async () => {
106
56
  if (baggage.has('feeMint')) {
107
57
  return baggage.get('feeMint');
@@ -114,345 +64,49 @@ const start = async (zcf, privateArgs, baggage) => {
114
64
  baggage.init('feeMint', feeMintTemp);
115
65
  return feeMintTemp;
116
66
  };
117
- /** @type {ZCFMint} */
67
+ trace('awaiting takeFeeMint');
118
68
  const feeMint = await takeFeeMint();
119
- const feeKit = feeMint.getIssuerRecord();
120
- const emptyAmount = AmountMath.makeEmpty(feeKit.brand);
121
-
122
- /**
123
- * @param {Brand} brand
124
- * @param {Keyword} keyword
125
- */
126
- const saveBrandKeyword = (brand, keyword) => {
127
- keywordForBrand.init(brand, keyword);
128
- brandForKeyword.init(keyword, brand);
129
- };
130
-
131
- saveBrandKeyword(feeKit.brand, 'Fee');
132
- // no need to saveIssuer() b/c registerFeeMint did it
133
-
134
- const { augmentVirtualPublicFacet, makeVirtualGovernorFacet, params } =
135
- await handleParamGovernance(
136
- zcf,
137
- privateArgs.initialPoserInvitation,
138
- {
139
- [AMM_INSTANCE]: ParamTypes.INSTANCE,
140
- },
141
- privateArgs.storageNode,
142
- privateArgs.marshaller,
143
- );
144
-
145
- /** @type {Promise<XYKAMMPublicFacet>} */
146
- const ammPublicFacet = E(zcf.getZoeService()).getPublicFacet(
147
- params.getAmmInstance(),
148
- );
149
-
150
- /**
151
- * @param {MethodContext} _context
152
- * @param {Issuer} issuer
153
- * @param {string} keyword
154
- */
155
- const addIssuer = async (_context, issuer, keyword) => {
156
- const brand = await E(issuer).getBrand();
157
- trace('addIssuer', { brand, keyword });
158
- assert(
159
- keyword !== 'Fee' && brand !== feeKit.brand,
160
- `'Fee' brand is a special case handled by the reserve contract`,
161
- );
162
-
163
- trace('addIssuer storing', {
164
- keyword,
165
- brand,
166
- });
167
-
168
- saveBrandKeyword(brand, keyword);
169
- await zcf.saveIssuer(issuer, keyword);
170
- };
171
-
172
- /**
173
- * @param {MethodContext} _context
174
- * @param {Issuer} baseIssuer on which the liquidity issuer is based
175
- */
176
- const addLiquidityIssuer = async (_context, baseIssuer) => {
177
- const getBrand = () => {
178
- try {
179
- return zcf.getBrandForIssuer(baseIssuer);
180
- } catch {
181
- assert.fail(
182
- `baseIssuer ${baseIssuer} not known; try addIssuer() first`,
183
- );
184
- }
185
- };
186
- const baseBrand = getBrand();
187
- const baseKeyword = keywordForBrand.get(baseBrand);
188
- const liquidityIssuer = E(ammPublicFacet).getLiquidityIssuer(baseBrand);
189
- const liquidityBrand = await E(liquidityIssuer).getBrand();
190
- const liquidityKeyword = makeLiquidityKeyword(baseKeyword);
191
-
192
- trace('addLiquidityIssuer', {
193
- baseBrand,
194
- baseKeyword,
195
- liquidityBrand,
196
- liquidityKeyword,
197
- });
198
- saveBrandKeyword(liquidityBrand, liquidityKeyword);
199
- liquidityBrandForBrand.init(baseBrand, liquidityBrand);
200
-
201
- await zcf.saveIssuer(liquidityIssuer, liquidityKeyword);
202
- };
203
-
204
- const conjureKeyword = async baseBrand => {
205
- const allegedName = await E(baseBrand).getAllegedName();
206
- const safeName = allegedName.replace(nonalphanumeric, '');
207
- let keyword;
208
- let keywordNum = 0;
209
- do {
210
- // 'R' to guarantee leading uppercase
211
- keyword = `R${safeName}${keywordNum || ''}`;
212
- keywordNum += 1;
213
- } while (brandForKeyword.has(keyword));
214
- return keyword;
215
- };
216
-
217
- /**
218
- * @param {MethodContext} context
219
- * @param {Brand} ammSecondaryBrand
220
- */
221
- const addIssuerFromAmm = async (context, ammSecondaryBrand) => {
222
- assert(
223
- ammSecondaryBrand !== feeKit.brand,
224
- 'Fee is a special case handled by the reserve contract',
225
- );
226
-
227
- const keyword = await conjureKeyword(ammSecondaryBrand);
228
- trace('addIssuerFromAmm', { ammSecondaryBrand, keyword });
229
- // validate the AMM has this brand and match its issuer
230
- const issuer = await E(ammPublicFacet).getSecondaryIssuer(
231
- ammSecondaryBrand,
232
- );
233
- await addIssuer(context, issuer, keyword);
234
- };
235
-
236
- const getKeywordForBrand = brand => {
237
- keywordForBrand.has(brand) ||
238
- assert.fail(
239
- X`Issuer not defined for brand ${q(brand)}; first call addIssuer()`,
240
- );
241
- return keywordForBrand.get(brand);
242
- };
243
-
244
- // We keep the associated liquidity tokens in the same seat
245
- const { zcfSeat: collateralSeat } = zcf.makeEmptySeatKit();
246
- const getAllocations = () => {
247
- return collateralSeat.getCurrentAllocation();
248
- };
249
-
250
- /**
251
- * Anyone can deposit any assets to the reserve
252
- *
253
- * @param {ZCFSeat} seat
254
- */
255
- const addCollateralHook = async seat => {
256
- const {
257
- give: { Collateral: amountIn },
258
- } = seat.getProposal();
259
-
260
- const collateralKeyword = getKeywordForBrand(amountIn.brand);
261
- seat.decrementBy(harden({ Collateral: amountIn }));
262
- collateralSeat.incrementBy(harden({ [collateralKeyword]: amountIn }));
263
-
264
- zcf.reallocate(collateralSeat, seat);
265
- seat.exit();
266
-
267
- trace('received collateral', amountIn);
268
- return 'added Collateral to the Reserve';
269
- };
69
+ const makeAssetReserveKit = await prepareAssetReserveKit(baggage, {
70
+ feeMint,
71
+ makeRecorderKit,
72
+ // eslint-disable-next-line @jessie.js/no-nested-await -- spurious
73
+ storageNode: await privateArgs.storageNode,
74
+ zcf,
75
+ });
270
76
 
271
- const makeAddCollateralInvitation = () =>
272
- zcf.makeInvitation(addCollateralHook, 'Add Collateral');
77
+ const { assetReserveKit } = await provideAll(baggage, {
78
+ assetReserveKit: makeAssetReserveKit,
79
+ });
273
80
 
274
- /** @type {import('../contractSupport.js').MetricsPublisherKit<MetricsNotification>} */
275
- const { metricsPublication, metricsSubscription } = makeMetricsPublisherKit(
81
+ trace('awaiting handleParamGovernance');
82
+ const { makeDurableGovernorFacet } = await handleParamGovernance(
83
+ zcf,
84
+ privateArgs.initialPoserInvitation,
85
+ {},
276
86
  privateArgs.storageNode,
277
87
  privateArgs.marshaller,
278
88
  );
279
89
 
280
- const updateMetrics = ({ state }) => {
281
- const metrics = harden({
282
- allocations: getAllocations(),
283
- shortfallBalance: state.shortfallBalance,
284
- totalFeeMinted: state.totalFeeMinted,
285
- totalFeeBurned: state.totalFeeBurned,
286
- });
287
- metricsPublication.updateState(metrics);
288
- };
289
-
290
- const increaseLiquidationShortfall = ({ state }, shortfall) => {
291
- state.shortfallBalance = AmountMath.add(state.shortfallBalance, shortfall);
292
- updateMetrics({ state });
293
- };
294
-
295
- const reduceLiquidationShortfall = ({ state }, reduction) => {
296
- if (AmountMath.isGTE(reduction, state.shortfallBalance)) {
297
- state.shortfallBalance = emptyAmount;
298
- } else {
299
- state.shortfallBalance = AmountMath.subtract(
300
- state.shortfallBalance,
301
- reduction,
302
- );
303
- }
304
- updateMetrics({ state });
305
- };
306
-
307
- const init = () => {
308
- const initialState = {
309
- totalFeeMinted: emptyAmount,
310
- totalFeeBurned: emptyAmount,
311
- shortfallBalance: emptyAmount,
312
- };
313
- updateMetrics({ state: initialState });
314
- return initialState;
315
- };
316
-
317
- /**
318
- * Takes collateral from the reserve, mints Fee tokens to accompany it, and uses both
319
- * to add Liquidity to a pool in the AMM.
320
- *
321
- * @param {MethodContext} context
322
- * @param {Amount} collateral
323
- * @param {Amount} fee
324
- */
325
- const addLiquidityToAmmPool = async ({ state }, collateral, fee) => {
326
- // verify we have the funds
327
- const collateralKeyword = getKeywordForBrand(collateral.brand);
328
- if (
329
- !AmountMath.isGTE(
330
- collateralSeat.getCurrentAllocation()[collateralKeyword],
331
- collateral,
332
- )
333
- ) {
334
- throw new Error('insufficient reserves for that transaction');
335
- }
336
-
337
- // create the Fee tokens
338
- const offerToSeat = feeMint.mintGains(harden({ Fee: fee }));
339
- state.totalFeeMinted = AmountMath.add(state.totalFeeMinted, fee);
340
-
341
- offerToSeat.incrementBy(
342
- collateralSeat.decrementBy(
343
- harden({
344
- [collateralKeyword]: collateral,
345
- }),
346
- ),
347
- );
348
- zcf.reallocate(collateralSeat, offerToSeat);
349
-
350
- // Add Fee tokens and collateral to the AMM
351
- const invitation = await E(
352
- ammPublicFacet,
353
- ).makeAddLiquidityAtRateInvitation();
354
- const mapping = harden({
355
- Fee: 'Central',
356
- [collateralKeyword]: 'Secondary',
357
- });
358
-
359
- const liqBrand = liquidityBrandForBrand.get(collateral.brand);
360
- const proposal = harden({
361
- give: {
362
- Central: fee,
363
- Secondary: collateral,
364
- },
365
- want: { Liquidity: AmountMath.makeEmpty(liqBrand) },
366
- });
367
-
368
- // chain await the completion of both the offer and the `deposited` promise
369
- await E.get(offerTo(zcf, invitation, mapping, proposal, offerToSeat))
370
- .deposited;
371
-
372
- // transfer from userSeat to LiquidityToken holdings
373
- const liquidityAmount = offerToSeat.getCurrentAllocation();
374
- const liquidityKeyword = makeLiquidityKeyword(collateralKeyword);
375
-
376
- offerToSeat.decrementBy(
377
- harden({
378
- Liquidity: liquidityAmount.Liquidity,
379
- }),
380
- );
381
- collateralSeat.incrementBy(
382
- harden({
383
- [liquidityKeyword]: liquidityAmount.Liquidity,
384
- }),
385
- );
386
- zcf.reallocate(offerToSeat, collateralSeat);
387
- updateMetrics({ state });
388
- };
389
-
390
- const burnFeesToReduceShortfall = ({ state }, reduction) => {
391
- reduction = AmountMath.coerce(feeKit.brand, reduction);
392
- const feeKeyword = keywordForBrand.get(feeKit.brand);
393
- const feeBalance = collateralSeat.getAmountAllocated(feeKeyword);
394
- const amountToBurn = AmountMath.min(reduction, feeBalance);
395
- if (AmountMath.isEmpty(amountToBurn)) {
396
- return;
397
- }
398
-
399
- feeMint.burnLosses(harden({ [feeKeyword]: amountToBurn }), collateralSeat);
400
- state.totalFeeBurned = AmountMath.add(state.totalFeeBurned, amountToBurn);
401
- updateMetrics({ state });
402
- };
403
-
404
- const shortfallReportingFacet = {
405
- increaseLiquidationShortfall,
406
- // currently exposed for testing. Maybe it only gets called internally?
407
- reduceLiquidationShortfall,
408
- };
409
-
410
- /** @param {MethodContext} context */
411
- const makeShortfallReportingInvitation = ({ facets }) => {
412
- const handleShortfallReportingOffer = () => {
413
- return facets.shortfallReportingFacet;
414
- };
415
-
416
- return zcf.makeInvitation(
417
- handleShortfallReportingOffer,
418
- 'getFacetForReportingShortfalls',
419
- );
420
- };
421
-
422
- const { governorFacet, limitedCreatorFacet } = makeVirtualGovernorFacet({
423
- makeAddCollateralInvitation,
424
- // add makeRedeemLiquidityTokensInvitation later. For now just store them
425
- getAllocations,
426
- addIssuer,
427
- makeShortfallReportingInvitation,
428
- getMetrics: () => metricsSubscription,
429
- });
430
-
431
- const governedApis = { addLiquidityToAmmPool, burnFeesToReduceShortfall };
432
-
433
- const publicFacet = augmentVirtualPublicFacet(
434
- Far('Collateral Reserve public', {
435
- makeAddCollateralInvitation,
436
- addIssuerFromAmm,
437
- addLiquidityIssuer,
438
- }),
90
+ const { governorFacet } = makeDurableGovernorFacet(
91
+ baggage,
92
+ assetReserveKit.machine,
93
+ // reconstruct facet so that the keys are enumerable and that the client can't depend on object identity
94
+ {
95
+ burnFeesToReduceShortfall: reduction =>
96
+ assetReserveKit.governedApis.burnFeesToReduceShortfall(reduction),
97
+ },
439
98
  );
440
99
 
441
- const makeAssetReserve = vivifyKindMulti(baggage, 'assetReserve', init, {
442
- publicFacet,
100
+ return {
101
+ /** @type {GovernedCreatorFacet<typeof assetReserveKit.machine>} */
443
102
  creatorFacet: governorFacet,
444
- shortfallReportingFacet,
445
- limitedCreatorFacet,
446
- governedApis,
447
- });
448
-
449
- const { creatorFacet, publicFacet: pFacet } = makeAssetReserve();
450
- return { creatorFacet, publicFacet: pFacet };
103
+ /** @type {GovernedPublicFacet<typeof assetReserveKit.public>} */
104
+ // cast due to missing governance mixins
105
+ // XXX https://github.com/Agoric/agoric-sdk/issues/5200
106
+ publicFacet: /** @type {any} */ (assetReserveKit.public),
107
+ };
451
108
  };
452
-
453
- harden(start);
454
-
455
- export { start };
109
+ harden(prepare);
456
110
 
457
111
  /**
458
112
  * @typedef {object} ShortfallReporter
@@ -461,22 +115,11 @@ export { start };
461
115
  */
462
116
 
463
117
  /**
464
- * @typedef {object} OriginalAssetReserveCreatorFacet
465
- * @property {() => Invitation} makeAddCollateralInvitation
118
+ * @typedef {object} AssetReserveLimitedCreatorFacet
119
+ * @property {(issuer: Issuer, kwd: string) => void} addIssuer
466
120
  * @property {() => Allocation} getAllocations
467
- * @property {(issuer: Issuer) => void} addIssuer
468
- * @property {() => Invitation<ShortfallReporter>} makeShortfallReportingInvitation
469
- * @property {() => MetricsNotification} getMetrics
470
- */
471
-
472
- /**
473
- * @typedef {object} OriginalAssetReservePublicFacet
474
- * @property {() => Invitation} makeAddCollateralInvitation
475
- * @property {(brand: Brand) => void} addIssuerFromAmm
476
- * @property {(issuer: Issuer) => void} addLiquidityIssuer
121
+ * @property {() => Promise<Invitation<ShortfallReporter>>} makeShortfallReportingInvitation
477
122
  */
478
123
 
479
- /** @typedef {Awaited<ReturnType<typeof start>>['publicFacet']} AssetReservePublicFacet */
480
- /** @typedef {Awaited<ReturnType<typeof start>>['creatorFacet']} AssetReserveCreatorFacet the creator facet for the governor */
481
- /** @typedef {LimitedCreatorFacet<OriginalAssetReserveCreatorFacet>} AssetReserveLimitedCreatorFacet */
482
- /** @typedef {GovernedContractFacetAccess<AssetReservePublicFacet,AssetReserveLimitedCreatorFacet>} GovernedAssetReserveFacetAccess */
124
+ /** @typedef {Awaited<ReturnType<typeof prepare>>['publicFacet']} AssetReservePublicFacet */
125
+ /** @typedef {Awaited<ReturnType<typeof prepare>>['creatorFacet']} AssetReserveCreatorFacet the creator facet for the governor */