@agoric/portfolio-api 0.1.1-dev-9da7d27.0.9da7d27 → 0.1.1-dev-72866d0.0.72866d0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/portfolio-api",
3
- "version": "0.1.1-dev-9da7d27.0.9da7d27",
3
+ "version": "0.1.1-dev-72866d0.0.72866d0",
4
4
  "description": "API for Portfolio management",
5
5
  "repository": {
6
6
  "type": "git",
@@ -47,8 +47,8 @@
47
47
  "generate:ymax-machine": "npx tsx scripts/gen-ymax-machine.mts"
48
48
  },
49
49
  "dependencies": {
50
- "@agoric/internal": "0.3.3-dev-9da7d27.0.9da7d27",
51
- "@agoric/orchestration": "0.1.1-dev-9da7d27.0.9da7d27",
50
+ "@agoric/internal": "0.3.3-dev-72866d0.0.72866d0",
51
+ "@agoric/orchestration": "0.1.1-dev-72866d0.0.72866d0",
52
52
  "@endo/common": "^1.4.0",
53
53
  "@endo/errors": "^1.3.1",
54
54
  "@endo/patterns": "^1.9.1"
@@ -74,5 +74,5 @@
74
74
  "engines": {
75
75
  "node": "^22.11 || ^24.14"
76
76
  },
77
- "gitHead": "9da7d27d81eb8041c614c909dd593dd995ee5b04"
77
+ "gitHead": "72866d0bcfd6d6c23cb8980111572bb7934076fb"
78
78
  }
@@ -26,10 +26,21 @@ export type ComputeTargetBalancesOptions<C extends AssetPlaceRef, T extends stri
26
26
  * target sink is unavailable or suppressed.
27
27
  */
28
28
  depositFromChain?: SupportedChain;
29
+ /** Instruments for which deposit and/or withdraw is unavailable. */
29
30
  instrumentBlocks?: {
30
31
  noDepositInstruments: Set<AssetPlaceRef>;
31
32
  noWithdrawInstruments: Set<AssetPlaceRef>;
32
33
  };
34
+ /**
35
+ * Available instrument liquidity, in the same units as currentBalances.
36
+ * Hubs and missing entries default to unlimited liquidity.
37
+ */
38
+ instrumentLiquidity?: Partial<Record<AssetPlaceRef, NatValue>>;
39
+ /**
40
+ * Maximum fraction of each instrument's available liquidity to be considered
41
+ * for withdrawal, in basis points (defaults to no limit).
42
+ */
43
+ withdrawalLiquidityLimitBps?: NatValue;
33
44
  };
34
45
  /**
35
46
  * Derive target balances for allocation keys, suppressing small changes and
@@ -42,5 +53,5 @@ export type ComputeTargetBalancesOptions<C extends AssetPlaceRef, T extends stri
42
53
  * Returns only entries whose values change by at least ACCOUNT_DUST_EPSILON
43
54
  * compared to current.
44
55
  */
45
- export declare const computeTargetBalances: <C extends AssetPlaceRef, T extends string & keyof TargetAllocation>({ brand, currentBalances, balanceDelta, targetAllocation, network, depositFromChain, instrumentBlocks, }: ComputeTargetBalancesOptions<C, T>) => Partial<Record<C | T, NatAmount>>;
56
+ export declare const computeTargetBalances: <C extends AssetPlaceRef, T extends string & keyof TargetAllocation>({ brand, currentBalances, balanceDelta, targetAllocation, network, depositFromChain, instrumentBlocks, instrumentLiquidity, withdrawalLiquidityLimitBps, }: ComputeTargetBalancesOptions<C, T>) => Partial<Record<C | T, NatAmount>>;
46
57
  //# sourceMappingURL=target-balances.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"target-balances.d.ts","sourceRoot":"","sources":["target-balances.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAO5E,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAE3E,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAElE,OAAO,KAAK,EAEV,WAAW,EAEZ,MAAM,2BAA2B,CAAC;AAInC,eAAO,MAAM,sBAAsB,WAAa,CAAC;AAEjD,eAAO,MAAM,kBAAkB,EAA6B,gBAAgB,CAAC;AAG7E,MAAM,MAAM,4BAA4B,CACtC,CAAC,SAAS,aAAa,EACvB,CAAC,SAAS,MAAM,GAAG,MAAM,gBAAgB,I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
+
54
65
 
55
66
 
56
67
  ;
