@scallop-io/sui-scallop-sdk 1.3.4-alpha.1 → 1.3.4-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.
Files changed (102) hide show
  1. package/dist/index.js +550 -544
  2. package/dist/index.js.map +1 -1
  3. package/dist/index.mjs +549 -544
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +2 -2
  6. package/src/builders/loyaltyProgramBuilder.ts +1 -1
  7. package/src/constants/common.ts +0 -1
  8. package/src/constants/enum.ts +12 -11
  9. package/src/constants/index.ts +0 -1
  10. package/src/constants/queryKeys.ts +1 -5
  11. package/src/constants/rpc.ts +16 -0
  12. package/src/constants/testAddress.ts +35 -76
  13. package/src/constants/tokenBucket.ts +1 -1
  14. package/src/models/scallop.ts +3 -2
  15. package/src/models/scallopAddress.ts +2 -2
  16. package/src/models/scallopBuilder.ts +7 -5
  17. package/src/models/scallopCache.ts +1 -1
  18. package/src/models/scallopClient.ts +27 -26
  19. package/src/models/scallopQuery.ts +61 -19
  20. package/src/models/scallopUtils.ts +74 -109
  21. package/src/models/suiKit.ts +11 -0
  22. package/src/queries/borrowIncentiveQuery.ts +6 -13
  23. package/src/queries/coreQuery.ts +245 -236
  24. package/src/queries/isolatedAsset.ts +4 -4
  25. package/src/queries/loyaltyProgramQuery.ts +1 -1
  26. package/src/queries/portfolioQuery.ts +58 -34
  27. package/src/queries/referralQuery.ts +1 -0
  28. package/src/queries/spoolQuery.ts +17 -17
  29. package/src/types/query/core.ts +0 -1
  30. package/src/utils/tokenBucket.ts +2 -1
  31. package/dist/builders/borrowIncentiveBuilder.d.ts +0 -12
  32. package/dist/builders/coreBuilder.d.ts +0 -12
  33. package/dist/builders/index.d.ts +0 -12
  34. package/dist/builders/loyaltyProgramBuilder.d.ts +0 -12
  35. package/dist/builders/oracle.d.ts +0 -14
  36. package/dist/builders/referralBuilder.d.ts +0 -12
  37. package/dist/builders/sCoinBuilder.d.ts +0 -4
  38. package/dist/builders/spoolBuilder.d.ts +0 -12
  39. package/dist/builders/vescaBuilder.d.ts +0 -25
  40. package/dist/constants/cache.d.ts +0 -14
  41. package/dist/constants/coinGecko.d.ts +0 -2
  42. package/dist/constants/common.d.ts +0 -20
  43. package/dist/constants/enum.d.ts +0 -13
  44. package/dist/constants/flashloan.d.ts +0 -2
  45. package/dist/constants/index.d.ts +0 -12
  46. package/dist/constants/poolAddress.d.ts +0 -5
  47. package/dist/constants/pyth.d.ts +0 -5
  48. package/dist/constants/queryKeys.d.ts +0 -59
  49. package/dist/constants/testAddress.d.ts +0 -2
  50. package/dist/constants/tokenBucket.d.ts +0 -2
  51. package/dist/constants/vesca.d.ts +0 -5
  52. package/dist/index.d.ts +0 -3
  53. package/dist/models/index.d.ts +0 -8
  54. package/dist/models/scallop.d.ts +0 -74
  55. package/dist/models/scallopAddress.d.ts +0 -150
  56. package/dist/models/scallopBuilder.d.ts +0 -89
  57. package/dist/models/scallopCache.d.ts +0 -74
  58. package/dist/models/scallopClient.d.ts +0 -321
  59. package/dist/models/scallopIndexer.d.ts +0 -89
  60. package/dist/models/scallopQuery.d.ts +0 -474
  61. package/dist/models/scallopUtils.d.ts +0 -222
  62. package/dist/queries/borrowIncentiveQuery.d.ts +0 -61
  63. package/dist/queries/coreQuery.d.ts +0 -167
  64. package/dist/queries/index.d.ts +0 -8
  65. package/dist/queries/isolatedAsset.d.ts +0 -14
  66. package/dist/queries/loyaltyProgramQuery.d.ts +0 -10
  67. package/dist/queries/portfolioQuery.d.ts +0 -73
  68. package/dist/queries/priceQuery.d.ts +0 -16
  69. package/dist/queries/referralQuery.d.ts +0 -7
  70. package/dist/queries/sCoinQuery.d.ts +0 -41
  71. package/dist/queries/spoolQuery.d.ts +0 -70
  72. package/dist/queries/supplyLimit.d.ts +0 -9
  73. package/dist/queries/vescaQuery.d.ts +0 -36
  74. package/dist/types/address.d.ts +0 -107
  75. package/dist/types/builder/borrowIncentive.d.ts +0 -35
  76. package/dist/types/builder/core.d.ts +0 -56
  77. package/dist/types/builder/index.d.ts +0 -24
  78. package/dist/types/builder/loyaltyProgram.d.ts +0 -23
  79. package/dist/types/builder/referral.d.ts +0 -30
  80. package/dist/types/builder/sCoin.d.ts +0 -37
  81. package/dist/types/builder/spool.d.ts +0 -29
  82. package/dist/types/builder/vesca.d.ts +0 -51
  83. package/dist/types/constant/common.d.ts +0 -24
  84. package/dist/types/constant/enum.d.ts +0 -48
  85. package/dist/types/constant/index.d.ts +0 -2
  86. package/dist/types/index.d.ts +0 -6
  87. package/dist/types/model.d.ts +0 -54
  88. package/dist/types/query/borrowIncentive.d.ts +0 -124
  89. package/dist/types/query/core.d.ts +0 -362
  90. package/dist/types/query/index.d.ts +0 -7
  91. package/dist/types/query/loyaltyProgram.d.ts +0 -5
  92. package/dist/types/query/portfolio.d.ts +0 -115
  93. package/dist/types/query/sCoin.d.ts +0 -1
  94. package/dist/types/query/spool.d.ts +0 -122
  95. package/dist/types/query/vesca.d.ts +0 -26
  96. package/dist/types/utils.d.ts +0 -21
  97. package/dist/utils/builder.d.ts +0 -15
  98. package/dist/utils/index.d.ts +0 -5
  99. package/dist/utils/indexer.d.ts +0 -17
  100. package/dist/utils/query.d.ts +0 -62
  101. package/dist/utils/tokenBucket.d.ts +0 -11
  102. package/dist/utils/util.d.ts +0 -26
