@naviprotocol/multiply 0.2.0-preview.20261102.2 → 0.2.0-preview.20261118.2
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/README.md +132 -170
- package/dist/cap-registry.d.ts +18 -0
- package/dist/cap-registry.d.ts.map +1 -0
- package/dist/cap-registry.js +85 -0
- package/dist/cap-registry.js.map +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +390 -320
- package/dist/client.js.map +1 -1
- package/dist/converters.d.ts.map +1 -1
- package/dist/converters.js +200 -140
- package/dist/converters.js.map +1 -1
- package/dist/data-source.d.ts +5 -2
- package/dist/data-source.d.ts.map +1 -1
- package/dist/data-source.js +233 -169
- package/dist/data-source.js.map +1 -1
- package/dist/funding.d.ts +5 -0
- package/dist/funding.d.ts.map +1 -0
- package/dist/funding.js +68 -0
- package/dist/funding.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -15
- package/dist/index.js.map +1 -1
- package/dist/planner.d.ts +1 -1
- package/dist/planner.d.ts.map +1 -1
- package/dist/planner.js +276 -187
- package/dist/planner.js.map +1 -1
- package/dist/strategies.d.ts +8 -1
- package/dist/strategies.d.ts.map +1 -1
- package/dist/strategies.js +13 -12
- package/dist/strategies.js.map +1 -1
- package/dist/types.d.ts +69 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/planner.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { check as
|
|
2
|
-
import { validateConversion as
|
|
3
|
-
import { atomPrices as
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
const
|
|
1
|
+
import { check as r, uint as X, decimal as S, BN as h, sameCoin as k, ceil as F, floor as D, MultiplyError as B } from "./utils.js";
|
|
2
|
+
import { validateConversion as ne } from "./converters.js";
|
|
3
|
+
import { atomPrices as ae, amountCushion as le, borrowCapacity as pe, supplyCapacity as ce, withinLtv as de } from "./amounts.js";
|
|
4
|
+
const o = (i) => new h(i);
|
|
5
|
+
function ee(i, n) {
|
|
6
|
+
const E = ae(n), b = o(i.collateralAmount).times(E.collateral), u = o(i.debtAmount).times(E.debt), L = b.minus(u);
|
|
7
7
|
return {
|
|
8
|
-
healthFactor:
|
|
9
|
-
leverage:
|
|
8
|
+
healthFactor: u.isZero() ? null : b.times(n.liquidationThreshold).div(u.times(n.borrowWeight)).toFixed(12),
|
|
9
|
+
leverage: b.isZero() && u.isZero() || L.lte(0) ? null : b.div(L).toFixed(12)
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
|
|
12
|
+
function Ne(i, n) {
|
|
13
|
+
r(
|
|
14
14
|
[
|
|
15
15
|
"open",
|
|
16
16
|
"add_principal",
|
|
@@ -19,65 +19,78 @@ function ce(t, i) {
|
|
|
19
19
|
"repay",
|
|
20
20
|
"add_collateral",
|
|
21
21
|
"close"
|
|
22
|
-
].includes(
|
|
22
|
+
].includes(i?.type),
|
|
23
23
|
"INVALID_ARGUMENT",
|
|
24
24
|
"Unknown Multiply action"
|
|
25
|
-
), ["open", "add_principal", "remove_principal", "repay", "add_collateral"].includes(
|
|
26
|
-
Number.isInteger(
|
|
25
|
+
), ["open", "add_principal", "remove_principal", "repay", "add_collateral"].includes(i.type) && (r("amount" in i, "INVALID_ARGUMENT", "This action requires an amount"), X(i.amount, !1)), (i.type === "open" || i.type === "set_leverage") && S(i.targetLeverage, "1"), "targetLeverage" in i && i.targetLeverage !== void 0 && S(i.targetLeverage, "1"), r(
|
|
26
|
+
Number.isInteger(n?.maxSlippageBps) && n.maxSlippageBps >= 0 && n.maxSlippageBps < 1e4,
|
|
27
27
|
"INVALID_ARGUMENT",
|
|
28
28
|
"Slippage must be integer bps in [0, 10000)"
|
|
29
|
-
),
|
|
29
|
+
), S(n.minHealthFactor, "1"), n.maxLeverage !== void 0 && S(n.maxLeverage, "1");
|
|
30
30
|
}
|
|
31
|
-
async function
|
|
32
|
-
const
|
|
33
|
-
for (let
|
|
34
|
-
const
|
|
35
|
-
|
|
31
|
+
async function Ae(i, n, E) {
|
|
32
|
+
const b = n.filter((v) => v.supports(i.coinIn, i.coinOut)), u = await Promise.allSettled(b.map((v) => v.quote(i))), L = [];
|
|
33
|
+
for (let v = 0; v < u.length; v++) {
|
|
34
|
+
const a = u[v];
|
|
35
|
+
if (a.status === "fulfilled")
|
|
36
|
+
ne(a.value, i, b[v].id), L.push(a.value);
|
|
37
|
+
else if (!(a.reason instanceof B) || !["CONVERTER_UNAVAILABLE", "CONVERSION_AMOUNT_TOO_SMALL", "NO_CONVERSION_ROUTE"].includes(
|
|
38
|
+
a.reason.code
|
|
39
|
+
))
|
|
40
|
+
throw a.reason;
|
|
36
41
|
}
|
|
37
|
-
if (
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
if (L.length)
|
|
43
|
+
return L.sort((v, a) => o(a.minOutput.amount).comparedTo(v.minOutput.amount) || v.converterId.localeCompare(a.converterId))[0];
|
|
44
|
+
r(
|
|
45
|
+
E.supports(i.coinIn, i.coinOut),
|
|
41
46
|
"NO_CONVERSION_ROUTE",
|
|
42
47
|
"No converter supports this pair"
|
|
43
48
|
);
|
|
44
49
|
try {
|
|
45
|
-
const
|
|
46
|
-
return
|
|
47
|
-
} catch (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const v = await E.quote(i);
|
|
51
|
+
return ne(v, i, E.id), v;
|
|
52
|
+
} catch (v) {
|
|
53
|
+
if (!(v instanceof B) || !["CONVERTER_UNAVAILABLE", "CONVERSION_AMOUNT_TOO_SMALL", "NO_CONVERSION_ROUTE"].includes(
|
|
54
|
+
v.code
|
|
55
|
+
))
|
|
56
|
+
throw v;
|
|
57
|
+
const a = v.code === "CONVERSION_AMOUNT_TOO_SMALL" && u.every(
|
|
58
|
+
(p) => p.status === "rejected" && p.reason instanceof B && p.reason.code === "CONVERSION_AMOUNT_TOO_SMALL"
|
|
59
|
+
);
|
|
60
|
+
throw new B(
|
|
61
|
+
a ? "CONVERSION_AMOUNT_TOO_SMALL" : "NO_CONVERSION_ROUTE",
|
|
62
|
+
"No eligible executable conversion route"
|
|
51
63
|
);
|
|
52
64
|
}
|
|
53
65
|
}
|
|
54
|
-
async function
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
async function Te(i, n, E, b, u, L, v) {
|
|
67
|
+
const a = o(b.collateralAmount), p = o(b.debtAmount), { collateral: N, debt: T } = ae(u);
|
|
68
|
+
r(
|
|
69
|
+
N.gt(0) && T.gt(0) && S(u.ltv).lte(1) && S(u.liquidationThreshold).lte(1) && S(u.borrowWeight).gte(1),
|
|
58
70
|
"READ_FAILED",
|
|
59
71
|
"Invalid market risk or price data"
|
|
60
72
|
);
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
const Z = a.times(N).minus(p.times(T));
|
|
74
|
+
r(
|
|
75
|
+
a.isZero() && p.isZero() || Z.gt(0),
|
|
64
76
|
"POSITION_UNSUPPORTED",
|
|
65
77
|
"Position has non-positive equity"
|
|
66
78
|
);
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
79
|
+
const Y = ee(b, u), se = "targetLeverage" in n ? n.targetLeverage : void 0, s = S(se ?? Y.leverage ?? "1", "1");
|
|
80
|
+
n.type === "set_leverage" && r(
|
|
81
|
+
s.minus(Y.leverage ?? "1").abs().gt("0.000001"),
|
|
70
82
|
"NO_OP",
|
|
71
83
|
"Position already has the requested leverage"
|
|
72
|
-
),
|
|
73
|
-
Number.isSafeInteger(
|
|
84
|
+
), X(L), X(u.flash.feeBps), r(
|
|
85
|
+
Number.isSafeInteger(u.observedAt) && Date.now() - u.observedAt <= 12e4 && u.observedAt <= Date.now() + 5e3,
|
|
74
86
|
"STALE_QUOTE",
|
|
75
87
|
"Market snapshot is stale"
|
|
76
|
-
),
|
|
77
|
-
const
|
|
78
|
-
version:
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
), E.maxLeverage && r(s.lte(E.maxLeverage), "CONSTRAINT_VIOLATION", "Target exceeds maxLeverage");
|
|
89
|
+
const M = [], P = [], Q = [], t = {
|
|
90
|
+
version: 4,
|
|
91
|
+
funding: { kind: "none" },
|
|
92
|
+
snapshot: b,
|
|
93
|
+
market: u,
|
|
81
94
|
conversions: [],
|
|
82
95
|
direction: "increase",
|
|
83
96
|
flashAmount: "0",
|
|
@@ -85,212 +98,288 @@ async function fe(t, i, h, A, n, E, p) {
|
|
|
85
98
|
borrowAmount: "0",
|
|
86
99
|
borrowFee: "0",
|
|
87
100
|
withdrawAmount: "0",
|
|
88
|
-
collateralAfter:
|
|
89
|
-
debtAfter:
|
|
90
|
-
minCollateralAfter:
|
|
91
|
-
},
|
|
92
|
-
const
|
|
93
|
-
return
|
|
94
|
-
},
|
|
95
|
-
let
|
|
96
|
-
const z = async (
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
++
|
|
101
|
+
collateralAfter: a.toFixed(0),
|
|
102
|
+
debtAfter: p.toFixed(0),
|
|
103
|
+
minCollateralAfter: a.toFixed(0)
|
|
104
|
+
}, W = (l) => o(F(l.times(o(u.flash.feeBps).div(1e4).plus(1)))), re = (l) => o(D(l.times(L).div(1e4))), U = (l, f) => {
|
|
105
|
+
const C = l.times(N), g = C.minus(f.times(T));
|
|
106
|
+
return g.gt(0) && C.div(g).minus(s).abs().lte(s.times("0.0001"));
|
|
107
|
+
}, ue = Date.now() + 45e3;
|
|
108
|
+
let me = 0;
|
|
109
|
+
const z = async (l, f) => {
|
|
110
|
+
const C = ue - Date.now();
|
|
111
|
+
r(
|
|
112
|
+
++me <= 32 && C > 0,
|
|
100
113
|
"NO_CONVERGENCE",
|
|
101
114
|
"Conversion quote budget exhausted"
|
|
102
115
|
);
|
|
103
|
-
let
|
|
116
|
+
let g;
|
|
104
117
|
try {
|
|
105
118
|
return await Promise.race([
|
|
106
|
-
|
|
107
|
-
amountIn:
|
|
108
|
-
coinIn:
|
|
109
|
-
coinOut:
|
|
110
|
-
slippageBps:
|
|
119
|
+
v({
|
|
120
|
+
amountIn: l,
|
|
121
|
+
coinIn: f ? i.debtCoinType : i.collateralCoinType,
|
|
122
|
+
coinOut: f ? i.collateralCoinType : i.debtCoinType,
|
|
123
|
+
slippageBps: E.maxSlippageBps
|
|
111
124
|
}),
|
|
112
|
-
new Promise((
|
|
113
|
-
|
|
114
|
-
() =>
|
|
115
|
-
|
|
125
|
+
new Promise((m, _) => {
|
|
126
|
+
g = setTimeout(
|
|
127
|
+
() => _(new B("NO_CONVERGENCE", "Conversion quote time budget exhausted")),
|
|
128
|
+
C
|
|
116
129
|
);
|
|
117
130
|
})
|
|
118
131
|
]);
|
|
119
132
|
} finally {
|
|
120
|
-
clearTimeout(
|
|
133
|
+
clearTimeout(g);
|
|
121
134
|
}
|
|
122
|
-
},
|
|
123
|
-
if (
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
),
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
} else
|
|
132
|
-
else if (
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
const
|
|
135
|
+
}, te = (l, f) => M.push({ coinType: l, amount: f });
|
|
136
|
+
if (n.type === "repay" || n.type === "add_collateral")
|
|
137
|
+
if (t.direction = n.type, te(
|
|
138
|
+
n.type === "repay" ? i.debtCoinType : i.collateralCoinType,
|
|
139
|
+
n.amount
|
|
140
|
+
), n.type === "repay") {
|
|
141
|
+
t.debtAfter = h.maximum(0, p.minus(n.amount)).toFixed(0);
|
|
142
|
+
const l = h.maximum(0, o(n.amount).minus(p)).toFixed(0);
|
|
143
|
+
l !== "0" && P.push({ coinType: i.debtCoinType, amount: l });
|
|
144
|
+
} else t.collateralAfter = t.minCollateralAfter = a.plus(n.amount).toFixed(0);
|
|
145
|
+
else if (n.type === "open" || n.type === "add_principal" || n.type === "set_leverage" && s.gt(Y.leverage ?? "1")) {
|
|
146
|
+
const l = o("amount" in n ? n.amount : "0"), f = "inputCoinType" in n && n.inputCoinType ? n.inputCoinType : i.principalCoinType, C = k(f, i.collateralCoinType);
|
|
147
|
+
l.gt(0) && te(f, l.toFixed(0));
|
|
148
|
+
const g = async (I) => {
|
|
149
|
+
const q = I.plus(C ? 0 : l), H = q.gt(0) ? await z(q.toFixed(0), !0) : void 0, V = W(I), G = re(V), ie = a.plus(C ? l : 0).plus(H?.expectedOutput.amount ?? 0), oe = p.plus(V).plus(G);
|
|
137
150
|
return {
|
|
138
|
-
q:
|
|
139
|
-
F:
|
|
140
|
-
repayment:
|
|
141
|
-
fee:
|
|
142
|
-
c:
|
|
143
|
-
d:
|
|
144
|
-
residual:
|
|
151
|
+
q: H,
|
|
152
|
+
F: I,
|
|
153
|
+
repayment: V,
|
|
154
|
+
fee: G,
|
|
155
|
+
c: ie,
|
|
156
|
+
d: oe,
|
|
157
|
+
residual: oe.times(T).minus(ie.times(N).times(s.minus(1)).div(s))
|
|
145
158
|
};
|
|
146
|
-
},
|
|
147
|
-
|
|
159
|
+
}, m = o(
|
|
160
|
+
F(
|
|
161
|
+
Z.plus(l.times(C ? N : T)).times(s.minus(1)).div(T).times(2).plus(1)
|
|
162
|
+
)
|
|
148
163
|
);
|
|
149
|
-
let
|
|
150
|
-
const
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
!
|
|
164
|
+
let _ = o(0), d = m, e, c, w = o(0);
|
|
165
|
+
const A = async () => {
|
|
166
|
+
if (_ = o(0), d = m, e = l.gt(0) ? await g(_) : void 0, e && U(e.c, e.d)) return !0;
|
|
167
|
+
r(
|
|
168
|
+
!e || e.residual.lt(0),
|
|
154
169
|
"ACTION_UNSUPPORTED",
|
|
155
170
|
"This contribution requires deleveraging; use set_leverage separately"
|
|
156
|
-
),
|
|
157
|
-
for (let
|
|
158
|
-
|
|
159
|
-
return
|
|
171
|
+
), w = e?.residual ?? p.times(T).minus(a.times(N).times(s.minus(1)).div(s)), c = await g(d);
|
|
172
|
+
for (let I = 0; c.residual.lt(0) && I < 4 && !U(c.c, c.d); I++)
|
|
173
|
+
d = d.times(2), c = await g(d);
|
|
174
|
+
return U(c.c, c.d) ? (e = c, !0) : (r(c.residual.gte(0), "NO_CONVERGENCE", "Could not bracket leverage"), !1);
|
|
160
175
|
};
|
|
161
|
-
let O = await
|
|
162
|
-
for (let
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
176
|
+
let O = await A(), x = 0, y = 0, R;
|
|
177
|
+
for (let I = 0; !O && I < 24; I++) {
|
|
178
|
+
const q = d.minus(_), H = o(
|
|
179
|
+
D(
|
|
180
|
+
_.plus(q.times(w.negated()).div(c.residual.minus(w)))
|
|
166
181
|
)
|
|
167
|
-
),
|
|
168
|
-
if (
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
182
|
+
), V = h.maximum(_.plus(1), h.minimum(d, H));
|
|
183
|
+
if (e = await g(V), O = U(e.c, e.d), O) break;
|
|
184
|
+
const G = e.residual.abs();
|
|
185
|
+
y = R && G.gte(R.times("0.9")) ? y + 1 : 0, R = G, q.lte(1) || y >= 3 ? (r(
|
|
186
|
+
x < 2,
|
|
172
187
|
"NO_CONVERGENCE",
|
|
173
188
|
"Conversion quotes could not achieve the requested leverage within tolerance"
|
|
174
|
-
),
|
|
189
|
+
), x++, O = await A(), y = 0, R = void 0) : e.residual.lt(0) ? (_ = V, w = e.residual) : (d = V, c = e);
|
|
175
190
|
}
|
|
176
|
-
|
|
177
|
-
O &&
|
|
191
|
+
r(
|
|
192
|
+
O && e,
|
|
178
193
|
"NO_CONVERGENCE",
|
|
179
194
|
"Conversion quotes could not achieve the requested leverage within tolerance"
|
|
180
|
-
),
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
195
|
+
), t.conversions = e.q ? [
|
|
196
|
+
{
|
|
197
|
+
...e.q,
|
|
198
|
+
id: "increase",
|
|
199
|
+
purpose: e.F.gt(0) ? "leverage" : "input",
|
|
200
|
+
source: C || l.isZero() ? "flash" : e.F.gt(0) ? "wallet-and-flash" : "wallet"
|
|
201
|
+
}
|
|
202
|
+
] : [], t.flashAmount = e.F.toFixed(0), t.flashRepayment = t.borrowAmount = e.repayment.toFixed(0), t.borrowFee = e.fee.toFixed(0), t.collateralAfter = e.c.toFixed(0), t.minCollateralAfter = a.plus(C ? l : 0).plus(e.q?.minOutput.amount ?? 0).toFixed(0), t.debtAfter = e.d.toFixed(0);
|
|
203
|
+
} else if (k(i.principalCoinType, i.collateralCoinType)) {
|
|
204
|
+
t.direction = "decrease", r(a.gt(0), "POSITION_NOT_FOUND", "No collateral to withdraw");
|
|
205
|
+
const l = n.type === "close", f = o(n.type === "remove_principal" ? n.amount : 0);
|
|
206
|
+
r(
|
|
207
|
+
l || f.times(N).lt(Z),
|
|
186
208
|
"CONSTRAINT_VIOLATION",
|
|
187
209
|
"Withdrawal consumes all equity; use close"
|
|
188
210
|
);
|
|
189
|
-
const
|
|
190
|
-
const O = await z(
|
|
211
|
+
const C = p.isZero() ? p : o(F(p.times("1.0001").plus(le(u.debt)))), g = async (A) => {
|
|
212
|
+
const O = A.gt(0) ? await z(A.toFixed(0), !1) : void 0, x = o(O?.minOutput.amount ?? 0), y = l || s.eq(1) ? C : h.minimum(p, o(D(x.div(o(u.flash.feeBps).div(1e4).plus(1))))), R = l ? a : A.plus(f), I = a.minus(R), q = h.maximum(0, p.minus(y));
|
|
191
213
|
return {
|
|
192
214
|
q: O,
|
|
193
|
-
W:
|
|
194
|
-
R:
|
|
195
|
-
|
|
215
|
+
W: A,
|
|
216
|
+
R: y,
|
|
217
|
+
total: R,
|
|
218
|
+
c: I,
|
|
219
|
+
d: q,
|
|
220
|
+
repayment: W(y),
|
|
221
|
+
available: x,
|
|
222
|
+
residual: q.times(T).minus(I.times(N).times(s.minus(1)).div(s))
|
|
223
|
+
};
|
|
224
|
+
}, m = l || s.eq(1), _ = a.minus(f);
|
|
225
|
+
let d = p.isZero() ? o(0) : l || s.eq(1) ? o(F(W(C).times(T).div(N).times("1.01"))) : o(
|
|
226
|
+
F(
|
|
227
|
+
p.times(T).minus(a.minus(f).times(N).times(s.minus(1)).div(s)).times(s).div(N)
|
|
228
|
+
)
|
|
229
|
+
);
|
|
230
|
+
m && (d = h.minimum(d, _));
|
|
231
|
+
let e;
|
|
232
|
+
for (let A = 0; A < 20; A++) {
|
|
233
|
+
r(
|
|
234
|
+
d.gte(0) && d.plus(l ? 0 : f).lte(a),
|
|
235
|
+
"CONSTRAINT_VIOLATION",
|
|
236
|
+
"Insufficient collateral to repay debt and withdraw principal"
|
|
237
|
+
), e = await g(d);
|
|
238
|
+
const O = e.repayment.minus(e.available);
|
|
239
|
+
if (r(
|
|
240
|
+
!m || d.lt(_) || O.lte(0),
|
|
241
|
+
"CONSTRAINT_VIOLATION",
|
|
242
|
+
"Insufficient collateral to repay debt and withdraw principal"
|
|
243
|
+
), (l || s.eq(1)) && O.lt(0) && d.gt(0) && A < 2) {
|
|
244
|
+
const R = o(F(d.times(e.repayment).div(e.available)));
|
|
245
|
+
if (R.gt(0) && R.lt(d)) {
|
|
246
|
+
d = R;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (O.lte(0) && (l || U(e.c, e.d))) break;
|
|
251
|
+
const x = l || s.eq(1) ? O.times(T).div(N) : e.residual.times(s).div(N);
|
|
252
|
+
let y = h.maximum(1, o(F(d.plus(x.times("1.01")).plus(O.gt(0) ? 1 : 0))));
|
|
253
|
+
m && (y = h.minimum(y, _)), r(A < 19 && !y.eq(d), "NO_CONVERGENCE", "No feasible collateral-principal decrease"), d = y;
|
|
254
|
+
}
|
|
255
|
+
r(
|
|
256
|
+
e && e.repayment.lte(e.available),
|
|
257
|
+
"NO_CONVERGENCE",
|
|
258
|
+
"Missing collateral-principal decrease plan"
|
|
259
|
+
), t.conversions = e.q ? [{ ...e.q, id: "repayment", purpose: "repayment", source: "withdrawal" }] : [], t.flashAmount = e.R.toFixed(0), t.flashRepayment = e.repayment.toFixed(0), t.withdrawAmount = e.total.toFixed(0), t.collateralAfter = t.minCollateralAfter = e.c.toFixed(0), t.debtAfter = e.d.toFixed(0);
|
|
260
|
+
const c = e.total.minus(e.W);
|
|
261
|
+
c.gt(0) && (P.push({ coinType: i.principalCoinType, amount: c.toFixed(0) }), Q.push({ coinType: i.principalCoinType, amount: c.toFixed(0) }));
|
|
262
|
+
const w = o(e.q?.expectedOutput.amount ?? 0).minus(e.repayment).plus(h.maximum(0, e.R.minus(p)));
|
|
263
|
+
w.gt(0) && P.push({ coinType: i.debtCoinType, amount: D(w) });
|
|
264
|
+
} else {
|
|
265
|
+
t.direction = "decrease", r(a.gt(0), "POSITION_NOT_FOUND", "No collateral to withdraw");
|
|
266
|
+
const l = n.type === "close", f = o(n.type === "remove_principal" ? n.amount : 0);
|
|
267
|
+
r(
|
|
268
|
+
f.times(T).lt(Z) || l,
|
|
269
|
+
"CONSTRAINT_VIOLATION",
|
|
270
|
+
"Withdrawal consumes all equity; use close"
|
|
271
|
+
);
|
|
272
|
+
const C = p.isZero() ? p : o(F(p.times("1.0001").plus(le(u.debt)))), g = async (c) => {
|
|
273
|
+
const w = await z(c.toFixed(0), !1), A = o(w.minOutput.amount).minus(f), O = l || s.eq(1) ? C : h.minimum(p, o(D(A.div(o(u.flash.feeBps).div(1e4).plus(1))))), x = h.maximum(0, p.minus(O)), y = a.minus(c);
|
|
274
|
+
return {
|
|
275
|
+
q: w,
|
|
276
|
+
W: c,
|
|
277
|
+
R: O,
|
|
278
|
+
repayment: W(O),
|
|
196
279
|
c: y,
|
|
197
|
-
d:
|
|
198
|
-
residual:
|
|
199
|
-
available:
|
|
280
|
+
d: x,
|
|
281
|
+
residual: x.times(T).minus(y.times(N).times(s.minus(1)).div(s)),
|
|
282
|
+
available: A
|
|
200
283
|
};
|
|
201
284
|
};
|
|
202
|
-
let
|
|
203
|
-
if (
|
|
285
|
+
let m;
|
|
286
|
+
if (l) m = await g(a);
|
|
204
287
|
else {
|
|
205
|
-
let
|
|
206
|
-
|
|
207
|
-
|
|
288
|
+
let c = o(
|
|
289
|
+
F(
|
|
290
|
+
p.times(T).minus(a.times(N).times(s.minus(1)).div(s)).plus(f.times(T)).times(s).div(N)
|
|
208
291
|
)
|
|
209
292
|
);
|
|
210
|
-
|
|
211
|
-
for (let
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
if (
|
|
215
|
-
const
|
|
293
|
+
s.eq(1) && (c = o(F(C.plus(f).times(T).div(N).times("1.01")))), c = h.maximum(1, h.minimum(a, c));
|
|
294
|
+
for (let w = 0; w < 20; w++) {
|
|
295
|
+
m = await g(c);
|
|
296
|
+
const A = m.repayment.plus(f).minus(m.q.minOutput.amount);
|
|
297
|
+
if (m.R.gte(0) && A.lte(0) && U(m.c, m.d)) break;
|
|
298
|
+
const O = s.eq(1) ? A.times(T).div(N) : m.residual.times(s).div(N), x = h.maximum(
|
|
216
299
|
1,
|
|
217
|
-
|
|
300
|
+
o(F(c.plus(O.times("1.01")).plus(A.gt(0) ? 1 : 0)))
|
|
218
301
|
);
|
|
219
|
-
|
|
220
|
-
|
|
302
|
+
r(
|
|
303
|
+
x.lt(a) && !x.eq(c) && w < 19,
|
|
221
304
|
"NO_CONVERGENCE",
|
|
222
305
|
"No feasible deleverage route within constraints"
|
|
223
|
-
),
|
|
306
|
+
), c = x;
|
|
224
307
|
}
|
|
225
308
|
}
|
|
226
|
-
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
309
|
+
r(m && m.R.gte(0), "NO_CONVERGENCE", "Missing decrease plan");
|
|
310
|
+
const _ = h.maximum(0, m.R.minus(p)), d = o(m.q.expectedOutput.amount).plus(_).minus(m.repayment), e = o(m.q.minOutput.amount).minus(m.repayment);
|
|
311
|
+
r(
|
|
312
|
+
e.gte(f) && e.gte(0),
|
|
230
313
|
"CONSTRAINT_VIOLATION",
|
|
231
314
|
"Conversion cannot cover flash repayment and withdrawal"
|
|
232
|
-
),
|
|
315
|
+
), t.conversions = [
|
|
316
|
+
{ ...m.q, id: "withdrawal", purpose: "repayment", source: "withdrawal" }
|
|
317
|
+
], t.flashAmount = m.R.toFixed(0), t.flashRepayment = m.repayment.toFixed(0), t.withdrawAmount = m.W.toFixed(0), t.collateralAfter = t.minCollateralAfter = m.c.toFixed(0), t.debtAfter = m.d.toFixed(0), d.gt(0) && P.push({ coinType: i.principalCoinType, amount: D(d) }), e.gt(0) && Q.push({ coinType: i.principalCoinType, amount: D(e) });
|
|
233
318
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
319
|
+
M.length && (t.funding = {
|
|
320
|
+
kind: k(M[0].coinType, i.collateralCoinType) ? "collateral" : "debt",
|
|
321
|
+
input: M[0]
|
|
322
|
+
});
|
|
323
|
+
for (const l of [
|
|
324
|
+
t.flashAmount,
|
|
325
|
+
t.flashRepayment,
|
|
326
|
+
t.borrowAmount,
|
|
327
|
+
t.borrowFee,
|
|
328
|
+
t.withdrawAmount,
|
|
329
|
+
t.collateralAfter,
|
|
330
|
+
t.debtAfter
|
|
242
331
|
])
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
332
|
+
X(l);
|
|
333
|
+
const J = o(t.flashAmount);
|
|
334
|
+
r(
|
|
335
|
+
J.isZero() || J.gte(u.flash.min) && J.lte(u.flash.max),
|
|
247
336
|
"CAPACITY_EXCEEDED",
|
|
248
337
|
"Flash liquidity limit exceeded"
|
|
249
|
-
),
|
|
250
|
-
|
|
338
|
+
), r(
|
|
339
|
+
o(t.borrowAmount).plus(t.borrowFee).lte(pe(u)),
|
|
251
340
|
"CAPACITY_EXCEEDED",
|
|
252
341
|
"Borrow liquidity limit exceeded"
|
|
253
|
-
),
|
|
254
|
-
|
|
342
|
+
), r(
|
|
343
|
+
o(t.collateralAfter).minus(a).lte(ce(u)),
|
|
255
344
|
"CAPACITY_EXCEEDED",
|
|
256
345
|
"Supply capacity exceeded"
|
|
257
346
|
);
|
|
258
|
-
const
|
|
259
|
-
{ ...
|
|
260
|
-
|
|
347
|
+
const K = { collateralAmount: t.collateralAfter, debtAmount: t.debtAfter }, j = ee(
|
|
348
|
+
{ ...K, collateralAmount: t.minCollateralAfter },
|
|
349
|
+
u
|
|
261
350
|
);
|
|
262
|
-
|
|
263
|
-
|
|
351
|
+
r(
|
|
352
|
+
j.healthFactor === null || o(j.healthFactor).gte(E.minHealthFactor),
|
|
264
353
|
"CONSTRAINT_VIOLATION",
|
|
265
354
|
"Minimum output does not meet health factor constraint"
|
|
266
|
-
),
|
|
267
|
-
|
|
268
|
-
{ ...
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
355
|
+
), r(
|
|
356
|
+
de(
|
|
357
|
+
{ ...K, collateralAmount: t.minCollateralAfter },
|
|
358
|
+
b,
|
|
359
|
+
u,
|
|
360
|
+
t.direction !== "increase"
|
|
272
361
|
),
|
|
273
362
|
"CONSTRAINT_VIOLATION",
|
|
274
363
|
"Projected debt exceeds collateral LTV"
|
|
275
|
-
),
|
|
276
|
-
|
|
364
|
+
), E.maxLeverage && j.leverage && r(
|
|
365
|
+
o(j.leverage).lte(E.maxLeverage),
|
|
277
366
|
"CONSTRAINT_VIOLATION",
|
|
278
367
|
"Maximum leverage exceeded"
|
|
279
368
|
);
|
|
280
|
-
const
|
|
281
|
-
return ["open", "add_principal", "remove_principal", "set_leverage"].includes(
|
|
282
|
-
|
|
369
|
+
const $ = ee(K, u);
|
|
370
|
+
return ["open", "add_principal", "remove_principal", "set_leverage"].includes(n.type) && r(
|
|
371
|
+
$.leverage !== null && o($.leverage).minus(s).abs().lte(s.times("0.0001")),
|
|
283
372
|
"NO_CONVERGENCE",
|
|
284
373
|
"Conversion quote cannot achieve the requested leverage within tolerance"
|
|
285
374
|
), {
|
|
286
|
-
execution:
|
|
287
|
-
estimate: { inputs:
|
|
375
|
+
execution: t,
|
|
376
|
+
estimate: { inputs: M, outputs: P, minOutputs: Q, ...$ }
|
|
288
377
|
};
|
|
289
378
|
}
|
|
290
379
|
export {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
380
|
+
Ae as chooseConversion,
|
|
381
|
+
ee as metrics,
|
|
382
|
+
Te as plan,
|
|
383
|
+
Ne as validateAction
|
|
295
384
|
};
|
|
296
385
|
//# sourceMappingURL=planner.js.map
|