@@ -161,7 +172,17 @@ export const computeTargetBalances = (
161
172
  network,
162
173
  depositFromChain,
163
174
  instrumentBlocks,
175
+ instrumentLiquidity,
176
+ withdrawalLiquidityLimitBps = 10_000n,
164
177
  } ) => {
178
+ const computeWithdrawLimit = (place , current ) => {
179
+ const liquidity = isInstrumentId(place)
180
+ ? getOwn(instrumentLiquidity ?? {}, place)
181
+ : undefined;
182
+ if (liquidity === undefined) return current;
183
+ const limit = (liquidity * withdrawalLiquidityLimitBps) / 10_000n;
184
+ return limit <= 0n ? 0n : bigintMin(current, limit);
185
+ };
165
186
  const { noDepositInstruments, noWithdrawInstruments } =
166
187
  instrumentBlocks ?? {};
167
188
  const currentValues = objectMap(
@@ -249,11 +270,14 @@ export const computeTargetBalances = (
249
270
  // at current 52, and then 52/15/15/18 from the originally-a-sink D being
250
271
  // withdraw-blocked at current 18.
251
272
  const suppressions = new Map ();
252
- for (;;) {
273
+ const limitedSources = new Set ();
274
+ let done = false;
275
+ while (!done) {
253
276
  const badSources = [];
254
277
  const needsSuppress = [];
278
+ const newlyLimited = [];
255
279
  for (const [place, draftRecord] of typedEntries(draft)) {
256
- if (suppressions.has(place)) continue;
280
+ if (suppressions.has(place) || limitedSources.has(place)) continue;
257
281
  const { weight, current, blockDeposit, blockWithdraw } = draftRecord;
258
282
  const target = (liquidTotal * weight) / sumW; // rounds down
259
283
  /** positive delta is a sink, negative delta is a source */
@@ -263,15 +287,28 @@ export const computeTargetBalances = (
263
287
  const isBlocked =
264
288
  (delta > 0n && blockDeposit) || (delta < 0n && blockWithdraw);
265
289
  const isSuppressed = delta !== 0n && absDelta < draftRecord.deltaSoftMin;
290
+ const limit = computeWithdrawLimit(place, current);
266
291
  if (isBlocked && delta < 0n) {
267
292
  badSources.push([draftRecord, 0n]);
268
293
  } else if (isSuppressed && delta < 0n) {
269
294
  needsSuppress.push([draftRecord, draftRecord.deltaSoftMin - absDelta]);
295
+ } else if (delta < -limit) {
296
+ // Limit this source, then recompute the other targets.
297
+ if (limit && limit < draftRecord.deltaSoftMin) {
298
+ needsSuppress.push([draftRecord, draftRecord.deltaSoftMin - limit]);
299
+ draftRecord.resolvedDelta = 0n;
300
+ continue;
301
+ }
302
+ draftRecord.target = current - limit;
303
+ draftRecord.delta = -limit;
304
+ draftRecord.resolvedDelta = -limit;
305
+ newlyLimited.push(draftRecord);
306
+ continue;
270
307
  }
271
308
  draftRecord.resolvedDelta = isBlocked || isSuppressed ? 0n : delta;
272
309
  }
273
310
 
274
- if (badSources.length === 0) {
311
+ if (badSources.length === 0 && newlyLimited.length === 0) {
275
312
  // No sources to block, but there still might be suppressions.
276
313
  // Cement those with the largest gap.
277
314
  sortEntriesDesc(needsSuppress);
@@ -281,6 +318,11 @@ export const computeTargetBalances = (
281
318
  badSources.push(needsSuppress[i]);
282
319
  }
283
320
  }
321
+ for (const source of newlyLimited) {
322
+ limitedSources.add(source.place);
323
+ liquidTotal -= source.target;
324
+ sumW -= source.weight;
325
+ }
284
326
  for (const [source, gap] of badSources) {
285
327
  const { place, current, weight, blockWithdraw } = source;
286
328
  // A blocked source is not usable even as a withdrawal fallback.
@@ -290,7 +332,7 @@ export const computeTargetBalances = (
290
332
  liquidTotal -= current;
291
333
  sumW -= weight;
292
334
  }
293
- if (badSources.length === 0 || liquidTotal < 0n) break;
335
+ done = liquidTotal < 0n || (!badSources.length && !newlyLimited.length);
294
336
  }
295
337
 
296
338
  // If all sources are suppressed for a deposit or rebalance, we're done.
@@ -307,7 +349,8 @@ export const computeTargetBalances = (
307
349
  )) {
308
350
  if (isDust(value)) break;
309
351
  if (draft[place]?.blockWithdraw !== false) continue;
310
- const take = bigintMin(unsatisfied, value);
352
+ const take = bigintMin(unsatisfied, computeWithdrawLimit(place, value));
353
+ if (take === 0n) continue;
311
354
  fallback[place] = makeNatAmount(brand, value - take);
312
355
  unsatisfied -= take;
313
356
  if (unsatisfied === 0n) return { ...fallback };
@@ -338,12 +381,20 @@ export const computeTargetBalances = (
338
381
  const chainPlace = `@${chainName}` ;
339
382
  draft[chainPlace] ??= makeDraftRecord(chainPlace);
340
383
  draft[chainPlace].resolvedDelta += value;
341
- if (!pending.has(draft[chainPlace])) remainder -= value;
384
+ // A hub that will still be visited by the loop below accounts for its own
385
+ // claims, unless that visit is skipped for suppression/limitation.
386
+ if (
387
+ !pending.has(draft[chainPlace]) ||
388
+ suppressions.has(chainPlace) ||
389
+ limitedSources.has(chainPlace)
390
+ ) {
391
+ remainder -= value;
392
+ }
342
393
  };
343
394
  for (const draftRecord of pending) {
344
395
  pending.delete(draftRecord);
345
396
  const { place, current, delta, resolvedDelta } = draftRecord;
346
- if (suppressions.has(place)) continue;
397
+ if (suppressions.has(place) || limitedSources.has(place)) continue;
347
398
  // No adjustment is necessary for a source and/or satisfied delta.
348
399
  if (delta <= 0n || resolvedDelta !== 0n) {
349
400
  const newBalance = current + resolvedDelta;