@naviprotocol/wallet-client 1.3.2-dev.0 → 1.3.2-dev.1
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.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +41 -41
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -3,7 +3,7 @@ import pe from "mitt";
|
|
|
3
3
|
import Pe from "axios";
|
|
4
4
|
import { withSingleton as q, getCoins as se, mergeCoinsPTB as P, getPriceFeeds as K, updateOraclePricesPTB as Q, filterPriceFeeds as G, getFlashLoanAsset as ie, flashloanPTB as le, depositCoinPTB as I, repayFlashLoanPTB as re, borrowCoinPTB as oe, withdrawCoinPTB as ce, repayCoinPTB as ue, getPools as Be, getPool as B, getHealthFactor as Te, liquidatePTB as ve, getUserAvailableLendingRewards as Y, getUserClaimedRewardHistory as Ee, claimLendingRewardsPTB as Re, getLendingState as Se, createAccountCapPTB as Oe, withCache as de, parseTxValue as U } from "@naviprotocol/lending";
|
|
5
5
|
import T, { BigNumber as $ } from "bignumber.js";
|
|
6
|
-
import { normalizeStructTag as
|
|
6
|
+
import { normalizeStructTag as C } from "@mysten/sui/utils";
|
|
7
7
|
import { Transaction as m } from "@mysten/sui/transactions";
|
|
8
8
|
import { buildSwapPTBFromQuote as z, generateRefId as Ae, getQuote as xe } from "@naviprotocol/astros-aggregator-sdk";
|
|
9
9
|
import { SuilendClient as Z, LENDING_MARKET_ID as ke, LENDING_MARKET_TYPE as ee, parseReserve as Fe, parseObligation as je } from "@suilend/sdk";
|
|
@@ -119,7 +119,7 @@ class E {
|
|
|
119
119
|
else {
|
|
120
120
|
const a = {};
|
|
121
121
|
e == null || e.forEach((t) => {
|
|
122
|
-
const s =
|
|
122
|
+
const s = C(t.coinType);
|
|
123
123
|
let n = a[s];
|
|
124
124
|
n || (n = {
|
|
125
125
|
coinType: s,
|
|
@@ -136,7 +136,7 @@ class E {
|
|
|
136
136
|
* @returns UserCoinBalance object with coin type, total amount, and coin objects
|
|
137
137
|
*/
|
|
138
138
|
getBalance(e) {
|
|
139
|
-
return e =
|
|
139
|
+
return e = C(e), this.balances[e] || { coinType: e, amount: $(0), coins: [] };
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
142
|
* Gets all coin types in the portfolio
|
|
@@ -153,7 +153,7 @@ class E {
|
|
|
153
153
|
* @returns BigNumber representing the total balance
|
|
154
154
|
*/
|
|
155
155
|
balanceOf(e) {
|
|
156
|
-
e =
|
|
156
|
+
e = C(e);
|
|
157
157
|
const a = this.getBalance(e);
|
|
158
158
|
return (a == null ? void 0 : a.amount) || $(0);
|
|
159
159
|
}
|
|
@@ -243,7 +243,7 @@ class We extends A {
|
|
|
243
243
|
throw new Error("Insufficient balance");
|
|
244
244
|
const i = new m();
|
|
245
245
|
let l = i.gas;
|
|
246
|
-
|
|
246
|
+
C(e) !== C("0x2::sui::SUI") && (l = P(i, n.coins, {
|
|
247
247
|
balance: c.toNumber()
|
|
248
248
|
}));
|
|
249
249
|
const u = i.splitCoins(l, t);
|
|
@@ -356,14 +356,14 @@ async function te(o, e, a, t) {
|
|
|
356
356
|
});
|
|
357
357
|
if (!h)
|
|
358
358
|
throw new Error(`${n.token.symbol} pool not support flashloan`);
|
|
359
|
-
const
|
|
359
|
+
const y = (j = t == null ? void 0 : t.slippage) != null ? j : 5e-3, b = (N = t == null ? void 0 : t.amount) != null ? N : Number(r.supplyBalance), d = await this.walletClient.swap.getQuote(
|
|
360
360
|
s.suiCoinType,
|
|
361
361
|
n.suiCoinType,
|
|
362
|
-
|
|
362
|
+
b
|
|
363
363
|
), g = Number(d.amount_out);
|
|
364
364
|
if (g <= 0)
|
|
365
365
|
throw new Error("The amount of swapAmountOut must be greater than 0.");
|
|
366
|
-
const
|
|
366
|
+
const p = Math.floor(g * (1 - y)), v = Math.floor(p / (1 + h.flashloanFee));
|
|
367
367
|
if (v <= 0)
|
|
368
368
|
throw new Error("The calculated borrow amount must be positive.");
|
|
369
369
|
const [R, W] = await le(o, n, v), [S] = o.moveCall({
|
|
@@ -374,10 +374,10 @@ async function te(o, e, a, t) {
|
|
|
374
374
|
await I(o, n, S, {
|
|
375
375
|
amount: v
|
|
376
376
|
});
|
|
377
|
-
const O = await u.withdrawCoinPTB(o, s.suiCoinType,
|
|
377
|
+
const O = await u.withdrawCoinPTB(o, s.suiCoinType, b), x = await z(
|
|
378
378
|
i,
|
|
379
379
|
o,
|
|
380
|
-
|
|
380
|
+
p,
|
|
381
381
|
O,
|
|
382
382
|
d
|
|
383
383
|
), k = o.moveCall({
|
|
@@ -420,29 +420,29 @@ async function ae(o, e, a, t) {
|
|
|
420
420
|
new T(h).multipliedBy(_ * (60 * 3) / (365 * 24 * 3600)).plus(new T(1).shiftedBy(-n.token.decimals)).plus(h).toNumber()
|
|
421
421
|
);
|
|
422
422
|
}
|
|
423
|
-
const
|
|
423
|
+
const y = await ie(n, {
|
|
424
424
|
env: this.config.env
|
|
425
425
|
});
|
|
426
|
-
if (!
|
|
426
|
+
if (!y)
|
|
427
427
|
throw new Error(`${n.token.symbol} pool not support flashloan`);
|
|
428
428
|
if (!this.walletClient)
|
|
429
429
|
throw new Error("Wallet client not found");
|
|
430
|
-
const
|
|
430
|
+
const b = (V = t == null ? void 0 : t.slippage) != null ? V : 5e-3, d = Math.ceil(h * (1 + y.flashloanFee)), g = [
|
|
431
431
|
{ coinType: n.suiCoinType, chain: 1999 },
|
|
432
432
|
{ coinType: c.suiCoinType, chain: 1999 }
|
|
433
|
-
],
|
|
433
|
+
], p = `${this.walletClient.lending.config.aggregatorBaseUrl}/bridge-coins/price`, R = await (await fetch(p, {
|
|
434
434
|
method: "POST",
|
|
435
435
|
headers: { "Content-Type": "application/json" },
|
|
436
436
|
body: JSON.stringify(g)
|
|
437
437
|
})).json(), W = (X = R == null ? void 0 : R.data) == null ? void 0 : X.list, S = {};
|
|
438
438
|
W.forEach((_) => {
|
|
439
|
-
S[_.coinType] = _;
|
|
439
|
+
S[C(_.coinType)] = _;
|
|
440
440
|
});
|
|
441
|
-
const O = S[n.suiCoinType], x = S[c.suiCoinType];
|
|
441
|
+
const O = S[C(n.suiCoinType)], x = S[C(c.suiCoinType)];
|
|
442
442
|
if (!O || !x)
|
|
443
443
|
throw new Error("Fetch price failed");
|
|
444
444
|
const k = Number(
|
|
445
|
-
T(d).div(Math.pow(10, O.decimals)).multipliedBy(O).multipliedBy(1 +
|
|
445
|
+
T(d).div(Math.pow(10, O.decimals)).multipliedBy(O).multipliedBy(1 + b).div(x).multipliedBy(Math.pow(10, c.token.decimals)).toFixed(0)
|
|
446
446
|
), [M, L] = await le(o, n, h), [F] = o.moveCall({
|
|
447
447
|
target: "0x2::coin::from_balance",
|
|
448
448
|
arguments: [M],
|
|
@@ -489,7 +489,7 @@ async function ne(o, e, a, t) {
|
|
|
489
489
|
balance: l,
|
|
490
490
|
useGasCoin: !0
|
|
491
491
|
});
|
|
492
|
-
if (
|
|
492
|
+
if (C(e) !== C(n.suiCoinType)) {
|
|
493
493
|
const h = await this.walletClient.swap.getQuote(e, n.suiCoinType, l);
|
|
494
494
|
r = await this.walletClient.swap.buildSwapPTBFromQuote(
|
|
495
495
|
o,
|
|
@@ -506,7 +506,7 @@ class Me {
|
|
|
506
506
|
}
|
|
507
507
|
async getPool(e) {
|
|
508
508
|
const a = await this.walletClient.lending.getPool(e), s = (await this.walletClient.lending.getLendingState()).find(
|
|
509
|
-
(i) => i.pool.suiCoinType ===
|
|
509
|
+
(i) => i.pool.suiCoinType === C(e)
|
|
510
510
|
), n = s ? new T(s.supplyBalance).toNumber() : 0, c = s ? new T(s.borrowBalance).toNumber() : 0;
|
|
511
511
|
return {
|
|
512
512
|
supplyBalance: n,
|
|
@@ -545,13 +545,13 @@ class H {
|
|
|
545
545
|
return new H(e, t[0], a);
|
|
546
546
|
}
|
|
547
547
|
async getPool(e) {
|
|
548
|
-
var
|
|
549
|
-
const a =
|
|
548
|
+
var y, b;
|
|
549
|
+
const a = C(e), t = this.suilendClient.lendingMarket.reserves, s = /* @__PURE__ */ new Set();
|
|
550
550
|
t.forEach((d) => {
|
|
551
|
-
s.add(
|
|
552
|
-
g && s.add(
|
|
551
|
+
s.add(C(d.coinType.name)), d.depositsPoolRewardManager.poolRewards.forEach((g) => {
|
|
552
|
+
g && s.add(C(g.coinType.name));
|
|
553
553
|
}), d.borrowsPoolRewardManager.poolRewards.forEach((g) => {
|
|
554
|
-
g && s.add(
|
|
554
|
+
g && s.add(C(g.coinType.name));
|
|
555
555
|
});
|
|
556
556
|
});
|
|
557
557
|
const n = await Ne(Array.from(s)), i = t.map((d) => Fe(d, n)).reduce(
|
|
@@ -562,8 +562,8 @@ class H {
|
|
|
562
562
|
), u = je(l, i), r = u.deposits.find((d) => d.coinType === a), w = u.borrows.find((d) => d.coinType === a), f = n[a].decimals;
|
|
563
563
|
return {
|
|
564
564
|
cTokenExchangeRate: i[a].cTokenExchangeRate.toNumber(),
|
|
565
|
-
supplyBalance: (
|
|
566
|
-
borrowBalance: (
|
|
565
|
+
supplyBalance: (y = r == null ? void 0 : r.depositedAmount.shiftedBy(f).decimalPlaces(0).toNumber()) != null ? y : 0,
|
|
566
|
+
borrowBalance: (b = w == null ? void 0 : w.borrowedAmount.shiftedBy(f).decimalPlaces(0).toNumber()) != null ? b : 0,
|
|
567
567
|
borrowAPR: i[a].borrowAprPercent.toNumber()
|
|
568
568
|
};
|
|
569
569
|
}
|
|
@@ -779,7 +779,7 @@ class Le extends A {
|
|
|
779
779
|
* @returns Transaction response or dry run response
|
|
780
780
|
*/
|
|
781
781
|
async liquidate(e, a, t, s, n) {
|
|
782
|
-
var d, g,
|
|
782
|
+
var d, g, p;
|
|
783
783
|
if (!this.walletClient)
|
|
784
784
|
throw new Error("Wallet client not found");
|
|
785
785
|
const c = await B(e, {
|
|
@@ -802,22 +802,22 @@ class Le extends A {
|
|
|
802
802
|
target: "0x2::coin::from_balance",
|
|
803
803
|
arguments: [w],
|
|
804
804
|
typeArguments: [i.suiCoinType]
|
|
805
|
-
}), [
|
|
805
|
+
}), [y] = l.moveCall({
|
|
806
806
|
target: "0x2::coin::from_balance",
|
|
807
807
|
arguments: [f],
|
|
808
808
|
typeArguments: [c.suiCoinType]
|
|
809
809
|
});
|
|
810
|
-
l.transferObjects([h,
|
|
811
|
-
const
|
|
810
|
+
l.transferObjects([h, y], this.walletClient.address);
|
|
811
|
+
const b = await this.walletClient.signExecuteTransaction({
|
|
812
812
|
transaction: l,
|
|
813
813
|
dryRun: (d = n == null ? void 0 : n.dryRun) != null ? d : !1
|
|
814
814
|
});
|
|
815
|
-
return !(n != null && n.dryRun) && ((
|
|
815
|
+
return !(n != null && n.dryRun) && ((p = (g = b.effects) == null ? void 0 : g.status) == null ? void 0 : p.status) === "success" && (this.emit("lending:liquidate-success", {
|
|
816
816
|
payIdentifier: e,
|
|
817
817
|
payAmount: a,
|
|
818
818
|
collateralIdentifier: t,
|
|
819
819
|
liquidationAddress: s
|
|
820
|
-
}), this.walletClient.module("balance").updatePortfolio()),
|
|
820
|
+
}), this.walletClient.module("balance").updatePortfolio()), b;
|
|
821
821
|
}
|
|
822
822
|
/**
|
|
823
823
|
* Gets the available lending rewards for the current user.
|
|
@@ -1141,7 +1141,7 @@ class qe extends A {
|
|
|
1141
1141
|
* @returns Transaction response or dry run response
|
|
1142
1142
|
*/
|
|
1143
1143
|
async swap(e, a, t, s, n) {
|
|
1144
|
-
var
|
|
1144
|
+
var b, d, g;
|
|
1145
1145
|
if (!this.walletClient)
|
|
1146
1146
|
throw new Error("Wallet client not found");
|
|
1147
1147
|
await this.walletClient.module("balance").waitForUpdate();
|
|
@@ -1159,10 +1159,10 @@ class qe extends A {
|
|
|
1159
1159
|
}), f = await this.walletClient.signer.signTransaction(w), h = [f.signature];
|
|
1160
1160
|
try {
|
|
1161
1161
|
await Ie(f.bytes, h);
|
|
1162
|
-
} catch (
|
|
1163
|
-
console.error(
|
|
1162
|
+
} catch (p) {
|
|
1163
|
+
console.error(p);
|
|
1164
1164
|
}
|
|
1165
|
-
const
|
|
1165
|
+
const y = await this.walletClient.client.executeTransactionBlock({
|
|
1166
1166
|
transactionBlock: f.bytes,
|
|
1167
1167
|
signature: h,
|
|
1168
1168
|
options: {
|
|
@@ -1171,17 +1171,17 @@ class qe extends A {
|
|
|
1171
1171
|
showBalanceChanges: !0
|
|
1172
1172
|
}
|
|
1173
1173
|
});
|
|
1174
|
-
if (((d = (
|
|
1175
|
-
const
|
|
1176
|
-
|
|
1174
|
+
if (((d = (b = y.effects) == null ? void 0 : b.status) == null ? void 0 : d.status) === "success") {
|
|
1175
|
+
const p = (g = y.events) == null ? void 0 : g.find((v) => v.type.includes("::slippage::SwapEvent"));
|
|
1176
|
+
p && this.emit("swap:swap-success", {
|
|
1177
1177
|
fromCoinType: e,
|
|
1178
1178
|
toCoinType: a,
|
|
1179
1179
|
fromAmount: t,
|
|
1180
1180
|
slippage: s,
|
|
1181
|
-
toAmount: Number(
|
|
1181
|
+
toAmount: Number(p.parsedJson.amount_out)
|
|
1182
1182
|
}), this.walletClient.module("balance").updatePortfolio();
|
|
1183
1183
|
}
|
|
1184
|
-
return
|
|
1184
|
+
return y;
|
|
1185
1185
|
}
|
|
1186
1186
|
}
|
|
1187
1187
|
class Ke extends A {
|