@scallop-io/sui-scallop-sdk 1.5.3 → 2.0.0-alpha.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/dist/index.d.mts +451 -602
- package/dist/index.d.ts +451 -602
- package/dist/index.js +29 -60
- package/dist/index.mjs +6 -6
- package/package.json +1 -1
- package/src/builders/loyaltyProgramBuilder.ts +5 -3
- package/src/builders/oracle.ts +10 -24
- package/src/builders/referralBuilder.ts +5 -9
- package/src/builders/sCoinBuilder.ts +9 -8
- package/src/builders/spoolBuilder.ts +4 -6
- package/src/constants/common.ts +114 -126
- package/src/constants/index.ts +0 -5
- package/src/constants/pyth.ts +25 -34
- package/src/constants/queryKeys.ts +2 -0
- package/src/models/index.ts +1 -0
- package/src/models/scallop.ts +23 -19
- package/src/models/scallopAddress.ts +7 -4
- package/src/models/scallopBuilder.ts +36 -41
- package/src/models/scallopCache.ts +1 -1
- package/src/models/scallopClient.ts +93 -94
- package/src/models/scallopConstants.ts +342 -0
- package/src/models/scallopIndexer.ts +11 -24
- package/src/models/scallopQuery.ts +70 -77
- package/src/models/scallopUtils.ts +122 -249
- package/src/queries/borrowIncentiveQuery.ts +21 -56
- package/src/queries/borrowLimitQuery.ts +3 -6
- package/src/queries/coreQuery.ts +94 -112
- package/src/queries/flashloanFeeQuery.ts +86 -0
- package/src/queries/isolatedAssetQuery.ts +12 -11
- package/src/queries/poolAddressesQuery.ts +187 -112
- package/src/queries/portfolioQuery.ts +65 -67
- package/src/queries/priceQuery.ts +16 -22
- package/src/queries/sCoinQuery.ts +15 -16
- package/src/queries/spoolQuery.ts +49 -59
- package/src/queries/supplyLimitQuery.ts +2 -6
- package/src/queries/xOracleQuery.ts +4 -15
- package/src/types/address.ts +12 -18
- package/src/types/builder/borrowIncentive.ts +2 -3
- package/src/types/builder/core.ts +20 -27
- package/src/types/builder/index.ts +1 -2
- package/src/types/builder/referral.ts +4 -8
- package/src/types/builder/sCoin.ts +4 -8
- package/src/types/builder/spool.ts +7 -10
- package/src/types/constant/common.ts +43 -49
- package/src/types/constant/enum.ts +15 -27
- package/src/types/constant/xOracle.ts +3 -5
- package/src/types/model.ts +49 -28
- package/src/types/query/borrowIncentive.ts +7 -24
- package/src/types/query/core.ts +8 -18
- package/src/types/query/portfolio.ts +8 -17
- package/src/types/query/spool.ts +5 -11
- package/src/types/utils.ts +1 -21
- package/src/utils/core.ts +1 -1
- package/src/utils/query.ts +13 -20
- package/src/utils/util.ts +6 -84
- package/src/constants/coinGecko.ts +0 -34
- package/src/constants/enum.ts +0 -268
- package/src/constants/flashloan.ts +0 -18
- package/src/constants/poolAddress.ts +0 -898
- package/src/models/scallopPrice.ts +0 -0
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { normalizeStructTag } from '@mysten/sui/utils';
|
|
2
|
-
import {
|
|
3
|
-
SUPPORT_BORROW_INCENTIVE_POOLS,
|
|
4
|
-
SUPPORT_BORROW_INCENTIVE_REWARDS,
|
|
5
|
-
} from '../constants';
|
|
6
2
|
import {
|
|
7
3
|
parseOriginBorrowIncentivePoolData,
|
|
8
4
|
parseOriginBorrowIncentiveAccountData,
|
|
@@ -14,11 +10,8 @@ import type {
|
|
|
14
10
|
BorrowIncentivePools,
|
|
15
11
|
BorrowIncentiveAccountsQueryInterface,
|
|
16
12
|
BorrowIncentiveAccounts,
|
|
17
|
-
SupportBorrowIncentiveCoins,
|
|
18
|
-
SupportBorrowIncentiveRewardCoins,
|
|
19
13
|
BorrowIncentivePoolPoints,
|
|
20
14
|
OptionalKeys,
|
|
21
|
-
BorrowIncentivePool,
|
|
22
15
|
CoinPrices,
|
|
23
16
|
MarketPools,
|
|
24
17
|
} from '../types';
|
|
@@ -56,9 +49,7 @@ export const queryBorrowIncentivePools = async (address: ScallopAddress) => {
|
|
|
56
49
|
*/
|
|
57
50
|
export const getBorrowIncentivePools = async (
|
|
58
51
|
query: ScallopQuery,
|
|
59
|
-
borrowIncentiveCoinNames:
|
|
60
|
-
...SUPPORT_BORROW_INCENTIVE_POOLS,
|
|
61
|
-
],
|
|
52
|
+
borrowIncentiveCoinNames: string[] = [...query.constants.whitelist.lending],
|
|
62
53
|
indexer: boolean = false,
|
|
63
54
|
marketPools?: MarketPools,
|
|
64
55
|
coinPrices?: CoinPrices
|
|
@@ -69,36 +60,13 @@ export const getBorrowIncentivePools = async (
|
|
|
69
60
|
(await query.getMarketPools(undefined, { coinPrices, indexer })).pools;
|
|
70
61
|
coinPrices = coinPrices ?? (await query.getAllCoinPrices({ marketPools }));
|
|
71
62
|
|
|
72
|
-
if (indexer) {
|
|
73
|
-
const borrowIncentivePoolsIndexer =
|
|
74
|
-
await query.indexer.getBorrowIncentivePools();
|
|
75
|
-
|
|
76
|
-
const updateBorrowIncentivePool = (pool: BorrowIncentivePool) => {
|
|
77
|
-
if (!borrowIncentiveCoinNames.includes(pool.coinName)) return;
|
|
78
|
-
pool.coinPrice = coinPrices[pool.coinName] || pool.coinPrice;
|
|
79
|
-
for (const sCoinName of SUPPORT_BORROW_INCENTIVE_REWARDS) {
|
|
80
|
-
if (pool.points[sCoinName]) {
|
|
81
|
-
pool.points[sCoinName].coinPrice =
|
|
82
|
-
coinPrices[sCoinName] ?? pool.points[sCoinName].coinPrice;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
borrowIncentivePools[pool.coinName] = pool;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
Object.values(borrowIncentivePoolsIndexer).forEach(
|
|
89
|
-
updateBorrowIncentivePool
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
return borrowIncentivePools;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
63
|
const borrowIncentivePoolsQueryData = await queryBorrowIncentivePools(
|
|
96
64
|
query.address
|
|
97
65
|
);
|
|
98
66
|
|
|
99
67
|
for (const pool of borrowIncentivePoolsQueryData?.incentive_pools ?? []) {
|
|
100
68
|
const borrowIncentivePoolPoints: OptionalKeys<
|
|
101
|
-
Record<
|
|
69
|
+
Record<string, BorrowIncentivePoolPoints>
|
|
102
70
|
> = {};
|
|
103
71
|
const parsedBorrowIncentivePoolData = parseOriginBorrowIncentivePoolData(
|
|
104
72
|
query.utils,
|
|
@@ -106,10 +74,7 @@ export const getBorrowIncentivePools = async (
|
|
|
106
74
|
);
|
|
107
75
|
|
|
108
76
|
const poolCoinType = normalizeStructTag(pool.pool_type.name);
|
|
109
|
-
const poolCoinName =
|
|
110
|
-
query.utils.parseCoinNameFromType<SupportBorrowIncentiveCoins>(
|
|
111
|
-
poolCoinType
|
|
112
|
-
);
|
|
77
|
+
const poolCoinName = query.utils.parseCoinNameFromType(poolCoinType);
|
|
113
78
|
const poolCoinPrice = coinPrices?.[poolCoinName] ?? 0;
|
|
114
79
|
const poolCoinDecimal = query.utils.getCoinDecimal(poolCoinName);
|
|
115
80
|
|
|
@@ -122,12 +87,16 @@ export const getBorrowIncentivePools = async (
|
|
|
122
87
|
for (const [coinName, poolPoint] of Object.entries(
|
|
123
88
|
parsedBorrowIncentivePoolData.poolPoints
|
|
124
89
|
)) {
|
|
90
|
+
if (!poolPoint) continue;
|
|
125
91
|
const rewardCoinType = poolPoint.pointType;
|
|
126
92
|
const rewardCoinName = query.utils.parseCoinNameFromType(
|
|
127
93
|
rewardCoinType
|
|
128
|
-
) as
|
|
94
|
+
) as string;
|
|
129
95
|
// handle for scoin name
|
|
130
96
|
const rewardCoinDecimal = query.utils.getCoinDecimal(rewardCoinName);
|
|
97
|
+
if (rewardCoinDecimal === undefined)
|
|
98
|
+
throw new Error(`Coin decimal not found for ${rewardCoinName}`);
|
|
99
|
+
|
|
131
100
|
const rewardCoinPrice = coinPrices?.[rewardCoinName] ?? 0;
|
|
132
101
|
|
|
133
102
|
const symbol = query.utils.parseSymbol(rewardCoinName);
|
|
@@ -142,18 +111,17 @@ export const getBorrowIncentivePools = async (
|
|
|
142
111
|
poolCoinDecimal
|
|
143
112
|
);
|
|
144
113
|
|
|
145
|
-
borrowIncentivePoolPoints[coinName as
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
};
|
|
114
|
+
borrowIncentivePoolPoints[coinName as string] = {
|
|
115
|
+
symbol,
|
|
116
|
+
coinName: rewardCoinName,
|
|
117
|
+
coinType: rewardCoinType,
|
|
118
|
+
coinDecimal,
|
|
119
|
+
coinPrice: rewardCoinPrice,
|
|
120
|
+
points: poolPoint.points,
|
|
121
|
+
distributedPoint: poolPoint.distributedPoint,
|
|
122
|
+
weightedAmount: poolPoint.weightedAmount,
|
|
123
|
+
...calculatedPoolPoint,
|
|
124
|
+
};
|
|
157
125
|
}
|
|
158
126
|
|
|
159
127
|
const stakedAmount = BigNumber(parsedBorrowIncentivePoolData.staked);
|
|
@@ -190,9 +158,7 @@ export const queryBorrowIncentiveAccounts = async (
|
|
|
190
158
|
utils: ScallopUtils;
|
|
191
159
|
},
|
|
192
160
|
obligationId: string | SuiObjectRef,
|
|
193
|
-
borrowIncentiveCoinNames:
|
|
194
|
-
...SUPPORT_BORROW_INCENTIVE_POOLS,
|
|
195
|
-
]
|
|
161
|
+
borrowIncentiveCoinNames: string[] = [...utils.constants.whitelist.lending]
|
|
196
162
|
) => {
|
|
197
163
|
const queryPkgId = utils.address.get('borrowIncentive.query');
|
|
198
164
|
const incentiveAccountsId = utils.address.get(
|
|
@@ -211,8 +177,7 @@ export const queryBorrowIncentiveAccounts = async (
|
|
|
211
177
|
const parsedBorrowIncentiveAccount =
|
|
212
178
|
parseOriginBorrowIncentiveAccountData(accountData);
|
|
213
179
|
const poolType = parsedBorrowIncentiveAccount.poolType;
|
|
214
|
-
const coinName =
|
|
215
|
-
utils.parseCoinNameFromType<SupportBorrowIncentiveCoins>(poolType);
|
|
180
|
+
const coinName = utils.parseCoinNameFromType(poolType);
|
|
216
181
|
|
|
217
182
|
if (
|
|
218
183
|
borrowIncentiveCoinNames &&
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ScallopUtils } from 'src/models';
|
|
2
|
-
import { SupportPoolCoins } from 'src/types';
|
|
3
2
|
import { z as zod } from 'zod';
|
|
4
3
|
|
|
5
4
|
const borrowLimitZod = zod.object({
|
|
@@ -26,10 +25,7 @@ const borrowLimitKeyType = `0xe7dbb371a9595631f7964b7ece42255ad0e738cc85fe6da26c
|
|
|
26
25
|
* @param poolName
|
|
27
26
|
* @returns supply limit (decimals included)
|
|
28
27
|
*/
|
|
29
|
-
export const getBorrowLimit = async (
|
|
30
|
-
utils: ScallopUtils,
|
|
31
|
-
poolName: SupportPoolCoins
|
|
32
|
-
) => {
|
|
28
|
+
export const getBorrowLimit = async (utils: ScallopUtils, poolName: string) => {
|
|
33
29
|
try {
|
|
34
30
|
const poolCoinType = utils.parseCoinType(poolName).slice(2);
|
|
35
31
|
const marketObject = utils.address.get('core.market');
|
|
@@ -44,7 +40,8 @@ export const getBorrowLimit = async (
|
|
|
44
40
|
});
|
|
45
41
|
|
|
46
42
|
const parsedData = borrowLimitZod.safeParse(object?.data?.content);
|
|
47
|
-
if (!parsedData.success) return
|
|
43
|
+
if (!parsedData.success) return '0';
|
|
44
|
+
|
|
48
45
|
return parsedData.data.fields.value;
|
|
49
46
|
} catch (e: any) {
|
|
50
47
|
console.error(`Error in getBorrowLimit for ${poolName}: ${e.message}`);
|
package/src/queries/coreQuery.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
import { normalizeStructTag, SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils';
|
|
2
|
-
import {
|
|
3
|
-
SUPPORT_POOLS,
|
|
4
|
-
PROTOCOL_OBJECT_ID,
|
|
5
|
-
SUPPORT_COLLATERALS,
|
|
6
|
-
FlashLoanFeeObjectMap,
|
|
7
|
-
POOL_ADDRESSES,
|
|
8
|
-
} from '../constants';
|
|
9
2
|
import {
|
|
10
3
|
parseOriginMarketPoolData,
|
|
11
4
|
calculateMarketPoolData,
|
|
@@ -27,16 +20,12 @@ import {
|
|
|
27
20
|
MarketCollaterals,
|
|
28
21
|
MarketCollateral,
|
|
29
22
|
MarketQueryInterface,
|
|
30
|
-
SupportAssetCoins,
|
|
31
|
-
SupportPoolCoins,
|
|
32
|
-
SupportCollateralCoins,
|
|
33
23
|
ObligationQueryInterface,
|
|
34
24
|
Obligation,
|
|
35
25
|
InterestModel,
|
|
36
26
|
BalanceSheet,
|
|
37
27
|
RiskModel,
|
|
38
28
|
CollateralStat,
|
|
39
|
-
SupportMarketCoins,
|
|
40
29
|
OptionalKeys,
|
|
41
30
|
CoinPrices,
|
|
42
31
|
OriginMarketPoolData,
|
|
@@ -49,6 +38,8 @@ import { getSupplyLimit } from './supplyLimitQuery';
|
|
|
49
38
|
import { isIsolatedAsset } from './isolatedAssetQuery';
|
|
50
39
|
import { getBorrowLimit } from './borrowLimitQuery';
|
|
51
40
|
import { queryMultipleObjects } from './objectsQuery';
|
|
41
|
+
import { ScallopConstants } from 'src/models/scallopConstants';
|
|
42
|
+
import { queryFlashLoanFees } from './flashloanFeeQuery';
|
|
52
43
|
|
|
53
44
|
/**
|
|
54
45
|
* Query market data.
|
|
@@ -87,8 +78,12 @@ export const queryMarket = async (
|
|
|
87
78
|
collaterals[item.coinName] = item;
|
|
88
79
|
};
|
|
89
80
|
|
|
90
|
-
Object.values(marketIndexer.pools)
|
|
91
|
-
|
|
81
|
+
Object.values(marketIndexer.pools)
|
|
82
|
+
.filter((t) => !!t)
|
|
83
|
+
.forEach(updatePools);
|
|
84
|
+
Object.values(marketIndexer.collaterals)
|
|
85
|
+
.filter((t) => !!t)
|
|
86
|
+
.forEach(updateCollaterals);
|
|
92
87
|
|
|
93
88
|
return {
|
|
94
89
|
pools,
|
|
@@ -108,12 +103,11 @@ export const queryMarket = async (
|
|
|
108
103
|
|
|
109
104
|
for (const pool of marketData?.pools ?? []) {
|
|
110
105
|
const coinType = normalizeStructTag(pool.type.name);
|
|
111
|
-
const poolCoinName =
|
|
112
|
-
query.utils.parseCoinNameFromType<SupportPoolCoins>(coinType);
|
|
106
|
+
const poolCoinName = query.utils.parseCoinNameFromType(coinType);
|
|
113
107
|
const coinPrice = coinPrices[poolCoinName] ?? 0;
|
|
114
108
|
|
|
115
109
|
// Filter pools not yet supported by the SDK.
|
|
116
|
-
if (!
|
|
110
|
+
if (!query.constants.whitelist.lending.has(poolCoinName)) {
|
|
117
111
|
continue;
|
|
118
112
|
}
|
|
119
113
|
|
|
@@ -152,9 +146,10 @@ export const queryMarket = async (
|
|
|
152
146
|
symbol: query.utils.parseSymbol(poolCoinName),
|
|
153
147
|
coinType: coinType,
|
|
154
148
|
marketCoinType: query.utils.parseMarketCoinType(poolCoinName),
|
|
155
|
-
sCoinType:
|
|
156
|
-
query.utils.
|
|
157
|
-
|
|
149
|
+
sCoinType:
|
|
150
|
+
query.utils.parseSCoinType(
|
|
151
|
+
query.utils.parseMarketCoinName(poolCoinName)
|
|
152
|
+
) ?? '',
|
|
158
153
|
coinWrappedType: query.utils.getCoinWrappedType(poolCoinName),
|
|
159
154
|
coinPrice: coinPrice,
|
|
160
155
|
highKink: parsedMarketPoolData.highKink,
|
|
@@ -170,12 +165,11 @@ export const queryMarket = async (
|
|
|
170
165
|
|
|
171
166
|
for (const collateral of marketData?.collaterals ?? []) {
|
|
172
167
|
const coinType = normalizeStructTag(collateral.type.name);
|
|
173
|
-
const collateralCoinName =
|
|
174
|
-
query.utils.parseCoinNameFromType<SupportCollateralCoins>(coinType);
|
|
168
|
+
const collateralCoinName = query.utils.parseCoinNameFromType(coinType);
|
|
175
169
|
const coinPrice = coinPrices[collateralCoinName] ?? 0;
|
|
176
170
|
|
|
177
171
|
// Filter collaterals not yet supported by the SDK.
|
|
178
|
-
if (!
|
|
172
|
+
if (!query.constants.whitelist.collateral.has(collateralCoinName)) {
|
|
179
173
|
continue;
|
|
180
174
|
}
|
|
181
175
|
|
|
@@ -223,20 +217,20 @@ export const queryMarket = async (
|
|
|
223
217
|
|
|
224
218
|
const queryRequiredMarketObjects = async (
|
|
225
219
|
query: ScallopQuery,
|
|
226
|
-
poolCoinNames:
|
|
220
|
+
poolCoinNames: string[]
|
|
227
221
|
) => {
|
|
228
222
|
// Prepare all tasks for querying each object type
|
|
229
223
|
const tasks = poolCoinNames.map((t) => ({
|
|
230
224
|
poolCoinName: t,
|
|
231
|
-
balanceSheet:
|
|
232
|
-
collateralStat:
|
|
233
|
-
borrowDynamic:
|
|
234
|
-
interestModel:
|
|
235
|
-
riskModel:
|
|
236
|
-
borrowFeeKey:
|
|
237
|
-
supplyLimitKey:
|
|
238
|
-
borrowLimitKey:
|
|
239
|
-
isolatedAssetKey:
|
|
225
|
+
balanceSheet: query.constants.poolAddresses[t]?.lendingPoolAddress,
|
|
226
|
+
collateralStat: query.constants.poolAddresses[t]?.collateralPoolAddress,
|
|
227
|
+
borrowDynamic: query.constants.poolAddresses[t]?.borrowDynamic,
|
|
228
|
+
interestModel: query.constants.poolAddresses[t]?.interestModel,
|
|
229
|
+
riskModel: query.constants.poolAddresses[t]?.riskModel,
|
|
230
|
+
borrowFeeKey: query.constants.poolAddresses[t]?.borrowFeeKey,
|
|
231
|
+
supplyLimitKey: query.constants.poolAddresses[t]?.supplyLimitKey,
|
|
232
|
+
borrowLimitKey: query.constants.poolAddresses[t]?.borrowLimitKey,
|
|
233
|
+
isolatedAssetKey: query.constants.poolAddresses[t]?.isolatedAssetKey,
|
|
240
234
|
}));
|
|
241
235
|
|
|
242
236
|
// Query all objects for each key in parallel
|
|
@@ -366,7 +360,7 @@ const queryRequiredMarketObjects = async (
|
|
|
366
360
|
return acc;
|
|
367
361
|
},
|
|
368
362
|
{} as Record<
|
|
369
|
-
|
|
363
|
+
string,
|
|
370
364
|
{
|
|
371
365
|
balanceSheet: SuiObjectData;
|
|
372
366
|
collateralStat?: SuiObjectData;
|
|
@@ -398,7 +392,7 @@ const queryRequiredMarketObjects = async (
|
|
|
398
392
|
*/
|
|
399
393
|
export const getMarketPools = async (
|
|
400
394
|
query: ScallopQuery,
|
|
401
|
-
poolCoinNames:
|
|
395
|
+
poolCoinNames: string[],
|
|
402
396
|
indexer: boolean = false,
|
|
403
397
|
coinPrices?: CoinPrices
|
|
404
398
|
): Promise<{
|
|
@@ -440,8 +434,12 @@ export const getMarketPools = async (
|
|
|
440
434
|
collaterals[item.coinName] = item;
|
|
441
435
|
};
|
|
442
436
|
|
|
443
|
-
Object.values(marketIndexer.pools)
|
|
444
|
-
|
|
437
|
+
Object.values(marketIndexer.pools)
|
|
438
|
+
.filter((t) => !!t)
|
|
439
|
+
.forEach(updatePools);
|
|
440
|
+
Object.values(marketIndexer.collaterals)
|
|
441
|
+
.filter((t) => !!t)
|
|
442
|
+
.forEach(updateCollaterals);
|
|
445
443
|
|
|
446
444
|
return {
|
|
447
445
|
pools,
|
|
@@ -468,8 +466,7 @@ export const getMarketPools = async (
|
|
|
468
466
|
pools[poolCoinName] = result?.marketPool;
|
|
469
467
|
}
|
|
470
468
|
if (result?.collateral) {
|
|
471
|
-
collaterals[poolCoinName as
|
|
472
|
-
result.collateral;
|
|
469
|
+
collaterals[poolCoinName as string] = result.collateral;
|
|
473
470
|
}
|
|
474
471
|
} catch (e) {
|
|
475
472
|
console.error(e);
|
|
@@ -578,7 +575,7 @@ const parseMarketPoolObjects = ({
|
|
|
578
575
|
*/
|
|
579
576
|
export const getMarketPool = async (
|
|
580
577
|
query: ScallopQuery,
|
|
581
|
-
poolCoinName:
|
|
578
|
+
poolCoinName: string,
|
|
582
579
|
indexer: boolean = false,
|
|
583
580
|
coinPrice: number,
|
|
584
581
|
requiredObjects?: {
|
|
@@ -606,9 +603,9 @@ export const getMarketPool = async (
|
|
|
606
603
|
);
|
|
607
604
|
|
|
608
605
|
let marketCollateralIndexer: MarketCollateral | undefined = undefined;
|
|
609
|
-
if (
|
|
606
|
+
if (query.constants.whitelist.collateral.has(poolCoinName as string)) {
|
|
610
607
|
marketCollateralIndexer = await query.indexer.getMarketCollateral(
|
|
611
|
-
poolCoinName as
|
|
608
|
+
poolCoinName as string
|
|
612
609
|
);
|
|
613
610
|
marketCollateralIndexer.coinPrice =
|
|
614
611
|
coinPrice ?? marketCollateralIndexer.coinPrice;
|
|
@@ -642,7 +639,7 @@ export const getMarketPool = async (
|
|
|
642
639
|
)
|
|
643
640
|
: undefined;
|
|
644
641
|
|
|
645
|
-
const basePoolData = <T extends
|
|
642
|
+
const basePoolData = <T extends string = string>() => ({
|
|
646
643
|
coinName: poolCoinName as T,
|
|
647
644
|
symbol: query.utils.parseSymbol(poolCoinName),
|
|
648
645
|
marketCoinType: query.utils.parseMarketCoinType(poolCoinName),
|
|
@@ -651,9 +648,10 @@ export const getMarketPool = async (
|
|
|
651
648
|
return {
|
|
652
649
|
marketPool: {
|
|
653
650
|
...basePoolData(),
|
|
654
|
-
sCoinType:
|
|
655
|
-
query.utils.
|
|
656
|
-
|
|
651
|
+
sCoinType:
|
|
652
|
+
query.utils.parseSCoinType(
|
|
653
|
+
query.utils.parseMarketCoinName(poolCoinName)
|
|
654
|
+
) ?? '',
|
|
657
655
|
coinWrappedType: query.utils.getCoinWrappedType(poolCoinName),
|
|
658
656
|
coinPrice: coinPrice ?? 0,
|
|
659
657
|
highKink: parsedMarketPoolData.highKink,
|
|
@@ -667,7 +665,7 @@ export const getMarketPool = async (
|
|
|
667
665
|
},
|
|
668
666
|
collateral: parsedMarketCollateralData
|
|
669
667
|
? {
|
|
670
|
-
...basePoolData<
|
|
668
|
+
...basePoolData<string>(),
|
|
671
669
|
coinWrappedType: query.utils.getCoinWrappedType(poolCoinName),
|
|
672
670
|
coinPrice: coinPrice,
|
|
673
671
|
collateralFactor: parsedMarketCollateralData.collateralFactor,
|
|
@@ -699,7 +697,7 @@ export const getMarketPool = async (
|
|
|
699
697
|
*/
|
|
700
698
|
export const getMarketCollaterals = async (
|
|
701
699
|
query: ScallopQuery,
|
|
702
|
-
collateralCoinNames:
|
|
700
|
+
collateralCoinNames: string[] = [...query.constants.whitelist.collateral],
|
|
703
701
|
indexer: boolean = false
|
|
704
702
|
) => {
|
|
705
703
|
const marketId = query.address.get('core.market');
|
|
@@ -717,7 +715,9 @@ export const getMarketCollaterals = async (
|
|
|
717
715
|
);
|
|
718
716
|
marketCollaterals[marketCollateral.coinName] = marketCollateral;
|
|
719
717
|
};
|
|
720
|
-
Object.values(marketCollateralsIndexer)
|
|
718
|
+
Object.values(marketCollateralsIndexer)
|
|
719
|
+
.filter((t) => !!t)
|
|
720
|
+
.forEach(updateMarketCollateral);
|
|
721
721
|
return marketCollaterals;
|
|
722
722
|
}
|
|
723
723
|
|
|
@@ -753,7 +753,7 @@ export const getMarketCollaterals = async (
|
|
|
753
753
|
*/
|
|
754
754
|
export const getMarketCollateral = async (
|
|
755
755
|
query: ScallopQuery,
|
|
756
|
-
collateralCoinName:
|
|
756
|
+
collateralCoinName: string,
|
|
757
757
|
indexer: boolean = false,
|
|
758
758
|
marketObject?: SuiObjectData | null,
|
|
759
759
|
coinPrice?: number
|
|
@@ -891,20 +891,20 @@ export const getMarketCollateral = async (
|
|
|
891
891
|
*/
|
|
892
892
|
export const getObligations = async (
|
|
893
893
|
{
|
|
894
|
-
|
|
894
|
+
constants,
|
|
895
895
|
}: {
|
|
896
|
-
|
|
896
|
+
constants: ScallopConstants;
|
|
897
897
|
},
|
|
898
898
|
ownerAddress: string
|
|
899
899
|
) => {
|
|
900
900
|
const owner = ownerAddress;
|
|
901
|
-
const protocolObjectId =
|
|
901
|
+
const protocolObjectId = constants.protocolObjectId;
|
|
902
902
|
const keyObjectsResponse: SuiObjectResponse[] = [];
|
|
903
903
|
let hasNextPage = false;
|
|
904
904
|
let nextCursor: string | null | undefined = null;
|
|
905
905
|
do {
|
|
906
906
|
const paginatedKeyObjectsResponse =
|
|
907
|
-
await
|
|
907
|
+
await constants.cache.queryGetOwnedObjects({
|
|
908
908
|
owner,
|
|
909
909
|
filter: {
|
|
910
910
|
StructType: `${protocolObjectId}::obligation::ObligationKey`,
|
|
@@ -935,7 +935,7 @@ export const getObligations = async (
|
|
|
935
935
|
const obligations: Obligation[] = [];
|
|
936
936
|
// fetch all obligations with multi get objects
|
|
937
937
|
const obligationsObjects = await queryMultipleObjects(
|
|
938
|
-
|
|
938
|
+
constants.cache,
|
|
939
939
|
keyObjects
|
|
940
940
|
.map((ref) => ref.data?.content)
|
|
941
941
|
.filter(
|
|
@@ -953,7 +953,7 @@ export const getObligations = async (
|
|
|
953
953
|
const fields = content.fields as any;
|
|
954
954
|
const obligationId = String(fields.ownership.fields.of);
|
|
955
955
|
const locked = await getObligationLocked(
|
|
956
|
-
|
|
956
|
+
constants.cache,
|
|
957
957
|
obligationsObjects[idx]
|
|
958
958
|
);
|
|
959
959
|
obligations.push({ id: obligationId, keyId, locked });
|
|
@@ -1044,11 +1044,11 @@ export const queryObligation = async (
|
|
|
1044
1044
|
*/
|
|
1045
1045
|
export const getCoinAmounts = async (
|
|
1046
1046
|
query: ScallopQuery,
|
|
1047
|
-
assetCoinNames:
|
|
1047
|
+
assetCoinNames: string[] = [...query.constants.whitelist.lending],
|
|
1048
1048
|
ownerAddress?: string
|
|
1049
1049
|
) => {
|
|
1050
1050
|
const owner = ownerAddress ?? query.suiKit.currentAddress();
|
|
1051
|
-
const assetCoins = {} as OptionalKeys<Record<
|
|
1051
|
+
const assetCoins = {} as OptionalKeys<Record<string, number>>;
|
|
1052
1052
|
|
|
1053
1053
|
await Promise.allSettled(
|
|
1054
1054
|
assetCoinNames.map(async (assetCoinName) => {
|
|
@@ -1070,7 +1070,7 @@ export const getCoinAmounts = async (
|
|
|
1070
1070
|
*/
|
|
1071
1071
|
export const getCoinAmount = async (
|
|
1072
1072
|
query: ScallopQuery,
|
|
1073
|
-
assetCoinName:
|
|
1073
|
+
assetCoinName: string,
|
|
1074
1074
|
ownerAddress?: string
|
|
1075
1075
|
) => {
|
|
1076
1076
|
const owner = ownerAddress ?? query.suiKit.currentAddress();
|
|
@@ -1092,16 +1092,16 @@ export const getCoinAmount = async (
|
|
|
1092
1092
|
*/
|
|
1093
1093
|
export const getMarketCoinAmounts = async (
|
|
1094
1094
|
query: ScallopQuery,
|
|
1095
|
-
marketCoinNames?:
|
|
1095
|
+
marketCoinNames?: string[],
|
|
1096
1096
|
ownerAddress?: string
|
|
1097
1097
|
) => {
|
|
1098
1098
|
marketCoinNames =
|
|
1099
1099
|
marketCoinNames ||
|
|
1100
|
-
[...
|
|
1100
|
+
[...query.constants.whitelist.lending].map((poolCoinName) =>
|
|
1101
1101
|
query.utils.parseMarketCoinName(poolCoinName)
|
|
1102
1102
|
);
|
|
1103
1103
|
const owner = ownerAddress ?? query.suiKit.currentAddress();
|
|
1104
|
-
const marketCoins = {} as OptionalKeys<Record<
|
|
1104
|
+
const marketCoins = {} as OptionalKeys<Record<string, number>>;
|
|
1105
1105
|
|
|
1106
1106
|
await Promise.allSettled(
|
|
1107
1107
|
marketCoinNames.map(async (marketCoinName) => {
|
|
@@ -1127,7 +1127,7 @@ export const getMarketCoinAmounts = async (
|
|
|
1127
1127
|
*/
|
|
1128
1128
|
export const getMarketCoinAmount = async (
|
|
1129
1129
|
query: ScallopQuery,
|
|
1130
|
-
marketCoinName:
|
|
1130
|
+
marketCoinName: string,
|
|
1131
1131
|
ownerAddress?: string
|
|
1132
1132
|
) => {
|
|
1133
1133
|
const owner = ownerAddress ?? query.suiKit.currentAddress();
|
|
@@ -1148,10 +1148,11 @@ export const getMarketCoinAmount = async (
|
|
|
1148
1148
|
|
|
1149
1149
|
export const getFlashLoanFees = async (
|
|
1150
1150
|
query: ScallopQuery,
|
|
1151
|
-
assetNames:
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
const missingAssets:
|
|
1151
|
+
assetNames: string[],
|
|
1152
|
+
feeRate = 1e4
|
|
1153
|
+
): Promise<Record<string, number>> => {
|
|
1154
|
+
const missingAssets: string[] = [];
|
|
1155
|
+
let results: Record<string, number> = {};
|
|
1155
1156
|
|
|
1156
1157
|
// create mapping from asset type to asset name
|
|
1157
1158
|
const assetTypeMap = assetNames.reduce(
|
|
@@ -1160,67 +1161,48 @@ export const getFlashLoanFees = async (
|
|
|
1160
1161
|
prev[assetType] = curr;
|
|
1161
1162
|
return prev;
|
|
1162
1163
|
},
|
|
1163
|
-
{} as Record<string,
|
|
1164
|
+
{} as Record<string, string>
|
|
1164
1165
|
);
|
|
1165
1166
|
|
|
1166
1167
|
// use the mapped object first
|
|
1167
1168
|
const objIds = assetNames
|
|
1168
1169
|
.map((assetName) => {
|
|
1169
|
-
|
|
1170
|
+
const mappedFlashloanFeeObject =
|
|
1171
|
+
query.constants.poolAddresses[assetName]?.flashloanFeeObject;
|
|
1172
|
+
if (!mappedFlashloanFeeObject) {
|
|
1170
1173
|
missingAssets.push(assetName);
|
|
1171
1174
|
return null;
|
|
1172
1175
|
} else {
|
|
1173
|
-
return
|
|
1176
|
+
return mappedFlashloanFeeObject;
|
|
1174
1177
|
}
|
|
1175
1178
|
})
|
|
1176
1179
|
.filter((t) => !!t) as string[];
|
|
1177
1180
|
|
|
1178
|
-
const flashloanFeeObjects = await query.cache
|
|
1181
|
+
const flashloanFeeObjects = await queryMultipleObjects(query.cache, objIds);
|
|
1179
1182
|
|
|
1180
1183
|
if (missingAssets.length > 0) {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
throw new Error('Failed to get market object');
|
|
1186
|
-
|
|
1187
|
-
// get vault
|
|
1188
|
-
const vault = (marketObjectRes.data.content.fields as any).vault;
|
|
1189
|
-
|
|
1190
|
-
// get vault balance sheet object id
|
|
1191
|
-
const flashloanFeesTableId = vault.fields.flash_loan_fees.fields.table
|
|
1192
|
-
.fields.id.id as string;
|
|
1193
|
-
|
|
1194
|
-
// the balance sheet is a VecSet<0x1::type_name::TypeName
|
|
1195
|
-
const balanceSheetDynamicFields = await query.cache.queryGetDynamicFields({
|
|
1196
|
-
parentId: flashloanFeesTableId,
|
|
1197
|
-
limit: 10,
|
|
1198
|
-
});
|
|
1199
|
-
|
|
1200
|
-
// get the dynamic object ids
|
|
1201
|
-
const dynamicFieldObjectIds =
|
|
1202
|
-
balanceSheetDynamicFields?.data
|
|
1203
|
-
.filter((field) => {
|
|
1204
|
-
const assetType = (field.name.value as any).name as string;
|
|
1205
|
-
return !!assetTypeMap[assetType];
|
|
1206
|
-
})
|
|
1207
|
-
.map((field) => field.objectId) ?? [];
|
|
1208
|
-
|
|
1209
|
-
flashloanFeeObjects.push(
|
|
1210
|
-
...(await query.cache.queryGetObjects(dynamicFieldObjectIds))
|
|
1184
|
+
const missingDatas = await queryFlashLoanFees(
|
|
1185
|
+
query.utils,
|
|
1186
|
+
missingAssets,
|
|
1187
|
+
feeRate
|
|
1211
1188
|
);
|
|
1189
|
+
results = { ...results, ...missingDatas };
|
|
1212
1190
|
}
|
|
1213
1191
|
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1192
|
+
results = {
|
|
1193
|
+
...results,
|
|
1194
|
+
...flashloanFeeObjects.reduce(
|
|
1195
|
+
(prev, curr) => {
|
|
1196
|
+
if (curr.content?.dataType === 'moveObject') {
|
|
1197
|
+
const objectFields = curr.content.fields as any;
|
|
1198
|
+
const assetType = (curr.content.fields as any).name.fields.name;
|
|
1199
|
+
const feeNumerator = +objectFields.value;
|
|
1200
|
+
prev[assetTypeMap[assetType]] = feeNumerator / feeRate;
|
|
1201
|
+
}
|
|
1202
|
+
return prev;
|
|
1203
|
+
},
|
|
1204
|
+
{} as Record<string, number>
|
|
1205
|
+
),
|
|
1206
|
+
};
|
|
1207
|
+
return results;
|
|
1226
1208
|
};
|