@naviprotocol/wallet-client 1.2.0 → 1.3.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +140 -140
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +391 -276
- package/dist/index.esm.js.map +1 -1
- package/dist/modules/lendingModule/index.d.ts +9 -0
- package/dist/modules/lendingModule/index.d.ts.map +1 -1
- package/dist/modules/lendingModule/migrate.d.ts +2 -0
- package/dist/modules/lendingModule/migrate.d.ts.map +1 -1
- package/dist/modules/lendingModule/protocols/index.d.ts +17 -0
- package/dist/modules/lendingModule/protocols/index.d.ts.map +1 -0
- package/dist/modules/lendingModule/protocols/navi.d.ts +17 -0
- package/dist/modules/lendingModule/protocols/navi.d.ts.map +1 -0
- package/dist/modules/lendingModule/protocols/suilend.d.ts +23 -0
- package/dist/modules/lendingModule/protocols/suilend.d.ts.map +1 -0
- package/package.json +6 -3
package/dist/index.esm.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { getFullnodeUrl as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { withSingleton as
|
|
5
|
-
import
|
|
6
|
-
import { normalizeStructTag as
|
|
7
|
-
import { Transaction as
|
|
8
|
-
import { buildSwapPTBFromQuote as
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
1
|
+
import { getFullnodeUrl as de, SuiClient as we } from "@mysten/sui/client";
|
|
2
|
+
import he from "mitt";
|
|
3
|
+
import fe from "axios";
|
|
4
|
+
import { withSingleton as Q, getCoins as ge, mergeCoinsPTB as T, getPriceFeeds as K, updateOraclePricesPTB as $, filterPriceFeeds as q, getFlashLoanAsset as te, flashloanPTB as ae, depositCoinPTB as N, repayFlashLoanPTB as ne, borrowCoinPTB as se, withdrawCoinPTB as ie, repayCoinPTB as le, getPools as me, getPool as v, getHealthFactor as Ce, liquidatePTB as be, getUserAvailableLendingRewards as J, getUserClaimedRewardHistory as ye, claimLendingRewardsPTB as pe, getLendingState as Pe, createAccountCapPTB as Be, withCache as re, parseTxValue as _ } from "@naviprotocol/lending";
|
|
5
|
+
import R, { BigNumber as L } from "bignumber.js";
|
|
6
|
+
import { normalizeStructTag as P } from "@mysten/sui/utils";
|
|
7
|
+
import { Transaction as m } from "@mysten/sui/transactions";
|
|
8
|
+
import { buildSwapPTBFromQuote as G, generateRefId as Te, getQuote as ve } from "@naviprotocol/astros-aggregator-sdk";
|
|
9
|
+
import { SuilendClient as V, LENDING_MARKET_ID as Ee, LENDING_MARKET_TYPE as X, parseReserve as Re, parseObligation as Se } from "@suilend/sdk";
|
|
10
|
+
import { getCoinMetadataMap as Ae, MAX_U64 as Oe } from "@suilend/sui-fe";
|
|
11
|
+
import { executeAuction as xe } from "shio-sdk";
|
|
12
|
+
import { Signer as oe } from "@mysten/sui/cryptography";
|
|
13
|
+
const ce = fe.create({
|
|
12
14
|
timeout: 2e4
|
|
13
15
|
});
|
|
14
|
-
|
|
16
|
+
ce.interceptors.response.use(
|
|
15
17
|
function(r) {
|
|
16
18
|
if (r.data.err)
|
|
17
19
|
throw new Error(r.data.err);
|
|
@@ -23,7 +25,7 @@ se.interceptors.response.use(
|
|
|
23
25
|
return Promise.reject(r);
|
|
24
26
|
}
|
|
25
27
|
);
|
|
26
|
-
class
|
|
28
|
+
class ke {
|
|
27
29
|
/**
|
|
28
30
|
* Creates a new custom transport instance
|
|
29
31
|
*
|
|
@@ -42,7 +44,7 @@ class Be {
|
|
|
42
44
|
* @returns Promise<T> - The response data from the RPC call
|
|
43
45
|
*/
|
|
44
46
|
async request(e) {
|
|
45
|
-
return this.requestId += 1, (await
|
|
47
|
+
return this.requestId += 1, (await ce.post(this.rpcUrl, {
|
|
46
48
|
jsonrpc: "2.0",
|
|
47
49
|
id: this.requestId,
|
|
48
50
|
method: e.method,
|
|
@@ -62,7 +64,7 @@ class Be {
|
|
|
62
64
|
throw new Error("subscribe not implemented.");
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
class
|
|
67
|
+
class O {
|
|
66
68
|
/**
|
|
67
69
|
* Gets the current configuration for this module
|
|
68
70
|
*
|
|
@@ -105,23 +107,23 @@ class R {
|
|
|
105
107
|
this.walletClient = void 0;
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
|
-
class
|
|
110
|
+
class A {
|
|
109
111
|
/**
|
|
110
112
|
* Creates a new user portfolio
|
|
111
113
|
*
|
|
112
114
|
* @param coins - Array of coin objects or existing portfolio to copy from
|
|
113
115
|
*/
|
|
114
116
|
constructor(e) {
|
|
115
|
-
if (e instanceof
|
|
117
|
+
if (e instanceof A)
|
|
116
118
|
this.balances = JSON.parse(JSON.stringify(e.balances));
|
|
117
119
|
else {
|
|
118
120
|
const a = {};
|
|
119
121
|
e == null || e.forEach((t) => {
|
|
120
|
-
const s =
|
|
122
|
+
const s = P(t.coinType);
|
|
121
123
|
let n = a[s];
|
|
122
124
|
n || (n = {
|
|
123
125
|
coinType: s,
|
|
124
|
-
amount:
|
|
126
|
+
amount: L(0),
|
|
125
127
|
coins: []
|
|
126
128
|
}, a[s] = n), n.coins.push(t), n.amount = n.amount.plus(t.balance);
|
|
127
129
|
}), this.balances = a;
|
|
@@ -134,7 +136,7 @@ class E {
|
|
|
134
136
|
* @returns UserCoinBalance object with coin type, total amount, and coin objects
|
|
135
137
|
*/
|
|
136
138
|
getBalance(e) {
|
|
137
|
-
return e =
|
|
139
|
+
return e = P(e), this.balances[e] || { coinType: e, amount: L(0), coins: [] };
|
|
138
140
|
}
|
|
139
141
|
/**
|
|
140
142
|
* Gets all coin types in the portfolio
|
|
@@ -151,9 +153,9 @@ class E {
|
|
|
151
153
|
* @returns BigNumber representing the total balance
|
|
152
154
|
*/
|
|
153
155
|
balanceOf(e) {
|
|
154
|
-
e =
|
|
156
|
+
e = P(e);
|
|
155
157
|
const a = this.getBalance(e);
|
|
156
|
-
return (a == null ? void 0 : a.amount) ||
|
|
158
|
+
return (a == null ? void 0 : a.amount) || L(0);
|
|
157
159
|
}
|
|
158
160
|
/**
|
|
159
161
|
* Creates a deep copy of the portfolio
|
|
@@ -164,22 +166,22 @@ class E {
|
|
|
164
166
|
* @returns New UserPortfolio instance with copied data
|
|
165
167
|
*/
|
|
166
168
|
clone() {
|
|
167
|
-
const e = JSON.parse(JSON.stringify(this.balances)), a = new
|
|
169
|
+
const e = JSON.parse(JSON.stringify(this.balances)), a = new A();
|
|
168
170
|
return a.balances = e, a;
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
|
-
class
|
|
173
|
+
class Ne extends O {
|
|
172
174
|
constructor() {
|
|
173
175
|
super(...arguments), this.name = "balance", this.defaultConfig = {
|
|
174
176
|
coinPollingInterval: 6e3,
|
|
175
177
|
disableCoinPolling: !1
|
|
176
|
-
}, this.updatePortfolio =
|
|
178
|
+
}, this.updatePortfolio = Q(async () => {
|
|
177
179
|
if (!this.walletClient)
|
|
178
180
|
throw new Error("Wallet client not found");
|
|
179
|
-
this._coins = await
|
|
181
|
+
this._coins = await ge(this.walletClient.address, {
|
|
180
182
|
client: this.walletClient.client
|
|
181
|
-
}), this._portfolio = new
|
|
182
|
-
}), this._coins = [], this._portfolio = new
|
|
183
|
+
}), this._portfolio = new A(this._coins), this.emit("balance:portfolio-updated", {});
|
|
184
|
+
}), this._coins = [], this._portfolio = new A(), this.pollingTimer = null, this._updatePromise = null;
|
|
183
185
|
}
|
|
184
186
|
/**
|
|
185
187
|
* Gets the current coin objects from the wallet
|
|
@@ -230,26 +232,26 @@ class Pe extends R {
|
|
|
230
232
|
* @returns Transaction response or dry run response
|
|
231
233
|
*/
|
|
232
234
|
async sendCoinBatch(e, a, t, s) {
|
|
233
|
-
var
|
|
235
|
+
var h;
|
|
234
236
|
if (!this.walletClient)
|
|
235
237
|
throw new Error("Wallet client not found");
|
|
236
238
|
if (a.length !== t.length)
|
|
237
239
|
throw new Error("Recipients and amounts must have the same length");
|
|
238
240
|
await this.waitForUpdate();
|
|
239
|
-
const n = this.portfolio.getBalance(e), c = t.reduce((
|
|
241
|
+
const n = this.portfolio.getBalance(e), c = t.reduce((g, f) => g.plus(f), R(0));
|
|
240
242
|
if (n.amount.lt(c))
|
|
241
243
|
throw new Error("Insufficient balance");
|
|
242
|
-
const
|
|
243
|
-
let
|
|
244
|
-
|
|
244
|
+
const i = new m();
|
|
245
|
+
let l = i.gas;
|
|
246
|
+
P(e) !== P("0x2::sui::SUI") && (l = T(i, n.coins, {
|
|
245
247
|
balance: c.toNumber()
|
|
246
248
|
}));
|
|
247
|
-
const
|
|
248
|
-
return a.forEach((
|
|
249
|
-
|
|
249
|
+
const u = i.splitCoins(l, t);
|
|
250
|
+
return a.forEach((g, f) => {
|
|
251
|
+
i.transferObjects([u[f]], g);
|
|
250
252
|
}), await this.walletClient.signExecuteTransaction({
|
|
251
|
-
transaction:
|
|
252
|
-
dryRun: (
|
|
253
|
+
transaction: i,
|
|
254
|
+
dryRun: (h = s == null ? void 0 : s.dryRun) != null ? h : !1
|
|
253
255
|
});
|
|
254
256
|
}
|
|
255
257
|
/**
|
|
@@ -281,9 +283,9 @@ class Pe extends R {
|
|
|
281
283
|
if (e.length !== a.length)
|
|
282
284
|
throw new Error("Objects and recipients must have the same length");
|
|
283
285
|
await this.waitForUpdate();
|
|
284
|
-
const s = new
|
|
285
|
-
return e.forEach((
|
|
286
|
-
s.transferObjects([s.object(
|
|
286
|
+
const s = new m();
|
|
287
|
+
return e.forEach((i, l) => {
|
|
288
|
+
s.transferObjects([s.object(i)], a[l]);
|
|
287
289
|
}), await this.walletClient.signExecuteTransaction({
|
|
288
290
|
transaction: s,
|
|
289
291
|
dryRun: (c = t == null ? void 0 : t.dryRun) != null ? c : !1
|
|
@@ -327,184 +329,283 @@ class Pe extends R {
|
|
|
327
329
|
super.uninstall(), this.pollingTimer && (clearTimeout(this.pollingTimer), this.pollingTimer = null);
|
|
328
330
|
}
|
|
329
331
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
return r ? new v(r.toString()).shiftedBy(-1 * Te).multipliedBy(100).decimalPlaces(e, v.ROUND_DOWN).toFixed(e) : "0.00";
|
|
333
|
-
}
|
|
334
|
-
async function D(r, e, a, t) {
|
|
335
|
-
var S, O;
|
|
332
|
+
async function Y(r, e, a, t) {
|
|
333
|
+
var x, k, S;
|
|
336
334
|
if (!this.walletClient)
|
|
337
335
|
throw new Error("Wallet client not found");
|
|
338
336
|
await this.walletClient.balance.updatePortfolio();
|
|
339
|
-
const s = await this.getPool(e), n = await this.getPool(a), c = await this.getLendingState(), l =
|
|
337
|
+
const s = await this.getPool(e), n = await this.getPool(a), c = await this.getLendingState(), i = this.walletClient.address, l = (x = t == null ? void 0 : t.protocol) != null ? x : "navi", u = await this.getProtocol(l);
|
|
338
|
+
if (!u)
|
|
339
|
+
throw new Error(`Protocol ${l} not found`);
|
|
340
|
+
const o = await u.getPool(s.coinType);
|
|
340
341
|
if (console.log("migrateBetweenSupplyPTB", {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
fromPool: s,
|
|
344
|
-
toPool: n,
|
|
345
|
-
address: i
|
|
346
|
-
}), !l || l.supplyBalance === "0")
|
|
342
|
+
fromPoolLending: o
|
|
343
|
+
}), l === "navi" && o.supplyBalance === 0)
|
|
347
344
|
throw new Error("No supply balance");
|
|
348
|
-
if (t != null && t.amount && t.amount >
|
|
345
|
+
if (t != null && t.amount && t.amount > o.supplyBalance)
|
|
349
346
|
throw new Error("Amount is less than supply balance");
|
|
350
|
-
const
|
|
351
|
-
await
|
|
347
|
+
const h = await K(), g = await Promise.all([s, n]);
|
|
348
|
+
await $(
|
|
352
349
|
r,
|
|
353
|
-
|
|
350
|
+
q(h, {
|
|
354
351
|
lendingState: c,
|
|
355
|
-
pools:
|
|
352
|
+
pools: g
|
|
356
353
|
})
|
|
357
354
|
);
|
|
358
|
-
const
|
|
355
|
+
const f = await te(n, {
|
|
359
356
|
env: this.config.env
|
|
360
357
|
});
|
|
361
|
-
if (!
|
|
358
|
+
if (!f)
|
|
362
359
|
throw new Error(`${n.token.symbol} pool not support flashloan`);
|
|
363
|
-
const
|
|
360
|
+
const b = (k = t == null ? void 0 : t.slippage) != null ? k : 5e-3, y = (S = t == null ? void 0 : t.amount) != null ? S : Number(o.supplyBalance), d = await this.walletClient.swap.getQuote(
|
|
364
361
|
s.suiCoinType,
|
|
365
362
|
n.suiCoinType,
|
|
366
|
-
|
|
367
|
-
),
|
|
368
|
-
if (
|
|
363
|
+
y
|
|
364
|
+
), w = Number(d.amount_out);
|
|
365
|
+
if (w <= 0)
|
|
369
366
|
throw new Error("The amount of swapAmountOut must be greater than 0.");
|
|
370
|
-
const
|
|
371
|
-
if (
|
|
367
|
+
const C = Math.floor(w * (1 - b)), p = Math.floor(C / (1 + f.flashloanFee));
|
|
368
|
+
if (p <= 0)
|
|
372
369
|
throw new Error("The calculated borrow amount must be positive.");
|
|
373
|
-
const [
|
|
370
|
+
const [E, B] = await ae(r, n, p), [F] = r.moveCall({
|
|
374
371
|
target: "0x2::coin::from_balance",
|
|
375
|
-
arguments: [
|
|
372
|
+
arguments: [E],
|
|
376
373
|
typeArguments: [n.suiCoinType]
|
|
377
374
|
});
|
|
378
|
-
await
|
|
379
|
-
amount:
|
|
375
|
+
await N(r, n, F, {
|
|
376
|
+
amount: p
|
|
380
377
|
});
|
|
381
|
-
const j = await
|
|
378
|
+
const j = await u.withdrawCoinPTB(r, s.suiCoinType, y), I = await G(
|
|
382
379
|
i,
|
|
383
380
|
r,
|
|
384
|
-
|
|
381
|
+
C,
|
|
385
382
|
j,
|
|
386
|
-
|
|
383
|
+
d
|
|
387
384
|
), U = r.moveCall({
|
|
388
385
|
target: "0x2::coin::into_balance",
|
|
389
|
-
arguments: [
|
|
386
|
+
arguments: [I],
|
|
390
387
|
typeArguments: [n.suiCoinType]
|
|
391
|
-
}), [
|
|
388
|
+
}), [M] = await ne(r, n, B, U), [W] = r.moveCall({
|
|
392
389
|
target: "0x2::coin::from_balance",
|
|
393
|
-
arguments: [
|
|
390
|
+
arguments: [M],
|
|
394
391
|
typeArguments: [n.suiCoinType]
|
|
395
392
|
});
|
|
396
|
-
return await
|
|
393
|
+
return await N(r, n, W), r;
|
|
397
394
|
}
|
|
398
|
-
async function
|
|
399
|
-
var
|
|
395
|
+
async function Z(r, e, a, t) {
|
|
396
|
+
var S, H, D;
|
|
400
397
|
if (!this.walletClient)
|
|
401
398
|
throw new Error("Wallet client not found");
|
|
402
399
|
await this.walletClient.balance.updatePortfolio();
|
|
403
|
-
const s = this.walletClient.address, n = await this.getPool(e), c = await this.getPool(a),
|
|
400
|
+
const s = this.walletClient.address, n = await this.getPool(e), c = await this.getPool(a), i = await this.getLendingState(), l = (S = t == null ? void 0 : t.protocol) != null ? S : "navi", u = await this.getProtocol(l);
|
|
401
|
+
if (!u)
|
|
402
|
+
throw new Error(`Protocol ${l} not found`);
|
|
403
|
+
const o = await u.getPool(n.coinType);
|
|
404
404
|
if (console.log("migrateBetweenBorrowPTB", {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
fromPool: n,
|
|
408
|
-
toPool: c,
|
|
409
|
-
address: s
|
|
410
|
-
}), !i || i.borrowBalance === "0")
|
|
405
|
+
fromPoolLending: o
|
|
406
|
+
}), o.borrowBalance === 0)
|
|
411
407
|
throw new Error("No borrow balance");
|
|
412
|
-
if (t != null && t.amount && t.amount >
|
|
413
|
-
throw new Error("Amount is
|
|
414
|
-
const
|
|
415
|
-
await
|
|
408
|
+
if (t != null && t.amount && t.amount > o.borrowBalance)
|
|
409
|
+
throw new Error("Amount is more than borrow balance");
|
|
410
|
+
const h = await K(), g = await Promise.all([n, c]);
|
|
411
|
+
await $(
|
|
416
412
|
r,
|
|
417
|
-
|
|
418
|
-
lendingState:
|
|
419
|
-
pools:
|
|
413
|
+
q(h, {
|
|
414
|
+
lendingState: i,
|
|
415
|
+
pools: g
|
|
420
416
|
})
|
|
421
417
|
);
|
|
422
|
-
let
|
|
423
|
-
if (Number(
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
new
|
|
418
|
+
let f = (H = t == null ? void 0 : t.amount) != null ? H : Number(o.borrowBalance);
|
|
419
|
+
if (Number(o.borrowBalance) === f) {
|
|
420
|
+
const ue = o.borrowAPR / 100;
|
|
421
|
+
f = Math.ceil(
|
|
422
|
+
new R(f).multipliedBy(ue * (60 * 3) / (365 * 24 * 3600)).plus(new R(1).shiftedBy(-n.token.decimals)).plus(f).toNumber()
|
|
427
423
|
);
|
|
428
424
|
}
|
|
429
|
-
const
|
|
425
|
+
const b = await te(n, {
|
|
430
426
|
env: this.config.env
|
|
431
427
|
});
|
|
432
|
-
if (!
|
|
428
|
+
if (!b)
|
|
433
429
|
throw new Error(`${n.token.symbol} pool not support flashloan`);
|
|
434
430
|
if (!this.walletClient)
|
|
435
431
|
throw new Error("Wallet client not found");
|
|
436
|
-
const
|
|
432
|
+
const y = (D = t == null ? void 0 : t.slippage) != null ? D : 5e-3, d = Math.ceil(f * (1 + b.flashloanFee)), w = d, C = await this.walletClient.swap.getQuote(
|
|
437
433
|
n.suiCoinType,
|
|
438
434
|
c.suiCoinType,
|
|
439
|
-
|
|
435
|
+
w
|
|
440
436
|
);
|
|
441
|
-
let
|
|
437
|
+
let p = 1 + y, E = Math.ceil(Number(C.amount_out) * p), B = await this.walletClient.swap.getQuote(
|
|
442
438
|
c.suiCoinType,
|
|
443
439
|
n.suiCoinType,
|
|
444
|
-
|
|
440
|
+
E
|
|
445
441
|
);
|
|
446
|
-
for (
|
|
447
|
-
|
|
442
|
+
for (console.log("migrateBetweenBorrowPTB", {
|
|
443
|
+
toQuote: B,
|
|
444
|
+
minSwapAmount: w,
|
|
445
|
+
toQuoteAmount: Number(B.amount_out),
|
|
446
|
+
rate: p
|
|
447
|
+
}); Number(B.amount_out) < w; )
|
|
448
|
+
p = p * Math.max(w / Number(B.amount_out), 1.001), E = Math.ceil(Number(C.amount_out) * p), B = await this.walletClient.swap.getQuote(
|
|
448
449
|
c.suiCoinType,
|
|
449
450
|
n.suiCoinType,
|
|
450
|
-
|
|
451
|
-
)
|
|
452
|
-
|
|
451
|
+
E
|
|
452
|
+
), console.log("migrateBetweenBorrowPTB", {
|
|
453
|
+
toQuote: B,
|
|
454
|
+
minSwapAmount: w,
|
|
455
|
+
toQuoteAmount: Number(B.amount_out),
|
|
456
|
+
rate: p
|
|
457
|
+
});
|
|
458
|
+
const [F, j] = await ae(r, n, f), [I] = r.moveCall({
|
|
453
459
|
target: "0x2::coin::from_balance",
|
|
454
460
|
arguments: [F],
|
|
455
461
|
typeArguments: [n.suiCoinType]
|
|
456
462
|
});
|
|
457
|
-
await
|
|
458
|
-
|
|
459
|
-
});
|
|
460
|
-
const U = await te(r, c, h), W = await $(
|
|
463
|
+
await u.repayCoinPTB(r, n.suiCoinType, I, f);
|
|
464
|
+
const U = await se(r, c, E), M = await G(
|
|
461
465
|
s,
|
|
462
466
|
r,
|
|
463
|
-
|
|
467
|
+
d,
|
|
464
468
|
U,
|
|
465
|
-
|
|
466
|
-
),
|
|
469
|
+
B
|
|
470
|
+
), W = r.moveCall({
|
|
467
471
|
target: "0x2::coin::into_balance",
|
|
468
|
-
arguments: [
|
|
472
|
+
arguments: [M],
|
|
469
473
|
typeArguments: [n.suiCoinType]
|
|
470
|
-
}), [
|
|
474
|
+
}), [x] = await ne(r, n, j, W), [k] = r.moveCall({
|
|
471
475
|
target: "0x2::coin::from_balance",
|
|
472
|
-
arguments: [
|
|
476
|
+
arguments: [x],
|
|
473
477
|
typeArguments: [n.suiCoinType]
|
|
474
478
|
});
|
|
475
|
-
return r.transferObjects([
|
|
479
|
+
return r.transferObjects([k], s), r;
|
|
476
480
|
}
|
|
477
|
-
async function
|
|
478
|
-
var
|
|
481
|
+
async function ee(r, e, a, t) {
|
|
482
|
+
var u, o;
|
|
479
483
|
if (!this.walletClient)
|
|
480
484
|
throw new Error("Wallet client not found");
|
|
481
485
|
await this.walletClient.balance.updatePortfolio(), this.walletClient.address;
|
|
482
486
|
const s = await this.getPool(a), n = this.walletClient.balance.portfolio.getBalance(e);
|
|
483
487
|
if (n.amount.eq(0))
|
|
484
488
|
throw new Error(`No balance of ${e}`);
|
|
485
|
-
if (
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
+
if (console.log("migrateBalanceToSupplyPTB", {
|
|
490
|
+
balance: n.amount.toNumber()
|
|
491
|
+
}), t != null && t.amount && n.amount.lt(t.amount))
|
|
492
|
+
throw new Error("Amount is greater than balance");
|
|
493
|
+
const c = (u = t == null ? void 0 : t.amount) != null ? u : n.amount.toNumber(), i = (o = t == null ? void 0 : t.slippage) != null ? o : 5e-3;
|
|
494
|
+
let l = await T(r, n.coins, {
|
|
489
495
|
balance: c,
|
|
490
496
|
useGasCoin: !0
|
|
491
497
|
});
|
|
492
|
-
if (
|
|
493
|
-
const
|
|
494
|
-
|
|
498
|
+
if (P(e) !== P(s.suiCoinType)) {
|
|
499
|
+
const h = await this.walletClient.swap.getQuote(e, s.suiCoinType, c);
|
|
500
|
+
l = await this.walletClient.swap.buildSwapPTBFromQuote(
|
|
495
501
|
r,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
502
|
+
h,
|
|
503
|
+
l,
|
|
504
|
+
i
|
|
499
505
|
);
|
|
500
506
|
}
|
|
501
|
-
return await
|
|
507
|
+
return await N(r, s, l), r;
|
|
502
508
|
}
|
|
503
|
-
class
|
|
509
|
+
class _e {
|
|
510
|
+
constructor(e) {
|
|
511
|
+
this.name = "navi", this.walletClient = e;
|
|
512
|
+
}
|
|
513
|
+
async getPool(e) {
|
|
514
|
+
const a = await this.walletClient.lending.getPool(e), s = (await this.walletClient.lending.getLendingState()).find(
|
|
515
|
+
(i) => i.pool.suiCoinType === P(e)
|
|
516
|
+
), n = s ? new R(s.supplyBalance).toNumber() : 0, c = s ? new R(s.borrowBalance).toNumber() : 0;
|
|
517
|
+
return {
|
|
518
|
+
supplyBalance: n,
|
|
519
|
+
borrowBalance: c,
|
|
520
|
+
borrowAPR: parseFloat(a.borrowIncentiveApyInfo.apy)
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
async depositCoinPTB(e, a, t, s) {
|
|
524
|
+
throw new Error("Not implemented");
|
|
525
|
+
}
|
|
526
|
+
async withdrawCoinPTB(e, a, t) {
|
|
527
|
+
return ie(e, a, t);
|
|
528
|
+
}
|
|
529
|
+
async borrowCoinPTB(e, a, t) {
|
|
530
|
+
throw new Error("Not implemented");
|
|
531
|
+
}
|
|
532
|
+
async repayCoinPTB(e, a, t, s) {
|
|
533
|
+
le(e, a, t, { amount: s });
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
class z {
|
|
537
|
+
constructor(e, a, t) {
|
|
538
|
+
this.name = "suilend", this.walletClient = e, this.obligationOwnerCap = a, this.suilendClient = t;
|
|
539
|
+
}
|
|
540
|
+
static async create(e) {
|
|
541
|
+
const [a, t] = await Promise.all([
|
|
542
|
+
V.initialize(Ee, X, e.client),
|
|
543
|
+
V.getObligationOwnerCaps(
|
|
544
|
+
e.address,
|
|
545
|
+
[X],
|
|
546
|
+
e.client
|
|
547
|
+
)
|
|
548
|
+
]);
|
|
549
|
+
if (t.length === 0)
|
|
550
|
+
throw new Error("No existing obligations found for wallet address: " + e.address);
|
|
551
|
+
return new z(e, t[0], a);
|
|
552
|
+
}
|
|
553
|
+
async getPool(e) {
|
|
554
|
+
var b, y;
|
|
555
|
+
const a = P(e), t = this.suilendClient.lendingMarket.reserves, s = /* @__PURE__ */ new Set();
|
|
556
|
+
t.forEach((d) => {
|
|
557
|
+
s.add(P(d.coinType.name)), d.depositsPoolRewardManager.poolRewards.forEach((w) => {
|
|
558
|
+
w && s.add(P(w.coinType.name));
|
|
559
|
+
}), d.borrowsPoolRewardManager.poolRewards.forEach((w) => {
|
|
560
|
+
w && s.add(P(w.coinType.name));
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
const n = await Ae(Array.from(s)), i = t.map((d) => Re(d, n)).reduce(
|
|
564
|
+
(d, w) => (d[w.coinType] = w, d),
|
|
565
|
+
{}
|
|
566
|
+
), l = await this.suilendClient.getObligation(
|
|
567
|
+
this.obligationOwnerCap.obligationId
|
|
568
|
+
), u = Se(l, i), o = u.deposits.find((d) => d.coinType === a), h = u.borrows.find((d) => d.coinType === a), g = n[a].decimals;
|
|
569
|
+
return {
|
|
570
|
+
cTokenExchangeRate: i[a].cTokenExchangeRate.toNumber(),
|
|
571
|
+
supplyBalance: (b = o == null ? void 0 : o.depositedAmount.shiftedBy(g).decimalPlaces(0).toNumber()) != null ? b : 0,
|
|
572
|
+
borrowBalance: (y = h == null ? void 0 : h.borrowedAmount.shiftedBy(g).decimalPlaces(0).toNumber()) != null ? y : 0,
|
|
573
|
+
borrowAPR: i[a].borrowAprPercent.toNumber()
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
async depositCoinPTB(e, a, t, s) {
|
|
577
|
+
throw new Error("Not implemented");
|
|
578
|
+
}
|
|
579
|
+
async withdrawCoinPTB(e, a, t) {
|
|
580
|
+
const s = await this.getPool(a), n = t === s.supplyBalance;
|
|
581
|
+
return await this.suilendClient.withdraw(
|
|
582
|
+
this.obligationOwnerCap.id,
|
|
583
|
+
this.obligationOwnerCap.obligationId,
|
|
584
|
+
a,
|
|
585
|
+
n ? Oe.toString() : Math.ceil(t / s.cTokenExchangeRate).toString(),
|
|
586
|
+
e
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
async borrowCoinPTB(e, a, t) {
|
|
590
|
+
throw new Error("Not implemented");
|
|
591
|
+
}
|
|
592
|
+
async repayCoinPTB(e, a, t, s) {
|
|
593
|
+
this.suilendClient.repay(this.obligationOwnerCap.obligationId, a, t, e), e.transferObjects([t], e.pure.address(this.walletClient.address));
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
class Fe extends O {
|
|
504
597
|
constructor() {
|
|
505
598
|
super(), this.name = "lending", this.defaultConfig = {
|
|
506
|
-
env: "prod"
|
|
507
|
-
|
|
599
|
+
env: "prod",
|
|
600
|
+
protocols: []
|
|
601
|
+
}, this._protocols = [], this.migrateBetweenSupplyPTB = Y, this.migrateBetweenBorrowPTB = Z, this.migrateBalanceToSupplyPTB = ee, this.migrateBetweenSupplyPTB = Y.bind(this), this.migrateBetweenBorrowPTB = Z.bind(this), this.migrateBalanceToSupplyPTB = ee.bind(this);
|
|
602
|
+
}
|
|
603
|
+
/** Get protocols (either from internal storage or config) */
|
|
604
|
+
get protocols() {
|
|
605
|
+
return this._protocols.length > 0 ? this._protocols : this.config.protocols;
|
|
606
|
+
}
|
|
607
|
+
async getProtocol(e) {
|
|
608
|
+
return (!this.protocols || this.protocols.length === 0) && await this.initializeProtocols(), this.protocols.find((a) => a.name === e);
|
|
508
609
|
}
|
|
509
610
|
/**
|
|
510
611
|
* Gets all available lending pools
|
|
@@ -513,7 +614,7 @@ class ve extends R {
|
|
|
513
614
|
* @returns Array of pool information
|
|
514
615
|
*/
|
|
515
616
|
async getPools(e) {
|
|
516
|
-
return await
|
|
617
|
+
return await me({
|
|
517
618
|
env: this.config.env,
|
|
518
619
|
...e
|
|
519
620
|
});
|
|
@@ -526,7 +627,7 @@ class ve extends R {
|
|
|
526
627
|
* @returns Pool information
|
|
527
628
|
*/
|
|
528
629
|
async getPool(e, a) {
|
|
529
|
-
return await
|
|
630
|
+
return await v(e, {
|
|
530
631
|
env: this.config.env,
|
|
531
632
|
...a
|
|
532
633
|
});
|
|
@@ -540,30 +641,30 @@ class ve extends R {
|
|
|
540
641
|
* @returns Transaction response or dry run response
|
|
541
642
|
*/
|
|
542
643
|
async deposit(e, a, t) {
|
|
543
|
-
var
|
|
644
|
+
var u, o, h;
|
|
544
645
|
if (!this.walletClient)
|
|
545
646
|
throw new Error("Wallet client not found");
|
|
546
|
-
const s = await
|
|
647
|
+
const s = await v(e, {
|
|
547
648
|
env: this.config.env
|
|
548
649
|
});
|
|
549
650
|
await this.walletClient.module("balance").waitForUpdate();
|
|
550
|
-
const n = new
|
|
651
|
+
const n = new m(), c = this.walletClient.module("balance").portfolio.getBalance(s.suiCoinType), i = T(n, c.coins, {
|
|
551
652
|
balance: a,
|
|
552
653
|
useGasCoin: !0
|
|
553
654
|
});
|
|
554
|
-
await
|
|
655
|
+
await N(n, s, i, {
|
|
555
656
|
env: this.config.env,
|
|
556
657
|
amount: a,
|
|
557
658
|
accountCap: t == null ? void 0 : t.accountCap
|
|
558
659
|
});
|
|
559
|
-
const
|
|
660
|
+
const l = await this.walletClient.signExecuteTransaction({
|
|
560
661
|
transaction: n,
|
|
561
|
-
dryRun: (
|
|
662
|
+
dryRun: (u = t == null ? void 0 : t.dryRun) != null ? u : !1
|
|
562
663
|
});
|
|
563
|
-
return !(t != null && t.dryRun) && ((
|
|
664
|
+
return !(t != null && t.dryRun) && ((h = (o = l.effects) == null ? void 0 : o.status) == null ? void 0 : h.status) === "success" && (this.emit("lending:deposit-success", {
|
|
564
665
|
identifier: e,
|
|
565
666
|
amount: a
|
|
566
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
667
|
+
}), this.walletClient.module("balance").updatePortfolio()), l;
|
|
567
668
|
}
|
|
568
669
|
/**
|
|
569
670
|
* Withdraws coins from a lending pool
|
|
@@ -574,26 +675,26 @@ class ve extends R {
|
|
|
574
675
|
* @returns Transaction response or dry run response
|
|
575
676
|
*/
|
|
576
677
|
async withdraw(e, a, t) {
|
|
577
|
-
var
|
|
678
|
+
var l, u, o;
|
|
578
679
|
if (!this.walletClient)
|
|
579
680
|
throw new Error("Wallet client not found");
|
|
580
|
-
const s = await
|
|
681
|
+
const s = await v(e, {
|
|
581
682
|
env: this.config.env
|
|
582
|
-
}), n = new
|
|
683
|
+
}), n = new m();
|
|
583
684
|
t != null && t.disableUpdateOracle || await this.updateOraclePTB(n, [s]);
|
|
584
|
-
const c = await
|
|
685
|
+
const c = await ie(n, s, a, {
|
|
585
686
|
env: this.config.env,
|
|
586
687
|
accountCap: t == null ? void 0 : t.accountCap
|
|
587
688
|
});
|
|
588
689
|
n.transferObjects([c], this.walletClient.address);
|
|
589
|
-
const
|
|
690
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
590
691
|
transaction: n,
|
|
591
|
-
dryRun: (
|
|
692
|
+
dryRun: (l = t == null ? void 0 : t.dryRun) != null ? l : !1
|
|
592
693
|
});
|
|
593
|
-
return !(t != null && t.dryRun) && ((
|
|
694
|
+
return !(t != null && t.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("lending:withdraw-success", {
|
|
594
695
|
identifier: e,
|
|
595
696
|
amount: a
|
|
596
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
697
|
+
}), this.walletClient.module("balance").updatePortfolio()), i;
|
|
597
698
|
}
|
|
598
699
|
/**
|
|
599
700
|
* Borrows coins from a lending pool
|
|
@@ -604,26 +705,26 @@ class ve extends R {
|
|
|
604
705
|
* @returns Transaction response or dry run response
|
|
605
706
|
*/
|
|
606
707
|
async borrow(e, a, t) {
|
|
607
|
-
var
|
|
708
|
+
var l, u, o;
|
|
608
709
|
if (!this.walletClient)
|
|
609
710
|
throw new Error("Wallet client not found");
|
|
610
|
-
const s = await
|
|
711
|
+
const s = await v(e, {
|
|
611
712
|
env: this.config.env
|
|
612
|
-
}), n = new
|
|
713
|
+
}), n = new m();
|
|
613
714
|
t != null && t.disableUpdateOracle || await this.updateOraclePTB(n, [s]);
|
|
614
|
-
const c = await
|
|
715
|
+
const c = await se(n, s, a, {
|
|
615
716
|
env: this.config.env,
|
|
616
717
|
accountCap: t == null ? void 0 : t.accountCap
|
|
617
718
|
});
|
|
618
719
|
n.transferObjects([c], this.walletClient.address);
|
|
619
|
-
const
|
|
720
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
620
721
|
transaction: n,
|
|
621
|
-
dryRun: (
|
|
722
|
+
dryRun: (l = t == null ? void 0 : t.dryRun) != null ? l : !1
|
|
622
723
|
});
|
|
623
|
-
return !(t != null && t.dryRun) && ((
|
|
724
|
+
return !(t != null && t.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("lending:borrow-success", {
|
|
624
725
|
identifier: e,
|
|
625
726
|
amount: a
|
|
626
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
727
|
+
}), this.walletClient.module("balance").updatePortfolio()), i;
|
|
627
728
|
}
|
|
628
729
|
/**
|
|
629
730
|
* Repays borrowed coins to a lending pool
|
|
@@ -634,29 +735,29 @@ class ve extends R {
|
|
|
634
735
|
* @returns Transaction response or dry run response
|
|
635
736
|
*/
|
|
636
737
|
async repay(e, a, t) {
|
|
637
|
-
var
|
|
738
|
+
var u, o, h;
|
|
638
739
|
if (!this.walletClient)
|
|
639
740
|
throw new Error("Wallet client not found");
|
|
640
741
|
await this.walletClient.module("balance").waitForUpdate();
|
|
641
|
-
const s = await
|
|
742
|
+
const s = await v(e, {
|
|
642
743
|
env: this.config.env
|
|
643
|
-
}), n = new
|
|
744
|
+
}), n = new m(), c = this.walletClient.module("balance").portfolio.getBalance(s.suiCoinType), i = T(n, c.coins, {
|
|
644
745
|
balance: a,
|
|
645
746
|
useGasCoin: !0
|
|
646
747
|
});
|
|
647
|
-
await
|
|
748
|
+
await le(n, s, i, {
|
|
648
749
|
env: this.config.env,
|
|
649
750
|
amount: a,
|
|
650
751
|
accountCap: t == null ? void 0 : t.accountCap
|
|
651
752
|
});
|
|
652
|
-
const
|
|
753
|
+
const l = await this.walletClient.signExecuteTransaction({
|
|
653
754
|
transaction: n,
|
|
654
|
-
dryRun: (
|
|
755
|
+
dryRun: (u = t == null ? void 0 : t.dryRun) != null ? u : !1
|
|
655
756
|
});
|
|
656
|
-
return !(t != null && t.dryRun) && ((
|
|
757
|
+
return !(t != null && t.dryRun) && ((h = (o = l.effects) == null ? void 0 : o.status) == null ? void 0 : h.status) === "success" && (this.emit("lending:repay-success", {
|
|
657
758
|
identifier: e,
|
|
658
759
|
amount: a
|
|
659
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
760
|
+
}), this.walletClient.module("balance").updatePortfolio()), l;
|
|
660
761
|
}
|
|
661
762
|
/**
|
|
662
763
|
* Gets the current health factor for the user
|
|
@@ -667,7 +768,7 @@ class ve extends R {
|
|
|
667
768
|
if (!this.walletClient)
|
|
668
769
|
throw new Error("Wallet client not found");
|
|
669
770
|
const e = this.walletClient.address;
|
|
670
|
-
return await
|
|
771
|
+
return await Ce(e, {
|
|
671
772
|
env: this.config.env,
|
|
672
773
|
client: this.walletClient.client
|
|
673
774
|
});
|
|
@@ -683,45 +784,45 @@ class ve extends R {
|
|
|
683
784
|
* @returns Transaction response or dry run response
|
|
684
785
|
*/
|
|
685
786
|
async liquidate(e, a, t, s, n) {
|
|
686
|
-
var
|
|
787
|
+
var d, w, C;
|
|
687
788
|
if (!this.walletClient)
|
|
688
789
|
throw new Error("Wallet client not found");
|
|
689
|
-
const c = await
|
|
790
|
+
const c = await v(e, {
|
|
690
791
|
env: this.config.env
|
|
691
|
-
}),
|
|
792
|
+
}), i = await v(t, {
|
|
692
793
|
env: this.config.env
|
|
693
|
-
}),
|
|
794
|
+
}), l = new m(), u = this.walletClient.module("balance").portfolio.getBalance(c.suiCoinType), o = T(l, u.coins, {
|
|
694
795
|
balance: a,
|
|
695
796
|
useGasCoin: !0
|
|
696
|
-
}), [
|
|
697
|
-
i,
|
|
698
|
-
c,
|
|
699
|
-
u,
|
|
797
|
+
}), [h, g] = await be(
|
|
700
798
|
l,
|
|
799
|
+
c,
|
|
800
|
+
o,
|
|
801
|
+
i,
|
|
701
802
|
s,
|
|
702
803
|
{
|
|
703
804
|
env: this.config.env
|
|
704
805
|
}
|
|
705
|
-
), [
|
|
806
|
+
), [f] = l.moveCall({
|
|
706
807
|
target: "0x2::coin::from_balance",
|
|
707
|
-
arguments: [
|
|
708
|
-
typeArguments: [
|
|
709
|
-
}), [b] =
|
|
808
|
+
arguments: [h],
|
|
809
|
+
typeArguments: [i.suiCoinType]
|
|
810
|
+
}), [b] = l.moveCall({
|
|
710
811
|
target: "0x2::coin::from_balance",
|
|
711
|
-
arguments: [
|
|
812
|
+
arguments: [g],
|
|
712
813
|
typeArguments: [c.suiCoinType]
|
|
713
814
|
});
|
|
714
|
-
|
|
715
|
-
const
|
|
716
|
-
transaction:
|
|
717
|
-
dryRun: (
|
|
815
|
+
l.transferObjects([f, b], this.walletClient.address);
|
|
816
|
+
const y = await this.walletClient.signExecuteTransaction({
|
|
817
|
+
transaction: l,
|
|
818
|
+
dryRun: (d = n == null ? void 0 : n.dryRun) != null ? d : !1
|
|
718
819
|
});
|
|
719
|
-
return !(n != null && n.dryRun) && ((
|
|
820
|
+
return !(n != null && n.dryRun) && ((C = (w = y.effects) == null ? void 0 : w.status) == null ? void 0 : C.status) === "success" && (this.emit("lending:liquidate-success", {
|
|
720
821
|
payIdentifier: e,
|
|
721
822
|
payAmount: a,
|
|
722
823
|
collateralIdentifier: t,
|
|
723
824
|
liquidationAddress: s
|
|
724
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
825
|
+
}), this.walletClient.module("balance").updatePortfolio()), y;
|
|
725
826
|
}
|
|
726
827
|
/**
|
|
727
828
|
* Gets the available lending rewards for the current user.
|
|
@@ -733,7 +834,7 @@ class ve extends R {
|
|
|
733
834
|
async getAvailableRewards(e) {
|
|
734
835
|
if (!this.walletClient)
|
|
735
836
|
throw new Error("Wallet client not found");
|
|
736
|
-
return
|
|
837
|
+
return J(this.walletClient.address, {
|
|
737
838
|
...e,
|
|
738
839
|
env: this.config.env
|
|
739
840
|
});
|
|
@@ -748,7 +849,7 @@ class ve extends R {
|
|
|
748
849
|
async getClaimedRewardHistory(e) {
|
|
749
850
|
if (!this.walletClient)
|
|
750
851
|
throw new Error("Wallet client not found");
|
|
751
|
-
return
|
|
852
|
+
return ye(this.walletClient.address, {
|
|
752
853
|
...e
|
|
753
854
|
});
|
|
754
855
|
}
|
|
@@ -759,13 +860,13 @@ class ve extends R {
|
|
|
759
860
|
* @returns Transaction response or dry run response
|
|
760
861
|
*/
|
|
761
862
|
async claimAllRewards(e) {
|
|
762
|
-
var n, c,
|
|
863
|
+
var n, c, i;
|
|
763
864
|
if (!this.walletClient)
|
|
764
865
|
throw new Error("Wallet client not found");
|
|
765
|
-
const a = new
|
|
866
|
+
const a = new m(), t = await J(this.walletClient.address, {
|
|
766
867
|
env: this.config.env
|
|
767
868
|
});
|
|
768
|
-
await
|
|
869
|
+
await pe(a, t, {
|
|
769
870
|
env: this.config.env,
|
|
770
871
|
accountCap: e == null ? void 0 : e.accountCap
|
|
771
872
|
});
|
|
@@ -773,20 +874,20 @@ class ve extends R {
|
|
|
773
874
|
transaction: a,
|
|
774
875
|
dryRun: (n = e == null ? void 0 : e.dryRun) != null ? n : !1
|
|
775
876
|
});
|
|
776
|
-
return !(e != null && e.dryRun) && ((
|
|
877
|
+
return !(e != null && e.dryRun) && ((i = (c = s.effects) == null ? void 0 : c.status) == null ? void 0 : i.status) === "success" && (this.emit("lending:claim-rewards-success", {
|
|
777
878
|
rewards: t
|
|
778
879
|
}), this.walletClient.module("balance").updatePortfolio()), s;
|
|
779
880
|
}
|
|
780
881
|
async updateOraclePTB(e, a) {
|
|
781
|
-
const t = await
|
|
882
|
+
const t = await K({
|
|
782
883
|
env: this.config.env
|
|
783
884
|
}), s = await this.getLendingState({
|
|
784
885
|
cacheTime: 60 * 1e3
|
|
785
|
-
}), n =
|
|
886
|
+
}), n = q(t, {
|
|
786
887
|
lendingState: s,
|
|
787
888
|
pools: a
|
|
788
889
|
});
|
|
789
|
-
await
|
|
890
|
+
await $(e, n, {
|
|
790
891
|
env: this.config.env,
|
|
791
892
|
updatePythPriceFeeds: !0
|
|
792
893
|
});
|
|
@@ -802,7 +903,7 @@ class ve extends R {
|
|
|
802
903
|
var s;
|
|
803
904
|
if (!this.walletClient)
|
|
804
905
|
throw new Error("Wallet client not found");
|
|
805
|
-
const a = new
|
|
906
|
+
const a = new m();
|
|
806
907
|
return await this.updateOraclePTB(a, e == null ? void 0 : e.pools), await this.walletClient.signExecuteTransaction({
|
|
807
908
|
transaction: a,
|
|
808
909
|
dryRun: (s = e == null ? void 0 : e.dryRun) != null ? s : !1
|
|
@@ -817,7 +918,7 @@ class ve extends R {
|
|
|
817
918
|
async getLendingState(e) {
|
|
818
919
|
if (!this.walletClient)
|
|
819
920
|
throw new Error("Wallet client not found");
|
|
820
|
-
return await
|
|
921
|
+
return await Pe(this.walletClient.address, {
|
|
821
922
|
env: this.config.env,
|
|
822
923
|
...e,
|
|
823
924
|
client: this.walletClient.client
|
|
@@ -833,22 +934,36 @@ class ve extends R {
|
|
|
833
934
|
var n;
|
|
834
935
|
if (!this.walletClient)
|
|
835
936
|
throw new Error("Wallet client not found");
|
|
836
|
-
const a = new
|
|
937
|
+
const a = new m(), t = await Be(a);
|
|
837
938
|
return a.transferObjects([t], this.walletClient.address), await this.walletClient.signExecuteTransaction({
|
|
838
939
|
transaction: a,
|
|
839
940
|
dryRun: (n = e == null ? void 0 : e.dryRun) != null ? n : !1
|
|
840
941
|
});
|
|
841
942
|
}
|
|
943
|
+
async initializeProtocols() {
|
|
944
|
+
if (!this.walletClient) {
|
|
945
|
+
console.warn("Wallet client not available during protocol initialization");
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const e = [];
|
|
949
|
+
e.push(new _e(this.walletClient));
|
|
950
|
+
try {
|
|
951
|
+
e.push(await z.create(this.walletClient));
|
|
952
|
+
} catch (a) {
|
|
953
|
+
console.warn("Failed to initialize SuilendProtocol:", a);
|
|
954
|
+
}
|
|
955
|
+
this._protocols = e;
|
|
956
|
+
}
|
|
842
957
|
}
|
|
843
|
-
class
|
|
958
|
+
class je extends O {
|
|
844
959
|
constructor() {
|
|
845
960
|
super(...arguments), this.name = "volo", this.defaultConfig = {
|
|
846
961
|
packageId: "0x68d22cf8bdbcd11ecba1e094922873e4080d4d11133e2443fddda0bfd11dae20",
|
|
847
962
|
poolId: "0x2d914e23d82fedef1b5f56a32d5c64bdcc3087ccfea2b4d6ea51a71f587840e5",
|
|
848
963
|
metadataId: "0x680cd26af32b2bde8d3361e804c53ec1d1cfe24c7f039eb7f549e8dfde389a60",
|
|
849
964
|
coinType: "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT"
|
|
850
|
-
}, this.getStats =
|
|
851
|
-
|
|
965
|
+
}, this.getStats = re(
|
|
966
|
+
Q(async () => (await fetch("https://open-api.naviprotocol.io/api/volo/stats").then((a) => a.json())).data)
|
|
852
967
|
);
|
|
853
968
|
}
|
|
854
969
|
/**
|
|
@@ -865,7 +980,7 @@ class Ee extends R {
|
|
|
865
980
|
e.object(this.config.poolId),
|
|
866
981
|
e.object(this.config.metadataId),
|
|
867
982
|
e.object("0x05"),
|
|
868
|
-
|
|
983
|
+
_(a, e.object)
|
|
869
984
|
],
|
|
870
985
|
typeArguments: []
|
|
871
986
|
});
|
|
@@ -885,7 +1000,7 @@ class Ee extends R {
|
|
|
885
1000
|
e.object(this.config.poolId),
|
|
886
1001
|
e.object(this.config.metadataId),
|
|
887
1002
|
e.object("0x05"),
|
|
888
|
-
|
|
1003
|
+
_(a, e.object)
|
|
889
1004
|
],
|
|
890
1005
|
typeArguments: []
|
|
891
1006
|
});
|
|
@@ -900,25 +1015,25 @@ class Ee extends R {
|
|
|
900
1015
|
* @throws Error if wallet client not found or stake amount is too low
|
|
901
1016
|
*/
|
|
902
1017
|
async stake(e, a) {
|
|
903
|
-
var
|
|
1018
|
+
var l, u, o;
|
|
904
1019
|
if (!this.walletClient)
|
|
905
1020
|
throw new Error("Wallet client not found");
|
|
906
1021
|
if (e < 1e9)
|
|
907
1022
|
throw new Error("Stake amount should be greater than 1Sui");
|
|
908
|
-
const t = new
|
|
1023
|
+
const t = new m();
|
|
909
1024
|
await this.walletClient.module("balance").waitForUpdate();
|
|
910
|
-
const s = this.walletClient.module("balance").portfolio.getBalance("0x2::sui::SUI"), n =
|
|
1025
|
+
const s = this.walletClient.module("balance").portfolio.getBalance("0x2::sui::SUI"), n = T(t, s.coins, {
|
|
911
1026
|
balance: e,
|
|
912
1027
|
useGasCoin: !0
|
|
913
1028
|
}), c = await this.stakePTB(t, n);
|
|
914
1029
|
t.transferObjects([c], this.walletClient.address);
|
|
915
|
-
const
|
|
1030
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
916
1031
|
transaction: t,
|
|
917
|
-
dryRun: (
|
|
1032
|
+
dryRun: (l = a == null ? void 0 : a.dryRun) != null ? l : !1
|
|
918
1033
|
});
|
|
919
|
-
return !(a != null && a.dryRun) && ((
|
|
1034
|
+
return !(a != null && a.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("volo:stake-success", {
|
|
920
1035
|
suiAmount: e
|
|
921
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
1036
|
+
}), this.walletClient.module("balance").updatePortfolio()), i;
|
|
922
1037
|
}
|
|
923
1038
|
/**
|
|
924
1039
|
* Unstake vSUI tokens to receive SUI tokens
|
|
@@ -929,26 +1044,26 @@ class Ee extends R {
|
|
|
929
1044
|
* @throws Error if wallet client not found or unstake amount is too low
|
|
930
1045
|
*/
|
|
931
1046
|
async unstake(e, a) {
|
|
932
|
-
var
|
|
1047
|
+
var l, u, o;
|
|
933
1048
|
if (!this.walletClient)
|
|
934
1049
|
throw new Error("Wallet client not found");
|
|
935
1050
|
if (e < 1e9)
|
|
936
1051
|
throw new Error("Unstake amount should >= 1vSui");
|
|
937
1052
|
await this.walletClient.module("balance").waitForUpdate();
|
|
938
|
-
const t = new
|
|
1053
|
+
const t = new m(), s = this.walletClient.module("balance").portfolio.getBalance(this.config.coinType), n = T(t, s.coins, {
|
|
939
1054
|
balance: e
|
|
940
1055
|
}), c = await this.unstakePTB(t, n);
|
|
941
1056
|
t.transferObjects([c], this.walletClient.address);
|
|
942
|
-
const
|
|
1057
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
943
1058
|
transaction: t,
|
|
944
|
-
dryRun: (
|
|
1059
|
+
dryRun: (l = a == null ? void 0 : a.dryRun) != null ? l : !1
|
|
945
1060
|
});
|
|
946
|
-
return !(a != null && a.dryRun) && ((
|
|
1061
|
+
return !(a != null && a.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("volo:unstake-success", {
|
|
947
1062
|
vSuiAmount: e
|
|
948
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
1063
|
+
}), this.walletClient.module("balance").updatePortfolio()), i;
|
|
949
1064
|
}
|
|
950
1065
|
}
|
|
951
|
-
class
|
|
1066
|
+
class Ie extends O {
|
|
952
1067
|
constructor() {
|
|
953
1068
|
super(...arguments), this.name = "swap", this.defaultConfig = {
|
|
954
1069
|
apiKey: "",
|
|
@@ -965,7 +1080,7 @@ class Re extends R {
|
|
|
965
1080
|
* @returns Referral ID if API key is configured, otherwise 0
|
|
966
1081
|
*/
|
|
967
1082
|
get referral() {
|
|
968
|
-
return this.config.apiKey ?
|
|
1083
|
+
return this.config.apiKey ? Te(this.config.apiKey) : 0;
|
|
969
1084
|
}
|
|
970
1085
|
/**
|
|
971
1086
|
* Get the swap options
|
|
@@ -987,7 +1102,7 @@ class Re extends R {
|
|
|
987
1102
|
* @returns A promise that resolves with the quote
|
|
988
1103
|
*/
|
|
989
1104
|
async getQuote(e, a, t) {
|
|
990
|
-
return
|
|
1105
|
+
return ve(e, a, t, this.config.apiKey, this.swapOptions);
|
|
991
1106
|
}
|
|
992
1107
|
/**
|
|
993
1108
|
* Builds a swap transaction from a quote
|
|
@@ -1000,8 +1115,8 @@ class Re extends R {
|
|
|
1000
1115
|
async buildSwapPTBFromQuote(e, a, t, s) {
|
|
1001
1116
|
if (!this.walletClient)
|
|
1002
1117
|
throw new Error("Wallet client not found");
|
|
1003
|
-
const n = new
|
|
1004
|
-
return await
|
|
1118
|
+
const n = new R(a.amount_out).multipliedBy(1 - s).toFixed(0);
|
|
1119
|
+
return await G(
|
|
1005
1120
|
this.walletClient.address,
|
|
1006
1121
|
e,
|
|
1007
1122
|
Number(n),
|
|
@@ -1031,57 +1146,57 @@ class Re extends R {
|
|
|
1031
1146
|
* @returns Transaction response or dry run response
|
|
1032
1147
|
*/
|
|
1033
1148
|
async swap(e, a, t, s, n) {
|
|
1034
|
-
var
|
|
1149
|
+
var y, d, w;
|
|
1035
1150
|
if (!this.walletClient)
|
|
1036
1151
|
throw new Error("Wallet client not found");
|
|
1037
1152
|
await this.walletClient.module("balance").waitForUpdate();
|
|
1038
|
-
const c = this.walletClient.module("balance").portfolio.getBalance(e),
|
|
1153
|
+
const c = this.walletClient.module("balance").portfolio.getBalance(e), i = new m(), l = T(i, c.coins, {
|
|
1039
1154
|
balance: t,
|
|
1040
1155
|
useGasCoin: !0
|
|
1041
|
-
}),
|
|
1042
|
-
if (
|
|
1156
|
+
}), u = await this.getQuote(e, a, t), o = await this.buildSwapPTBFromQuote(i, u, l, s);
|
|
1157
|
+
if (i.transferObjects([o], this.walletClient.address), i.setSender(this.walletClient.address), n != null && n.dryRun)
|
|
1043
1158
|
return await this.walletClient.signExecuteTransaction({
|
|
1044
|
-
transaction:
|
|
1159
|
+
transaction: i,
|
|
1045
1160
|
dryRun: !0
|
|
1046
1161
|
});
|
|
1047
|
-
const
|
|
1162
|
+
const h = await i.build({
|
|
1048
1163
|
client: this.walletClient.client
|
|
1049
|
-
}),
|
|
1164
|
+
}), g = await this.walletClient.signer.signTransaction(h), f = [g.signature];
|
|
1050
1165
|
try {
|
|
1051
|
-
await
|
|
1052
|
-
} catch (
|
|
1053
|
-
console.error(
|
|
1166
|
+
await xe(g.bytes, f);
|
|
1167
|
+
} catch (C) {
|
|
1168
|
+
console.error(C);
|
|
1054
1169
|
}
|
|
1055
1170
|
const b = await this.walletClient.client.executeTransactionBlock({
|
|
1056
|
-
transactionBlock:
|
|
1057
|
-
signature:
|
|
1171
|
+
transactionBlock: g.bytes,
|
|
1172
|
+
signature: f,
|
|
1058
1173
|
options: {
|
|
1059
1174
|
showEffects: !0,
|
|
1060
1175
|
showEvents: !0,
|
|
1061
1176
|
showBalanceChanges: !0
|
|
1062
1177
|
}
|
|
1063
1178
|
});
|
|
1064
|
-
if (((
|
|
1065
|
-
const
|
|
1066
|
-
|
|
1179
|
+
if (((d = (y = b.effects) == null ? void 0 : y.status) == null ? void 0 : d.status) === "success") {
|
|
1180
|
+
const C = (w = b.events) == null ? void 0 : w.find((p) => p.type.includes("::slippage::SwapEvent"));
|
|
1181
|
+
C && this.emit("swap:swap-success", {
|
|
1067
1182
|
fromCoinType: e,
|
|
1068
1183
|
toCoinType: a,
|
|
1069
1184
|
fromAmount: t,
|
|
1070
1185
|
slippage: s,
|
|
1071
|
-
toAmount: Number(
|
|
1186
|
+
toAmount: Number(C.parsedJson.amount_out)
|
|
1072
1187
|
}), this.walletClient.module("balance").updatePortfolio();
|
|
1073
1188
|
}
|
|
1074
1189
|
return b;
|
|
1075
1190
|
}
|
|
1076
1191
|
}
|
|
1077
|
-
class
|
|
1192
|
+
class Ue extends O {
|
|
1078
1193
|
constructor() {
|
|
1079
1194
|
super(...arguments), this.name = "haedal", this.defaultConfig = {
|
|
1080
1195
|
packageId: "0x3f45767c1aa95b25422f675800f02d8a813ec793a00b60667d071a77ba7178a2",
|
|
1081
1196
|
configId: "0x47b224762220393057ebf4f70501b6e657c3e56684737568439a04f80849b2ca",
|
|
1082
1197
|
coinType: "0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI"
|
|
1083
|
-
}, this.getApy =
|
|
1084
|
-
|
|
1198
|
+
}, this.getApy = re(
|
|
1199
|
+
Q(async () => (await fetch("https://open-api.naviprotocol.io/api/haedal/stats").then((a) => a.json())).data.apy)
|
|
1085
1200
|
);
|
|
1086
1201
|
}
|
|
1087
1202
|
/**
|
|
@@ -1097,7 +1212,7 @@ class Se extends R {
|
|
|
1097
1212
|
arguments: [
|
|
1098
1213
|
e.object("0x05"),
|
|
1099
1214
|
e.object(this.config.configId),
|
|
1100
|
-
|
|
1215
|
+
_(a, e.object),
|
|
1101
1216
|
e.pure.address("0x0000000000000000000000000000000000000000000000000000000000000000")
|
|
1102
1217
|
],
|
|
1103
1218
|
typeArguments: []
|
|
@@ -1117,7 +1232,7 @@ class Se extends R {
|
|
|
1117
1232
|
arguments: [
|
|
1118
1233
|
e.object("0x05"),
|
|
1119
1234
|
e.object(this.config.configId),
|
|
1120
|
-
|
|
1235
|
+
_(a, e.object)
|
|
1121
1236
|
],
|
|
1122
1237
|
typeArguments: []
|
|
1123
1238
|
});
|
|
@@ -1130,21 +1245,21 @@ class Se extends R {
|
|
|
1130
1245
|
* @param options Optional dry run
|
|
1131
1246
|
*/
|
|
1132
1247
|
async stake(e, a) {
|
|
1133
|
-
var
|
|
1248
|
+
var l, u, o;
|
|
1134
1249
|
if (!this.walletClient)
|
|
1135
1250
|
throw new Error("Wallet client not found");
|
|
1136
|
-
const t = new
|
|
1251
|
+
const t = new m();
|
|
1137
1252
|
await this.walletClient.module("balance").waitForUpdate();
|
|
1138
|
-
const s = this.walletClient.module("balance").portfolio.getBalance("0x2::sui::SUI"), n =
|
|
1253
|
+
const s = this.walletClient.module("balance").portfolio.getBalance("0x2::sui::SUI"), n = T(t, s.coins, {
|
|
1139
1254
|
balance: e,
|
|
1140
1255
|
useGasCoin: !0
|
|
1141
1256
|
}), c = await this.stakePTB(t, n);
|
|
1142
1257
|
t.transferObjects([c], this.walletClient.address);
|
|
1143
|
-
const
|
|
1258
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
1144
1259
|
transaction: t,
|
|
1145
|
-
dryRun: (
|
|
1260
|
+
dryRun: (l = a == null ? void 0 : a.dryRun) != null ? l : !1
|
|
1146
1261
|
});
|
|
1147
|
-
return !(a != null && a.dryRun) && ((
|
|
1262
|
+
return !(a != null && a.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("haedal:stake-success", { suiAmount: e }), this.walletClient.module("balance").updatePortfolio()), i;
|
|
1148
1263
|
}
|
|
1149
1264
|
/**
|
|
1150
1265
|
* Unstake haSUI for SUI
|
|
@@ -1153,47 +1268,47 @@ class Se extends R {
|
|
|
1153
1268
|
* @param options Optional dry run
|
|
1154
1269
|
*/
|
|
1155
1270
|
async unstake(e, a) {
|
|
1156
|
-
var
|
|
1271
|
+
var l, u, o;
|
|
1157
1272
|
if (!this.walletClient)
|
|
1158
1273
|
throw new Error("Wallet client not found");
|
|
1159
1274
|
await this.walletClient.module("balance").waitForUpdate();
|
|
1160
|
-
const t = new
|
|
1275
|
+
const t = new m(), s = this.walletClient.module("balance").portfolio.getBalance(this.config.coinType), n = T(t, s.coins, {
|
|
1161
1276
|
balance: e
|
|
1162
1277
|
}), c = await this.unstakePTB(t, n);
|
|
1163
1278
|
t.transferObjects([c], this.walletClient.address);
|
|
1164
|
-
const
|
|
1279
|
+
const i = await this.walletClient.signExecuteTransaction({
|
|
1165
1280
|
transaction: t,
|
|
1166
|
-
dryRun: (
|
|
1281
|
+
dryRun: (l = a == null ? void 0 : a.dryRun) != null ? l : !1
|
|
1167
1282
|
});
|
|
1168
|
-
return !(a != null && a.dryRun) && ((
|
|
1283
|
+
return !(a != null && a.dryRun) && ((o = (u = i.effects) == null ? void 0 : u.status) == null ? void 0 : o.status) === "success" && (this.emit("haedal:unstake-success", { haSUIAmount: e }), this.walletClient.module("balance").updatePortfolio()), i;
|
|
1169
1284
|
}
|
|
1170
1285
|
}
|
|
1171
|
-
const
|
|
1286
|
+
const Me = {
|
|
1172
1287
|
/** Balance management module */
|
|
1173
|
-
balance: new
|
|
1288
|
+
balance: new Ne(),
|
|
1174
1289
|
/** Lending protocol module */
|
|
1175
|
-
lending: new
|
|
1290
|
+
lending: new Fe(),
|
|
1176
1291
|
/** Volo protocol module */
|
|
1177
|
-
volo: new
|
|
1292
|
+
volo: new je(),
|
|
1178
1293
|
/** DEX swapping module */
|
|
1179
|
-
swap: new
|
|
1294
|
+
swap: new Ie(),
|
|
1180
1295
|
/** Haedal protocol module */
|
|
1181
|
-
haedal: new
|
|
1182
|
-
},
|
|
1183
|
-
url:
|
|
1296
|
+
haedal: new Ue()
|
|
1297
|
+
}, We = {
|
|
1298
|
+
url: de("mainnet")
|
|
1184
1299
|
};
|
|
1185
|
-
class
|
|
1300
|
+
class Ye {
|
|
1186
1301
|
/**
|
|
1187
1302
|
* Creates a new wallet client instance
|
|
1188
1303
|
* @param options - Configuration options for the wallet client
|
|
1189
1304
|
*/
|
|
1190
1305
|
constructor(e) {
|
|
1191
|
-
this.modules =
|
|
1306
|
+
this.modules = Me, this.userConfigs = {}, this.events = he();
|
|
1192
1307
|
const a = {
|
|
1193
|
-
...
|
|
1308
|
+
...We,
|
|
1194
1309
|
...e.client
|
|
1195
1310
|
};
|
|
1196
|
-
a.transport = a.transport || new
|
|
1311
|
+
a.transport = a.transport || new ke(a.url), this.client = new we(a), this.signer = e.signer, this.userConfigs = e.configs || {}, Object.entries(this.modules).forEach(([t, s]) => {
|
|
1197
1312
|
if (t !== s.name)
|
|
1198
1313
|
throw new Error(`Module name ${t} does not match module name ${s.name}`);
|
|
1199
1314
|
s.install(this);
|
|
@@ -1223,7 +1338,7 @@ class Le {
|
|
|
1223
1338
|
const { dryRun: a = !1, ...t } = e;
|
|
1224
1339
|
if (a) {
|
|
1225
1340
|
let s;
|
|
1226
|
-
return e.transaction instanceof
|
|
1341
|
+
return e.transaction instanceof m ? (e.transaction.setSenderIfNotSet(this.address), s = await e.transaction.build({
|
|
1227
1342
|
client: this.client
|
|
1228
1343
|
})) : s = e.transaction, this.client.dryRunTransactionBlock({
|
|
1229
1344
|
transactionBlock: s
|
|
@@ -1269,7 +1384,7 @@ class Le {
|
|
|
1269
1384
|
return e in this.modules;
|
|
1270
1385
|
}
|
|
1271
1386
|
}
|
|
1272
|
-
class
|
|
1387
|
+
class Ze extends oe {
|
|
1273
1388
|
/**
|
|
1274
1389
|
* Creates a new watch-only signer
|
|
1275
1390
|
*
|
|
@@ -1333,7 +1448,7 @@ class Qe extends ne {
|
|
|
1333
1448
|
return {};
|
|
1334
1449
|
}
|
|
1335
1450
|
}
|
|
1336
|
-
class
|
|
1451
|
+
class et extends oe {
|
|
1337
1452
|
/**
|
|
1338
1453
|
* Creates a new watch-only signer
|
|
1339
1454
|
*
|
|
@@ -1386,8 +1501,8 @@ class qe extends ne {
|
|
|
1386
1501
|
}
|
|
1387
1502
|
}
|
|
1388
1503
|
export {
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1504
|
+
Ye as WalletClient,
|
|
1505
|
+
Ze as WatchSigner,
|
|
1506
|
+
et as WebSigner
|
|
1392
1507
|
};
|
|
1393
1508
|
//# sourceMappingURL=index.esm.js.map
|