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