@agoric/inter-protocol 0.16.2-other-dev-8f8782b.0 → 0.16.2-other-dev-3eb1a1d.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 (191) hide show
  1. package/README.md +6 -6
  2. package/package.json +42 -36
  3. package/scripts/build-bundles.js +5 -21
  4. package/src/auction/auctionBook.d.ts +147 -0
  5. package/src/auction/auctionBook.d.ts.map +1 -0
  6. package/src/auction/auctionBook.js +156 -150
  7. package/src/auction/auctionMath.d.ts +17 -0
  8. package/src/auction/auctionMath.d.ts.map +1 -0
  9. package/src/auction/auctionMath.js +81 -0
  10. package/src/auction/auctioneer.d.ts +69 -0
  11. package/src/auction/auctioneer.d.ts.map +1 -0
  12. package/src/auction/auctioneer.js +70 -59
  13. package/src/auction/offerBook.d.ts +45 -0
  14. package/src/auction/offerBook.d.ts.map +1 -0
  15. package/src/auction/offerBook.js +13 -12
  16. package/src/auction/params.d.ts +144 -0
  17. package/src/auction/params.d.ts.map +1 -0
  18. package/src/auction/params.js +10 -9
  19. package/src/auction/scheduleMath.d.ts +5 -0
  20. package/src/auction/scheduleMath.d.ts.map +1 -0
  21. package/src/auction/scheduleMath.js +18 -16
  22. package/src/auction/scheduler.d.ts +50 -0
  23. package/src/auction/scheduler.d.ts.map +1 -0
  24. package/src/auction/scheduler.js +53 -47
  25. package/src/auction/sortedOffers.d.ts +8 -0
  26. package/src/auction/sortedOffers.d.ts.map +1 -0
  27. package/src/auction/sortedOffers.js +10 -9
  28. package/src/auction/util.d.ts +30 -0
  29. package/src/auction/util.d.ts.map +1 -0
  30. package/src/auction/util.js +9 -6
  31. package/src/clientSupport.d.ts +167 -0
  32. package/src/clientSupport.d.ts.map +1 -0
  33. package/src/clientSupport.js +155 -97
  34. package/src/collectFees.d.ts +2 -0
  35. package/src/collectFees.d.ts.map +1 -0
  36. package/src/contractSupport.d.ts +28 -0
  37. package/src/contractSupport.d.ts.map +1 -0
  38. package/src/contractSupport.js +19 -13
  39. package/src/econCommitteeCharter.d.ts +40 -0
  40. package/src/econCommitteeCharter.d.ts.map +1 -0
  41. package/src/econCommitteeCharter.js +21 -20
  42. package/src/feeDistributor.d.ts +224 -0
  43. package/src/feeDistributor.d.ts.map +1 -0
  44. package/src/feeDistributor.js +37 -33
  45. package/src/index.d.ts +2 -0
  46. package/src/index.d.ts.map +1 -0
  47. package/src/index.js +1 -0
  48. package/src/interest-math.d.ts +3 -0
  49. package/src/interest-math.d.ts.map +1 -0
  50. package/src/interest.d.ts +28 -0
  51. package/src/interest.d.ts.map +1 -0
  52. package/src/interest.js +23 -18
  53. package/src/price/README.md +13 -0
  54. package/src/price/fluxAggregatorContract.d.ts +71 -0
  55. package/src/price/fluxAggregatorContract.d.ts.map +1 -0
  56. package/src/price/fluxAggregatorContract.js +64 -55
  57. package/src/price/fluxAggregatorKit.d.ts +103 -0
  58. package/src/price/fluxAggregatorKit.d.ts.map +1 -0
  59. package/src/price/fluxAggregatorKit.js +52 -40
  60. package/src/price/priceOracleKit.d.ts +38 -0
  61. package/src/price/priceOracleKit.d.ts.map +1 -0
  62. package/src/price/priceOracleKit.js +12 -14
  63. package/src/price/roundsManager.d.ts +204 -0
  64. package/src/price/roundsManager.d.ts.map +1 -0
  65. package/src/price/roundsManager.js +131 -85
  66. package/src/proposals/README.md +2 -3
  67. package/src/proposals/add-auction.d.ts +109 -0
  68. package/src/proposals/add-auction.d.ts.map +1 -0
  69. package/src/proposals/add-auction.js +256 -0
  70. package/src/proposals/addAssetToVault.d.ts +173 -0
  71. package/src/proposals/addAssetToVault.d.ts.map +1 -0
  72. package/src/proposals/addAssetToVault.js +192 -40
  73. package/src/proposals/committee-proposal.d.ts +113 -0
  74. package/src/proposals/committee-proposal.d.ts.map +1 -0
  75. package/src/proposals/committee-proposal.js +25 -31
  76. package/src/proposals/core-proposal.d.ts +149 -0
  77. package/src/proposals/core-proposal.d.ts.map +1 -0
  78. package/src/proposals/core-proposal.js +9 -11
  79. package/src/proposals/deploy-price-feeds.d.ts +76 -0
  80. package/src/proposals/deploy-price-feeds.d.ts.map +1 -0
  81. package/src/proposals/deploy-price-feeds.js +316 -0
  82. package/src/proposals/econ-behaviors.d.ts +541 -0
  83. package/src/proposals/econ-behaviors.d.ts.map +1 -0
  84. package/src/proposals/econ-behaviors.js +64 -45
  85. package/src/proposals/price-feed-proposal.d.ts +84 -0
  86. package/src/proposals/price-feed-proposal.d.ts.map +1 -0
  87. package/src/proposals/price-feed-proposal.js +109 -51
  88. package/src/proposals/replace-fee-distributor.d.ts +48 -0
  89. package/src/proposals/replace-fee-distributor.d.ts.map +1 -0
  90. package/src/proposals/replace-fee-distributor.js +195 -0
  91. package/src/proposals/replace-scaledPriceAuthorities.d.ts +30 -0
  92. package/src/proposals/replace-scaledPriceAuthorities.d.ts.map +1 -0
  93. package/src/proposals/replace-scaledPriceAuthorities.js +124 -0
  94. package/src/proposals/replaceElectorate.d.ts +55 -0
  95. package/src/proposals/replaceElectorate.d.ts.map +1 -0
  96. package/src/proposals/replaceElectorate.js +521 -0
  97. package/src/proposals/startEconCommittee.d.ts +34 -0
  98. package/src/proposals/startEconCommittee.d.ts.map +1 -0
  99. package/src/proposals/startEconCommittee.js +2 -2
  100. package/src/proposals/startPSM.d.ts +59 -0
  101. package/src/proposals/startPSM.d.ts.map +1 -0
  102. package/src/proposals/startPSM.js +44 -29
  103. package/src/proposals/upgrade-scaledPriceAuthorities.d.ts +24 -0
  104. package/src/proposals/upgrade-scaledPriceAuthorities.d.ts.map +1 -0
  105. package/src/proposals/upgrade-scaledPriceAuthorities.js +78 -0
  106. package/src/proposals/upgrade-vaults.d.ts +41 -0
  107. package/src/proposals/upgrade-vaults.d.ts.map +1 -0
  108. package/src/proposals/upgrade-vaults.js +202 -0
  109. package/src/proposals/utils.d.ts +19 -0
  110. package/src/proposals/utils.d.ts.map +1 -0
  111. package/src/proposals/utils.js +43 -9
  112. package/src/provisionPool.d.ts +163 -0
  113. package/src/provisionPool.d.ts.map +1 -0
  114. package/src/provisionPool.js +122 -0
  115. package/src/provisionPoolKit.d.ts +359 -0
  116. package/src/provisionPoolKit.d.ts.map +1 -0
  117. package/src/provisionPoolKit.js +522 -0
  118. package/src/psm/psm.d.ts +125 -0
  119. package/src/psm/psm.d.ts.map +1 -0
  120. package/src/psm/psm.js +80 -79
  121. package/src/psm/types-ambient.d.ts +2 -0
  122. package/src/psm/types-ambient.d.ts.map +1 -0
  123. package/src/psm/types-ambient.js +3 -0
  124. package/src/reserve/assetReserve.d.ts +47 -0
  125. package/src/reserve/assetReserve.d.ts.map +1 -0
  126. package/src/reserve/assetReserve.js +28 -19
  127. package/src/reserve/assetReserveKit.d.ts +74 -0
  128. package/src/reserve/assetReserveKit.d.ts.map +1 -0
  129. package/src/reserve/assetReserveKit.js +24 -30
  130. package/src/reserve/params.d.ts +10 -0
  131. package/src/reserve/params.d.ts.map +1 -0
  132. package/src/tokens.d.ts +3 -0
  133. package/src/tokens.d.ts.map +1 -0
  134. package/src/tokens.js +5 -0
  135. package/src/vaultFactory/burn.d.ts +2 -0
  136. package/src/vaultFactory/burn.d.ts.map +1 -0
  137. package/src/vaultFactory/burn.js +1 -1
  138. package/src/vaultFactory/liquidation.d.ts +23 -0
  139. package/src/vaultFactory/liquidation.d.ts.map +1 -0
  140. package/src/vaultFactory/liquidation.js +37 -24
  141. package/src/vaultFactory/math.d.ts +11 -0
  142. package/src/vaultFactory/math.d.ts.map +1 -0
  143. package/src/vaultFactory/math.js +10 -9
  144. package/src/vaultFactory/orderedVaultStore.d.ts +93 -0
  145. package/src/vaultFactory/orderedVaultStore.d.ts.map +1 -0
  146. package/src/vaultFactory/orderedVaultStore.js +8 -11
  147. package/src/vaultFactory/params.d.ts +140 -0
  148. package/src/vaultFactory/params.d.ts.map +1 -0
  149. package/src/vaultFactory/params.js +52 -24
  150. package/src/vaultFactory/prioritizedVaults.d.ts +279 -0
  151. package/src/vaultFactory/prioritizedVaults.d.ts.map +1 -0
  152. package/src/vaultFactory/prioritizedVaults.js +4 -4
  153. package/src/vaultFactory/proceeds.d.ts +35 -0
  154. package/src/vaultFactory/proceeds.d.ts.map +1 -0
  155. package/src/vaultFactory/proceeds.js +26 -18
  156. package/src/vaultFactory/storeUtils.d.ts +25 -0
  157. package/src/vaultFactory/storeUtils.d.ts.map +1 -0
  158. package/src/vaultFactory/storeUtils.js +10 -12
  159. package/src/vaultFactory/types-ambient.d.ts +137 -0
  160. package/src/vaultFactory/types-ambient.d.ts.map +1 -0
  161. package/src/vaultFactory/{types.js → types-ambient.js} +42 -42
  162. package/src/vaultFactory/vault.d.ts +343 -0
  163. package/src/vaultFactory/vault.d.ts.map +1 -0
  164. package/src/vaultFactory/vault.js +105 -99
  165. package/src/vaultFactory/vaultDirector.d.ts +341 -0
  166. package/src/vaultFactory/vaultDirector.d.ts.map +1 -0
  167. package/src/vaultFactory/vaultDirector.js +86 -64
  168. package/src/vaultFactory/vaultFactory.d.ts +202 -0
  169. package/src/vaultFactory/vaultFactory.d.ts.map +1 -0
  170. package/src/vaultFactory/vaultFactory.js +52 -33
  171. package/src/vaultFactory/vaultHolder.d.ts +126 -0
  172. package/src/vaultFactory/vaultHolder.d.ts.map +1 -0
  173. package/src/vaultFactory/vaultHolder.js +11 -16
  174. package/src/vaultFactory/vaultKit.d.ts +32 -0
  175. package/src/vaultFactory/vaultKit.d.ts.map +1 -0
  176. package/src/vaultFactory/vaultKit.js +5 -4
  177. package/src/vaultFactory/vaultManager.d.ts +674 -0
  178. package/src/vaultFactory/vaultManager.d.ts.map +1 -0
  179. package/src/vaultFactory/vaultManager.js +257 -158
  180. package/CHANGELOG.md +0 -1041
  181. package/exported.js +0 -2
  182. package/scripts/add-collateral-core.js +0 -112
  183. package/scripts/deploy-contracts.js +0 -100
  184. package/scripts/init-core.js +0 -198
  185. package/scripts/invite-committee-core.js +0 -42
  186. package/scripts/manual-price-feed.js +0 -117
  187. package/scripts/price-feed-core.js +0 -104
  188. package/scripts/start-local-chain.sh +0 -84
  189. package/src/psm/types.js +0 -3
  190. package/src/typeGuards.js +0 -13
  191. package/src/vaultFactory/type-imports.js +0 -4
