@naviprotocol/multiply 0.2.0-preview.20261102.2 → 0.2.0-preview.20261103.1

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,198 +1,243 @@
1
- import { voloConverter as me, haedalConverter as de, astrosConverter as ge, validateConversion as Ce } from "./converters.js";
2
- import { defaultClient as ye, createNaviDataSource as be } from "./data-source.js";
3
- import { strategies as fe, capName as W } from "./strategies.js";
4
- import { validateAction as ne, metrics as ce, plan as he, chooseConversion as ve } from "./planner.js";
5
- import { tokenDecimals as E, amountCushion as re, withinLtv as _e, supplyCapacity as Ae, borrowCapacity as Te, atomPrices as we } from "./amounts.js";
6
- import { check as i, jsonCopy as P, coinType as J, sameCoin as R, checksum as Q, address as f, BN as l, minOutputGuard as K, uint as q } from "./utils.js";
7
- function Le(v = {}) {
8
- const B = v.client ?? ye(), y = v.dataSource ?? be({ client: B, services: v.services }), Z = v.converters ?? [me({ client: B }), de({ client: B })], x = v.fallbackConverter ?? ge({ services: v.services }), $ = /* @__PURE__ */ new Map();
9
- for (const e of [...Z, x])
10
- i(!$.has(e.id), "INVALID_ARGUMENT", `Duplicate converter id: ${e.id}`), $.set(e.id, e);
11
- const G = (v.strategies ?? Object.values(fe)).map(
12
- (e) => N(P(e))
1
+ import { voloConverter as Ae, haedalConverter as Te, astrosConverter as _e, validateConversion as he } from "./converters.js";
2
+ import { defaultClient as ve, createNaviDataSource as we } from "./data-source.js";
3
+ import { strategies as Ie } from "./strategies.js";
4
+ import { indexRegistryStrategies as Ee, normalizeCapRegistry as ue, MAINNET_CAP_REGISTRY as Oe, registryKeyString as O, registerCreatedCapPTB as ke } from "./cap-registry.js";
5
+ import { validateAction as pe, metrics as de, plan as Ne, chooseConversion as Se } from "./planner.js";
6
+ import { tokenDecimals as N, amountCushion as ge, withinLtv as Ue, supplyCapacity as Le, borrowCapacity as je, atomPrices as De } from "./amounts.js";
7
+ import { check as r, jsonCopy as S, coinType as x, sameCoin as K, checksum as U, address as C, BN as u, minOutputGuard as ee, uint as G } from "./utils.js";
8
+ function Be(T = {}) {
9
+ const z = T.client ?? ve(), te = T.converters ?? [Ae({ client: z }), Te({ client: z })], ae = T.fallbackConverter ?? _e({ services: T.services }), P = /* @__PURE__ */ new Map();
10
+ for (const e of [...te, ae])
11
+ r(!P.has(e.id), "INVALID_ARGUMENT", `Duplicate converter id: ${e.id}`), P.set(e.id, e);
12
+ const L = (T.strategies ?? Object.values(Ie)).map(
13
+ (e) => re(S(e))
13
14
  );
14
- i(
15
- new Set(G.map((e) => e.id)).size === G.length,
15
+ r(
16
+ new Set(L.map((e) => e.id)).size === L.length,
16
17
  "INVALID_ARGUMENT",
17
18
  "Strategy registry contains duplicate ids"
18
19
  );
19
- function N(e) {
20
- i(
20
+ const oe = Ee(L), j = ue(T.capRegistry ?? Oe), b = T.dataSource ?? we({
21
+ client: z,
22
+ services: T.services,
23
+ capRegistry: j,
24
+ strategies: L
25
+ });
26
+ function ne(e = "INVALID_ARGUMENT") {
27
+ r(
28
+ b.capRegistry && JSON.stringify(ue(b.capRegistry)) === JSON.stringify(j),
29
+ e,
30
+ "DataSource Registry deployment differs from the execution deployment"
31
+ );
32
+ }
33
+ ne();
34
+ async function Y(e, d = "INVALID_ARGUMENT") {
35
+ const s = re(S(e)), a = oe.get(O(s.registryKey));
36
+ return r(
37
+ a && await U(a) === await U(s),
38
+ d,
39
+ "Strategy differs from the configured Registry binding"
40
+ ), s;
41
+ }
42
+ function re(e) {
43
+ r(
21
44
  typeof e?.id == "string" && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(e.id),
22
45
  "INVALID_ARGUMENT",
23
46
  "Strategy id must be a stable ASCII slug"
24
- ), i(
47
+ ), r(
25
48
  typeof e.market == "string" && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(e.market),
26
49
  "INVALID_ARGUMENT",
27
50
  "Missing market"
28
- ), i(
51
+ ), r(
29
52
  e.emodeId === void 0,
30
53
  "STRATEGY_UNSUPPORTED",
31
54
  "E-Mode risk overrides are not supported in this release"
32
- );
33
- const p = {
55
+ ), O(e.registryKey);
56
+ const d = {
34
57
  ...e,
35
- principalCoinType: J(e.principalCoinType),
36
- collateralCoinType: J(e.collateralCoinType),
37
- debtCoinType: J(e.debtCoinType)
58
+ registryKey: { ...e.registryKey },
59
+ principalCoinType: x(e.principalCoinType),
60
+ collateralCoinType: x(e.collateralCoinType),
61
+ debtCoinType: x(e.debtCoinType)
38
62
  };
39
- return i(
40
- R(p.principalCoinType, p.debtCoinType) && !R(p.collateralCoinType, p.debtCoinType),
63
+ return r(
64
+ K(d.principalCoinType, d.debtCoinType) && !K(d.collateralCoinType, d.debtCoinType),
41
65
  "STRATEGY_UNSUPPORTED",
42
66
  "Requires principal = debt and a distinct collateral token"
43
- ), p;
67
+ ), d;
44
68
  }
45
- async function z(e, p) {
46
- const u = (await y.listAccountCaps(e)).filter((m) => m.strategyId === p.id);
47
- i(
48
- u.length <= 1,
49
- "DUPLICATE_ACCOUNT_CAP",
50
- "More than one Multiply Cap exists for this wallet and strategy"
51
- );
52
- const t = u[0];
53
- return t && (f(t.id), f(t.accountOwner), i(
54
- t.name === W(p.id),
55
- "INVALID_ACCOUNT_CAP",
56
- "Cap name does not match strategy"
57
- ), t.strategy && i(
58
- await Q(N(t.strategy)) === await Q(p),
59
- "INVALID_ACCOUNT_CAP",
60
- "Stored strategy definition differs from the requested market or tokens"
61
- )), t;
69
+ async function H(e) {
70
+ ne("STALE_QUOTE");
71
+ const d = await b.listAccountCaps(e), s = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), y = /* @__PURE__ */ new Set();
72
+ return d.map((p) => {
73
+ const t = O(p.registryKey), c = oe.get(t);
74
+ r(c, "UNSUPPORTED_REGISTRY_STRATEGY", "Unknown registered Multiply strategy"), r(
75
+ c.id === p.strategyId,
76
+ "INVALID_ACCOUNT_CAP",
77
+ "Cap numeric binding differs from strategy ID"
78
+ );
79
+ const g = C(p.id), m = C(p.accountOwner);
80
+ return r(
81
+ g !== C("0x0") && m !== C("0x0"),
82
+ "INVALID_ACCOUNT_CAP",
83
+ "Zero Cap identity"
84
+ ), r(
85
+ !s.has(t) && !a.has(g) && !y.has(m),
86
+ "DUPLICATE_ACCOUNT_CAP",
87
+ "Duplicate strategy, Cap or lending account in Registry"
88
+ ), s.add(t), a.add(g), y.add(m), { ...p, id: g, accountOwner: m, registryKey: { ...p.registryKey } };
89
+ });
90
+ }
91
+ function X(e, d) {
92
+ const s = O(d.registryKey);
93
+ return e.find((a) => O(a.registryKey) === s);
62
94
  }
63
- async function ie(e, p = {}) {
64
- e = f(e);
65
- const u = [];
66
- for (const t of (p.strategy ? [p.strategy] : G).map(
67
- N
68
- )) {
69
- const m = await z(e, t);
70
- if (!m) continue;
71
- const a = await y.readMarket(t), c = await y.readPosition(m, t, a);
72
- q(c.collateralAmount), q(c.debtAmount), u.push({
95
+ async function me(e, d = {}) {
96
+ e = C(e);
97
+ const s = [], a = await H(e);
98
+ for (const y of d.strategy ? [d.strategy] : L) {
99
+ const p = await Y(y), t = X(a, p);
100
+ if (!t) continue;
101
+ const c = await b.readMarket(p), g = await b.readPosition(t, p, c);
102
+ G(g.collateralAmount), G(g.debtAmount), s.push({
73
103
  address: e,
74
- strategy: t,
75
- accountCap: m,
76
- ...c,
77
- ...ce(c, a)
104
+ strategy: p,
105
+ accountCap: t,
106
+ ...g,
107
+ ...de(g, c)
78
108
  });
79
109
  }
80
- return u;
110
+ return s;
81
111
  }
82
- async function se(e, p, u, t) {
83
- e = f(e), i(e !== f("0x0"), "INVALID_ARGUMENT", "Multiply owner cannot be zero"), ne(u, t);
84
- const m = N(p), a = await z(e, m);
85
- i(
86
- a || u.type === "open",
112
+ async function ye(e, d, s, a) {
113
+ e = C(e), r(e !== C("0x0"), "INVALID_ARGUMENT", "Multiply owner cannot be zero"), pe(s, a);
114
+ const y = await Y(d), p = X(await H(e), y);
115
+ r(
116
+ p || s.type === "open",
87
117
  "POSITION_NOT_FOUND",
88
118
  "Only open may create a Multiply Cap"
89
119
  );
90
- const c = await y.readMarket(m), g = a ? await y.readPosition(a, m, c) : { collateralAmount: "0", debtAmount: "0" };
91
- q(g.collateralAmount), q(g.debtAmount);
92
- const C = g.collateralAmount === "0" && g.debtAmount === "0";
93
- if (i(
94
- u.type !== "open" || C,
120
+ const t = await b.readMarket(y), c = p ? await b.readPosition(p, y, t) : { collateralAmount: "0", debtAmount: "0" };
121
+ G(c.collateralAmount), G(c.debtAmount);
122
+ const g = c.collateralAmount === "0" && c.debtAmount === "0";
123
+ if (r(
124
+ s.type !== "open" || g,
95
125
  "POSITION_EXISTS",
96
126
  "Use add_principal or set_leverage for an existing position"
97
- ), i(
98
- u.type === "open" || !C,
127
+ ), r(
128
+ s.type === "open" || !g,
99
129
  "POSITION_NOT_FOUND",
100
130
  "Empty position; use open to reuse this Cap"
101
- ), "targetLeverage" in u && u.targetLeverage) {
102
- const b = new l(c.liquidationThreshold).div(
103
- new l(c.borrowWeight).times(t.minHealthFactor)
104
- ), j = new l(1).minus(new l(1).div(u.targetLeverage)), L = we(c), k = !["open", "add_principal"].includes(u.type) && j.times(g.collateralAmount).times(L.collateral).lte(new l(g.debtAmount).times(L.debt));
105
- i(
106
- j.lte(b) && (j.lte(new l(c.ltv).div(c.borrowWeight)) || k),
131
+ ), "targetLeverage" in s && s.targetLeverage) {
132
+ const A = new u(t.liquidationThreshold).div(
133
+ new u(t.borrowWeight).times(a.minHealthFactor)
134
+ ), v = new u(1).minus(new u(1).div(s.targetLeverage)), M = De(t), F = !["open", "add_principal"].includes(s.type) && v.times(c.collateralAmount).times(M.collateral).lte(new u(c.debtAmount).times(M.debt));
135
+ r(
136
+ v.lte(A) && (v.lte(new u(t.ltv).div(t.borrowWeight)) || F),
107
137
  "CONSTRAINT_VIOLATION",
108
138
  "Target leverage exceeds protocol or health factor limits"
109
139
  );
110
140
  }
111
- const o = await y.readBorrowFeeBps(c, a?.accountOwner), _ = await he(
112
- m,
113
- u,
114
- t,
115
- g,
141
+ const m = await b.readBorrowFeeBps(t, p?.accountOwner), o = await Ne(
142
+ y,
143
+ s,
144
+ a,
116
145
  c,
117
- o,
118
- (b) => ve(b, Z, x)
119
- ), A = {
146
+ t,
147
+ m,
148
+ (A) => Se(A, te, ae)
149
+ ), h = {
120
150
  address: e,
121
- strategy: m,
122
- action: P(u),
123
- constraints: P(t),
151
+ strategy: y,
152
+ action: S(s),
153
+ constraints: S(a),
124
154
  expiresAt: Math.min(
125
155
  Date.now() + 12e4,
126
- ..._.execution.conversions.map((b) => b.expiresAt)
156
+ ...o.execution.conversions.map((A) => A.expiresAt)
127
157
  ),
128
- estimate: _.estimate,
158
+ estimate: o.estimate,
129
159
  execution: {
130
- ..._.execution,
131
- accountCap: a ? { kind: "existing", ...a } : { kind: "create", name: W(m.id) },
160
+ ...o.execution,
161
+ capRegistry: { ...j },
162
+ accountCap: p ? { kind: "existing", ...p } : { kind: "create", registryKey: { ...y.registryKey } },
132
163
  checksum: ""
133
164
  }
134
165
  };
135
- return A.execution.checksum = await Q(A), P(A);
166
+ return h.execution.checksum = await U(h), S(h);
136
167
  }
137
- async function le(e, p, u) {
138
- const t = P(p);
139
- i(
140
- t.execution?.version === 2,
168
+ async function fe(e, d, s) {
169
+ const a = S(d);
170
+ r(
171
+ a.execution?.version === 3,
141
172
  "INVALID_QUOTE",
142
173
  "Unknown quote version; request a fresh quote"
143
174
  );
144
- const m = t.execution.checksum;
145
- t.execution.checksum = "", i(
146
- m === await Q(t),
175
+ const y = a.execution.checksum;
176
+ a.execution.checksum = "", r(
177
+ y === await U(a),
147
178
  "INVALID_QUOTE",
148
179
  "Quote contents have changed; request a fresh quote"
149
- ), t.execution.checksum = m, i(
150
- Number.isSafeInteger(t.expiresAt) && t.expiresAt > Date.now(),
180
+ ), a.execution.checksum = y, r(
181
+ Number.isSafeInteger(a.expiresAt) && a.expiresAt > Date.now(),
151
182
  "QUOTE_EXPIRED",
152
183
  "Multiply quote expired"
153
- ), i(
154
- f(t.address) !== f("0x0"),
184
+ ), r(
185
+ C(a.address) !== C("0x0"),
155
186
  "INVALID_ARGUMENT",
156
187
  "Multiply owner cannot be zero"
157
- ), N(t.strategy), ne(t.action, t.constraints);
158
- const a = t.execution, c = t.strategy, g = u.coins ?? [];
159
- i(
160
- g.length === t.estimate.inputs.length,
188
+ ), await Y(a.strategy, "STALE_QUOTE"), r(
189
+ await U(a.execution.capRegistry) === await U(j),
190
+ "STALE_QUOTE",
191
+ "Registry deployment changed since quote"
192
+ ), r(
193
+ O(a.execution.accountCap.registryKey) === O(a.strategy.registryKey),
194
+ "INVALID_QUOTE",
195
+ "Quote Cap key differs from strategy"
196
+ );
197
+ const p = e.getData().sender;
198
+ r(
199
+ !p || C(p) === C(a.address),
200
+ "INVALID_INPUTS",
201
+ "Transaction sender differs from quote wallet"
202
+ ), pe(a.action, a.constraints);
203
+ const t = a.execution, c = a.strategy, g = s.coins ?? [];
204
+ r(
205
+ g.length === a.estimate.inputs.length,
161
206
  "INVALID_INPUTS",
162
207
  "Provide exactly the quoted input coins"
163
208
  );
164
- for (const n of t.estimate.inputs)
165
- i(
166
- g.filter((s) => R(s.coinType, n.coinType)).length === 1,
209
+ for (const n of a.estimate.inputs)
210
+ r(
211
+ g.filter((l) => K(l.coinType, n.coinType)).length === 1,
167
212
  "INVALID_INPUTS",
168
213
  `Missing or duplicate input: ${n.coinType}`
169
214
  );
170
- for (const n of a.conversions)
171
- i(
172
- $.has(n.converterId),
215
+ for (const n of t.conversions)
216
+ r(
217
+ P.has(n.converterId),
173
218
  "INVALID_CONVERSION",
174
219
  "Quoted converter is not configured"
175
- ), Ce(
220
+ ), he(
176
221
  n,
177
222
  {
178
223
  coinIn: n.input.coinType,
179
224
  coinOut: n.expectedOutput.coinType,
180
225
  amountIn: n.input.amount,
181
- slippageBps: t.constraints.maxSlippageBps
226
+ slippageBps: a.constraints.maxSlippageBps
182
227
  },
183
228
  n.converterId
184
229
  );
185
- const C = await z(t.address, c);
186
- a.accountCap.kind === "create" ? i(
187
- !C && !u.accountCap,
230
+ const m = X(await H(a.address), c);
231
+ t.accountCap.kind === "create" ? r(
232
+ !m && !s.accountCap,
188
233
  "STALE_QUOTE",
189
234
  "Cap was created since quote or an unexpected Cap was supplied"
190
- ) : (i(
191
- C && C.id === a.accountCap.id && C.accountOwner === a.accountCap.accountOwner,
235
+ ) : (r(
236
+ m && m.id === t.accountCap.id && m.accountOwner === t.accountCap.accountOwner,
192
237
  "STALE_QUOTE",
193
238
  "AccountCap changed since quote"
194
- ), typeof u.accountCap == "string" && i(f(u.accountCap) === f(C.id), "INVALID_INPUTS", "Wrong AccountCap"));
195
- const o = await y.readMarket(c);
239
+ ), typeof s.accountCap == "string" && r(C(s.accountCap) === C(m.id), "INVALID_INPUTS", "Wrong AccountCap"));
240
+ const o = await b.readMarket(c);
196
241
  for (const n of [
197
242
  "package",
198
243
  "storage",
@@ -201,122 +246,116 @@ function Le(v = {}) {
201
246
  "incentiveV3",
202
247
  "limter"
203
248
  ])
204
- i(o.config[n] === a.market.config[n], "STALE_QUOTE", `Market ${n} changed`);
205
- i(
206
- o.collateral.contract.pool === a.market.collateral.contract.pool && o.debt.contract.pool === a.market.debt.contract.pool && o.flash.poolId === a.market.flash.poolId && E(o.collateral) === E(a.market.collateral) && E(o.debt) === E(a.market.debt),
249
+ r(o.config[n] === t.market.config[n], "STALE_QUOTE", `Market ${n} changed`);
250
+ r(
251
+ o.collateral.contract.pool === t.market.collateral.contract.pool && o.debt.contract.pool === t.market.debt.contract.pool && o.flash.poolId === t.market.flash.poolId && N(o.collateral) === N(t.market.collateral) && N(o.debt) === N(t.market.debt),
207
252
  "STALE_QUOTE",
208
253
  "Pool configuration changed"
209
254
  );
210
- const _ = C ? await y.readPosition(C, c, o) : { collateralAmount: "0", debtAmount: "0" }, A = new l(_.collateralAmount).minus(a.snapshot.collateralAmount), b = new l(_.debtAmount).minus(a.snapshot.debtAmount);
211
- i(
255
+ const h = m ? await b.readPosition(m, c, o) : { collateralAmount: "0", debtAmount: "0" }, A = new u(h.collateralAmount).minus(t.snapshot.collateralAmount), v = new u(h.debtAmount).minus(t.snapshot.debtAmount);
256
+ r(
212
257
  A.gte(0) && A.lte(
213
- new l(a.snapshot.collateralAmount).times("0.0001").plus(re(o.collateral))
214
- ) && b.gte(0) && b.lte(
215
- new l(a.snapshot.debtAmount).times("0.0001").plus(re(o.debt))
258
+ new u(t.snapshot.collateralAmount).times("0.0001").plus(ge(o.collateral))
259
+ ) && v.gte(0) && v.lte(
260
+ new u(t.snapshot.debtAmount).times("0.0001").plus(ge(o.debt))
216
261
  ),
217
262
  "STALE_QUOTE",
218
263
  "Position changed since quote"
219
264
  );
220
- const j = await y.readBorrowFeeBps(o, C?.accountOwner);
221
- i(
222
- new l(a.borrowAmount).times(j).div(1e4).integerValue().lte(a.borrowFee),
265
+ const M = await b.readBorrowFeeBps(o, m?.accountOwner);
266
+ r(
267
+ new u(t.borrowAmount).times(M).div(1e4).integerValue().lte(t.borrowFee),
223
268
  "STALE_QUOTE",
224
269
  "Borrow fee increased"
225
- ), i(o.flash.feeBps === a.market.flash.feeBps, "STALE_QUOTE", "Flash fee changed");
226
- const L = {
227
- collateralAmount: new l(a.minCollateralAfter).plus(A).toFixed(0),
228
- debtAmount: new l(a.debtAfter).plus(t.action.type === "close" ? 0 : b).toFixed(0)
229
- }, k = ce(L, o);
230
- i(
231
- k.healthFactor === null || new l(k.healthFactor).gte(t.constraints.minHealthFactor),
270
+ ), r(o.flash.feeBps === t.market.flash.feeBps, "STALE_QUOTE", "Flash fee changed");
271
+ const F = {
272
+ collateralAmount: new u(t.minCollateralAfter).plus(A).toFixed(0),
273
+ debtAmount: new u(t.debtAfter).plus(a.action.type === "close" ? 0 : v).toFixed(0)
274
+ }, Q = de(F, o);
275
+ r(
276
+ Q.healthFactor === null || new u(Q.healthFactor).gte(a.constraints.minHealthFactor),
232
277
  "STALE_QUOTE",
233
278
  "Updated market no longer satisfies HF"
234
- ), i(
279
+ ), r(
235
280
  Number.isSafeInteger(o.observedAt) && Date.now() - o.observedAt <= 12e4,
236
281
  "STALE_QUOTE",
237
282
  "Current market snapshot is stale"
238
- ), t.constraints.maxLeverage && k.leverage && i(
239
- new l(k.leverage).lte(t.constraints.maxLeverage),
283
+ ), a.constraints.maxLeverage && Q.leverage && r(
284
+ new u(Q.leverage).lte(a.constraints.maxLeverage),
240
285
  "STALE_QUOTE",
241
286
  "Updated market exceeds maximum leverage"
242
- ), i(
243
- _e(L, _, o, a.direction !== "increase"),
287
+ ), r(
288
+ Ue(F, h, o, t.direction !== "increase"),
244
289
  "STALE_QUOTE",
245
290
  "Updated market exceeds LTV"
246
- ), i(
247
- new l(a.flashAmount).lte(o.flash.max) && (a.flashAmount === "0" || new l(a.flashAmount).gte(o.flash.min)),
291
+ ), r(
292
+ new u(t.flashAmount).lte(o.flash.max) && (t.flashAmount === "0" || new u(t.flashAmount).gte(o.flash.min)),
248
293
  "CAPACITY_EXCEEDED",
249
294
  "Flash capacity changed"
250
- ), i(
251
- new l(a.collateralAfter).minus(a.snapshot.collateralAmount).lte(Ae(o)),
295
+ ), r(
296
+ new u(t.collateralAfter).minus(t.snapshot.collateralAmount).lte(Le(o)),
252
297
  "CAPACITY_EXCEEDED",
253
298
  "Supply capacity changed"
254
- ), i(
255
- new l(a.borrowAmount).plus(a.borrowFee).lte(Te(o)),
299
+ ), r(
300
+ new u(t.borrowAmount).plus(t.borrowFee).lte(je(o)),
256
301
  "CAPACITY_EXCEEDED",
257
302
  "Borrow liquidity changed"
258
- ), await y.prepareOracle(e, c, o);
259
- const r = o.config, U = a.accountCap.kind === "create" ? e.moveCall({ target: `${r.package}::lending::create_account` }) : void 0, T = U ?? u.accountCap ?? e.object(a.accountCap.id);
260
- U && (e.moveCall({
261
- target: `${r.package}::account::set_account_name`,
262
- arguments: [U, e.pure.string(W(c.id))]
263
- }), e.moveCall({
264
- target: `${r.package}::account::set_account_description`,
265
- arguments: [U, e.pure.string(JSON.stringify(c))]
266
- }));
267
- const w = [], H = (n, s) => {
268
- const d = g.find((O) => R(O.coinType, n)), I = e.splitCoins(d.coin, [s])[0];
269
- return w.push(d), I;
270
- }, S = (n, s) => e.moveCall({
303
+ ), e.setSenderIfNotSet(a.address), await b.prepareOracle(e, c, o);
304
+ const i = o.config, $ = t.accountCap.kind === "create" ? e.moveCall({ target: `${i.package}::lending::create_account` }) : void 0, w = $ ?? s.accountCap ?? e.object(t.accountCap.id);
305
+ $ && ke(e, $, c.registryKey, j);
306
+ const I = [], W = (n, l) => {
307
+ const f = g.find((k) => K(k.coinType, n)), E = e.splitCoins(f.coin, [l])[0];
308
+ return I.push(f), E;
309
+ }, D = (n, l) => e.moveCall({
271
310
  target: "0x2::coin::from_balance",
272
- typeArguments: [s],
311
+ typeArguments: [l],
273
312
  arguments: [n]
274
- }), ue = (n, s) => e.moveCall({ target: "0x2::coin::into_balance", typeArguments: [s], arguments: [n] }), ee = (n) => e.moveCall({
275
- target: `${r.package}::incentive_v3::deposit_with_account_cap`,
313
+ }), Ce = (n, l) => e.moveCall({ target: "0x2::coin::into_balance", typeArguments: [l], arguments: [n] }), ce = (n) => e.moveCall({
314
+ target: `${i.package}::incentive_v3::deposit_with_account_cap`,
276
315
  typeArguments: [c.collateralCoinType],
277
316
  arguments: [
278
317
  e.object("0x6"),
279
- e.object(r.storage),
318
+ e.object(i.storage),
280
319
  e.object(o.collateral.contract.pool),
281
320
  e.pure.u8(o.collateral.id),
282
321
  n,
283
- e.object(r.incentiveV2),
284
- e.object(r.incentiveV3),
285
- T
322
+ e.object(i.incentiveV2),
323
+ e.object(i.incentiveV3),
324
+ w
286
325
  ]
287
- }), te = (n) => S(
326
+ }), ie = (n) => D(
288
327
  e.moveCall({
289
- target: `${r.package}::incentive_v3::repay_with_account_cap`,
328
+ target: `${i.package}::incentive_v3::repay_with_account_cap`,
290
329
  typeArguments: [c.debtCoinType],
291
330
  arguments: [
292
331
  e.object("0x6"),
293
- e.object(r.priceOracle),
294
- e.object(r.storage),
332
+ e.object(i.priceOracle),
333
+ e.object(i.storage),
295
334
  e.object(o.debt.contract.pool),
296
335
  e.pure.u8(o.debt.id),
297
336
  n,
298
- e.object(r.incentiveV2),
299
- e.object(r.incentiveV3),
300
- T
337
+ e.object(i.incentiveV2),
338
+ e.object(i.incentiveV3),
339
+ w
301
340
  ]
302
341
  }),
303
342
  c.debtCoinType
304
343
  );
305
- let F, M;
306
- if (a.flashAmount !== "0") {
344
+ let B, q;
345
+ if (t.flashAmount !== "0") {
307
346
  const n = e.moveCall({
308
347
  target: `${o.flash.config.package}::lending::flash_loan_with_ctx_v2`,
309
348
  typeArguments: [c.debtCoinType],
310
349
  arguments: [
311
350
  e.object(o.flash.config.flashloanConfig),
312
351
  e.object(o.flash.poolId),
313
- e.pure.u64(a.flashAmount),
352
+ e.pure.u64(t.flashAmount),
314
353
  e.object("0x5")
315
354
  ]
316
355
  });
317
- F = S(n[0], c.debtCoinType), M = n[1];
356
+ B = D(n[0], c.debtCoinType), q = n[1];
318
357
  }
319
- const ae = (n) => M ? S(
358
+ const se = (n) => q ? D(
320
359
  e.moveCall({
321
360
  target: `${o.flash.config.package}::lending::flash_repay_with_ctx`,
322
361
  typeArguments: [c.debtCoinType],
@@ -324,185 +363,185 @@ function Le(v = {}) {
324
363
  e.object("0x6"),
325
364
  e.object(o.flash.config.storage),
326
365
  e.object(o.flash.poolId),
327
- M,
328
- ue(n, c.debtCoinType)
366
+ q,
367
+ Ce(n, c.debtCoinType)
329
368
  ]
330
369
  }),
331
370
  c.debtCoinType
332
- ) : n, oe = async (n) => {
333
- const s = a.conversions[0], d = await $.get(s.converterId).buildPTB(e, s, n, {
334
- changeRecipient: t.address
371
+ ) : n, le = async (n) => {
372
+ const l = t.conversions[0], f = await P.get(l.converterId).buildPTB(e, l, n, {
373
+ changeRecipient: a.address
335
374
  });
336
- return K(e, d.coinOut, s.minOutput.amount), w.push(...d.change), d.coinOut;
375
+ return ee(e, f.coinOut, l.minOutput.amount), I.push(...f.change), f.coinOut;
337
376
  };
338
- if (a.direction === "repay")
339
- w.push({
377
+ if (t.direction === "repay")
378
+ I.push({
340
379
  coinType: c.debtCoinType,
341
- coin: te(H(c.debtCoinType, t.estimate.inputs[0].amount))
380
+ coin: ie(W(c.debtCoinType, a.estimate.inputs[0].amount))
342
381
  });
343
- else if (a.direction === "add_collateral")
344
- ee(H(c.collateralCoinType, t.estimate.inputs[0].amount));
345
- else if (a.direction === "increase") {
346
- let n = F;
347
- if (t.estimate.inputs.length) {
348
- const s = H(c.principalCoinType, t.estimate.inputs[0].amount);
349
- n ? e.mergeCoins(n, [s]) : n = s;
382
+ else if (t.direction === "add_collateral")
383
+ ce(W(c.collateralCoinType, a.estimate.inputs[0].amount));
384
+ else if (t.direction === "increase") {
385
+ let n = B;
386
+ if (a.estimate.inputs.length) {
387
+ const l = W(c.principalCoinType, a.estimate.inputs[0].amount);
388
+ n ? e.mergeCoins(n, [l]) : n = l;
350
389
  }
351
- if (i(n, "INVALID_QUOTE", "Increase plan has no funds"), ee(await oe(n)), M) {
352
- const s = e.moveCall({
353
- target: `${r.package}::incentive_v3::borrow_with_account_cap_v2`,
390
+ if (r(n, "INVALID_QUOTE", "Increase plan has no funds"), ce(await le(n)), q) {
391
+ const l = e.moveCall({
392
+ target: `${i.package}::incentive_v3::borrow_with_account_cap_v2`,
354
393
  typeArguments: [c.debtCoinType],
355
394
  arguments: [
356
395
  e.object("0x6"),
357
- e.object(r.priceOracle),
358
- e.object(r.storage),
396
+ e.object(i.priceOracle),
397
+ e.object(i.storage),
359
398
  e.object(o.debt.contract.pool),
360
399
  e.pure.u8(o.debt.id),
361
- e.pure.u64(a.borrowAmount),
362
- e.object(r.incentiveV2),
363
- e.object(r.incentiveV3),
364
- T,
400
+ e.pure.u64(t.borrowAmount),
401
+ e.object(i.incentiveV2),
402
+ e.object(i.incentiveV3),
403
+ w,
365
404
  e.object("0x5")
366
405
  ]
367
406
  });
368
- w.push({ coinType: c.debtCoinType, coin: ae(S(s, c.debtCoinType)) });
407
+ I.push({ coinType: c.debtCoinType, coin: se(D(l, c.debtCoinType)) });
369
408
  }
370
409
  } else {
371
- const n = F ? te(F) : void 0;
372
- let s = e.pure.u64(
373
- a.withdrawAmount
410
+ const n = B ? ie(B) : void 0;
411
+ let l = e.pure.u64(
412
+ t.withdrawAmount
374
413
  );
375
- if (t.action.type === "close") {
376
- const h = e.moveCall({
377
- target: `${r.package}::account::account_owner`,
378
- arguments: [T]
414
+ if (a.action.type === "close") {
415
+ const _ = e.moveCall({
416
+ target: `${i.package}::account::account_owner`,
417
+ arguments: [w]
379
418
  });
380
419
  e.moveCall({
381
- target: `${r.package}::lending::update_state_of_user`,
382
- arguments: [e.object("0x6"), e.object(r.storage), h]
420
+ target: `${i.package}::lending::update_state_of_user`,
421
+ arguments: [e.object("0x6"), e.object(i.storage), _]
383
422
  });
384
423
  const V = e.moveCall({
385
- target: `${r.package}::logic::user_collateral_balance`,
386
- arguments: [e.object(r.storage), e.pure.u8(o.collateral.id), h]
387
- }), Y = e.moveCall({ target: "0x1::u256::try_as_u64", arguments: [V] });
388
- if (s = e.moveCall({
424
+ target: `${i.package}::logic::user_collateral_balance`,
425
+ arguments: [e.object(i.storage), e.pure.u8(o.collateral.id), _]
426
+ }), Z = e.moveCall({ target: "0x1::u256::try_as_u64", arguments: [V] });
427
+ if (l = e.moveCall({
389
428
  target: "0x1::option::destroy_some",
390
429
  typeArguments: ["u64"],
391
- arguments: [Y]
392
- }), E(o.collateral) !== 9 && (s = e.moveCall({
393
- target: `${r.package}::pool::unnormal_amount`,
430
+ arguments: [Z]
431
+ }), N(o.collateral) !== 9 && (l = e.moveCall({
432
+ target: `${i.package}::pool::unnormal_amount`,
394
433
  typeArguments: [c.collateralCoinType],
395
- arguments: [e.object(o.collateral.contract.pool), s]
396
- }), E(o.collateral) < 9)) {
397
- const pe = e.moveCall({
434
+ arguments: [e.object(o.collateral.contract.pool), l]
435
+ }), N(o.collateral) < 9)) {
436
+ const be = e.moveCall({
398
437
  target: "0x1::u64::checked_add",
399
- arguments: [s, e.pure.u64("1")]
438
+ arguments: [l, e.pure.u64("1")]
400
439
  });
401
- s = e.moveCall({
440
+ l = e.moveCall({
402
441
  target: "0x1::option::destroy_some",
403
442
  typeArguments: ["u64"],
404
- arguments: [pe]
443
+ arguments: [be]
405
444
  });
406
445
  }
407
446
  }
408
- const d = S(
447
+ const f = D(
409
448
  e.moveCall({
410
- target: `${r.package}::incentive_v3::withdraw_with_account_cap_v2`,
449
+ target: `${i.package}::incentive_v3::withdraw_with_account_cap_v2`,
411
450
  typeArguments: [c.collateralCoinType],
412
451
  arguments: [
413
452
  e.object("0x6"),
414
- e.object(r.priceOracle),
415
- e.object(r.storage),
453
+ e.object(i.priceOracle),
454
+ e.object(i.storage),
416
455
  e.object(o.collateral.contract.pool),
417
456
  e.pure.u8(o.collateral.id),
418
- s,
419
- e.object(r.incentiveV2),
420
- e.object(r.incentiveV3),
421
- T,
457
+ l,
458
+ e.object(i.incentiveV2),
459
+ e.object(i.incentiveV3),
460
+ w,
422
461
  e.object("0x5")
423
462
  ]
424
463
  }),
425
464
  c.collateralCoinType
426
- ), I = e.splitCoins(d, [a.withdrawAmount])[0];
427
- w.push({ coinType: c.collateralCoinType, coin: d });
428
- const O = await oe(I);
429
- n && e.mergeCoins(O, [n]);
430
- const D = ae(O);
431
- t.action.type === "remove_principal" && K(e, D, t.action.amount);
432
- for (const h of t.estimate.minOutputs) K(e, D, h.amount);
433
- w.push({ coinType: c.principalCoinType, coin: D });
465
+ ), E = e.splitCoins(f, [t.withdrawAmount])[0];
466
+ I.push({ coinType: c.collateralCoinType, coin: f });
467
+ const k = await le(E);
468
+ n && e.mergeCoins(k, [n]);
469
+ const R = se(k);
470
+ a.action.type === "remove_principal" && ee(e, R, a.action.amount);
471
+ for (const _ of a.estimate.minOutputs) ee(e, R, _.amount);
472
+ I.push({ coinType: c.principalCoinType, coin: R });
434
473
  }
435
- const X = e.moveCall({
436
- target: `${r.package}::account::account_owner`,
437
- arguments: [T]
474
+ const J = e.moveCall({
475
+ target: `${i.package}::account::account_owner`,
476
+ arguments: [w]
438
477
  });
439
- if (t.action.type === "close")
440
- for (const [n, s] of [
478
+ if (a.action.type === "close")
479
+ for (const [n, l] of [
441
480
  ["user_collateral_balance", o.collateral.id],
442
481
  ["user_loan_balance", o.debt.id]
443
482
  ]) {
444
- const d = e.moveCall({
445
- target: `${r.package}::logic::${n}`,
446
- arguments: [e.object(r.storage), e.pure.u8(s), X]
447
- }), I = e.moveCall({
483
+ const f = e.moveCall({
484
+ target: `${i.package}::logic::${n}`,
485
+ arguments: [e.object(i.storage), e.pure.u8(l), J]
486
+ }), E = e.moveCall({
448
487
  target: "0x1::u256::checked_sub",
449
- arguments: [e.pure.u256("0"), d]
488
+ arguments: [e.pure.u256("0"), f]
450
489
  });
451
490
  e.moveCall({
452
491
  target: "0x1::option::destroy_some",
453
492
  typeArguments: ["u256"],
454
- arguments: [I]
493
+ arguments: [E]
455
494
  });
456
495
  }
457
- if (t.constraints.maxLeverage !== void 0) {
458
- const n = (h, V) => e.moveCall({
459
- target: `${r.package}::logic::${h}`,
496
+ if (a.constraints.maxLeverage !== void 0) {
497
+ const n = (_, V) => e.moveCall({
498
+ target: `${i.package}::logic::${_}`,
460
499
  arguments: [
461
500
  e.object("0x6"),
462
- e.object(r.priceOracle),
463
- e.object(r.storage),
501
+ e.object(i.priceOracle),
502
+ e.object(i.storage),
464
503
  e.pure.u8(V),
465
- X
504
+ J
466
505
  ]
467
- }), s = (h, V) => {
468
- const Y = e.moveCall({
506
+ }), l = (_, V) => {
507
+ const Z = e.moveCall({
469
508
  target: "0x1::u256::checked_mul",
470
- arguments: [h, e.pure.u256(V)]
509
+ arguments: [_, e.pure.u256(V)]
471
510
  });
472
511
  return e.moveCall({
473
512
  target: "0x1::option::destroy_some",
474
513
  typeArguments: ["u256"],
475
- arguments: [Y]
514
+ arguments: [Z]
476
515
  });
477
- }, d = new l(t.constraints.maxLeverage).shiftedBy(12).integerValue(l.ROUND_FLOOR), I = s(
516
+ }, f = new u(a.constraints.maxLeverage).shiftedBy(12).integerValue(u.ROUND_FLOOR), E = l(
478
517
  n("user_collateral_value", o.collateral.id),
479
- d.minus("1e12").toFixed(0)
480
- ), O = s(n("user_loan_value", o.debt.id), d.toFixed(0)), D = e.moveCall({
518
+ f.minus("1e12").toFixed(0)
519
+ ), k = l(n("user_loan_value", o.debt.id), f.toFixed(0)), R = e.moveCall({
481
520
  target: "0x1::u256::checked_sub",
482
- arguments: [I, O]
521
+ arguments: [E, k]
483
522
  });
484
523
  e.moveCall({
485
524
  target: "0x1::option::destroy_some",
486
525
  typeArguments: ["u256"],
487
- arguments: [D]
526
+ arguments: [R]
488
527
  });
489
528
  }
490
529
  return e.moveCall({
491
- target: `${r.limter}::navi_adaptor::verify_navi_position_healthy`,
530
+ target: `${i.limter}::navi_adaptor::verify_navi_position_healthy`,
492
531
  arguments: [
493
532
  e.object("0x6"),
494
- e.object(r.storage),
495
- e.object(r.priceOracle),
496
- X,
533
+ e.object(i.storage),
534
+ e.object(i.priceOracle),
535
+ J,
497
536
  e.pure.u256(
498
- new l(t.constraints.minHealthFactor).shiftedBy(27).integerValue(l.ROUND_CEIL).toFixed(0)
537
+ new u(a.constraints.minHealthFactor).shiftedBy(27).integerValue(u.ROUND_CEIL).toFixed(0)
499
538
  )
500
539
  ]
501
- }), { coins: w, accountCap: T, createdAccountCap: U };
540
+ }), { coins: I, accountCap: w, createdAccountCap: $ };
502
541
  }
503
- return { getPosition: ie, quote: se, buildPTB: le };
542
+ return { getPosition: me, quote: ye, buildPTB: fe };
504
543
  }
505
544
  export {
506
- Le as createMultiplyClient
545
+ Be as createMultiplyClient
507
546
  };
508
547
  //# sourceMappingURL=client.js.map