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

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.
@@ -0,0 +1,269 @@
1
+ import axios, { AxiosInstance } from 'axios';
2
+ import { SDK_API_BASE_URL } from '../constants';
3
+ import type {
4
+ Market,
5
+ MarketPools,
6
+ MarketPool,
7
+ MarketCollaterals,
8
+ MarketCollateral,
9
+ Spools,
10
+ Spool,
11
+ BorrowIncentivePools,
12
+ BorrowIncentivePool,
13
+ SupportPoolCoins,
14
+ SupportCollateralCoins,
15
+ SupportStakeMarketCoins,
16
+ SupportBorrowIncentiveCoins,
17
+ TotalValueLocked,
18
+ } from '../types';
19
+
20
+ /**
21
+ * @description
22
+ * It provides methods to obtain sdk index data from mainnet.
23
+ *
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const scallopIndexer = new scallopIndexer(<parameters>);
28
+ * scallopIndexer.<indexer functions>();
29
+ * await scallopIndexer.<indexer async functions>();
30
+ * ```
31
+ */
32
+ export class ScallopIndexer {
33
+ private _requestClient: AxiosInstance;
34
+
35
+ public constructor() {
36
+ this._requestClient = axios.create({
37
+ baseURL: SDK_API_BASE_URL,
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ Accept: 'application/json',
41
+ },
42
+ timeout: 30000,
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Get market index data.
48
+ *
49
+ * @return Market data.
50
+ */
51
+ public async getMarket(): Promise<Pick<Market, 'pools' | 'collaterals'>> {
52
+ const response = await this._requestClient.get<{
53
+ pools: MarketPool[];
54
+ collaterals: MarketCollateral[];
55
+ }>(`${SDK_API_BASE_URL}/api/market`);
56
+
57
+ if (response.status === 200) {
58
+ return {
59
+ pools: response.data.pools.reduce((marketPools, marketPool) => {
60
+ marketPools[marketPool.coinName] = marketPool;
61
+ return marketPools;
62
+ }, {} as MarketPools),
63
+ collaterals: response.data.collaterals.reduce(
64
+ (marketCollaterals, marketCollateral) => {
65
+ marketCollaterals[marketCollateral.coinName] = marketCollateral;
66
+ return marketCollaterals;
67
+ },
68
+ {} as MarketCollaterals
69
+ ),
70
+ };
71
+ } else {
72
+ throw Error('Failed to getMarket.');
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Get market pools index data.
78
+ *
79
+ * @return Market pools data.
80
+ */
81
+ public async getMarketPools(): Promise<Required<MarketPools>> {
82
+ const response = await this._requestClient.get<{
83
+ pools: MarketPool[];
84
+ }>(`${SDK_API_BASE_URL}/api/market/pools`);
85
+
86
+ if (response.status === 200) {
87
+ return response.data.pools.reduce((marketPools, marketPool) => {
88
+ marketPools[marketPool.coinName] = marketPool;
89
+ return marketPools;
90
+ }, {} as MarketPools) as Required<MarketPools>;
91
+ } else {
92
+ throw Error('Failed to getMarketPools.');
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Get market pool index data.
98
+ *
99
+ * @return Market pool data.
100
+ */
101
+ public async getMarketPool(
102
+ poolCoinName: SupportPoolCoins
103
+ ): Promise<MarketPool> {
104
+ const response = await this._requestClient.get<{
105
+ pool: MarketPool;
106
+ }>(`${SDK_API_BASE_URL}/api/market/pool/${poolCoinName}`);
107
+
108
+ if (response.status === 200) {
109
+ return response.data.pool;
110
+ } else {
111
+ throw Error('Failed to getMarketPool.');
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Get market collaterals index data.
117
+ *
118
+ * @return Market collaterals data.
119
+ */
120
+ public async getMarketCollaterals(): Promise<Required<MarketCollaterals>> {
121
+ const response = await this._requestClient.get<{
122
+ collaterals: MarketCollateral[];
123
+ }>(`${SDK_API_BASE_URL}/api/market/collaterals`);
124
+
125
+ if (response.status === 200) {
126
+ return response.data.collaterals.reduce(
127
+ (marketCollaterals, marketCollateral) => {
128
+ marketCollaterals[marketCollateral.coinName] = marketCollateral;
129
+ return marketCollaterals;
130
+ },
131
+ {} as MarketCollaterals
132
+ ) as Required<MarketCollaterals>;
133
+ } else {
134
+ throw Error('Failed to getMarketCollaterals.');
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Get market collateral index data.
140
+ *
141
+ * @return Market collateral data.
142
+ */
143
+ public async getMarketCollateral(
144
+ collateralCoinName: SupportCollateralCoins
145
+ ): Promise<MarketCollateral> {
146
+ const response = await this._requestClient.get<{
147
+ collateral: MarketCollateral;
148
+ }>(`${SDK_API_BASE_URL}/api/market/collateral/${collateralCoinName}`);
149
+
150
+ if (response.status === 200) {
151
+ return response.data.collateral;
152
+ } else {
153
+ throw Error('Failed to getMarketCollateral.');
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Get spools index data.
159
+ *
160
+ * @return Spools data.
161
+ */
162
+ public async getSpools(): Promise<Required<Spools>> {
163
+ const response = await this._requestClient.get<{
164
+ spools: Spool[];
165
+ }>(`${SDK_API_BASE_URL}/api/spools`);
166
+
167
+ if (response.status === 200) {
168
+ return response.data.spools.reduce((spools, spool) => {
169
+ spools[spool.marketCoinName] = spool;
170
+ return spools;
171
+ }, {} as Spools) as Required<Spools>;
172
+ } else {
173
+ throw Error('Failed to getSpools.');
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Get spool index data.
179
+ *
180
+ * @return Spool data.
181
+ */
182
+ public async getSpool(
183
+ marketCoinName: SupportStakeMarketCoins
184
+ ): Promise<Spool> {
185
+ const response = await this._requestClient.get<{
186
+ spool: Spool;
187
+ }>(`${SDK_API_BASE_URL}/api/spool/${marketCoinName}`);
188
+
189
+ if (response.status === 200) {
190
+ return response.data.spool;
191
+ } else {
192
+ throw Error('Failed to getSpool.');
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Get borrow incentive pools index data.
198
+ *
199
+ * @return Borrow incentive pools data.
200
+ */
201
+ public async getBorrowIncentivePools(): Promise<
202
+ Required<BorrowIncentivePools>
203
+ > {
204
+ const response = await this._requestClient.get<{
205
+ borrowIncentivePools: BorrowIncentivePool[];
206
+ }>(`${SDK_API_BASE_URL}/api/borrowIncentivePools`);
207
+
208
+ if (response.status === 200) {
209
+ return response.data.borrowIncentivePools.reduce(
210
+ (borrowIncentivePools, borrowIncentivePool) => {
211
+ borrowIncentivePools[borrowIncentivePool.coinName] =
212
+ borrowIncentivePool;
213
+ return borrowIncentivePools;
214
+ },
215
+ {} as BorrowIncentivePools
216
+ ) as Required<BorrowIncentivePools>;
217
+ } else {
218
+ throw Error('Failed to getBorrowIncentivePools.');
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Get borrow incentive pool index data.
224
+ *
225
+ * @return Borrow incentive pool data.
226
+ */
227
+ public async getBorrowIncentivePool(
228
+ borrowIncentiveCoinName: SupportBorrowIncentiveCoins
229
+ ): Promise<BorrowIncentivePool> {
230
+ const response = await this._requestClient.get<{
231
+ borrowIncentivePool: BorrowIncentivePool;
232
+ }>(
233
+ `${SDK_API_BASE_URL}/api/borrowIncentivePool/${borrowIncentiveCoinName}`
234
+ );
235
+
236
+ if (response.status === 200) {
237
+ return response.data.borrowIncentivePool;
238
+ } else {
239
+ throw Error('Failed to getSpool.');
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Get total value locked index data.
245
+ *
246
+ * @return Total value locked.
247
+ */
248
+ public async getTotalValueLocked(): Promise<
249
+ TotalValueLocked & {
250
+ totalValueChangeRatio: number;
251
+ borrowValueChangeRatio: number;
252
+ supplyValueChangeRatio: number;
253
+ }
254
+ > {
255
+ const response = await this._requestClient.get<
256
+ TotalValueLocked & {
257
+ totalValueChangeRatio: number;
258
+ borrowValueChangeRatio: number;
259
+ supplyValueChangeRatio: number;
260
+ }
261
+ >(`${SDK_API_BASE_URL}/api/market/tvl`);
262
+
263
+ if (response.status === 200) {
264
+ return response.data;
265
+ } else {
266
+ throw Error('Failed to getTotalValueLocked.');
267
+ }
268
+ }
269
+ }
@@ -40,10 +40,11 @@ import {
40
40
  } from '../types';
41
41
  import { ScallopAddress } from './scallopAddress';
42
42
  import { ScallopUtils } from './scallopUtils';
43
+ import { ScallopIndexer } from './scallopIndexer';
43
44
 
44
45
  /**
45
46
  * @description
46
- * it provides methods for getting on-chain data from the Scallop contract.
47
+ * It provides methods for getting on-chain data from the Scallop contract.
47
48
  *
48
49
  * @example
49
50
  * ```typescript
@@ -59,6 +60,7 @@ export class ScallopQuery {
59
60
  public suiKit: SuiKit;
60
61
  public address: ScallopAddress;
61
62
  public utils: ScallopUtils;
63
+ public indexer: ScallopIndexer;
62
64
 
63
65
  public constructor(
64
66
  params: ScallopQueryParams,
@@ -79,6 +81,7 @@ export class ScallopQuery {
79
81
  address: this.address,
80
82
  query: this,
81
83
  });
84
+ this.indexer = new ScallopIndexer();
82
85
  }
83
86
 
84
87
  /**
@@ -97,11 +100,11 @@ export class ScallopQuery {
97
100
 
98
101
  /**
99
102
  * Query market data.
100
- *
103
+ * @param indexer - Whether to use indexer.
101
104
  * @return Market data.
102
105
  */
103
- public async queryMarket() {
104
- return await queryMarket(this);
106
+ public async queryMarket(indexer: boolean = false) {
107
+ return await queryMarket(this, indexer);
105
108
  }
106
109
 
107
110
  /**
@@ -112,20 +115,28 @@ export class ScallopQuery {
112
115
  * the `queryMarket` method to reduce time consumption.
113
116
  *
114
117
  * @param poolCoinNames - Specific an array of support pool coin name.
118
+ * @param indexer - Whether to use indexer.
115
119
  * @return Market pools data.
116
120
  */
117
- public async getMarketPools(poolCoinNames?: SupportPoolCoins[]) {
118
- return await getMarketPools(this, poolCoinNames);
121
+ public async getMarketPools(
122
+ poolCoinNames?: SupportPoolCoins[],
123
+ indexer: boolean = false
124
+ ) {
125
+ return await getMarketPools(this, poolCoinNames, indexer);
119
126
  }
120
127
 
121
128
  /**
122
129
  * Get market pool
123
130
  *
124
131
  * @param poolCoinName - Specific support pool coin name.
132
+ * @param indexer - Whether to use indexer.
125
133
  * @return Market pool data.
126
134
  */
127
- public async getMarketPool(poolCoinName: SupportPoolCoins) {
128
- return await getMarketPool(this, poolCoinName);
135
+ public async getMarketPool(
136
+ poolCoinName: SupportPoolCoins,
137
+ indexer: boolean = false
138
+ ) {
139
+ return await getMarketPool(this, poolCoinName, indexer);
129
140
  }
130
141
 
131
142
  /**
@@ -136,22 +147,28 @@ export class ScallopQuery {
136
147
  * the `queryMarket` method to reduce time consumption.
137
148
  *
138
149
  * @param collateralCoinNames - Specific an array of support collateral coin name.
150
+ * @param indexer - Whether to use indexer.
139
151
  * @return Market collaterals data.
140
152
  */
141
153
  public async getMarketCollaterals(
142
- collateralCoinNames?: SupportCollateralCoins[]
154
+ collateralCoinNames?: SupportCollateralCoins[],
155
+ indexer: boolean = false
143
156
  ) {
144
- return await getMarketCollaterals(this, collateralCoinNames);
157
+ return await getMarketCollaterals(this, collateralCoinNames, indexer);
145
158
  }
146
159
 
147
160
  /**
148
161
  * Get market collateral
149
162
  *
150
163
  * @param collateralCoinName - Specific support collateral coin name.
164
+ * @param indexer - Whether to use indexer.
151
165
  * @return Market collateral data.
152
166
  */
153
- public async getMarketCollateral(collateralCoinName: SupportCollateralCoins) {
154
- return await getMarketCollateral(this, collateralCoinName);
167
+ public async getMarketCollateral(
168
+ collateralCoinName: SupportCollateralCoins,
169
+ indexer: boolean = false
170
+ ) {
171
+ return await getMarketCollateral(this, collateralCoinName, indexer);
155
172
  }
156
173
 
157
174
  /**
@@ -246,20 +263,28 @@ export class ScallopQuery {
246
263
  * Get spools data.
247
264
  *
248
265
  * @param stakeMarketCoinNames - Specific an array of support stake market coin name.
266
+ * @param indexer - Whether to use indexer.
249
267
  * @return Spools data.
250
268
  */
251
- public async getSpools(stakeMarketCoinNames?: SupportStakeMarketCoins[]) {
252
- return await getSpools(this, stakeMarketCoinNames);
269
+ public async getSpools(
270
+ stakeMarketCoinNames?: SupportStakeMarketCoins[],
271
+ indexer: boolean = false
272
+ ) {
273
+ return await getSpools(this, stakeMarketCoinNames, indexer);
253
274
  }
254
275
 
255
276
  /**
256
277
  * Get spool data.
257
278
  *
258
279
  * @param stakeMarketCoinName - Specific support stake market coin name.
280
+ * @param indexer - Whether to use indexer.
259
281
  * @return Spool data.
260
282
  */
261
- public async getSpool(stakeMarketCoinName: SupportStakeMarketCoins) {
262
- return await getSpool(this, stakeMarketCoinName);
283
+ public async getSpool(
284
+ stakeMarketCoinName: SupportStakeMarketCoins,
285
+ indexer: boolean = false
286
+ ) {
287
+ return await getSpool(this, stakeMarketCoinName, indexer);
263
288
  }
264
289
 
265
290
  /**
@@ -374,12 +399,14 @@ export class ScallopQuery {
374
399
  * Get borrow incentive pools data.
375
400
  *
376
401
  * @param coinNames - Specific an array of support borrow incentive coin name.
402
+ * @param indexer - Whether to use indexer.
377
403
  * @return Borrow incentive pools data.
378
404
  */
379
405
  public async getBorrowIncentivePools(
380
- coinNames?: SupportBorrowIncentiveCoins[]
406
+ coinNames?: SupportBorrowIncentiveCoins[],
407
+ indexer: boolean = false
381
408
  ) {
382
- return await queryBorrowIncentivePools(this, coinNames);
409
+ return await queryBorrowIncentivePools(this, coinNames, indexer);
383
410
  }
384
411
 
385
412
  /**
@@ -401,13 +428,15 @@ export class ScallopQuery {
401
428
  *
402
429
  * @param poolCoinNames - Specific an array of support pool coin name.
403
430
  * @param ownerAddress - The owner address.
431
+ * @param indexer - Whether to use indexer.
404
432
  * @return All lending and spool infomation.
405
433
  */
406
434
  public async getLendings(
407
435
  poolCoinNames?: SupportPoolCoins[],
408
- ownerAddress?: string
436
+ ownerAddress?: string,
437
+ indexer: boolean = false
409
438
  ) {
410
- return await getLendings(this, poolCoinNames, ownerAddress);
439
+ return await getLendings(this, poolCoinNames, ownerAddress, indexer);
411
440
  }
412
441
 
413
442
  /**
@@ -415,13 +444,15 @@ export class ScallopQuery {
415
444
  *
416
445
  * @param poolCoinName - Specific support pool coin name.
417
446
  * @param ownerAddress - The owner address.
447
+ * @param indexer - Whether to use indexer.
418
448
  * @return Lending pool data.
419
449
  */
420
450
  public async getLending(
421
451
  poolCoinName: SupportPoolCoins,
422
- ownerAddress?: string
452
+ ownerAddress?: string,
453
+ indexer: boolean = false
423
454
  ) {
424
- return await getLending(this, poolCoinName, ownerAddress);
455
+ return await getLending(this, poolCoinName, ownerAddress, indexer);
425
456
  }
426
457
 
427
458
  /**
@@ -431,10 +462,14 @@ export class ScallopQuery {
431
462
  * All collateral and borrowing information in all obligation accounts owned by the user.
432
463
  *
433
464
  * @param ownerAddress - The owner address.
465
+ * @param indexer - Whether to use indexer.
434
466
  * @return All obligation accounts information.
435
467
  */
436
- public async getObligationAccounts(ownerAddress?: string) {
437
- return await getObligationAccounts(this, ownerAddress);
468
+ public async getObligationAccounts(
469
+ ownerAddress?: string,
470
+ indexer: boolean = false
471
+ ) {
472
+ return await getObligationAccounts(this, ownerAddress, indexer);
438
473
  }
439
474
 
440
475
  /**
@@ -445,24 +480,32 @@ export class ScallopQuery {
445
480
  *
446
481
  * @param obligationId - The obligation id.
447
482
  * @param ownerAddress - The owner address.
483
+ * @param indexer - Whether to use indexer.
448
484
  * @return Borrowing and collateral information.
449
485
  */
450
486
  public async getObligationAccount(
451
487
  obligationId: string,
452
- ownerAddress?: string
488
+ ownerAddress?: string,
489
+ indexer: boolean = false
453
490
  ) {
454
- return await getObligationAccount(this, obligationId, ownerAddress);
491
+ return await getObligationAccount(
492
+ this,
493
+ obligationId,
494
+ ownerAddress,
495
+ indexer
496
+ );
455
497
  }
456
498
 
457
499
  /**
458
500
  * Get total value locked.
459
501
  *
502
+ * @param indexer - Whether to use indexer.
460
503
  * @description
461
504
  * Include total supplied value and total borrowed value.
462
505
  *
463
506
  * @return Total value locked.
464
507
  */
465
- public async getTvl() {
466
- return await getTotalValueLocked(this);
508
+ public async getTvl(indexer: boolean = false) {
509
+ return await getTotalValueLocked(this, indexer);
467
510
  }
468
511
  }
@@ -23,11 +23,13 @@ import type {
23
23
  *
24
24
  * @param query - The Scallop query instance.
25
25
  * @param borrowIncentiveCoinNames - Specific an array of support borrow incentive coin name.
26
+ * @param indexer - Whether to use indexer.
26
27
  * @return Borrow incentive pools data.
27
28
  */
28
29
  export const queryBorrowIncentivePools = async (
29
30
  query: ScallopQuery,
30
- borrowIncentiveCoinNames?: SupportBorrowIncentiveCoins[]
31
+ borrowIncentiveCoinNames?: SupportBorrowIncentiveCoins[],
32
+ indexer: boolean = false
31
33
  ) => {
32
34
  borrowIncentiveCoinNames = borrowIncentiveCoinNames || [
33
35
  ...SUPPORT_BORROW_INCENTIVE_POOLS,
@@ -47,8 +49,32 @@ export const queryBorrowIncentivePools = async (
47
49
  borrowIncentivePoolsQueryData.reward_pool
48
50
  );
49
51
  const rewardCoinType = parsedBorrowIncentiveRewardPoolData.rewardType;
52
+ const rewardCoinName =
53
+ query.utils.parseCoinNameFromType<SupportAssetCoins>(rewardCoinType);
54
+ const coinPrices = await query.utils.getCoinPrices(
55
+ [...new Set([...borrowIncentiveCoinNames, rewardCoinName])] ?? []
56
+ );
50
57
 
51
58
  const borrowIncentivePools: BorrowIncentivePools = {};
59
+
60
+ if (indexer) {
61
+ const borrowIncentivePoolsIndexer =
62
+ await query.indexer.getBorrowIncentivePools();
63
+ for (const borrowIncentivePool of Object.values(
64
+ borrowIncentivePoolsIndexer
65
+ )) {
66
+ if (!borrowIncentiveCoinNames.includes(borrowIncentivePool.coinName))
67
+ continue;
68
+ borrowIncentivePool.coinPrice =
69
+ coinPrices[borrowIncentivePool.coinName] ||
70
+ borrowIncentivePool.coinPrice;
71
+ borrowIncentivePool.rewardCoinPrice =
72
+ coinPrices[rewardCoinName] || borrowIncentivePool.rewardCoinPrice;
73
+ borrowIncentivePools[borrowIncentivePool.coinName] = borrowIncentivePool;
74
+ }
75
+ return borrowIncentivePools;
76
+ }
77
+
52
78
  for (const pool of borrowIncentivePoolsQueryData.incentive_pools) {
53
79
  const coinType = normalizeStructTag(pool.pool_type.name);
54
80
  const coinName =
@@ -61,11 +87,6 @@ export const queryBorrowIncentivePools = async (
61
87
  continue;
62
88
  }
63
89
 
64
- const coinPrices = await query.utils.getCoinPrices([
65
- coinName,
66
- rewardCoinName,
67
- ]);
68
-
69
90
  const parsedBorrowIncentivePoolData =
70
91
  parseOriginBorrowIncentivePoolData(pool);
71
92