@scallop-io/sui-scallop-sdk 0.46.55 → 0.46.57
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/pyth.d.ts +1 -1
- package/dist/index.js +1533 -1378
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1499 -1344
- package/dist/index.mjs.map +1 -1
- package/dist/models/scallop.d.ts +1 -1
- package/dist/models/scallopAddress.d.ts +4 -4
- package/dist/models/scallopBuilder.d.ts +3 -6
- package/dist/models/scallopCache.d.ts +2 -2
- package/dist/models/scallopClient.d.ts +7 -2
- package/dist/models/scallopIndexer.d.ts +3 -3
- package/dist/models/scallopPrice.d.ts +0 -0
- package/dist/models/scallopQuery.d.ts +10 -4
- package/dist/models/scallopUtils.d.ts +8 -7
- package/dist/queries/borrowIncentiveQuery.d.ts +10 -4
- package/dist/queries/coreQuery.d.ts +8 -4
- package/dist/queries/priceQuery.d.ts +7 -3
- package/dist/queries/referralQuery.d.ts +2 -2
- package/dist/queries/sCoinQuery.d.ts +18 -4
- package/dist/queries/spoolQuery.d.ts +10 -4
- package/dist/queries/vescaQuery.d.ts +7 -5
- package/dist/types/builder/index.d.ts +8 -1
- package/dist/types/builder/vesca.d.ts +2 -1
- package/dist/types/model.d.ts +27 -12
- package/dist/types/query/core.d.ts +1 -0
- package/dist/utils/query.d.ts +1 -1
- package/package.json +1 -1
- package/src/builders/borrowIncentiveBuilder.ts +19 -21
- package/src/builders/coreBuilder.ts +10 -8
- package/src/builders/spoolBuilder.ts +2 -2
- package/src/builders/vescaBuilder.ts +12 -4
- package/src/constants/common.ts +2 -0
- package/src/constants/enum.ts +4 -0
- package/src/constants/pyth.ts +2 -2
- package/src/models/scallop.ts +14 -20
- package/src/models/scallopAddress.ts +15 -5
- package/src/models/scallopBuilder.ts +42 -32
- package/src/models/scallopCache.ts +2 -2
- package/src/models/scallopClient.ts +91 -32
- package/src/models/scallopIndexer.ts +15 -8
- package/src/models/scallopPrice.ts +0 -0
- package/src/models/scallopQuery.ts +47 -25
- package/src/models/scallopUtils.ts +75 -74
- package/src/queries/borrowIncentiveQuery.ts +40 -29
- package/src/queries/coreQuery.ts +40 -26
- package/src/queries/portfolioQuery.ts +1 -2
- package/src/queries/priceQuery.ts +20 -9
- package/src/queries/referralQuery.ts +4 -4
- package/src/queries/sCoinQuery.ts +95 -17
- package/src/queries/spoolQuery.ts +26 -14
- package/src/queries/vescaQuery.ts +32 -26
- package/src/types/builder/index.ts +11 -1
- package/src/types/builder/vesca.ts +8 -1
- package/src/types/model.ts +40 -11
- package/src/types/query/core.ts +1 -0
- package/src/utils/query.ts +1 -1
|
@@ -15,11 +15,13 @@ import { ScallopQuery } from './scallopQuery';
|
|
|
15
15
|
import { ScallopCache } from './scallopCache';
|
|
16
16
|
import { requireSender } from 'src/utils';
|
|
17
17
|
import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client';
|
|
18
|
-
import type {
|
|
18
|
+
import type {
|
|
19
|
+
TransactionObjectArgument,
|
|
20
|
+
TransactionResult,
|
|
21
|
+
} from '@mysten/sui.js/transactions';
|
|
19
22
|
import type { SuiObjectArg } from '@scallop-io/sui-kit';
|
|
20
23
|
import type {
|
|
21
24
|
ScallopClientFnReturnType,
|
|
22
|
-
ScallopInstanceParams,
|
|
23
25
|
ScallopClientParams,
|
|
24
26
|
SupportPoolCoins,
|
|
25
27
|
SupportCollateralCoins,
|
|
@@ -29,6 +31,8 @@ import type {
|
|
|
29
31
|
SupportBorrowIncentiveCoins,
|
|
30
32
|
ScallopTxBlock,
|
|
31
33
|
SupportSCoin,
|
|
34
|
+
ScallopClientVeScaReturnType,
|
|
35
|
+
ScallopClientInstanceParams,
|
|
32
36
|
} from '../types';
|
|
33
37
|
|
|
34
38
|
/**
|
|
@@ -56,48 +60,42 @@ export class ScallopClient {
|
|
|
56
60
|
|
|
57
61
|
public constructor(
|
|
58
62
|
params: ScallopClientParams,
|
|
59
|
-
instance?:
|
|
63
|
+
instance?: ScallopClientInstanceParams
|
|
60
64
|
) {
|
|
61
65
|
this.params = params;
|
|
62
|
-
this.suiKit =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
this.suiKit =
|
|
67
|
+
instance?.suiKit ?? instance?.builder?.suiKit ?? new SuiKit(params);
|
|
68
|
+
this.walletAddress = normalizeSuiAddress(
|
|
69
|
+
params?.walletAddress || this.suiKit.currentAddress()
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (instance?.builder) {
|
|
73
|
+
this.builder = instance.builder;
|
|
74
|
+
this.query = this.builder.query;
|
|
75
|
+
this.utils = this.query.utils;
|
|
76
|
+
this.address = this.utils.address;
|
|
77
|
+
this.cache = this.address.cache;
|
|
78
|
+
} else {
|
|
79
|
+
this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
|
|
80
|
+
this.address = new ScallopAddress(
|
|
68
81
|
{
|
|
69
82
|
id: params?.addressesId || ADDRESSES_ID,
|
|
70
83
|
network: params?.networkType,
|
|
71
84
|
},
|
|
72
|
-
|
|
85
|
+
{
|
|
86
|
+
cache: this.cache,
|
|
87
|
+
}
|
|
73
88
|
);
|
|
74
|
-
|
|
75
|
-
instance?.query ??
|
|
76
|
-
new ScallopQuery(params, {
|
|
77
|
-
suiKit: this.suiKit,
|
|
89
|
+
this.utils = new ScallopUtils(this.params, {
|
|
78
90
|
address: this.address,
|
|
79
|
-
cache: this.cache,
|
|
80
91
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
new ScallopUtils(params, {
|
|
84
|
-
suiKit: this.suiKit,
|
|
85
|
-
address: this.address,
|
|
86
|
-
query: this.query,
|
|
87
|
-
cache: this.cache,
|
|
92
|
+
this.query = new ScallopQuery(this.params, {
|
|
93
|
+
utils: this.utils,
|
|
88
94
|
});
|
|
89
|
-
|
|
90
|
-
instance?.builder ??
|
|
91
|
-
new ScallopBuilder(params, {
|
|
92
|
-
suiKit: this.suiKit,
|
|
93
|
-
address: this.address,
|
|
95
|
+
this.builder = new ScallopBuilder(this.params, {
|
|
94
96
|
query: this.query,
|
|
95
|
-
utils: this.utils,
|
|
96
|
-
cache: this.cache,
|
|
97
97
|
});
|
|
98
|
-
|
|
99
|
-
params?.walletAddress || this.suiKit.currentAddress()
|
|
100
|
-
);
|
|
98
|
+
}
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
/**
|
|
@@ -1077,6 +1075,67 @@ export class ScallopClient {
|
|
|
1077
1075
|
}
|
|
1078
1076
|
}
|
|
1079
1077
|
|
|
1078
|
+
/* ==================== VeSCA ==================== */
|
|
1079
|
+
/**
|
|
1080
|
+
* Claim unlocked SCA from all veSCA accounts.
|
|
1081
|
+
*/
|
|
1082
|
+
public async claimAllUnlockedSca(): Promise<SuiTransactionBlockResponse>;
|
|
1083
|
+
public async claimAllUnlockedSca<S extends boolean>(
|
|
1084
|
+
sign?: S
|
|
1085
|
+
): Promise<ScallopClientVeScaReturnType<S>>;
|
|
1086
|
+
public async claimAllUnlockedSca<S extends boolean>(
|
|
1087
|
+
sign: S = true as S
|
|
1088
|
+
): Promise<ScallopClientVeScaReturnType<S>> {
|
|
1089
|
+
// get all veSca keys
|
|
1090
|
+
const veScaKeys = (
|
|
1091
|
+
(await this.query.getVeScas(this.walletAddress)) ?? []
|
|
1092
|
+
).map(({ keyObject }) => keyObject);
|
|
1093
|
+
if (veScaKeys.length === 0) {
|
|
1094
|
+
throw new Error('No veSCA found in the wallet');
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const scaCoins: TransactionResult[] = [];
|
|
1098
|
+
const tx = this.builder.createTxBlock();
|
|
1099
|
+
tx.setSender(this.walletAddress);
|
|
1100
|
+
|
|
1101
|
+
await Promise.all(
|
|
1102
|
+
veScaKeys.map(async (key) => {
|
|
1103
|
+
try {
|
|
1104
|
+
const scaCoin = await tx.redeemScaQuick(key, false);
|
|
1105
|
+
if (!scaCoin) return;
|
|
1106
|
+
scaCoins.push(scaCoin);
|
|
1107
|
+
} catch (e) {
|
|
1108
|
+
// ignore
|
|
1109
|
+
}
|
|
1110
|
+
})
|
|
1111
|
+
);
|
|
1112
|
+
|
|
1113
|
+
if (scaCoins.length === 0) {
|
|
1114
|
+
throw new Error('No unlocked SCA found in the veSCA accounts');
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
if (scaCoins.length > 1) {
|
|
1118
|
+
tx.mergeCoins(scaCoins[0], scaCoins.slice(1));
|
|
1119
|
+
}
|
|
1120
|
+
await this.utils.mergeSimilarCoins(
|
|
1121
|
+
tx,
|
|
1122
|
+
scaCoins[0],
|
|
1123
|
+
'sca',
|
|
1124
|
+
this.walletAddress
|
|
1125
|
+
);
|
|
1126
|
+
|
|
1127
|
+
if (sign) {
|
|
1128
|
+
return (await this.suiKit.signAndSendTxn(
|
|
1129
|
+
tx
|
|
1130
|
+
)) as ScallopClientVeScaReturnType<S>;
|
|
1131
|
+
} else {
|
|
1132
|
+
return {
|
|
1133
|
+
tx: tx.txBlock,
|
|
1134
|
+
scaCoin: scaCoins[0],
|
|
1135
|
+
} as ScallopClientVeScaReturnType<S>;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1080
1139
|
/* ==================== Other Method ==================== */
|
|
1081
1140
|
|
|
1082
1141
|
/**
|
|
@@ -17,11 +17,13 @@ import type {
|
|
|
17
17
|
TotalValueLocked,
|
|
18
18
|
ScallopQueryParams,
|
|
19
19
|
ScallopParams,
|
|
20
|
-
ScallopInstanceParams,
|
|
21
20
|
BorrowIncentivePoolPoints,
|
|
22
21
|
SupportBorrowIncentiveRewardCoins,
|
|
22
|
+
ScallopIndexerInstanceParams,
|
|
23
23
|
} from '../types';
|
|
24
24
|
import { ScallopCache } from './scallopCache';
|
|
25
|
+
import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
|
|
26
|
+
import { SuiKit } from '@scallop-io/sui-kit';
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
29
|
* @description
|
|
@@ -36,13 +38,18 @@ import { ScallopCache } from './scallopCache';
|
|
|
36
38
|
* ```
|
|
37
39
|
*/
|
|
38
40
|
export class ScallopIndexer {
|
|
39
|
-
private readonly
|
|
41
|
+
private readonly cache: ScallopCache;
|
|
40
42
|
public readonly params: ScallopQueryParams;
|
|
41
43
|
private readonly _requestClient: AxiosInstance;
|
|
42
44
|
|
|
43
|
-
public constructor(
|
|
45
|
+
public constructor(
|
|
46
|
+
params: ScallopParams,
|
|
47
|
+
instance?: ScallopIndexerInstanceParams
|
|
48
|
+
) {
|
|
44
49
|
this.params = params;
|
|
45
|
-
this.
|
|
50
|
+
this.cache =
|
|
51
|
+
instance?.cache ??
|
|
52
|
+
new ScallopCache(new SuiKit({}), DEFAULT_CACHE_OPTIONS);
|
|
46
53
|
this._requestClient = axios.create({
|
|
47
54
|
baseURL: SDK_API_BASE_URL,
|
|
48
55
|
headers: {
|
|
@@ -59,7 +66,7 @@ export class ScallopIndexer {
|
|
|
59
66
|
* @return Market data.
|
|
60
67
|
*/
|
|
61
68
|
public async getMarket(): Promise<Pick<Market, 'pools' | 'collaterals'>> {
|
|
62
|
-
const response = await this.
|
|
69
|
+
const response = await this.cache.queryClient.fetchQuery({
|
|
63
70
|
queryKey: ['market'],
|
|
64
71
|
queryFn: async () => {
|
|
65
72
|
return await this._requestClient.get<{
|
|
@@ -137,7 +144,7 @@ export class ScallopIndexer {
|
|
|
137
144
|
* @return Spools data.
|
|
138
145
|
*/
|
|
139
146
|
public async getSpools(): Promise<Required<Spools>> {
|
|
140
|
-
const response = await this.
|
|
147
|
+
const response = await this.cache.queryClient.fetchQuery({
|
|
141
148
|
queryKey: ['spools'],
|
|
142
149
|
queryFn: async () => {
|
|
143
150
|
return await this._requestClient.get<{
|
|
@@ -175,7 +182,7 @@ export class ScallopIndexer {
|
|
|
175
182
|
public async getBorrowIncentivePools(): Promise<
|
|
176
183
|
Required<BorrowIncentivePools>
|
|
177
184
|
> {
|
|
178
|
-
const response = await this.
|
|
185
|
+
const response = await this.cache.queryClient.fetchQuery({
|
|
179
186
|
queryKey: ['borrowIncentivePools'],
|
|
180
187
|
queryFn: async () => {
|
|
181
188
|
return await this._requestClient.get<{
|
|
@@ -237,7 +244,7 @@ export class ScallopIndexer {
|
|
|
237
244
|
supplyValueChangeRatio: number;
|
|
238
245
|
}
|
|
239
246
|
> {
|
|
240
|
-
const response = await this.
|
|
247
|
+
const response = await this.cache.queryClient.fetchQuery({
|
|
241
248
|
queryKey: ['totalValueLocked'],
|
|
242
249
|
queryFn: async () => {
|
|
243
250
|
return await this._requestClient.get<
|
|
File without changes
|
|
@@ -36,7 +36,6 @@ import {
|
|
|
36
36
|
} from '../queries';
|
|
37
37
|
import {
|
|
38
38
|
ScallopQueryParams,
|
|
39
|
-
ScallopInstanceParams,
|
|
40
39
|
SupportStakeMarketCoins,
|
|
41
40
|
SupportAssetCoins,
|
|
42
41
|
SupportPoolCoins,
|
|
@@ -46,6 +45,7 @@ import {
|
|
|
46
45
|
StakeRewardPools,
|
|
47
46
|
SupportBorrowIncentiveCoins,
|
|
48
47
|
SupportSCoin,
|
|
48
|
+
ScallopQueryInstanceParams,
|
|
49
49
|
} from '../types';
|
|
50
50
|
import { ScallopAddress } from './scallopAddress';
|
|
51
51
|
import { ScallopUtils } from './scallopUtils';
|
|
@@ -56,6 +56,7 @@ import { SuiObjectData } from '@mysten/sui.js/client';
|
|
|
56
56
|
import {
|
|
57
57
|
getSCoinAmount,
|
|
58
58
|
getSCoinAmounts,
|
|
59
|
+
getSCoinSwapRate,
|
|
59
60
|
getSCoinTotalSupply,
|
|
60
61
|
} from 'src/queries/sCoinQuery';
|
|
61
62
|
import { normalizeSuiAddress } from '@mysten/sui.js/utils';
|
|
@@ -84,32 +85,36 @@ export class ScallopQuery {
|
|
|
84
85
|
|
|
85
86
|
public constructor(
|
|
86
87
|
params: ScallopQueryParams,
|
|
87
|
-
instance?:
|
|
88
|
+
instance?: ScallopQueryInstanceParams
|
|
88
89
|
) {
|
|
89
90
|
this.params = params;
|
|
90
|
-
this.suiKit =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
instance
|
|
95
|
-
|
|
91
|
+
this.suiKit =
|
|
92
|
+
instance?.suiKit ?? instance?.utils?.suiKit ?? new SuiKit(params);
|
|
93
|
+
if (instance?.utils) {
|
|
94
|
+
this.utils = instance.utils;
|
|
95
|
+
this.address = instance.utils.address;
|
|
96
|
+
this.cache = this.address.cache;
|
|
97
|
+
} else {
|
|
98
|
+
this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
|
|
99
|
+
this.address = new ScallopAddress(
|
|
96
100
|
{
|
|
97
101
|
id: params?.addressesId || ADDRESSES_ID,
|
|
98
102
|
network: params?.networkType,
|
|
99
103
|
},
|
|
100
|
-
|
|
104
|
+
{
|
|
105
|
+
cache: this.cache,
|
|
106
|
+
}
|
|
101
107
|
);
|
|
102
|
-
|
|
103
|
-
instance?.utils ??
|
|
104
|
-
new ScallopUtils(this.params, {
|
|
105
|
-
suiKit: this.suiKit,
|
|
108
|
+
this.utils = new ScallopUtils(this.params, {
|
|
106
109
|
address: this.address,
|
|
107
|
-
cache: this.cache,
|
|
108
|
-
query: this,
|
|
109
110
|
});
|
|
110
|
-
|
|
111
|
+
}
|
|
112
|
+
this.indexer =
|
|
113
|
+
instance?.indexer ??
|
|
114
|
+
new ScallopIndexer(this.params, { cache: this.cache });
|
|
115
|
+
|
|
111
116
|
this.walletAddress = normalizeSuiAddress(
|
|
112
|
-
params
|
|
117
|
+
params.walletAddress || this.suiKit.currentAddress()
|
|
113
118
|
);
|
|
114
119
|
}
|
|
115
120
|
|
|
@@ -365,8 +370,9 @@ export class ScallopQuery {
|
|
|
365
370
|
* @param stakeMarketCoinNames - Specific an array of support stake market coin name.
|
|
366
371
|
* @return Stake pools data.
|
|
367
372
|
*/
|
|
368
|
-
public async getStakePools(
|
|
369
|
-
stakeMarketCoinNames =
|
|
373
|
+
public async getStakePools(
|
|
374
|
+
stakeMarketCoinNames: SupportStakeMarketCoins[] = [...SUPPORT_SPOOLS]
|
|
375
|
+
) {
|
|
370
376
|
const stakePools: StakePools = {};
|
|
371
377
|
for (const stakeMarketCoinName of stakeMarketCoinNames) {
|
|
372
378
|
const stakePool = await getStakePool(this, stakeMarketCoinName);
|
|
@@ -404,9 +410,8 @@ export class ScallopQuery {
|
|
|
404
410
|
* @return Stake reward pools data.
|
|
405
411
|
*/
|
|
406
412
|
public async getStakeRewardPools(
|
|
407
|
-
stakeMarketCoinNames
|
|
413
|
+
stakeMarketCoinNames: SupportStakeMarketCoins[] = [...SUPPORT_SPOOLS]
|
|
408
414
|
) {
|
|
409
|
-
stakeMarketCoinNames = stakeMarketCoinNames ?? [...SUPPORT_SPOOLS];
|
|
410
415
|
const stakeRewardPools: StakeRewardPools = {};
|
|
411
416
|
await Promise.allSettled(
|
|
412
417
|
stakeMarketCoinNames.map(async (stakeMarketCoinName) => {
|
|
@@ -558,7 +563,7 @@ export class ScallopQuery {
|
|
|
558
563
|
* @param walletAddress
|
|
559
564
|
* @returns array of veSca
|
|
560
565
|
*/
|
|
561
|
-
public async getVeScas(walletAddress
|
|
566
|
+
public async getVeScas(walletAddress: string = this.walletAddress) {
|
|
562
567
|
return await getVeScas(this, walletAddress);
|
|
563
568
|
}
|
|
564
569
|
|
|
@@ -567,7 +572,7 @@ export class ScallopQuery {
|
|
|
567
572
|
* @returns Promise<string | undefined>
|
|
568
573
|
*/
|
|
569
574
|
public async getVeScaTreasuryInfo() {
|
|
570
|
-
return await getVeScaTreasuryInfo(this);
|
|
575
|
+
return await getVeScaTreasuryInfo(this.utils);
|
|
571
576
|
}
|
|
572
577
|
|
|
573
578
|
/**
|
|
@@ -575,8 +580,13 @@ export class ScallopQuery {
|
|
|
575
580
|
* @param walletAddress
|
|
576
581
|
* @returns veScaKeyId
|
|
577
582
|
*/
|
|
578
|
-
public async getVeScaKeyIdFromReferralBindings(
|
|
579
|
-
|
|
583
|
+
public async getVeScaKeyIdFromReferralBindings(
|
|
584
|
+
walletAddress: string = this.walletAddress
|
|
585
|
+
) {
|
|
586
|
+
return await queryVeScaKeyIdFromReferralBindings(
|
|
587
|
+
this.address,
|
|
588
|
+
walletAddress
|
|
589
|
+
);
|
|
580
590
|
}
|
|
581
591
|
|
|
582
592
|
/**
|
|
@@ -646,6 +656,18 @@ export class ScallopQuery {
|
|
|
646
656
|
: 0;
|
|
647
657
|
}
|
|
648
658
|
|
|
659
|
+
/**
|
|
660
|
+
* Get swap rate from sCoin A to sCoin B
|
|
661
|
+
* @param assetCoinNames
|
|
662
|
+
* @returns
|
|
663
|
+
*/
|
|
664
|
+
public async getSCoinSwapRate(
|
|
665
|
+
fromSCoin: SupportSCoin,
|
|
666
|
+
toSCoin: SupportSCoin
|
|
667
|
+
) {
|
|
668
|
+
return await getSCoinSwapRate(this, fromSCoin, toSCoin);
|
|
669
|
+
}
|
|
670
|
+
|
|
649
671
|
/*
|
|
650
672
|
* Get flashloan fee for specified assets
|
|
651
673
|
*/
|
|
@@ -2,7 +2,6 @@ import { SUI_TYPE_ARG, normalizeStructTag } from '@mysten/sui.js/utils';
|
|
|
2
2
|
import { SuiKit } from '@scallop-io/sui-kit';
|
|
3
3
|
import { SuiPriceServiceConnection } from '@pythnetwork/pyth-sui-js';
|
|
4
4
|
import { ScallopAddress } from './scallopAddress';
|
|
5
|
-
import { ScallopQuery } from './scallopQuery';
|
|
6
5
|
import {
|
|
7
6
|
ADDRESSES_ID,
|
|
8
7
|
PROTOCOL_OBJECT_ID,
|
|
@@ -19,7 +18,7 @@ import {
|
|
|
19
18
|
SUPPORT_SCOIN,
|
|
20
19
|
sCoinIds,
|
|
21
20
|
} from '../constants';
|
|
22
|
-
import { queryObligation } from '../queries';
|
|
21
|
+
import { getPythPrice, queryObligation } from '../queries';
|
|
23
22
|
import {
|
|
24
23
|
parseDataFromPythPriceFeed,
|
|
25
24
|
isMarketCoin,
|
|
@@ -31,7 +30,6 @@ import { ScallopCache } from './scallopCache';
|
|
|
31
30
|
import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
|
|
32
31
|
import type {
|
|
33
32
|
ScallopUtilsParams,
|
|
34
|
-
ScallopInstanceParams,
|
|
35
33
|
SupportCoins,
|
|
36
34
|
SupportAssetCoins,
|
|
37
35
|
SupportMarketCoins,
|
|
@@ -41,6 +39,7 @@ import type {
|
|
|
41
39
|
PriceMap,
|
|
42
40
|
CoinWrappedType,
|
|
43
41
|
SupportSCoin,
|
|
42
|
+
ScallopUtilsInstanceParams,
|
|
44
43
|
} from '../types';
|
|
45
44
|
import type { SuiAddressArg, SuiTxArg, SuiTxBlock } from '@scallop-io/sui-kit';
|
|
46
45
|
|
|
@@ -60,36 +59,42 @@ export class ScallopUtils {
|
|
|
60
59
|
public readonly params: ScallopUtilsParams;
|
|
61
60
|
public readonly isTestnet: boolean;
|
|
62
61
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
public suiKit: SuiKit;
|
|
63
|
+
public address: ScallopAddress;
|
|
64
|
+
public cache: ScallopCache;
|
|
66
65
|
private _priceMap: PriceMap = new Map();
|
|
67
|
-
private _cache: ScallopCache;
|
|
68
66
|
|
|
69
67
|
public constructor(
|
|
70
68
|
params: ScallopUtilsParams,
|
|
71
|
-
instance?:
|
|
69
|
+
instance?: ScallopUtilsInstanceParams
|
|
72
70
|
) {
|
|
73
|
-
this.params =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.
|
|
78
|
-
instance?.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
71
|
+
this.params = {
|
|
72
|
+
pythEndpoints: params.pythEndpoints ?? PYTH_ENDPOINTS['mainnet'],
|
|
73
|
+
...params,
|
|
74
|
+
};
|
|
75
|
+
this.suiKit =
|
|
76
|
+
instance?.suiKit ??
|
|
77
|
+
instance?.address?.cache._suiKit ??
|
|
78
|
+
new SuiKit(params);
|
|
79
|
+
|
|
80
|
+
if (instance?.address) {
|
|
81
|
+
this.address = instance.address;
|
|
82
|
+
this.cache = this.address.cache;
|
|
83
|
+
this.suiKit = this.address.cache._suiKit;
|
|
84
|
+
} else {
|
|
85
|
+
this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
|
|
86
|
+
this.address =
|
|
87
|
+
instance?.address ??
|
|
88
|
+
new ScallopAddress(
|
|
89
|
+
{
|
|
90
|
+
id: params?.addressesId || ADDRESSES_ID,
|
|
91
|
+
network: params?.networkType,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
cache: this.cache,
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
93
98
|
this.isTestnet = params.networkType
|
|
94
99
|
? params.networkType === 'testnet'
|
|
95
100
|
: false;
|
|
@@ -102,14 +107,10 @@ export class ScallopUtils {
|
|
|
102
107
|
* @param address - ScallopAddress instance.
|
|
103
108
|
*/
|
|
104
109
|
public async init(force: boolean = false, address?: ScallopAddress) {
|
|
105
|
-
if (force || !this.
|
|
106
|
-
await this.
|
|
110
|
+
if (force || !this.address.getAddresses() || !address?.getAddresses()) {
|
|
111
|
+
await this.address.read();
|
|
107
112
|
} else {
|
|
108
|
-
this.
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (!this._query.address.getAddresses()) {
|
|
112
|
-
await this._query.init(force, this._address);
|
|
113
|
+
this.address = address;
|
|
113
114
|
}
|
|
114
115
|
}
|
|
115
116
|
|
|
@@ -146,7 +147,7 @@ export class ScallopUtils {
|
|
|
146
147
|
public parseCoinType(coinName: SupportCoins) {
|
|
147
148
|
coinName = isMarketCoin(coinName) ? this.parseCoinName(coinName) : coinName;
|
|
148
149
|
const coinPackageId =
|
|
149
|
-
this.
|
|
150
|
+
this.address.get(`core.coins.${coinName}.id`) ||
|
|
150
151
|
coinIds[coinName] ||
|
|
151
152
|
undefined;
|
|
152
153
|
if (!coinPackageId) {
|
|
@@ -154,20 +155,20 @@ export class ScallopUtils {
|
|
|
154
155
|
}
|
|
155
156
|
if (coinName === 'sui')
|
|
156
157
|
return normalizeStructTag(`${coinPackageId}::sui::SUI`);
|
|
157
|
-
const
|
|
158
|
-
this.
|
|
159
|
-
this.
|
|
160
|
-
this.
|
|
161
|
-
this.
|
|
162
|
-
this.
|
|
163
|
-
this.
|
|
158
|
+
const wormHolePackageIds = [
|
|
159
|
+
this.address.get('core.coins.usdc.id') ?? wormholeCoinIds.usdc,
|
|
160
|
+
this.address.get('core.coins.usdt.id') ?? wormholeCoinIds.usdt,
|
|
161
|
+
this.address.get('core.coins.eth.id') ?? wormholeCoinIds.eth,
|
|
162
|
+
this.address.get('core.coins.btc.id') ?? wormholeCoinIds.btc,
|
|
163
|
+
this.address.get('core.coins.sol.id') ?? wormholeCoinIds.sol,
|
|
164
|
+
this.address.get('core.coins.apt.id') ?? wormholeCoinIds.apt,
|
|
164
165
|
];
|
|
165
|
-
const
|
|
166
|
-
this.
|
|
166
|
+
const voloPackageIds = [
|
|
167
|
+
this.address.get('core.coins.vsui.id') ?? voloCoinIds.vsui,
|
|
167
168
|
];
|
|
168
|
-
if (
|
|
169
|
+
if (wormHolePackageIds.includes(coinPackageId)) {
|
|
169
170
|
return `${coinPackageId}::coin::COIN`;
|
|
170
|
-
} else if (
|
|
171
|
+
} else if (voloPackageIds.includes(coinPackageId)) {
|
|
171
172
|
return `${coinPackageId}::cert::CERT`;
|
|
172
173
|
} else {
|
|
173
174
|
return `${coinPackageId}::${coinName}::${coinName.toUpperCase()}`;
|
|
@@ -222,7 +223,7 @@ export class ScallopUtils {
|
|
|
222
223
|
* @returns sCoin treasury id
|
|
223
224
|
*/
|
|
224
225
|
public getSCoinTreasury(sCoinName: SupportSCoin) {
|
|
225
|
-
return this.
|
|
226
|
+
return this.address.get(`scoin.coins.${sCoinName}.treasury`);
|
|
226
227
|
}
|
|
227
228
|
|
|
228
229
|
/**
|
|
@@ -234,7 +235,7 @@ export class ScallopUtils {
|
|
|
234
235
|
*/
|
|
235
236
|
public parseMarketCoinType(coinName: SupportCoins) {
|
|
236
237
|
const protocolObjectId =
|
|
237
|
-
this.
|
|
238
|
+
this.address.get('core.object') || PROTOCOL_OBJECT_ID;
|
|
238
239
|
const coinType = this.parseCoinType(coinName);
|
|
239
240
|
return `${protocolObjectId}::reserve::MarketCoin<${coinType}>`;
|
|
240
241
|
}
|
|
@@ -267,27 +268,27 @@ export class ScallopUtils {
|
|
|
267
268
|
|
|
268
269
|
const wormHoleCoinTypeMap: Record<string, SupportAssetCoins> = {
|
|
269
270
|
[`${
|
|
270
|
-
this.
|
|
271
|
+
this.address.get('core.coins.usdc.id') ?? wormholeCoinIds.usdc
|
|
271
272
|
}::coin::COIN`]: 'usdc',
|
|
272
273
|
[`${
|
|
273
|
-
this.
|
|
274
|
+
this.address.get('core.coins.usdt.id') ?? wormholeCoinIds.usdt
|
|
274
275
|
}::coin::COIN`]: 'usdt',
|
|
275
276
|
[`${
|
|
276
|
-
this.
|
|
277
|
+
this.address.get('core.coins.eth.id') ?? wormholeCoinIds.eth
|
|
277
278
|
}::coin::COIN`]: 'eth',
|
|
278
279
|
[`${
|
|
279
|
-
this.
|
|
280
|
+
this.address.get('core.coins.btc.id') ?? wormholeCoinIds.btc
|
|
280
281
|
}::coin::COIN`]: 'btc',
|
|
281
282
|
[`${
|
|
282
|
-
this.
|
|
283
|
+
this.address.get('core.coins.sol.id') ?? wormholeCoinIds.sol
|
|
283
284
|
}::coin::COIN`]: 'sol',
|
|
284
285
|
[`${
|
|
285
|
-
this.
|
|
286
|
+
this.address.get('core.coins.apt.id') ?? wormholeCoinIds.apt
|
|
286
287
|
}::coin::COIN`]: 'apt',
|
|
287
288
|
};
|
|
288
289
|
const voloCoinTypeMap: Record<string, SupportAssetCoins> = {
|
|
289
290
|
[`${
|
|
290
|
-
this.
|
|
291
|
+
this.address.get('core.coins.vsui.id') ?? voloCoinIds.vsui
|
|
291
292
|
}::cert::CERT`]: 'vsui',
|
|
292
293
|
};
|
|
293
294
|
|
|
@@ -388,8 +389,8 @@ export class ScallopUtils {
|
|
|
388
389
|
coinType: string = SUI_TYPE_ARG,
|
|
389
390
|
ownerAddress?: string
|
|
390
391
|
) {
|
|
391
|
-
ownerAddress = ownerAddress || this.
|
|
392
|
-
const coins = await this.
|
|
392
|
+
ownerAddress = ownerAddress || this.suiKit.currentAddress();
|
|
393
|
+
const coins = await this.suiKit.suiInteractor.selectCoins(
|
|
393
394
|
ownerAddress,
|
|
394
395
|
amount,
|
|
395
396
|
coinType
|
|
@@ -412,14 +413,14 @@ export class ScallopUtils {
|
|
|
412
413
|
): Promise<void> {
|
|
413
414
|
// merge to existing coins if exist
|
|
414
415
|
try {
|
|
415
|
-
const
|
|
416
|
+
const existingCoins = await this.selectCoins(
|
|
416
417
|
Number.MAX_SAFE_INTEGER,
|
|
417
418
|
coinType,
|
|
418
419
|
sender
|
|
419
420
|
);
|
|
420
421
|
|
|
421
|
-
if (
|
|
422
|
-
txBlock.mergeCoins(dest,
|
|
422
|
+
if (existingCoins.length > 0) {
|
|
423
|
+
txBlock.mergeCoins(dest, existingCoins.slice(0, 500));
|
|
423
424
|
}
|
|
424
425
|
} catch (e) {
|
|
425
426
|
// ignore
|
|
@@ -437,15 +438,15 @@ export class ScallopUtils {
|
|
|
437
438
|
* @return Asset coin Names.
|
|
438
439
|
*/
|
|
439
440
|
public async getObligationCoinNames(obligationId: SuiAddressArg) {
|
|
440
|
-
const obligation = await queryObligation(this
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
441
|
+
const obligation = await queryObligation(this, obligationId);
|
|
442
|
+
if (!obligation) return undefined;
|
|
443
|
+
|
|
444
|
+
const collateralCoinTypes = obligation.collaterals.map((collateral) => {
|
|
445
|
+
return `0x${collateral.type.name}`;
|
|
446
|
+
});
|
|
447
|
+
const debtCoinTypes = obligation.debts.map((debt) => {
|
|
448
|
+
return `0x${debt.type.name}`;
|
|
449
|
+
});
|
|
449
450
|
const obligationCoinTypes = [
|
|
450
451
|
...new Set([...collateralCoinTypes, ...debtCoinTypes]),
|
|
451
452
|
];
|
|
@@ -507,7 +508,7 @@ export class ScallopUtils {
|
|
|
507
508
|
for (const endpoint of endpoints) {
|
|
508
509
|
const priceIds = Array.from(failedRequests.values()).reduce(
|
|
509
510
|
(acc, coinName) => {
|
|
510
|
-
const priceId = this.
|
|
511
|
+
const priceId = this.address.get(
|
|
511
512
|
`core.coins.${coinName}.oracle.pyth.feed`
|
|
512
513
|
);
|
|
513
514
|
acc[coinName] = priceId;
|
|
@@ -520,14 +521,14 @@ export class ScallopUtils {
|
|
|
520
521
|
Object.entries(priceIds).map(async ([coinName, priceId]) => {
|
|
521
522
|
const pythConnection = new SuiPriceServiceConnection(endpoint);
|
|
522
523
|
try {
|
|
523
|
-
const feed = await this.
|
|
524
|
+
const feed = await this.address.cache.queryClient.fetchQuery({
|
|
524
525
|
queryKey: [priceId],
|
|
525
526
|
queryFn: async () => {
|
|
526
527
|
return await pythConnection.getLatestPriceFeeds([priceId]);
|
|
527
528
|
},
|
|
528
529
|
});
|
|
529
530
|
if (feed) {
|
|
530
|
-
const data = parseDataFromPythPriceFeed(feed[0], this.
|
|
531
|
+
const data = parseDataFromPythPriceFeed(feed[0], this.address);
|
|
531
532
|
this._priceMap.set(coinName as SupportAssetCoins, {
|
|
532
533
|
price: data.price,
|
|
533
534
|
publishTime: data.publishTime,
|
|
@@ -548,7 +549,7 @@ export class ScallopUtils {
|
|
|
548
549
|
if (failedRequests.size > 0) {
|
|
549
550
|
await Promise.allSettled(
|
|
550
551
|
Array.from(failedRequests.values()).map(async (coinName) => {
|
|
551
|
-
const price = await this
|
|
552
|
+
const price = await getPythPrice(this, coinName);
|
|
552
553
|
this._priceMap.set(coinName, {
|
|
553
554
|
price: price,
|
|
554
555
|
publishTime: Date.now(),
|