@scallop-io/sui-scallop-sdk 1.3.4-alpha.7 → 1.3.4
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 +3 -3
- package/dist/constants/enum.d.ts +1 -1
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/poolAddress.d.ts +1 -1
- package/dist/index.js +434 -335
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +434 -335
- package/dist/index.mjs.map +1 -1
- package/dist/models/scallopQuery.d.ts +11 -3
- package/dist/models/scallopUtils.d.ts +1 -1
- package/dist/queries/borrowLimitQuery.d.ts +9 -0
- package/dist/queries/coreQuery.d.ts +2 -0
- package/dist/queries/index.d.ts +1 -0
- package/dist/queries/portfolioQuery.d.ts +2 -0
- package/dist/queries/sCoinQuery.d.ts +1 -1
- package/dist/queries/supplyLimitQuery.d.ts +2 -2
- package/dist/types/query/core.d.ts +2 -0
- package/dist/types/utils.d.ts +1 -1
- package/package.json +1 -1
- package/src/constants/coinGecko.ts +4 -2
- package/src/constants/common.ts +6 -1
- package/src/constants/enum.ts +20 -15
- package/src/constants/index.ts +1 -0
- package/src/constants/poolAddress.ts +7 -1
- package/src/constants/pyth.ts +2 -0
- package/src/constants/testAddress.ts +76 -35
- package/src/models/scallopBuilder.ts +2 -3
- package/src/models/scallopQuery.ts +26 -18
- package/src/models/scallopUtils.ts +5 -6
- package/src/queries/borrowLimitQuery.ts +52 -0
- package/src/queries/coreQuery.ts +238 -229
- package/src/queries/index.ts +1 -0
- package/src/queries/isolatedAssetQuery.ts +4 -5
- package/src/queries/priceQuery.ts +6 -4
- package/src/queries/referralQuery.ts +0 -1
- package/src/queries/spoolQuery.ts +1 -1
- package/src/queries/supplyLimitQuery.ts +22 -18
- package/src/types/query/core.ts +2 -0
- package/src/types/utils.ts +1 -1
|
@@ -130,10 +130,9 @@ export class ScallopUtils {
|
|
|
130
130
|
* @param address - ScallopAddress instance.
|
|
131
131
|
*/
|
|
132
132
|
public async init(force: boolean = false, address?: ScallopAddress) {
|
|
133
|
-
if (
|
|
133
|
+
if (address && !this.address) this.address = address;
|
|
134
|
+
if (force || !this.address.getAddresses()) {
|
|
134
135
|
await this.address.read();
|
|
135
|
-
} else {
|
|
136
|
-
this.address = address;
|
|
137
136
|
}
|
|
138
137
|
}
|
|
139
138
|
|
|
@@ -537,9 +536,9 @@ export class ScallopUtils {
|
|
|
537
536
|
for (const endpoint of endpoints) {
|
|
538
537
|
const priceIdPairs = Array.from(failedRequests.values()).reduce(
|
|
539
538
|
(acc, coinName) => {
|
|
540
|
-
const priceId =
|
|
541
|
-
`core.coins.${coinName}.oracle.pyth.feed`
|
|
542
|
-
|
|
539
|
+
const priceId =
|
|
540
|
+
this.address.get(`core.coins.${coinName}.oracle.pyth.feed`) ??
|
|
541
|
+
PYTH_FEED_IDS[coinName];
|
|
543
542
|
acc.push([coinName, priceId]);
|
|
544
543
|
return acc;
|
|
545
544
|
},
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ScallopUtils } from 'src/models';
|
|
2
|
+
import { SupportPoolCoins } from 'src/types';
|
|
3
|
+
import { z as zod } from 'zod';
|
|
4
|
+
|
|
5
|
+
const borrowLimitZod = zod.object({
|
|
6
|
+
dataType: zod.string(),
|
|
7
|
+
type: zod.string(),
|
|
8
|
+
hasPublicTransfer: zod.boolean(),
|
|
9
|
+
fields: zod.object({
|
|
10
|
+
id: zod.object({
|
|
11
|
+
id: zod.string(),
|
|
12
|
+
}),
|
|
13
|
+
name: zod.object({
|
|
14
|
+
type: zod.string(),
|
|
15
|
+
}),
|
|
16
|
+
value: zod.string(),
|
|
17
|
+
}),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const borrowLimitKeyType = `0xe7dbb371a9595631f7964b7ece42255ad0e738cc85fe6da26c7221b220f01af6::market_dynamic_keys::BorrowLimitKey`; // prod
|
|
21
|
+
// const borrowLimitKeyType = `0xb784ea287d944e478a3ceaa071f8885072cce6b7224cf245914dc2f9963f460e::market_dynamic_keys::BorrowLimitKey`;
|
|
22
|
+
/**
|
|
23
|
+
* Return supply limit of a pool (including the decimals)
|
|
24
|
+
* @param utils
|
|
25
|
+
* @param poolName
|
|
26
|
+
* @returns supply limit (decimals included)
|
|
27
|
+
*/
|
|
28
|
+
export const getBorrowLimit = async (
|
|
29
|
+
utils: ScallopUtils,
|
|
30
|
+
poolName: SupportPoolCoins
|
|
31
|
+
) => {
|
|
32
|
+
try {
|
|
33
|
+
const poolCoinType = utils.parseCoinType(poolName).slice(2);
|
|
34
|
+
const marketObject = utils.address.get('core.market');
|
|
35
|
+
if (!marketObject) return null;
|
|
36
|
+
|
|
37
|
+
const object = await utils.cache.queryGetDynamicFieldObject({
|
|
38
|
+
parentId: marketObject,
|
|
39
|
+
name: {
|
|
40
|
+
type: borrowLimitKeyType,
|
|
41
|
+
value: poolCoinType,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const parsedData = borrowLimitZod.safeParse(object?.data?.content);
|
|
46
|
+
if (!parsedData.success) return null;
|
|
47
|
+
return parsedData.data.fields.value;
|
|
48
|
+
} catch (e: any) {
|
|
49
|
+
console.error(`Error in getBorrowLimit for ${poolName}: ${e.message}`);
|
|
50
|
+
return '0';
|
|
51
|
+
}
|
|
52
|
+
};
|
package/src/queries/coreQuery.ts
CHANGED
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
PROTOCOL_OBJECT_ID,
|
|
5
5
|
SUPPORT_COLLATERALS,
|
|
6
6
|
BORROW_FEE_PROTOCOL_ID,
|
|
7
|
-
USE_TEST_ADDRESS,
|
|
8
7
|
FlashLoanFeeObjectMap,
|
|
9
8
|
} from '../constants';
|
|
10
9
|
import {
|
|
@@ -39,7 +38,8 @@ import {
|
|
|
39
38
|
} from '../types';
|
|
40
39
|
import BigNumber from 'bignumber.js';
|
|
41
40
|
import { getSupplyLimit } from './supplyLimitQuery';
|
|
42
|
-
|
|
41
|
+
import { isIsolatedAsset } from './isolatedAssetQuery';
|
|
42
|
+
import { getBorrowLimit } from './borrowLimitQuery';
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Query market data.
|
|
@@ -140,6 +140,12 @@ export const queryMarket = async (
|
|
|
140
140
|
.shiftedBy(-coinDecimal)
|
|
141
141
|
.toNumber();
|
|
142
142
|
|
|
143
|
+
const maxBorrowCoin = BigNumber(
|
|
144
|
+
(await getBorrowLimit(query.utils, poolCoinName)) ?? '0'
|
|
145
|
+
)
|
|
146
|
+
.shiftedBy(-coinDecimal)
|
|
147
|
+
.toNumber();
|
|
148
|
+
|
|
143
149
|
pools[poolCoinName] = {
|
|
144
150
|
coinName: poolCoinName,
|
|
145
151
|
symbol: query.utils.parseSymbol(poolCoinName),
|
|
@@ -158,9 +164,10 @@ export const queryMarket = async (
|
|
|
158
164
|
borrowFee: parsedMarketPoolData.borrowFee,
|
|
159
165
|
marketCoinSupplyAmount: parsedMarketPoolData.marketCoinSupplyAmount,
|
|
160
166
|
minBorrowAmount: parsedMarketPoolData.minBorrowAmount,
|
|
161
|
-
|
|
162
|
-
isIsolated: false,
|
|
167
|
+
isIsolated: await isIsolatedAsset(query.utils, poolCoinName),
|
|
168
|
+
// isIsolated: false,
|
|
163
169
|
maxSupplyCoin,
|
|
170
|
+
maxBorrowCoin,
|
|
164
171
|
...calculatedMarketPoolData,
|
|
165
172
|
};
|
|
166
173
|
}
|
|
@@ -206,6 +213,7 @@ export const queryMarket = async (
|
|
|
206
213
|
liquidationPanelty: parsedMarketCollateralData.liquidationPanelty,
|
|
207
214
|
liquidationReserveFactor:
|
|
208
215
|
parsedMarketCollateralData.liquidationReserveFactor,
|
|
216
|
+
isIsolated: await isIsolatedAsset(query.utils, collateralCoinName),
|
|
209
217
|
...calculatedMarketCollateralData,
|
|
210
218
|
};
|
|
211
219
|
}
|
|
@@ -296,131 +304,132 @@ export const getMarketPool = async (
|
|
|
296
304
|
indexer: boolean = false,
|
|
297
305
|
marketObject?: SuiObjectData | null,
|
|
298
306
|
coinPrice?: number
|
|
299
|
-
) => {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
marketPoolIndexer.coinName
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
return marketPoolIndexer;
|
|
318
|
-
}
|
|
307
|
+
): Promise<MarketPool | undefined> => {
|
|
308
|
+
try {
|
|
309
|
+
coinPrice =
|
|
310
|
+
coinPrice ||
|
|
311
|
+
(await query.utils.getCoinPrices([poolCoinName]))?.[poolCoinName];
|
|
312
|
+
|
|
313
|
+
if (indexer) {
|
|
314
|
+
const marketPoolIndexer = await query.indexer.getMarketPool(poolCoinName);
|
|
315
|
+
if (!marketPoolIndexer) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
marketPoolIndexer.coinPrice = coinPrice ?? marketPoolIndexer.coinPrice;
|
|
319
|
+
marketPoolIndexer.coinWrappedType = query.utils.getCoinWrappedType(
|
|
320
|
+
marketPoolIndexer.coinName
|
|
321
|
+
);
|
|
319
322
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
marketObject ??
|
|
323
|
-
(
|
|
324
|
-
await query.cache.queryGetObject(marketId, {
|
|
325
|
-
showContent: true,
|
|
326
|
-
})
|
|
327
|
-
)?.data;
|
|
323
|
+
return marketPoolIndexer;
|
|
324
|
+
}
|
|
328
325
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
326
|
+
const marketId = query.address.get('core.market');
|
|
327
|
+
marketObject =
|
|
328
|
+
marketObject ||
|
|
329
|
+
(
|
|
330
|
+
await query.cache.queryGetObject(marketId, {
|
|
331
|
+
showContent: true,
|
|
332
|
+
})
|
|
333
|
+
)?.data;
|
|
334
|
+
|
|
335
|
+
if (!(marketObject && marketObject.content?.dataType === 'moveObject'))
|
|
336
|
+
throw new Error(`Failed to fetch marketObject`);
|
|
337
|
+
|
|
338
|
+
const fields = marketObject.content.fields as any;
|
|
339
|
+
const coinType = query.utils.parseCoinType(poolCoinName);
|
|
340
|
+
// Get balance sheet.
|
|
341
|
+
const balanceSheetParentId =
|
|
342
|
+
fields.vault.fields.balance_sheets.fields.table.fields.id.id;
|
|
343
|
+
const balanceSheetDynamicFieldObjectResponse =
|
|
344
|
+
await query.cache.queryGetDynamicFieldObject({
|
|
345
|
+
parentId: balanceSheetParentId,
|
|
346
|
+
name: {
|
|
347
|
+
type: '0x1::type_name::TypeName',
|
|
348
|
+
value: {
|
|
349
|
+
name: coinType.substring(2),
|
|
344
350
|
},
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
throw new Error(
|
|
348
|
-
`Failed to fetch balanceSheetDynamicFieldObjectResponse for ${poolCoinName}`
|
|
349
|
-
);
|
|
351
|
+
},
|
|
352
|
+
});
|
|
350
353
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
+
const balanceSheetDynamicFieldObject =
|
|
355
|
+
balanceSheetDynamicFieldObjectResponse?.data;
|
|
356
|
+
|
|
357
|
+
if (
|
|
358
|
+
!(
|
|
354
359
|
balanceSheetDynamicFieldObject &&
|
|
355
360
|
balanceSheetDynamicFieldObject.content &&
|
|
356
361
|
'fields' in balanceSheetDynamicFieldObject.content
|
|
357
|
-
)
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
throw new Error(
|
|
365
|
+
`Failed to fetch balanceSheetDynamicFieldObject for ${poolCoinName}: ${balanceSheetDynamicFieldObjectResponse?.error?.code.toString()}`
|
|
366
|
+
);
|
|
367
|
+
const balanceSheet: BalanceSheet = (
|
|
368
|
+
balanceSheetDynamicFieldObject.content.fields as any
|
|
369
|
+
).value.fields;
|
|
370
|
+
|
|
371
|
+
// Get borrow index.
|
|
372
|
+
const borrowIndexParentId =
|
|
373
|
+
fields.borrow_dynamics.fields.table.fields.id.id;
|
|
374
|
+
const borrowIndexDynamicFieldObjectResponse =
|
|
375
|
+
await query.cache.queryGetDynamicFieldObject({
|
|
376
|
+
parentId: borrowIndexParentId,
|
|
377
|
+
name: {
|
|
378
|
+
type: '0x1::type_name::TypeName',
|
|
379
|
+
value: {
|
|
380
|
+
name: coinType.substring(2),
|
|
374
381
|
},
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
throw new Error(
|
|
378
|
-
`Failed to fetch borrowIndexDynamicFieldObjectResponse for ${poolCoinName}`
|
|
379
|
-
);
|
|
382
|
+
},
|
|
383
|
+
});
|
|
380
384
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
385
|
+
const borrowIndexDynamicFieldObject =
|
|
386
|
+
borrowIndexDynamicFieldObjectResponse?.data;
|
|
387
|
+
if (
|
|
388
|
+
!(
|
|
384
389
|
borrowIndexDynamicFieldObject &&
|
|
385
390
|
borrowIndexDynamicFieldObject.content &&
|
|
386
391
|
'fields' in borrowIndexDynamicFieldObject.content
|
|
387
|
-
)
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
392
|
+
)
|
|
393
|
+
)
|
|
394
|
+
throw new Error(
|
|
395
|
+
`Failed to fetch borrowIndexDynamicFieldObject for ${poolCoinName}`
|
|
396
|
+
);
|
|
397
|
+
const borrowIndex: BorrowIndex = (
|
|
398
|
+
borrowIndexDynamicFieldObject.content.fields as any
|
|
399
|
+
).value.fields;
|
|
400
|
+
|
|
401
|
+
// Get interest models.
|
|
402
|
+
const interestModelParentId =
|
|
403
|
+
fields.interest_models.fields.table.fields.id.id;
|
|
404
|
+
const interestModelDynamicFieldObjectResponse =
|
|
405
|
+
await query.cache.queryGetDynamicFieldObject({
|
|
406
|
+
parentId: interestModelParentId,
|
|
407
|
+
name: {
|
|
408
|
+
type: '0x1::type_name::TypeName',
|
|
409
|
+
value: {
|
|
410
|
+
name: coinType.substring(2),
|
|
404
411
|
},
|
|
405
|
-
}
|
|
412
|
+
},
|
|
413
|
+
});
|
|
406
414
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const interestModelDynamicFieldObject =
|
|
412
|
-
interestModelDynamicFieldObjectResponse.data;
|
|
413
|
-
if (
|
|
415
|
+
const interestModelDynamicFieldObject =
|
|
416
|
+
interestModelDynamicFieldObjectResponse?.data;
|
|
417
|
+
if (
|
|
418
|
+
!(
|
|
414
419
|
interestModelDynamicFieldObject &&
|
|
415
420
|
interestModelDynamicFieldObject.content &&
|
|
416
421
|
'fields' in interestModelDynamicFieldObject.content
|
|
417
|
-
)
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
+
)
|
|
423
|
+
)
|
|
424
|
+
throw new Error(
|
|
425
|
+
`Failed to fetch interestModelDynamicFieldObject for ${poolCoinName}: ${interestModelDynamicFieldObject}`
|
|
426
|
+
);
|
|
427
|
+
const interestModel: InterestModel = (
|
|
428
|
+
interestModelDynamicFieldObject.content.fields as any
|
|
429
|
+
).value.fields;
|
|
422
430
|
|
|
423
|
-
|
|
431
|
+
// Get borrow fee.
|
|
432
|
+
const getBorrowFee = async () => {
|
|
424
433
|
const borrowFeeDynamicFieldObjectResponse =
|
|
425
434
|
await query.cache.queryGetDynamicFieldObject({
|
|
426
435
|
parentId: marketId,
|
|
@@ -434,29 +443,19 @@ export const getMarketPool = async (
|
|
|
434
443
|
},
|
|
435
444
|
});
|
|
436
445
|
|
|
437
|
-
if (!borrowFeeDynamicFieldObjectResponse)
|
|
438
|
-
throw new Error(
|
|
439
|
-
`Failed to fetch borrowFeeDynamicFieldObjectResponse for ${poolCoinName}`
|
|
440
|
-
);
|
|
441
446
|
const borrowFeeDynamicFieldObject =
|
|
442
|
-
borrowFeeDynamicFieldObjectResponse
|
|
447
|
+
borrowFeeDynamicFieldObjectResponse?.data;
|
|
443
448
|
if (
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
449
|
+
!(
|
|
450
|
+
borrowFeeDynamicFieldObject &&
|
|
451
|
+
borrowFeeDynamicFieldObject.content &&
|
|
452
|
+
'fields' in borrowFeeDynamicFieldObject.content
|
|
453
|
+
)
|
|
454
|
+
)
|
|
455
|
+
return { value: '0' };
|
|
456
|
+
return (borrowFeeDynamicFieldObject.content.fields as any).value.fields;
|
|
457
|
+
};
|
|
453
458
|
|
|
454
|
-
if (
|
|
455
|
-
balanceSheet &&
|
|
456
|
-
borrowIndex &&
|
|
457
|
-
interestModel &&
|
|
458
|
-
(USE_TEST_ADDRESS || borrowFeeRate)
|
|
459
|
-
) {
|
|
460
459
|
const parsedMarketPoolData = parseOriginMarketPoolData({
|
|
461
460
|
type: interestModel.type.fields,
|
|
462
461
|
maxBorrowRate: interestModel.max_borrow_rate.fields,
|
|
@@ -470,7 +469,7 @@ export const getMarketPool = async (
|
|
|
470
469
|
reserve: balanceSheet.revenue,
|
|
471
470
|
reserveFactor: interestModel.revenue_factor.fields,
|
|
472
471
|
borrowWeight: interestModel.borrow_weight.fields,
|
|
473
|
-
borrowFeeRate:
|
|
472
|
+
borrowFeeRate: await getBorrowFee(),
|
|
474
473
|
baseBorrowRatePerSec: interestModel.base_borrow_rate_per_sec.fields,
|
|
475
474
|
borrowRateOnHighKink: interestModel.borrow_rate_on_high_kink.fields,
|
|
476
475
|
borrowRateOnMidKink: interestModel.borrow_rate_on_mid_kink.fields,
|
|
@@ -490,8 +489,13 @@ export const getMarketPool = async (
|
|
|
490
489
|
)
|
|
491
490
|
.shiftedBy(-coinDecimal)
|
|
492
491
|
.toNumber();
|
|
492
|
+
const maxBorrowCoin = BigNumber(
|
|
493
|
+
(await getBorrowLimit(query.utils, poolCoinName)) ?? '0'
|
|
494
|
+
)
|
|
495
|
+
.shiftedBy(-coinDecimal)
|
|
496
|
+
.toNumber();
|
|
493
497
|
|
|
494
|
-
|
|
498
|
+
return {
|
|
495
499
|
coinName: poolCoinName,
|
|
496
500
|
symbol: query.utils.parseSymbol(poolCoinName),
|
|
497
501
|
coinType: query.utils.parseCoinType(poolCoinName),
|
|
@@ -510,13 +514,13 @@ export const getMarketPool = async (
|
|
|
510
514
|
marketCoinSupplyAmount: parsedMarketPoolData.marketCoinSupplyAmount,
|
|
511
515
|
minBorrowAmount: parsedMarketPoolData.minBorrowAmount,
|
|
512
516
|
maxSupplyCoin,
|
|
513
|
-
|
|
514
|
-
isIsolated:
|
|
517
|
+
maxBorrowCoin,
|
|
518
|
+
isIsolated: await isIsolatedAsset(query.utils, poolCoinName),
|
|
515
519
|
...calculatedMarketPoolData,
|
|
516
520
|
};
|
|
521
|
+
} catch (e: any) {
|
|
522
|
+
console.error(e.message);
|
|
517
523
|
}
|
|
518
|
-
|
|
519
|
-
return marketPool;
|
|
520
524
|
};
|
|
521
525
|
|
|
522
526
|
/**
|
|
@@ -594,7 +598,7 @@ export const getMarketCollateral = async (
|
|
|
594
598
|
indexer: boolean = false,
|
|
595
599
|
marketObject?: SuiObjectData | null,
|
|
596
600
|
coinPrice?: number
|
|
597
|
-
) => {
|
|
601
|
+
): Promise<MarketCollateral | undefined> => {
|
|
598
602
|
coinPrice =
|
|
599
603
|
coinPrice ||
|
|
600
604
|
(await query.utils.getCoinPrices([collateralCoinName]))?.[
|
|
@@ -613,9 +617,9 @@ export const getMarketCollateral = async (
|
|
|
613
617
|
return marketCollateralIndexer;
|
|
614
618
|
}
|
|
615
619
|
|
|
616
|
-
let marketCollateral: MarketCollateral | undefined;
|
|
617
|
-
let riskModel: RiskModel | undefined;
|
|
618
|
-
let collateralStat: CollateralStat | undefined;
|
|
620
|
+
// let marketCollateral: MarketCollateral | undefined;
|
|
621
|
+
// let riskModel: RiskModel | undefined;
|
|
622
|
+
// let collateralStat: CollateralStat | undefined;
|
|
619
623
|
|
|
620
624
|
const marketId = query.address.get('core.market');
|
|
621
625
|
marketObject =
|
|
@@ -626,101 +630,106 @@ export const getMarketCollateral = async (
|
|
|
626
630
|
})
|
|
627
631
|
)?.data;
|
|
628
632
|
|
|
629
|
-
if (marketObject)
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
if (!riskModelDynamicFieldObjectResponse) return undefined;
|
|
648
|
-
const riskModelDynamicFieldObject =
|
|
649
|
-
riskModelDynamicFieldObjectResponse.data;
|
|
650
|
-
if (
|
|
651
|
-
riskModelDynamicFieldObject &&
|
|
652
|
-
riskModelDynamicFieldObject.content &&
|
|
653
|
-
'fields' in riskModelDynamicFieldObject.content
|
|
654
|
-
) {
|
|
655
|
-
const dynamicFields = riskModelDynamicFieldObject.content.fields as any;
|
|
656
|
-
riskModel = dynamicFields.value.fields;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// Get collateral stat.
|
|
660
|
-
const collateralStatParentId =
|
|
661
|
-
fields.collateral_stats.fields.table.fields.id.id;
|
|
662
|
-
const collateralStatDynamicFieldObjectResponse =
|
|
663
|
-
await query.cache.queryGetDynamicFieldObject({
|
|
664
|
-
parentId: collateralStatParentId,
|
|
665
|
-
name: {
|
|
666
|
-
type: '0x1::type_name::TypeName',
|
|
667
|
-
value: {
|
|
668
|
-
name: coinType.substring(2),
|
|
669
|
-
},
|
|
670
|
-
},
|
|
671
|
-
});
|
|
633
|
+
if (!(marketObject && marketObject.content?.dataType === 'moveObject'))
|
|
634
|
+
throw new Error(`Failed to fetch marketObject`);
|
|
635
|
+
|
|
636
|
+
const fields = marketObject.content.fields as any;
|
|
637
|
+
const coinType = query.utils.parseCoinType(collateralCoinName);
|
|
638
|
+
|
|
639
|
+
// Get risk model.
|
|
640
|
+
const riskModelParentId = fields.risk_models.fields.table.fields.id.id;
|
|
641
|
+
const riskModelDynamicFieldObjectResponse =
|
|
642
|
+
await query.cache.queryGetDynamicFieldObject({
|
|
643
|
+
parentId: riskModelParentId,
|
|
644
|
+
name: {
|
|
645
|
+
type: '0x1::type_name::TypeName',
|
|
646
|
+
value: {
|
|
647
|
+
name: coinType.substring(2),
|
|
648
|
+
},
|
|
649
|
+
},
|
|
650
|
+
});
|
|
672
651
|
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
652
|
+
const riskModelDynamicFieldObject = riskModelDynamicFieldObjectResponse?.data;
|
|
653
|
+
if (
|
|
654
|
+
!(
|
|
655
|
+
riskModelDynamicFieldObject &&
|
|
656
|
+
riskModelDynamicFieldObject.content &&
|
|
657
|
+
'fields' in riskModelDynamicFieldObject.content
|
|
658
|
+
)
|
|
659
|
+
)
|
|
660
|
+
throw new Error(
|
|
661
|
+
`Failed to fetch riskModelDynamicFieldObject for ${riskModelDynamicFieldObjectResponse?.error?.code.toString()}: `
|
|
662
|
+
);
|
|
687
663
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
664
|
+
const riskModel: RiskModel = (
|
|
665
|
+
riskModelDynamicFieldObject.content.fields as any
|
|
666
|
+
).value.fields;
|
|
667
|
+
|
|
668
|
+
// Get collateral stat.
|
|
669
|
+
const collateralStatParentId =
|
|
670
|
+
fields.collateral_stats.fields.table.fields.id.id;
|
|
671
|
+
const collateralStatDynamicFieldObjectResponse =
|
|
672
|
+
await query.cache.queryGetDynamicFieldObject({
|
|
673
|
+
parentId: collateralStatParentId,
|
|
674
|
+
name: {
|
|
675
|
+
type: '0x1::type_name::TypeName',
|
|
676
|
+
value: {
|
|
677
|
+
name: coinType.substring(2),
|
|
678
|
+
},
|
|
679
|
+
},
|
|
698
680
|
});
|
|
699
681
|
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
682
|
+
const collateralStatDynamicFieldObject =
|
|
683
|
+
collateralStatDynamicFieldObjectResponse?.data;
|
|
684
|
+
|
|
685
|
+
if (
|
|
686
|
+
!(
|
|
687
|
+
collateralStatDynamicFieldObject &&
|
|
688
|
+
collateralStatDynamicFieldObject.content &&
|
|
689
|
+
'fields' in collateralStatDynamicFieldObject.content
|
|
690
|
+
)
|
|
691
|
+
)
|
|
692
|
+
throw new Error(
|
|
693
|
+
`Failed to fetch collateralStatDynamicFieldObject for ${collateralCoinName}: ${collateralStatDynamicFieldObjectResponse?.error?.code.toString()}`
|
|
703
694
|
);
|
|
704
695
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
696
|
+
const collateralStat: CollateralStat = (
|
|
697
|
+
collateralStatDynamicFieldObject.content.fields as any
|
|
698
|
+
).value.fields;
|
|
699
|
+
|
|
700
|
+
const parsedMarketCollateralData = parseOriginMarketCollateralData({
|
|
701
|
+
type: riskModel.type.fields,
|
|
702
|
+
collateralFactor: riskModel.collateral_factor.fields,
|
|
703
|
+
liquidationFactor: riskModel.liquidation_factor.fields,
|
|
704
|
+
liquidationDiscount: riskModel.liquidation_discount.fields,
|
|
705
|
+
liquidationPanelty: riskModel.liquidation_penalty.fields,
|
|
706
|
+
liquidationReserveFactor: riskModel.liquidation_revenue_factor.fields,
|
|
707
|
+
maxCollateralAmount: riskModel.max_collateral_amount,
|
|
708
|
+
totalCollateralAmount: collateralStat.amount,
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
const calculatedMarketCollateralData = calculateMarketCollateralData(
|
|
712
|
+
query.utils,
|
|
713
|
+
parsedMarketCollateralData
|
|
714
|
+
);
|
|
722
715
|
|
|
723
|
-
return
|
|
716
|
+
return {
|
|
717
|
+
coinName: collateralCoinName,
|
|
718
|
+
symbol: query.utils.parseSymbol(collateralCoinName),
|
|
719
|
+
coinType: query.utils.parseCoinType(collateralCoinName),
|
|
720
|
+
marketCoinType: query.utils.parseMarketCoinType(collateralCoinName),
|
|
721
|
+
coinWrappedType: query.utils.getCoinWrappedType(collateralCoinName),
|
|
722
|
+
coinDecimal: query.utils.getCoinDecimal(collateralCoinName),
|
|
723
|
+
coinPrice: coinPrice ?? 0,
|
|
724
|
+
collateralFactor: parsedMarketCollateralData.collateralFactor,
|
|
725
|
+
liquidationFactor: parsedMarketCollateralData.liquidationFactor,
|
|
726
|
+
liquidationDiscount: parsedMarketCollateralData.liquidationDiscount,
|
|
727
|
+
liquidationPanelty: parsedMarketCollateralData.liquidationPanelty,
|
|
728
|
+
liquidationReserveFactor:
|
|
729
|
+
parsedMarketCollateralData.liquidationReserveFactor,
|
|
730
|
+
isIsolated: await isIsolatedAsset(query.utils, collateralCoinName),
|
|
731
|
+
...calculatedMarketCollateralData,
|
|
732
|
+
};
|
|
724
733
|
};
|
|
725
734
|
|
|
726
735
|
/**
|
package/src/queries/index.ts
CHANGED