@scallop-io/sui-scallop-sdk 1.3.5-rc.1 → 1.4.0
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/constants/common.d.ts +1 -1
- package/dist/constants/enum.d.ts +1 -1
- package/dist/constants/tokenBucket.d.ts +1 -1
- package/dist/index.js +299 -340
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +298 -339
- package/dist/index.mjs.map +1 -1
- package/dist/models/scallopClient.d.ts +4 -4
- package/dist/models/scallopQuery.d.ts +35 -57
- package/dist/models/scallopUtils.d.ts +10 -10
- package/dist/queries/borrowIncentiveQuery.d.ts +2 -2
- package/dist/queries/priceQuery.d.ts +2 -36
- package/dist/types/builder/borrowIncentive.d.ts +6 -6
- package/dist/types/constant/common.d.ts +1 -1
- package/dist/types/utils.d.ts +6 -2
- package/package.json +1 -1
- package/src/builders/borrowIncentiveBuilder.ts +13 -2
- package/src/constants/common.ts +2 -4
- package/src/constants/enum.ts +16 -11
- package/src/constants/tokenBucket.ts +1 -1
- package/src/models/scallopClient.ts +10 -27
- package/src/models/scallopQuery.ts +38 -44
- package/src/models/scallopUtils.ts +18 -23
- package/src/queries/borrowIncentiveQuery.ts +12 -29
- package/src/queries/coreQuery.ts +191 -191
- package/src/queries/portfolioQuery.ts +78 -80
- package/src/queries/priceQuery.ts +2 -36
- package/src/queries/sCoinQuery.ts +3 -3
- package/src/queries/spoolQuery.ts +6 -4
- package/src/types/builder/borrowIncentive.ts +15 -10
- package/src/types/constant/common.ts +2 -1
- package/src/types/utils.ts +10 -2
- package/src/utils/indexer.ts +9 -3
- package/src/utils/query.ts +87 -0
- package/src/utils/tokenBucket.ts +2 -2
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { SuiObjectData } from '@mysten/sui/client';
|
|
2
|
-
import type { ScallopAddress
|
|
3
|
-
import type {
|
|
4
|
-
CoinPrices,
|
|
5
|
-
MarketPools,
|
|
6
|
-
OptionalKeys,
|
|
7
|
-
SupportAssetCoins,
|
|
8
|
-
SupportSCoin,
|
|
9
|
-
} from '../types';
|
|
10
|
-
import { SUPPORT_SCOIN } from 'src/constants/common';
|
|
11
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
+
import type { ScallopAddress } from '../models';
|
|
3
|
+
import type { SupportAssetCoins } from '../types';
|
|
12
4
|
|
|
13
5
|
/**
|
|
14
6
|
* Get price from pyth fee object.
|
|
@@ -134,29 +126,3 @@ export const getPythPrices = async (
|
|
|
134
126
|
{} as Record<SupportAssetCoins, number>
|
|
135
127
|
);
|
|
136
128
|
};
|
|
137
|
-
|
|
138
|
-
export const getAllCoinPrices = async (
|
|
139
|
-
query: ScallopQuery,
|
|
140
|
-
marketPools?: MarketPools,
|
|
141
|
-
coinPrices?: CoinPrices
|
|
142
|
-
) => {
|
|
143
|
-
coinPrices = coinPrices ?? (await query.utils.getCoinPrices());
|
|
144
|
-
marketPools =
|
|
145
|
-
marketPools ??
|
|
146
|
-
(await query.getMarketPools(undefined, undefined, { coinPrices }));
|
|
147
|
-
if (!marketPools) {
|
|
148
|
-
throw new Error(`Failed to fetch market pool for getAllCoinPrices`);
|
|
149
|
-
}
|
|
150
|
-
const sCoinPrices: OptionalKeys<Record<SupportSCoin, number>> = {};
|
|
151
|
-
SUPPORT_SCOIN.forEach((sCoinName) => {
|
|
152
|
-
const coinName = query.utils.parseCoinName(sCoinName);
|
|
153
|
-
sCoinPrices[sCoinName] = BigNumber(coinPrices[coinName] ?? 0)
|
|
154
|
-
.multipliedBy(marketPools[coinName]?.conversionRate ?? 1)
|
|
155
|
-
.toNumber();
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
...coinPrices,
|
|
160
|
-
...sCoinPrices,
|
|
161
|
-
};
|
|
162
|
-
};
|
|
@@ -139,8 +139,8 @@ export const getSCoinSwapRate = async (
|
|
|
139
139
|
|
|
140
140
|
// Get lending data for both sCoin A and sCoin B
|
|
141
141
|
const marketPools = await Promise.all([
|
|
142
|
-
query.getMarketPool(fromCoinName
|
|
143
|
-
query.getMarketPool(toCoinName
|
|
142
|
+
query.getMarketPool(fromCoinName),
|
|
143
|
+
query.getMarketPool(toCoinName),
|
|
144
144
|
]);
|
|
145
145
|
if (marketPools.some((pool) => !pool))
|
|
146
146
|
throw new Error('Failed to fetch the lendings data');
|
|
@@ -153,7 +153,7 @@ export const getSCoinSwapRate = async (
|
|
|
153
153
|
const BtoSCoinBRate = 1 / marketPools[1]!.conversionRate;
|
|
154
154
|
|
|
155
155
|
const calcAtoBRate = async () => {
|
|
156
|
-
const prices = await query.utils.getCoinPrices();
|
|
156
|
+
const prices = await query.utils.getCoinPrices([fromCoinName, toCoinName]);
|
|
157
157
|
if (!prices[fromCoinName] || !prices[toCoinName]) {
|
|
158
158
|
throw new Error('Failed to fetch the coin prices');
|
|
159
159
|
}
|
|
@@ -43,7 +43,7 @@ export const getSpools = async (
|
|
|
43
43
|
coinPrices = coinPrices ?? (await query.utils.getCoinPrices()) ?? {};
|
|
44
44
|
|
|
45
45
|
marketPools =
|
|
46
|
-
marketPools ?? (await query.getMarketPools(stakeCoinNames, indexer));
|
|
46
|
+
marketPools ?? (await query.getMarketPools(stakeCoinNames, { indexer }));
|
|
47
47
|
if (!marketPools)
|
|
48
48
|
throw new Error(`Fail to fetch marketPools for ${stakeCoinNames}`);
|
|
49
49
|
|
|
@@ -71,6 +71,7 @@ export const getSpools = async (
|
|
|
71
71
|
};
|
|
72
72
|
Object.values(spoolsIndexer).forEach(updateSpools);
|
|
73
73
|
|
|
74
|
+
// console.log(spools);
|
|
74
75
|
return spools;
|
|
75
76
|
}
|
|
76
77
|
|
|
@@ -111,7 +112,7 @@ export const getSpool = async (
|
|
|
111
112
|
coinPrices?: CoinPrices
|
|
112
113
|
) => {
|
|
113
114
|
const coinName = query.utils.parseCoinName<SupportStakeCoins>(marketCoinName);
|
|
114
|
-
marketPool = marketPool || (await query.getMarketPool(coinName, indexer));
|
|
115
|
+
marketPool = marketPool || (await query.getMarketPool(coinName, { indexer }));
|
|
115
116
|
if (!marketPool) {
|
|
116
117
|
throw new Error(`Failed to fetch marketPool for ${marketCoinName}`);
|
|
117
118
|
}
|
|
@@ -121,7 +122,7 @@ export const getSpool = async (
|
|
|
121
122
|
`spool.pools.${marketCoinName}.rewardPoolId`
|
|
122
123
|
);
|
|
123
124
|
let spool: Spool | undefined = undefined;
|
|
124
|
-
coinPrices = coinPrices || (await query.utils.getCoinPrices());
|
|
125
|
+
coinPrices = coinPrices || (await query.utils.getCoinPrices([coinName]));
|
|
125
126
|
|
|
126
127
|
if (indexer) {
|
|
127
128
|
const spoolIndexer = await query.indexer.getSpool(marketCoinName);
|
|
@@ -151,7 +152,8 @@ export const getSpool = async (
|
|
|
151
152
|
}
|
|
152
153
|
|
|
153
154
|
const rewardCoinName = query.utils.getSpoolRewardCoinName(marketCoinName);
|
|
154
|
-
coinPrices =
|
|
155
|
+
coinPrices =
|
|
156
|
+
coinPrices || (await query.utils.getCoinPrices([coinName, rewardCoinName]));
|
|
155
157
|
|
|
156
158
|
const spoolObject = spoolObjectResponse[0];
|
|
157
159
|
const rewardPoolObject = spoolObjectResponse[1];
|
|
@@ -4,7 +4,10 @@ import type {
|
|
|
4
4
|
} from '@scallop-io/sui-kit';
|
|
5
5
|
import type { TransactionResult } from '@mysten/sui/transactions';
|
|
6
6
|
import type { ScallopBuilder } from '../../models';
|
|
7
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
SupportBorrowIncentiveCoins,
|
|
9
|
+
SupportBorrowIncentiveRewardCoins,
|
|
10
|
+
} from '../constant';
|
|
8
11
|
|
|
9
12
|
export type BorrowIncentiveIds = {
|
|
10
13
|
borrowIncentivePkg: string;
|
|
@@ -32,6 +35,7 @@ export type BorrowIncentiveNormalMethods = {
|
|
|
32
35
|
claimBorrowIncentive: (
|
|
33
36
|
obligation: SuiObjectArg,
|
|
34
37
|
obligationKey: SuiObjectArg,
|
|
38
|
+
coinName: SupportBorrowIncentiveCoins,
|
|
35
39
|
rewardType: SupportBorrowIncentiveRewardCoins
|
|
36
40
|
) => TransactionResult;
|
|
37
41
|
deactivateBoost: (obligation: SuiObjectArg, veScaKey: SuiObjectArg) => void;
|
|
@@ -39,22 +43,23 @@ export type BorrowIncentiveNormalMethods = {
|
|
|
39
43
|
|
|
40
44
|
export type BorrowIncentiveQuickMethods = {
|
|
41
45
|
stakeObligationQuick(
|
|
42
|
-
obligation?:
|
|
43
|
-
obligationKey?:
|
|
46
|
+
obligation?: SuiObjectArg,
|
|
47
|
+
obligationKey?: SuiObjectArg
|
|
44
48
|
): Promise<void>;
|
|
45
49
|
stakeObligationWithVeScaQuick(
|
|
46
|
-
obligation?:
|
|
47
|
-
obligationKey?:
|
|
48
|
-
veScaKey?:
|
|
50
|
+
obligation?: SuiObjectArg,
|
|
51
|
+
obligationKey?: SuiObjectArg,
|
|
52
|
+
veScaKey?: SuiObjectArg
|
|
49
53
|
): Promise<void>;
|
|
50
54
|
unstakeObligationQuick(
|
|
51
|
-
obligation?:
|
|
52
|
-
obligationKey?:
|
|
55
|
+
obligation?: SuiObjectArg,
|
|
56
|
+
obligationKey?: SuiObjectArg
|
|
53
57
|
): Promise<void>;
|
|
54
58
|
claimBorrowIncentiveQuick(
|
|
59
|
+
coinName: SupportBorrowIncentiveCoins,
|
|
55
60
|
rewardType: SupportBorrowIncentiveRewardCoins,
|
|
56
|
-
obligation?:
|
|
57
|
-
obligationKey?:
|
|
61
|
+
obligation?: SuiObjectArg,
|
|
62
|
+
obligationKey?: SuiObjectArg
|
|
58
63
|
): Promise<TransactionResult>;
|
|
59
64
|
};
|
|
60
65
|
|
|
@@ -23,7 +23,8 @@ export type SupportCoins =
|
|
|
23
23
|
export type SupportAssetCoins =
|
|
24
24
|
| SupportPoolCoins
|
|
25
25
|
| SupportCollateralCoins
|
|
26
|
-
| SupportStakeRewardCoins
|
|
26
|
+
| SupportStakeRewardCoins
|
|
27
|
+
| SupportBorrowIncentiveRewardCoins;
|
|
27
28
|
export type SupportPoolCoins = (typeof SUPPORT_POOLS)[number];
|
|
28
29
|
export type SupportCollateralCoins = (typeof SUPPORT_COLLATERALS)[number];
|
|
29
30
|
export type SupportMarketCoins =
|
package/src/types/utils.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SupportAssetCoins } from './constant';
|
|
2
2
|
|
|
3
3
|
export type OptionalKeys<T> = {
|
|
4
4
|
[K in keyof T]?: T[K];
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
export type CoinPrices = OptionalKeys<Record<
|
|
7
|
+
export type CoinPrices = OptionalKeys<Record<SupportAssetCoins, number>>;
|
|
8
|
+
|
|
9
|
+
export type PriceMap = Map<
|
|
10
|
+
SupportAssetCoins,
|
|
11
|
+
{
|
|
12
|
+
price: number;
|
|
13
|
+
publishTime: number;
|
|
14
|
+
}
|
|
15
|
+
>;
|
|
8
16
|
|
|
9
17
|
export type PoolAddressInfo = {
|
|
10
18
|
name: string;
|
package/src/utils/indexer.ts
CHANGED
|
@@ -11,14 +11,20 @@ export async function callMethodWithIndexerFallback(
|
|
|
11
11
|
context: any,
|
|
12
12
|
...args: any[]
|
|
13
13
|
) {
|
|
14
|
-
const
|
|
14
|
+
const lastArgs = args[args.length - 1]; // Assume last argument is always `indexer`
|
|
15
15
|
|
|
16
|
-
if (indexer) {
|
|
16
|
+
if (typeof lastArgs === 'object' && lastArgs.indexer) {
|
|
17
17
|
try {
|
|
18
18
|
return await method.apply(context, args);
|
|
19
19
|
} catch (e: any) {
|
|
20
20
|
console.warn(`Indexer requests failed: ${e}. Retrying without indexer..`);
|
|
21
|
-
return await method.apply(context, [
|
|
21
|
+
return await method.apply(context, [
|
|
22
|
+
...args.slice(0, -1),
|
|
23
|
+
{
|
|
24
|
+
...lastArgs,
|
|
25
|
+
indexer: false,
|
|
26
|
+
},
|
|
27
|
+
]);
|
|
22
28
|
}
|
|
23
29
|
}
|
|
24
30
|
return await method.apply(context, args);
|
package/src/utils/query.ts
CHANGED
|
@@ -546,6 +546,93 @@ export const calculateBorrowIncentivePoolPointData = (
|
|
|
546
546
|
};
|
|
547
547
|
};
|
|
548
548
|
|
|
549
|
+
// /**
|
|
550
|
+
// * Parse origin borrow incentive reward pool data to a more readable format.
|
|
551
|
+
// *
|
|
552
|
+
// * @param originBorrowIncentiveRewardPoolData - Origin borrow incentive reward pool data
|
|
553
|
+
// * @return Parsed borrow incentive reward pool data
|
|
554
|
+
// */
|
|
555
|
+
// export const parseOriginBorrowIncentiveRewardPoolData = (
|
|
556
|
+
// originBorrowIncentiveRewardPoolData: OriginBorrowIncentiveRewardPoolData
|
|
557
|
+
// ): ParsedBorrowIncentiveRewardPoolData => {
|
|
558
|
+
// return {
|
|
559
|
+
// rewardType: normalizeStructTag(
|
|
560
|
+
// originBorrowIncentiveRewardPoolData.reward_type.name
|
|
561
|
+
// ),
|
|
562
|
+
// claimedRewards: Number(originBorrowIncentiveRewardPoolData.claimed_rewards),
|
|
563
|
+
// exchangeRateNumerator: Number(
|
|
564
|
+
// originBorrowIncentiveRewardPoolData.exchange_rate_numerator
|
|
565
|
+
// ),
|
|
566
|
+
// exchangeRateDenominator: Number(
|
|
567
|
+
// originBorrowIncentiveRewardPoolData.exchange_rate_denominator
|
|
568
|
+
// ),
|
|
569
|
+
// remainingRewards: Number(
|
|
570
|
+
// originBorrowIncentiveRewardPoolData.remaining_reward
|
|
571
|
+
// ),
|
|
572
|
+
// };
|
|
573
|
+
// };
|
|
574
|
+
|
|
575
|
+
// export const calculateBorrowIncentiveRewardPoolData = (
|
|
576
|
+
// parsedBorrowIncentivePoolData: ParsedBorrowIncentivePoolData,
|
|
577
|
+
// parsedBorrowIncentiveRewardPoolData: ParsedBorrowIncentiveRewardPoolData,
|
|
578
|
+
// calculatedBorrowIncentivePoolData: CalculatedBorrowIncentivePoolData,
|
|
579
|
+
// rewardCoinPrice: number,
|
|
580
|
+
// rewardCoinDecimal: number
|
|
581
|
+
// ): CalculatedBorrowIncentiveRewardPoolData => {
|
|
582
|
+
// const rateYearFactor = 365 * 24 * 60 * 60;
|
|
583
|
+
|
|
584
|
+
// const rewardPerSec = BigNumber(
|
|
585
|
+
// calculatedBorrowIncentivePoolData.distributedPointPerSec
|
|
586
|
+
// )
|
|
587
|
+
// .multipliedBy(parsedBorrowIncentiveRewardPoolData.exchangeRateNumerator)
|
|
588
|
+
// .dividedBy(parsedBorrowIncentiveRewardPoolData.exchangeRateDenominator);
|
|
589
|
+
// const totalRewardAmount = BigNumber(parsedBorrowIncentivePoolData.maxPoint)
|
|
590
|
+
// .multipliedBy(parsedBorrowIncentiveRewardPoolData.exchangeRateNumerator)
|
|
591
|
+
// .dividedBy(parsedBorrowIncentiveRewardPoolData.exchangeRateDenominator);
|
|
592
|
+
// const totalRewardCoin = totalRewardAmount.shiftedBy(-1 * rewardCoinDecimal);
|
|
593
|
+
// const totalRewardValue = totalRewardCoin.multipliedBy(rewardCoinPrice);
|
|
594
|
+
// const remaindRewardAmount = BigNumber(
|
|
595
|
+
// parsedBorrowIncentiveRewardPoolData.remainingRewards
|
|
596
|
+
// );
|
|
597
|
+
// const remaindRewardCoin = remaindRewardAmount.shiftedBy(
|
|
598
|
+
// -1 * rewardCoinDecimal
|
|
599
|
+
// );
|
|
600
|
+
// const remaindRewardValue = remaindRewardCoin.multipliedBy(rewardCoinPrice);
|
|
601
|
+
// const claimedRewardAmount = BigNumber(
|
|
602
|
+
// parsedBorrowIncentiveRewardPoolData.claimedRewards
|
|
603
|
+
// );
|
|
604
|
+
// const claimedRewardCoin = claimedRewardAmount.shiftedBy(
|
|
605
|
+
// -1 * rewardCoinDecimal
|
|
606
|
+
// );
|
|
607
|
+
// const claimedRewardValue = claimedRewardCoin.multipliedBy(rewardCoinPrice);
|
|
608
|
+
|
|
609
|
+
// const rewardValueForYear = BigNumber(rewardPerSec)
|
|
610
|
+
// .shiftedBy(-1 * rewardCoinDecimal)
|
|
611
|
+
// .multipliedBy(rateYearFactor)
|
|
612
|
+
// .multipliedBy(rewardCoinPrice);
|
|
613
|
+
// const rewardRate = rewardValueForYear
|
|
614
|
+
// .dividedBy(calculatedBorrowIncentivePoolData.stakedValue)
|
|
615
|
+
// .isFinite()
|
|
616
|
+
// ? rewardValueForYear
|
|
617
|
+
// .dividedBy(calculatedBorrowIncentivePoolData.stakedValue)
|
|
618
|
+
// .toNumber()
|
|
619
|
+
// : Infinity;
|
|
620
|
+
|
|
621
|
+
// return {
|
|
622
|
+
// rewardApr: rewardRate,
|
|
623
|
+
// totalRewardAmount: totalRewardAmount.toNumber(),
|
|
624
|
+
// totalRewardCoin: totalRewardCoin.toNumber(),
|
|
625
|
+
// totalRewardValue: totalRewardValue.toNumber(),
|
|
626
|
+
// remaindRewardAmount: remaindRewardAmount.toNumber(),
|
|
627
|
+
// remaindRewardCoin: remaindRewardCoin.toNumber(),
|
|
628
|
+
// remaindRewardValue: remaindRewardValue.toNumber(),
|
|
629
|
+
// claimedRewardAmount: claimedRewardAmount.toNumber(),
|
|
630
|
+
// claimedRewardCoin: claimedRewardCoin.toNumber(),
|
|
631
|
+
// claimedRewardValue: claimedRewardValue.toNumber(),
|
|
632
|
+
// rewardPerSec: rewardPerSec.toNumber(),
|
|
633
|
+
// };
|
|
634
|
+
// };
|
|
635
|
+
|
|
549
636
|
export const parseOriginBorrowIncentiveAccountPoolPointData = (
|
|
550
637
|
originBorrowIncentiveAccountPoolPointData: OriginBorrowIncentiveAccountPoolData
|
|
551
638
|
): ParsedBorrowIncentiveAccountPoolData => {
|
package/src/utils/tokenBucket.ts
CHANGED
|
@@ -38,8 +38,8 @@ const callWithRateLimit = async <T>(
|
|
|
38
38
|
tokenBucket: TokenBucket,
|
|
39
39
|
fn: () => Promise<T>,
|
|
40
40
|
retryDelayInMs = DEFAULT_INTERVAL_IN_MS,
|
|
41
|
-
maxRetries =
|
|
42
|
-
backoffFactor =
|
|
41
|
+
maxRetries = 15,
|
|
42
|
+
backoffFactor = 2 // The factor by which to increase the delay
|
|
43
43
|
): Promise<T | null> => {
|
|
44
44
|
let retries = 0;
|
|
45
45
|
|