@agoric/portfolio-api 0.1.1-dev-72866d0.0.72866d0 → 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
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,6 +26,12 @@ 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;
|
|
29
35
|
/** Instruments for which deposit and/or withdraw is unavailable. */
|
|
30
36
|
instrumentBlocks?: {
|
|
31
37
|
noDepositInstruments: Set<AssetPlaceRef>;
|
|
@@ -48,10 +54,11 @@ export type ComputeTargetBalancesOptions<C extends AssetPlaceRef, T extends stri
|
|
|
48
54
|
* When target allocations cannot be satisfied, strive for proportionality and
|
|
49
55
|
* bend the rules for a withdrawal, but do not increase any position beyond its
|
|
50
56
|
* target allocation (opting instead to leave the excess at a non-instrument
|
|
51
|
-
* hub).
|
|
57
|
+
* hub). If blocked or liquidity-limited sources prevent a full withdrawal,
|
|
58
|
+
* withdraw as much as possible from the remaining available funds.
|
|
52
59
|
*
|
|
53
60
|
* Returns only entries whose values change by at least ACCOUNT_DUST_EPSILON
|
|
54
61
|
* compared to current.
|
|
55
62
|
*/
|
|
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>>;
|
|
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>>;
|
|
57
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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C,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,6 +47,12 @@ hardenOrFreeze(TargetBalanceError);
|
|
|
47
47
|
|
|
48
48
|
|
|
49
49
|
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
50
56
|
|
|
51
57
|
|
|
52
58
|
|
|
@@ -72,6 +78,12 @@ hardenOrFreeze(TargetBalanceError);
|
|
|
72
78
|
const placeRecordsByNetwork = new WeakMap
|
|
73
79
|
|
|
74
80
|
|
|
81
|
+
();
|
|
82
|
+
|
|
83
|
+
/** network -> destination chain -> source hub -> least movable amount */
|
|
84
|
+
const routeMinsByNetwork = new WeakMap
|
|
85
|
+
|
|
86
|
+
|
|
75
87
|
();
|
|
76
88
|
|
|
77
89
|
const failTargetBalance = (message ) => {
|
|
@@ -100,6 +112,14 @@ const bigintMin = (first , ...rest ) => {
|
|
|
100
112
|
}
|
|
101
113
|
return min;
|
|
102
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
|
+
};
|
|
103
123
|
// #endregion
|
|
104
124
|
|
|
105
125
|
const sortEntriesDesc = (
|
|
@@ -150,13 +170,53 @@ const isNonemptyPositionEntry = (entry ) => {
|
|
|
150
170
|
return isInstrumentId(place) && value > 0n;
|
|
151
171
|
};
|
|
152
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
|
+
|
|
153
212
|
/**
|
|
154
213
|
* Derive target balances for allocation keys, suppressing small changes and
|
|
155
214
|
* movements blocked by e.g. lack of instrument liquidity or available capacity.
|
|
156
215
|
* When target allocations cannot be satisfied, strive for proportionality and
|
|
157
216
|
* bend the rules for a withdrawal, but do not increase any position beyond its
|
|
158
217
|
* target allocation (opting instead to leave the excess at a non-instrument
|
|
159
|
-
* hub).
|
|
218
|
+
* hub). If blocked or liquidity-limited sources prevent a full withdrawal,
|
|
219
|
+
* withdraw as much as possible from the remaining available funds.
|
|
160
220
|
*
|
|
161
221
|
* Returns only entries whose values change by at least ACCOUNT_DUST_EPSILON
|
|
162
222
|
* compared to current.
|
|
@@ -171,6 +231,7 @@ export const computeTargetBalances = (
|
|
|
171
231
|
targetAllocation = {},
|
|
172
232
|
network,
|
|
173
233
|
depositFromChain,
|
|
234
|
+
withdrawToChain,
|
|
174
235
|
instrumentBlocks,
|
|
175
236
|
instrumentLiquidity,
|
|
176
237
|
withdrawalLiquidityLimitBps = 10_000n,
|
|
@@ -344,20 +405,77 @@ export const computeTargetBalances = (
|
|
|
344
405
|
if (liquidTotal < 0n && balanceDelta < 0n) {
|
|
345
406
|
const fallback = Object.create(null) ;
|
|
346
407
|
let unsatisfied = -balanceDelta;
|
|
347
|
-
|
|
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(
|
|
348
417
|
typedEntries(currentValues) ,
|
|
349
418
|
)) {
|
|
419
|
+
const [place, value] = entry;
|
|
350
420
|
if (isDust(value)) break;
|
|
351
421
|
if (draft[place]?.blockWithdraw !== false) continue;
|
|
352
|
-
const
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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;
|
|
356
474
|
if (unsatisfied === 0n) return { ...fallback };
|
|
357
475
|
}
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
return {};
|
|
476
|
+
// Preserve any available withdrawal even when blocked or liquidity-limited
|
|
477
|
+
// positions prevent satisfying the full request.
|
|
478
|
+
return { ...fallback };
|
|
361
479
|
}
|
|
362
480
|
|
|
363
481
|
// Blocked/suppressed *sinks* just leave funds in a source chain account.
|