@naviprotocol/multiply 0.2.0-preview.20261123.1 → 0.2.0-preview.20261124.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/dist/client.js CHANGED
@@ -1,208 +1,232 @@
1
- import { validateMarketMode as G, assertModeExposure as ue } from "./emode.js";
2
- import { voloConverter as we, haedalConverter as Ie, astrosConverter as Ee, validateConversion as Oe } from "./converters.js";
3
- import { defaultClient as Ne, createNaviDataSource as ke } from "./data-source.js";
4
- import { strategies as Se } from "./strategies.js";
5
- import { indexRegistryStrategies as Ue, registryKeyString as O, normalizeCapRegistry as de, MAINNET_CAP_REGISTRY as Le, registerCreatedCapPTB as De } from "./cap-registry.js";
6
- import { validateAction as ge, plan as je, metrics as me, chooseConversion as Re } from "./planner.js";
7
- import { positionSides as ee, atomPrices as Pe, isDustClose as V, tokenDecimals as k, amountCushion as ye, withinLtv as Ve, supplyCapacity as Me, borrowCapacity as Qe } from "./amounts.js";
8
- import { check as n, jsonCopy as U, coinType as M, sameCoin as N, address as _, uint as z, BN as m, checksum as L, minOutputGuard as te } from "./utils.js";
9
- import { validateFunding as Fe } from "./funding.js";
10
- function We(w = {}) {
11
- const Q = w.client ?? Ne(), oe = w.converters ?? [we({ client: Q }), Ie({ client: Q })], ae = w.fallbackConverter ?? Ee({ services: w.services, client: Q }), F = /* @__PURE__ */ new Map();
12
- for (const e of [...oe, ae])
13
- n(!F.has(e.id), "INVALID_ARGUMENT", `Duplicate converter id: ${e.id}`), F.set(e.id, e);
14
- const D = (w.strategies ?? Object.values(Se)).map(
15
- (e) => ie(U(e))
1
+ import { validateMarketMode as q, assertModeExposure as Ce } from "./emode.js";
2
+ import { voloConverter as Ue, haedalConverter as Le, astrosConverter as De, validateConversion as Re } from "./converters.js";
3
+ import { defaultClient as Ve, createNaviDataSource as Pe } from "./data-source.js";
4
+ import { strategies as je } from "./strategies.js";
5
+ import { indexRegistryStrategies as Me, registryKeyString as S, normalizeCapRegistry as Te, MAINNET_CAP_REGISTRY as Qe, normalizeEnv as $e, assertRegistryUsable as ve, accountCapType as he, registerCreatedCapPTB as Fe } from "./cap-registry.js";
6
+ import { validateAction as be, plan as Be, chooseConversion as Ke, metrics as Y } from "./planner.js";
7
+ import { positionSides as re, atomPrices as Ge, isDustClose as Q, tokenDecimals as D, leverageLimit as Ae, amountCushion as _e, withinLtv as ze, supplyCapacity as qe, borrowCapacity as Ye, withinSameReserveCapacity as He } from "./amounts.js";
8
+ import { check as o, jsonCopy as V, coinType as $, sameCoin as k, address as A, uint as H, BN as y, checksum as P, assertAddressPTB as Xe, minOutputGuard as ce } from "./utils.js";
9
+ import { validateFunding as We } from "./funding.js";
10
+ function rt(T = {}) {
11
+ const F = T.client ?? Ve(), X = T.converters ?? [Ue({ client: F }), Le({ client: F })], W = T.fallbackConverter ?? De({ services: T.services, client: F }), B = /* @__PURE__ */ new Map();
12
+ for (const e of [...X, W])
13
+ o(!B.has(e.id), "INVALID_ARGUMENT", `Duplicate converter id: ${e.id}`), B.set(e.id, e);
14
+ const j = (T.strategies ?? Object.values(je)).map(
15
+ (e) => pe(V(e))
16
16
  );
17
- n(
18
- new Set(D.map((e) => e.id)).size === D.length,
17
+ o(
18
+ new Set(j.map((e) => e.id)).size === j.length,
19
19
  "INVALID_ARGUMENT",
20
20
  "Strategy registry contains duplicate ids"
21
21
  );
22
- const q = Ue(D), ne = new Set((w.retiredRegistryKeys ?? []).map(O));
23
- n(
24
- [...ne].every((e) => !q.has(e)),
22
+ const J = Me(j), se = new Set((T.retiredRegistryKeys ?? []).map(S));
23
+ o(
24
+ [...se].every((e) => !J.has(e)),
25
25
  "INVALID_ARGUMENT",
26
26
  "Active and retired Registry keys overlap"
27
27
  );
28
- const j = de(w.capRegistry ?? Le), h = w.dataSource ?? ke({
29
- client: Q,
30
- services: w.services,
31
- capRegistry: j,
32
- strategies: D,
33
- retiredRegistryKeys: w.retiredRegistryKeys
34
- });
35
- function re(e = "INVALID_ARGUMENT") {
36
- n(
37
- h.capRegistry && JSON.stringify(de(h.capRegistry)) === JSON.stringify(j),
28
+ const M = Te(T.capRegistry ?? Qe);
29
+ o(
30
+ !(T.dataSource && T.env !== void 0),
31
+ "INVALID_ARGUMENT",
32
+ "env cannot be combined with a custom dataSource; set env on the data source"
33
+ );
34
+ const h = T.dataSource ?? Pe({
35
+ client: F,
36
+ services: T.services,
37
+ capRegistry: T.capRegistry === void 0 ? void 0 : M,
38
+ strategies: j,
39
+ retiredRegistryKeys: T.retiredRegistryKeys,
40
+ // ST-18: passed through as given; the data source normalizes it (omitted stays omitted, ST-01).
41
+ env: T.env
42
+ }), U = $e(h.env), le = T.capRegistry !== void 0;
43
+ function ue(e = "INVALID_ARGUMENT") {
44
+ o(
45
+ h.capRegistry && JSON.stringify(Te(h.capRegistry)) === JSON.stringify(M),
38
46
  e,
39
47
  "DataSource Registry deployment differs from the execution deployment"
40
48
  );
41
49
  }
42
- re();
43
- async function Y(e, s = "INVALID_ARGUMENT") {
44
- const t = ie(U(e)), g = q.get(O(t.registryKey));
45
- return n(
46
- g && await L(g) === await L(t),
47
- s,
50
+ ue();
51
+ async function Z(e, l = "INVALID_ARGUMENT") {
52
+ const t = pe(V(e)), g = J.get(S(t.registryKey));
53
+ return o(
54
+ g && await P(g) === await P(t),
55
+ l,
48
56
  "Strategy differs from the configured Registry binding"
49
57
  ), t;
50
58
  }
51
- function ie(e) {
52
- n(
59
+ function pe(e) {
60
+ o(
53
61
  typeof e?.id == "string" && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(e.id),
54
62
  "INVALID_ARGUMENT",
55
63
  "Strategy id must be a stable ASCII slug"
56
- ), n(
64
+ ), o(
57
65
  typeof e.market == "string" && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(e.market),
58
66
  "INVALID_ARGUMENT",
59
67
  "Missing market"
60
- ), n(
68
+ ), o(
61
69
  e.emodeId === void 0 || Number.isSafeInteger(e.emodeId) && e.emodeId >= 0,
62
70
  "INVALID_ARGUMENT",
63
71
  "E-Mode ID must be a nonnegative safe integer"
64
- ), O(e.registryKey);
65
- const s = {
72
+ ), S(e.registryKey);
73
+ const l = {
66
74
  ...e,
67
75
  registryKey: { ...e.registryKey },
68
- principalCoinType: M(e.principalCoinType),
69
- collateralCoinType: M(e.collateralCoinType),
70
- debtCoinType: M(e.debtCoinType)
76
+ principalCoinType: $(e.principalCoinType),
77
+ collateralCoinType: $(e.collateralCoinType),
78
+ debtCoinType: $(e.debtCoinType)
71
79
  };
72
- return n(
73
- (N(s.principalCoinType, s.debtCoinType) || N(s.principalCoinType, s.collateralCoinType)) && !N(s.collateralCoinType, s.debtCoinType),
80
+ return o(
81
+ (k(l.principalCoinType, l.debtCoinType) || k(l.principalCoinType, l.collateralCoinType)) && !k(l.collateralCoinType, l.debtCoinType),
74
82
  "STRATEGY_UNSUPPORTED",
75
83
  "Principal must equal collateral or debt, with distinct collateral and debt"
76
- ), s.acceptedInputCoinTypes = (s.acceptedInputCoinTypes ?? [s.principalCoinType]).map(M), n(
77
- s.acceptedInputCoinTypes.length > 0 && s.acceptedInputCoinTypes[0] === s.principalCoinType && new Set(s.acceptedInputCoinTypes).size === s.acceptedInputCoinTypes.length && s.acceptedInputCoinTypes.every(
78
- (t) => t === s.collateralCoinType || t === s.debtCoinType
84
+ ), l.acceptedInputCoinTypes = (l.acceptedInputCoinTypes ?? [l.principalCoinType]).map($), o(
85
+ l.acceptedInputCoinTypes.length > 0 && l.acceptedInputCoinTypes[0] === l.principalCoinType && new Set(l.acceptedInputCoinTypes).size === l.acceptedInputCoinTypes.length && l.acceptedInputCoinTypes.every(
86
+ (t) => t === l.collateralCoinType || t === l.debtCoinType
79
87
  ),
80
88
  "STRATEGY_UNSUPPORTED",
81
89
  "Accepted inputs must be distinct strategy assets with principal first"
82
- ), s;
90
+ ), l;
83
91
  }
84
- async function H(e) {
85
- re("STALE_QUOTE");
86
- const s = await h.listAccountCaps(e), t = /* @__PURE__ */ new Set(), g = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
87
- return s.map((a) => {
88
- const d = O(a.registryKey), l = q.get(d), i = _(a.id), b = _(a.accountOwner);
89
- return n(
90
- i !== _("0x0") && b !== _("0x0"),
92
+ async function x(e) {
93
+ ue("STALE_QUOTE");
94
+ const l = await h.listAccountCaps(e), t = /* @__PURE__ */ new Set(), g = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set();
95
+ return l.map((n) => {
96
+ const d = S(n.registryKey), c = J.get(d), r = A(n.id), b = A(n.accountOwner);
97
+ return o(
98
+ r !== A("0x0") && b !== A("0x0"),
91
99
  "INVALID_ACCOUNT_CAP",
92
100
  "Zero Cap identity"
93
- ), n(
94
- !t.has(d) && !g.has(i) && !o.has(b),
101
+ ), o(
102
+ !t.has(d) && !g.has(r) && !a.has(b),
95
103
  "DUPLICATE_ACCOUNT_CAP",
96
104
  "Duplicate strategy, Cap or lending account in Registry"
97
- ), t.add(d), g.add(i), o.add(b), ne.has(d) ? null : (n(l, "UNSUPPORTED_REGISTRY_STRATEGY", "Unknown registered Multiply strategy"), n(
98
- l.id === a.strategyId,
105
+ ), t.add(d), g.add(r), a.add(b), se.has(d) ? null : (o(c, "UNSUPPORTED_REGISTRY_STRATEGY", "Unknown registered Multiply strategy"), o(
106
+ c.id === n.strategyId,
99
107
  "INVALID_ACCOUNT_CAP",
100
108
  "Cap numeric binding differs from strategy ID"
101
- ), { ...a, id: i, accountOwner: b, registryKey: { ...a.registryKey } });
102
- }).filter((a) => a !== null);
109
+ ), o(
110
+ n.transient === void 0 || n.transient === !0,
111
+ "INVALID_ACCOUNT_CAP",
112
+ "Cap transient marker must be true or absent"
113
+ ), { ...n, id: r, accountOwner: b, registryKey: { ...n.registryKey } });
114
+ }).filter((n) => n !== null);
103
115
  }
104
- function X(e, s) {
105
- const t = O(s.registryKey);
106
- return e.find((g) => O(g.registryKey) === t);
116
+ function ee(e, l) {
117
+ const t = S(l.registryKey);
118
+ return e.find((g) => S(g.registryKey) === t);
107
119
  }
108
- async function fe(e, s = {}) {
109
- e = _(e);
110
- const t = [], g = await H(e);
111
- for (const o of s.strategy ? [s.strategy] : D) {
112
- const a = await Y(o), d = X(g, a);
120
+ async function Ie(e, l = {}) {
121
+ e = A(e);
122
+ const t = [], g = await x(e);
123
+ for (const a of l.strategy ? [l.strategy] : j) {
124
+ const n = await Z(a), d = ee(g, n);
113
125
  if (!d) continue;
114
- const l = await h.readMarket(a), i = await h.readPosition(d, a, l);
115
- G(a, l), z(i.collateralAmount), z(i.debtAmount), t.push({
126
+ const c = await h.readMarket(n), r = await h.readPosition(d, n, c);
127
+ q(n, c), H(r.collateralAmount), H(r.debtAmount), t.push({
116
128
  address: e,
117
- strategy: a,
129
+ strategy: n,
118
130
  accountCap: d,
119
- ...i,
120
- ...me(i, l)
131
+ ...r,
132
+ ...Y(r, c)
121
133
  });
122
134
  }
123
135
  return t;
124
136
  }
125
- async function Ce(e, s, t, g) {
126
- e = _(e), n(e !== _("0x0"), "INVALID_ARGUMENT", "Multiply owner cannot be zero"), ge(t, g);
127
- const o = await Y(s);
137
+ async function we(e, l, t, g) {
138
+ e = A(e), o(e !== A("0x0"), "INVALID_ARGUMENT", "Multiply owner cannot be zero"), be(t, g);
139
+ const a = await Z(l);
128
140
  if (t.type === "open" || t.type === "add_principal") {
129
- const y = M(t.inputCoinType ?? o.principalCoinType);
130
- n(
131
- o.acceptedInputCoinTypes.includes(y),
141
+ const m = $(t.inputCoinType ?? a.principalCoinType);
142
+ o(
143
+ a.acceptedInputCoinTypes.includes(m),
132
144
  "UNSUPPORTED_INPUT_COIN",
133
145
  "Input coin is not accepted by this strategy"
134
- ), t = { ...t, inputCoinType: y };
146
+ ), t = { ...t, inputCoinType: m };
135
147
  }
136
- const a = X(await H(e), o);
137
- n(
138
- a || t.type === "open",
148
+ const n = ee(await x(e), a);
149
+ o(
150
+ n || t.type === "open",
139
151
  "POSITION_NOT_FOUND",
140
152
  "Only open may create a Multiply Cap"
141
153
  );
142
- const d = await h.readMarket(o);
143
- G(o, d);
144
- const l = a ? await h.readPosition(a, o, d) : { collateralAmount: "0", debtAmount: "0" };
145
- z(l.collateralAmount), z(l.debtAmount);
146
- const i = a ? ee(l, d) : { collateral: !1, debt: !1 }, b = !i.collateral && !i.debt;
147
- if (n(
154
+ const d = await h.readMarket(a);
155
+ q(a, d);
156
+ const c = n ? await h.readPosition(n, a, d) : { collateralAmount: "0", debtAmount: "0" };
157
+ H(c.collateralAmount), H(c.debtAmount);
158
+ const r = n ? re(c, d) : { collateral: !1, debt: !1 }, b = !r.collateral && !r.debt;
159
+ if (o(
148
160
  t.type !== "open" || b,
149
161
  "POSITION_EXISTS",
150
162
  "Use add_principal or set_leverage for an existing position"
151
- ), n(
163
+ ), o(
152
164
  t.type === "open" || !b,
153
165
  "POSITION_NOT_FOUND",
154
166
  "Empty position; use open to reuse this Cap"
155
167
  ), "targetLeverage" in t && t.targetLeverage) {
156
- const y = new m(d.liquidationThreshold).div(
157
- new m(d.borrowWeight).times(g.minHealthFactor)
158
- ), v = new m(1).minus(new m(1).div(t.targetLeverage)), E = Pe(d), R = !["open", "add_principal"].includes(t.type) && v.times(l.collateralAmount).times(E.collateral).lte(new m(l.debtAmount).times(E.debt));
159
- n(
160
- v.lte(y) && (v.lte(new m(d.ltv).div(d.borrowWeight)) || R),
168
+ const m = new y(d.liquidationThreshold).div(
169
+ new y(d.borrowWeight).times(g.minHealthFactor)
170
+ ), L = new y(1).minus(new y(1).div(t.targetLeverage)), N = Ge(d), te = !["open", "add_principal"].includes(t.type) && L.times(c.collateralAmount).times(N.collateral).lte(new y(c.debtAmount).times(N.debt));
171
+ o(
172
+ L.lte(m) && (L.lte(new y(d.ltv).div(d.borrowWeight)) || te),
161
173
  "CONSTRAINT_VIOLATION",
162
174
  "Target leverage exceeds protocol or health factor limits"
163
175
  );
164
176
  }
165
- const p = await h.readBorrowFeeBps(d, a?.accountOwner), I = await je(
166
- o,
177
+ const u = await h.readBorrowFeeBps(d, n?.accountOwner), _ = { coinIn: a.debtCoinType, coinOut: a.collateralCoinType }, O = X.filter((m) => m.supports(_.coinIn, _.coinOut)), i = O.length === 0 ? W : O.length === 1 ? O[0] : void 0, I = await Be(
178
+ a,
167
179
  t,
168
180
  g,
169
- l,
181
+ c,
170
182
  d,
171
- p,
172
- (y) => Re(y, oe, ae)
173
- ), r = {
183
+ u,
184
+ (m) => Ke(m, X, W),
185
+ i?.getRateHint ? () => i.getRateHint(_) : void 0,
186
+ i?.getStakingSnapshot ? async () => {
187
+ const m = await i.getStakingSnapshot(_);
188
+ return m && o(
189
+ m.converterId === i.id,
190
+ "INVALID_CONVERSION",
191
+ "Native snapshot provider mismatch"
192
+ ), m;
193
+ } : void 0
194
+ );
195
+ n || (ve(U, le), he(U));
196
+ const w = {
174
197
  address: e,
175
- strategy: o,
176
- action: U(t),
177
- constraints: U(g),
198
+ strategy: a,
199
+ action: V(t),
200
+ constraints: V(g),
178
201
  expiresAt: Math.min(
179
202
  Date.now() + 12e4,
180
- ...I.execution.conversions.map((y) => y.expiresAt)
203
+ ...I.execution.conversions.map((m) => m.expiresAt)
181
204
  ),
182
205
  estimate: I.estimate,
183
206
  execution: {
184
207
  ...I.execution,
185
- capRegistry: { ...j },
186
- accountCap: a ? { kind: "existing", ...a } : { kind: "create", registryKey: { ...o.registryKey } },
208
+ env: U,
209
+ capRegistry: { ...M },
210
+ accountCap: n ? { kind: "existing", ...n } : { kind: "create", registryKey: { ...a.registryKey } },
187
211
  checksum: ""
188
212
  }
189
213
  };
190
- return r.execution.checksum = await L(r), U(r);
214
+ return w.execution.checksum = await P(w), V(w);
191
215
  }
192
- async function Te(e, s) {
193
- const t = e.execution, g = e.strategy, o = X(await H(e.address), g);
194
- t.accountCap.kind === "create" ? n(
195
- !o && !s.accountCap,
216
+ async function Ee(e, l) {
217
+ const t = e.execution, g = e.strategy, a = ee(await x(e.address), g);
218
+ t.accountCap.kind === "create" ? o(
219
+ !a && !l.accountCap,
196
220
  "STALE_QUOTE",
197
221
  "Cap was created since quote or an unexpected Cap was supplied"
198
- ) : (n(
199
- o && o.id === t.accountCap.id && o.accountOwner === t.accountCap.accountOwner,
222
+ ) : (o(
223
+ a && a.id === t.accountCap.id && a.accountOwner === t.accountCap.accountOwner,
200
224
  "STALE_QUOTE",
201
225
  "AccountCap changed since quote"
202
- ), typeof s.accountCap == "string" && n(_(s.accountCap) === _(o.id), "INVALID_INPUTS", "Wrong AccountCap"));
203
- const a = await h.readMarket(g);
204
- G(g, a), ue(e.action, e.execution, a);
205
- for (const r of [
226
+ ), typeof l.accountCap == "string" && o(A(l.accountCap) === A(a.id), "INVALID_INPUTS", "Wrong AccountCap"));
227
+ const n = await h.readMarket(g);
228
+ q(g, n), Ce(e.action, e.execution, n);
229
+ for (const i of [
206
230
  "package",
207
231
  "storage",
208
232
  "priceOracle",
@@ -210,365 +234,397 @@ function We(w = {}) {
210
234
  "incentiveV3",
211
235
  "limter"
212
236
  ])
213
- n(a.config[r] === t.market.config[r], "STALE_QUOTE", `Market ${r} changed`);
214
- n(
215
- a.collateral.contract.pool === t.market.collateral.contract.pool && a.debt.contract.pool === t.market.debt.contract.pool && a.flash.poolId === t.market.flash.poolId && k(a.collateral) === k(t.market.collateral) && k(a.debt) === k(t.market.debt),
237
+ o(n.config[i] === t.market.config[i], "STALE_QUOTE", `Market ${i} changed`);
238
+ o(
239
+ n.collateral.contract.pool === t.market.collateral.contract.pool && n.debt.contract.pool === t.market.debt.contract.pool && n.flash.poolId === t.market.flash.poolId && D(n.collateral) === D(t.market.collateral) && D(n.debt) === D(t.market.debt),
216
240
  "STALE_QUOTE",
217
241
  "Pool configuration changed"
218
242
  );
219
- const d = o ? await h.readPosition(o, g, a) : { collateralAmount: "0", debtAmount: "0" };
220
- if (o) {
221
- const r = ee(d, a);
222
- if (n(
223
- e.action.type === "open" == (!r.collateral && !r.debt),
243
+ const d = a ? await h.readPosition(a, g, n) : { collateralAmount: "0", debtAmount: "0" };
244
+ if (a) {
245
+ const i = re(d, n);
246
+ if (o(
247
+ e.action.type === "open" == (!i.collateral && !i.debt),
224
248
  "STALE_QUOTE",
225
249
  "Position no longer satisfies the action"
226
250
  ), e.action.type !== "open") {
227
- n(
228
- t.snapshot.protocolCollateralAmount !== void 0 || k(a.collateral) >= 9 || t.snapshot.collateralAmount !== "0",
251
+ o(
252
+ t.snapshot.protocolCollateralAmount !== void 0 || D(n.collateral) >= 9 || t.snapshot.collateralAmount !== "0",
229
253
  "STALE_QUOTE",
230
254
  "Quote lacks precise collateral state"
231
255
  );
232
- const y = ee(t.snapshot, a);
233
- n(
234
- y.collateral === r.collateral && y.debt === r.debt,
256
+ const I = re(t.snapshot, n);
257
+ o(
258
+ I.collateral === i.collateral && I.debt === i.debt,
235
259
  "STALE_QUOTE",
236
260
  "Position crossed a zero balance"
237
261
  );
238
262
  }
239
- e.action.type === "close" && V(g, t.snapshot, a) && n(
240
- V(g, d, a),
263
+ e.action.type === "close" && Q(g, t.snapshot, n) && o(
264
+ Q(g, d, n),
241
265
  "STALE_QUOTE",
242
266
  "Collateral became transferable; request a new quote"
243
267
  );
244
268
  }
245
- const l = new m(d.collateralAmount).minus(t.snapshot.collateralAmount), i = new m(d.debtAmount).minus(t.snapshot.debtAmount);
246
- n(
247
- l.gte(0) && l.lte(
248
- new m(t.snapshot.collateralAmount).times("0.0001").plus(ye(a.collateral))
249
- ) && i.gte(0) && i.lte(
250
- new m(t.snapshot.debtAmount).times("0.0001").plus(ye(a.debt))
269
+ const c = new y(d.collateralAmount).minus(t.snapshot.collateralAmount), r = new y(d.debtAmount).minus(t.snapshot.debtAmount);
270
+ o(
271
+ c.gte(0) && c.lte(
272
+ new y(t.snapshot.collateralAmount).times("0.0001").plus(_e(n.collateral))
273
+ ) && r.gte(0) && r.lte(
274
+ new y(t.snapshot.debtAmount).times("0.0001").plus(_e(n.debt))
251
275
  ),
252
276
  "STALE_QUOTE",
253
277
  "Position changed since quote"
254
278
  );
255
- const b = await h.readBorrowFeeBps(a, o?.accountOwner);
256
- n(
257
- new m(t.borrowAmount).times(b).div(1e4).integerValue().lte(t.borrowFee),
279
+ const b = await h.readBorrowFeeBps(n, a?.accountOwner);
280
+ o(
281
+ new y(t.borrowAmount).times(b).div(1e4).integerValue().lte(t.borrowFee),
258
282
  "STALE_QUOTE",
259
283
  "Borrow fee increased"
260
- ), n(a.flash.feeBps === t.market.flash.feeBps, "STALE_QUOTE", "Flash fee changed");
261
- const p = {
262
- collateralAmount: new m(t.minCollateralAfter).plus(l).toFixed(0),
263
- debtAmount: new m(t.debtAfter).plus(e.action.type === "close" ? 0 : i).toFixed(0)
264
- }, I = me(p, a);
265
- return n(
266
- I.healthFactor === null || new m(I.healthFactor).gte(e.constraints.minHealthFactor),
284
+ ), o(n.flash.feeBps === t.market.flash.feeBps, "STALE_QUOTE", "Flash fee changed");
285
+ const u = {
286
+ collateralAmount: new y(t.minCollateralAfter).plus(c).toFixed(0),
287
+ debtAmount: new y(t.debtAfter).plus(e.action.type === "close" ? 0 : r).toFixed(0)
288
+ }, _ = Y(u, n);
289
+ o(
290
+ _.healthFactor === null || new y(_.healthFactor).gte(e.constraints.minHealthFactor),
267
291
  "STALE_QUOTE",
268
292
  "Updated market no longer satisfies HF"
269
- ), n(
270
- Number.isSafeInteger(a.observedAt) && Date.now() - a.observedAt <= 12e4,
293
+ ), o(
294
+ Number.isSafeInteger(n.observedAt) && Date.now() - n.observedAt <= 12e4,
271
295
  "STALE_QUOTE",
272
296
  "Current market snapshot is stale"
273
- ), e.constraints.maxLeverage && I.leverage && n(
274
- new m(I.leverage).lte(e.constraints.maxLeverage),
297
+ );
298
+ const O = Ae(
299
+ e.constraints.maxLeverage,
300
+ e.action,
301
+ t.direction,
302
+ Y(d, n).leverage
303
+ );
304
+ return O && _.leverage && o(
305
+ new y(_.leverage).lte(O),
275
306
  "STALE_QUOTE",
276
307
  "Updated market exceeds maximum leverage"
277
- ), n(
278
- Ve(p, d, a, t.direction !== "increase"),
308
+ ), o(
309
+ ze(u, d, n, t.direction !== "increase"),
279
310
  "STALE_QUOTE",
280
311
  "Updated market exceeds LTV"
281
- ), n(
282
- new m(t.flashAmount).lte(a.flash.max) && (t.flashAmount === "0" || new m(t.flashAmount).gte(a.flash.min)),
312
+ ), o(
313
+ new y(t.flashAmount).lte(n.flash.max) && (t.flashAmount === "0" || new y(t.flashAmount).gte(n.flash.min)),
283
314
  "CAPACITY_EXCEEDED",
284
315
  "Flash capacity changed"
285
- ), n(
286
- new m(t.collateralAfter).minus(t.snapshot.collateralAmount).lte(Me(a)),
316
+ ), o(
317
+ new y(t.collateralAfter).minus(t.snapshot.collateralAmount).lte(qe(n)),
287
318
  "CAPACITY_EXCEEDED",
288
319
  "Supply capacity changed"
289
- ), n(
290
- new m(t.borrowAmount).plus(t.borrowFee).lte(Qe(a)),
320
+ ), o(
321
+ new y(t.borrowAmount).plus(t.borrowFee).lte(Ye(n)),
291
322
  "CAPACITY_EXCEEDED",
292
323
  "Borrow liquidity changed"
293
- ), { fresh: a, now: d };
324
+ ), o(
325
+ He(g, t, n),
326
+ "CAPACITY_EXCEEDED",
327
+ "Flash and borrow from the same reserve exceed its cash"
328
+ ), { fresh: n, now: d };
294
329
  }
295
- async function ce(e, s, t, g) {
296
- const o = U(s);
297
- n(
298
- o.execution?.version === 4,
330
+ async function de(e, l, t, g) {
331
+ const a = V(l);
332
+ o(
333
+ a.execution?.version === 4,
299
334
  "INVALID_QUOTE",
300
335
  "Unknown quote version; request a fresh quote"
301
336
  );
302
- const a = o.execution.checksum;
303
- o.execution.checksum = "", n(
304
- a === await L(o),
337
+ const n = a.execution.checksum;
338
+ a.execution.checksum = "", o(
339
+ n === await P(a),
305
340
  "INVALID_QUOTE",
306
341
  "Quote contents have changed; request a fresh quote"
307
- ), o.execution.checksum = a, n(
308
- Number.isSafeInteger(o.expiresAt) && o.expiresAt > Date.now(),
342
+ ), a.execution.checksum = n, o(
343
+ a.execution.env !== void 0,
344
+ "INVALID_QUOTE",
345
+ "Quote has no NAVI env (made by an older SDK); request a fresh quote"
346
+ ), o(
347
+ a.execution.env === U,
348
+ "INVALID_QUOTE",
349
+ `Quote was made for NAVI env ${String(a.execution.env)}; this client targets ${U}`
350
+ ), o(
351
+ Number.isSafeInteger(a.expiresAt) && a.expiresAt > Date.now(),
309
352
  "QUOTE_EXPIRED",
310
353
  "Multiply quote expired"
311
- ), n(
312
- _(o.address) !== _("0x0"),
354
+ ), o(
355
+ A(a.address) !== A("0x0"),
313
356
  "INVALID_ARGUMENT",
314
357
  "Multiply owner cannot be zero"
315
- ), await Y(o.strategy, "STALE_QUOTE"), n(
316
- await L(o.execution.capRegistry) === await L(j),
358
+ ), await Z(a.strategy, "STALE_QUOTE"), o(
359
+ await P(a.execution.capRegistry) === await P(M),
317
360
  "STALE_QUOTE",
318
361
  "Registry deployment changed since quote"
319
- ), n(
320
- O(o.execution.accountCap.registryKey) === O(o.strategy.registryKey),
362
+ ), o(
363
+ S(a.execution.accountCap.registryKey) === S(a.strategy.registryKey),
321
364
  "INVALID_QUOTE",
322
365
  "Quote Cap key differs from strategy"
323
366
  );
324
367
  const d = e.getData().sender;
325
- n(
326
- !d || _(d) === _(o.address),
368
+ o(
369
+ !d || A(d) === A(a.address),
327
370
  "INVALID_INPUTS",
328
371
  "Transaction sender differs from quote wallet"
329
- ), ge(o.action, o.constraints);
330
- const l = o.execution, i = o.strategy;
331
- Fe(o);
372
+ ), be(a.action, a.constraints);
373
+ const c = a.execution, r = a.strategy;
374
+ We(a);
332
375
  const b = t.coins ?? [];
333
- n(
334
- b.length === o.estimate.inputs.length,
376
+ o(
377
+ b.length === a.estimate.inputs.length,
335
378
  "INVALID_INPUTS",
336
379
  "Provide exactly the quoted input coins"
337
380
  );
338
- for (const c of o.estimate.inputs)
339
- n(
340
- b.filter((u) => N(u.coinType, c.coinType)).length === 1,
381
+ for (const s of a.estimate.inputs)
382
+ o(
383
+ b.filter((p) => k(p.coinType, s.coinType)).length === 1,
341
384
  "INVALID_INPUTS",
342
- `Missing or duplicate input: ${c.coinType}`
385
+ `Missing or duplicate input: ${s.coinType}`
343
386
  );
344
- for (const c of l.conversions)
345
- n(
346
- F.has(c.converterId),
387
+ for (const s of c.conversions)
388
+ o(
389
+ B.has(s.converterId),
347
390
  "INVALID_CONVERSION",
348
391
  "Quoted converter is not configured"
349
- ), Oe(
350
- c,
392
+ ), Re(
393
+ s,
351
394
  {
352
- coinIn: c.input.coinType,
353
- coinOut: c.expectedOutput.coinType,
354
- amountIn: c.input.amount,
355
- slippageBps: o.constraints.maxSlippageBps
395
+ coinIn: s.input.coinType,
396
+ coinOut: s.expectedOutput.coinType,
397
+ amountIn: s.input.amount,
398
+ slippageBps: a.constraints.maxSlippageBps
356
399
  },
357
- c.converterId
400
+ s.converterId
358
401
  );
359
- const { fresh: p, now: I } = g ? { fresh: l.market, now: l.snapshot } : await Te(o, t);
360
- G(i, p), ue(o.action, l, p), e.setSenderIfNotSet(o.address), await h.prepareOracle(e, i, p);
361
- const r = p.config, y = l.accountCap.kind === "create" ? e.moveCall({ target: `${r.package}::lending::create_account` }) : void 0, v = y ?? t.accountCap ?? e.object(l.accountCap.id);
362
- y && (i.emodeId !== void 0 && e.moveCall({
363
- target: `${r.package}::lending::enter_emode_with_account_cap`,
364
- arguments: [e.object(r.storage), e.pure.u64(i.emodeId), y]
365
- }), De(e, y, i.registryKey, j));
366
- const E = [], R = (c, u) => {
367
- const C = b.find((S) => N(S.coinType, c)), f = e.splitCoins(C.coin, [u])[0];
368
- return E.push(C), f;
369
- }, P = (c, u) => e.moveCall({
402
+ const { fresh: u, now: _ } = g ? { fresh: c.market, now: c.snapshot } : await Ee(a, t);
403
+ q(r, u), Ce(a.action, c, u), c.accountCap.kind === "create" && ve(U, le);
404
+ const O = c.accountCap.kind === "existing" && c.accountCap.transient === !0;
405
+ o(
406
+ !O || t.accountCap !== void 0,
407
+ "INVALID_INPUTS",
408
+ "A transient AccountCap must be supplied as a transaction argument"
409
+ ), e.setSenderIfNotSet(a.address), await h.prepareOracle(e, r, u);
410
+ const i = u.config, I = c.accountCap.kind === "create" ? e.moveCall({ target: `${i.package}::lending::create_account` }) : void 0, w = I ?? t.accountCap ?? e.object(c.accountCap.id);
411
+ c.accountCap.kind === "existing" && t.accountCap !== void 0 && !O && Xe(
412
+ e,
413
+ e.moveCall({
414
+ target: `${i.package}::account::account_owner`,
415
+ arguments: [w]
416
+ }),
417
+ c.accountCap.accountOwner
418
+ ), I && (r.emodeId !== void 0 && e.moveCall({
419
+ target: `${i.package}::lending::enter_emode_with_account_cap`,
420
+ arguments: [e.object(i.storage), e.pure.u64(r.emodeId), I]
421
+ }), Fe(e, I, r.registryKey, M, he(U)));
422
+ const m = [], L = (s, p) => {
423
+ const C = b.find((R) => k(R.coinType, s)), f = e.splitCoins(C.coin, [p])[0];
424
+ return m.push(C), f;
425
+ }, N = (s, p) => e.moveCall({
370
426
  target: "0x2::coin::from_balance",
371
- typeArguments: [u],
372
- arguments: [c]
373
- }), he = (c, u) => e.moveCall({ target: "0x2::coin::into_balance", typeArguments: [u], arguments: [c] }), W = (c) => e.moveCall({
374
- target: `${r.package}::incentive_v3::deposit_with_account_cap`,
375
- typeArguments: [i.collateralCoinType],
427
+ typeArguments: [p],
428
+ arguments: [s]
429
+ }), te = (s, p) => e.moveCall({ target: "0x2::coin::into_balance", typeArguments: [p], arguments: [s] }), ae = (s) => e.moveCall({
430
+ target: `${i.package}::incentive_v3::deposit_with_account_cap`,
431
+ typeArguments: [r.collateralCoinType],
376
432
  arguments: [
377
433
  e.object("0x6"),
378
- e.object(r.storage),
379
- e.object(p.collateral.contract.pool),
380
- e.pure.u8(p.collateral.id),
381
- c,
382
- e.object(r.incentiveV2),
383
- e.object(r.incentiveV3),
384
- v
434
+ e.object(i.storage),
435
+ e.object(u.collateral.contract.pool),
436
+ e.pure.u8(u.collateral.id),
437
+ s,
438
+ e.object(i.incentiveV2),
439
+ e.object(i.incentiveV3),
440
+ w
385
441
  ]
386
- }), se = (c) => P(
442
+ }), ge = (s) => N(
387
443
  e.moveCall({
388
- target: `${r.package}::incentive_v3::repay_with_account_cap`,
389
- typeArguments: [i.debtCoinType],
444
+ target: `${i.package}::incentive_v3::repay_with_account_cap`,
445
+ typeArguments: [r.debtCoinType],
390
446
  arguments: [
391
447
  e.object("0x6"),
392
- e.object(r.priceOracle),
393
- e.object(r.storage),
394
- e.object(p.debt.contract.pool),
395
- e.pure.u8(p.debt.id),
396
- c,
397
- e.object(r.incentiveV2),
398
- e.object(r.incentiveV3),
399
- v
448
+ e.object(i.priceOracle),
449
+ e.object(i.storage),
450
+ e.object(u.debt.contract.pool),
451
+ e.pure.u8(u.debt.id),
452
+ s,
453
+ e.object(i.incentiveV2),
454
+ e.object(i.incentiveV3),
455
+ w
400
456
  ]
401
457
  }),
402
- i.debtCoinType
458
+ r.debtCoinType
403
459
  );
404
- let $, B;
405
- if (l.flashAmount !== "0") {
406
- const c = e.moveCall({
407
- target: `${p.flash.config.package}::lending::flash_loan_with_ctx_v2`,
408
- typeArguments: [i.debtCoinType],
460
+ let K, G;
461
+ if (c.flashAmount !== "0") {
462
+ const s = e.moveCall({
463
+ target: `${u.flash.config.package}::lending::flash_loan_with_ctx_v2`,
464
+ typeArguments: [r.debtCoinType],
409
465
  arguments: [
410
- e.object(p.flash.config.flashloanConfig),
411
- e.object(p.flash.poolId),
412
- e.pure.u64(l.flashAmount),
466
+ e.object(u.flash.config.flashloanConfig),
467
+ e.object(u.flash.poolId),
468
+ e.pure.u64(c.flashAmount),
413
469
  e.object("0x5")
414
470
  ]
415
471
  });
416
- $ = P(c[0], i.debtCoinType), B = c[1];
472
+ K = N(s[0], r.debtCoinType), G = s[1];
417
473
  }
418
- const le = (c) => B ? P(
474
+ const me = (s) => G ? N(
419
475
  e.moveCall({
420
- target: `${p.flash.config.package}::lending::flash_repay_with_ctx`,
421
- typeArguments: [i.debtCoinType],
476
+ target: `${u.flash.config.package}::lending::flash_repay_with_ctx`,
477
+ typeArguments: [r.debtCoinType],
422
478
  arguments: [
423
479
  e.object("0x6"),
424
- e.object(p.flash.config.storage),
425
- e.object(p.flash.poolId),
426
- B,
427
- he(c, i.debtCoinType)
480
+ e.object(u.flash.config.storage),
481
+ e.object(u.flash.poolId),
482
+ G,
483
+ te(s, r.debtCoinType)
428
484
  ]
429
485
  }),
430
- i.debtCoinType
431
- ) : c, pe = async (c, u) => {
432
- const C = await F.get(u.converterId).buildPTB(e, u, c, {
433
- changeRecipient: o.address
486
+ r.debtCoinType
487
+ ) : s, ye = async (s, p) => {
488
+ const C = await B.get(p.converterId).buildPTB(e, p, s, {
489
+ changeRecipient: a.address
434
490
  });
435
- return te(e, C.coinOut, u.minOutput.amount), E.push(...C.change), C.coinOut;
491
+ return ce(e, C.coinOut, p.minOutput.amount), m.push(...C.change), C.coinOut;
436
492
  };
437
- if (l.direction === "repay")
438
- E.push({
439
- coinType: i.debtCoinType,
440
- coin: se(R(i.debtCoinType, o.estimate.inputs[0].amount))
493
+ if (c.direction === "repay")
494
+ m.push({
495
+ coinType: r.debtCoinType,
496
+ coin: ge(L(r.debtCoinType, a.estimate.inputs[0].amount))
441
497
  });
442
- else if (l.direction === "add_collateral")
443
- W(R(i.collateralCoinType, o.estimate.inputs[0].amount));
444
- else if (l.direction === "increase") {
445
- let c = $;
446
- if (l.funding.kind !== "none") {
447
- const u = R(l.funding.input.coinType, l.funding.input.amount);
448
- l.funding.kind === "collateral" ? W(u) : c ? e.mergeCoins(c, [u]) : c = u;
498
+ else if (c.direction === "add_collateral")
499
+ ae(L(r.collateralCoinType, a.estimate.inputs[0].amount));
500
+ else if (c.direction === "increase") {
501
+ let s = K;
502
+ if (c.funding.kind !== "none") {
503
+ const p = L(c.funding.input.coinType, c.funding.input.amount);
504
+ c.funding.kind === "collateral" ? ae(p) : s ? e.mergeCoins(s, [p]) : s = p;
449
505
  }
450
- for (const u of l.conversions)
451
- n(c, "INVALID_QUOTE", "Missing conversion funding"), W(await pe(c, u)), c = void 0;
452
- if (B) {
453
- const u = e.moveCall({
454
- target: `${r.package}::incentive_v3::borrow_with_account_cap_v2`,
455
- typeArguments: [i.debtCoinType],
506
+ for (const p of c.conversions)
507
+ o(s, "INVALID_QUOTE", "Missing conversion funding"), ae(await ye(s, p)), s = void 0;
508
+ if (G) {
509
+ const p = e.moveCall({
510
+ target: `${i.package}::incentive_v3::borrow_with_account_cap_v2`,
511
+ typeArguments: [r.debtCoinType],
456
512
  arguments: [
457
513
  e.object("0x6"),
458
- e.object(r.priceOracle),
459
- e.object(r.storage),
460
- e.object(p.debt.contract.pool),
461
- e.pure.u8(p.debt.id),
462
- e.pure.u64(l.borrowAmount),
463
- e.object(r.incentiveV2),
464
- e.object(r.incentiveV3),
465
- v,
514
+ e.object(i.priceOracle),
515
+ e.object(i.storage),
516
+ e.object(u.debt.contract.pool),
517
+ e.pure.u8(u.debt.id),
518
+ e.pure.u64(c.borrowAmount),
519
+ e.object(i.incentiveV2),
520
+ e.object(i.incentiveV3),
521
+ w,
466
522
  e.object("0x5")
467
523
  ]
468
524
  });
469
- E.push({ coinType: i.debtCoinType, coin: le(P(u, i.debtCoinType)) });
525
+ m.push({ coinType: r.debtCoinType, coin: me(N(p, r.debtCoinType)) });
470
526
  }
471
527
  } else {
472
- const c = $ ? se($) : void 0;
473
- let u = e.pure.u64(
474
- l.withdrawAmount
528
+ const s = K ? ge(K) : void 0;
529
+ let p = e.pure.u64(
530
+ c.withdrawAmount
475
531
  );
476
- if (o.action.type === "close") {
477
- const T = e.moveCall({
478
- target: `${r.package}::account::account_owner`,
479
- arguments: [v]
532
+ if (a.action.type === "close") {
533
+ const v = e.moveCall({
534
+ target: `${i.package}::account::account_owner`,
535
+ arguments: [w]
480
536
  });
481
537
  e.moveCall({
482
- target: `${r.package}::lending::update_state_of_user`,
483
- arguments: [e.object("0x6"), e.object(r.storage), T]
538
+ target: `${i.package}::lending::update_state_of_user`,
539
+ arguments: [e.object("0x6"), e.object(i.storage), v]
484
540
  });
485
- const A = e.moveCall({
486
- target: `${r.package}::logic::user_collateral_balance`,
487
- arguments: [e.object(r.storage), e.pure.u8(p.collateral.id), T]
541
+ const E = e.moveCall({
542
+ target: `${i.package}::logic::user_collateral_balance`,
543
+ arguments: [e.object(i.storage), e.pure.u8(u.collateral.id), v]
488
544
  });
489
- if (V(i, l.snapshot, p))
490
- for (const [x, Ae] of [
491
- [A, e.pure.u256("1")],
492
- [e.pure.u256("999"), A]
545
+ if (Q(r, c.snapshot, u))
546
+ for (const [ie, Se] of [
547
+ [E, e.pure.u256("1")],
548
+ [e.pure.u256("999"), E]
493
549
  ]) {
494
- const ve = e.moveCall({
550
+ const ke = e.moveCall({
495
551
  target: "0x1::u256::checked_sub",
496
- arguments: [x, Ae]
552
+ arguments: [ie, Se]
497
553
  });
498
554
  e.moveCall({
499
555
  target: "0x1::option::destroy_some",
500
556
  typeArguments: ["u256"],
501
- arguments: [ve]
557
+ arguments: [ke]
502
558
  });
503
559
  }
504
- const Z = e.moveCall({ target: "0x1::u256::try_as_u64", arguments: [A] });
505
- if (u = e.moveCall({
560
+ const ne = e.moveCall({ target: "0x1::u256::try_as_u64", arguments: [E] });
561
+ if (p = e.moveCall({
506
562
  target: "0x1::option::destroy_some",
507
563
  typeArguments: ["u64"],
508
- arguments: [Z]
509
- }), k(p.collateral) !== 9 && (u = e.moveCall({
510
- target: `${r.package}::pool::unnormal_amount`,
511
- typeArguments: [i.collateralCoinType],
512
- arguments: [e.object(p.collateral.contract.pool), u]
513
- }), k(p.collateral) < 9)) {
514
- const x = e.moveCall({
564
+ arguments: [ne]
565
+ }), D(u.collateral) !== 9 && (p = e.moveCall({
566
+ target: `${i.package}::pool::unnormal_amount`,
567
+ typeArguments: [r.collateralCoinType],
568
+ arguments: [e.object(u.collateral.contract.pool), p]
569
+ }), D(u.collateral) < 9)) {
570
+ const ie = e.moveCall({
515
571
  target: "0x1::u64::checked_add",
516
- arguments: [u, e.pure.u64("1")]
572
+ arguments: [p, e.pure.u64("1")]
517
573
  });
518
- u = e.moveCall({
574
+ p = e.moveCall({
519
575
  target: "0x1::option::destroy_some",
520
576
  typeArguments: ["u64"],
521
- arguments: [x]
577
+ arguments: [ie]
522
578
  });
523
579
  }
524
580
  }
525
- const C = P(
581
+ const C = N(
526
582
  e.moveCall({
527
- target: `${r.package}::incentive_v3::withdraw_with_account_cap_v2`,
528
- typeArguments: [i.collateralCoinType],
583
+ target: `${i.package}::incentive_v3::withdraw_with_account_cap_v2`,
584
+ typeArguments: [r.collateralCoinType],
529
585
  arguments: [
530
586
  e.object("0x6"),
531
- e.object(r.priceOracle),
532
- e.object(r.storage),
533
- e.object(p.collateral.contract.pool),
534
- e.pure.u8(p.collateral.id),
535
- u,
536
- e.object(r.incentiveV2),
537
- e.object(r.incentiveV3),
538
- v,
587
+ e.object(i.priceOracle),
588
+ e.object(i.storage),
589
+ e.object(u.collateral.contract.pool),
590
+ e.pure.u8(u.collateral.id),
591
+ p,
592
+ e.object(i.incentiveV2),
593
+ e.object(i.incentiveV3),
594
+ w,
539
595
  e.object("0x5")
540
596
  ]
541
597
  }),
542
- i.collateralCoinType
598
+ r.collateralCoinType
543
599
  );
544
600
  let f;
545
- for (const T of l.conversions) {
546
- const A = e.splitCoins(C, [T.input.amount])[0];
547
- f = await pe(A, T);
601
+ for (const v of c.conversions) {
602
+ const E = e.splitCoins(C, [v.input.amount])[0];
603
+ f = await ye(E, v);
548
604
  }
549
- c && (f ? e.mergeCoins(f, [c]) : f = c), f && (f = le(f));
550
- const S = N(i.principalCoinType, i.collateralCoinType) ? C : f, K = o.action.type === "close" && V(i, I, p) && V(i, l.snapshot, p);
551
- K || (n(S, "INVALID_QUOTE", "Missing principal output"), o.action.type === "remove_principal" && te(e, S, o.action.amount));
552
- for (const T of o.estimate.minOutputs) {
553
- if (K && T.amount === "0" && N(T.coinType, i.principalCoinType))
605
+ s && (f ? e.mergeCoins(f, [s]) : f = s), f && (f = me(f));
606
+ const R = k(r.principalCoinType, r.collateralCoinType) ? C : f, z = a.action.type === "close" && Q(r, _, u) && Q(r, c.snapshot, u);
607
+ z || (o(R, "INVALID_QUOTE", "Missing principal output"), a.action.type === "remove_principal" && ce(e, R, a.action.amount));
608
+ for (const v of a.estimate.minOutputs) {
609
+ if (z && v.amount === "0" && k(v.coinType, r.principalCoinType))
554
610
  continue;
555
- const A = N(T.coinType, i.collateralCoinType) ? C : f;
556
- n(A, "INVALID_QUOTE", "Missing minimum output coin"), te(e, A, T.amount);
611
+ const E = k(v.coinType, r.collateralCoinType) ? C : f;
612
+ o(E, "INVALID_QUOTE", "Missing minimum output coin"), ce(e, E, v.amount);
557
613
  }
558
- E.push({ coinType: i.collateralCoinType, coin: C }), f && E.push({ coinType: i.debtCoinType, coin: f });
614
+ m.push({ coinType: r.collateralCoinType, coin: C }), f && m.push({ coinType: r.debtCoinType, coin: f });
559
615
  }
560
- const J = e.moveCall({
561
- target: `${r.package}::account::account_owner`,
562
- arguments: [v]
616
+ const oe = e.moveCall({
617
+ target: `${i.package}::account::account_owner`,
618
+ arguments: [w]
563
619
  });
564
- if (o.action.type === "close")
565
- for (const [c, u] of [
566
- ["user_collateral_balance", p.collateral.id],
567
- ["user_loan_balance", p.debt.id]
620
+ if (a.action.type === "close")
621
+ for (const [s, p] of [
622
+ ["user_collateral_balance", u.collateral.id],
623
+ ["user_loan_balance", u.debt.id]
568
624
  ]) {
569
625
  const C = e.moveCall({
570
- target: `${r.package}::logic::${c}`,
571
- arguments: [e.object(r.storage), e.pure.u8(u), J]
626
+ target: `${i.package}::logic::${s}`,
627
+ arguments: [e.object(i.storage), e.pure.u8(p), oe]
572
628
  }), f = e.moveCall({
573
629
  target: "0x1::u256::checked_sub",
574
630
  arguments: [e.pure.u256("0"), C]
@@ -579,61 +635,67 @@ function We(w = {}) {
579
635
  arguments: [f]
580
636
  });
581
637
  }
582
- if (o.constraints.maxLeverage !== void 0) {
583
- const c = (T, A) => e.moveCall({
584
- target: `${r.package}::logic::${T}`,
638
+ const fe = Ae(
639
+ a.constraints.maxLeverage,
640
+ a.action,
641
+ c.direction,
642
+ Y(_, u).leverage
643
+ );
644
+ if (fe) {
645
+ const s = (v, E) => e.moveCall({
646
+ target: `${i.package}::logic::${v}`,
585
647
  arguments: [
586
648
  e.object("0x6"),
587
- e.object(r.priceOracle),
588
- e.object(r.storage),
589
- e.pure.u8(A),
590
- J
649
+ e.object(i.priceOracle),
650
+ e.object(i.storage),
651
+ e.pure.u8(E),
652
+ oe
591
653
  ]
592
- }), u = (T, A) => {
593
- const Z = e.moveCall({
654
+ }), p = (v, E) => {
655
+ const ne = e.moveCall({
594
656
  target: "0x1::u256::checked_mul",
595
- arguments: [T, e.pure.u256(A)]
657
+ arguments: [v, e.pure.u256(E)]
596
658
  });
597
659
  return e.moveCall({
598
660
  target: "0x1::option::destroy_some",
599
661
  typeArguments: ["u256"],
600
- arguments: [Z]
662
+ arguments: [ne]
601
663
  });
602
- }, C = new m(o.constraints.maxLeverage).shiftedBy(12).integerValue(m.ROUND_FLOOR), f = u(
603
- c("user_collateral_value", p.collateral.id),
664
+ }, C = fe.shiftedBy(12).integerValue(y.ROUND_FLOOR), f = p(
665
+ s("user_collateral_value", u.collateral.id),
604
666
  C.minus("1e12").toFixed(0)
605
- ), S = u(c("user_loan_value", p.debt.id), C.toFixed(0)), K = e.moveCall({
667
+ ), R = p(s("user_loan_value", u.debt.id), C.toFixed(0)), z = e.moveCall({
606
668
  target: "0x1::u256::checked_sub",
607
- arguments: [f, S]
669
+ arguments: [f, R]
608
670
  });
609
671
  e.moveCall({
610
672
  target: "0x1::option::destroy_some",
611
673
  typeArguments: ["u256"],
612
- arguments: [K]
674
+ arguments: [z]
613
675
  });
614
676
  }
615
677
  return e.moveCall({
616
- target: `${r.limter}::navi_adaptor::verify_navi_position_healthy`,
678
+ target: `${i.limter}::navi_adaptor::verify_navi_position_healthy`,
617
679
  arguments: [
618
680
  e.object("0x6"),
619
- e.object(r.storage),
620
- e.object(r.priceOracle),
621
- J,
681
+ e.object(i.storage),
682
+ e.object(i.priceOracle),
683
+ oe,
622
684
  e.pure.u256(
623
- new m(o.constraints.minHealthFactor).shiftedBy(27).integerValue(m.ROUND_CEIL).toFixed(0)
685
+ new y(a.constraints.minHealthFactor).shiftedBy(27).integerValue(y.ROUND_CEIL).toFixed(0)
624
686
  )
625
687
  ]
626
- }), { coins: E, accountCap: v, createdAccountCap: y };
688
+ }), { coins: m, accountCap: w, createdAccountCap: I };
627
689
  }
628
- function be(e, s, t) {
629
- return ce(e, s, t, !1);
690
+ function Oe(e, l, t) {
691
+ return de(e, l, t, !1);
630
692
  }
631
- function _e(e, s, t) {
632
- return ce(e, s, { coins: t }, !0);
693
+ function Ne(e, l, t) {
694
+ return de(e, l, { coins: t }, !0);
633
695
  }
634
- return { getPosition: fe, quote: Ce, buildPTB: be, buildPTBForSimulation: _e };
696
+ return { getPosition: Ie, quote: we, buildPTB: Oe, buildPTBForSimulation: Ne };
635
697
  }
636
698
  export {
637
- We as createMultiplyClient
699
+ rt as createMultiplyClient
638
700
  };
639
701
  //# sourceMappingURL=client.js.map