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