@@ -1,22 +1,25 @@
1
- /* eslint-disable consistent-return */
2
1
  /**
3
2
  * @file Vault Manager object manages vault-based debts for a collateral type.
4
3
  *
5
- * The responsibilities include:
6
- * - opening a new vault backed by the collateral
7
- * - publishing metrics on the vault economy for that collateral
8
- * - charging interest on all active vaults
9
- * - liquidating active vaults that have exceeded the debt ratio
4
+ * The responsibilities include:
10
5
  *
11
- * Once a vault is settled (liquidated or closed) it can still be used, traded,
12
- * etc. but is no longer the concern of the manager. It can't be liquidated,
13
- * have interest charged, or be counted in the metrics.
6
+ * - opening a new vault backed by the collateral
7
+ * - publishing metrics on the vault economy for that collateral
8
+ * - charging interest on all active vaults
9
+ * - liquidating active vaults that have exceeded the debt ratio
14
10
  *
15
- * Undercollateralized vaults can have their assets sent to the auctioneer to be
16
- * liquidated. If the auction is unsuccessful, the liquidation may be reverted.
11
+ * Once a vault is settled (liquidated or closed) it can still be used, traded,
12
+ * etc. but is no longer the concern of the manager. It can't be liquidated,
13
+ * have interest charged, or be counted in the metrics.
14
+ *
15
+ * Undercollateralized vaults can have their assets sent to the auctioneer to be
16
+ * liquidated. If the auction is unsuccessful, the liquidation may be
17
+ * reverted.
17
18
  */
