@scallop-io/sui-scallop-sdk 0.42.6 → 0.42.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.
- package/dist/index.js +60 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +60 -54
- package/dist/index.mjs.map +1 -1
- package/dist/types/query/core.d.ts +1 -1
- package/dist/types/query/portfolio.d.ts +23 -22
- package/dist/types/query/spool.d.ts +2 -2
- package/package.json +1 -1
- package/src/queries/portfolioQuery.ts +61 -55
- package/src/queries/spoolQuery.ts +2 -2
- package/src/types/query/core.ts +1 -1
- package/src/types/query/portfolio.ts +23 -22
- package/src/types/query/spool.ts +2 -2
- package/src/utils/query.ts +4 -4
|
@@ -24,25 +24,25 @@ export type Lending = Required<Pick<MarketPool, 'coinName' | 'symbol' | 'coinTyp
|
|
|
24
24
|
availableClaimAmount: number;
|
|
25
25
|
};
|
|
26
26
|
export type ObligationAccount = {
|
|
27
|
-
obligationId
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
totalDebtValue: number;
|
|
27
|
+
obligationId: string;
|
|
28
|
+
totalDepositedValue: number;
|
|
29
|
+
totalBorrowedValue: number;
|
|
31
30
|
totalBalanceValue: number;
|
|
32
31
|
totalBorrowCapacityValue: number;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
collaterals: OptionalKeys<Record<
|
|
41
|
-
coinName:
|
|
32
|
+
totalAvailableCollateralValue: number;
|
|
33
|
+
totalBorrowedValueWithWeight: number;
|
|
34
|
+
totalRequiredCollateralValue: number;
|
|
35
|
+
totalUnhealthyCollateralValue: number;
|
|
36
|
+
totalRiskLevel: number;
|
|
37
|
+
totalDepositedPools: number;
|
|
38
|
+
totalBorrowedPools: number;
|
|
39
|
+
collaterals: OptionalKeys<Record<SupportCollateralCoins, {
|
|
40
|
+
coinName: SupportCollateralCoins;
|
|
42
41
|
coinType: string;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
symbol: string;
|
|
43
|
+
depositedAmount: number;
|
|
44
|
+
depositedCoin: number;
|
|
45
|
+
depositedValue: number;
|
|
46
46
|
borrowCapacityValue: number;
|
|
47
47
|
requiredCollateralValue: number;
|
|
48
48
|
availableDepositAmount: number;
|
|
@@ -50,13 +50,14 @@ export type ObligationAccount = {
|
|
|
50
50
|
availableWithdrawAmount: number;
|
|
51
51
|
availableWithdrawCoin: number;
|
|
52
52
|
}>>;
|
|
53
|
-
debts: OptionalKeys<Record<
|
|
54
|
-
coinName:
|
|
53
|
+
debts: OptionalKeys<Record<SupportPoolCoins, {
|
|
54
|
+
coinName: SupportPoolCoins;
|
|
55
55
|
coinType: string;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
symbol: string;
|
|
57
|
+
borrowedAmount: number;
|
|
58
|
+
borrowedCoin: number;
|
|
59
|
+
borrowedValue: number;
|
|
60
|
+
borrowedValueWithWeight: number;
|
|
60
61
|
borrowIndex: number;
|
|
61
62
|
availableBorrowAmount: number;
|
|
62
63
|
availableBorrowCoin: number;
|
|
@@ -7,8 +7,8 @@ export type RewardPools = OptionalKeys<Record<SupportStakeMarketCoins, RewardPoo
|
|
|
7
7
|
export type StakeAccounts = Record<SupportStakeMarketCoins, StakeAccount[]>;
|
|
8
8
|
export type Spools = OptionalKeys<Record<SupportStakeMarketCoins, Spool>>;
|
|
9
9
|
export type Spool = {
|
|
10
|
-
|
|
11
|
-
symbol:
|
|
10
|
+
marketCoinName: SupportStakeMarketCoins;
|
|
11
|
+
symbol: string;
|
|
12
12
|
coinType: string;
|
|
13
13
|
marketCoinType: string;
|
|
14
14
|
rewardCoinType: string;
|
package/package.json
CHANGED
|
@@ -275,13 +275,13 @@ export const getObligationAccount = async (
|
|
|
275
275
|
|
|
276
276
|
const collaterals: ObligationAccount['collaterals'] = {};
|
|
277
277
|
const debts: ObligationAccount['debts'] = {};
|
|
278
|
-
let
|
|
279
|
-
let
|
|
278
|
+
let totalDepositedPools = 0;
|
|
279
|
+
let totalDepositedValue = BigNumber(0);
|
|
280
280
|
let totalBorrowCapacityValue = BigNumber(0);
|
|
281
281
|
let totalRequiredCollateralValue = BigNumber(0);
|
|
282
|
-
let
|
|
283
|
-
let
|
|
284
|
-
let
|
|
282
|
+
let totalBorrowedPools = 0;
|
|
283
|
+
let totalBorrowedValue = BigNumber(0);
|
|
284
|
+
let totalBorrowedValueWithWeight = BigNumber(0);
|
|
285
285
|
|
|
286
286
|
for (const collateral of obligationQuery.collaterals) {
|
|
287
287
|
const collateralCoinName =
|
|
@@ -294,13 +294,13 @@ export const getObligationAccount = async (
|
|
|
294
294
|
const coinAmount = coinAmounts?.[collateralCoinName] ?? 0;
|
|
295
295
|
|
|
296
296
|
if (marketCollateral && coinPrice) {
|
|
297
|
-
const
|
|
298
|
-
const
|
|
299
|
-
const
|
|
300
|
-
const borrowCapacityValue =
|
|
297
|
+
const depositedAmount = BigNumber(collateral.amount);
|
|
298
|
+
const depositedCoin = depositedAmount.shiftedBy(-1 * coinDecimal);
|
|
299
|
+
const depositedValue = depositedCoin.multipliedBy(coinPrice);
|
|
300
|
+
const borrowCapacityValue = depositedValue.multipliedBy(
|
|
301
301
|
marketCollateral.collateralFactor
|
|
302
302
|
);
|
|
303
|
-
const requiredCollateralValue =
|
|
303
|
+
const requiredCollateralValue = depositedValue.multipliedBy(
|
|
304
304
|
marketCollateral.liquidationFactor
|
|
305
305
|
);
|
|
306
306
|
const availableDepositAmount = BigNumber(coinAmount);
|
|
@@ -308,23 +308,24 @@ export const getObligationAccount = async (
|
|
|
308
308
|
-1 * coinDecimal
|
|
309
309
|
);
|
|
310
310
|
|
|
311
|
-
|
|
311
|
+
totalDepositedValue = totalDepositedValue.plus(depositedValue);
|
|
312
312
|
totalBorrowCapacityValue =
|
|
313
313
|
totalBorrowCapacityValue.plus(borrowCapacityValue);
|
|
314
314
|
totalRequiredCollateralValue = totalRequiredCollateralValue.plus(
|
|
315
315
|
requiredCollateralValue
|
|
316
316
|
);
|
|
317
317
|
|
|
318
|
-
if (
|
|
319
|
-
|
|
318
|
+
if (depositedAmount.isGreaterThan(0)) {
|
|
319
|
+
totalDepositedPools++;
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
collaterals[collateralCoinName] = {
|
|
323
323
|
coinName: collateralCoinName,
|
|
324
324
|
coinType: collateral.type.name,
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
325
|
+
symbol: query.utils.parseSymbol(collateralCoinName),
|
|
326
|
+
depositedAmount: depositedAmount.toNumber(),
|
|
327
|
+
depositedCoin: depositedCoin.toNumber(),
|
|
328
|
+
depositedValue: depositedValue.toNumber(),
|
|
328
329
|
borrowCapacityValue: borrowCapacityValue.toNumber(),
|
|
329
330
|
requiredCollateralValue: requiredCollateralValue.toNumber(),
|
|
330
331
|
availableDepositAmount: availableDepositAmount.toNumber(),
|
|
@@ -346,32 +347,36 @@ export const getObligationAccount = async (
|
|
|
346
347
|
if (marketPool && coinPrice) {
|
|
347
348
|
const increasedRate =
|
|
348
349
|
marketPool.borrowIndex / Number(debt.borrowIndex) - 1;
|
|
349
|
-
const
|
|
350
|
-
const
|
|
351
|
-
const availableRepayAmount =
|
|
350
|
+
const borrowedAmount = BigNumber(debt.amount);
|
|
351
|
+
const borrowedCoin = borrowedAmount.shiftedBy(-1 * coinDecimal);
|
|
352
|
+
const availableRepayAmount = borrowedAmount.multipliedBy(
|
|
353
|
+
increasedRate + 1
|
|
354
|
+
);
|
|
352
355
|
const availableRepayCoin = availableRepayAmount.shiftedBy(
|
|
353
356
|
-1 * coinDecimal
|
|
354
357
|
);
|
|
355
|
-
const
|
|
356
|
-
const
|
|
358
|
+
const borrowedValue = availableRepayCoin.multipliedBy(coinPrice);
|
|
359
|
+
const borrowedValueWithWeight = borrowedValue.multipliedBy(
|
|
357
360
|
marketPool.borrowWeight
|
|
358
361
|
);
|
|
359
362
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
+
totalBorrowedValue = totalBorrowedValue.plus(borrowedValue);
|
|
364
|
+
totalBorrowedValueWithWeight = totalBorrowedValueWithWeight.plus(
|
|
365
|
+
borrowedValueWithWeight
|
|
366
|
+
);
|
|
363
367
|
|
|
364
|
-
if (
|
|
365
|
-
|
|
368
|
+
if (borrowedAmount.isGreaterThan(0)) {
|
|
369
|
+
totalBorrowedPools++;
|
|
366
370
|
}
|
|
367
371
|
|
|
368
372
|
debts[poolCoinName] = {
|
|
369
373
|
coinName: poolCoinName,
|
|
370
374
|
coinType: debt.type.name,
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
+
symbol: query.utils.parseSymbol(poolCoinName),
|
|
376
|
+
borrowedAmount: borrowedAmount.toNumber(),
|
|
377
|
+
borrowedCoin: borrowedCoin.toNumber(),
|
|
378
|
+
borrowedValue: borrowedValue.toNumber(),
|
|
379
|
+
borrowedValueWithWeight: borrowedValueWithWeight.toNumber(),
|
|
375
380
|
borrowIndex: Number(debt.borrowIndex),
|
|
376
381
|
availableBorrowAmount: 0,
|
|
377
382
|
availableBorrowCoin: 0,
|
|
@@ -382,55 +387,56 @@ export const getObligationAccount = async (
|
|
|
382
387
|
}
|
|
383
388
|
|
|
384
389
|
let riskLevel =
|
|
385
|
-
totalRequiredCollateralValue.isZero() &&
|
|
390
|
+
totalRequiredCollateralValue.isZero() &&
|
|
391
|
+
totalBorrowedValueWithWeight.isZero()
|
|
386
392
|
? BigNumber(0)
|
|
387
|
-
:
|
|
393
|
+
: totalBorrowedValueWithWeight.dividedBy(totalRequiredCollateralValue);
|
|
388
394
|
riskLevel = riskLevel.isFinite()
|
|
389
395
|
? riskLevel.isLessThan(1)
|
|
390
396
|
? riskLevel
|
|
391
397
|
: BigNumber(1)
|
|
392
398
|
: BigNumber(1);
|
|
393
399
|
|
|
394
|
-
const accountBalanceValue =
|
|
395
|
-
.minus(
|
|
400
|
+
const accountBalanceValue = totalDepositedValue
|
|
401
|
+
.minus(totalBorrowedValue)
|
|
396
402
|
.isGreaterThan(0)
|
|
397
|
-
?
|
|
403
|
+
? totalDepositedValue.minus(totalBorrowedValue)
|
|
398
404
|
: BigNumber(0);
|
|
399
405
|
const availableCollateralValue = totalBorrowCapacityValue
|
|
400
|
-
.minus(
|
|
406
|
+
.minus(totalBorrowedValueWithWeight)
|
|
401
407
|
.isGreaterThan(0)
|
|
402
|
-
? totalBorrowCapacityValue.minus(
|
|
408
|
+
? totalBorrowCapacityValue.minus(totalBorrowedValueWithWeight)
|
|
403
409
|
: BigNumber(0);
|
|
404
|
-
const requiredCollateralValue =
|
|
410
|
+
const requiredCollateralValue = totalBorrowedValueWithWeight.isGreaterThan(0)
|
|
405
411
|
? totalRequiredCollateralValue
|
|
406
412
|
: BigNumber(0);
|
|
407
|
-
const unhealthyCollateralValue =
|
|
413
|
+
const unhealthyCollateralValue = totalBorrowedValueWithWeight
|
|
408
414
|
.minus(requiredCollateralValue)
|
|
409
415
|
.isGreaterThan(0)
|
|
410
|
-
?
|
|
416
|
+
? totalBorrowedValueWithWeight.minus(requiredCollateralValue)
|
|
411
417
|
: BigNumber(0);
|
|
412
418
|
|
|
413
419
|
const obligationAccount: ObligationAccount = {
|
|
414
420
|
obligationId: obligationId,
|
|
415
421
|
// Deposited collateral value (collateral balance)
|
|
416
|
-
|
|
422
|
+
totalDepositedValue: totalDepositedValue.toNumber(),
|
|
417
423
|
// Borrowed debt value (liabilities balance)
|
|
418
|
-
|
|
424
|
+
totalBorrowedValue: totalBorrowedValue.toNumber(),
|
|
419
425
|
// The difference between the user’s actual deposit and loan (remaining balance)
|
|
420
426
|
totalBalanceValue: accountBalanceValue.toNumber(),
|
|
421
427
|
// Effective collateral value (the actual collateral value included in the calculation).
|
|
422
428
|
totalBorrowCapacityValue: totalBorrowCapacityValue.toNumber(),
|
|
423
429
|
// Available collateral value (the remaining collateral value that can be borrowed).
|
|
424
|
-
|
|
430
|
+
totalAvailableCollateralValue: availableCollateralValue.toNumber(),
|
|
425
431
|
// Available debt value (the actual borrowing value included in the calculation).
|
|
426
|
-
|
|
432
|
+
totalBorrowedValueWithWeight: totalBorrowedValueWithWeight.toNumber(),
|
|
427
433
|
// Required collateral value (avoid be liquidated).
|
|
428
|
-
|
|
434
|
+
totalRequiredCollateralValue: requiredCollateralValue.toNumber(),
|
|
429
435
|
// Unliquidated collateral value (pending liquidation).
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
436
|
+
totalUnhealthyCollateralValue: unhealthyCollateralValue.toNumber(),
|
|
437
|
+
totalRiskLevel: riskLevel.toNumber(),
|
|
438
|
+
totalDepositedPools,
|
|
439
|
+
totalBorrowedPools,
|
|
434
440
|
collaterals,
|
|
435
441
|
debts,
|
|
436
442
|
};
|
|
@@ -442,16 +448,16 @@ export const getObligationAccount = async (
|
|
|
442
448
|
market.collaterals[collateralCoinName as SupportCollateralCoins];
|
|
443
449
|
if (marketCollateral) {
|
|
444
450
|
const availableWithdrawAmount =
|
|
445
|
-
obligationAccount.
|
|
446
|
-
? obligationCollateral.
|
|
451
|
+
obligationAccount.totalBorrowedValueWithWeight === 0
|
|
452
|
+
? obligationCollateral.depositedAmount
|
|
447
453
|
: Math.min(
|
|
448
|
-
BigNumber(obligationAccount.
|
|
454
|
+
BigNumber(obligationAccount.totalAvailableCollateralValue)
|
|
449
455
|
.dividedBy(marketCollateral.collateralFactor)
|
|
450
456
|
.dividedBy(marketCollateral.coinPrice)
|
|
451
457
|
// Note: reduced chance of failure when calculations are inaccurate
|
|
452
458
|
.multipliedBy(0.99)
|
|
453
459
|
.toNumber(),
|
|
454
|
-
obligationCollateral.
|
|
460
|
+
obligationCollateral.depositedAmount,
|
|
455
461
|
marketCollateral.depositAmount
|
|
456
462
|
);
|
|
457
463
|
obligationCollateral.availableWithdrawAmount = availableWithdrawAmount;
|
|
@@ -471,9 +477,9 @@ export const getObligationAccount = async (
|
|
|
471
477
|
.toNumber();
|
|
472
478
|
|
|
473
479
|
const availableBorrowAmount =
|
|
474
|
-
obligationAccount.
|
|
480
|
+
obligationAccount.totalAvailableCollateralValue !== 0
|
|
475
481
|
? Math.min(
|
|
476
|
-
BigNumber(obligationAccount.
|
|
482
|
+
BigNumber(obligationAccount.totalAvailableCollateralValue)
|
|
477
483
|
.dividedBy(
|
|
478
484
|
BigNumber(marketPool.coinPrice).multipliedBy(
|
|
479
485
|
marketPool.borrowWeight
|
|
@@ -142,8 +142,8 @@ export const getSpool = async (
|
|
|
142
142
|
);
|
|
143
143
|
|
|
144
144
|
spool = {
|
|
145
|
-
|
|
146
|
-
symbol: query.utils.parseSymbol(
|
|
145
|
+
marketCoinName: stakeMarketCoinName,
|
|
146
|
+
symbol: query.utils.parseSymbol(stakeMarketCoinName),
|
|
147
147
|
coinType: query.utils.parseCoinType(stakeCoinName),
|
|
148
148
|
marketCoinType: query.utils.parseMarketCoinType(stakeCoinName),
|
|
149
149
|
rewardCoinType: isMarketCoin(rewardCoin)
|
package/src/types/query/core.ts
CHANGED
|
@@ -40,28 +40,28 @@ export type Lending = Required<
|
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
export type ObligationAccount = {
|
|
43
|
-
obligationId
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
totalDebtValue: number;
|
|
43
|
+
obligationId: string;
|
|
44
|
+
totalDepositedValue: number;
|
|
45
|
+
totalBorrowedValue: number;
|
|
47
46
|
totalBalanceValue: number;
|
|
48
47
|
totalBorrowCapacityValue: number;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
totalAvailableCollateralValue: number;
|
|
49
|
+
totalBorrowedValueWithWeight: number;
|
|
50
|
+
totalRequiredCollateralValue: number;
|
|
51
|
+
totalUnhealthyCollateralValue: number;
|
|
52
|
+
totalRiskLevel: number;
|
|
53
|
+
totalDepositedPools: number;
|
|
54
|
+
totalBorrowedPools: number;
|
|
56
55
|
collaterals: OptionalKeys<
|
|
57
56
|
Record<
|
|
58
|
-
|
|
57
|
+
SupportCollateralCoins,
|
|
59
58
|
{
|
|
60
|
-
coinName:
|
|
59
|
+
coinName: SupportCollateralCoins;
|
|
61
60
|
coinType: string;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
symbol: string;
|
|
62
|
+
depositedAmount: number;
|
|
63
|
+
depositedCoin: number;
|
|
64
|
+
depositedValue: number;
|
|
65
65
|
borrowCapacityValue: number;
|
|
66
66
|
requiredCollateralValue: number;
|
|
67
67
|
availableDepositAmount: number;
|
|
@@ -73,14 +73,15 @@ export type ObligationAccount = {
|
|
|
73
73
|
>;
|
|
74
74
|
debts: OptionalKeys<
|
|
75
75
|
Record<
|
|
76
|
-
|
|
76
|
+
SupportPoolCoins,
|
|
77
77
|
{
|
|
78
|
-
coinName:
|
|
78
|
+
coinName: SupportPoolCoins;
|
|
79
79
|
coinType: string;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
symbol: string;
|
|
81
|
+
borrowedAmount: number;
|
|
82
|
+
borrowedCoin: number;
|
|
83
|
+
borrowedValue: number;
|
|
84
|
+
borrowedValueWithWeight: number;
|
|
84
85
|
borrowIndex: number;
|
|
85
86
|
availableBorrowAmount: number;
|
|
86
87
|
availableBorrowCoin: number;
|
package/src/types/query/spool.ts
CHANGED
|
@@ -14,8 +14,8 @@ export type StakeAccounts = Record<SupportStakeMarketCoins, StakeAccount[]>;
|
|
|
14
14
|
export type Spools = OptionalKeys<Record<SupportStakeMarketCoins, Spool>>;
|
|
15
15
|
|
|
16
16
|
export type Spool = {
|
|
17
|
-
|
|
18
|
-
symbol:
|
|
17
|
+
marketCoinName: SupportStakeMarketCoins;
|
|
18
|
+
symbol: string;
|
|
19
19
|
coinType: string;
|
|
20
20
|
marketCoinType: string;
|
|
21
21
|
rewardCoinType: string;
|
package/src/utils/query.ts
CHANGED
|
@@ -101,12 +101,12 @@ export const calculateMarketPoolData = (
|
|
|
101
101
|
parsedMarketPoolData.debtAmount
|
|
102
102
|
);
|
|
103
103
|
const borrowCoin = borrowAmount.shiftedBy(-1 * coinDecimal);
|
|
104
|
-
const
|
|
104
|
+
const reserveAmount = BigNumber(parsedMarketPoolData.reserveAmount).plus(
|
|
105
105
|
increasedDebtAmount.multipliedBy(parsedMarketPoolData.reserveFactor)
|
|
106
106
|
);
|
|
107
|
-
const reserveCoin =
|
|
107
|
+
const reserveCoin = reserveAmount.shiftedBy(-1 * coinDecimal);
|
|
108
108
|
const supplyAmount = BigNumber(borrowAmount).plus(
|
|
109
|
-
Math.max(parsedMarketPoolData.cashAmount -
|
|
109
|
+
Math.max(parsedMarketPoolData.cashAmount - reserveAmount.toNumber(), 0)
|
|
110
110
|
);
|
|
111
111
|
const supplyCoin = supplyAmount.shiftedBy(-1 * coinDecimal);
|
|
112
112
|
let utilizationRate = BigNumber(borrowAmount).dividedBy(supplyAmount);
|
|
@@ -143,7 +143,7 @@ export const calculateMarketPoolData = (
|
|
|
143
143
|
supplyCoin: supplyCoin.toNumber(),
|
|
144
144
|
borrowAmount: borrowAmount.toNumber(),
|
|
145
145
|
borrowCoin: borrowCoin.toNumber(),
|
|
146
|
-
reserveAmount:
|
|
146
|
+
reserveAmount: reserveAmount.toNumber(),
|
|
147
147
|
reserveCoin: reserveCoin.toNumber(),
|
|
148
148
|
utilizationRate: utilizationRate.toNumber(),
|
|
149
149
|
supplyApr: supplyApr.toNumber(),
|