@naviprotocol/multiply 0.2.0-preview.20261124.2 → 0.2.0-preview.20261124.4

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