@@ -34,6 +34,7 @@ import type {
34
34
  ScallopClientVeScaReturnType,
35
35
  ScallopClientInstanceParams,
36
36
  } from '../types';
37
+ import { newSuiKit } from './suiKit';
37
38
 
38
39
  /**
39
40
  * @description
@@ -64,9 +65,9 @@ export class ScallopClient {
64
65
  ) {
65
66
  this.params = params;
66
67
  this.suiKit =
67
- instance?.suiKit ?? instance?.builder?.suiKit ?? new SuiKit(params);
68
+ instance?.suiKit ?? instance?.builder?.suiKit ?? newSuiKit(params);
68
69
  this.walletAddress = normalizeSuiAddress(
69
- params?.walletAddress || this.suiKit.currentAddress()
70
+ params?.walletAddress ?? this.suiKit.currentAddress()
70
71
  );
71
72
 
72
73
  if (instance?.builder) {
@@ -83,7 +84,7 @@ export class ScallopClient {
83
84
  );
84
85
  this.address = new ScallopAddress(
85
86
  {
86
- id: params?.addressesId || ADDRESSES_ID,
87
+ id: params?.addressesId ?? ADDRESSES_ID,
87
88
  network: params?.networkType,
88
89
  forceInterface: params?.forceAddressesInterface,
89
90
  },
@@ -142,7 +143,7 @@ export class ScallopClient {
142
143
  * @return Obligations data.
143
144
  */
144
145
  public async getObligations(ownerAddress?: string) {
145
- const owner = ownerAddress || this.walletAddress;
146
+ const owner = ownerAddress ?? this.walletAddress;
146
147
  return await this.query.getObligations(owner);
147
148
  }
148
149
 
