@naviprotocol/lending 1.4.0-emode.1 → 1.4.0-emode.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,541 +1,552 @@
1
- import { Transaction as B } from "@mysten/sui/transactions";
2
- import { bcs as l, toHex as J, fromHex as Q } from "@mysten/bcs";
3
- import { SuiClient as X, getFullnodeUrl as Z } from "@mysten/sui/client";
4
- import x from "lodash.camelcase";
5
- import { normalizeStructTag as ee } from "@mysten/sui/utils";
6
- import { SuiPriceServiceConnection as q, SuiPythClient as te } from "@pythnetwork/pyth-sui-js";
7
- import y from "bignumber.js";
8
- import { bcs as w } from "@mysten/sui/bcs";
9
- const R = l.bytes(32).transform({
1
+ import { Transaction as U } from "@mysten/sui/transactions";
2
+ import { bcs as p, toHex as ne, fromHex as re } from "@mysten/bcs";
3
+ import { SuiClient as ae, getFullnodeUrl as ce } from "@mysten/sui/client";
4
+ import ie from "lodash.camelcase";
5
+ import { normalizeStructTag as se } from "@mysten/sui/utils";
6
+ import { SuiPriceServiceConnection as z, SuiPythClient as ue } from "@pythnetwork/pyth-sui-js";
7
+ import l from "bignumber.js";
8
+ import { bcs as _ } from "@mysten/sui/bcs";
9
+ const L = p.bytes(32).transform({
10
10
  // To change the input type, you need to provide a type definition for the input
11
- input: (e) => Q(e),
12
- output: (e) => J(e)
13
- }), Ee = l.struct("IncentiveAPYInfo", {
11
+ input: (e) => re(e),
12
+ output: (e) => ne(e)
13
+ }), Le = p.struct("IncentiveAPYInfo", {
14
14
  /** Asset identifier */
15
- asset_id: l.u8(),
15
+ asset_id: p.u8(),
16
16
  /** Annual Percentage Yield as a 256-bit integer */
17
- apy: l.u256(),
17
+ apy: p.u256(),
18
18
  /** List of supported coin types for this incentive */
19
- coin_types: l.vector(l.string())
20
- }), oe = l.struct("IncentivePoolInfo", {
19
+ coin_types: p.vector(p.string())
20
+ }), le = p.struct("IncentivePoolInfo", {
21
21
  /** Unique pool identifier */
22
- pool_id: R,
22
+ pool_id: L,
23
23
  /** Address holding the incentive funds */
24
- funds: R,
24
+ funds: L,
25
25
  /** Current phase of the incentive program */
26
- phase: l.u64(),
26
+ phase: p.u64(),
27
27
  /** Timestamp when the incentive started */
28
- start_at: l.u64(),
28
+ start_at: p.u64(),
29
29
  /** Timestamp when the incentive ends */
30
- end_at: l.u64(),
30
+ end_at: p.u64(),
31
31
  /** Timestamp when the incentive was closed */
32
- closed_at: l.u64(),
32
+ closed_at: p.u64(),
33
33
  /** Total supply of incentive tokens */
34
- total_supply: l.u64(),
34
+ total_supply: p.u64(),
35
35
  /** Asset identifier for the incentive */
36
- asset_id: l.u8(),
36
+ asset_id: p.u8(),
37
37
  /** Option type for the incentive */
38
- option: l.u8(),
38
+ option: p.u8(),
39
39
  /** Factor used in incentive calculations */
40
- factor: l.u256(),
40
+ factor: p.u256(),
41
41
  /** Amount of incentives already distributed */
42
- distributed: l.u64(),
42
+ distributed: p.u64(),
43
43
  /** Amount of incentives currently available */
44
- available: l.u256(),
44
+ available: p.u256(),
45
45
  /** Total amount of incentives */
46
- total: l.u256()
47
- }), Ve = l.struct("IncentivePoolInfoByPhase", {
46
+ total: p.u256()
47
+ }), qe = p.struct("IncentivePoolInfoByPhase", {
48
48
  /** Phase number */
49
- phase: l.u64(),
49
+ phase: p.u64(),
50
50
  /** List of incentive pools in this phase */
51
- pools: l.vector(oe)
52
- }), Ne = l.struct("OracleInfo", {
51
+ pools: p.vector(le)
52
+ }), We = p.struct("OracleInfo", {
53
53
  /** Oracle identifier */
54
- oracle_id: l.u8(),
54
+ oracle_id: p.u8(),
55
55
  /** Current price as a 256-bit integer */
56
- price: l.u256(),
56
+ price: p.u256(),
57
57
  /** Number of decimal places for the price */
58
- decimals: l.u8(),
58
+ decimals: p.u8(),
59
59
  /** Whether the oracle data is valid */
60
- valid: l.bool()
61
- }), De = l.struct("FlashLoanAssetConfig", {
60
+ valid: p.bool()
61
+ }), He = p.struct("FlashLoanAssetConfig", {
62
62
  /** Unique identifier for the flash loan asset */
63
- id: l.string(),
63
+ id: p.string(),
64
64
  /** Asset identifier */
65
- asset_id: l.u8(),
65
+ asset_id: p.u8(),
66
66
  /** Coin type for the asset */
67
- coin_type: l.string(),
67
+ coin_type: p.string(),
68
68
  /** Pool identifier for the flash loan */
69
- pool_id: l.string(),
69
+ pool_id: p.string(),
70
70
  /** Rate paid to suppliers for flash loans */
71
- rate_to_supplier: l.u64(),
71
+ rate_to_supplier: p.u64(),
72
72
  /** Rate paid to treasury for flash loans */
73
- rate_to_treasury: l.u64(),
73
+ rate_to_treasury: p.u64(),
74
74
  /** Maximum flash loan amount */
75
- max: l.u64(),
75
+ max: p.u64(),
76
76
  /** Minimum flash loan amount */
77
- min: l.u64()
78
- }), Re = l.struct("ReserveDataInfo", {
77
+ min: p.u64()
78
+ }), Ge = p.struct("ReserveDataInfo", {
79
79
  /** Reserve identifier */
80
- id: l.u8(),
80
+ id: p.u8(),
81
81
  /** Oracle identifier for price feeds */
82
- oracle_id: l.u8(),
82
+ oracle_id: p.u8(),
83
83
  /** Coin type for the reserve */
84
- coin_type: l.string(),
84
+ coin_type: p.string(),
85
85
  /** Maximum supply capacity */
86
- supply_cap: l.u256(),
86
+ supply_cap: p.u256(),
87
87
  /** Maximum borrow capacity */
88
- borrow_cap: l.u256(),
88
+ borrow_cap: p.u256(),
89
89
  /** Current supply interest rate */
90
- supply_rate: l.u256(),
90
+ supply_rate: p.u256(),
91
91
  /** Current borrow interest rate */
92
- borrow_rate: l.u256(),
92
+ borrow_rate: p.u256(),
93
93
  /** Current supply index for interest calculation */
94
- supply_index: l.u256(),
94
+ supply_index: p.u256(),
95
95
  /** Current borrow index for interest calculation */
96
- borrow_index: l.u256(),
96
+ borrow_index: p.u256(),
97
97
  /** Total amount supplied to the reserve */
98
- total_supply: l.u256(),
98
+ total_supply: p.u256(),
99
99
  /** Total amount borrowed from the reserve */
100
- total_borrow: l.u256(),
100
+ total_borrow: p.u256(),
101
101
  /** Timestamp of last update */
102
- last_update_at: l.u64(),
102
+ last_update_at: p.u64(),
103
103
  /** Loan-to-Value ratio for collateral */
104
- ltv: l.u256(),
104
+ ltv: p.u256(),
105
105
  /** Treasury factor for fee calculations */
106
- treasury_factor: l.u256(),
106
+ treasury_factor: p.u256(),
107
107
  /** Current treasury balance */
108
- treasury_balance: l.u256(),
108
+ treasury_balance: p.u256(),
109
109
  /** Base interest rate */
110
- base_rate: l.u256(),
110
+ base_rate: p.u256(),
111
111
  /** Interest rate multiplier */
112
- multiplier: l.u256(),
112
+ multiplier: p.u256(),
113
113
  /** Jump rate multiplier for high utilization */
114
- jump_rate_multiplier: l.u256(),
114
+ jump_rate_multiplier: p.u256(),
115
115
  /** Reserve factor for protocol fees */
116
- reserve_factor: l.u256(),
116
+ reserve_factor: p.u256(),
117
117
  /** Optimal utilization rate */
118
- optimal_utilization: l.u256(),
118
+ optimal_utilization: p.u256(),
119
119
  /** Liquidation ratio threshold */
120
- liquidation_ratio: l.u256(),
120
+ liquidation_ratio: p.u256(),
121
121
  /** Liquidation bonus for liquidators */
122
- liquidation_bonus: l.u256(),
122
+ liquidation_bonus: p.u256(),
123
123
  /** Liquidation threshold */
124
- liquidation_threshold: l.u256()
125
- }), re = l.struct("UserStateInfo", {
124
+ liquidation_threshold: p.u256()
125
+ }), pe = p.struct("UserStateInfo", {
126
126
  /** Asset identifier */
127
- asset_id: l.u8(),
127
+ asset_id: p.u8(),
128
128
  /** User's current borrow balance */
129
- borrow_balance: l.u256(),
129
+ borrow_balance: p.u256(),
130
130
  /** User's current supply balance */
131
- supply_balance: l.u256()
132
- }), ne = "1.4.0-emode.1", k = {
133
- version: ne
134
- }, ae = k.version, ce = () => {
131
+ supply_balance: p.u256()
132
+ }), de = "1.4.0-emode.11", S = {
133
+ version: de
134
+ }, me = S.version, ge = () => {
135
135
  if (typeof process < "u" && process.versions && process.versions.node)
136
136
  try {
137
- const e = require("os"), t = process.version, o = e.type(), r = e.arch();
138
- return `Node.js ${t.startsWith("v") ? t.substring(1) : t}; ${o}/${r}`;
137
+ const e = require("os"), o = process.version, n = e.type(), r = e.arch();
138
+ return `Node.js ${o.startsWith("v") ? o.substring(1) : o}; ${n}/${r}`;
139
139
  } catch {
140
140
  return `Node.js ${process.version}; OS/Unknown (Error)`;
141
141
  }
142
142
  return "Node/Unknown";
143
- }, se = () => {
143
+ }, fe = () => {
144
144
  let e = "";
145
- return typeof process < "u" && process.versions && process.versions.node && (e = `lending/${ae} (${ce()})`), e;
146
- }, U = se(), T = new X({
147
- url: Z("mainnet")
145
+ return typeof process < "u" && process.versions && process.versions.node && (e = `lending/${me} (${ge()})`), e;
146
+ }, q = fe(), $ = new ae({
147
+ url: ce("mainnet")
148
148
  });
149
- function H(e) {
150
- const t = [];
151
- return e.forEach((o, r) => {
152
- const n = r === e.length - 1;
153
- if (typeof o == "object" && o !== null && n) {
154
- const { client: a, disableCache: c, cacheTime: s, ...u } = o;
155
- t.push(u);
149
+ function K(e) {
150
+ const o = [];
151
+ return e.forEach((n, r) => {
152
+ const t = r === e.length - 1;
153
+ if (typeof n == "object" && n !== null && t) {
154
+ const { client: a, disableCache: i, cacheTime: c, ...u } = n;
155
+ o.push(u);
156
156
  } else
157
- t.push(o);
158
- }), JSON.stringify(t);
157
+ o.push(n);
158
+ }), JSON.stringify(o);
159
159
  }
160
- function v(e) {
161
- const t = {};
162
- return (...o) => {
163
- const r = H(o);
164
- return t[r] || (t[r] = e(...o).finally(() => {
165
- delete t[r];
166
- })), t[r];
160
+ function B(e) {
161
+ const o = {};
162
+ return (...n) => {
163
+ const r = K(n);
164
+ return o[r] || (o[r] = e(...n).finally(() => {
165
+ delete o[r];
166
+ })), o[r];
167
167
  };
168
168
  }
169
- function j(e) {
170
- let t = {};
171
- return (...o) => {
172
- const r = o[o.length - 1], n = H(o), a = t[n];
173
- return !r?.disableCache && typeof a?.data < "u" && (typeof r?.cacheTime > "u" || r.cacheTime > Date.now() - a.cacheAt) ? a.data : e(...o).then((c) => (t[n] = {
174
- data: c,
169
+ function I(e) {
170
+ let o = {};
171
+ return (...n) => {
172
+ const r = n[n.length - 1], t = K(n), a = o[t];
173
+ return !r?.disableCache && typeof a?.data < "u" && (typeof r?.cacheTime > "u" || r.cacheTime > Date.now() - a.cacheAt) ? a.data : e(...n).then((i) => (o[t] = {
174
+ data: i,
175
175
  cacheAt: Date.now()
176
- }, c));
176
+ }, i));
177
177
  };
178
178
  }
179
- function V(e) {
180
- return Array.isArray(e) ? e.map((t) => V(t)) : e != null && typeof e == "object" ? Object.keys(e).reduce(
181
- (t, o) => ({
182
- ...t,
183
- [x(o)]: V(e[o])
179
+ function R(e) {
180
+ return Array.isArray(e) ? e.map((o) => R(o)) : e != null && typeof e == "object" ? Object.keys(e).reduce(
181
+ (o, n) => ({
182
+ ...o,
183
+ [ie(n)]: R(e[n])
184
184
  }),
185
185
  {}
186
186
  ) : e;
187
187
  }
188
- function p(e, t) {
189
- return typeof e == "object" ? e : t(e);
188
+ function f(e, o) {
189
+ return typeof e == "object" ? e : o(e);
190
190
  }
191
- function ie(e, t) {
192
- return typeof t == "string" ? e.object(t) : typeof t == "object" && t.$kind ? t : e.object(t.contract.pool);
191
+ function ye(e, o) {
192
+ return typeof o == "string" ? e.object(o) : typeof o == "object" && o.$kind ? o : e.object(o.contract.pool);
193
193
  }
194
- function M(e, t, o) {
194
+ function O(e, o, n) {
195
195
  if (e.results && e.results.length > 0) {
196
196
  if (e.results[0].returnValues && e.results[0].returnValues.length > 0)
197
- return e.results[0].returnValues.map((r, n) => (t[n] || t[0]).parse(Uint8Array.from(r[0])));
197
+ return e.results[0].returnValues.map((r, t) => (o[t] || o[0]).parse(Uint8Array.from(r[0])));
198
198
  } else if (e.error)
199
199
  return console.log(`Get an error, msg: ${e.error}`), [];
200
200
  return [];
201
201
  }
202
- function g(e) {
203
- return ee(e);
202
+ function b(e) {
203
+ return se(e);
204
204
  }
205
- function G(e) {
206
- const t = (e || 0) / Math.pow(10, 27);
207
- return t > Math.pow(10, 5) ? 1 / 0 : t;
205
+ function Y(e) {
206
+ const o = (e || 0) / Math.pow(10, 27);
207
+ return o > Math.pow(10, 5) ? 1 / 0 : o;
208
208
  }
209
- new q("https://hermes.pyth.network", {
209
+ new z("https://hermes.pyth.network", {
210
210
  timeout: 2e4
211
211
  });
212
- const ue = 27, F = (e, t) => {
213
- if (!Number(e) || !Number(t)) return new y(0);
214
- const o = new y(1).shiftedBy(1 * ue), r = o.multipliedBy(new y(0.5));
215
- return new y(e).multipliedBy(new y(t)).plus(r).dividedBy(o).integerValue(y.ROUND_DOWN);
216
- }, $ = U ? {
217
- "User-Agent": U
212
+ const ve = 27, W = (e, o) => {
213
+ if (!Number(e) || !Number(o)) return new l(0);
214
+ const n = new l(1).shiftedBy(1 * ve), r = n.multipliedBy(new l(0.5));
215
+ return new l(e).multipliedBy(new l(o)).plus(r).dividedBy(n).integerValue(l.ROUND_DOWN);
216
+ }, V = q ? {
217
+ "User-Agent": q
218
218
  } : {};
219
- function le(e) {
219
+ function J(e) {
220
220
  return e.reduce(
221
- (t, o) => (t[o.id] = o, t),
221
+ (o, n) => (o[n.uniqueId] = n, o),
222
222
  {}
223
223
  );
224
224
  }
225
- function S(e) {
225
+ function H(e) {
226
226
  return e.reduce(
227
- (t, o) => (t[o.emodeId] = o, t),
227
+ (o, n) => (o[n.uniqueId] = n, o),
228
228
  {}
229
229
  );
230
230
  }
231
- function O(e, t) {
232
- const o = e.emodes.find((r) => r.emodeId === t.emodeId);
233
- if (!o)
231
+ function G(e, o) {
232
+ const n = e.emodes.find((r) => r.emodeId === o.emodeId);
233
+ if (!n)
234
234
  throw new Error("EMode not found in pool");
235
235
  return {
236
236
  ...e,
237
- emode: o,
237
+ emode: n,
238
238
  isEMode: !0
239
239
  };
240
240
  }
241
- function pe(e) {
242
- const [t, o] = e.split("-");
243
- return !t || !o ? null : {
244
- marketKey: t,
245
- poolId: parseInt(o)
241
+ function we(e) {
242
+ const [o, n] = e.split("-");
243
+ return !o || !n ? null : {
244
+ marketKey: o,
245
+ poolId: parseInt(n)
246
246
  };
247
247
  }
248
- var P = /* @__PURE__ */ ((e) => (e[e.Supply = 1] = "Supply", e[e.Withdraw = 2] = "Withdraw", e[e.Borrow = 3] = "Borrow", e[e.Repay = 4] = "Repay", e))(P || {});
249
- const E = j(
250
- v(
248
+ function P() {
249
+ return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
250
+ }
251
+ var D = /* @__PURE__ */ ((e) => (e[e.Supply = 1] = "Supply", e[e.Withdraw = 2] = "Withdraw", e[e.Borrow = 3] = "Borrow", e[e.Repay = 4] = "Repay", e))(D || {});
252
+ const N = I(
253
+ B(
251
254
  async (e) => {
252
- const t = N(e?.market || I), o = `https://open-api.naviprotocol.io/api/navi/pools?env=${e?.env || "prod"}&sdk=${k.version}&market=${t?.key || I}`, r = await fetch(o, { headers: $ }).then((n) => n.json());
253
- return r.data.forEach((n) => {
254
- const a = r.meta.emodes.filter(
255
- (c) => !!c.assets.find((s) => s.assetId === n.id) && c.isActive
256
- );
257
- n.emodes = a, n.market = r.meta.market;
255
+ const o = (e?.markets || [E.main]).map((t) => j(t)), n = `https://open-api.naviprotocol.io/api/navi/pools?env=${e?.env || "prod"}&sdk=${S.version}&market=${o.map(
256
+ (t) => t.key
257
+ )}`, r = await fetch(n, { headers: V }).then((t) => t.json());
258
+ return r.data.forEach((t) => {
259
+ const i = r.meta.emodes.filter((h) => {
260
+ const C = j(h.marketId);
261
+ return t.market === C.key && h.isActive;
262
+ }).filter((h) => !!h.assets.find((C) => C.assetId === t.id));
263
+ t.emodes = i;
264
+ const c = l(t.totalSupplyAmount).div(Math.pow(10, 9)).decimalPlaces(t.token.decimals, l.ROUND_DOWN).toString(), u = l(t.borrowedAmount).shiftedBy(-9).decimalPlaces(t.token.decimals, l.ROUND_DOWN).toString(), s = l(c).multipliedBy(t.oracle.price).toString(), g = l(u).multipliedBy(t.oracle.price).toString(), m = l(t.supplyCapCeiling).shiftedBy(-27).decimalPlaces(t.token.decimals, l.ROUND_DOWN).toString(), d = l.max(
265
+ l(t.borrowedAmount),
266
+ l(t.validBorrowAmount)
267
+ ).shiftedBy(-9).decimalPlaces(t.token.decimals, l.ROUND_DOWN).toString(), v = l(m).multipliedBy(t.oracle.price).toString(), y = l(d).multipliedBy(t.oracle.price).toString();
268
+ t.poolSupplyAmount = c, t.poolBorrowAmount = u, t.poolSupplyValue = s, t.poolBorrowValue = g, t.poolSupplyCapAmount = m, t.poolBorrowCapAmount = d, t.poolSupplyCapValue = v, t.poolBorrowCapValue = y;
258
269
  }), r.data;
259
270
  }
260
271
  )
261
272
  );
262
- async function C(e, t) {
263
- let o = t?.market;
273
+ async function A(e, o) {
274
+ let n = o?.market;
264
275
  if (typeof e == "string") {
265
- const a = pe(e);
266
- a && (o = a.marketKey, e = a.poolId);
276
+ const a = we(e);
277
+ a && (n = a.marketKey, e = a.poolId);
267
278
  }
268
- const r = await E({
269
- ...t,
270
- market: o,
271
- cacheTime: f
279
+ const r = await N({
280
+ ...o,
281
+ markets: [n || M],
282
+ cacheTime: w
272
283
  });
273
284
  if (typeof e == "object")
274
285
  return e;
275
- const n = r.find((a) => typeof e == "string" ? g(a.suiCoinType) === g(e) : typeof e == "number" ? a.id === e : !1);
276
- if (!n)
286
+ const t = r.find((a) => typeof e == "string" ? b(a.suiCoinType) === b(e) : typeof e == "number" ? a.id === e : !1);
287
+ if (!t)
277
288
  throw new Error("Pool not found");
278
- return n.isDeprecated && console.log(`The lending pool for coinType ${n.suiCoinType} is going to be deprecated.`), n;
289
+ return t.isDeprecated && console.log(`The lending pool for coinType ${t.suiCoinType} is going to be deprecated.`), t;
279
290
  }
280
- const Ue = j(
281
- v(async (e) => {
282
- const t = `https://open-api.naviprotocol.io/api/navi/stats?sdk=${k.version}`;
283
- return (await fetch(t, { headers: $ }).then((r) => r.json())).data;
291
+ const ze = I(
292
+ B(async (e) => {
293
+ const o = `https://open-api.naviprotocol.io/api/navi/stats?sdk=${S.version}`;
294
+ return (await fetch(o, { headers: V }).then((r) => r.json())).data;
284
295
  })
285
- ), Fe = j(
286
- v(
296
+ ), Ke = I(
297
+ B(
287
298
  async (e) => {
288
- const t = `https://open-api.naviprotocol.io/api/navi/fee?sdk=${k.version}`;
289
- return await fetch(t, { headers: $ }).then((r) => r.json());
299
+ const o = `https://open-api.naviprotocol.io/api/navi/fee?sdk=${S.version}`;
300
+ return await fetch(o, { headers: V }).then((r) => r.json());
290
301
  }
291
302
  )
292
303
  );
293
- async function de(e, t, o, r) {
294
- const n = await h({
304
+ async function he(e, o, n, r) {
305
+ const t = await k({
295
306
  ...r,
296
- cacheTime: f
297
- }), a = await C(t, r);
307
+ cacheTime: w
308
+ }), a = await A(o, r);
298
309
  if (a?.deprecatedAt && Date.now() > a.deprecatedAt)
299
310
  throw new Error(`The lending pool for coinType ${a.suiCoinType} has been deprecated.`);
300
- const c = typeof o == "object" && o.$kind === "GasCoin";
301
- if (g(a.suiCoinType) === g("0x2::sui::SUI") && c) {
311
+ const i = typeof n == "object" && n.$kind === "GasCoin";
312
+ if (b(a.suiCoinType) === b("0x2::sui::SUI") && i) {
302
313
  if (!r?.amount)
303
314
  throw new Error("Amount is required for sui coin");
304
- o = e.splitCoins(o, [r.amount]);
315
+ n = e.splitCoins(n, [r.amount]);
305
316
  }
306
- let s;
307
- return typeof r?.amount < "u" ? s = p(r.amount, e.pure.u64) : s = e.moveCall({
317
+ let c;
318
+ return typeof r?.amount < "u" ? c = f(r.amount, e.pure.u64) : c = e.moveCall({
308
319
  target: "0x2::coin::value",
309
- arguments: [p(o, e.object)],
320
+ arguments: [f(n, e.object)],
310
321
  typeArguments: [a.suiCoinType]
311
322
  }), r?.accountCap ? e.moveCall({
312
- target: `${n.package}::incentive_v3::deposit_with_account_cap`,
323
+ target: `${t.package}::incentive_v3::deposit_with_account_cap`,
313
324
  arguments: [
314
325
  e.object("0x06"),
315
- e.object(n.storage),
326
+ e.object(t.storage),
316
327
  e.object(a.contract.pool),
317
328
  e.pure.u8(a.id),
318
- p(o, e.object),
319
- e.object(n.incentiveV2),
320
- e.object(n.incentiveV3),
321
- p(r.accountCap, e.object)
329
+ f(n, e.object),
330
+ e.object(t.incentiveV2),
331
+ e.object(t.incentiveV3),
332
+ f(r.accountCap, e.object)
322
333
  ],
323
334
  typeArguments: [a.suiCoinType]
324
335
  }) : e.moveCall({
325
- target: `${n.package}::incentive_v3::entry_deposit`,
336
+ target: `${t.package}::incentive_v3::entry_deposit`,
326
337
  arguments: [
327
338
  e.object("0x06"),
328
- e.object(n.storage),
339
+ e.object(t.storage),
329
340
  e.object(a.contract.pool),
330
341
  e.pure.u8(a.id),
331
- p(o, e.object),
332
- s,
333
- e.object(n.incentiveV2),
334
- e.object(n.incentiveV3)
342
+ f(n, e.object),
343
+ c,
344
+ e.object(t.incentiveV2),
345
+ e.object(t.incentiveV3)
335
346
  ],
336
347
  typeArguments: [a.suiCoinType]
337
- }), n.version === 2 && a.id === 0 && e.moveCall({
338
- target: `${n.package}::pool::refresh_stake`,
348
+ }), t.version === 2 && a.id === 0 && (!r?.env || r?.env === "prod") && e.moveCall({
349
+ target: `${t.package}::pool::refresh_stake`,
339
350
  arguments: [e.object(a.contract.pool), e.object("0x05")]
340
351
  }), e;
341
352
  }
342
- async function Se(e, t, o, r) {
343
- const n = await h({
353
+ async function Ye(e, o, n, r) {
354
+ const t = await k({
344
355
  ...r,
345
- cacheTime: f
346
- }), a = await C(t, r), c = p(o, e.pure.u64);
347
- let s;
348
- if (n.version === 1)
356
+ cacheTime: w
357
+ }), a = await A(o, r), i = f(n, e.pure.u64);
358
+ let c;
359
+ if (t.version === 1)
349
360
  if (r?.accountCap) {
350
- const [i] = e.moveCall({
351
- target: `${n.package}::incentive_v3::withdraw_with_account_cap`,
361
+ const [s] = e.moveCall({
362
+ target: `${t.package}::incentive_v3::withdraw_with_account_cap`,
352
363
  arguments: [
353
364
  e.object("0x06"),
354
- e.object(n.priceOracle),
355
- e.object(n.storage),
365
+ e.object(t.priceOracle),
366
+ e.object(t.storage),
356
367
  e.object(a.contract.pool),
357
368
  e.pure.u8(a.id),
358
- c,
359
- e.object(n.incentiveV2),
360
- e.object(n.incentiveV3),
361
- p(r.accountCap, e.object)
369
+ i,
370
+ e.object(t.incentiveV2),
371
+ e.object(t.incentiveV3),
372
+ f(r.accountCap, e.object)
362
373
  ],
363
374
  typeArguments: [a.suiCoinType]
364
375
  });
365
- s = i;
376
+ c = s;
366
377
  } else {
367
- const [i] = e.moveCall({
368
- target: `${n.package}::incentive_v3::withdraw`,
378
+ const [s] = e.moveCall({
379
+ target: `${t.package}::incentive_v3::withdraw`,
369
380
  arguments: [
370
381
  e.object("0x06"),
371
- e.object(n.priceOracle),
372
- e.object(n.storage),
382
+ e.object(t.priceOracle),
383
+ e.object(t.storage),
373
384
  e.object(a.contract.pool),
374
385
  e.pure.u8(a.id),
375
- c,
376
- e.object(n.incentiveV2),
377
- e.object(n.incentiveV3)
386
+ i,
387
+ e.object(t.incentiveV2),
388
+ e.object(t.incentiveV3)
378
389
  ],
379
390
  typeArguments: [a.suiCoinType]
380
391
  });
381
- s = i;
392
+ c = s;
382
393
  }
383
394
  else if (r?.accountCap) {
384
- const [i] = e.moveCall({
385
- target: `${n.package}::incentive_v3::withdraw_with_account_cap_v2`,
395
+ const [s] = e.moveCall({
396
+ target: `${t.package}::incentive_v3::withdraw_with_account_cap_v2`,
386
397
  arguments: [
387
398
  e.object("0x06"),
388
- e.object(n.priceOracle),
389
- e.object(n.storage),
399
+ e.object(t.priceOracle),
400
+ e.object(t.storage),
390
401
  e.object(a.contract.pool),
391
402
  e.pure.u8(a.id),
392
- c,
393
- e.object(n.incentiveV2),
394
- e.object(n.incentiveV3),
395
- p(r.accountCap, e.object),
403
+ i,
404
+ e.object(t.incentiveV2),
405
+ e.object(t.incentiveV3),
406
+ f(r.accountCap, e.object),
396
407
  e.object("0x05")
397
408
  ],
398
409
  typeArguments: [a.suiCoinType]
399
410
  });
400
- s = i;
411
+ c = s;
401
412
  } else {
402
- const [i] = e.moveCall({
403
- target: `${n.package}::incentive_v3::withdraw_v2`,
413
+ const [s] = e.moveCall({
414
+ target: `${t.package}::incentive_v3::withdraw_v2`,
404
415
  arguments: [
405
416
  e.object("0x06"),
406
- e.object(n.priceOracle),
407
- e.object(n.storage),
417
+ e.object(t.priceOracle),
418
+ e.object(t.storage),
408
419
  e.object(a.contract.pool),
409
420
  e.pure.u8(a.id),
410
- c,
411
- e.object(n.incentiveV2),
412
- e.object(n.incentiveV3),
421
+ i,
422
+ e.object(t.incentiveV2),
423
+ e.object(t.incentiveV3),
413
424
  e.object("0x05")
414
425
  ],
415
426
  typeArguments: [a.suiCoinType]
416
427
  });
417
- s = i;
428
+ c = s;
418
429
  }
419
430
  return e.moveCall({
420
431
  target: "0x2::coin::from_balance",
421
- arguments: [s],
432
+ arguments: [c],
422
433
  typeArguments: [a.suiCoinType]
423
434
  });
424
435
  }
425
- async function Oe(e, t, o, r) {
426
- const n = await h({
436
+ async function Je(e, o, n, r) {
437
+ const t = await k({
427
438
  ...r,
428
- cacheTime: f
429
- }), a = await C(t, r);
439
+ cacheTime: w
440
+ }), a = await A(o, r);
430
441
  if (a?.deprecatedAt && Date.now() > a.deprecatedAt)
431
442
  throw new Error(`The lending pool for coinType ${a.suiCoinType} has been deprecated.`);
432
- const c = p(o, e.pure.u64);
433
- let s;
434
- if (n.version === 1)
443
+ const i = f(n, e.pure.u64);
444
+ let c;
445
+ if (t.version === 1)
435
446
  if (r?.accountCap) {
436
- const [i] = e.moveCall({
437
- target: `${n.package}::incentive_v3::borrow_with_account_cap`,
447
+ const [s] = e.moveCall({
448
+ target: `${t.package}::incentive_v3::borrow_with_account_cap`,
438
449
  arguments: [
439
450
  e.object("0x06"),
440
- e.object(n.priceOracle),
441
- e.object(n.storage),
451
+ e.object(t.priceOracle),
452
+ e.object(t.storage),
442
453
  e.object(a.contract.pool),
443
454
  e.pure.u8(a.id),
444
- c,
445
- e.object(n.incentiveV2),
446
- e.object(n.incentiveV3),
447
- p(r.accountCap, e.object)
455
+ i,
456
+ e.object(t.incentiveV2),
457
+ e.object(t.incentiveV3),
458
+ f(r.accountCap, e.object)
448
459
  ],
449
460
  typeArguments: [a.suiCoinType]
450
461
  });
451
- s = i;
462
+ c = s;
452
463
  } else {
453
- const [i] = e.moveCall({
454
- target: `${n.package}::incentive_v3::borrow`,
464
+ const [s] = e.moveCall({
465
+ target: `${t.package}::incentive_v3::borrow`,
455
466
  arguments: [
456
467
  e.object("0x06"),
457
- e.object(n.priceOracle),
458
- e.object(n.storage),
468
+ e.object(t.priceOracle),
469
+ e.object(t.storage),
459
470
  e.object(a.contract.pool),
460
471
  e.pure.u8(a.id),
461
- c,
462
- e.object(n.incentiveV2),
463
- e.object(n.incentiveV3)
472
+ i,
473
+ e.object(t.incentiveV2),
474
+ e.object(t.incentiveV3)
464
475
  ],
465
476
  typeArguments: [a.suiCoinType]
466
477
  });
467
- s = i;
478
+ c = s;
468
479
  }
469
480
  else if (r?.accountCap) {
470
- const [i] = e.moveCall({
471
- target: `${n.package}::incentive_v3::borrow_with_account_cap_v2`,
481
+ const [s] = e.moveCall({
482
+ target: `${t.package}::incentive_v3::borrow_with_account_cap_v2`,
472
483
  arguments: [
473
484
  e.object("0x06"),
474
- e.object(n.priceOracle),
475
- e.object(n.storage),
485
+ e.object(t.priceOracle),
486
+ e.object(t.storage),
476
487
  e.object(a.contract.pool),
477
488
  e.pure.u8(a.id),
478
- c,
479
- e.object(n.incentiveV2),
480
- e.object(n.incentiveV3),
481
- p(r.accountCap, e.object),
489
+ i,
490
+ e.object(t.incentiveV2),
491
+ e.object(t.incentiveV3),
492
+ f(r.accountCap, e.object),
482
493
  e.object("0x05")
483
494
  ],
484
495
  typeArguments: [a.suiCoinType]
485
496
  });
486
- s = i;
497
+ c = s;
487
498
  } else {
488
- const [i] = e.moveCall({
489
- target: `${n.package}::incentive_v3::borrow_v2`,
499
+ const [s] = e.moveCall({
500
+ target: `${t.package}::incentive_v3::borrow_v2`,
490
501
  arguments: [
491
502
  e.object("0x06"),
492
- e.object(n.priceOracle),
493
- e.object(n.storage),
503
+ e.object(t.priceOracle),
504
+ e.object(t.storage),
494
505
  e.object(a.contract.pool),
495
506
  e.pure.u8(a.id),
496
- c,
497
- e.object(n.incentiveV2),
498
- e.object(n.incentiveV3),
507
+ i,
508
+ e.object(t.incentiveV2),
509
+ e.object(t.incentiveV3),
499
510
  e.object("0x05")
500
511
  ],
501
512
  typeArguments: [a.suiCoinType]
502
513
  });
503
- s = i;
514
+ c = s;
504
515
  }
505
516
  return e.moveCall({
506
517
  target: "0x2::coin::from_balance",
507
- arguments: [e.object(s)],
518
+ arguments: [e.object(c)],
508
519
  typeArguments: [a.suiCoinType]
509
520
  });
510
521
  }
511
- async function Le(e, t, o, r) {
512
- const n = await h({
522
+ async function Qe(e, o, n, r) {
523
+ const t = await k({
513
524
  ...r,
514
- cacheTime: f
515
- }), a = await C(t, r), c = typeof o == "object" && o.$kind === "GasCoin";
516
- if (g(a.suiCoinType) === g("0x2::sui::SUI") && c) {
525
+ cacheTime: w
526
+ }), a = await A(o, r), i = typeof n == "object" && n.$kind === "GasCoin";
527
+ if (b(a.suiCoinType) === b("0x2::sui::SUI") && i) {
517
528
  if (!r?.amount)
518
529
  throw new Error("Amount is required for sui coin");
519
- o = e.splitCoins(o, [r.amount]);
530
+ n = e.splitCoins(n, [r.amount]);
520
531
  }
521
- let s;
522
- if (typeof r?.amount < "u" ? s = p(r.amount, e.pure.u64) : s = e.moveCall({
532
+ let c;
533
+ if (typeof r?.amount < "u" ? c = f(r.amount, e.pure.u64) : c = e.moveCall({
523
534
  target: "0x2::coin::value",
524
- arguments: [p(o, e.object)],
535
+ arguments: [f(n, e.object)],
525
536
  typeArguments: [a.suiCoinType]
526
537
  }), r?.accountCap) {
527
538
  const [u] = e.moveCall({
528
- target: `${n.package}::incentive_v3::repay_with_account_cap`,
539
+ target: `${t.package}::incentive_v3::repay_with_account_cap`,
529
540
  arguments: [
530
541
  e.object("0x06"),
531
- e.object(n.priceOracle),
532
- e.object(n.storage),
542
+ e.object(t.priceOracle),
543
+ e.object(t.storage),
533
544
  e.object(a.contract.pool),
534
545
  e.pure.u8(a.id),
535
- p(o, e.object),
536
- e.object(n.incentiveV2),
537
- e.object(n.incentiveV3),
538
- p(r.accountCap, e.object)
546
+ f(n, e.object),
547
+ e.object(t.incentiveV2),
548
+ e.object(t.incentiveV3),
549
+ f(r.accountCap, e.object)
539
550
  ],
540
551
  typeArguments: [a.suiCoinType]
541
552
  });
@@ -546,496 +557,749 @@ async function Le(e, t, o, r) {
546
557
  });
547
558
  } else
548
559
  return e.moveCall({
549
- target: `${n.package}::incentive_v3::entry_repay`,
560
+ target: `${t.package}::incentive_v3::entry_repay`,
550
561
  arguments: [
551
562
  e.object("0x06"),
552
- e.object(n.priceOracle),
553
- e.object(n.storage),
563
+ e.object(t.priceOracle),
564
+ e.object(t.storage),
554
565
  e.object(a.contract.pool),
555
566
  e.pure.u8(a.id),
556
- p(o, e.object),
557
- s,
558
- e.object(n.incentiveV2),
559
- e.object(n.incentiveV3)
567
+ f(n, e.object),
568
+ c,
569
+ e.object(t.incentiveV2),
570
+ e.object(t.incentiveV3)
560
571
  ],
561
572
  typeArguments: [a.suiCoinType]
562
573
  }), e;
563
574
  }
564
- const qe = j(
565
- v(
575
+ const Xe = I(
576
+ B(
566
577
  async (e) => {
567
- const t = await h({
578
+ const o = await k({
568
579
  ...e
569
580
  });
570
581
  if (e?.address && typeof e?.asset < "u")
571
582
  try {
572
- const n = await C(e.asset, e), a = e?.client ?? T, c = new B();
573
- c.moveCall({
574
- target: `${t.package}::incentive_v3::get_borrow_fee_v2`,
583
+ const t = await A(e.asset, e), a = e?.client ?? $, i = new U();
584
+ i.moveCall({
585
+ target: `${o.package}::incentive_v3::get_borrow_fee_v2`,
575
586
  arguments: [
576
- c.object(t.incentiveV3),
577
- c.pure.address(e.address),
578
- c.pure.u8(n.id),
579
- c.pure.u64(1e4)
587
+ i.object(o.incentiveV3),
588
+ i.pure.address(e.address),
589
+ i.pure.u8(t.id),
590
+ i.pure.u64(1e4)
580
591
  ],
581
592
  typeArguments: []
582
593
  });
583
- const s = await a.devInspectTransactionBlock({
584
- transactionBlock: c,
594
+ const c = await a.devInspectTransactionBlock({
595
+ transactionBlock: i,
585
596
  sender: e.address
586
- }), u = M(s, [w.u64()]);
597
+ }), u = O(c, [_.u64()]);
587
598
  return (Number(u[0]) || 0) / 100;
588
- } catch (n) {
589
- console.error(n);
599
+ } catch (t) {
600
+ console.error(t);
590
601
  }
591
- const r = (await T.getObject({
592
- id: t.incentiveV3,
602
+ const r = (await $.getObject({
603
+ id: o.incentiveV3,
593
604
  options: { showType: !0, showOwner: !0, showContent: !0 }
594
605
  })).data.content.fields.borrow_fee_rate;
595
606
  return Number(r) / 100;
596
607
  }
597
608
  )
598
- ), I = "main", me = {
599
- main: {
600
- id: 0,
601
- key: "main",
602
- name: "Main Market"
603
- }
604
- };
605
- class fe {
606
- constructor(t, o) {
607
- this.pools = [], this.emodes = [], this.config = N(t), this.addPools(o);
608
- }
609
- addPools(t) {
610
- const o = le(this.pools), r = S(this.emodes);
611
- t.forEach((n) => {
612
- if (!this.checkMarket(n.market)) {
613
- console.warn(`Pool is not in market ${this.config.name}`, n);
614
- return;
615
- }
616
- o[n.id] || this.pools.push(n), n.emodes.forEach((c) => {
617
- r[c.emodeId] || this.emodes.push(c);
618
- });
619
- });
620
- }
621
- getEMode(t) {
622
- if (!this.checkMarket(t.marketId))
623
- return console.warn(
624
- `EMode market mismatch ${this.config.id} !== ${t.marketId}`,
625
- t
626
- ), null;
627
- const n = S(this.emodes)[t.emodeId];
628
- return n || (console.warn(
629
- `EMode not found ${t.emodeId} in market ${this.config.name}`,
630
- t
631
- ), null);
632
- }
633
- getEModePools(t) {
634
- const o = this.getEMode(t);
635
- if (!o)
636
- return [];
637
- const r = o.assets.map((a) => a.assetId);
638
- return this.pools.filter((a) => r.includes(a.id)).map((a) => ({
639
- ...a,
640
- emode: o,
641
- isEMode: !0
642
- }));
643
- }
644
- checkMarket(t) {
645
- let o = !1;
646
- return typeof t == "number" && t === this.config.id && (o = !0), typeof t == "string" && t === this.config.key && (o = !0), typeof t == "object" && t.id === this.config.id && (o = !0), o;
647
- }
648
- }
649
- const N = (e) => {
650
- const o = Object.values(me).find((r) => typeof e == "number" ? r.id === e : typeof e == "string" ? r.key === e : r.id === e.id);
651
- if (!o)
652
- throw new Error("Market not found");
653
- return o;
654
- }, ge = j(
655
- v(
656
- async (e, t) => (await Promise.all(
657
- e.map((r) => E({
658
- cacheTime: 6e4,
659
- ...t,
660
- market: r
661
- }))
662
- )).map((r) => {
663
- const n = r[0].market;
664
- return new fe(n, r);
665
- })
666
- )
667
- ), He = j(
668
- v(
669
- async (e, t) => (await ge([e], t))[0]
670
- )
671
- ), h = j(
672
- v(
673
- async (e) => {
674
- const t = N(e?.market || I), o = `https://open-api.naviprotocol.io/api/navi/config?env=${e?.env || "prod"}&sdk=${k.version}&market=${t.key}`;
675
- return (await fetch(o, { headers: $ }).then((n) => n.json())).data;
676
- }
677
- )
678
- ), f = 1e3 * 60 * 5;
679
- async function he(e, t) {
680
- const o = await h({
681
- cacheTime: f,
682
- ...t
609
+ );
610
+ async function be(e, o) {
611
+ const n = await k({
612
+ cacheTime: w,
613
+ ...o
683
614
  });
684
615
  return e.moveCall({
685
- target: `${o.package}::lending::create_account`,
616
+ target: `${n.package}::lending::create_account`,
686
617
  arguments: []
687
618
  });
688
619
  }
689
- async function ye(e, t, o) {
690
- const r = await h({
691
- cacheTime: f,
692
- ...o
620
+ async function ke(e, o, n) {
621
+ const r = await k({
622
+ cacheTime: w,
623
+ ...n
693
624
  });
694
625
  return e.moveCall({
695
626
  target: `${r.package}::lending_core::account_owner`,
696
- arguments: [t]
627
+ arguments: [o]
697
628
  });
698
629
  }
699
- async function we(e, t, o) {
700
- const r = await h({
701
- ...o,
702
- cacheTime: f
630
+ async function _e(e, o, n) {
631
+ const r = await k({
632
+ ...n,
633
+ cacheTime: w
703
634
  });
704
- return o?.accountCap ? e.moveCall({
635
+ return n?.accountCap ? e.moveCall({
705
636
  target: `${r.package}::lending::enter_emode_with_account_cap`,
706
637
  arguments: [
707
638
  e.object(r.storage),
708
- p(t, e.pure.u64),
709
- p(o.accountCap, e.object)
639
+ f(o, e.pure.u64),
640
+ f(n.accountCap, e.object)
710
641
  ]
711
642
  }) : e.moveCall({
712
643
  target: `${r.package}::lending::enter_emode`,
713
- arguments: [e.object(r.storage), p(t, e.pure.u64)]
644
+ arguments: [e.object(r.storage), f(o, e.pure.u64)]
714
645
  }), e;
715
646
  }
716
- async function Ge(e, t) {
717
- const o = await h({
718
- ...t,
719
- cacheTime: f
647
+ async function Ze(e, o) {
648
+ const n = await k({
649
+ ...o,
650
+ cacheTime: w
720
651
  });
721
- return t?.accountCap ? e.moveCall({
722
- target: `${o.package}::lending::exit_emode_with_account_cap`,
723
- arguments: [e.object(o.storage), p(t.accountCap, e.object)]
652
+ return o?.accountCap ? e.moveCall({
653
+ target: `${n.package}::lending::exit_emode_with_account_cap`,
654
+ arguments: [e.object(n.storage), f(o.accountCap, e.object)]
724
655
  }) : e.moveCall({
725
- target: `${o.package}::lending::exit_emode`,
726
- arguments: [e.object(o.storage)]
656
+ target: `${n.package}::lending::exit_emode`,
657
+ arguments: [e.object(n.storage)]
727
658
  }), e;
728
659
  }
729
- async function We(e, t, o) {
730
- const r = await h({
731
- cacheTime: f,
732
- ...o
733
- }), n = await he(e, o);
734
- await we(e, t, {
735
- ...o,
736
- accountCap: n
660
+ async function xe(e, o, n) {
661
+ const r = await k({
662
+ cacheTime: w,
663
+ ...n
664
+ }), t = await be(e, n);
665
+ await _e(e, o, {
666
+ ...n,
667
+ accountCap: t
737
668
  });
738
- const a = await N(o?.market || I), c = await ye(e, n, o);
669
+ const a = await j(n?.market || M), i = await ke(e, t, n);
739
670
  return e.moveCall({
740
671
  target: `${r.emode.contract.registryPackage}::registry::register_emode_for_account_cap`,
741
672
  arguments: [
742
673
  e.object(r.emode.contract.registryObject),
743
- c,
744
- p(a.id, e.pure.u64),
745
- p(t, e.pure.u64)
674
+ i,
675
+ f(a.id, e.pure.u64),
676
+ f(o, e.pure.u64)
746
677
  ]
747
- }), n;
678
+ }), t;
748
679
  }
749
- const be = j(
750
- v(
751
- async (e, t) => {
752
- const o = await h({
753
- cacheTime: f,
754
- ...t
755
- }), r = new B(), n = t?.client ?? T;
680
+ const Q = I(
681
+ B(
682
+ async (e, o) => {
683
+ const n = await k({
684
+ cacheTime: w,
685
+ ...o
686
+ }), r = new U(), t = o?.client ?? $;
756
687
  r.moveCall({
757
- target: `${o.emode.contract.registryPackage}::registry::find_user_emode_account_caps`,
758
- arguments: [r.object(o.emode.contract.registryObject), r.pure.address(e)]
688
+ target: `${n.emode.contract.registryPackage}::registry::find_user_emode_account_caps`,
689
+ arguments: [r.object(n.emode.contract.registryObject), r.pure.address(e)]
759
690
  });
760
- const c = (await n.devInspectTransactionBlock({
691
+ const i = (await t.devInspectTransactionBlock({
761
692
  transactionBlock: r,
762
693
  sender: e
763
- })).results[0].returnValues, s = w.vector(w.u64()).parse(Uint8Array.from(c[0][0])), u = w.vector(w.u64()).parse(Uint8Array.from(c[1][0])), i = w.vector(w.Address).parse(Uint8Array.from(c[2][0]));
764
- return s.map((m, d) => ({
765
- marketId: Number(m),
766
- emodeId: Number(u[d]),
767
- accountCap: i[d].toString()
694
+ })).results[0].returnValues, c = _.vector(_.u64()).parse(Uint8Array.from(i[0][0])), u = _.vector(_.u64()).parse(Uint8Array.from(i[1][0])), s = _.vector(_.Address).parse(Uint8Array.from(i[2][0]));
695
+ return c.map((g, m) => ({
696
+ marketId: Number(g),
697
+ emodeId: Number(u[m]),
698
+ accountCap: s[m].toString()
768
699
  }));
769
700
  }
770
701
  )
771
702
  );
772
- function ze(e, t, o) {
773
- const r = typeof o?.balance == "number", n = r ? o.balance : 0;
703
+ function Ce(e) {
704
+ return `${j(e.marketId).key}-${e.emodeId}`;
705
+ }
706
+ const M = "main", E = {
707
+ main: {
708
+ id: 0,
709
+ key: "main",
710
+ name: "Main Market"
711
+ }
712
+ };
713
+ class je {
714
+ constructor(o, n) {
715
+ this.pools = [], this.emodes = [], this._overview = {
716
+ marketTotalSupplyValue: "0",
717
+ marketTotalBorrowValue: "0"
718
+ }, this.config = j(o), this.addPools(n);
719
+ }
720
+ get overview() {
721
+ return this._overview;
722
+ }
723
+ addPools(o) {
724
+ const n = J(this.pools), r = H(this.emodes);
725
+ let t = l(0), a = l(0);
726
+ o.forEach((i) => {
727
+ if (!this.checkMarket(i.market)) {
728
+ console.warn(`Pool is not in market ${this.config.name}`, i);
729
+ return;
730
+ }
731
+ n[i.uniqueId] || this.pools.push(i), i.emodes.forEach((u) => {
732
+ r[u.uniqueId] || this.emodes.push(u);
733
+ }), a = a.plus(i.poolBorrowValue), t = t.plus(i.poolSupplyValue);
734
+ }), this._overview = {
735
+ marketTotalSupplyValue: t.toString(),
736
+ marketTotalBorrowValue: a.toString()
737
+ };
738
+ }
739
+ getEMode(o) {
740
+ if (!this.checkMarket(o.marketId))
741
+ return console.warn(
742
+ `EMode market mismatch ${this.config.id} !== ${o.marketId}`,
743
+ o
744
+ ), null;
745
+ const t = H(this.emodes)[Ce(o)];
746
+ return t || (console.warn(
747
+ `EMode not found ${o.emodeId} in market ${this.config.name}`,
748
+ o
749
+ ), null);
750
+ }
751
+ getEModePools(o) {
752
+ const n = this.getEMode(o);
753
+ if (!n)
754
+ return [];
755
+ const r = n.assets.map((a) => a.assetId);
756
+ return this.pools.filter((a) => r.includes(a.id)).map((a) => ({
757
+ ...a,
758
+ emode: n,
759
+ isEMode: !0
760
+ }));
761
+ }
762
+ checkMarket(o) {
763
+ let n = !1;
764
+ return typeof o == "number" && o === this.config.id && (n = !0), typeof o == "string" && o === this.config.key && (n = !0), typeof o == "object" && o.id === this.config.id && (n = !0), n;
765
+ }
766
+ }
767
+ const j = (e) => {
768
+ const n = Object.values(E).find((r) => typeof e == "number" ? r.id === e : typeof e == "string" ? r.key === e : r.id === e.id);
769
+ if (!n)
770
+ throw new Error("Market not found");
771
+ return n;
772
+ }, Te = I(
773
+ B(
774
+ async (e, o) => {
775
+ const n = await N({
776
+ cacheTime: 6e4,
777
+ ...o,
778
+ markets: e
779
+ });
780
+ return e.map((r) => {
781
+ const t = j(r), a = n.filter((i) => i.market === t.key);
782
+ return new je(r, a);
783
+ });
784
+ }
785
+ )
786
+ ), et = I(
787
+ B(
788
+ async (e, o) => (await Te([e], o))[0]
789
+ )
790
+ ), k = I(
791
+ B(
792
+ async (e) => {
793
+ const o = j(e?.market || M), n = `https://open-api.naviprotocol.io/api/navi/config?env=${e?.env || "prod"}&sdk=${S.version}&market=${o.key}`;
794
+ return (await fetch(n, { headers: V }).then((t) => t.json())).data;
795
+ }
796
+ )
797
+ ), w = 1e3 * 60 * 5;
798
+ function tt(e, o, n) {
799
+ const r = typeof n?.balance == "number", t = r ? n.balance : 0;
774
800
  let a = 0;
775
- const c = [];
776
- let s = "";
777
- if (t.sort((u, i) => Number(i.balance) - Number(u.balance)).forEach((u) => {
778
- if (!(r && a >= n) && Number(u.balance) !== 0) {
779
- if (s || (s = u.coinType), s !== u.coinType)
801
+ const i = [];
802
+ let c = "";
803
+ if (o.sort((u, s) => Number(s.balance) - Number(u.balance)).forEach((u) => {
804
+ if (!(r && a >= t) && Number(u.balance) !== 0) {
805
+ if (c || (c = u.coinType), c !== u.coinType)
780
806
  throw new Error("All coins must be of the same type");
781
- a += Number(u.balance), c.push(u.coinObjectId);
807
+ a += Number(u.balance), i.push(u.coinObjectId);
782
808
  }
783
- }), c.length === 0)
809
+ }), i.length === 0)
784
810
  throw new Error("No coins to merge");
785
- if (r && a < n)
811
+ if (r && a < t)
786
812
  throw new Error(
787
- `Balance is less than the specified balance: ${a} < ${n}`
813
+ `Balance is less than the specified balance: ${a} < ${t}`
788
814
  );
789
- return g(s) === g("0x2::sui::SUI") && o?.useGasCoin ? r ? e.splitCoins(e.gas, [e.pure.u64(n)]) : e.gas : (c.length === 1 ? e.object(c[0]) : e.mergeCoins(c[0], c.slice(1)), r ? e.splitCoins(c[0], [e.pure.u64(n)]) : c[0]);
815
+ return b(c) === b("0x2::sui::SUI") && n?.useGasCoin ? r ? e.splitCoins(e.gas, [e.pure.u64(t)]) : e.gas : (i.length === 1 ? e.object(i[0]) : e.mergeCoins(i[0], i.slice(1)), r ? e.splitCoins(i[0], [e.pure.u64(t)]) : i[0]);
790
816
  }
791
- async function W(e, t, o, r, n, a, c) {
792
- const s = await h({
793
- ...c,
794
- cacheTime: f
795
- }), u = await C(o, c);
817
+ async function X(e, o, n, r, t, a, i) {
818
+ const c = await k({
819
+ ...i,
820
+ cacheTime: w
821
+ }), u = await A(n, i);
796
822
  return e.moveCall({
797
- target: `${s.uiGetter}::calculator_unchecked::dynamic_health_factor`,
823
+ target: `${c.uiGetter}::calculator_unchecked::dynamic_health_factor`,
798
824
  arguments: [
799
825
  e.object("0x06"),
800
- e.object(s.storage),
801
- e.object(s.oracle.priceOracle),
802
- ie(e, u),
803
- p(t, e.pure.address),
804
- p(u.id, e.pure.u8),
805
- p(r, e.pure.u64),
806
- p(n, e.pure.u64),
807
- p(a, e.pure.bool)
826
+ e.object(c.storage),
827
+ e.object(c.oracle.priceOracle),
828
+ ye(e, u),
829
+ f(o, e.pure.address),
830
+ f(u.id, e.pure.u8),
831
+ f(r, e.pure.u64),
832
+ f(t, e.pure.u64),
833
+ f(a, e.pure.bool)
808
834
  ],
809
835
  typeArguments: [u.suiCoinType]
810
836
  });
811
837
  }
812
- async function ve(e, t, o) {
813
- return W(e, t, 0, 0, 0, !1, o);
838
+ async function Be(e, o, n) {
839
+ return X(e, o, 0, 0, 0, !1, n);
814
840
  }
815
- const L = j(
816
- async (e, t) => {
817
- const o = await h({
818
- ...t,
819
- cacheTime: f
820
- }), r = new B(), n = t?.client ?? T, a = await E(t);
841
+ async function Z(e, o, n) {
842
+ const r = new U(), t = n?.client ?? $, a = await N({
843
+ ...n,
844
+ markets: Object.values(E)
845
+ }), i = J(a);
846
+ for (let g of o) {
847
+ const m = await k({
848
+ ...n,
849
+ cacheTime: w,
850
+ market: g.market
851
+ });
821
852
  r.moveCall({
822
- target: `${o.uiGetter}::getter_unchecked::get_user_state`,
823
- arguments: [r.object(o.storage), r.pure.address(e)]
853
+ target: `${m.uiGetter}::getter_unchecked::get_user_state`,
854
+ arguments: [r.object(m.storage), r.pure.address(g.address)]
824
855
  });
825
- const c = await n.devInspectTransactionBlock({
826
- transactionBlock: r,
827
- sender: e
828
- }), s = M(c, [w.vector(re)]);
829
- return V(
830
- s[0].filter((i) => i.supply_balance !== "0" || i.borrow_balance !== "0")
831
- ).map((i) => {
832
- const m = a.find((_) => _.id === i.assetId), d = F(
833
- i.supplyBalance,
834
- m.currentSupplyIndex
835
- ).toString(), b = F(
836
- i.borrowBalance,
837
- m.currentBorrowIndex
856
+ }
857
+ const u = ((await t.devInspectTransactionBlock({
858
+ transactionBlock: r,
859
+ sender: e
860
+ })).results || []).map((g) => g.returnValues?.map((m) => _.vector(pe).parse(Uint8Array.from(m[0])))[0] || []), s = [];
861
+ return u.forEach((g, m) => {
862
+ const d = o[m], v = j(d.market);
863
+ g.forEach((y) => {
864
+ if (y.supply_balance === "0" && y.borrow_balance === "0")
865
+ return;
866
+ const h = i[`${v.key}-${y.asset_id}`];
867
+ if (!h)
868
+ return;
869
+ const C = W(
870
+ y.supply_balance,
871
+ h.currentSupplyIndex
872
+ ).toString(), T = W(
873
+ y.borrow_balance,
874
+ h.currentBorrowIndex
838
875
  ).toString();
839
- return {
840
- ...i,
841
- supplyBalance: d,
842
- borrowBalance: b,
843
- pool: m
844
- };
845
- }).filter((i) => !!i.pool);
876
+ s.push({
877
+ supplyBalance: C,
878
+ borrowBalance: T,
879
+ assetId: y.asset_id,
880
+ market: v.key,
881
+ pool: h,
882
+ emodeId: d.emodeId
883
+ });
884
+ });
885
+ }), s;
886
+ }
887
+ const ot = I(
888
+ async (e, o) => {
889
+ const r = (o?.markets || Object.keys(E)).map((t) => j(t)).map((t) => ({
890
+ address: e,
891
+ market: t.key
892
+ }));
893
+ return await Z(e, r, o);
846
894
  }
847
895
  );
848
- async function Ke(e, t) {
849
- const o = t?.client ?? T, r = new B();
850
- await ve(r, e, t);
851
- const n = await o.devInspectTransactionBlock({
896
+ async function nt(e, o) {
897
+ const n = o?.client ?? $, r = new U();
898
+ await Be(r, e, o);
899
+ const t = await n.devInspectTransactionBlock({
852
900
  transactionBlock: r,
853
901
  sender: e
854
- }), a = M(n, [w.u256()]);
855
- return G(Number(a[0]) || 0);
902
+ }), a = O(t, [_.u256()]);
903
+ return Y(Number(a[0]) || 0);
856
904
  }
857
- async function Ye(e, t, o, r) {
858
- const n = r?.client ?? T, a = new B();
859
- let c = 0, s = 0;
860
- const u = await C(t, r);
861
- if (o.forEach((b) => {
862
- b.type === P.Supply ? c += b.amount : b.type === P.Withdraw ? c -= b.amount : b.type === P.Borrow ? s += b.amount : b.type === P.Repay && (s -= b.amount);
863
- }), c * s < 0)
905
+ async function rt(e, o, n, r) {
906
+ const t = r?.client ?? $, a = new U();
907
+ let i = 0, c = 0;
908
+ const u = await A(o, r);
909
+ if (n.forEach((d) => {
910
+ d.type === D.Supply ? i += d.amount : d.type === D.Withdraw ? i -= d.amount : d.type === D.Borrow ? c += d.amount : d.type === D.Repay && (c -= d.amount);
911
+ }), i * c < 0)
864
912
  throw new Error("Invalid operations");
865
- const i = c > 0 || s > 0;
866
- await W(
913
+ const s = i > 0 || c > 0;
914
+ await X(
867
915
  a,
868
916
  e,
869
917
  u,
918
+ Math.abs(i),
870
919
  Math.abs(c),
871
- Math.abs(s),
872
- i,
920
+ s,
873
921
  r
874
922
  );
875
- const m = await n.devInspectTransactionBlock({
923
+ const g = await t.devInspectTransactionBlock({
876
924
  transactionBlock: a,
877
925
  sender: e
878
- }), d = M(m, [w.u256()]);
879
- return G(Number(d[0]) || 0);
926
+ }), m = O(g, [_.u256()]);
927
+ return Y(Number(m[0]) || 0);
880
928
  }
881
- const Je = v(
882
- async (e, t) => {
883
- const o = new URLSearchParams();
884
- t?.cursor && o.set("cursor", t.cursor), o.set("userAddress", e);
885
- const r = `https://open-api.naviprotocol.io/api/navi/user/transactions?${o.toString()}&sdk=${k.version}`;
886
- return (await fetch(r, { headers: $ }).then((a) => a.json())).data;
929
+ const at = B(
930
+ async (e, o) => {
931
+ const n = new URLSearchParams();
932
+ o?.cursor && n.set("cursor", o.cursor), n.set("userAddress", e);
933
+ const r = `https://open-api.naviprotocol.io/api/navi/user/transactions?${n.toString()}&sdk=${S.version}`;
934
+ return (await fetch(r, { headers: V }).then((a) => a.json())).data;
887
935
  }
888
936
  );
889
- async function Qe(e, t) {
890
- let o = null;
891
- const r = [], n = t?.client ?? T;
937
+ async function ct(e, o) {
938
+ let n = null;
939
+ const r = [], t = o?.client ?? $;
892
940
  do {
893
941
  let a;
894
- if (t?.coinType ? a = await n.getCoins({
942
+ if (o?.coinType ? a = await t.getCoins({
895
943
  owner: e,
896
- coinType: t?.coinType,
897
- cursor: o,
944
+ coinType: o?.coinType,
945
+ cursor: n,
898
946
  limit: 100
899
- }) : a = await n.getAllCoins({
947
+ }) : a = await t.getAllCoins({
900
948
  owner: e,
901
- cursor: o,
949
+ cursor: n,
902
950
  limit: 100
903
951
  }), !a.data || !a.data.length)
904
952
  break;
905
- r.push(...a.data), o = a.nextCursor;
906
- } while (o);
953
+ r.push(...a.data), n = a.nextCursor;
954
+ } while (n);
907
955
  return r;
908
956
  }
909
- const Xe = j(
910
- async (e, t) => {
911
- const o = [], [r, n] = await Promise.all([
912
- L(e, t),
913
- be(e, t)
914
- ]);
915
- return r.forEach((c) => {
916
- if (y(c.supplyBalance).gt(0)) {
917
- const s = y(c.supplyBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, y.ROUND_DOWN);
918
- o.push({
919
- id: c.pool.uid,
920
- wallet: e,
921
- protocol: "navi",
922
- type: "navi-lending-supply",
923
- "navi-lending-supply": {
924
- amount: s.toString(),
925
- pool: c.pool,
926
- token: c.pool.token,
927
- valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
928
- }
929
- });
930
- }
931
- if (y(c.borrowBalance).gt(0)) {
932
- const s = y(c.borrowBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, y.ROUND_DOWN);
933
- o.push({
934
- id: c.pool.uid,
935
- wallet: e,
936
- protocol: "navi",
937
- type: "navi-lending-borrow",
938
- "navi-lending-borrow": {
939
- amount: s.toString(),
940
- pool: c.pool,
941
- token: c.pool.token,
942
- valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
943
- }
944
- });
945
- }
946
- }), (await Promise.all(
947
- n.map(async (c) => L(c.accountCap, t))
948
- )).forEach((c, s) => {
949
- const u = n[s];
950
- c.forEach((i) => {
951
- if (y(i.supplyBalance).gt(0)) {
952
- const m = y(i.supplyBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, y.ROUND_DOWN);
953
- o.push({
954
- id: i.pool.uid,
957
+ const Ae = I(
958
+ async (e, o) => {
959
+ const n = [], r = (o?.markets || Object.keys(E)).map((c) => j(c)), t = await Q(e, o), a = r.map((c) => ({
960
+ address: e,
961
+ market: c.key
962
+ })).concat(
963
+ t.filter((c) => !!r.find((u) => u.id === c.marketId)).map((c) => ({
964
+ address: e,
965
+ market: j(c.marketId).key,
966
+ emodeId: c.emodeId
967
+ }))
968
+ );
969
+ return (await Z(e, a, o)).forEach((c) => {
970
+ const u = c.emodeId ? t.find((s) => {
971
+ const g = j(c.market);
972
+ return s.emodeId === c.emodeId && s.marketId === g.id;
973
+ }) : void 0;
974
+ if (u) {
975
+ if (l(c.supplyBalance).gt(0)) {
976
+ const s = l(c.supplyBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, l.ROUND_DOWN);
977
+ n.push({
978
+ id: `${c.pool.uniqueId}_${u.emodeId}_navi-lending-emode-supply`,
955
979
  wallet: e,
956
980
  protocol: "navi",
981
+ market: c.market,
957
982
  type: "navi-lending-emode-supply",
958
983
  "navi-lending-emode-supply": {
959
- amount: m.toString(),
960
- pool: O(i.pool, u),
961
- token: i.pool.token,
962
- valueUSD: m.multipliedBy(i.pool.oracle.price).toString()
984
+ amount: s.toString(),
985
+ pool: G(c.pool, u),
986
+ token: c.pool.token,
987
+ valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
963
988
  }
964
989
  });
965
990
  }
966
- if (y(i.borrowBalance).gt(0)) {
967
- const m = y(i.borrowBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, y.ROUND_DOWN);
968
- o.push({
969
- id: i.pool.uid,
991
+ if (l(c.borrowBalance).gt(0)) {
992
+ const s = l(c.borrowBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, l.ROUND_DOWN);
993
+ n.push({
994
+ id: `${c.pool.uniqueId}_${u.emodeId}_navi-lending-emode-borrow`,
970
995
  wallet: e,
971
996
  protocol: "navi",
997
+ market: c.market,
972
998
  type: "navi-lending-emode-borrow",
973
999
  "navi-lending-emode-borrow": {
974
- amount: m.toString(),
975
- pool: O(i.pool, u),
976
- token: i.pool.token,
977
- valueUSD: m.multipliedBy(i.pool.oracle.price).toString()
1000
+ amount: s.toString(),
1001
+ pool: G(c.pool, u),
1002
+ token: c.pool.token,
1003
+ valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
978
1004
  }
979
1005
  });
980
1006
  }
981
- });
982
- }), o;
1007
+ } else {
1008
+ if (l(c.supplyBalance).gt(0)) {
1009
+ const s = l(c.supplyBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, l.ROUND_DOWN);
1010
+ n.push({
1011
+ id: `${c.pool.uniqueId}_navi-lending-supply`,
1012
+ wallet: e,
1013
+ protocol: "navi",
1014
+ type: "navi-lending-supply",
1015
+ market: c.market,
1016
+ "navi-lending-supply": {
1017
+ amount: s.toString(),
1018
+ pool: c.pool,
1019
+ token: c.pool.token,
1020
+ valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
1021
+ }
1022
+ });
1023
+ }
1024
+ if (l(c.borrowBalance).gt(0)) {
1025
+ const s = l(c.borrowBalance).shiftedBy(-9).decimalPlaces(c.pool.token.decimals, l.ROUND_DOWN);
1026
+ n.push({
1027
+ id: `${c.pool.uniqueId}_navi-lending-borrow`,
1028
+ wallet: e,
1029
+ protocol: "navi",
1030
+ market: c.market,
1031
+ type: "navi-lending-borrow",
1032
+ "navi-lending-borrow": {
1033
+ amount: s.toString(),
1034
+ pool: c.pool,
1035
+ token: c.pool.token,
1036
+ valueUSD: s.multipliedBy(c.pool.oracle.price).toString()
1037
+ }
1038
+ });
1039
+ }
1040
+ }
1041
+ }), n;
1042
+ }
1043
+ );
1044
+ class it {
1045
+ constructor(o) {
1046
+ this._positions = [], this._overview = {
1047
+ hf: 1 / 0,
1048
+ netVaule: "0",
1049
+ netWorthApr: "0",
1050
+ totalSupplyValue: "0",
1051
+ totalBorrowValue: "0",
1052
+ totalsupplyApy: "0",
1053
+ totalBorrowApy: "0",
1054
+ maxLiquidationValue: "0",
1055
+ maxLoanToVaule: "0"
1056
+ }, this.positions = o;
1057
+ }
1058
+ get positions() {
1059
+ return this._positions;
1060
+ }
1061
+ get overview() {
1062
+ return this._overview;
1063
+ }
1064
+ set positions(o) {
1065
+ this._positions = o, this._overview = this.getPositionsOverview(o);
1066
+ }
1067
+ deposit(o, n) {
1068
+ const r = !!o.isEMode;
1069
+ let t;
1070
+ r ? t = {
1071
+ id: P(),
1072
+ wallet: "",
1073
+ protocol: "navi",
1074
+ market: "",
1075
+ type: "navi-lending-emode-supply",
1076
+ "navi-lending-emode-supply": {
1077
+ amount: n.toString(),
1078
+ valueUSD: l(n).multipliedBy(o.oracle.price).toString(),
1079
+ token: o.token,
1080
+ pool: o
1081
+ }
1082
+ } : t = {
1083
+ id: P(),
1084
+ wallet: "",
1085
+ protocol: "navi",
1086
+ market: "",
1087
+ type: "navi-lending-supply",
1088
+ "navi-lending-supply": {
1089
+ amount: n.toString(),
1090
+ valueUSD: l(n).multipliedBy(o.oracle.price).toString(),
1091
+ token: o.token,
1092
+ pool: o
1093
+ }
1094
+ }, this.positions = [...this.positions, t];
1095
+ }
1096
+ withdraw(o, n) {
1097
+ const r = !!o.isEMode;
1098
+ let t;
1099
+ r ? t = {
1100
+ id: P(),
1101
+ wallet: "",
1102
+ protocol: "navi",
1103
+ market: "",
1104
+ type: "navi-lending-emode-supply",
1105
+ "navi-lending-emode-supply": {
1106
+ amount: (-n).toString(),
1107
+ valueUSD: l(-n).multipliedBy(o.oracle.price).toString(),
1108
+ token: o.token,
1109
+ pool: o
1110
+ }
1111
+ } : t = {
1112
+ id: P(),
1113
+ wallet: "",
1114
+ protocol: "navi",
1115
+ market: "",
1116
+ type: "navi-lending-supply",
1117
+ "navi-lending-supply": {
1118
+ amount: (-n).toString(),
1119
+ valueUSD: l(-n).multipliedBy(o.oracle.price).toString(),
1120
+ token: o.token,
1121
+ pool: o
1122
+ }
1123
+ }, this.positions = [...this.positions, t];
1124
+ }
1125
+ borrow(o, n) {
1126
+ const r = !!o.isEMode;
1127
+ let t;
1128
+ r ? t = {
1129
+ id: P(),
1130
+ wallet: "",
1131
+ protocol: "navi",
1132
+ market: "",
1133
+ type: "navi-lending-emode-borrow",
1134
+ "navi-lending-emode-borrow": {
1135
+ amount: n.toString(),
1136
+ valueUSD: l(n).multipliedBy(o.oracle.price).toString(),
1137
+ token: o.token,
1138
+ pool: o
1139
+ }
1140
+ } : t = {
1141
+ id: P(),
1142
+ wallet: "",
1143
+ protocol: "navi",
1144
+ market: "",
1145
+ type: "navi-lending-borrow",
1146
+ "navi-lending-borrow": {
1147
+ amount: n.toString(),
1148
+ valueUSD: l(n).multipliedBy(o.oracle.price).toString(),
1149
+ token: o.token,
1150
+ pool: o
1151
+ }
1152
+ }, this.positions = [...this.positions, t];
1153
+ }
1154
+ repay(o, n) {
1155
+ const r = !!o.isEMode;
1156
+ let t;
1157
+ r ? t = {
1158
+ id: P(),
1159
+ wallet: "",
1160
+ protocol: "navi",
1161
+ market: "",
1162
+ type: "navi-lending-emode-borrow",
1163
+ "navi-lending-emode-borrow": {
1164
+ amount: (-n).toString(),
1165
+ valueUSD: l(-n).multipliedBy(o.oracle.price).toString(),
1166
+ token: o.token,
1167
+ pool: o
1168
+ }
1169
+ } : t = {
1170
+ id: P(),
1171
+ wallet: "",
1172
+ protocol: "navi",
1173
+ market: "",
1174
+ type: "navi-lending-borrow",
1175
+ "navi-lending-borrow": {
1176
+ amount: (-n).toString(),
1177
+ valueUSD: l(-n).multipliedBy(o.oracle.price).toString(),
1178
+ token: o.token,
1179
+ pool: o
1180
+ }
1181
+ }, this.positions = [...this.positions, t];
1182
+ }
1183
+ findPositionsByPool(o, n) {
1184
+ const r = !!o.isEMode;
1185
+ return this.positions.filter((t) => r ? t.type === (n ? "navi-lending-emode-supply" : "navi-lending-emode-borrow") : t.type === (n ? "navi-lending-supply" : "navi-lending-borrow"));
1186
+ }
1187
+ getPositionsOverview(o) {
1188
+ let n = new l(0), r = new l(0), t = new l(0), a = new l(0), i = new l(0), c = new l(0);
1189
+ o.forEach((m) => {
1190
+ if (m.type === "navi-lending-supply") {
1191
+ const d = m["navi-lending-supply"];
1192
+ n = n.plus(d.valueUSD), i = i.plus(
1193
+ new l(d.valueUSD).multipliedBy(d.pool.liquidationFactor.threshold)
1194
+ ), c = c.plus(
1195
+ new l(d.valueUSD).multipliedBy(d.pool.ltvValue)
1196
+ );
1197
+ } else if (m.type === "navi-lending-borrow")
1198
+ r = r.plus(m["navi-lending-borrow"].valueUSD);
1199
+ else if (m.type === "navi-lending-emode-supply") {
1200
+ const d = m["navi-lending-emode-supply"];
1201
+ n = n.plus(d.valueUSD);
1202
+ const v = d.pool.emode.assets.find(
1203
+ (y) => y.assetId === d.pool.id
1204
+ );
1205
+ i = i.plus(
1206
+ new l(d.valueUSD).multipliedBy(v.lt)
1207
+ ), c = c.plus(
1208
+ new l(d.valueUSD).multipliedBy(v.ltv)
1209
+ );
1210
+ } else m.type === "navi-lending-emode-borrow" && (r = r.plus(m["navi-lending-emode-borrow"].valueUSD));
1211
+ }), o.forEach((m) => {
1212
+ if (m.type === "navi-lending-supply") {
1213
+ const d = m["navi-lending-supply"], v = d.pool.supplyIncentiveApyInfo.apy;
1214
+ t = t.plus(
1215
+ new l(d.valueUSD).dividedBy(n).multipliedBy(new l(v).dividedBy(100))
1216
+ );
1217
+ } else if (m.type === "navi-lending-borrow") {
1218
+ const d = m["navi-lending-borrow"], v = d.pool.borrowIncentiveApyInfo.apy;
1219
+ r.gt(0) && (a = a.plus(
1220
+ new l(d.valueUSD).dividedBy(r).multipliedBy(new l(v).dividedBy(100))
1221
+ ));
1222
+ } else if (m.type === "navi-lending-emode-supply") {
1223
+ const d = m["navi-lending-emode-supply"], v = d.pool.supplyIncentiveApyInfo.apy;
1224
+ n.gt(0) && (t = t.plus(
1225
+ new l(d.valueUSD).dividedBy(n).multipliedBy(new l(v).dividedBy(100))
1226
+ ));
1227
+ } else if (m.type === "navi-lending-emode-borrow") {
1228
+ const d = m["navi-lending-emode-borrow"], v = d.pool.borrowIncentiveApyInfo.apy;
1229
+ r.gt(0) && (a = a.plus(
1230
+ new l(d.valueUSD).dividedBy(r).multipliedBy(new l(v).dividedBy(100))
1231
+ ));
1232
+ }
1233
+ });
1234
+ const u = n.minus(r), s = n.minus(r).eq(0) ? new l(0) : n.multipliedBy(t).minus(r.multipliedBy(a)).div(n.minus(r));
1235
+ return {
1236
+ hf: r.toNumber() !== 0 ? i.dividedBy(r).toNumber() : 1 / 0,
1237
+ netVaule: u.toString(),
1238
+ netWorthApr: s.toString(),
1239
+ totalSupplyValue: n.toString(),
1240
+ totalBorrowValue: r.toString(),
1241
+ totalsupplyApy: t.toString(),
1242
+ totalBorrowApy: a.toString(),
1243
+ maxLiquidationValue: i.toString(),
1244
+ maxLoanToVaule: c.toString()
1245
+ };
983
1246
  }
984
- ), z = new q("https://hermes.pyth.network", {
1247
+ }
1248
+ const x = new z("https://hermes.pyth.network", {
985
1249
  timeout: 1e4
986
1250
  });
987
- async function Ce(e) {
1251
+ async function Ie(e) {
988
1252
  try {
989
- const t = [], o = await z.getLatestPriceFeeds(e);
990
- if (!o) return t;
1253
+ const o = [], n = await x.getLatestPriceFeeds(e);
1254
+ if (!n) return o;
991
1255
  const r = Math.floor((/* @__PURE__ */ new Date()).valueOf() / 1e3);
992
- for (const n of o) {
993
- const a = n.getPriceUnchecked();
1256
+ for (const t of n) {
1257
+ const a = t.getPriceUnchecked();
994
1258
  if (a.publishTime > r) {
995
1259
  console.warn(
996
- `pyth price feed is invalid, id: ${n.id}, publish time: ${a.publishTime}, current timestamp: ${r}`
1260
+ `pyth price feed is invalid, id: ${t.id}, publish time: ${a.publishTime}, current timestamp: ${r}`
997
1261
  );
998
1262
  continue;
999
1263
  }
1000
- r - n.getPriceUnchecked().publishTime > 30 && (console.info(
1001
- `stale price feed, id: ${n.id}, publish time: ${a.publishTime}, current timestamp: ${r}`
1002
- ), t.push(n.id));
1264
+ r - t.getPriceUnchecked().publishTime > 30 && (console.info(
1265
+ `stale price feed, id: ${t.id}, publish time: ${a.publishTime}, current timestamp: ${r}`
1266
+ ), o.push(t.id));
1003
1267
  }
1004
- return t;
1005
- } catch (t) {
1006
- throw new Error(`failed to get pyth stale price feed id, msg: ${t.message}`);
1268
+ return o;
1269
+ } catch (o) {
1270
+ throw new Error(`failed to get pyth stale price feed id, msg: ${o.message}`);
1007
1271
  }
1008
1272
  }
1009
- async function je(e, t, o) {
1010
- const r = o?.client ?? T, n = await h({
1011
- ...o,
1012
- cacheTime: f
1273
+ async function $e(e, o, n) {
1274
+ const r = n?.client ?? $, t = await k({
1275
+ ...n,
1276
+ cacheTime: w
1013
1277
  });
1014
1278
  try {
1015
- const a = await z.getPriceFeedsUpdateData(t);
1016
- return await new te(
1279
+ const a = await x.getPriceFeedsUpdateData(o);
1280
+ return await new ue(
1017
1281
  r,
1018
- n.oracle.pythStateId,
1019
- n.oracle.wormholeStateId
1020
- ).updatePriceFeeds(e, a, t);
1282
+ t.oracle.pythStateId,
1283
+ t.oracle.wormholeStateId
1284
+ ).updatePriceFeeds(e, a, o);
1021
1285
  } catch (a) {
1022
1286
  throw new Error(`failed to update pyth price feeds, msg: ${a.message}`);
1023
1287
  }
1024
1288
  }
1025
- async function Ze(e, t, o) {
1026
- const r = await h({
1027
- ...o,
1028
- cacheTime: f
1289
+ async function Se(e, o, n) {
1290
+ const r = await k({
1291
+ ...n,
1292
+ cacheTime: w
1029
1293
  });
1030
- if (o?.updatePythPriceFeeds) {
1031
- const n = t.filter((a) => !!a.pythPriceFeedId).map((a) => a.pythPriceFeedId);
1294
+ if (n?.updatePythPriceFeeds) {
1295
+ const t = o.filter((a) => !!a.pythPriceFeedId).map((a) => a.pythPriceFeedId);
1032
1296
  try {
1033
- const a = await Ce(n);
1034
- a.length > 0 && await je(e, a, o);
1297
+ const a = await Ie(t);
1298
+ a.length > 0 && await $e(e, a, n);
1035
1299
  } catch {
1036
1300
  }
1037
1301
  }
1038
- for (const n of t)
1302
+ for (const t of o)
1039
1303
  e.moveCall({
1040
1304
  target: `${r.oracle.packageId}::oracle_pro::update_single_price`,
1041
1305
  arguments: [
@@ -1047,296 +1311,370 @@ async function Ze(e, t, o) {
1047
1311
  // Price oracle contract
1048
1312
  e.object(r.oracle.supraOracleHolder),
1049
1313
  // Supra oracle holder
1050
- e.object(n.pythPriceInfoObject),
1314
+ e.object(t.pythPriceInfoObject),
1051
1315
  // Pyth price info object
1052
- e.pure.address(n.feedId)
1316
+ e.pure.address(t.feedId)
1053
1317
  // Price feed ID
1054
1318
  ]
1055
1319
  });
1056
1320
  return e;
1057
1321
  }
1058
- async function _e(e) {
1059
- return (await h({
1322
+ async function ee(e) {
1323
+ return (await k({
1060
1324
  ...e,
1061
- cacheTime: f
1325
+ cacheTime: w
1062
1326
  })).oracle.feeds;
1063
1327
  }
1064
- function xe(e, t) {
1065
- return e.filter((o) => !!(t?.lendingState && t.lendingState.find((n) => n.assetId === o.assetId) || t?.pools && t.pools.find((n) => n.id === o.assetId)));
1328
+ function Pe(e, o) {
1329
+ return e.filter((n) => !!(o?.lendingState && o.lendingState.find((t) => t.assetId === n.assetId) || o?.lendingPositions && o.lendingPositions.find((t) => [
1330
+ "navi-lending-supply",
1331
+ "navi-lending-borrow",
1332
+ "navi-lending-emode-supply",
1333
+ "navi-lending-emode-borrow"
1334
+ ].includes(t.type) ? t[t.type]?.pool?.id === n.assetId : !1) || o?.pools && o.pools.find((t) => t.id === n.assetId)));
1335
+ }
1336
+ async function st(e, o, n, r) {
1337
+ try {
1338
+ const t = await ee({
1339
+ ...r
1340
+ }), a = [];
1341
+ n.forEach((s) => {
1342
+ a.includes(s.market) || a.push(s.market);
1343
+ });
1344
+ const i = await Ae(o, {
1345
+ ...r,
1346
+ markets: a
1347
+ }), c = Pe(t, {
1348
+ lendingPositions: i,
1349
+ pools: n
1350
+ });
1351
+ return await Se(e, c, {
1352
+ updatePythPriceFeeds: !0,
1353
+ ...r
1354
+ });
1355
+ } catch (t) {
1356
+ if (r?.throws)
1357
+ throw t;
1358
+ return console.error(t), e;
1359
+ }
1066
1360
  }
1067
- const D = j(
1068
- v(
1361
+ const F = I(
1362
+ B(
1069
1363
  async (e) => {
1070
- const t = `https://open-api.naviprotocol.io/api/navi/flashloan?env=${e?.env || "prod"}&sdk=${k.version}&market=${e?.market || I}`, o = await fetch(t, { headers: $ }).then((r) => r.json());
1071
- return Object.keys(o.data).map((r) => ({
1072
- ...o.data[r],
1364
+ const o = `https://open-api.naviprotocol.io/api/navi/flashloan?env=${e?.env || "prod"}&sdk=${S.version}&market=${e?.market || M}`, n = await fetch(o, { headers: V }).then((r) => r.json());
1365
+ return Object.keys(n.data).map((r) => ({
1366
+ ...n.data[r],
1073
1367
  coinType: r
1074
1368
  }));
1075
1369
  }
1076
1370
  )
1077
1371
  );
1078
- async function et(e, t) {
1079
- return (await D(t)).find((r) => typeof e == "string" ? g(r.coinType) === g(e) : typeof e == "number" ? r.assetId === e : r.assetId === e.id) || null;
1372
+ async function ut(e, o) {
1373
+ return (await F(o)).find((r) => typeof e == "string" ? b(r.coinType) === b(e) : typeof e == "number" ? r.assetId === e : r.assetId === e.id) || null;
1080
1374
  }
1081
- async function tt(e, t, o, r) {
1082
- const n = await h({
1375
+ async function lt(e, o, n, r) {
1376
+ const t = await k({
1083
1377
  ...r,
1084
- cacheTime: f
1085
- }), a = await C(t, r);
1086
- if (!(await D({
1378
+ cacheTime: w
1379
+ }), a = await A(o, r);
1380
+ if (!(await F({
1087
1381
  ...r,
1088
- cacheTime: f
1382
+ cacheTime: w
1089
1383
  })).some(
1090
- (u) => g(u.coinType) === g(a.suiCoinType)
1384
+ (u) => b(u.coinType) === b(a.suiCoinType)
1091
1385
  ))
1092
1386
  throw new Error("Pool does not support flashloan");
1093
- if (n.version === 1) {
1094
- const [u, i] = e.moveCall({
1095
- target: `${n.package}::lending::flash_loan_with_ctx`,
1387
+ if (t.version === 1) {
1388
+ const [u, s] = e.moveCall({
1389
+ target: `${t.package}::lending::flash_loan_with_ctx`,
1096
1390
  arguments: [
1097
- e.object(n.flashloanConfig),
1391
+ e.object(t.flashloanConfig),
1098
1392
  e.object(a.contract.pool),
1099
- p(o, e.pure.u64)
1393
+ f(n, e.pure.u64)
1100
1394
  ],
1101
1395
  typeArguments: [a.suiCoinType]
1102
1396
  });
1103
- return [u, i];
1397
+ return [u, s];
1104
1398
  } else {
1105
- const [u, i] = e.moveCall({
1106
- target: `${n.package}::lending::flash_loan_with_ctx_v2`,
1399
+ const [u, s] = e.moveCall({
1400
+ target: `${t.package}::lending::flash_loan_with_ctx_v2`,
1107
1401
  arguments: [
1108
- e.object(n.flashloanConfig),
1402
+ e.object(t.flashloanConfig),
1109
1403
  e.object(a.contract.pool),
1110
- p(o, e.pure.u64),
1404
+ f(n, e.pure.u64),
1111
1405
  e.object("0x05")
1112
1406
  ],
1113
1407
  typeArguments: [a.suiCoinType]
1114
1408
  });
1115
- return [u, i];
1409
+ return [u, s];
1116
1410
  }
1117
1411
  }
1118
- async function ot(e, t, o, r, n) {
1119
- const a = await h({
1120
- ...n,
1121
- cacheTime: f
1122
- }), c = await C(t, n);
1123
- if (!(await D({
1124
- ...n,
1125
- cacheTime: f
1412
+ async function pt(e, o, n, r, t) {
1413
+ const a = await k({
1414
+ ...t,
1415
+ cacheTime: w
1416
+ }), i = await A(o, t);
1417
+ if (!(await F({
1418
+ ...t,
1419
+ cacheTime: w
1126
1420
  })).some(
1127
- (m) => g(m.coinType) === g(c.suiCoinType)
1421
+ (g) => b(g.coinType) === b(i.suiCoinType)
1128
1422
  ))
1129
1423
  throw new Error("Pool does not support flashloan");
1130
- const [i] = e.moveCall({
1424
+ const [s] = e.moveCall({
1131
1425
  target: `${a.package}::lending::flash_repay_with_ctx`,
1132
1426
  arguments: [
1133
1427
  e.object("0x06"),
1134
1428
  e.object(a.storage),
1135
- e.object(c.contract.pool),
1136
- p(o, e.object),
1137
- p(r, e.object)
1429
+ e.object(i.contract.pool),
1430
+ f(n, e.object),
1431
+ f(r, e.object)
1138
1432
  ],
1139
- typeArguments: [c.suiCoinType]
1433
+ typeArguments: [i.suiCoinType]
1140
1434
  });
1141
- return [i];
1435
+ return [s];
1142
1436
  }
1143
- async function rt(e, t, o, r, n, a) {
1144
- const c = {
1437
+ async function dt(e, o, n, r, t, a) {
1438
+ const i = {
1145
1439
  ...a,
1146
- cacheTime: f
1147
- }, s = await h(c), u = await C(t, c), i = await C(r, c);
1148
- if (s.version === 1) {
1149
- const [m, d] = e.moveCall({
1150
- target: `${s.package}::incentive_v3::liquidation`,
1440
+ cacheTime: w
1441
+ }, c = await k(i), u = await A(o, i), s = await A(r, i);
1442
+ if (c.version === 1) {
1443
+ const [g, m] = e.moveCall({
1444
+ target: `${c.package}::incentive_v3::liquidation`,
1151
1445
  arguments: [
1152
1446
  e.object("0x06"),
1153
1447
  // Clock object
1154
- e.object(s.priceOracle),
1448
+ e.object(c.priceOracle),
1155
1449
  // Price oracle for asset pricing
1156
- e.object(s.storage),
1450
+ e.object(c.storage),
1157
1451
  // Protocol storage
1158
1452
  e.pure.u8(u.id),
1159
1453
  // Pay asset ID
1160
1454
  e.object(u.contract.pool),
1161
1455
  // Pay asset pool contract
1162
- p(o, e.object),
1456
+ f(n, e.object),
1163
1457
  // Debt repayment amount
1164
- e.pure.u8(i.id),
1458
+ e.pure.u8(s.id),
1165
1459
  // Collateral asset ID
1166
- e.object(i.contract.pool),
1460
+ e.object(s.contract.pool),
1167
1461
  // Collateral asset pool contract
1168
- p(n, e.pure.address),
1462
+ f(t, e.pure.address),
1169
1463
  // Borrower address
1170
- e.object(s.incentiveV2),
1464
+ e.object(c.incentiveV2),
1171
1465
  // Incentive V2 contract
1172
- e.object(s.incentiveV3)
1466
+ e.object(c.incentiveV3)
1173
1467
  // Incentive V3 contract
1174
1468
  ],
1175
- typeArguments: [u.suiCoinType, i.suiCoinType]
1469
+ typeArguments: [u.suiCoinType, s.suiCoinType]
1176
1470
  });
1177
- return [m, d];
1471
+ return [g, m];
1178
1472
  } else {
1179
- const [m, d] = e.moveCall({
1180
- target: `${s.package}::incentive_v3::liquidation_v2`,
1473
+ const [g, m] = e.moveCall({
1474
+ target: `${c.package}::incentive_v3::liquidation_v2`,
1181
1475
  arguments: [
1182
1476
  e.object("0x06"),
1183
1477
  // Clock object
1184
- e.object(s.priceOracle),
1478
+ e.object(c.priceOracle),
1185
1479
  // Price oracle for asset pricing
1186
- e.object(s.storage),
1480
+ e.object(c.storage),
1187
1481
  // Protocol storage
1188
1482
  e.pure.u8(u.id),
1189
1483
  // Pay asset ID
1190
1484
  e.object(u.contract.pool),
1191
1485
  // Pay asset pool contract
1192
- p(o, e.object),
1486
+ f(n, e.object),
1193
1487
  // Debt repayment amount
1194
- e.pure.u8(i.id),
1488
+ e.pure.u8(s.id),
1195
1489
  // Collateral asset ID
1196
- e.object(i.contract.pool),
1490
+ e.object(s.contract.pool),
1197
1491
  // Collateral asset pool contract
1198
- p(n, e.pure.address),
1492
+ f(t, e.pure.address),
1199
1493
  // Borrower address
1200
- e.object(s.incentiveV2),
1494
+ e.object(c.incentiveV2),
1201
1495
  // Incentive V2 contract
1202
- e.object(s.incentiveV3),
1496
+ e.object(c.incentiveV3),
1203
1497
  // Incentive V3 contract
1204
1498
  e.object("0x05")
1205
1499
  // SuiSystemState object
1206
1500
  ],
1207
- typeArguments: [u.suiCoinType, i.suiCoinType]
1501
+ typeArguments: [u.suiCoinType, s.suiCoinType]
1208
1502
  });
1209
- return [m, d];
1503
+ return [g, m];
1210
1504
  }
1211
1505
  }
1212
- async function nt(e, t) {
1213
- const o = await _e(t), r = await E({
1214
- ...t,
1215
- cacheTime: f
1216
- }), n = t?.client ?? T, a = await h({
1217
- ...t,
1218
- cacheTime: f
1219
- }), c = new B();
1220
- c.moveCall({
1221
- target: `${a.uiGetter}::incentive_v3_getter::get_user_atomic_claimable_rewards`,
1222
- arguments: [
1223
- c.object("0x06"),
1224
- // Clock object
1225
- c.object(a.storage),
1226
- // Protocol storage
1227
- c.object(a.incentiveV3),
1228
- // Incentive V3 contract
1229
- c.pure.address(e)
1230
- // User address
1231
- ]
1232
- });
1233
- const s = await n.devInspectTransactionBlock({
1234
- transactionBlock: c,
1506
+ async function Ve(e, o, n) {
1507
+ const r = n?.client ?? $, t = new U(), a = await N({
1508
+ ...n,
1509
+ markets: Object.values(E),
1510
+ cacheTime: w
1511
+ }), i = await ee(n);
1512
+ for (let g of o) {
1513
+ const m = await k({
1514
+ ...n,
1515
+ cacheTime: w,
1516
+ market: g.market
1517
+ });
1518
+ t.moveCall({
1519
+ target: `${m.uiGetter}::incentive_v3_getter::get_user_atomic_claimable_rewards`,
1520
+ arguments: [
1521
+ t.object("0x06"),
1522
+ // Clock object
1523
+ t.object(m.storage),
1524
+ // Protocol storage
1525
+ t.object(m.incentiveV3),
1526
+ // Incentive V3 contract
1527
+ t.pure.address(g.address)
1528
+ // User address
1529
+ ]
1530
+ });
1531
+ }
1532
+ const c = await r.devInspectTransactionBlock({
1533
+ transactionBlock: t,
1235
1534
  sender: e
1236
- }), u = M(
1237
- s,
1238
- [
1239
- w.vector(w.string()),
1240
- // Asset coin types
1241
- w.vector(w.string()),
1242
- // Reward coin types
1243
- w.vector(w.u8()),
1244
- // Reward options
1245
- w.vector(w.Address),
1246
- // Rule IDs
1247
- w.vector(w.u256())
1248
- // Claimable amounts
1249
- ]
1250
- ), i = [];
1251
- if (u.length === 5 && Array.isArray(u[0])) {
1252
- const m = u[0].length;
1253
- for (let d = 0; d < m; d++) {
1254
- const b = o.find(
1255
- (A) => g(A.coinType) === g(u[1][d])
1256
- ), _ = r.find(
1257
- (A) => g(A.coinType) === g(u[0][d])
1258
- );
1259
- !b || !_ || i.push({
1260
- assetId: _.id,
1261
- assetCoinType: g(u[0][d]),
1262
- rewardCoinType: g(u[1][d]),
1263
- option: Number(u[2][d]),
1264
- userClaimableReward: Number(u[4][d]) / Math.pow(10, b.priceDecimal),
1265
- ruleIds: Array.isArray(u[3][d]) ? u[3][d] : [u[3][d]]
1266
- });
1535
+ }), u = [];
1536
+ c?.results?.forEach((g) => {
1537
+ u.push(
1538
+ O(
1539
+ {
1540
+ results: [g]
1541
+ },
1542
+ [
1543
+ _.vector(_.string()),
1544
+ // Asset coin types
1545
+ _.vector(_.string()),
1546
+ // Reward coin types
1547
+ _.vector(_.u8()),
1548
+ // Reward options
1549
+ _.vector(_.Address),
1550
+ // Rule IDs
1551
+ _.vector(_.u256())
1552
+ // Claimable amounts
1553
+ ]
1554
+ )
1555
+ );
1556
+ });
1557
+ const s = [];
1558
+ return u.forEach((g, m) => {
1559
+ const d = o[m];
1560
+ if (g.length === 5 && Array.isArray(g[0])) {
1561
+ const v = g[0].length;
1562
+ for (let y = 0; y < v; y++) {
1563
+ const h = i.find(
1564
+ (T) => b(T.coinType) === b(g[1][y])
1565
+ ), C = a.find(
1566
+ (T) => b(T.coinType) === b(g[0][y])
1567
+ );
1568
+ !h || !C || s.push({
1569
+ assetId: C.id,
1570
+ assetCoinType: b(g[0][y]),
1571
+ rewardCoinType: b(g[1][y]),
1572
+ option: Number(g[2][y]),
1573
+ userClaimableReward: Number(g[4][y]) / Math.pow(10, h.priceDecimal),
1574
+ ruleIds: Array.isArray(g[3][y]) ? g[3][y] : [g[3][y]],
1575
+ market: d.market,
1576
+ owner: d.address,
1577
+ emodeId: d.emodeId
1578
+ });
1579
+ }
1267
1580
  }
1268
- }
1269
- return i;
1581
+ }), s;
1270
1582
  }
1271
- function at(e) {
1272
- const t = /* @__PURE__ */ new Map();
1583
+ async function mt(e, o) {
1584
+ const n = (o?.markets || [E.main]).map((a) => j(a)), r = await Q(e, o), t = n.map((a) => ({
1585
+ address: e,
1586
+ market: a.key
1587
+ })).concat(
1588
+ r.filter((a) => !!n.find((i) => i.id === a.marketId)).map((a) => {
1589
+ const i = j(a.marketId);
1590
+ return {
1591
+ address: e,
1592
+ market: i.key,
1593
+ emodeId: a.emodeId
1594
+ };
1595
+ })
1596
+ );
1597
+ return await Ve(e, t, o);
1598
+ }
1599
+ function gt(e) {
1600
+ const o = /* @__PURE__ */ new Map();
1273
1601
  e.forEach((r) => {
1274
- const n = r.assetId, a = r.option, c = `${n}-${a}-${r.rewardCoinType}`;
1275
- t.has(c) ? t.get(c).total += r.userClaimableReward : t.set(c, {
1276
- assetId: n,
1602
+ const t = r.assetId, a = r.option, i = `${t}-${a}-${r.rewardCoinType}-${r.market}`;
1603
+ o.has(i) ? o.get(i).total += r.userClaimableReward : o.set(i, {
1604
+ assetId: t,
1277
1605
  rewardType: a,
1278
1606
  coinType: r.rewardCoinType,
1279
- total: Number(r.userClaimableReward)
1607
+ total: Number(r.userClaimableReward),
1608
+ market: r.market
1280
1609
  });
1281
1610
  });
1282
- const o = /* @__PURE__ */ new Map();
1283
- for (const { assetId: r, rewardType: n, coinType: a, total: c } of t.values()) {
1284
- const s = `${r}-${n}`;
1285
- o.has(s) || o.set(s, { assetId: r, rewardType: n, rewards: /* @__PURE__ */ new Map() });
1286
- const u = o.get(s);
1287
- u.rewards.set(a, (u.rewards.get(a) || 0) + c);
1611
+ const n = /* @__PURE__ */ new Map();
1612
+ for (const { assetId: r, rewardType: t, coinType: a, total: i, market: c } of o.values()) {
1613
+ const u = `${r}-${t}-${c}`;
1614
+ n.has(u) || n.set(u, { assetId: r, rewardType: t, market: c, rewards: /* @__PURE__ */ new Map() });
1615
+ const s = n.get(u);
1616
+ s.rewards.set(a, (s.rewards.get(a) || 0) + i);
1288
1617
  }
1289
- return Array.from(o.values()).map((r) => ({
1618
+ return Array.from(n.values()).map((r) => ({
1290
1619
  assetId: r.assetId,
1291
1620
  rewardType: r.rewardType,
1292
- rewards: Array.from(r.rewards.entries()).map(([n, a]) => ({
1293
- coinType: n,
1621
+ market: r.market,
1622
+ rewards: Array.from(r.rewards.entries()).map(([t, a]) => ({
1623
+ coinType: t,
1294
1624
  available: a.toFixed(6)
1295
1625
  }))
1296
1626
  }));
1297
1627
  }
1298
- const ct = v(
1299
- async (e, t) => {
1300
- const o = `https://open-api.naviprotocol.io/api/navi/user/total_claimed_reward?userAddress=${e}&sdk=${k.version}&market=${t?.market || I}`;
1301
- return (await fetch(o, { headers: $ }).then((n) => n.json())).data;
1628
+ const ft = B(
1629
+ async (e, o) => {
1630
+ const n = `https://open-api.naviprotocol.io/api/navi/user/total_claimed_reward?userAddress=${e}&sdk=${S.version}&market=${o?.market || M}`;
1631
+ return (await fetch(n, { headers: V }).then((t) => t.json())).data;
1302
1632
  }
1303
- ), st = v(
1304
- async (e, t) => {
1305
- const o = `https://open-api.naviprotocol.io/api/navi/user/rewards?userAddress=${e}&page=${t?.page || 1}&pageSize=${t?.size || 400}&sdk=${k.version}&market=${t?.market || I}`, r = await fetch(o, { headers: $ }).then((n) => n.json());
1306
- return V({
1633
+ ), yt = B(
1634
+ async (e, o) => {
1635
+ const n = `https://open-api.naviprotocol.io/api/navi/user/rewards?userAddress=${e}&page=${o?.page || 1}&pageSize=${o?.size || 400}&sdk=${S.version}&market=${o?.market || M}`, r = await fetch(n, { headers: V }).then((t) => t.json());
1636
+ return R({
1307
1637
  data: r.data.rewards
1308
1638
  });
1309
1639
  }
1310
1640
  );
1311
- async function it(e, t, o) {
1312
- const r = await h({
1313
- ...o,
1314
- cacheTime: f
1315
- }), n = await E({
1316
- ...o,
1317
- cacheTime: f
1641
+ async function vt(e, o, n) {
1642
+ const r = await k({
1643
+ ...n,
1644
+ cacheTime: w
1645
+ }), t = await N({
1646
+ ...n,
1647
+ markets: Object.values(E),
1648
+ cacheTime: w
1318
1649
  }), a = /* @__PURE__ */ new Map();
1319
- for (const s of t) {
1320
- const { rewardCoinType: u, ruleIds: i } = s;
1321
- for (const m of i) {
1322
- a.has(u) || a.set(u, { assetIds: [], ruleIds: [], amount: 0 });
1323
- const d = a.get(u);
1324
- d.assetIds.push(s.assetCoinType.replace("0x", "")), d.ruleIds.push(m), d.amount += s.userClaimableReward;
1650
+ for (const c of o) {
1651
+ const { rewardCoinType: u, ruleIds: s, market: g, owner: m, emodeId: d } = c, v = `${u}-${g}-${m}`;
1652
+ for (const y of s) {
1653
+ a.has(v) || a.set(v, {
1654
+ assetIds: [],
1655
+ ruleIds: [],
1656
+ amount: 0,
1657
+ market: g,
1658
+ owner: m,
1659
+ isEMode: typeof d < "u"
1660
+ });
1661
+ const h = a.get(v);
1662
+ h.assetIds.push(c.assetCoinType.replace("0x", "")), h.ruleIds.push(y), h.amount += c.userClaimableReward;
1325
1663
  }
1326
1664
  }
1327
- const c = [];
1328
- for (const [s, { assetIds: u, ruleIds: i, amount: m }] of a) {
1329
- const d = n.find(
1330
- (_) => g(_.suiCoinType) === g(s)
1665
+ const i = [];
1666
+ for (const [c, { assetIds: u, ruleIds: s, amount: g, market: m, owner: d, isEMode: v }] of a) {
1667
+ const y = t.find(
1668
+ (C) => b(C.suiCoinType) === b(c) && C.market === m
1331
1669
  );
1332
- if (!d || !d.contract.rewardFundId)
1333
- throw new Error(`No matching rewardFund found for reward coin: ${s}`);
1334
- const b = d.contract.rewardFundId;
1335
- if (o?.accountCap && !o.customCoinReceive)
1670
+ if (!y || !y.contract.rewardFundId)
1671
+ throw new Error(`No matching rewardFund found for reward coin: ${c} ${m}`);
1672
+ const h = y.contract.rewardFundId;
1673
+ if (n?.accountCap && !n.customCoinReceive)
1336
1674
  throw new Error("customCoinReceive is required when accountCap is provided");
1337
- if (o?.customCoinReceive) {
1338
- let _;
1339
- o.accountCap ? _ = e.moveCall({
1675
+ if (n?.customCoinReceive) {
1676
+ let C;
1677
+ n.accountCap ? C = e.moveCall({
1340
1678
  target: `${r.package}::incentive_v3::claim_reward_with_account_cap`,
1341
1679
  arguments: [
1342
1680
  e.object("0x06"),
@@ -1345,17 +1683,36 @@ async function it(e, t, o) {
1345
1683
  // Incentive V3 contract
1346
1684
  e.object(r.storage),
1347
1685
  // Protocol storage
1348
- e.object(b),
1686
+ e.object(h),
1349
1687
  // Reward fund
1350
1688
  e.pure.vector("string", u),
1351
1689
  // Asset IDs
1352
- e.pure.vector("address", i),
1690
+ e.pure.vector("address", s),
1353
1691
  // Rule IDs
1354
- p(o.accountCap, e.object)
1692
+ f(n.accountCap, e.object)
1355
1693
  // Account capability
1356
1694
  ],
1357
- typeArguments: [s]
1358
- }) : _ = e.moveCall({
1695
+ typeArguments: [c]
1696
+ }) : v ? C = e.moveCall({
1697
+ target: `${r.package}::incentive_v3::claim_reward_with_account_cap`,
1698
+ arguments: [
1699
+ e.object("0x06"),
1700
+ // Clock object
1701
+ e.object(r.incentiveV3),
1702
+ // Incentive V3 contract
1703
+ e.object(r.storage),
1704
+ // Protocol storage
1705
+ e.object(h),
1706
+ // Reward fund
1707
+ e.pure.vector("string", u),
1708
+ // Asset IDs
1709
+ e.pure.vector("address", s),
1710
+ // Rule IDs
1711
+ f(d, e.object)
1712
+ // Account capability
1713
+ ],
1714
+ typeArguments: [c]
1715
+ }) : C = e.moveCall({
1359
1716
  target: `${r.package}::incentive_v3::claim_reward`,
1360
1717
  arguments: [
1361
1718
  e.object("0x06"),
@@ -1364,39 +1721,70 @@ async function it(e, t, o) {
1364
1721
  // Incentive V3 contract
1365
1722
  e.object(r.storage),
1366
1723
  // Protocol storage
1367
- e.object(b),
1724
+ e.object(h),
1368
1725
  // Reward fund
1369
1726
  e.pure.vector("string", u),
1370
1727
  // Asset IDs
1371
- e.pure.vector("address", i)
1728
+ e.pure.vector("address", s)
1372
1729
  // Rule IDs
1373
1730
  ],
1374
- typeArguments: [s]
1731
+ typeArguments: [c]
1375
1732
  });
1376
- const [A] = e.moveCall({
1733
+ const [T] = e.moveCall({
1377
1734
  target: "0x2::coin::from_balance",
1378
- arguments: [_],
1379
- typeArguments: [s]
1735
+ arguments: [C],
1736
+ typeArguments: [c]
1380
1737
  });
1381
- if (o?.customCoinReceive.type === "transfer") {
1382
- if (!o.customCoinReceive.transfer)
1738
+ if (n?.customCoinReceive.type === "transfer") {
1739
+ if (!n.customCoinReceive.transfer)
1383
1740
  throw new Error("customCoinReceive.transfer is required");
1384
1741
  e.transferObjects(
1385
- [A],
1386
- p(o.customCoinReceive.transfer, e.pure.address)
1742
+ [T],
1743
+ f(n.customCoinReceive.transfer, e.pure.address)
1387
1744
  );
1388
1745
  }
1389
- if (o?.customCoinReceive.type === "depositNAVI") {
1390
- const K = y(d.totalSupplyAmount).shiftedBy(-9), Y = y(d.supplyCapCeiling).shiftedBy(-27);
1391
- K.plus(m).isGreaterThan(Y) && o?.customCoinReceive.depositNAVI?.fallbackReceiveAddress ? e.transferObjects(
1392
- [A],
1393
- e.pure.address(o.customCoinReceive.depositNAVI.fallbackReceiveAddress)
1394
- ) : await de(e, d, A, o);
1746
+ if (n?.customCoinReceive.type === "depositNAVI") {
1747
+ const te = l(y.totalSupplyAmount).shiftedBy(-9), oe = l(y.supplyCapCeiling).shiftedBy(-27);
1748
+ te.plus(g).isGreaterThan(oe) && n?.customCoinReceive.depositNAVI?.fallbackReceiveAddress ? e.transferObjects(
1749
+ [T],
1750
+ e.pure.address(n.customCoinReceive.depositNAVI.fallbackReceiveAddress)
1751
+ ) : await he(e, y, T, n);
1395
1752
  } else
1396
- c.push({
1397
- coin: A,
1398
- identifier: d
1753
+ i.push({
1754
+ coin: T,
1755
+ identifier: y,
1756
+ owner: d,
1757
+ isEMode: v
1399
1758
  });
1759
+ } else if (n?.accountCap || v) {
1760
+ const C = e.moveCall({
1761
+ target: `${r.package}::incentive_v3::claim_reward_with_account_cap`,
1762
+ arguments: [
1763
+ e.object("0x06"),
1764
+ // Clock object
1765
+ e.object(r.incentiveV3),
1766
+ // Incentive V3 contract
1767
+ e.object(r.storage),
1768
+ // Protocol storage
1769
+ e.object(h),
1770
+ // Reward fund
1771
+ e.pure.vector("string", u),
1772
+ // Asset IDs
1773
+ e.pure.vector("address", s),
1774
+ // Rule IDs
1775
+ f(n?.accountCap || d, e.object)
1776
+ // Account capability
1777
+ ],
1778
+ typeArguments: [c]
1779
+ }), [T] = e.moveCall({
1780
+ target: "0x2::coin::from_balance",
1781
+ arguments: [C],
1782
+ typeArguments: [c]
1783
+ });
1784
+ e.transferObjects(
1785
+ [T],
1786
+ f(n?.accountCap || d, e.pure.address)
1787
+ );
1400
1788
  } else
1401
1789
  e.moveCall({
1402
1790
  target: `${r.package}::incentive_v3::claim_reward_entry`,
@@ -1407,79 +1795,82 @@ async function it(e, t, o) {
1407
1795
  // Incentive V3 contract
1408
1796
  e.object(r.storage),
1409
1797
  // Protocol storage
1410
- e.object(b),
1798
+ e.object(h),
1411
1799
  // Reward fund
1412
1800
  e.pure.vector("string", u),
1413
1801
  // Asset IDs
1414
- e.pure.vector("address", i)
1802
+ e.pure.vector("address", s)
1415
1803
  // Rule IDs
1416
1804
  ],
1417
- typeArguments: [s]
1805
+ typeArguments: [c]
1418
1806
  });
1419
1807
  }
1420
- return c;
1808
+ return i;
1421
1809
  }
1422
1810
  export {
1423
- R as Address,
1424
- f as DEFAULT_CACHE_TIME,
1425
- I as DEFAULT_MARKET_IDENTITY,
1426
- De as FlashLoanAssetConfig,
1427
- Ee as IncentiveAPYInfo,
1428
- oe as IncentivePoolInfo,
1429
- Ve as IncentivePoolInfoByPhase,
1430
- me as MARKETS,
1431
- fe as Market,
1432
- Ne as OracleInfo,
1433
- P as PoolOperator,
1434
- Re as ReserveDataInfo,
1435
- re as UserStateInfo,
1436
- Oe as borrowCoinPTB,
1437
- it as claimLendingRewardsPTB,
1438
- he as createAccountCapPTB,
1439
- We as createEModeCapPTB,
1440
- de as depositCoinPTB,
1441
- we as enterEModePTB,
1442
- Ge as exitEModePTB,
1443
- xe as filterPriceFeeds,
1444
- tt as flashloanPTB,
1445
- ye as getAccountCapOwnerPTB,
1446
- D as getAllFlashLoanAssets,
1447
- qe as getBorrowFee,
1448
- Qe as getCoins,
1449
- h as getConfig,
1450
- Fe as getFees,
1451
- et as getFlashLoanAsset,
1452
- Ke as getHealthFactor,
1453
- ve as getHealthFactorPTB,
1454
- Xe as getLendingPositions,
1455
- L as getLendingState,
1456
- He as getMarket,
1457
- N as getMarketConfig,
1458
- ge as getMarkets,
1459
- C as getPool,
1460
- E as getPools,
1461
- _e as getPriceFeeds,
1462
- Ce as getPythStalePriceFeedId,
1463
- Ye as getSimulatedHealthFactor,
1464
- W as getSimulatedHealthFactorPTB,
1465
- Ue as getStats,
1466
- Je as getTransactions,
1467
- nt as getUserAvailableLendingRewards,
1468
- st as getUserClaimedRewardHistory,
1469
- be as getUserEModeCaps,
1470
- ct as getUserTotalClaimedReward,
1471
- rt as liquidatePTB,
1472
- ze as mergeCoinsPTB,
1473
- g as normalizeCoinType,
1474
- pe as parsePoolUID,
1475
- p as parseTxValue,
1476
- Le as repayCoinPTB,
1477
- ot as repayFlashLoanPTB,
1478
- at as summaryLendingRewards,
1479
- Ze as updateOraclePricesPTB,
1480
- je as updatePythPriceFeeds,
1481
- j as withCache,
1482
- v as withSingleton,
1483
- Se as withdrawCoinPTB
1811
+ L as Address,
1812
+ w as DEFAULT_CACHE_TIME,
1813
+ M as DEFAULT_MARKET_IDENTITY,
1814
+ He as FlashLoanAssetConfig,
1815
+ Le as IncentiveAPYInfo,
1816
+ le as IncentivePoolInfo,
1817
+ qe as IncentivePoolInfoByPhase,
1818
+ E as MARKETS,
1819
+ je as Market,
1820
+ We as OracleInfo,
1821
+ D as PoolOperator,
1822
+ Ge as ReserveDataInfo,
1823
+ it as UserPositions,
1824
+ pe as UserStateInfo,
1825
+ Je as borrowCoinPTB,
1826
+ vt as claimLendingRewardsPTB,
1827
+ be as createAccountCapPTB,
1828
+ xe as createEModeCapPTB,
1829
+ he as depositCoinPTB,
1830
+ Ce as emodeIdentityId,
1831
+ _e as enterEModePTB,
1832
+ Ze as exitEModePTB,
1833
+ Pe as filterPriceFeeds,
1834
+ lt as flashloanPTB,
1835
+ ke as getAccountCapOwnerPTB,
1836
+ F as getAllFlashLoanAssets,
1837
+ Xe as getBorrowFee,
1838
+ ct as getCoins,
1839
+ k as getConfig,
1840
+ Ke as getFees,
1841
+ ut as getFlashLoanAsset,
1842
+ nt as getHealthFactor,
1843
+ Be as getHealthFactorPTB,
1844
+ Ae as getLendingPositions,
1845
+ ot as getLendingState,
1846
+ et as getMarket,
1847
+ j as getMarketConfig,
1848
+ Te as getMarkets,
1849
+ A as getPool,
1850
+ N as getPools,
1851
+ ee as getPriceFeeds,
1852
+ Ie as getPythStalePriceFeedId,
1853
+ rt as getSimulatedHealthFactor,
1854
+ X as getSimulatedHealthFactorPTB,
1855
+ ze as getStats,
1856
+ at as getTransactions,
1857
+ mt as getUserAvailableLendingRewards,
1858
+ yt as getUserClaimedRewardHistory,
1859
+ Q as getUserEModeCaps,
1860
+ ft as getUserTotalClaimedReward,
1861
+ dt as liquidatePTB,
1862
+ tt as mergeCoinsPTB,
1863
+ b as normalizeCoinType,
1864
+ we as parsePoolUID,
1865
+ f as parseTxValue,
1866
+ Qe as repayCoinPTB,
1867
+ pt as repayFlashLoanPTB,
1868
+ gt as summaryLendingRewards,
1869
+ st as updateOraclePriceBeforeUserOperationPTB,
1870
+ Se as updateOraclePricesPTB,
1871
+ $e as updatePythPriceFeeds,
1872
+ I as withCache,
1873
+ B as withSingleton,
1874
+ Ye as withdrawCoinPTB
1484
1875
  };
1485
1876
  //# sourceMappingURL=index.esm.js.map