@scallop-io/sui-scallop-sdk 0.44.5 → 0.44.7

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.
@@ -6,7 +6,7 @@ export declare const SUPPORT_POOLS: readonly ["eth", "btc", "usdc", "usdt", "sui
6
6
  export declare const SUPPORT_COLLATERALS: readonly ["eth", "btc", "usdc", "usdt", "sui", "apt", "sol", "cetus", "afsui", "hasui", "vsui"];
7
7
  export declare const SUPPORT_SPOOLS: readonly ["ssui", "susdc", "susdt"];
8
8
  export declare const SUPPORT_SPOOLS_REWARDS: readonly ["sui"];
9
- export declare const SUPPORT_BORROW_INCENTIVE_POOLS: readonly ["sui", "usdc"];
9
+ export declare const SUPPORT_BORROW_INCENTIVE_POOLS: readonly ["sui", "usdc", "usdt"];
10
10
  export declare const SUPPORT_BORROW_INCENTIVE_REWARDS: readonly ["sui"];
11
11
  export declare const SUPPORT_ORACLES: readonly ["supra", "switchboard", "pyth"];
12
12
  export declare const SUPPORT_PACKAGES: readonly ["coinDecimalsRegistry", "math", "whitelist", "x", "protocol", "protocolWhitelist", "query", "supra", "pyth", "switchboard", "xOracle", "testCoin"];
package/dist/index.js CHANGED
@@ -93,7 +93,7 @@ var SUPPORT_COLLATERALS = [
93
93
  ];
94
94
  var SUPPORT_SPOOLS = ["ssui", "susdc", "susdt"];
95
95
  var SUPPORT_SPOOLS_REWARDS = ["sui"];
96
- var SUPPORT_BORROW_INCENTIVE_POOLS = ["sui", "usdc"];
96
+ var SUPPORT_BORROW_INCENTIVE_POOLS = ["sui", "usdc", "usdt"];
97
97
  var SUPPORT_BORROW_INCENTIVE_REWARDS = ["sui"];
98
98
  var SUPPORT_ORACLES = ["supra", "switchboard", "pyth"];
99
99
  var SUPPORT_PACKAGES = [
@@ -174,7 +174,8 @@ var spoolRewardCoins = {
174
174
  };
175
175
  var borrowIncentiveRewardCoins = {
176
176
  sui: "sui",
177
- usdc: "sui"
177
+ usdc: "sui",
178
+ usdt: "sui"
178
179
  };
179
180
  var coinIds = {
180
181
  sui: "0x0000000000000000000000000000000000000000000000000000000000000002",
@@ -1115,6 +1116,16 @@ var minBigNumber = (...args) => {
1115
1116
  )
1116
1117
  );
1117
1118
  };
1119
+ var estimatedFactor = (amount, scaleStep, type) => {
1120
+ const amountOfDigits = Math.max(
1121
+ 1,
1122
+ Math.floor(Math.log10(Math.abs(amount)) + 1)
1123
+ );
1124
+ const adjustScale = Math.max(Math.floor((amountOfDigits - 1) / scaleStep), 1) + 1;
1125
+ let adjustFactor = Math.pow(10, -adjustScale);
1126
+ adjustFactor = type === "increase" ? 1 - adjustFactor : 1 + adjustFactor;
1127
+ return adjustFactor;
1128
+ };
1118
1129
 
1119
1130
  // src/utils/util.ts
1120
1131
  var isMarketCoin = (coinName) => {
@@ -2373,16 +2384,19 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2373
2384
  let totalBorrowedPools = 0;
2374
2385
  let totalBorrowedValue = (0, import_bignumber3.default)(0);
2375
2386
  let totalBorrowedValueWithWeight = (0, import_bignumber3.default)(0);
2376
- for (const collateral of obligationQuery.collaterals) {
2377
- const collateralCoinName = query.utils.parseCoinNameFromType(
2378
- collateral.type.name
2379
- );
2380
- const coinDecimal = query.utils.getCoinDecimal(collateralCoinName);
2381
- const marketCollateral = market.collaterals[collateralCoinName];
2382
- const coinPrice = coinPrices?.[collateralCoinName];
2383
- const coinAmount = coinAmounts?.[collateralCoinName] ?? 0;
2387
+ for (const assetCoinName of assetCoinNames) {
2388
+ const collateral = obligationQuery.collaterals.find((collateral2) => {
2389
+ const collateralCoinName = query.utils.parseCoinNameFromType(
2390
+ collateral2.type.name
2391
+ );
2392
+ return assetCoinName === collateralCoinName;
2393
+ });
2394
+ const marketCollateral = market.collaterals[assetCoinName];
2395
+ const coinDecimal = query.utils.getCoinDecimal(assetCoinName);
2396
+ const coinPrice = coinPrices?.[assetCoinName];
2397
+ const coinAmount = coinAmounts?.[assetCoinName] ?? 0;
2384
2398
  if (marketCollateral && coinPrice) {
2385
- const depositedAmount = (0, import_bignumber3.default)(collateral.amount);
2399
+ const depositedAmount = (0, import_bignumber3.default)(collateral?.amount ?? 0);
2386
2400
  const depositedCoin = depositedAmount.shiftedBy(-1 * coinDecimal);
2387
2401
  const depositedValue = depositedCoin.multipliedBy(coinPrice);
2388
2402
  const borrowCapacityValue = depositedValue.multipliedBy(
@@ -2403,10 +2417,10 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2403
2417
  if (depositedAmount.isGreaterThan(0)) {
2404
2418
  totalDepositedPools++;
2405
2419
  }
2406
- collaterals[collateralCoinName] = {
2407
- coinName: collateralCoinName,
2408
- coinType: collateral.type.name,
2409
- symbol: query.utils.parseSymbol(collateralCoinName),
2420
+ collaterals[assetCoinName] = {
2421
+ coinName: assetCoinName,
2422
+ coinType: query.utils.parseCoinType(assetCoinName),
2423
+ symbol: query.utils.parseSymbol(assetCoinName),
2410
2424
  coinDecimal,
2411
2425
  coinPrice,
2412
2426
  depositedAmount: depositedAmount.toNumber(),
@@ -2421,24 +2435,30 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2421
2435
  };
2422
2436
  }
2423
2437
  }
2424
- for (const debt of obligationQuery.debts) {
2425
- const poolCoinName = query.utils.parseCoinNameFromType(
2426
- debt.type.name
2427
- );
2428
- const coinDecimal = query.utils.getCoinDecimal(poolCoinName);
2429
- const marketPool = market.pools[poolCoinName];
2430
- const coinPrice = coinPrices?.[poolCoinName];
2438
+ for (const assetCoinName of assetCoinNames) {
2439
+ const debt = obligationQuery.debts.find((debt2) => {
2440
+ const poolCoinName = query.utils.parseCoinNameFromType(
2441
+ debt2.type.name
2442
+ );
2443
+ return assetCoinName === poolCoinName;
2444
+ });
2445
+ const marketPool = market.pools[assetCoinName];
2446
+ const coinDecimal = query.utils.getCoinDecimal(assetCoinName);
2447
+ const coinPrice = coinPrices?.[assetCoinName];
2448
+ const coinAmount = coinAmounts?.[assetCoinName] ?? 0;
2431
2449
  if (marketPool && coinPrice) {
2432
- const increasedRate = marketPool.borrowIndex / Number(debt.borrowIndex) - 1;
2433
- const borrowedAmount = (0, import_bignumber3.default)(debt.amount);
2450
+ const increasedRate = debt?.borrowIndex ? marketPool.borrowIndex / Number(debt.borrowIndex) - 1 : 0;
2451
+ const borrowedAmount = (0, import_bignumber3.default)(debt?.amount ?? 0);
2434
2452
  const borrowedCoin = borrowedAmount.shiftedBy(-1 * coinDecimal);
2435
- const availableRepayAmount = borrowedAmount.multipliedBy(
2453
+ const requiredRepayAmount = borrowedAmount.multipliedBy(
2436
2454
  increasedRate + 1
2437
2455
  );
2456
+ const requiredRepayCoin = requiredRepayAmount.shiftedBy(-1 * coinDecimal);
2457
+ const availableRepayAmount = (0, import_bignumber3.default)(coinAmount);
2438
2458
  const availableRepayCoin = availableRepayAmount.shiftedBy(
2439
2459
  -1 * coinDecimal
2440
2460
  );
2441
- const borrowedValue = availableRepayCoin.multipliedBy(coinPrice);
2461
+ const borrowedValue = requiredRepayCoin.multipliedBy(coinPrice);
2442
2462
  const borrowedValueWithWeight = borrowedValue.multipliedBy(
2443
2463
  marketPool.borrowWeight
2444
2464
  );
@@ -2449,17 +2469,19 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2449
2469
  if (borrowedAmount.isGreaterThan(0)) {
2450
2470
  totalBorrowedPools++;
2451
2471
  }
2452
- debts[poolCoinName] = {
2453
- coinName: poolCoinName,
2454
- coinType: debt.type.name,
2455
- symbol: query.utils.parseSymbol(poolCoinName),
2472
+ debts[assetCoinName] = {
2473
+ coinName: assetCoinName,
2474
+ coinType: query.utils.parseCoinType(assetCoinName),
2475
+ symbol: query.utils.parseSymbol(assetCoinName),
2456
2476
  coinDecimal,
2457
2477
  coinPrice,
2458
2478
  borrowedAmount: borrowedAmount.toNumber(),
2459
2479
  borrowedCoin: borrowedCoin.toNumber(),
2460
2480
  borrowedValue: borrowedValue.toNumber(),
2461
2481
  borrowedValueWithWeight: borrowedValueWithWeight.toNumber(),
2462
- borrowIndex: Number(debt.borrowIndex),
2482
+ borrowIndex: Number(debt?.borrowIndex ?? 0),
2483
+ requiredRepayAmount: requiredRepayAmount.toNumber(),
2484
+ requiredRepayCoin: requiredRepayCoin.toNumber(),
2463
2485
  availableBorrowAmount: 0,
2464
2486
  availableBorrowCoin: 0,
2465
2487
  availableRepayAmount: availableRepayAmount.toNumber(),
@@ -2502,8 +2524,8 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2502
2524
  }
2503
2525
  }
2504
2526
  }
2505
- let riskLevel = totalRequiredCollateralValue.isZero() && totalBorrowedValueWithWeight.isZero() ? (0, import_bignumber3.default)(0) : totalBorrowedValueWithWeight.dividedBy(totalRequiredCollateralValue);
2506
- riskLevel = riskLevel.isFinite() ? riskLevel.isLessThan(1) ? riskLevel : (0, import_bignumber3.default)(1) : (0, import_bignumber3.default)(1);
2527
+ let riskLevel = totalRequiredCollateralValue.isZero() ? (0, import_bignumber3.default)(0) : totalBorrowedValueWithWeight.dividedBy(totalRequiredCollateralValue);
2528
+ riskLevel = riskLevel.isLessThan(1) ? riskLevel : (0, import_bignumber3.default)(1);
2507
2529
  const accountBalanceValue = totalDepositedValue.minus(totalBorrowedValue).isGreaterThan(0) ? totalDepositedValue.minus(totalBorrowedValue) : (0, import_bignumber3.default)(0);
2508
2530
  const availableCollateralValue = totalBorrowCapacityValue.minus(totalBorrowedValueWithWeight).isGreaterThan(0) ? totalBorrowCapacityValue.minus(totalBorrowedValueWithWeight) : (0, import_bignumber3.default)(0);
2509
2531
  const requiredCollateralValue = totalBorrowedValueWithWeight.isGreaterThan(0) ? totalRequiredCollateralValue : (0, import_bignumber3.default)(0);
@@ -2538,13 +2560,25 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2538
2560
  )) {
2539
2561
  const marketCollateral = market.collaterals[collateralCoinName];
2540
2562
  if (marketCollateral) {
2541
- const availableWithdrawAmount = obligationAccount.totalBorrowedValueWithWeight === 0 ? (0, import_bignumber3.default)(obligationCollateral.depositedAmount) : minBigNumber(
2542
- (0, import_bignumber3.default)(obligationAccount.totalAvailableCollateralValue).dividedBy(marketCollateral.collateralFactor).dividedBy(marketCollateral.coinPrice).multipliedBy(0.99).toNumber(),
2563
+ let estimatedAvailableWithdrawAmount = (0, import_bignumber3.default)(
2564
+ obligationAccount.totalAvailableCollateralValue
2565
+ ).dividedBy(marketCollateral.collateralFactor).dividedBy(marketCollateral.coinPrice).shiftedBy(marketCollateral.coinDecimal);
2566
+ estimatedAvailableWithdrawAmount = obligationAccount.totalBorrowedValueWithWeight === 0 ? (
2567
+ // Note: when there is no debt record, there is no need to estimate and the deposited amount is directly used as available withdraw.
2568
+ (0, import_bignumber3.default)(obligationCollateral.depositedAmount)
2569
+ ) : minBigNumber(
2570
+ estimatedAvailableWithdrawAmount.multipliedBy(
2571
+ estimatedFactor(
2572
+ (0, import_bignumber3.default)(obligationAccount.totalAvailableCollateralValue).dividedBy(marketCollateral.collateralFactor).toNumber(),
2573
+ 3,
2574
+ "increase"
2575
+ )
2576
+ ).toNumber(),
2543
2577
  obligationCollateral.depositedAmount,
2544
2578
  marketCollateral.depositAmount
2545
2579
  );
2546
- obligationCollateral.availableWithdrawAmount = availableWithdrawAmount.toNumber();
2547
- obligationCollateral.availableWithdrawCoin = availableWithdrawAmount.shiftedBy(-1 * obligationCollateral.coinDecimal).toNumber();
2580
+ obligationCollateral.availableWithdrawAmount = estimatedAvailableWithdrawAmount.toNumber();
2581
+ obligationCollateral.availableWithdrawCoin = estimatedAvailableWithdrawAmount.shiftedBy(-1 * obligationCollateral.coinDecimal).toNumber();
2548
2582
  }
2549
2583
  }
2550
2584
  for (const [poolCoinName, obligationDebt] of Object.entries(
@@ -2552,21 +2586,28 @@ var getObligationAccount = async (query, obligationId, ownerAddress, market, coi
2552
2586
  )) {
2553
2587
  const marketPool = market.pools[poolCoinName];
2554
2588
  if (marketPool) {
2555
- const availableRepayAmount = (0, import_bignumber3.default)(
2556
- obligationDebt.availableRepayAmount
2557
- ).multipliedBy(1.01);
2558
- const availableBorrowAmount = obligationAccount.totalAvailableCollateralValue !== 0 ? minBigNumber(
2559
- (0, import_bignumber3.default)(obligationAccount.totalAvailableCollateralValue).dividedBy(
2560
- (0, import_bignumber3.default)(marketPool.coinPrice).multipliedBy(
2561
- marketPool.borrowWeight
2589
+ const estimatedRequiredRepayAmount = (0, import_bignumber3.default)(
2590
+ obligationDebt.requiredRepayAmount
2591
+ ).multipliedBy(
2592
+ estimatedFactor(obligationDebt.borrowedValue, 3, "decrease")
2593
+ );
2594
+ let estimatedAvailableBorrowAmount = (0, import_bignumber3.default)(
2595
+ obligationAccount.totalAvailableCollateralValue
2596
+ ).dividedBy(marketPool.borrowWeight).shiftedBy(marketPool.coinDecimal).dividedBy(marketPool.coinPrice);
2597
+ estimatedAvailableBorrowAmount = obligationAccount.totalAvailableCollateralValue !== 0 ? minBigNumber(
2598
+ estimatedAvailableBorrowAmount.multipliedBy(
2599
+ estimatedFactor(
2600
+ estimatedAvailableBorrowAmount.shiftedBy(-1 * marketPool.coinDecimal).multipliedBy(marketPool.coinPrice).toNumber(),
2601
+ 3,
2602
+ "increase"
2562
2603
  )
2563
- ).multipliedBy(0.99).toNumber(),
2604
+ ).toNumber(),
2564
2605
  marketPool.supplyAmount
2565
2606
  ) : (0, import_bignumber3.default)(0);
2566
- obligationDebt.availableBorrowAmount = availableBorrowAmount.toNumber();
2567
- obligationDebt.availableBorrowCoin = availableBorrowAmount.shiftedBy(-1 * obligationDebt.coinDecimal).toNumber();
2568
- obligationDebt.availableRepayAmount = availableRepayAmount.toNumber();
2569
- obligationDebt.availableRepayCoin = availableRepayAmount.shiftedBy(-1 * obligationDebt.coinDecimal).toNumber();
2607
+ obligationDebt.availableBorrowAmount = estimatedAvailableBorrowAmount.toNumber();
2608
+ obligationDebt.availableBorrowCoin = estimatedAvailableBorrowAmount.shiftedBy(-1 * obligationDebt.coinDecimal).toNumber();
2609
+ obligationDebt.requiredRepayAmount = estimatedRequiredRepayAmount.toNumber();
2610
+ obligationDebt.requiredRepayCoin = estimatedRequiredRepayAmount.shiftedBy(-1 * obligationDebt.coinDecimal).toNumber();
2570
2611
  }
2571
2612
  }
2572
2613
  return obligationAccount;
@@ -2910,10 +2951,11 @@ var ScallopQuery = class {
2910
2951
  * borrowing and obligation information for specific pool.
2911
2952
  *
2912
2953
  * @param obligationId - The obligation id.
2954
+ * @param ownerAddress - The owner address.
2913
2955
  * @return Borrowing and collateral information.
2914
2956
  */
2915
- async getObligationAccount(obligationId) {
2916
- return await getObligationAccount(this, obligationId);
2957
+ async getObligationAccount(obligationId, ownerAddress) {
2958
+ return await getObligationAccount(this, obligationId, ownerAddress);
2917
2959
  }
2918
2960
  /**
2919
2961
  * Get total value locked.
@@ -4407,6 +4449,14 @@ var ScallopClient = class {
4407
4449
  const txBlock = this.builder.createTxBlock();
4408
4450
  const sender = walletAddress || this.walletAddress;
4409
4451
  txBlock.setSender(sender);
4452
+ const availableStake = SUPPORT_BORROW_INCENTIVE_POOLS.includes(poolCoinName);
4453
+ if (sign && availableStake) {
4454
+ await txBlock.unstakeObligationQuick(
4455
+ poolCoinName,
4456
+ obligationId,
4457
+ obligationKey
4458
+ );
4459
+ }
4410
4460
  const coin = await txBlock.borrowQuick(
4411
4461
  amount,
4412
4462
  poolCoinName,
@@ -4414,6 +4464,13 @@ var ScallopClient = class {
4414
4464
  obligationKey
4415
4465
  );
4416
4466
  txBlock.transferObjects([coin], sender);
4467
+ if (sign && availableStake) {
4468
+ await txBlock.stakeObligationQuick(
4469
+ poolCoinName,
4470
+ obligationId,
4471
+ obligationKey
4472
+ );
4473
+ }
4417
4474
  if (sign) {
4418
4475
  return await this.suiKit.signAndSendTxn(
4419
4476
  txBlock
@@ -4432,11 +4489,26 @@ var ScallopClient = class {
4432
4489
  * @param walletAddress - The wallet address of the owner.
4433
4490
  * @return Transaction block response or transaction block.
4434
4491
  */
4435
- async repay(poolCoinName, amount, sign = true, obligationId, walletAddress) {
4492
+ async repay(poolCoinName, amount, sign = true, obligationId, obligationKey, walletAddress) {
4436
4493
  const txBlock = this.builder.createTxBlock();
4437
4494
  const sender = walletAddress || this.walletAddress;
4438
4495
  txBlock.setSender(sender);
4496
+ const availableStake = SUPPORT_BORROW_INCENTIVE_POOLS.includes(poolCoinName);
4497
+ if (sign && availableStake) {
4498
+ await txBlock.unstakeObligationQuick(
4499
+ poolCoinName,
4500
+ obligationId,
4501
+ obligationKey
4502
+ );
4503
+ }
4439
4504
  await txBlock.repayQuick(amount, poolCoinName, obligationId);
4505
+ if (sign && availableStake) {
4506
+ await txBlock.stakeObligationQuick(
4507
+ poolCoinName,
4508
+ obligationId,
4509
+ obligationKey
4510
+ );
4511
+ }
4440
4512
  if (sign) {
4441
4513
  return await this.suiKit.signAndSendTxn(
4442
4514
  txBlock