@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/README.md +25 -0
- package/dist/amounts.d.ts +19 -0
- package/dist/amounts.d.ts.map +1 -0
- package/dist/amounts.js +46 -0
- package/dist/amounts.js.map +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +222 -191
- package/dist/client.js.map +1 -1
- package/dist/data-source.d.ts.map +1 -1
- package/dist/data-source.js +88 -88
- package/dist/data-source.js.map +1 -1
- package/dist/planner.d.ts.map +1 -1
- package/dist/planner.js +196 -150
- package/dist/planner.js.map +1 -1
- package/dist/types.d.ts +3 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -1
package/dist/client.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import { voloConverter as
|
|
2
|
-
import { defaultClient as
|
|
3
|
-
import { strategies as
|
|
4
|
-
import { validateAction as
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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:
|
|
28
|
-
collateralCoinType:
|
|
29
|
-
debtCoinType:
|
|
35
|
+
principalCoinType: J(e.principalCoinType),
|
|
36
|
+
collateralCoinType: J(e.collateralCoinType),
|
|
37
|
+
debtCoinType: J(e.debtCoinType)
|
|
30
38
|
};
|
|
31
39
|
return i(
|
|
32
|
-
|
|
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
|
|
38
|
-
const u = (await
|
|
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 && (
|
|
46
|
-
t.name ===
|
|
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
|
|
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
|
|
56
|
-
e =
|
|
63
|
+
async function ie(e, p = {}) {
|
|
64
|
+
e = f(e);
|
|
57
65
|
const u = [];
|
|
58
|
-
for (const t of (p.strategy ? [p.strategy] :
|
|
59
|
-
|
|
66
|
+
for (const t of (p.strategy ? [p.strategy] : G).map(
|
|
67
|
+
N
|
|
60
68
|
)) {
|
|
61
|
-
const m = await
|
|
69
|
+
const m = await z(e, t);
|
|
62
70
|
if (!m) continue;
|
|
63
|
-
const a = await
|
|
64
|
-
|
|
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
|
-
...
|
|
77
|
+
...ce(c, a)
|
|
70
78
|
});
|
|
71
79
|
}
|
|
72
80
|
return u;
|
|
73
81
|
}
|
|
74
|
-
async function
|
|
75
|
-
e =
|
|
76
|
-
const 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
|
|
83
|
-
|
|
84
|
-
const
|
|
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" ||
|
|
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" || !
|
|
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
|
|
95
|
-
new
|
|
96
|
-
|
|
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
|
-
|
|
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
|
|
111
|
+
const o = await y.readBorrowFeeBps(c, a?.accountOwner), _ = await he(
|
|
107
112
|
m,
|
|
108
113
|
u,
|
|
109
114
|
t,
|
|
110
|
-
|
|
115
|
+
g,
|
|
111
116
|
c,
|
|
112
|
-
|
|
113
|
-
(
|
|
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
|
-
...
|
|
126
|
+
..._.execution.conversions.map((b) => b.expiresAt)
|
|
122
127
|
),
|
|
123
|
-
estimate:
|
|
128
|
+
estimate: _.estimate,
|
|
124
129
|
execution: {
|
|
125
|
-
...
|
|
126
|
-
accountCap: a ? { kind: "existing", ...a } : { kind: "create", name:
|
|
130
|
+
..._.execution,
|
|
131
|
+
accountCap: a ? { kind: "existing", ...a } : { kind: "create", name: W(m.id) },
|
|
127
132
|
checksum: ""
|
|
128
133
|
}
|
|
129
134
|
};
|
|
130
|
-
return
|
|
135
|
+
return A.execution.checksum = await Q(A), P(A);
|
|
131
136
|
}
|
|
132
|
-
async function
|
|
137
|
+
async function le(e, p, u) {
|
|
133
138
|
const t = P(p);
|
|
134
|
-
i(
|
|
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
|
|
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(
|
|
145
|
-
|
|
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
|
-
|
|
160
|
+
g.length === t.estimate.inputs.length,
|
|
148
161
|
"INVALID_INPUTS",
|
|
149
162
|
"Provide exactly the quoted input coins"
|
|
150
163
|
);
|
|
151
|
-
for (const
|
|
164
|
+
for (const n of t.estimate.inputs)
|
|
152
165
|
i(
|
|
153
|
-
|
|
166
|
+
g.filter((s) => R(s.coinType, n.coinType)).length === 1,
|
|
154
167
|
"INVALID_INPUTS",
|
|
155
|
-
`Missing or duplicate input: ${
|
|
168
|
+
`Missing or duplicate input: ${n.coinType}`
|
|
156
169
|
);
|
|
157
|
-
for (const
|
|
170
|
+
for (const n of a.conversions)
|
|
158
171
|
i(
|
|
159
|
-
|
|
172
|
+
$.has(n.converterId),
|
|
160
173
|
"INVALID_CONVERSION",
|
|
161
174
|
"Quoted converter is not configured"
|
|
162
|
-
),
|
|
163
|
-
|
|
175
|
+
), Ce(
|
|
176
|
+
n,
|
|
164
177
|
{
|
|
165
|
-
coinIn:
|
|
166
|
-
coinOut:
|
|
167
|
-
amountIn:
|
|
178
|
+
coinIn: n.input.coinType,
|
|
179
|
+
coinOut: n.expectedOutput.coinType,
|
|
180
|
+
amountIn: n.input.amount,
|
|
168
181
|
slippageBps: t.constraints.maxSlippageBps
|
|
169
182
|
},
|
|
170
|
-
|
|
183
|
+
n.converterId
|
|
171
184
|
);
|
|
172
|
-
const
|
|
185
|
+
const C = await z(t.address, c);
|
|
173
186
|
a.accountCap.kind === "create" ? i(
|
|
174
|
-
!
|
|
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
|
-
|
|
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(
|
|
182
|
-
const
|
|
183
|
-
for (const
|
|
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(
|
|
204
|
+
i(o.config[n] === a.market.config[n], "STALE_QUOTE", `Market ${n} changed`);
|
|
192
205
|
i(
|
|
193
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
220
|
+
const j = await y.readBorrowFeeBps(o, C?.accountOwner);
|
|
204
221
|
i(
|
|
205
|
-
new
|
|
222
|
+
new l(a.borrowAmount).times(j).div(1e4).integerValue().lte(a.borrowFee),
|
|
206
223
|
"STALE_QUOTE",
|
|
207
224
|
"Borrow fee increased"
|
|
208
|
-
), i(
|
|
209
|
-
const
|
|
210
|
-
collateralAmount: new
|
|
211
|
-
debtAmount: new
|
|
212
|
-
},
|
|
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
|
-
|
|
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(
|
|
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 &&
|
|
222
|
-
new
|
|
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
|
-
|
|
243
|
+
_e(L, _, o, a.direction !== "increase"),
|
|
227
244
|
"STALE_QUOTE",
|
|
228
245
|
"Updated market exceeds LTV"
|
|
229
246
|
), i(
|
|
230
|
-
new
|
|
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
|
|
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
|
|
255
|
+
new l(a.borrowAmount).plus(a.borrowFee).lte(Te(o)),
|
|
239
256
|
"CAPACITY_EXCEEDED",
|
|
240
257
|
"Borrow liquidity changed"
|
|
241
|
-
), await
|
|
242
|
-
const r =
|
|
243
|
-
|
|
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: [
|
|
262
|
+
arguments: [U, e.pure.string(W(c.id))]
|
|
246
263
|
}), e.moveCall({
|
|
247
264
|
target: `${r.package}::account::set_account_description`,
|
|
248
|
-
arguments: [
|
|
265
|
+
arguments: [U, e.pure.string(JSON.stringify(c))]
|
|
249
266
|
}));
|
|
250
|
-
const
|
|
251
|
-
const d =
|
|
252
|
-
return
|
|
253
|
-
},
|
|
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: [
|
|
256
|
-
arguments: [
|
|
257
|
-
}),
|
|
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(
|
|
264
|
-
e.pure.u8(
|
|
265
|
-
|
|
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
|
-
|
|
285
|
+
T
|
|
269
286
|
]
|
|
270
|
-
}),
|
|
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(
|
|
279
|
-
e.pure.u8(
|
|
280
|
-
|
|
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
|
-
|
|
300
|
+
T
|
|
284
301
|
]
|
|
285
302
|
}),
|
|
286
303
|
c.debtCoinType
|
|
287
304
|
);
|
|
288
|
-
let
|
|
305
|
+
let F, M;
|
|
289
306
|
if (a.flashAmount !== "0") {
|
|
290
|
-
const
|
|
291
|
-
target: `${
|
|
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(
|
|
295
|
-
e.object(
|
|
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
|
-
|
|
317
|
+
F = S(n[0], c.debtCoinType), M = n[1];
|
|
301
318
|
}
|
|
302
|
-
const
|
|
319
|
+
const ae = (n) => M ? S(
|
|
303
320
|
e.moveCall({
|
|
304
|
-
target: `${
|
|
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(
|
|
309
|
-
e.object(
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
) :
|
|
316
|
-
const
|
|
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
|
|
336
|
+
return K(e, d.coinOut, s.minOutput.amount), w.push(...d.change), d.coinOut;
|
|
320
337
|
};
|
|
321
338
|
if (a.direction === "repay")
|
|
322
|
-
|
|
339
|
+
w.push({
|
|
323
340
|
coinType: c.debtCoinType,
|
|
324
|
-
coin:
|
|
341
|
+
coin: te(H(c.debtCoinType, t.estimate.inputs[0].amount))
|
|
325
342
|
});
|
|
326
343
|
else if (a.direction === "add_collateral")
|
|
327
|
-
|
|
344
|
+
ee(H(c.collateralCoinType, t.estimate.inputs[0].amount));
|
|
328
345
|
else if (a.direction === "increase") {
|
|
329
|
-
let
|
|
346
|
+
let n = F;
|
|
330
347
|
if (t.estimate.inputs.length) {
|
|
331
|
-
const
|
|
332
|
-
|
|
348
|
+
const s = H(c.principalCoinType, t.estimate.inputs[0].amount);
|
|
349
|
+
n ? e.mergeCoins(n, [s]) : n = s;
|
|
333
350
|
}
|
|
334
|
-
if (i(
|
|
335
|
-
const
|
|
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(
|
|
343
|
-
e.pure.u8(
|
|
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
|
-
|
|
364
|
+
T,
|
|
348
365
|
e.object("0x5")
|
|
349
366
|
]
|
|
350
367
|
});
|
|
351
|
-
|
|
368
|
+
w.push({ coinType: c.debtCoinType, coin: ae(S(s, c.debtCoinType)) });
|
|
352
369
|
}
|
|
353
370
|
} else {
|
|
354
|
-
const
|
|
355
|
-
let
|
|
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: [
|
|
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
|
|
384
|
+
const V = e.moveCall({
|
|
368
385
|
target: `${r.package}::logic::user_collateral_balance`,
|
|
369
|
-
arguments: [e.object(r.storage), e.pure.u8(
|
|
370
|
-
}),
|
|
371
|
-
|
|
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: [
|
|
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 =
|
|
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(
|
|
386
|
-
e.pure.u8(
|
|
387
|
-
|
|
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
|
-
|
|
421
|
+
T,
|
|
391
422
|
e.object("0x5")
|
|
392
423
|
]
|
|
393
424
|
}),
|
|
394
425
|
c.collateralCoinType
|
|
395
|
-
),
|
|
396
|
-
|
|
397
|
-
const O = await
|
|
398
|
-
|
|
399
|
-
const
|
|
400
|
-
t.action.type === "remove_principal" &&
|
|
401
|
-
for (const h of t.estimate.minOutputs)
|
|
402
|
-
|
|
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
|
|
435
|
+
const X = e.moveCall({
|
|
405
436
|
target: `${r.package}::account::account_owner`,
|
|
406
|
-
arguments: [
|
|
437
|
+
arguments: [T]
|
|
407
438
|
});
|
|
408
439
|
if (t.action.type === "close")
|
|
409
|
-
for (const [
|
|
410
|
-
["user_collateral_balance",
|
|
411
|
-
["user_loan_balance",
|
|
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::${
|
|
415
|
-
arguments: [e.object(r.storage), e.pure.u8(
|
|
416
|
-
}),
|
|
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: [
|
|
454
|
+
arguments: [I]
|
|
424
455
|
});
|
|
425
456
|
}
|
|
426
457
|
if (t.constraints.maxLeverage !== void 0) {
|
|
427
|
-
const
|
|
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(
|
|
434
|
-
|
|
464
|
+
e.pure.u8(V),
|
|
465
|
+
X
|
|
435
466
|
]
|
|
436
|
-
}),
|
|
437
|
-
const
|
|
467
|
+
}), s = (h, V) => {
|
|
468
|
+
const Y = e.moveCall({
|
|
438
469
|
target: "0x1::u256::checked_mul",
|
|
439
|
-
arguments: [h, e.pure.u256(
|
|
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: [
|
|
475
|
+
arguments: [Y]
|
|
445
476
|
});
|
|
446
|
-
}, d = new
|
|
447
|
-
|
|
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 =
|
|
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: [
|
|
482
|
+
arguments: [I, O]
|
|
452
483
|
});
|
|
453
484
|
e.moveCall({
|
|
454
485
|
target: "0x1::option::destroy_some",
|
|
455
486
|
typeArguments: ["u256"],
|
|
456
|
-
arguments: [
|
|
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
|
-
|
|
496
|
+
X,
|
|
466
497
|
e.pure.u256(
|
|
467
|
-
new
|
|
498
|
+
new l(t.constraints.minHealthFactor).shiftedBy(27).integerValue(l.ROUND_CEIL).toFixed(0)
|
|
468
499
|
)
|
|
469
500
|
]
|
|
470
|
-
}), { coins:
|
|
501
|
+
}), { coins: w, accountCap: T, createdAccountCap: U };
|
|
471
502
|
}
|
|
472
|
-
return { getPosition:
|
|
503
|
+
return { getPosition: ie, quote: se, buildPTB: le };
|
|
473
504
|
}
|
|
474
505
|
export {
|
|
475
|
-
|
|
506
|
+
Le as createMultiplyClient
|
|
476
507
|
};
|
|
477
508
|
//# sourceMappingURL=client.js.map
|