@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/README.md +70 -46
- package/dist/amounts.d.ts +3 -3
- package/dist/amounts.js.map +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +406 -344
- package/dist/client.js.map +1 -1
- package/dist/converters.d.ts.map +1 -1
- package/dist/converters.js +221 -199
- package/dist/converters.js.map +1 -1
- package/dist/data-source.d.ts +2 -1
- package/dist/data-source.d.ts.map +1 -1
- package/dist/data-source.js +210 -196
- package/dist/data-source.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -18
- package/dist/index.js.map +1 -1
- package/dist/market-prices.d.ts +8 -0
- package/dist/market-prices.d.ts.map +1 -0
- package/dist/market-prices.js +45 -0
- package/dist/market-prices.js.map +1 -0
- package/dist/native-redemption.d.ts +13 -0
- package/dist/native-redemption.d.ts.map +1 -0
- package/dist/native-redemption.js +154 -0
- package/dist/native-redemption.js.map +1 -0
- package/dist/planner.d.ts +4 -1
- package/dist/planner.d.ts.map +1 -1
- package/dist/planner.js +437 -341
- package/dist/planner.js.map +1 -1
- package/dist/rate-hints.d.ts +8 -0
- package/dist/rate-hints.d.ts.map +1 -0
- package/dist/rate-hints.js +58 -0
- package/dist/rate-hints.js.map +1 -0
- package/dist/types.d.ts +41 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,87 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
new Set(
|
|
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
|
|
23
|
-
|
|
24
|
-
[...
|
|
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
|
|
29
|
-
|
|
30
|
-
!(
|
|
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
|
|
35
|
-
client:
|
|
36
|
-
services:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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:
|
|
42
|
-
}),
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
51
|
-
async function
|
|
52
|
-
const t =
|
|
53
|
-
return
|
|
54
|
-
g && await
|
|
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
|
|
60
|
-
|
|
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
|
-
),
|
|
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
|
-
),
|
|
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
|
-
),
|
|
74
|
+
), L(e.registryKey);
|
|
73
75
|
const l = {
|
|
74
76
|
...e,
|
|
75
77
|
registryKey: { ...e.registryKey },
|
|
76
|
-
principalCoinType:
|
|
77
|
-
collateralCoinType:
|
|
78
|
-
debtCoinType:
|
|
78
|
+
principalCoinType: z(e.principalCoinType),
|
|
79
|
+
collateralCoinType: z(e.collateralCoinType),
|
|
80
|
+
debtCoinType: z(e.debtCoinType)
|
|
79
81
|
};
|
|
80
|
-
return
|
|
81
|
-
(
|
|
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(
|
|
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
|
|
93
|
-
|
|
94
|
-
const l = await
|
|
95
|
-
return l.map((
|
|
96
|
-
const
|
|
97
|
-
return
|
|
98
|
-
|
|
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
|
-
),
|
|
102
|
-
!t.has(
|
|
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(
|
|
106
|
-
c.id ===
|
|
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
|
-
),
|
|
110
|
-
|
|
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
|
-
), { ...
|
|
114
|
-
}).filter((
|
|
115
|
+
), { ...r, id: o, accountOwner: v, registryKey: { ...r.registryKey } });
|
|
116
|
+
}).filter((r) => r !== null);
|
|
115
117
|
}
|
|
116
|
-
function
|
|
117
|
-
const t =
|
|
118
|
-
return e.find((g) =>
|
|
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
|
|
121
|
-
e =
|
|
122
|
-
const t = [], g = await
|
|
123
|
-
for (const
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
126
|
-
const c = await
|
|
127
|
-
|
|
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:
|
|
130
|
-
accountCap:
|
|
131
|
-
...
|
|
132
|
-
...
|
|
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
|
|
138
|
-
e =
|
|
139
|
-
const
|
|
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 =
|
|
142
|
-
|
|
143
|
-
|
|
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
|
|
149
|
-
|
|
150
|
-
|
|
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
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
const
|
|
159
|
-
if (
|
|
160
|
-
t.type !== "open" ||
|
|
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
|
-
),
|
|
164
|
-
t.type === "open" || !
|
|
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
|
|
169
|
-
new
|
|
170
|
-
),
|
|
171
|
-
|
|
172
|
-
|
|
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
|
|
178
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
(m) =>
|
|
185
|
-
i?.getRateHint ? () =>
|
|
186
|
-
|
|
187
|
-
const m = await
|
|
188
|
-
return m &&
|
|
189
|
-
m.converterId ===
|
|
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
|
-
|
|
196
|
-
|
|
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:
|
|
199
|
-
action:
|
|
200
|
-
constraints:
|
|
243
|
+
strategy: p,
|
|
244
|
+
action: F(t),
|
|
245
|
+
constraints: F(g),
|
|
201
246
|
expiresAt: Math.min(
|
|
202
247
|
Date.now() + 12e4,
|
|
203
|
-
|
|
248
|
+
o.priceEvidence?.expiresAt ?? 1 / 0,
|
|
249
|
+
...D.execution.conversions.map((m) => m.expiresAt)
|
|
204
250
|
),
|
|
205
|
-
estimate:
|
|
251
|
+
estimate: D.estimate,
|
|
206
252
|
execution: {
|
|
207
|
-
...
|
|
208
|
-
env:
|
|
209
|
-
capRegistry: { ...
|
|
210
|
-
accountCap:
|
|
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
|
|
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
|
|
217
|
-
const t = e.execution, g = e.strategy,
|
|
218
|
-
t.accountCap.kind === "create" ?
|
|
219
|
-
!
|
|
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
|
-
) : (
|
|
223
|
-
|
|
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" &&
|
|
227
|
-
const
|
|
228
|
-
|
|
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
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
|
244
|
-
if (
|
|
245
|
-
const i =
|
|
246
|
-
if (
|
|
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
|
-
|
|
252
|
-
t.snapshot.protocolCollateralAmount !== void 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
|
|
257
|
-
|
|
258
|
-
|
|
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" &&
|
|
264
|
-
|
|
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
|
|
270
|
-
|
|
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
|
|
273
|
-
) &&
|
|
274
|
-
new
|
|
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
|
|
280
|
-
|
|
281
|
-
new
|
|
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
|
-
),
|
|
344
|
+
), a(r.flash.feeBps === t.market.flash.feeBps, "STALE_QUOTE", "Flash fee changed");
|
|
285
345
|
const u = {
|
|
286
|
-
collateralAmount: new
|
|
287
|
-
debtAmount: new
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
|
|
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
|
-
),
|
|
294
|
-
Number.isSafeInteger(
|
|
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
|
|
358
|
+
const V = we(
|
|
299
359
|
e.constraints.maxLeverage,
|
|
300
360
|
e.action,
|
|
301
361
|
t.direction,
|
|
302
|
-
|
|
362
|
+
W(p, r).leverage
|
|
303
363
|
);
|
|
304
|
-
return
|
|
305
|
-
new
|
|
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
|
-
),
|
|
309
|
-
|
|
368
|
+
), a(
|
|
369
|
+
qe(u, p, r, t.direction !== "increase"),
|
|
310
370
|
"STALE_QUOTE",
|
|
311
371
|
"Updated market exceeds LTV"
|
|
312
|
-
),
|
|
313
|
-
new
|
|
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
|
-
),
|
|
317
|
-
new
|
|
376
|
+
), a(
|
|
377
|
+
new f(t.collateralAfter).minus(t.snapshot.collateralAmount).lte(ze(r)),
|
|
318
378
|
"CAPACITY_EXCEEDED",
|
|
319
379
|
"Supply capacity changed"
|
|
320
|
-
),
|
|
321
|
-
new
|
|
380
|
+
), a(
|
|
381
|
+
new f(t.borrowAmount).plus(t.borrowFee).lte(Ye(r)),
|
|
322
382
|
"CAPACITY_EXCEEDED",
|
|
323
383
|
"Borrow liquidity changed"
|
|
324
|
-
),
|
|
325
|
-
|
|
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:
|
|
388
|
+
), { fresh: r, now: p };
|
|
329
389
|
}
|
|
330
|
-
async function
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
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
|
|
338
|
-
|
|
339
|
-
|
|
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
|
-
),
|
|
343
|
-
|
|
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
|
-
),
|
|
347
|
-
|
|
406
|
+
), a(
|
|
407
|
+
n.execution.env === R,
|
|
348
408
|
"INVALID_QUOTE",
|
|
349
|
-
`Quote was made for NAVI env ${String(
|
|
350
|
-
),
|
|
351
|
-
Number.isSafeInteger(
|
|
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
|
-
),
|
|
355
|
-
|
|
414
|
+
), a(
|
|
415
|
+
I(n.address) !== I("0x0"),
|
|
356
416
|
"INVALID_ARGUMENT",
|
|
357
417
|
"Multiply owner cannot be zero"
|
|
358
|
-
), await
|
|
359
|
-
await
|
|
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
|
-
),
|
|
363
|
-
|
|
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
|
|
368
|
-
|
|
369
|
-
!
|
|
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
|
-
),
|
|
373
|
-
const c =
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
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
|
|
382
|
-
|
|
383
|
-
|
|
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
|
-
|
|
389
|
-
|
|
448
|
+
a(
|
|
449
|
+
K.has(s.converterId),
|
|
390
450
|
"INVALID_CONVERSION",
|
|
391
451
|
"Quoted converter is not configured"
|
|
392
|
-
),
|
|
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:
|
|
458
|
+
slippageBps: n.constraints.maxSlippageBps
|
|
399
459
|
},
|
|
400
460
|
s.converterId
|
|
401
461
|
);
|
|
402
|
-
const { fresh: u, now:
|
|
403
|
-
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
!
|
|
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(
|
|
410
|
-
const i = u.config,
|
|
411
|
-
c.accountCap.kind === "existing" && t.accountCap !== void 0 && !
|
|
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: [
|
|
475
|
+
arguments: [C]
|
|
416
476
|
}),
|
|
417
477
|
c.accountCap.accountOwner
|
|
418
|
-
),
|
|
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(
|
|
421
|
-
}),
|
|
422
|
-
const
|
|
423
|
-
const
|
|
424
|
-
return
|
|
425
|
-
},
|
|
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: [
|
|
487
|
+
typeArguments: [d],
|
|
428
488
|
arguments: [s]
|
|
429
|
-
}),
|
|
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: [
|
|
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
|
-
|
|
500
|
+
C
|
|
441
501
|
]
|
|
442
|
-
}),
|
|
502
|
+
}), U = (s) => S(
|
|
443
503
|
e.moveCall({
|
|
444
504
|
target: `${i.package}::incentive_v3::repay_with_account_cap`,
|
|
445
|
-
typeArguments: [
|
|
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
|
-
|
|
515
|
+
C
|
|
456
516
|
]
|
|
457
517
|
}),
|
|
458
|
-
|
|
518
|
+
o.debtCoinType
|
|
459
519
|
);
|
|
460
|
-
let
|
|
520
|
+
let k, M;
|
|
461
521
|
if (c.flashAmount !== "0") {
|
|
462
522
|
const s = e.moveCall({
|
|
463
|
-
target: `${u.flash.config.package}::lending::
|
|
464
|
-
typeArguments: [
|
|
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
|
-
|
|
533
|
+
k = S(s[0], o.debtCoinType), M = s[1];
|
|
473
534
|
}
|
|
474
|
-
const
|
|
535
|
+
const fe = (s) => M ? S(
|
|
475
536
|
e.moveCall({
|
|
476
|
-
target: `${u.flash.config.package}::lending::
|
|
477
|
-
typeArguments: [
|
|
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
|
-
|
|
483
|
-
|
|
543
|
+
M,
|
|
544
|
+
m(s, o.debtCoinType),
|
|
545
|
+
C
|
|
484
546
|
]
|
|
485
547
|
}),
|
|
486
|
-
|
|
487
|
-
) : s,
|
|
488
|
-
const
|
|
489
|
-
changeRecipient:
|
|
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
|
|
553
|
+
return le(e, A.coinOut, d.minOutput.amount), N.push(...A.change), A.coinOut;
|
|
492
554
|
};
|
|
493
555
|
if (c.direction === "repay")
|
|
494
|
-
|
|
495
|
-
coinType:
|
|
496
|
-
coin:
|
|
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
|
-
|
|
561
|
+
h(D(o.collateralCoinType, n.estimate.inputs[0].amount));
|
|
500
562
|
else if (c.direction === "increase") {
|
|
501
|
-
let s =
|
|
563
|
+
let s = k;
|
|
502
564
|
if (c.funding.kind !== "none") {
|
|
503
|
-
const
|
|
504
|
-
c.funding.kind === "collateral" ?
|
|
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
|
|
507
|
-
|
|
508
|
-
if (
|
|
509
|
-
const
|
|
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: [
|
|
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
|
-
|
|
583
|
+
C,
|
|
522
584
|
e.object("0x5")
|
|
523
585
|
]
|
|
524
586
|
});
|
|
525
|
-
|
|
587
|
+
N.push({ coinType: o.debtCoinType, coin: fe(S(d, o.debtCoinType)) });
|
|
526
588
|
}
|
|
527
589
|
} else {
|
|
528
|
-
const s =
|
|
529
|
-
let
|
|
590
|
+
const s = k ? U(k) : void 0;
|
|
591
|
+
let d = e.pure.u64(
|
|
530
592
|
c.withdrawAmount
|
|
531
593
|
);
|
|
532
|
-
if (
|
|
533
|
-
const
|
|
594
|
+
if (n.action.type === "close") {
|
|
595
|
+
const b = e.moveCall({
|
|
534
596
|
target: `${i.package}::account::account_owner`,
|
|
535
|
-
arguments: [
|
|
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),
|
|
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),
|
|
605
|
+
arguments: [e.object(i.storage), e.pure.u8(u.collateral.id), b]
|
|
544
606
|
});
|
|
545
|
-
if (
|
|
546
|
-
for (const [
|
|
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
|
|
612
|
+
const Ue = e.moveCall({
|
|
551
613
|
target: "0x1::u256::checked_sub",
|
|
552
|
-
arguments: [
|
|
614
|
+
arguments: [ce, De]
|
|
553
615
|
});
|
|
554
616
|
e.moveCall({
|
|
555
617
|
target: "0x1::option::destroy_some",
|
|
556
618
|
typeArguments: ["u256"],
|
|
557
|
-
arguments: [
|
|
619
|
+
arguments: [Ue]
|
|
558
620
|
});
|
|
559
621
|
}
|
|
560
|
-
const
|
|
561
|
-
if (
|
|
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: [
|
|
565
|
-
}),
|
|
626
|
+
arguments: [re]
|
|
627
|
+
}), j(u.collateral) !== 9 && (d = e.moveCall({
|
|
566
628
|
target: `${i.package}::pool::unnormal_amount`,
|
|
567
|
-
typeArguments: [
|
|
568
|
-
arguments: [e.object(u.collateral.contract.pool),
|
|
569
|
-
}),
|
|
570
|
-
const
|
|
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: [
|
|
634
|
+
arguments: [d, e.pure.u64("1")]
|
|
573
635
|
});
|
|
574
|
-
|
|
636
|
+
d = e.moveCall({
|
|
575
637
|
target: "0x1::option::destroy_some",
|
|
576
638
|
typeArguments: ["u64"],
|
|
577
|
-
arguments: [
|
|
639
|
+
arguments: [ce]
|
|
578
640
|
});
|
|
579
641
|
}
|
|
580
642
|
}
|
|
581
|
-
const
|
|
643
|
+
const A = S(
|
|
582
644
|
e.moveCall({
|
|
583
645
|
target: `${i.package}::incentive_v3::withdraw_with_account_cap_v2`,
|
|
584
|
-
typeArguments: [
|
|
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
|
-
|
|
653
|
+
d,
|
|
592
654
|
e.object(i.incentiveV2),
|
|
593
655
|
e.object(i.incentiveV3),
|
|
594
|
-
|
|
656
|
+
C,
|
|
595
657
|
e.object("0x5")
|
|
596
658
|
]
|
|
597
659
|
}),
|
|
598
|
-
|
|
660
|
+
o.collateralCoinType
|
|
599
661
|
);
|
|
600
|
-
let
|
|
601
|
-
for (const
|
|
602
|
-
const E = e.splitCoins(
|
|
603
|
-
|
|
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 && (
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
for (const
|
|
609
|
-
if (
|
|
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 =
|
|
612
|
-
|
|
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
|
-
|
|
676
|
+
N.push({ coinType: o.collateralCoinType, coin: A }), T && N.push({ coinType: o.debtCoinType, coin: T });
|
|
615
677
|
}
|
|
616
|
-
const
|
|
678
|
+
const ie = e.moveCall({
|
|
617
679
|
target: `${i.package}::account::account_owner`,
|
|
618
|
-
arguments: [
|
|
680
|
+
arguments: [C]
|
|
619
681
|
});
|
|
620
|
-
if (
|
|
621
|
-
for (const [s,
|
|
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
|
|
687
|
+
const A = e.moveCall({
|
|
626
688
|
target: `${i.package}::logic::${s}`,
|
|
627
|
-
arguments: [e.object(i.storage), e.pure.u8(
|
|
628
|
-
}),
|
|
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"),
|
|
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: [
|
|
697
|
+
arguments: [T]
|
|
636
698
|
});
|
|
637
699
|
}
|
|
638
|
-
const
|
|
639
|
-
|
|
640
|
-
|
|
700
|
+
const ve = we(
|
|
701
|
+
n.constraints.maxLeverage,
|
|
702
|
+
n.action,
|
|
641
703
|
c.direction,
|
|
642
|
-
|
|
704
|
+
W(O, u).leverage
|
|
643
705
|
);
|
|
644
|
-
if (
|
|
645
|
-
const s = (
|
|
646
|
-
target: `${i.package}::logic::${
|
|
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
|
-
|
|
714
|
+
ie
|
|
653
715
|
]
|
|
654
|
-
}),
|
|
655
|
-
const
|
|
716
|
+
}), d = (b, E) => {
|
|
717
|
+
const re = e.moveCall({
|
|
656
718
|
target: "0x1::u256::checked_mul",
|
|
657
|
-
arguments: [
|
|
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: [
|
|
724
|
+
arguments: [re]
|
|
663
725
|
});
|
|
664
|
-
},
|
|
726
|
+
}, A = ve.shiftedBy(12).integerValue(f.ROUND_FLOOR), T = d(
|
|
665
727
|
s("user_collateral_value", u.collateral.id),
|
|
666
|
-
|
|
667
|
-
),
|
|
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: [
|
|
731
|
+
arguments: [T, Q]
|
|
670
732
|
});
|
|
671
733
|
e.moveCall({
|
|
672
734
|
target: "0x1::option::destroy_some",
|
|
673
735
|
typeArguments: ["u256"],
|
|
674
|
-
arguments: [
|
|
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
|
-
|
|
745
|
+
ie,
|
|
684
746
|
e.pure.u256(
|
|
685
|
-
new
|
|
747
|
+
new f(n.constraints.minHealthFactor).shiftedBy(27).integerValue(f.ROUND_CEIL).toFixed(0)
|
|
686
748
|
)
|
|
687
749
|
]
|
|
688
|
-
}), { coins:
|
|
750
|
+
}), { coins: N, accountCap: C, createdAccountCap: w };
|
|
689
751
|
}
|
|
690
|
-
function
|
|
691
|
-
return
|
|
752
|
+
function ke(e, l, t) {
|
|
753
|
+
return ye(e, l, t, !1);
|
|
692
754
|
}
|
|
693
|
-
function
|
|
694
|
-
return
|
|
755
|
+
function Re(e, l, t) {
|
|
756
|
+
return ye(e, l, { coins: t }, !0);
|
|
695
757
|
}
|
|
696
|
-
return { getPosition:
|
|
758
|
+
return { getPosition: Oe, quote: Ne, buildPTB: ke, buildPTBForSimulation: Re };
|
|
697
759
|
}
|
|
698
760
|
export {
|
|
699
|
-
|
|
761
|
+
lt as createMultiplyClient
|
|
700
762
|
};
|
|
701
763
|
//# sourceMappingURL=client.js.map
|