@agoric/portfolio-api 0.1.1-dev-9da7d27.0.9da7d27 → 0.1.1-dev-c0273ce.0.c0273ce
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 +4 -4
- package/src/target-balances.d.ts +20 -2
- package/src/target-balances.d.ts.map +1 -1
- package/src/target-balances.js +183 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/portfolio-api",
|
|
3
|
-
"version": "0.1.1-dev-
|
|
3
|
+
"version": "0.1.1-dev-c0273ce.0.c0273ce",
|
|
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-
|
|
51
|
-
"@agoric/orchestration": "0.1.1-dev-
|
|
50
|
+
"@agoric/internal": "0.3.3-dev-c0273ce.0.c0273ce",
|
|
51
|
+
"@agoric/orchestration": "0.1.1-dev-c0273ce.0.c0273ce",
|
|
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": "
|
|
77
|
+
"gitHead": "c0273ce42fd422c3733f511ba29ac250264dc2ca"
|
|
78
78
|
}
|
package/src/target-balances.d.ts
CHANGED
|
@@ -26,10 +26,27 @@ export type ComputeTargetBalancesOptions<C extends AssetPlaceRef, T extends stri
|
|
|
26
26
|
* target sink is unavailable or suppressed.
|
|
27
27
|
*/
|
|
28
28
|
depositFromChain?: SupportedChain;
|
|
29
|
+
/**
|
|
30
|
+
* Chain receiving a withdrawal. When a withdrawal can only be partially
|
|
31
|
+
* satisfied, funds on this chain are preferred and funds on other chains are
|
|
32
|
+
* used only in amounts that satisfy the minimums of some route to it.
|
|
33
|
+
*/
|
|
34
|
+
withdrawToChain?: SupportedChain;
|
|
35
|
+
/** Instruments for which deposit and/or withdraw is unavailable. */
|
|
29
36
|
instrumentBlocks?: {
|
|
30
37
|
noDepositInstruments: Set<AssetPlaceRef>;
|
|
31
38
|
noWithdrawInstruments: Set<AssetPlaceRef>;
|
|
32
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Available instrument liquidity, in the same units as currentBalances.
|
|
42
|
+
* Hubs and missing entries default to unlimited liquidity.
|
|
43
|
+
*/
|
|
44
|
+
instrumentLiquidity?: Partial<Record<AssetPlaceRef, NatValue>>;
|
|
45
|
+
/**
|
|
46
|
+
* Maximum fraction of each instrument's available liquidity to be considered
|
|
47
|
+
* for withdrawal, in basis points (defaults to no limit).
|
|
48
|
+
*/
|
|
49
|
+
withdrawalLiquidityLimitBps?: NatValue;
|
|
33
50
|
};
|
|
34
51
|
/**
|
|
35
52
|
* Derive target balances for allocation keys, suppressing small changes and
|
|
@@ -37,10 +54,11 @@ export type ComputeTargetBalancesOptions<C extends AssetPlaceRef, T extends stri
|
|
|
37
54
|
* When target allocations cannot be satisfied, strive for proportionality and
|
|
38
55
|
* bend the rules for a withdrawal, but do not increase any position beyond its
|
|
39
56
|
* target allocation (opting instead to leave the excess at a non-instrument
|
|
40
|
-
* hub).
|
|
57
|
+
* hub). If blocked or liquidity-limited sources prevent a full withdrawal,
|
|
58
|
+
* withdraw as much as possible from the remaining available funds.
|
|
41
59
|
*
|
|
42
60
|
* Returns only entries whose values change by at least ACCOUNT_DUST_EPSILON
|
|
43
61
|
* compared to current.
|
|
44
62
|
*/
|
|
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>>;
|
|
63
|
+
export declare const computeTargetBalances: <C extends AssetPlaceRef, T extends string & keyof TargetAllocation>({ brand, currentBalances, balanceDelta, targetAllocation, network, depositFromChain, withdrawToChain, instrumentBlocks, instrumentLiquidity, withdrawalLiquidityLimitBps, }: ComputeTargetBalancesOptions<C, T>) => Partial<Record<C | T, NatAmount>>;
|
|
46
64
|
//# 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,IACvC;IACF;;;OAGG;IACH,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpB,qEAAqE;IACrE,eAAe,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC;IAC/C;;;OAGG;IACH,YAAY,CAAC,EAAE,QAAQ,CAAC;IACxB,4EAA4E;IAC5E,gBAAgB,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC,CAAC;IACtD,wDAAwD;IACxD,OAAO,EAAE,WAAW,CAAC;IACrB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,cAAc,CAAC;IAClC,gBAAgB,CAAC,EAAE;QACjB,oBAAoB,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;QACzC,qBAAqB,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;KAC3C,CAAC;
|
|
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,IACvC;IACF;;;OAGG;IACH,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpB,qEAAqE;IACrE,eAAe,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC;IAC/C;;;OAGG;IACH,YAAY,CAAC,EAAE,QAAQ,CAAC;IACxB,4EAA4E;IAC5E,gBAAgB,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC,CAAC;IACtD,wDAAwD;IACxD,OAAO,EAAE,WAAW,CAAC;IACrB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,cAAc,CAAC;IAClC;;;;OAIG;IACH,eAAe,CAAC,EAAE,cAAc,CAAC;IACjC,oEAAoE;IACpE,gBAAgB,CAAC,EAAE;QACjB,oBAAoB,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;QACzC,qBAAqB,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;KAC3C,CAAC;IACF;;;OAGG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC/D;;;OAGG;IACH,2BAA2B,CAAC,EAAE,QAAQ,CAAC;CACxC,CAAC;AA6IF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,qBAAqB,GAChC,CAAC,SAAS,aAAa,EACvB,CAAC,SAAS,MAAM,GAAG,MAAM,gBAAgB,EACzC,6KAWC,4BAA4B,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,CAsVvE,CAAC"}
|
package/src/target-balances.js
CHANGED
|
@@ -47,10 +47,27 @@ hardenOrFreeze(TargetBalanceError);
|
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
50
57
|
|
|
51
58
|
|
|
52
59
|
|
|
53
60
|
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
54
71
|
|
|
55
72
|
|
|
56
73
|
;
|
|
@@ -61,6 +78,12 @@ hardenOrFreeze(TargetBalanceError);
|
|
|
61
78
|
const placeRecordsByNetwork = new WeakMap
|
|
62
79
|
|
|
63
80
|
|
|
81
|
+
();
|
|
82
|
+
|
|
83
|
+
/** network -> destination chain -> source hub -> least movable amount */
|
|
84
|
+
const routeMinsByNetwork = new WeakMap
|
|
85
|
+
|
|
86
|
+
|
|
64
87
|
();
|
|
65
88
|
|
|
66
89
|
const failTargetBalance = (message ) => {
|
|
@@ -89,6 +112,14 @@ const bigintMin = (first , ...rest ) => {
|
|
|
89
112
|
}
|
|
90
113
|
return min;
|
|
91
114
|
};
|
|
115
|
+
|
|
116
|
+
const bigintMax = (first , ...rest ) => {
|
|
117
|
+
let max = first;
|
|
118
|
+
for (const arg of rest) {
|
|
119
|
+
if (arg > max) max = arg;
|
|
120
|
+
}
|
|
121
|
+
return max;
|
|
122
|
+
};
|
|
92
123
|
// #endregion
|
|
93
124
|
|
|
94
125
|
const sortEntriesDesc = (
|
|
@@ -139,13 +170,53 @@ const isNonemptyPositionEntry = (entry ) => {
|
|
|
139
170
|
return isInstrumentId(place) && value > 0n;
|
|
140
171
|
};
|
|
141
172
|
|
|
173
|
+
/**
|
|
174
|
+
* The least amount that can be moved from one chain hub to another, i.e., the
|
|
175
|
+
* minimum over all hub-to-hub routes of the largest link `min` along the route,
|
|
176
|
+
* or undefined if there is no route.
|
|
177
|
+
*/
|
|
178
|
+
const getRouteMin = (
|
|
179
|
+
network ,
|
|
180
|
+
fromChain ,
|
|
181
|
+
toChain ,
|
|
182
|
+
) => {
|
|
183
|
+
const routeMins = provideLazyMap(
|
|
184
|
+
provideLazyMap(routeMinsByNetwork, network, () => new Map()),
|
|
185
|
+
toChain,
|
|
186
|
+
() => {
|
|
187
|
+
// Bottleneck shortest path (Dijkstra) over reversed hub-to-hub links.
|
|
188
|
+
const best = new Map ([[`@${toChain}`, 0n]]);
|
|
189
|
+
const todo = new Set ([`@${toChain}`]);
|
|
190
|
+
while (todo.size) {
|
|
191
|
+
let node ;
|
|
192
|
+
for (const n of todo) {
|
|
193
|
+
if (node === undefined || best.get(n) < best.get(node) ) node = n;
|
|
194
|
+
}
|
|
195
|
+
todo.delete(node );
|
|
196
|
+
for (const { src, dest, min = 0n } of network.links) {
|
|
197
|
+
if (dest !== node || !src.startsWith('@')) continue;
|
|
198
|
+
const cost = bigintMax(best.get(node) , min);
|
|
199
|
+
const prev = best.get(src);
|
|
200
|
+
if (prev === undefined || cost < prev) {
|
|
201
|
+
best.set(src, cost);
|
|
202
|
+
todo.add(src);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return best;
|
|
207
|
+
},
|
|
208
|
+
);
|
|
209
|
+
return routeMins.get(`@${fromChain}` );
|
|
210
|
+
};
|
|
211
|
+
|
|
142
212
|
/**
|
|
143
213
|
* Derive target balances for allocation keys, suppressing small changes and
|
|
144
214
|
* movements blocked by e.g. lack of instrument liquidity or available capacity.
|
|
145
215
|
* When target allocations cannot be satisfied, strive for proportionality and
|
|
146
216
|
* bend the rules for a withdrawal, but do not increase any position beyond its
|
|
147
217
|
* target allocation (opting instead to leave the excess at a non-instrument
|
|
148
|
-
* hub).
|
|
218
|
+
* hub). If blocked or liquidity-limited sources prevent a full withdrawal,
|
|
219
|
+
* withdraw as much as possible from the remaining available funds.
|
|
149
220
|
*
|
|
150
221
|
* Returns only entries whose values change by at least ACCOUNT_DUST_EPSILON
|
|
151
222
|
* compared to current.
|
|
@@ -160,8 +231,19 @@ export const computeTargetBalances = (
|
|
|
160
231
|
targetAllocation = {},
|
|
161
232
|
network,
|
|
162
233
|
depositFromChain,
|
|
234
|
+
withdrawToChain,
|
|
163
235
|
instrumentBlocks,
|
|
236
|
+
instrumentLiquidity,
|
|
237
|
+
withdrawalLiquidityLimitBps = 10_000n,
|
|
164
238
|
} ) => {
|
|
239
|
+
const computeWithdrawLimit = (place , current ) => {
|
|
240
|
+
const liquidity = isInstrumentId(place)
|
|
241
|
+
? getOwn(instrumentLiquidity ?? {}, place)
|
|
242
|
+
: undefined;
|
|
243
|
+
if (liquidity === undefined) return current;
|
|
244
|
+
const limit = (liquidity * withdrawalLiquidityLimitBps) / 10_000n;
|
|
245
|
+
return limit <= 0n ? 0n : bigintMin(current, limit);
|
|
246
|
+
};
|
|
165
247
|
const { noDepositInstruments, noWithdrawInstruments } =
|
|
166
248
|
instrumentBlocks ?? {};
|
|
167
249
|
const currentValues = objectMap(
|
|
@@ -249,11 +331,14 @@ export const computeTargetBalances = (
|
|
|
249
331
|
// at current 52, and then 52/15/15/18 from the originally-a-sink D being
|
|
250
332
|
// withdraw-blocked at current 18.
|
|
251
333
|
const suppressions = new Map ();
|
|
252
|
-
|
|
334
|
+
const limitedSources = new Set ();
|
|
335
|
+
let done = false;
|
|
336
|
+
while (!done) {
|
|
253
337
|
const badSources = [];
|
|
254
338
|
const needsSuppress = [];
|
|
339
|
+
const newlyLimited = [];
|
|
255
340
|
for (const [place, draftRecord] of typedEntries(draft)) {
|
|
256
|
-
if (suppressions.has(place)) continue;
|
|
341
|
+
if (suppressions.has(place) || limitedSources.has(place)) continue;
|
|
257
342
|
const { weight, current, blockDeposit, blockWithdraw } = draftRecord;
|
|
258
343
|
const target = (liquidTotal * weight) / sumW; // rounds down
|
|
259
344
|
/** positive delta is a sink, negative delta is a source */
|
|
@@ -263,15 +348,28 @@ export const computeTargetBalances = (
|
|
|
263
348
|
const isBlocked =
|
|
264
349
|
(delta > 0n && blockDeposit) || (delta < 0n && blockWithdraw);
|
|
265
350
|
const isSuppressed = delta !== 0n && absDelta < draftRecord.deltaSoftMin;
|
|
351
|
+
const limit = computeWithdrawLimit(place, current);
|
|
266
352
|
if (isBlocked && delta < 0n) {
|
|
267
353
|
badSources.push([draftRecord, 0n]);
|
|
268
354
|
} else if (isSuppressed && delta < 0n) {
|
|
269
355
|
needsSuppress.push([draftRecord, draftRecord.deltaSoftMin - absDelta]);
|
|
356
|
+
} else if (delta < -limit) {
|
|
357
|
+
// Limit this source, then recompute the other targets.
|
|
358
|
+
if (limit && limit < draftRecord.deltaSoftMin) {
|
|
359
|
+
needsSuppress.push([draftRecord, draftRecord.deltaSoftMin - limit]);
|
|
360
|
+
draftRecord.resolvedDelta = 0n;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
draftRecord.target = current - limit;
|
|
364
|
+
draftRecord.delta = -limit;
|
|
365
|
+
draftRecord.resolvedDelta = -limit;
|
|
366
|
+
newlyLimited.push(draftRecord);
|
|
367
|
+
continue;
|
|
270
368
|
}
|
|
271
369
|
draftRecord.resolvedDelta = isBlocked || isSuppressed ? 0n : delta;
|
|
272
370
|
}
|
|
273
371
|
|
|
274
|
-
if (badSources.length === 0) {
|
|
372
|
+
if (badSources.length === 0 && newlyLimited.length === 0) {
|
|
275
373
|
// No sources to block, but there still might be suppressions.
|
|
276
374
|
// Cement those with the largest gap.
|
|
277
375
|
sortEntriesDesc(needsSuppress);
|
|
@@ -281,6 +379,11 @@ export const computeTargetBalances = (
|
|
|
281
379
|
badSources.push(needsSuppress[i]);
|
|
282
380
|
}
|
|
283
381
|
}
|
|
382
|
+
for (const source of newlyLimited) {
|
|
383
|
+
limitedSources.add(source.place);
|
|
384
|
+
liquidTotal -= source.target;
|
|
385
|
+
sumW -= source.weight;
|
|
386
|
+
}
|
|
284
387
|
for (const [source, gap] of badSources) {
|
|
285
388
|
const { place, current, weight, blockWithdraw } = source;
|
|
286
389
|
// A blocked source is not usable even as a withdrawal fallback.
|
|
@@ -290,7 +393,7 @@ export const computeTargetBalances = (
|
|
|
290
393
|
liquidTotal -= current;
|
|
291
394
|
sumW -= weight;
|
|
292
395
|
}
|
|
293
|
-
|
|
396
|
+
done = liquidTotal < 0n || (!badSources.length && !newlyLimited.length);
|
|
294
397
|
}
|
|
295
398
|
|
|
296
399
|
// If all sources are suppressed for a deposit or rebalance, we're done.
|
|
@@ -302,19 +405,77 @@ export const computeTargetBalances = (
|
|
|
302
405
|
if (liquidTotal < 0n && balanceDelta < 0n) {
|
|
303
406
|
const fallback = Object.create(null) ;
|
|
304
407
|
let unsatisfied = -balanceDelta;
|
|
305
|
-
|
|
408
|
+
// When the destination is known, group sources by chain (destination
|
|
409
|
+
// first) so that a remote chain contributes only if its total can actually
|
|
410
|
+
// be moved. Otherwise, treat all sources as a single group.
|
|
411
|
+
const destChain = withdrawToChain
|
|
412
|
+
? getPlaceData(`@${withdrawToChain}` , network).chain
|
|
413
|
+
: undefined;
|
|
414
|
+
const groups = new Map ();
|
|
415
|
+
if (destChain) groups.set(destChain.name, []);
|
|
416
|
+
for (const entry of sortEntriesDesc(
|
|
306
417
|
typedEntries(currentValues) ,
|
|
307
418
|
)) {
|
|
419
|
+
const [place, value] = entry;
|
|
308
420
|
if (isDust(value)) break;
|
|
309
421
|
if (draft[place]?.blockWithdraw !== false) continue;
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
422
|
+
const key = destChain && draft[place].chain.name;
|
|
423
|
+
const group = groups.get(key);
|
|
424
|
+
if (group) group.push(entry);
|
|
425
|
+
else groups.set(key, [entry]);
|
|
426
|
+
}
|
|
427
|
+
const takeFrom = (sources , want ) => {
|
|
428
|
+
const takes = [];
|
|
429
|
+
let taken = 0n;
|
|
430
|
+
for (const [place, value] of sources) {
|
|
431
|
+
const available = computeWithdrawLimit(place, value);
|
|
432
|
+
const take = bigintMin(want - taken, available);
|
|
433
|
+
if (take === 0n) continue;
|
|
434
|
+
takes.push([place, value, take]);
|
|
435
|
+
taken += take;
|
|
436
|
+
if (taken === want) break;
|
|
437
|
+
}
|
|
438
|
+
return { takes, taken };
|
|
439
|
+
};
|
|
440
|
+
let destTakes = [];
|
|
441
|
+
for (const [chainName, sources] of groups) {
|
|
442
|
+
let { takes, taken } = takeFrom(sources, unsatisfied);
|
|
443
|
+
if (destChain && chainName === destChain.name) {
|
|
444
|
+
destTakes = takes;
|
|
445
|
+
} else if (destChain) {
|
|
446
|
+
const routeMin = getRouteMin(network, chainName , destChain.name);
|
|
447
|
+
if (routeMin === undefined) continue; // unreachable
|
|
448
|
+
if (taken < routeMin) {
|
|
449
|
+
// Too little to move from this chain. But if the chain has enough
|
|
450
|
+
// and only the remaining need is too small, move the route minimum
|
|
451
|
+
// and take the difference less from the destination chain.
|
|
452
|
+
const shortfall = routeMin - taken;
|
|
453
|
+
const destTotal = destTakes.reduce((acc, t) => acc + t[2], 0n);
|
|
454
|
+
if (taken !== unsatisfied || destTotal < shortfall) continue;
|
|
455
|
+
({ takes, taken } = takeFrom(sources, routeMin));
|
|
456
|
+
if (taken < routeMin) continue;
|
|
457
|
+
let excess = shortfall;
|
|
458
|
+
for (const destTake of destTakes.toReversed()) {
|
|
459
|
+
const [place, value, take] = destTake;
|
|
460
|
+
const giveBack = bigintMin(excess, take);
|
|
461
|
+
destTake[2] = take - giveBack;
|
|
462
|
+
if (destTake[2] === 0n) delete fallback[place];
|
|
463
|
+
else fallback[place] = makeNatAmount(brand, value - destTake[2]);
|
|
464
|
+
excess -= giveBack;
|
|
465
|
+
if (excess === 0n) break;
|
|
466
|
+
}
|
|
467
|
+
unsatisfied += shortfall;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
for (const [place, value, take] of takes) {
|
|
471
|
+
fallback[place] = makeNatAmount(brand, value - take);
|
|
472
|
+
}
|
|
473
|
+
unsatisfied -= taken;
|
|
313
474
|
if (unsatisfied === 0n) return { ...fallback };
|
|
314
475
|
}
|
|
315
|
-
//
|
|
316
|
-
//
|
|
317
|
-
return {};
|
|
476
|
+
// Preserve any available withdrawal even when blocked or liquidity-limited
|
|
477
|
+
// positions prevent satisfying the full request.
|
|
478
|
+
return { ...fallback };
|
|
318
479
|
}
|
|
319
480
|
|
|
320
481
|
// Blocked/suppressed *sinks* just leave funds in a source chain account.
|
|
@@ -338,12 +499,20 @@ export const computeTargetBalances = (
|
|
|
338
499
|
const chainPlace = `@${chainName}` ;
|
|
339
500
|
draft[chainPlace] ??= makeDraftRecord(chainPlace);
|
|
340
501
|
draft[chainPlace].resolvedDelta += value;
|
|
341
|
-
|
|
502
|
+
// A hub that will still be visited by the loop below accounts for its own
|
|
503
|
+
// claims, unless that visit is skipped for suppression/limitation.
|
|
504
|
+
if (
|
|
505
|
+
!pending.has(draft[chainPlace]) ||
|
|
506
|
+
suppressions.has(chainPlace) ||
|
|
507
|
+
limitedSources.has(chainPlace)
|
|
508
|
+
) {
|
|
509
|
+
remainder -= value;
|
|
510
|
+
}
|
|
342
511
|
};
|
|
343
512
|
for (const draftRecord of pending) {
|
|
344
513
|
pending.delete(draftRecord);
|
|
345
514
|
const { place, current, delta, resolvedDelta } = draftRecord;
|
|
346
|
-
if (suppressions.has(place)) continue;
|
|
515
|
+
if (suppressions.has(place) || limitedSources.has(place)) continue;
|
|
347
516
|
// No adjustment is necessary for a source and/or satisfied delta.
|
|
348
517
|
if (delta <= 0n || resolvedDelta !== 0n) {
|
|
349
518
|
const newBalance = current + resolvedDelta;
|