18
- import '@agoric/zoe/exported.js';
19
+ /// <reference types="@agoric/zoe/exported" />
19
20
 
21
+ import { X, Fail, q, makeError } from '@endo/errors';
22
+ import { E } from '@endo/eventual-send';
20
23
  import {
21
24
  AmountMath,
22
25
  AmountShape,
@@ -36,7 +39,6 @@ import {
36
39
  } from '@agoric/vat-data';
37
40
  import { TransferPartShape } from '@agoric/zoe/src/contractSupport/atomicTransfer.js';
38
41
  import {
39
- atomicRearrange,
40
42
  ceilMultiplyBy,
41
43
  floorDivideBy,
42
44
  getAmountIn,
@@ -49,8 +51,7 @@ import {
49
51
  TopicsRecordShape,
50
52
  } from '@agoric/zoe/src/contractSupport/index.js';
51
53
  import { PriceQuoteShape, SeatShape } from '@agoric/zoe/src/typeGuards.js';
52
- import { E } from '@endo/eventual-send';
53
- import { AuctionPFShape } from '../auction/auctioneer.js';
54
+ import { multiplyBy } from '@agoric/zoe/src/contractSupport/ratio.js';
54
55
  import {
55
56
  checkDebtLimit,
56
57
  makeNatAmountShape,
@@ -62,117 +63,163 @@ import { calculateMinimumCollateralization, minimumPrice } from './math.js';
62
63
  import { makePrioritizedVaults } from './prioritizedVaults.js';
63
64
  import { Phase, prepareVault } from './vault.js';
64
65
  import { calculateDistributionPlan } from './proceeds.js';
66
+ import { AuctionPFShape } from '../auction/auctioneer.js';
65
67
 
66
- const { details: X, Fail, quote: q } = assert;
68
+ /**
69
+ * @import {Baggage} from '@agoric/vat-data';
70
+ * @import {PriceAuthority, PriceDescription, PriceQuote, PriceQuoteValue, PriceQuery,} from '@agoric/zoe/tools/types.js';
71
+ */
67
72
 
68
73
  const trace = makeTracer('VM');
69
74
 
70
- /** @typedef {import('./storeUtils.js').NormalizedDebt} NormalizedDebt */
71
- /** @typedef {import('@agoric/time/src/types').RelativeTime} RelativeTime */
75
+ /**
76
+ * Watch a notifier that isn't expected to fail or finish unless the vat hosting
77
+ * the notifier is upgraded. This watcher supports that by providing a
78
+ * straightforward way to get a replacement if the notifier breaks.
79
+ *
80
+ * @template T notifier topic
81
+ * @template {any[]} [A=unknown[]] arbitrary arguments
82
+ * @param {ERef<LatestTopic<T>>} notifierP
83
+ * @param {import('@agoric/swingset-liveslots').PromiseWatcher<T, A>} watcher
84
+ * @param {A} args
85
+ */
86
+ export const watchQuoteNotifier = async (notifierP, watcher, ...args) => {
87
+ await undefined;
88
+
89
+ let updateCount;
90
+ for (;;) {
91
+ let value;
92
+ try {
93
+ ({ value, updateCount } = await E(notifierP).getUpdateSince(updateCount));
94
+ watcher.onFulfilled && watcher.onFulfilled(value, ...args);
95
+ } catch (e) {
96
+ watcher.onRejected && watcher.onRejected(e, ...args);
97
+ break;
98
+ }
99
+ if (updateCount === undefined) {
100
+ watcher.onRejected &&
101
+ watcher.onRejected(Error('stream finished'), ...args);
102
+ break;
103
+ }
104
+ }
105
+ };
106
+
107
+ /** @import {NormalizedDebt} from './storeUtils.js' */
108
+ /** @import {RelativeTime} from '@agoric/time' */
72
109
 
73
110
  // Metrics naming scheme: nouns are present values; past-participles are accumulative.
74
111
  /**
75
112
  * @typedef {object} MetricsNotification
76
- *
77
- * @property {Ratio | null} lockedQuote priceQuote that will be used for liquidation.
78
- * Non-null from priceLock time until liquidation has taken place.
79
- * @property {number} numActiveVaults present count of vaults
80
- * @property {number} numLiquidatingVaults present count of liquidating vaults
81
- * @property {Amount<'nat'>} totalCollateral present sum of collateral across all vaults
82
- * @property {Amount<'nat'>} totalDebt present sum of debt across all vaults
83
- * @property {Amount<'nat'>} retainedCollateral collateral held as a result of not returning excess refunds
84
- * to owners of vaults liquidated with shortfalls
85
- * @property {Amount<'nat'>} liquidatingCollateral present sum of collateral in vaults sent for liquidation
86
- * @property {Amount<'nat'>} liquidatingDebt present sum of debt in vaults sent for liquidation
87
- *
88
- * @property {Amount<'nat'>} totalCollateralSold running sum of collateral sold in liquidation
89
- * @property {Amount<'nat'>} totalOverageReceived running sum of overages, central received greater than debt
90
- * @property {Amount<'nat'>} totalProceedsReceived running sum of minted received from liquidation
91
- * @property {Amount<'nat'>} totalShortfallReceived running sum of shortfalls, minted received less than debt
92
- * @property {number} numLiquidationsCompleted running count of liquidated vaults
93
- * @property {number} numLiquidationsAborted running count of vault liquidations that were reverted.
113
+ * @property {Ratio | null} lockedQuote priceQuote that will be used for
114
+ * liquidation. Non-null from priceLock time until liquidation has taken
115
+ * place.
116
+ * @property {number} numActiveVaults present count of vaults
117
+ * @property {number} numLiquidatingVaults present count of liquidating vaults
118
+ * @property {Amount<'nat'>} totalCollateral present sum of collateral across
119
+ * all vaults
120
+ * @property {Amount<'nat'>} totalDebt present sum of debt across all vaults
121
+ * @property {Amount<'nat'>} retainedCollateral collateral held as a result of
122
+ * not returning excess refunds to owners of vaults liquidated with
123
+ * shortfalls
124
+ * @property {Amount<'nat'>} liquidatingCollateral present sum of collateral in
125
+ * vaults sent for liquidation
126
+ * @property {Amount<'nat'>} liquidatingDebt present sum of debt in vaults sent
127
+ * for liquidation
128
+ * @property {Amount<'nat'>} totalCollateralSold running sum of collateral sold
129
+ * in liquidation
130
+ * @property {Amount<'nat'>} totalOverageReceived running sum of overages,
131
+ * central received greater than debt
132
+ * @property {Amount<'nat'>} totalProceedsReceived running sum of minted
133
+ * received from liquidation
134
+ * @property {Amount<'nat'>} totalShortfallReceived running sum of shortfalls,
135
+ * minted received less than debt
136
+ * @property {number} numLiquidationsCompleted running count of liquidated
137
+ * vaults
138
+ * @property {number} numLiquidationsAborted running count of vault liquidations
139
+ * that were reverted.
94
140
  */
95
141
 
96
142
  /**
97
143
  * @typedef {{
98
- * compoundedInterest: Ratio,
99
- * interestRate: Ratio,
100
- * latestInterestUpdate: Timestamp,
144
+ * compoundedInterest: Ratio;
145
+ * interestRate: Ratio;
146
+ * latestInterestUpdate: Timestamp;
101
147
  * }} AssetState
102
148
  *
149
+ *
103
150
  * @typedef {{
104
- * getChargingPeriod: () => RelativeTime,
105
- * getRecordingPeriod: () => RelativeTime,
106
- * getDebtLimit: () => Amount<'nat'>,
107
- * getInterestRate: () => Ratio,
108
- * getLiquidationPadding: () => Ratio,
109
- * getLiquidationMargin: () => Ratio,
110
- * getLiquidationPenalty: () => Ratio,
111
- * getMintFee: () => Ratio,
112
- * getMinInitialDebt: () => Amount<'nat'>,
151
+ * getChargingPeriod: () => RelativeTime;
152
+ * getRecordingPeriod: () => RelativeTime;
153
+ * getDebtLimit: () => Amount<'nat'>;
154
+ * getInterestRate: () => Ratio;
155
+ * getLiquidationPadding: () => Ratio;
156
+ * getLiquidationMargin: () => Ratio;
157
+ * getLiquidationPenalty: () => Ratio;
158
+ * getMintFee: () => Ratio;
159
+ * getMinInitialDebt: () => Amount<'nat'>;
113
160
  * }} GovernedParamGetters
114
161
  */
115
162
 
116
163
  /**
117
164
  * @typedef {Readonly<{
118
- * debtMint: ZCFMint<'nat'>,
119
- * collateralBrand: Brand<'nat'>,
120
- * collateralUnit: Amount<'nat'>,
121
- * descriptionScope: string,
122
- * startTimeStamp: Timestamp,
123
- * storageNode: StorageNode,
165
+ * debtMint: ZCFMint<'nat'>;
166
+ * collateralBrand: Brand<'nat'>;
167
+ * collateralUnit: Amount<'nat'>;
168
+ * descriptionScope: string;
169
+ * startTimeStamp: Timestamp;
170
+ * storageNode: StorageNode;
124
171
  * }>} HeldParams
125
172
  */
126
173
 
127
174
  /**
128
175
  * @typedef {{
129
- * assetTopicKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit<AssetState>,
130
- * debtBrand: Brand<'nat'>,
131
- * liquidatingVaults: SetStore<Vault>,
132
- * metricsTopicKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit<MetricsNotification>,
133
- * poolIncrementSeat: ZCFSeat,
134
- * retainedCollateralSeat: ZCFSeat,
135
- * unsettledVaults: MapStore<string, Vault>,
176
+ * assetTopicKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit<AssetState>;
177
+ * debtBrand: Brand<'nat'>;
178
+ * liquidatingVaults: SetStore<Vault>;
179
+ * metricsTopicKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit<MetricsNotification>;
180
+ * poolIncrementSeat: ZCFSeat;
181
+ * retainedCollateralSeat: ZCFSeat;
182
+ * unsettledVaults: MapStore<string, Vault>;
136
183
  * }} ImmutableState
137
184
  */
138
185
 
139
186
  /**
140
187
  * @typedef {{
141
- * compoundedInterest: Ratio,
142
- * latestInterestUpdate: Timestamp,
143
- * numLiquidationsCompleted: number,
144
- * numLiquidationsAborted: number,
145
- * totalCollateral: Amount<'nat'>,
146
- * totalCollateralSold: Amount<'nat'>,
147
- * totalDebt: Amount<'nat'>,
148
- * liquidatingCollateral: Amount<'nat'>,
149
- * liquidatingDebt: Amount<'nat'>,
150
- * totalOverageReceived: Amount<'nat'>,
151
- * totalProceedsReceived: Amount<'nat'>,
152
- * totalShortfallReceived: Amount<'nat'>,
153
- * vaultCounter: number,
154
- * lockedQuote: PriceQuote | undefined,
188
+ * compoundedInterest: Ratio;
189
+ * latestInterestUpdate: Timestamp;
190
+ * numLiquidationsCompleted: number;
191
+ * numLiquidationsAborted: number;
192
+ * totalCollateral: Amount<'nat'>;
193
+ * totalCollateralSold: Amount<'nat'>;
194
+ * totalDebt: Amount<'nat'>;
195
+ * liquidatingCollateral: Amount<'nat'>;
196
+ * liquidatingDebt: Amount<'nat'>;
197
+ * totalOverageReceived: Amount<'nat'>;
198
+ * totalProceedsReceived: Amount<'nat'>;
199
+ * totalShortfallReceived: Amount<'nat'>;
200
+ * vaultCounter: number;
201
+ * lockedQuote: PriceQuote | undefined;
155
202
  * }} MutableState
156
203
  */
157
204
 
158
205
  /**
159
206
  * @type {(brand: Brand) => {
160
- * prioritizedVaults: ReturnType<typeof makePrioritizedVaults>,
161
- * storedQuotesNotifier: import('@agoric/notifier').StoredNotifier<PriceQuote>,
162
- * storedCollateralQuote: PriceQuote,
207
+ * prioritizedVaults: ReturnType<typeof makePrioritizedVaults>;
208
+ * storedQuotesNotifier: import('@agoric/notifier').StoredNotifier<PriceQuote>;
209
+ * storedCollateralQuote: PriceQuote | null;
163
210
  * }}
164
211
  */
165
212
  // any b/c will be filled after start()
166
213
  const collateralEphemera = makeEphemeraProvider(() => /** @type {any} */ ({}));
167
214
 
168
215
  /**
169
- * @param {import('@agoric/ertp').Baggage} baggage
216
+ * @param {import('@agoric/swingset-liveslots').Baggage} baggage
170
217
  * @param {{
171
- * zcf: import('./vaultFactory.js').VaultFactoryZCF,
172
- * marshaller: ERef<Marshaller>,
173
- * makeRecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').MakeRecorderKit,
174
- * makeERecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').MakeERecorderKit,
175
- * factoryPowers: import('./vaultDirector.js').FactoryPowersFacet,
218
+ * zcf: import('./vaultFactory.js').VaultFactoryZCF;
219
+ * marshaller: ERef<Marshaller>;
220
+ * makeRecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').MakeRecorderKit;
221
+ * makeERecorderKit: import('@agoric/zoe/src/contractSupport/recorder.js').MakeERecorderKit;
222
+ * factoryPowers: import('./vaultDirector.js').FactoryPowersFacet;
176
223
  * }} powers
177
224
  */
178
225
  export const prepareVaultManagerKit = (
@@ -203,8 +250,9 @@ export const prepareVaultManagerKit = (
203
250
  poolIncrementSeat: zcf.makeEmptySeatKit().zcfSeat,
204
251
 
205
252
  /**
206
- * Vaults that have been sent for liquidation. When we get proceeds (or lack
207
- * thereof) back from the liquidator, we will allocate them among the vaults.
253
+ * Vaults that have been sent for liquidation. When we get proceeds (or
254
+ * lack thereof) back from the liquidator, we will allocate them among the
255
+ * vaults.
208
256
  *
209
257
  * @type {SetStore<Vault>}
210
258
  */
@@ -292,7 +340,7 @@ export const prepareVaultManagerKit = (
292
340
  getCollateralQuote: M.call().returns(PriceQuoteShape),
293
341
  getPublicFacet: M.call().returns(M.remotable('publicFacet')),
294
342
  lockOraclePrices: M.call().returns(PriceQuoteShape),
295
- liquidateVaults: M.call(AuctionPFShape).returns(M.promise()),
343
+ liquidateVaults: M.call(M.eref(AuctionPFShape)).returns(M.promise()),
296
344
  }),
297
345
  },
298
346
  initState,
@@ -339,19 +387,11 @@ export const prepareVaultManagerKit = (
339
387
 
340
388
  // Some of these could go in closures but are kept on a facet anticipating future durability options.
341
389
  helper: {
342
- /**
343
- * Start non-durable processes (or restart if needed after vat restart)
344
- */
390
+ /** Start non-durable processes (or restart if needed after vat restart) */
345
391
  start() {
346
392
  const { state, facets } = this;
347
393
  trace(state.collateralBrand, 'helper.start()', state.vaultCounter);
348
- const {
349
- collateralBrand,
350
- collateralUnit,
351
- debtBrand,
352
- storageNode,
353
- unsettledVaults,
354
- } = state;
394
+ const { collateralBrand, unsettledVaults } = state;
355
395
 
356
396
  const ephemera = collateralEphemera(collateralBrand);
357
397
  ephemera.prioritizedVaults = makePrioritizedVaults(unsettledVaults);
@@ -374,44 +414,64 @@ export const prepareVaultManagerKit = (
374
414
  ),
375
415
  fail: reason => {
376
416
  zcf.shutdownWithFailure(
377
- assert.error(X`Unable to continue without a timer: ${reason}`),
417
+ makeError(X`Unable to continue without a timer: ${reason}`),
378
418
  );
379
419
  },
380
420
  finish: done => {
381
421
  zcf.shutdownWithFailure(
382
- assert.error(X`Unable to continue without a timer: ${done}`),
422
+ makeError(X`Unable to continue without a timer: ${done}`),
383
423
  );
384
424
  },
385
425
  });
386
426
 
387
- trace('helper.start() making quoteNotifier from', priceAuthority);
427
+ void facets.helper.observeQuoteNotifier();
428
+
429
+ trace('helper.start() done');
430
+ },
431
+ observeQuoteNotifier() {
432
+ const { state } = this;
433
+
434
+ const { collateralBrand, collateralUnit, debtBrand, storageNode } =
435
+ state;
436
+ const ephemera = collateralEphemera(collateralBrand);
437
+
388
438
  const quoteNotifier = E(priceAuthority).makeQuoteNotifier(
389
439
  collateralUnit,
390
440
  debtBrand,
391
441
  );
442
+ // @ts-expect-error XXX quotes
392
443
  ephemera.storedQuotesNotifier = makeStoredNotifier(
444
+ // @ts-expect-error XXX quotes
393
445
  quoteNotifier,
394
446
  E(storageNode).makeChildNode('quotes'),
395
447
  marshaller,
396
448
  );
397
- trace('helper.start() awaiting observe storedQuotesNotifier');
449
+ trace(
450
+ 'helper.start() awaiting observe storedQuotesNotifier',
451
+ collateralBrand,
452
+ );
398
453
  // NB: upon restart, there may not be a price for a while. If manager
399
- // operations are permitted, ones the depend on price information will
400
- // throw. See https://github.com/Agoric/agoric-sdk/issues/4317
401
- void observeNotifier(quoteNotifier, {
402
- updateState(value) {
403
- trace('vaultManager got new collateral quote', value);
454
+ // operations are permitted, ones that depend on price information
455
+ // will throw. See https://github.com/Agoric/agoric-sdk/issues/4317
456
+ const quoteWatcher = harden({
457
+ onFulfilled(value) {
458
+ trace('watcher updated price', value);
404
459
  ephemera.storedCollateralQuote = value;
405
460
  },
406
- fail(reason) {
407
- console.error('quoteNotifier failed to iterate', reason);
461
+ onRejected() {
462
+ // NOTE: drastic action, if the quoteNotifier fails, we don't know
463
+ // the value of the asset, nor do we know how long we'll be in
464
+ // ignorance. Best choice is to disable actions that require
465
+ // prices and restart when we have a new price. If we restart the
466
+ // notifier immediately, we'll trigger an infinite loop, so try
467
+ // to restart each time we get a request.
468
+
469
+ ephemera.storedCollateralQuote = null;
408
470
  },
409
471
  });
410
- trace('helper.start() done');
472
+ void watchQuoteNotifier(quoteNotifier, quoteWatcher);
411
473
  },
412
- /**
413
- * @param {Timestamp} updateTime
414
- */
474
+ /** @param {Timestamp} updateTime */
415
475
  async chargeAllVaults(updateTime) {
416
476
  const { state, facets } = this;
417
477
  const { collateralBrand, debtMint, poolIncrementSeat } = state;
@@ -515,7 +575,6 @@ export const prepareVaultManagerKit = (
515
575
  state.liquidatingDebt = AmountMath.add(state.liquidatingDebt, debt);
516
576
  },
517
577
  /**
518
- *
519
578
  * @param {Amount<'nat'>} debt
520
579
  * @param {Amount<'nat'>} collateral
521
580
  * @param {Amount<'nat'>} overage
@@ -598,14 +657,18 @@ export const prepareVaultManagerKit = (
598
657
  },
599
658
 
600
659
  /**
601
- * This is designed to tolerate an incomplete plan, in case calculateDistributionPlan encounters
602
- * an error during its calculation. We don't have a way to induce such errors in CI so we've
603
- * done so manually in dev and verified this function recovers as expected.
660
+ * This is designed to tolerate an incomplete plan, in case
661
+ * calculateDistributionPlan encounters an error during its calculation.
662
+ * We don't have a way to induce such errors in CI so we've done so
663
+ * manually in dev and verified this function recovers as expected.
604
664
  *
605
665
  * @param {AmountKeywordRecord} proceeds
606
666
  * @param {Amount<'nat'>} totalDebt
607
667
  * @param {Pick<PriceQuote, 'quoteAmount'>} oraclePriceAtStart
608
- * @param {MapStore<Vault, { collateralAmount: Amount<'nat'>, debtAmount: Amount<'nat'>}>} vaultData
668
+ * @param {MapStore<
669
+ * Vault,
670
+ * { collateralAmount: Amount<'nat'>; debtAmount: Amount<'nat'> }
671
+ * >} vaultData
609
672
  * @param {Amount<'nat'>} totalCollateral
610
673
  */
611
674
  planProceedsDistribution(
@@ -661,13 +724,14 @@ export const prepareVaultManagerKit = (
661
724
  },
662
725
 
663
726
  /**
664
- * This is designed to tolerate an incomplete plan, in case calculateDistributionPlan encounters
665
- * an error during its calculation. We don't have a way to induce such errors in CI so we've
666
- * done so manually in dev and verified this function recovers as expected.
727
+ * This is designed to tolerate an incomplete plan, in case
728
+ * calculateDistributionPlan encounters an error during its calculation.
729
+ * We don't have a way to induce such errors in CI so we've done so
730
+ * manually in dev and verified this function recovers as expected.
667
731
  *
668
732
  * @param {object} obj
669
733
  * @param {import('./proceeds.js').DistributionPlan} obj.plan
670
- * @param {Array<Vault>} obj.vaultsInPlan
734
+ * @param {Vault[]} obj.vaultsInPlan
671
735
  * @param {ZCFSeat} obj.liqSeat
672
736
  * @param {Amount<'nat'>} obj.totalCollateral
673
737
  * @param {Amount<'nat'>} obj.totalDebt
@@ -687,13 +751,13 @@ export const prepareVaultManagerKit = (
687
751
  if (plan.transfersToVault.length > 0) {
688
752
  const transfers = plan.transfersToVault.map(
689
753
  ([vaultIndex, amounts]) =>
690
- /** @type {import('@agoric/zoe/src/contractSupport/atomicTransfer.js').TransferPart} */ ([
754
+ /** @type {TransferPart} */ ([
691
755
  liqSeat,
692
756
  vaultsInPlan[vaultIndex].getVaultSeat(),
693
757
  amounts,
694
758
  ]),
695
759
  );
696
- atomicRearrange(zcf, harden(transfers));
760
+ zcf.atomicRearrange(harden(transfers));
697
761
  }
698
762
 
699
763
  const { prioritizedVaults } = collateralEphemera(
@@ -773,10 +837,15 @@ export const prepareVaultManagerKit = (
773
837
  * @param {Amount<'nat'>} collateralAmount
774
838
  */
775
839
  maxDebtFor(collateralAmount) {
776
- const { collateralBrand } = this.state;
840
+ const { state, facets } = this;
841
+ const { collateralBrand } = state;
777
842
  const { storedCollateralQuote } = collateralEphemera(collateralBrand);
778
- if (!storedCollateralQuote)
779
- throw Fail`maxDebtFor called before a collateral quote was available`;
843
+ if (!storedCollateralQuote) {
844
+ facets.helper.observeQuoteNotifier();
845
+
846
+ // it might take an arbitrary amount of time to get a new quote
847
+ throw Fail`maxDebtFor called before a collateral quote was available for ${collateralBrand}`;
848
+ }
780
849
  // use the lower price to prevent vault adjustments that put them imminently underwater
781
850
  const collateralPrice = minimumPrice(
782
851
  storedCollateralQuote,
@@ -840,9 +909,7 @@ export const prepareVaultManagerKit = (
840
909
  const { descriptionScope } = this.state;
841
910
  return `${descriptionScope}: ${base}`;
842
911
  },
843
- /**
844
- * coefficient on existing debt to calculate new debt
845
- */
912
+ /** coefficient on existing debt to calculate new debt */
846
913
  getCompoundedInterest() {
847
914
  return this.state.compoundedInterest;
848
915
  },
@@ -852,7 +919,8 @@ export const prepareVaultManagerKit = (
852
919
  * @param {NormalizedDebt} oldDebtNormalized
853
920
  * @param {Amount<'nat'>} oldCollateral
854
921
  * @param {VaultId} vaultId
855
- * @param {import('./vault.js').VaultPhase} vaultPhase at the end of whatever change updated balances
922
+ * @param {import('./vault.js').VaultPhase} vaultPhase at the end of
923
+ * whatever change updated balances
856
924
  * @param {Vault} vault
857
925
  * @returns {void}
858
926
  */
@@ -926,8 +994,9 @@ export const prepareVaultManagerKit = (
926
994
  );
927
995
  state.totalDebt = AmountMath.subtract(
928
996
  AmountMath.add(state.totalDebt, vault.getCurrentDebt()),
929
- oldDebtNormalized,
997
+ multiplyBy(oldDebtNormalized, state.compoundedInterest),
930
998
  );
999
+
931
1000
  void facets.helper.writeMetrics();
932
1001
  },
933
1002
  },
@@ -937,9 +1006,7 @@ export const prepareVaultManagerKit = (
937
1006
  return factoryPowers.getGovernedParams(collateralBrand);
938
1007
  },
939
1008
 
940
- /**
941
- * @param {ZCFSeat} seat
942
- */
1009
+ /** @param {ZCFSeat} seat */
943
1010
  async makeVaultKit(seat) {
944
1011
  const {
945
1012
  state,
@@ -963,7 +1030,6 @@ export const prepareVaultManagerKit = (
963
1030
 
964
1031
  try {
965
1032
  // TODO `await` is allowed until the above ordering is fixed
966
- // eslint-disable-next-line @jessie.js/no-nested-await
967
1033
  const vaultKit = await vault.initVaultKit(seat, vaultStorageNode);
968
1034
  // initVaultKit calls back to handleBalanceChange() which will add the
969
1035
  // vault to prioritizedVaults
@@ -1017,11 +1083,17 @@ export const prepareVaultManagerKit = (
1017
1083
  },
1018
1084
 
1019
1085
  getCollateralQuote() {
1086
+ const { state, facets } = this;
1020
1087
  const { storedCollateralQuote } = collateralEphemera(
1021
- this.state.collateralBrand,
1088
+ state.collateralBrand,
1022
1089
  );
1023
- if (!storedCollateralQuote)
1090
+ if (!storedCollateralQuote) {
1091
+ facets.helper.observeQuoteNotifier();
1092
+
1093
+ // it might take an arbitrary amount of time to get a new quote
1024
1094
  throw Fail`getCollateralQuote called before a collateral quote was available`;
1095
+ }
1096
+
1025
1097
  return storedCollateralQuote;
1026
1098
  },
1027
1099
 
@@ -1034,21 +1106,24 @@ export const prepareVaultManagerKit = (
1034
1106
  const { storedCollateralQuote } = collateralEphemera(
1035
1107
  state.collateralBrand,
1036
1108
  );
1037
- if (!storedCollateralQuote)
1038
- throw Fail`lockOraclePrices called before a collateral quote was available`;
1109
+ if (!storedCollateralQuote) {
1110
+ facets.helper.observeQuoteNotifier();
1111
+
1112
+ // it might take an arbitrary amount of time to get a new quote
1113
+ throw Fail`lockOraclePrices called before a collateral quote was available for ${state.collateralBrand}`;
1114
+ }
1115
+
1039
1116
  trace(
1040
- `lockPrice`,
1117
+ `lockOraclePrices`,
1041
1118
  getAmountIn(storedCollateralQuote),
1042
1119
  getAmountOut(storedCollateralQuote),
1043
1120
  );
1044
1121
 
1045
1122
  state.lockedQuote = storedCollateralQuote;
1046
- facets.helper.writeMetrics();
1123
+ void facets.helper.writeMetrics();
1047
1124
  return storedCollateralQuote;
1048
1125
  },
1049
- /**
1050
- * @param {AuctioneerPublicFacet} auctionPF
1051
- */
1126
+ /** @param {ERef<AuctioneerPublicFacet>} auctionPF */
1052
1127
  async liquidateVaults(auctionPF) {
1053
1128
  const { state, facets } = this;
1054
1129
  const { self, helper } = facets;
@@ -1061,11 +1136,28 @@ export const prepareVaultManagerKit = (
1061
1136
  } = state;
1062
1137
  trace(collateralBrand, 'considering liquidation');
1063
1138
 
1139
+ if (!lockedQuote) {
1140
+ // By design, the first cycle of auction may call this before a quote is locked
1141
+ // because the schedule is global at the vaultDirector level, and if a manager
1142
+ // starts after the price lock time there's nothing to be done.
1143
+ // NB: this message should not log repeatedly.
1144
+ console.error(
1145
+ 'Skipping liquidation because no quote is locked yet (may happen with new manager)',
1146
+ );
1147
+ return;
1148
+ }
1149
+
1150
+ const { storedCollateralQuote: collateralQuoteBefore } =
1151
+ collateralEphemera(this.state.collateralBrand);
1152
+ if (!collateralQuoteBefore) {
1153
+ console.error(
1154
+ 'Skipping liquidation because collateralQuote is missing',
1155
+ );
1156
+ return;
1157
+ }
1158
+
1064
1159
  const { prioritizedVaults } = collateralEphemera(collateralBrand);
1065
- assert(factoryPowers && prioritizedVaults && zcf);
1066
- lockedQuote ||
1067
- Fail`Must have locked a quote before liquidating vaults.`;
1068
- assert(lockedQuote); // redundant with previous line
1160
+ prioritizedVaults || Fail`prioritizedVaults missing from ephemera`;
1069
1161
 
1070
1162
  const liqMargin = self.getGovernedParams().getLiquidationMargin();
1071
1163
 
@@ -1087,6 +1179,7 @@ export const prepareVaultManagerKit = (
1087
1179
  state.lockedQuote = undefined;
1088
1180
 
1089
1181
  if (vaultData.getSize() === 0) {
1182
+ void helper.writeMetrics();
1090
1183
  return;
1091
1184
  }
1092
1185
  trace(
@@ -1126,7 +1219,10 @@ export const prepareVaultManagerKit = (
1126
1219
  const { plan, vaultsInPlan } = helper.planProceedsDistribution(
1127
1220
  proceeds,
1128
1221
  totalDebt,
1129
- storedCollateralQuote,
1222
+ // If a quote was available at the start of liquidation, but is no
1223
+ // longer, using the earlier price is better than failing to
1224
+ // distribute proceeds
1225
+ storedCollateralQuote || collateralQuoteBefore,
1130
1226
  vaultData,
1131
1227
  totalCollateral,
1132
1228
  );
@@ -1150,7 +1246,7 @@ export const prepareVaultManagerKit = (
1150
1246
  liquidatingVaults.delete(vault);
1151
1247
  }
1152
1248
 
1153
- await facets.helper.writeMetrics();
1249
+ void helper.writeMetrics();
1154
1250
  },
1155
1251
  },
1156
1252
  },
@@ -1174,7 +1270,12 @@ export const prepareVaultManagerKit = (
1174
1270
  },
1175
1271
  );
1176
1272
 
1177
- /** @param {Omit<Parameters<typeof makeVaultManagerKitInternal>[0], 'metricsStorageNode'>} externalParams */
1273
+ /**
1274
+ * @param {Omit<
1275
+ * Parameters<typeof makeVaultManagerKitInternal>[0],
1276
+ * 'metricsStorageNode'
1277
+ * >} externalParams
1278
+ */
1178
1279
  const makeVaultManagerKit = async externalParams => {
1179
1280
  const metricsStorageNode = await E(
1180
1281
  externalParams.storageNode,
@@ -1187,15 +1288,13 @@ export const prepareVaultManagerKit = (
1187
1288
  return makeVaultManagerKit;
1188
1289
  };
1189
1290
 
1291
+ /** @typedef {Awaited<ReturnType<ReturnType<typeof prepareVaultManagerKit>>>} VaultManagerKit */
1190
1292
  /**
1191
- * @typedef {Awaited<ReturnType<ReturnType<typeof prepareVaultManagerKit>>>} VaultManagerKit
1192
- */
1193
- /**
1194
- * @typedef {VaultManagerKit['self']} VaultManager
1195
- * Each VaultManager manages a single collateral type.
1293
+ * @typedef {VaultManagerKit['self']} VaultManager Each VaultManager manages a
1294
+ * single collateral type.
1196
1295
  *
1197
- * It manages some number of outstanding debt positions, each called a Vault,
1198
- * for which the collateral is provided in exchange for borrowed Minted.
1296
+ * It manages some number of outstanding debt positions, each called a Vault,
1297
+ * for which the collateral is provided in exchange for borrowed Minted.
1199
1298
  */
1200
1299
  /** @typedef {VaultManagerKit['collateral']} CollateralManager */
1201
1300