@@ -169,7 +170,7 @@ export class ScallopClient {
169
170
  * @return All stake accounts data.
170
171
  */
171
172
  async getAllStakeAccounts(ownerAddress?: string) {
172
- const owner = ownerAddress || this.walletAddress;
173
+ const owner = ownerAddress ?? this.walletAddress;
173
174
  return await this.query.getAllStakeAccounts(owner);
174
175
  }
175
176
 
@@ -187,7 +188,7 @@ export class ScallopClient {
187
188
  stakeMarketCoinName: SupportStakeMarketCoins,
188
189
  ownerAddress?: string
189
190
  ) {
190
- const owner = ownerAddress || this.walletAddress;
191
+ const owner = ownerAddress ?? this.walletAddress;
191
192
  return await this.query.getStakeAccounts(stakeMarketCoinName, owner);
192
193
  }
193
194
 
@@ -272,11 +273,11 @@ export class ScallopClient {
272
273
  walletAddress?: string
273
274
  ): Promise<ScallopClientFnReturnType<S>> {
274
275
  const txBlock = this.builder.createTxBlock();
275
- const sender = walletAddress || this.walletAddress;
276
+ const sender = walletAddress ?? this.walletAddress;
276
277
  txBlock.setSender(sender);
277
278
 
278
279
  const obligations = await this.query.getObligations(sender);
279
- const specificObligationId = obligationId || obligations[0]?.id;
280
+ const specificObligationId = obligationId ?? obligations[0]?.id;
280
281
  if (specificObligationId) {
281
282
  await txBlock.addCollateralQuick(
282
283
  amount,
@@ -319,7 +320,7 @@ export class ScallopClient {
319
320
  walletAddress?: string
320
321
  ): Promise<ScallopClientFnReturnType<S>> {
321
322
  const txBlock = this.builder.createTxBlock();
322
- const sender = walletAddress || this.walletAddress;
323
+ const sender = walletAddress ?? this.walletAddress;
323
324
  txBlock.setSender(sender);
324
325
 
325
326
  const collateralCoin = await txBlock.takeCollateralQuick(
@@ -365,7 +366,7 @@ export class ScallopClient {
365
366
  walletAddress?: string
366
367
  ): Promise<ScallopClientFnReturnType<S>> {
367
368
  const txBlock = this.builder.createTxBlock();
368
- const sender = walletAddress || this.walletAddress;
369
+ const sender = walletAddress ?? this.walletAddress;
369
370
  txBlock.setSender(sender);
370
371
 
371
372
  const sCoin = await txBlock.depositQuick(amount, poolCoinName);
@@ -409,14 +410,14 @@ export class ScallopClient {
409
410
  walletAddress?: string
410
411
  ): Promise<ScallopClientFnReturnType<S>> {
411
412
  const txBlock = this.builder.createTxBlock();
412
- const sender = walletAddress || this.walletAddress;
413
+ const sender = walletAddress ?? this.walletAddress;
413
414
  txBlock.setSender(sender);
414
415
 
415
416
  const stakeMarketCoinName =
416
417
  this.utils.parseMarketCoinName<SupportStakeMarketCoins>(stakeCoinName);
417
418
  const stakeAccounts =
418
419
  await this.query.getStakeAccounts(stakeMarketCoinName);
419
- const targetStakeAccount = stakeAccountId || stakeAccounts[0]?.id;
420
+ const targetStakeAccount = stakeAccountId ?? stakeAccounts[0]?.id;
420
421
 
421
422
  const marketCoin = await txBlock.depositQuick(amount, stakeCoinName, false);
422
423
  if (targetStakeAccount) {
@@ -466,7 +467,7 @@ export class ScallopClient {
466
467
  walletAddress?: string
467
468
  ): Promise<ScallopClientFnReturnType<S>> {
468
469
  const txBlock = this.builder.createTxBlock();
469
- const sender = walletAddress || this.walletAddress;
470
+ const sender = walletAddress ?? this.walletAddress;
470
471
  txBlock.setSender(sender);
471
472
 
472
473
  const coin = await txBlock.withdrawQuick(amount, poolCoinName);
@@ -501,7 +502,7 @@ export class ScallopClient {
501
502
  walletAddress?: string
502
503
  ): Promise<ScallopClientFnReturnType<S>> {
503
504
  const txBlock = this.builder.createTxBlock();
504
- const sender = walletAddress || this.walletAddress;
505
+ const sender = walletAddress ?? this.walletAddress;
505
506
  txBlock.setSender(sender);
506
507
 
507
508
  const availableStake = (
@@ -549,7 +550,7 @@ export class ScallopClient {
549
550
  walletAddress?: string
550
551
  ): Promise<ScallopClientFnReturnType<S>> {
551
552
  const txBlock = this.builder.createTxBlock();
552
- const sender = walletAddress || this.walletAddress;
553
+ const sender = walletAddress ?? this.walletAddress;
553
554
  txBlock.setSender(sender);
554
555
 
555
556
  const availableStake = (
@@ -610,7 +611,7 @@ export class ScallopClient {
610
611
  walletAddress?: string
611
612
  ): Promise<ScallopClientFnReturnType<S>> {
612
613
  const txBlock = this.builder.createTxBlock();
613
- const sender = walletAddress || this.walletAddress;
614
+ const sender = walletAddress ?? this.walletAddress;
614
615
  txBlock.setSender(sender);
615
616
  const [coin, loan] = txBlock.borrowFlashLoan(amount, poolCoinName);
616
617
  txBlock.repayFlashLoan(await callback(txBlock, coin), loan, poolCoinName);
@@ -647,7 +648,7 @@ export class ScallopClient {
647
648
  walletAddress?: string
648
649
  ): Promise<ScallopClientFnReturnType<S>> {
649
650
  const txBlock = this.builder.createTxBlock();
650
- const sender = walletAddress || this.walletAddress;
651
+ const sender = walletAddress ?? this.walletAddress;
651
652
  txBlock.setSender(sender);
652
653
 
653
654
  const stakeAccount = txBlock.createStakeAccount(marketCoinName);
@@ -691,12 +692,12 @@ export class ScallopClient {
691
692
  walletAddress?: string
692
693
  ): Promise<ScallopClientFnReturnType<S>> {
693
694
  const txBlock = this.builder.createTxBlock();
694
- const sender = walletAddress || this.walletAddress;
695
+ const sender = walletAddress ?? this.walletAddress;
695
696
  txBlock.setSender(sender);
696
697
 
697
698
  const stakeAccounts =
698
699
  await this.query.getStakeAccounts(stakeMarketCoinName);
699
- const targetStakeAccount = stakeAccountId || stakeAccounts[0]?.id;
700
+ const targetStakeAccount = stakeAccountId ?? stakeAccounts[0]?.id;
700
701
  if (targetStakeAccount) {
701
702
  await txBlock.stakeQuick(amount, stakeMarketCoinName, targetStakeAccount);
702
703
  } else {
@@ -743,7 +744,7 @@ export class ScallopClient {
743
744
  walletAddress?: string
744
745
  ): Promise<ScallopClientFnReturnType<S>> {
745
746
  const txBlock = this.builder.createTxBlock();
746
- const sender = walletAddress || this.walletAddress;
747
+ const sender = walletAddress ?? this.walletAddress;
747
748
  txBlock.setSender(sender);
748
749
 
749
750
  const sCoin = await txBlock.unstakeQuick(
@@ -802,7 +803,7 @@ export class ScallopClient {
802
803
  walletAddress?: string
803
804
  ): Promise<ScallopClientFnReturnType<S>> {
804
805
  const txBlock = this.builder.createTxBlock();
805
- const sender = walletAddress || this.walletAddress;
806
+ const sender = walletAddress ?? this.walletAddress;
806
807
  txBlock.setSender(sender);
807
808
 
808
809
  const stakeMarketCoin = await txBlock.unstakeQuick(
@@ -863,7 +864,7 @@ export class ScallopClient {
863
864
  walletAddress?: string
864
865
  ): Promise<ScallopClientFnReturnType<S>> {
865
866
  const txBlock = this.builder.createTxBlock();
866
- const sender = walletAddress || this.walletAddress;
867
+ const sender = walletAddress ?? this.walletAddress;
867
868
  txBlock.setSender(sender);
868
869
 
869
870
  const rewardCoins = await txBlock.claimQuick(
@@ -899,7 +900,7 @@ export class ScallopClient {
899
900
  walletAddress?: string
900
901
  ): Promise<ScallopClientFnReturnType<S>> {
901
902
  const txBlock = this.builder.createTxBlock();
902
- const sender = walletAddress || this.walletAddress;
903
+ const sender = walletAddress ?? this.walletAddress;
903
904
  txBlock.setSender(sender);
904
905
 
905
906
  await txBlock.stakeObligationWithVeScaQuick(obligationId, obligationKeyId);
@@ -929,7 +930,7 @@ export class ScallopClient {
929
930
  walletAddress?: string
930
931
  ): Promise<ScallopClientFnReturnType<S>> {
931
932
  const txBlock = this.builder.createTxBlock();
932
- const sender = walletAddress || this.walletAddress;
933
+ const sender = walletAddress ?? this.walletAddress;
933
934
  txBlock.setSender(sender);
934
935
 
935
936
  await txBlock.unstakeObligationQuick(obligationId, obligationKeyId);
@@ -961,7 +962,7 @@ export class ScallopClient {
961
962
  walletAddress?: string
962
963
  ): Promise<ScallopClientFnReturnType<S>> {
963
964
  const txBlock = this.builder.createTxBlock();
964
- const sender = walletAddress || this.walletAddress;
965
+ const sender = walletAddress ?? this.walletAddress;
965
966
  txBlock.setSender(sender);
966
967
 
967
968
  const rewardCoins = [];
@@ -1186,7 +1187,7 @@ export class ScallopClient {
1186
1187
  }
1187
1188
 
1188
1189
  const txBlock = this.builder.createTxBlock();
1189
- const recipient = receiveAddress || this.walletAddress;
1190
+ const recipient = receiveAddress ?? this.walletAddress;
1190
1191
  const packageId = this.address.get('core.packages.testCoin.id');
1191
1192
  const treasuryId = this.address.get(`core.coins.${assetCoinName}.treasury`);
1192
1193
  const target = `${packageId}::${assetCoinName}::mint`;
@@ -47,6 +47,9 @@ import {
47
47
  SupportBorrowIncentiveCoins,
48
48
  SupportSCoin,
49
49
  ScallopQueryInstanceParams,
50
+ MarketPool,
51
+ CoinPrices,
52
+ MarketPools,
50
53
  } from '../types';
51
54
  import { ScallopAddress } from './scallopAddress';
52
55
  import { ScallopUtils } from './scallopUtils';
@@ -64,6 +67,7 @@ import { normalizeSuiAddress } from '@mysten/sui/utils';
64
67
  import { getSupplyLimit } from 'src/queries/supplyLimit';
65
68
  import { withIndexerFallback } from 'src/utils/indexer';
66
69
  import { getIsolatedAssets, isIsolatedAsset } from 'src/queries/isolatedAsset';
70
+ import { newSuiKit } from './suiKit';
67
71
 
68
72
  /**
69
73
  * @description
@@ -93,10 +97,10 @@ export class ScallopQuery {
93
97
  ) {
94
98
  this.params = params;
95
99
  this.suiKit =
96
- instance?.suiKit ?? instance?.utils?.suiKit ?? new SuiKit(params);
100
+ instance?.suiKit ?? instance?.utils?.suiKit ?? newSuiKit(params);
97
101
 
98
102
  this.walletAddress = normalizeSuiAddress(
99
- params.walletAddress || this.suiKit.currentAddress()
103
+ params.walletAddress ?? this.suiKit.currentAddress()
100
104
  );
101
105
 
102
106
  if (instance?.utils) {
@@ -111,7 +115,7 @@ export class ScallopQuery {
111
115
  );
112
116
  this.address = new ScallopAddress(
113
117
  {
114
- id: params?.addressesId || ADDRESSES_ID,
118
+ id: params?.addressesId ?? ADDRESSES_ID,
115
119
  network: params?.networkType,
116
120
  forceInterface: params?.forceAddressesInterface,
117
121
  },
@@ -167,11 +171,10 @@ export class ScallopQuery {
167
171
  * @param address - ScallopAddress instance.
168
172
  */
169
173
  public async init(force: boolean = false, address?: ScallopAddress) {
170
- if (address && !this.address) {
171
- this.address = address;
172
- }
173
- if (force || !this.address.getAddresses()) {
174
+ if (force || !this.address.getAddresses() || !address?.getAddresses()) {
174
175
  await this.address.read();
176
+ } else {
177
+ this.address = address;
175
178
  }
176
179
 
177
180
  await this.utils.init(force, this.address);
@@ -184,8 +187,11 @@ export class ScallopQuery {
184
187
  * @param indexer - Whether to use indexer.
185
188
  * @return Market data.
186
189
  */
187
- public async queryMarket(indexer: boolean = false) {
188
- return await queryMarket(this, indexer);
190
+ public async queryMarket(
191
+ indexer: boolean = false,
192
+ args?: { coinPrices: CoinPrices }
193
+ ) {
194
+ return await queryMarket(this, indexer, args?.coinPrices);
189
195
  }
190
196
 
191
197
  /**
@@ -201,9 +207,12 @@ export class ScallopQuery {
201
207
  */
202
208
  public async getMarketPools(
203
209
  poolCoinNames?: SupportPoolCoins[],
204
- indexer: boolean = false
210
+ indexer: boolean = false,
211
+ args?: {
212
+ coinPrices?: CoinPrices;
213
+ }
205
214
  ) {
206
- return await getMarketPools(this, poolCoinNames, indexer);
215
+ return await getMarketPools(this, poolCoinNames, indexer, args?.coinPrices);
207
216
  }
208
217
 
209
218
  /**
@@ -215,9 +224,19 @@ export class ScallopQuery {
215
224
  */
216
225
  public async getMarketPool(
217
226
  poolCoinName: SupportPoolCoins,
218
- indexer: boolean = false
227
+ indexer: boolean = false,
228
+ args?: {
229
+ marketObject?: SuiObjectData | null;
230
+ coinPrice?: number;
231
+ }
219
232
  ) {
220
- return await getMarketPool(this, poolCoinName, indexer);
233
+ return await getMarketPool(
234
+ this,
235
+ poolCoinName,
236
+ indexer,
237
+ args?.marketObject,
238
+ args?.coinPrice
239
+ );
221
240
  }
222
241
 
223
242
  /**
@@ -359,9 +378,19 @@ export class ScallopQuery {
359
378
  */
360
379
  public async getSpools(
361
380
  stakeMarketCoinNames?: SupportStakeMarketCoins[],
362
- indexer: boolean = false
381
+ indexer: boolean = false,
382
+ args?: {
383
+ marketPools?: MarketPools;
384
+ coinPrices?: CoinPrices;
385
+ }
363
386
  ) {
364
- return await getSpools(this, stakeMarketCoinNames, indexer);
387
+ return await getSpools(
388
+ this,
389
+ stakeMarketCoinNames,
390
+ indexer,
391
+ args?.marketPools,
392
+ args?.coinPrices
393
+ );
365
394
  }
366
395
 
367
396
  /**
@@ -373,9 +402,16 @@ export class ScallopQuery {
373
402
  */
374
403
  public async getSpool(
375
404
  stakeMarketCoinName: SupportStakeMarketCoins,
376
- indexer: boolean = false
405
+ indexer: boolean = false,
406
+ args?: { marketPool?: MarketPool; coinPrices?: CoinPrices }
377
407
  ) {
378
- return await getSpool(this, stakeMarketCoinName, indexer);
408
+ return await getSpool(
409
+ this,
410
+ stakeMarketCoinName,
411
+ indexer,
412
+ args?.marketPool,
413
+ args?.coinPrices
414
+ );
379
415
  }
380
416
 
381
417
  /**
@@ -496,9 +532,15 @@ export class ScallopQuery {
496
532
  */
497
533
  public async getBorrowIncentivePools(
498
534
  coinNames?: SupportBorrowIncentiveCoins[],
499
- indexer: boolean = false
535
+ indexer: boolean = false,
536
+ args?: { coinPrices: CoinPrices }
500
537
  ) {
501
- return await getBorrowIncentivePools(this, coinNames, indexer);
538
+ return await getBorrowIncentivePools(
539
+ this,
540
+ coinNames,
541
+ indexer,
542
+ args?.coinPrices
543
+ );
502
544
  }
503
545
 
504
546
  /**
@@ -20,8 +20,9 @@ import {
20
20
  suiBridgeCoins,
21
21
  COIN_GECKGO_IDS,
22
22
  POOL_ADDRESSES,
23
+ sCoinTypeToName,
23
24
  } from '../constants';
24
- import { getPythPrice, queryObligation } from '../queries';
25
+ import { getPythPrices, queryObligation } from '../queries';
25
26
  import {
26
27
  parseDataFromPythPriceFeed,
27
28
  isMarketCoin,
@@ -41,7 +42,6 @@ import type {
41
42
  SupportStakeMarketCoins,
42
43
  SupportBorrowIncentiveCoins,
43
44
  CoinPrices,
44
- PriceMap,
45
45
  CoinWrappedType,
46
46
  SupportSCoin,
47
47
  ScallopUtilsInstanceParams,
@@ -51,6 +51,7 @@ import type {
51
51
  } from '../types';
52
52
  import { queryKeys } from 'src/constants';
53
53
  import type { SuiObjectArg, SuiTxArg, SuiTxBlock } from '@scallop-io/sui-kit';
54
+ import { newSuiKit } from './suiKit';
54
55
 
55
56
  /**
56
57
  * @description
@@ -72,7 +73,6 @@ export class ScallopUtils {
72
73
  public address: ScallopAddress;
73
74
  public cache: ScallopCache;
74
75
  public walletAddress: string;
75
- private _priceMap: PriceMap = new Map();
76
76
 
77
77
  public constructor(
78
78
  params: ScallopUtilsParams,
@@ -83,9 +83,7 @@ export class ScallopUtils {
83
83
  ...params,
84
84
  };
85
85
  this.suiKit =
86
- instance?.suiKit ??
87
- instance?.address?.cache._suiKit ??
88
- new SuiKit(params);
86
+ instance?.suiKit ?? instance?.address?.cache._suiKit ?? newSuiKit(params);
89
87
 
90
88
  this.walletAddress = params.walletAddress ?? this.suiKit.currentAddress();
91
89
 
@@ -103,7 +101,7 @@ export class ScallopUtils {
103
101
  instance?.address ??
104
102
  new ScallopAddress(
105
103
  {
106
- id: params?.addressesId || ADDRESSES_ID,
104
+ id: params?.addressesId ?? ADDRESSES_ID,
107
105
  network: params?.networkType,
108
106
  forceInterface: params?.forceAddressesInterface,
109
107
  },
@@ -132,9 +130,10 @@ export class ScallopUtils {
132
130
  * @param address - ScallopAddress instance.
133
131
  */
134
132
  public async init(force: boolean = false, address?: ScallopAddress) {
135
- if (address && !this.address) this.address = address;
136
- if (force || !this.address.getAddresses()) {
133
+ if (force || !this.address.getAddresses() || !address?.getAddresses()) {
137
134
  await this.address.read();
135
+ } else {
136
+ this.address = address;
138
137
  }
139
138
  }
140
139
 
@@ -226,6 +225,7 @@ export class ScallopUtils {
226
225
  return undefined;
227
226
  }
228
227
  }
228
+
229
229
  /**
230
230
  * Convert sCoin name into sCoin type
231
231
  * @param sCoinName
@@ -235,6 +235,15 @@ export class ScallopUtils {
235
235
  return sCoinIds[sCoinName];
236
236
  }
237
237
 
238
+ /**
239
+ * Convert sCoinType into sCoin name
240
+ * @param sCoinType
241
+ * @returns sCoin name
242
+ */
243
+ public parseSCoinNameFromType(sCoinType: string) {
244
+ return sCoinTypeToName[sCoinType];
245
+ }
246
+
238
247
  /**
239
248
  * Convert sCoin name into its underlying coin type
240
249
  * @param sCoinName
@@ -263,7 +272,7 @@ export class ScallopUtils {
263
272
  */
264
273
  public parseMarketCoinType(coinName: SupportCoins) {
265
274
  const protocolObjectId =
266
- this.address.get('core.object') || PROTOCOL_OBJECT_ID;
275
+ this.address.get('core.object') ?? PROTOCOL_OBJECT_ID;
267
276
  const coinType = this.parseCoinType(coinName);
268
277
  return `${protocolObjectId}::reserve::MarketCoin<${coinType}>`;
269
278
  }
@@ -292,7 +301,7 @@ export class ScallopUtils {
292
301
  const coinTypeRegex = new RegExp(`((0x[^:]+::[^:]+::[^<>]+))(?![^<>]*<)`);
293
302
  const coinTypeMatch = coinType.match(coinTypeRegex);
294
303
  const isMarketCoinType = coinType.includes('reserve::MarketCoin');
295
- coinType = coinTypeMatch?.[1] || coinType;
304
+ coinType = coinTypeMatch?.[1] ?? coinType;
296
305
 
297
306
  const wormHoleCoinTypeMap: Record<string, SupportAssetCoins> = {
298
307
  [`${
@@ -430,7 +439,7 @@ export class ScallopUtils {
430
439
  coinType: string = SUI_TYPE_ARG,
431
440
  ownerAddress?: string
432
441
  ) {
433
- ownerAddress = ownerAddress || this.suiKit.currentAddress();
442
+ ownerAddress = ownerAddress ?? this.suiKit.currentAddress();
434
443
  const coins = await this.suiKit.suiInteractor.selectCoins(
435
444
  ownerAddress,
436
445
  amount,
@@ -510,112 +519,68 @@ export class ScallopUtils {
510
519
  * @return Asset coin price.
511
520
  */
512
521
  public async getCoinPrices(
513
- assetCoinNames: SupportAssetCoins[] = [
522
+ _: SupportAssetCoins[] = [
514
523
  ...new Set([...SUPPORT_POOLS, ...SUPPORT_COLLATERALS]),
515
524
  ] as SupportAssetCoins[]
516
525
  ) {
517
- const coinPrices: CoinPrices = {};
518
- const existPricesCoinNames: SupportAssetCoins[] = [];
519
- const lackPricesCoinNames: SupportAssetCoins[] = [];
520
-
521
- assetCoinNames.forEach((assetCoinName) => {
522
- if (
523
- this._priceMap.has(assetCoinName) &&
524
- Date.now() - this._priceMap.get(assetCoinName)!.publishTime < 1000 * 60
525
- ) {
526
- existPricesCoinNames.push(assetCoinName);
527
- } else {
528
- lackPricesCoinNames.push(assetCoinName);
529
- this.cache.queryClient.invalidateQueries({
530
- queryKey: queryKeys.oracle.getPythLatestPriceFeed(
531
- this.address.get(`core.coins.${assetCoinName}.oracle.pyth.feed`)
532
- ),
533
- });
534
- }
535
- });
536
-
537
- if (existPricesCoinNames.length > 0) {
538
- for (const coinName of existPricesCoinNames) {
539
- coinPrices[coinName] = this._priceMap.get(coinName)!.price;
540
- }
541
- }
526
+ let coinPrices: CoinPrices = {};
527
+
528
+ const endpoints =
529
+ this.params.pythEndpoints ??
530
+ PYTH_ENDPOINTS[this.isTestnet ? 'testnet' : 'mainnet'];
531
+
532
+ const failedRequests: Set<SupportAssetCoins> = new Set([
533
+ ...SUPPORT_POOLS,
534
+ ...SUPPORT_COLLATERALS,
535
+ ]);
536
+
537
+ for (const endpoint of endpoints) {
538
+ const priceIdPairs = Array.from(failedRequests.values()).reduce(
539
+ (acc, coinName) => {
540
+ const priceId = this.address.get(
541
+ `core.coins.${coinName}.oracle.pyth.feed`
542
+ );
543
+ acc.push([coinName, priceId]);
544
+ return acc;
545
+ },
546
+ [] as [string, string][]
547
+ );
542
548
 
543
- if (lackPricesCoinNames.length > 0) {
544
- const endpoints =
545
- this.params.pythEndpoints ??
546
- PYTH_ENDPOINTS[this.isTestnet ? 'testnet' : 'mainnet'];
549
+ const priceIds = priceIdPairs.map(([_, priceId]) => priceId);
547
550
 
548
- const failedRequests: Set<SupportAssetCoins> = new Set(
549
- lackPricesCoinNames
550
- );
551
+ const pythConnection = new SuiPriceServiceConnection(endpoint, {
552
+ timeout: 2000,
553
+ });
551
554
 
552
- for (const endpoint of endpoints) {
553
- const priceIds = Array.from(failedRequests.values()).reduce(
554
- (acc, coinName) => {
555
- const priceId = this.address.get(
556
- `core.coins.${coinName}.oracle.pyth.feed`
557
- );
558
- if (priceId) {
559
- acc[coinName] = priceId;
560
- }
561
- return acc;
555
+ try {
556
+ const feeds = await this.cache.queryClient.fetchQuery({
557
+ queryKey: queryKeys.oracle.getPythLatestPriceFeeds(),
558
+ queryFn: async () => {
559
+ return await pythConnection.getLatestPriceFeeds(priceIds);
562
560
  },
563
- {} as Record<SupportAssetCoins, string>
564
- );
565
-
566
- await Promise.allSettled(
567
- Object.entries(priceIds)
568
- .filter(([_, priceId]) => !!priceId)
569
- .map(async ([coinName, priceId]) => {
570
- // console.log({ coinName, priceId, endpoint });
571
- const pythConnection = new SuiPriceServiceConnection(endpoint);
572
- try {
573
- const feed = await this.cache.queryClient.fetchQuery({
574
- queryKey: queryKeys.oracle.getPythLatestPriceFeed(
575
- priceId,
576
- endpoint
577
- ),
578
- queryFn: async () => {
579
- return (
580
- (await pythConnection.getLatestPriceFeeds([priceId])) ??
581
- []
582
- );
583
- },
584
- });
585
- if (feed[0]) {
586
- const data = parseDataFromPythPriceFeed(
587
- feed[0],
588
- this.address
589
- );
590
- this._priceMap.set(coinName as SupportAssetCoins, {
591
- price: data.price,
592
- publishTime: data.publishTime,
593
- });
594
- coinPrices[coinName as SupportAssetCoins] = data.price;
595
- failedRequests.delete(coinName as SupportAssetCoins); // remove success price feed to prevent duplicate request on the next endpoint
596
- }
597
- } catch (e) {
598
- console.warn(
599
- `Failed to get price ${coinName} feeds with endpoint ${endpoint}: ${e}`
600
- );
601
- }
602
- })
603
- );
604
- if (failedRequests.size === 0) break;
561
+ staleTime: 30000,
562
+ gcTime: 30000,
563
+ });
564
+ if (feeds) {
565
+ feeds.forEach((feed, idx) => {
566
+ const coinName = priceIdPairs[idx][0] as SupportAssetCoins;
567
+ const data = parseDataFromPythPriceFeed(feed, this.address);
568
+ coinPrices[coinName as SupportAssetCoins] = data.price;
569
+ failedRequests.delete(coinName as SupportAssetCoins); // remove success price feed to prevent duplicate request on the next endpoint
570
+ });
571
+ }
572
+ } catch (e: any) {
573
+ console.error(e.message);
605
574
  }
575
+ if (failedRequests.size === 0) break;
576
+ }
606
577
 
607
- if (failedRequests.size > 0) {
608
- await Promise.allSettled(
609
- Array.from(failedRequests.values()).map(async (coinName) => {
610
- const price = await getPythPrice(this, coinName);
611
- this._priceMap.set(coinName, {
612
- price: price,
613
- publishTime: Date.now(),
614
- });
615
- coinPrices[coinName] = price;
616
- })
617
- );
618
- }
578
+ if (failedRequests.size > 0) {
579
+ coinPrices = {
580
+ ...coinPrices,
581
+ ...(await getPythPrices(this, Array.from(failedRequests.values()))),
582
+ };
583
+ failedRequests.clear();
619
584
  }
620
585
 
621
586
  return coinPrices;
@@ -0,0 +1,11 @@
1
+ import { SuiKit, SuiKitParams } from '@scallop-io/sui-kit';
2
+ import { RPC_PROVIDERS } from 'src/constants/rpc';
3
+
4
+ export const newSuiKit = (params: SuiKitParams) => {
5
+ return new SuiKit({
6
+ ...params,
7
+ fullnodeUrls: Array.from(
8
+ new Set([...(params.fullnodeUrls ?? []), ...RPC_PROVIDERS])
9
+ ),
10
+ });
11
+ };