@scallop-io/sui-scallop-sdk 0.44.28 → 0.46.0

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 (60) hide show
  1. package/dist/builders/borrowIncentiveBuilder.d.ts +1 -1
  2. package/dist/builders/referralBuilder.d.ts +12 -0
  3. package/dist/constants/cache.d.ts +8 -0
  4. package/dist/constants/common.d.ts +1 -1
  5. package/dist/index.js +890 -419
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +967 -493
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/models/index.d.ts +1 -0
  10. package/dist/models/scallop.d.ts +7 -1
  11. package/dist/models/scallopAddress.d.ts +4 -2
  12. package/dist/models/scallopBuilder.d.ts +2 -0
  13. package/dist/models/scallopCache.d.ts +75 -0
  14. package/dist/models/scallopClient.d.ts +2 -0
  15. package/dist/models/scallopIndexer.d.ts +5 -3
  16. package/dist/models/scallopQuery.d.ts +28 -28
  17. package/dist/models/scallopUtils.d.ts +1 -0
  18. package/dist/queries/coreQuery.d.ts +3 -29
  19. package/dist/queries/index.d.ts +1 -0
  20. package/dist/queries/priceQuery.d.ts +3 -1
  21. package/dist/queries/referralQuery.d.ts +7 -0
  22. package/dist/queries/vescaQuery.d.ts +6 -2
  23. package/dist/types/address.d.ts +15 -0
  24. package/dist/types/builder/core.d.ts +2 -0
  25. package/dist/types/builder/index.d.ts +2 -1
  26. package/dist/types/builder/referral.d.ts +30 -0
  27. package/dist/types/builder/vesca.d.ts +1 -0
  28. package/dist/types/model.d.ts +2 -0
  29. package/package.json +8 -6
  30. package/src/builders/borrowIncentiveBuilder.ts +12 -21
  31. package/src/builders/coreBuilder.ts +54 -0
  32. package/src/builders/index.ts +5 -2
  33. package/src/builders/referralBuilder.ts +178 -0
  34. package/src/builders/vescaBuilder.ts +8 -6
  35. package/src/constants/cache.ts +15 -0
  36. package/src/constants/common.ts +9 -2
  37. package/src/constants/vesca.ts +1 -3
  38. package/src/models/index.ts +1 -0
  39. package/src/models/scallop.ts +26 -7
  40. package/src/models/scallopAddress.ts +87 -38
  41. package/src/models/scallopBuilder.ts +14 -4
  42. package/src/models/scallopCache.ts +285 -0
  43. package/src/models/scallopClient.ts +15 -4
  44. package/src/models/scallopIndexer.ts +58 -84
  45. package/src/models/scallopQuery.ts +54 -5
  46. package/src/models/scallopUtils.ts +66 -37
  47. package/src/queries/borrowIncentiveQuery.ts +6 -12
  48. package/src/queries/coreQuery.ts +83 -260
  49. package/src/queries/index.ts +1 -0
  50. package/src/queries/priceQuery.ts +48 -9
  51. package/src/queries/referralQuery.ts +27 -0
  52. package/src/queries/spoolQuery.ts +20 -27
  53. package/src/queries/vescaQuery.ts +95 -17
  54. package/src/types/address.ts +15 -0
  55. package/src/types/builder/core.ts +14 -0
  56. package/src/types/builder/index.ts +2 -0
  57. package/src/types/builder/referral.ts +51 -0
  58. package/src/types/builder/vesca.ts +1 -0
  59. package/src/types/model.ts +2 -0
  60. package/src/types/query/borrowIncentive.ts +0 -107
@@ -1,6 +1,4 @@
1
1
  import { normalizeStructTag } from '@mysten/sui.js/utils';
2
- import { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
3
- import BigNumber from 'bignumber.js';
4
2
  import {
5
3
  SUPPORT_POOLS,
6
4
  PROTOCOL_OBJECT_ID,
@@ -33,10 +31,10 @@ import {
33
31
  BalanceSheet,
34
32
  RiskModel,
35
33
  CollateralStat,
36
- CoinAmounts,
37
- MarketCoinAmounts,
38
34
  SupportMarketCoins,
35
+ OptionalKeys,
39
36
  } from '../types';
37
+ import BigNumber from 'bignumber.js';
40
38
 
41
39
  /**
42
40
  * Query market data.
@@ -54,10 +52,15 @@ export const queryMarket = async (
54
52
  ) => {
55
53
  const packageId = query.address.get('core.packages.query.id');
56
54
  const marketId = query.address.get('core.market');
57
- const txBlock = new SuiKitTxBlock();
58
55
  const queryTarget = `${packageId}::market_query::market_data`;
59
- txBlock.moveCall(queryTarget, [marketId]);
60
- const queryResult = await query.suiKit.inspectTxn(txBlock);
56
+ const args = [marketId];
57
+
58
+ // const txBlock = new SuiKitTxBlock();
59
+ // txBlock.moveCall(queryTarget, args);
60
+ const queryResult = await query.cache.queryInspectTxn(
61
+ { queryTarget, args }
62
+ // txBlock
63
+ );
61
64
  const marketData = queryResult.events[0].parsedJson as MarketQueryInterface;
62
65
  const coinPrices = await query.utils.getCoinPrices();
63
66
 
@@ -211,11 +214,8 @@ export const getMarketPools = async (
211
214
  ) => {
212
215
  poolCoinNames = poolCoinNames || [...SUPPORT_POOLS];
213
216
  const marketId = query.address.get('core.market');
214
- const marketObjectResponse = await query.suiKit.client().getObject({
215
- id: marketId,
216
- options: {
217
- showContent: true,
218
- },
217
+ const marketObjectResponse = await query.cache.queryGetObject(marketId, {
218
+ showContent: true,
219
219
  });
220
220
  const coinPrices = await query.utils.getCoinPrices(poolCoinNames ?? []);
221
221
 
@@ -274,11 +274,8 @@ export const getMarketPool = async (
274
274
  marketObject =
275
275
  marketObject ||
276
276
  (
277
- await query.suiKit.client().getObject({
278
- id: marketId,
279
- options: {
280
- showContent: true,
281
- },
277
+ await query.cache.queryGetObject(marketId, {
278
+ showContent: true,
282
279
  })
283
280
  ).data;
284
281
 
@@ -310,9 +307,8 @@ export const getMarketPool = async (
310
307
  // Get balance sheet.
311
308
  const balanceSheetParentId =
312
309
  fields.vault.fields.balance_sheets.fields.table.fields.id.id;
313
- const balanceSheetDynamicFieldObjectResponse = await query.suiKit
314
- .client()
315
- .getDynamicFieldObject({
310
+ const balanceSheetDynamicFieldObjectResponse =
311
+ await query.cache.queryGetDynamicFieldObject({
316
312
  parentId: balanceSheetParentId,
317
313
  name: {
318
314
  type: '0x1::type_name::TypeName',
@@ -336,9 +332,8 @@ export const getMarketPool = async (
336
332
  // Get borrow index.
337
333
  const borrowIndexParentId =
338
334
  fields.borrow_dynamics.fields.table.fields.id.id;
339
- const borrowIndexDynamicFieldObjectResponse = await query.suiKit
340
- .client()
341
- .getDynamicFieldObject({
335
+ const borrowIndexDynamicFieldObjectResponse =
336
+ await query.cache.queryGetDynamicFieldObject({
342
337
  parentId: borrowIndexParentId,
343
338
  name: {
344
339
  type: '0x1::type_name::TypeName',
@@ -362,9 +357,8 @@ export const getMarketPool = async (
362
357
  // Get interest models.
363
358
  const interestModelParentId =
364
359
  fields.interest_models.fields.table.fields.id.id;
365
- const interestModelDynamicFieldObjectResponse = await query.suiKit
366
- .client()
367
- .getDynamicFieldObject({
360
+ const interestModelDynamicFieldObjectResponse =
361
+ await query.cache.queryGetDynamicFieldObject({
368
362
  parentId: interestModelParentId,
369
363
  name: {
370
364
  type: '0x1::type_name::TypeName',
@@ -386,9 +380,8 @@ export const getMarketPool = async (
386
380
  }
387
381
 
388
382
  // Get borrow fee.
389
- const borrowFeeDynamicFieldObjectResponse = await query.suiKit
390
- .client()
391
- .getDynamicFieldObject({
383
+ const borrowFeeDynamicFieldObjectResponse =
384
+ await query.cache.queryGetDynamicFieldObject({
392
385
  parentId: marketId,
393
386
  name: {
394
387
  type: `${BORROW_FEE_PROTOCOL_ID}::market_dynamic_keys::BorrowFeeKey`,
@@ -482,11 +475,8 @@ export const getMarketCollaterals = async (
482
475
  ) => {
483
476
  collateralCoinNames = collateralCoinNames || [...SUPPORT_COLLATERALS];
484
477
  const marketId = query.address.get('core.market');
485
- const marketObjectResponse = await query.suiKit.client().getObject({
486
- id: marketId,
487
- options: {
488
- showContent: true,
489
- },
478
+ const marketObjectResponse = await query.cache.queryGetObject(marketId, {
479
+ showContent: true,
490
480
  });
491
481
  const coinPrices = await query.utils.getCoinPrices(collateralCoinNames ?? []);
492
482
 
@@ -544,11 +534,8 @@ export const getMarketCollateral = async (
544
534
  marketObject =
545
535
  marketObject ||
546
536
  (
547
- await query.suiKit.client().getObject({
548
- id: marketId,
549
- options: {
550
- showContent: true,
551
- },
537
+ await query.cache.queryGetObject(marketId, {
538
+ showContent: true,
552
539
  })
553
540
  ).data;
554
541
 
@@ -581,9 +568,8 @@ export const getMarketCollateral = async (
581
568
 
582
569
  // Get risk model.
583
570
  const riskModelParentId = fields.risk_models.fields.table.fields.id.id;
584
- const riskModelDynamicFieldObjectResponse = await query.suiKit
585
- .client()
586
- .getDynamicFieldObject({
571
+ const riskModelDynamicFieldObjectResponse =
572
+ await query.cache.queryGetDynamicFieldObject({
587
573
  parentId: riskModelParentId,
588
574
  name: {
589
575
  type: '0x1::type_name::TypeName',
@@ -606,9 +592,8 @@ export const getMarketCollateral = async (
606
592
  // Get collateral stat.
607
593
  const collateralStatParentId =
608
594
  fields.collateral_stats.fields.table.fields.id.id;
609
- const collateralStatDynamicFieldObjectResponse = await query.suiKit
610
- .client()
611
- .getDynamicFieldObject({
595
+ const collateralStatDynamicFieldObjectResponse =
596
+ await query.cache.queryGetDynamicFieldObject({
612
597
  parentId: collateralStatParentId,
613
598
  name: {
614
599
  type: '0x1::type_name::TypeName',
@@ -687,15 +672,14 @@ export const getObligations = async (
687
672
  let hasNextPage = false;
688
673
  let nextCursor: string | null | undefined = null;
689
674
  do {
690
- const paginatedKeyObjectsResponse = await query.suiKit
691
- .client()
692
- .getOwnedObjects({
693
- owner,
694
- filter: {
695
- StructType: `${protocolObjectId}::obligation::ObligationKey`,
696
- },
697
- cursor: nextCursor,
698
- });
675
+ const paginatedKeyObjectsResponse = await query.cache.queryGetOwnedObjects({
676
+ owner,
677
+ filter: {
678
+ StructType: `${protocolObjectId}::obligation::ObligationKey`,
679
+ },
680
+ cursor: nextCursor,
681
+ });
682
+
699
683
  keyObjectsResponse.push(...paginatedKeyObjectsResponse.data);
700
684
  if (
701
685
  paginatedKeyObjectsResponse.hasNextPage &&
@@ -711,7 +695,8 @@ export const getObligations = async (
711
695
  const keyObjectIds: string[] = keyObjectsResponse
712
696
  .map((ref: any) => ref?.data?.objectId)
713
697
  .filter((id: any) => id !== undefined);
714
- const keyObjects = await query.suiKit.getObjects(keyObjectIds);
698
+ const keyObjects = await query.cache.queryGetObjects(keyObjectIds);
699
+
715
700
  const obligations: Obligation[] = [];
716
701
  for (const keyObject of keyObjects) {
717
702
  const keyId = keyObject.objectId;
@@ -736,12 +721,10 @@ export const getObligationLocked = async (
736
721
  query: ScallopQuery,
737
722
  obligationId: string
738
723
  ) => {
739
- const obligationObjectResponse = await query.suiKit.client().getObject({
740
- id: obligationId,
741
- options: {
742
- showContent: true,
743
- },
744
- });
724
+ const obligationObjectResponse = await query.cache.queryGetObject(
725
+ obligationId,
726
+ { showContent: true }
727
+ );
745
728
  let obligationLocked = false;
746
729
  if (
747
730
  obligationObjectResponse.data &&
@@ -772,9 +755,14 @@ export const queryObligation = async (
772
755
  ) => {
773
756
  const packageId = query.address.get('core.packages.query.id');
774
757
  const queryTarget = `${packageId}::obligation_query::obligation_data`;
775
- const txBlock = new SuiKitTxBlock();
776
- txBlock.moveCall(queryTarget, [obligationId]);
777
- const queryResult = await query.suiKit.inspectTxn(txBlock);
758
+ const args = [obligationId];
759
+
760
+ // const txBlock = new SuiKitTxBlock();
761
+ // txBlock.moveCall(queryTarget, args);
762
+ const queryResult = await query.cache.queryInspectTxn(
763
+ { queryTarget, args }
764
+ // txBlock
765
+ );
778
766
  return queryResult.events[0].parsedJson as ObligationQueryInterface;
779
767
  };
780
768
 
@@ -793,61 +781,16 @@ export const getCoinAmounts = async (
793
781
  ) => {
794
782
  assetCoinNames = assetCoinNames || [...SUPPORT_POOLS];
795
783
  const owner = ownerAddress || query.suiKit.currentAddress();
796
- const coinObjectsResponse: SuiObjectResponse[] = [];
797
- let hasNextPage = false;
798
- let nextCursor: string | null | undefined = null;
799
- do {
800
- const paginatedCoinObjectsResponse = await query.suiKit
801
- .client()
802
- .getOwnedObjects({
803
- owner,
804
- filter: {
805
- MatchAny: assetCoinNames.map((assetCoinName) => {
806
- const coinType = query.utils.parseCoinType(assetCoinName);
807
- return { StructType: `0x2::coin::Coin<${coinType}>` };
808
- }),
809
- },
810
- options: {
811
- showType: true,
812
- showContent: true,
813
- },
814
- cursor: nextCursor,
815
- });
816
-
817
- coinObjectsResponse.push(...paginatedCoinObjectsResponse.data);
818
- if (
819
- paginatedCoinObjectsResponse.hasNextPage &&
820
- paginatedCoinObjectsResponse.nextCursor
821
- ) {
822
- hasNextPage = true;
823
- nextCursor = paginatedCoinObjectsResponse.nextCursor;
824
- } else {
825
- hasNextPage = false;
826
- }
827
- } while (hasNextPage);
784
+ const assetCoins = {} as OptionalKeys<Record<SupportAssetCoins, number>>;
828
785
 
829
- const coinAmounts: CoinAmounts = {};
830
- const coinObjects = coinObjectsResponse
831
- .map((response) => {
832
- return response.data;
786
+ await Promise.allSettled(
787
+ assetCoinNames.map(async (assetCoinName) => {
788
+ const marketCoin = await getCoinAmount(query, assetCoinName, owner);
789
+ assetCoins[assetCoinName] = marketCoin;
833
790
  })
834
- .filter(
835
- (object: any) => object !== undefined && object !== null
836
- ) as SuiObjectData[];
837
- for (const coinObject of coinObjects) {
838
- const type = coinObject.type as string;
839
- if (coinObject.content && 'fields' in coinObject.content) {
840
- const fields = coinObject.content.fields as any;
841
- const poolCoinName =
842
- query.utils.parseCoinNameFromType<SupportPoolCoins>(type);
843
- if (poolCoinName) {
844
- coinAmounts[poolCoinName] = BigNumber(coinAmounts[poolCoinName] ?? 0)
845
- .plus(fields.balance)
846
- .toNumber();
847
- }
848
- }
849
- }
850
- return coinAmounts;
791
+ );
792
+
793
+ return assetCoins;
851
794
  };
852
795
 
853
796
  /**
@@ -865,48 +808,11 @@ export const getCoinAmount = async (
865
808
  ) => {
866
809
  const owner = ownerAddress || query.suiKit.currentAddress();
867
810
  const coinType = query.utils.parseCoinType(assetCoinName);
868
- const coinObjectsResponse: SuiObjectResponse[] = [];
869
- let hasNextPage = false;
870
- let nextCursor: string | null | undefined = null;
871
- do {
872
- const paginatedCoinObjectsResponse = await query.suiKit
873
- .client()
874
- .getOwnedObjects({
875
- owner,
876
- filter: { StructType: `0x2::coin::Coin<${coinType}>` },
877
- options: {
878
- showContent: true,
879
- },
880
- cursor: nextCursor,
881
- });
882
-
883
- coinObjectsResponse.push(...paginatedCoinObjectsResponse.data);
884
- if (
885
- paginatedCoinObjectsResponse.hasNextPage &&
886
- paginatedCoinObjectsResponse.nextCursor
887
- ) {
888
- hasNextPage = true;
889
- nextCursor = paginatedCoinObjectsResponse.nextCursor;
890
- } else {
891
- hasNextPage = false;
892
- }
893
- } while (hasNextPage);
894
-
895
- let coinAmount: number = 0;
896
- const coinObjects = coinObjectsResponse
897
- .map((response) => {
898
- return response.data;
899
- })
900
- .filter(
901
- (object: any) => object !== undefined && object !== null
902
- ) as SuiObjectData[];
903
- for (const coinObject of coinObjects) {
904
- if (coinObject.content && 'fields' in coinObject.content) {
905
- const fields = coinObject.content.fields as any;
906
- coinAmount = BigNumber(coinAmount).plus(fields.balance).toNumber();
907
- }
908
- }
909
- return coinAmount;
811
+ const amount = await query.cache.queryGetCoinBalance({
812
+ owner,
813
+ coinType: coinType,
814
+ });
815
+ return BigNumber(amount).toNumber();
910
816
  };
911
817
 
912
818
  /**
@@ -928,64 +834,20 @@ export const getMarketCoinAmounts = async (
928
834
  query.utils.parseMarketCoinName(poolCoinName)
929
835
  );
930
836
  const owner = ownerAddress || query.suiKit.currentAddress();
931
- const marketCoinObjectsResponse: SuiObjectResponse[] = [];
932
- let hasNextPage = false;
933
- let nextCursor: string | null | undefined = null;
934
- do {
935
- const paginatedMarketCoinObjectsResponse = await query.suiKit
936
- .client()
937
- .getOwnedObjects({
938
- owner,
939
- filter: {
940
- MatchAny: marketCoinNames.map((marketCoinName) => {
941
- const marketCoinType =
942
- query.utils.parseMarketCoinType(marketCoinName);
943
- return { StructType: `0x2::coin::Coin<${marketCoinType}>` };
944
- }),
945
- },
946
- options: {
947
- showType: true,
948
- showContent: true,
949
- },
950
- cursor: nextCursor,
951
- });
952
-
953
- marketCoinObjectsResponse.push(...paginatedMarketCoinObjectsResponse.data);
954
- if (
955
- paginatedMarketCoinObjectsResponse.hasNextPage &&
956
- paginatedMarketCoinObjectsResponse.nextCursor
957
- ) {
958
- hasNextPage = true;
959
- nextCursor = paginatedMarketCoinObjectsResponse.nextCursor;
960
- } else {
961
- hasNextPage = false;
962
- }
963
- } while (hasNextPage);
964
-
965
- const marketCoinAmounts: MarketCoinAmounts = {};
966
- const marketCoinObjects = marketCoinObjectsResponse
967
- .map((response) => {
968
- return response.data;
837
+ const marketCoins = {} as OptionalKeys<Record<SupportMarketCoins, number>>;
838
+
839
+ Promise.allSettled(
840
+ marketCoinNames.map(async (marketCoinName) => {
841
+ const marketCoin = await getMarketCoinAmount(
842
+ query,
843
+ marketCoinName,
844
+ owner
845
+ );
846
+ marketCoins[marketCoinName] = marketCoin;
969
847
  })
970
- .filter(
971
- (object: any) => object !== undefined && object !== null
972
- ) as SuiObjectData[];
973
- for (const marketCoinObject of marketCoinObjects) {
974
- const marketCoinType = marketCoinObject.type as string;
975
- if (marketCoinObject.content && 'fields' in marketCoinObject.content) {
976
- const fields = marketCoinObject.content.fields as any;
977
- const marketCoinName =
978
- query.utils.parseCoinNameFromType<SupportMarketCoins>(marketCoinType);
979
- if (marketCoinName) {
980
- marketCoinAmounts[marketCoinName] = BigNumber(
981
- marketCoinAmounts[marketCoinName] ?? 0
982
- )
983
- .plus(fields.balance)
984
- .toNumber();
985
- }
986
- }
987
- }
988
- return marketCoinAmounts;
848
+ );
849
+
850
+ return marketCoins;
989
851
  };
990
852
 
991
853
  /**
@@ -1003,48 +865,9 @@ export const getMarketCoinAmount = async (
1003
865
  ) => {
1004
866
  const owner = ownerAddress || query.suiKit.currentAddress();
1005
867
  const marketCoinType = query.utils.parseMarketCoinType(marketCoinName);
1006
- const marketCoinObjectsResponse: SuiObjectResponse[] = [];
1007
- let hasNextPage = false;
1008
- let nextCursor: string | null | undefined = null;
1009
- do {
1010
- const paginatedMarketCoinObjectsResponse = await query.suiKit
1011
- .client()
1012
- .getOwnedObjects({
1013
- owner,
1014
- filter: { StructType: `0x2::coin::Coin<${marketCoinType}>` },
1015
- options: {
1016
- showContent: true,
1017
- },
1018
- cursor: nextCursor,
1019
- });
1020
-
1021
- marketCoinObjectsResponse.push(...paginatedMarketCoinObjectsResponse.data);
1022
- if (
1023
- paginatedMarketCoinObjectsResponse.hasNextPage &&
1024
- paginatedMarketCoinObjectsResponse.nextCursor
1025
- ) {
1026
- hasNextPage = true;
1027
- nextCursor = paginatedMarketCoinObjectsResponse.nextCursor;
1028
- } else {
1029
- hasNextPage = false;
1030
- }
1031
- } while (hasNextPage);
1032
-
1033
- let marketCoinAmount: number = 0;
1034
- const marketCoinObjects = marketCoinObjectsResponse
1035
- .map((response) => {
1036
- return response.data;
1037
- })
1038
- .filter(
1039
- (object: any) => object !== undefined && object !== null
1040
- ) as SuiObjectData[];
1041
- for (const marketCoinObject of marketCoinObjects) {
1042
- if (marketCoinObject.content && 'fields' in marketCoinObject.content) {
1043
- const fields = marketCoinObject.content.fields as any;
1044
- marketCoinAmount = BigNumber(marketCoinAmount)
1045
- .plus(fields.balance)
1046
- .toNumber();
1047
- }
1048
- }
1049
- return marketCoinAmount;
868
+ const amount = await query.cache.queryGetCoinBalance({
869
+ owner,
870
+ coinType: marketCoinType,
871
+ });
872
+ return BigNumber(amount).toNumber();
1050
873
  };
@@ -4,3 +4,4 @@ export * from './borrowIncentiveQuery';
4
4
  export * from './priceQuery';
5
5
  export * from './portfolioQuery';
6
6
  export * from './vescaQuery';
7
+ export * from './referralQuery';
@@ -1,3 +1,4 @@
1
+ import { SuiObjectData } from '@mysten/sui.js/src/client';
1
2
  import type { ScallopQuery } from '../models';
2
3
  import type { SupportAssetCoins } from '../types';
3
4
 
@@ -10,20 +11,19 @@ import type { SupportAssetCoins } from '../types';
10
11
  */
11
12
  export const getPythPrice = async (
12
13
  query: ScallopQuery,
13
- assetCoinName: SupportAssetCoins
14
+ assetCoinName: SupportAssetCoins,
15
+ priceFeedObject?: SuiObjectData | null
14
16
  ) => {
15
17
  const pythFeedObjectId = query.address.get(
16
18
  `core.coins.${assetCoinName}.oracle.pyth.feedObject`
17
19
  );
18
- const priceFeedObjectResponse = await query.suiKit.client().getObject({
19
- id: pythFeedObjectId,
20
- options: {
21
- showContent: true,
22
- },
23
- });
20
+ priceFeedObject =
21
+ priceFeedObject ||
22
+ (await query.cache.queryGetObject(pythFeedObjectId, { showContent: true }))
23
+ .data;
24
24
 
25
- if (priceFeedObjectResponse.data) {
26
- const priceFeedPoolObject = priceFeedObjectResponse.data;
25
+ if (priceFeedObject) {
26
+ const priceFeedPoolObject = priceFeedObject;
27
27
  if (
28
28
  priceFeedPoolObject.content &&
29
29
  'fields' in priceFeedPoolObject.content
@@ -56,3 +56,42 @@ export const getPythPrice = async (
56
56
 
57
57
  return 0;
58
58
  };
59
+
60
+ export const getPythPrices = async (
61
+ query: ScallopQuery,
62
+ assetCoinNames: SupportAssetCoins[]
63
+ ) => {
64
+ const seen: Record<string, boolean> = {};
65
+ const pythFeedObjectIds = assetCoinNames
66
+ .map((assetCoinName) => {
67
+ const pythFeedObjectId = query.address.get(
68
+ `core.coins.${assetCoinName}.oracle.pyth.feedObject`
69
+ );
70
+ if (seen[pythFeedObjectId]) return null;
71
+
72
+ seen[pythFeedObjectId] = true;
73
+ return pythFeedObjectId;
74
+ })
75
+ .filter((item) => !!item) as string[];
76
+ const priceFeedObjects = await query.cache.queryGetObjects(
77
+ pythFeedObjectIds,
78
+ {
79
+ showContent: true,
80
+ }
81
+ );
82
+
83
+ return (
84
+ await Promise.all(
85
+ priceFeedObjects.map(async (priceFeedObject, idx) => ({
86
+ coinName: assetCoinNames[idx],
87
+ price: await getPythPrice(query, assetCoinNames[idx], priceFeedObject),
88
+ }))
89
+ )
90
+ ).reduce(
91
+ (prev, curr) => {
92
+ prev[curr.coinName] = curr.price;
93
+ return prev;
94
+ },
95
+ {} as Record<SupportAssetCoins, number>
96
+ );
97
+ };
@@ -0,0 +1,27 @@
1
+ import { ScallopQuery } from 'src/models';
2
+
3
+ /**
4
+ * Query the veScaKeyId from the referral bindings table using the borrower address
5
+ * @param query
6
+ * @returns
7
+ */
8
+ export const queryVeScaKeyIdFromReferralBindings = async (
9
+ query: ScallopQuery,
10
+ refereeAddress: string
11
+ ): Promise<string | null> => {
12
+ const referralBindingTableId = query.address.get('referral.bindingTableId');
13
+
14
+ const referralBindResponse = await query.cache.queryGetDynamicFieldObject({
15
+ parentId: referralBindingTableId,
16
+ name: {
17
+ type: 'address',
18
+ value: refereeAddress,
19
+ },
20
+ });
21
+
22
+ if (referralBindResponse.data?.content?.dataType !== 'moveObject')
23
+ return null;
24
+
25
+ const fields = referralBindResponse.data.content.fields as any;
26
+ return fields.value;
27
+ };
@@ -133,25 +133,21 @@ export const getSpool = async (
133
133
  return spoolIndexer;
134
134
  }
135
135
 
136
- const spoolObjectResponse = await query.suiKit.client().multiGetObjects({
137
- ids: [poolId, rewardPoolId],
138
- options: {
136
+ const spoolObjectResponse = await query.cache.queryGetObjects(
137
+ [poolId, rewardPoolId],
138
+ {
139
139
  showContent: true,
140
- },
141
- });
140
+ }
141
+ );
142
142
 
143
- if (
144
- marketPool &&
145
- spoolObjectResponse[0].data &&
146
- spoolObjectResponse[1].data
147
- ) {
143
+ if (marketPool && spoolObjectResponse[0] && spoolObjectResponse[1]) {
148
144
  const rewardCoinName = query.utils.getSpoolRewardCoinName(marketCoinName);
149
145
  coinPrices =
150
146
  coinPrices ||
151
147
  (await query.utils.getCoinPrices([coinName, rewardCoinName]));
152
148
 
153
- const spoolObject = spoolObjectResponse[0].data;
154
- const rewardPoolObject = spoolObjectResponse[1].data;
149
+ const spoolObject = spoolObjectResponse[0];
150
+ const rewardPoolObject = spoolObjectResponse[1];
155
151
  if (spoolObject.content && 'fields' in spoolObject.content) {
156
152
  const spoolFields = spoolObject.content.fields as any;
157
153
  const parsedSpoolData = parseOriginSpoolData({
@@ -245,9 +241,8 @@ export const getStakeAccounts = async (
245
241
  let hasNextPage = false;
246
242
  let nextCursor: string | null | undefined = null;
247
243
  do {
248
- const paginatedStakeObjectsResponse = await query.suiKit
249
- .client()
250
- .getOwnedObjects({
244
+ const paginatedStakeObjectsResponse =
245
+ await query.cache.queryGetOwnedObjects({
251
246
  owner,
252
247
  filter: { StructType: stakeAccountType },
253
248
  options: {
@@ -298,7 +293,7 @@ export const getStakeAccounts = async (
298
293
  const stakeObjectIds: string[] = stakeObjectsResponse
299
294
  .map((ref: any) => ref?.data?.objectId)
300
295
  .filter((id: any) => id !== undefined);
301
- const stakeObjects = await query.suiKit.getObjects(stakeObjectIds);
296
+ const stakeObjects = await query.cache.queryGetObjects(stakeObjectIds);
302
297
  for (const stakeObject of stakeObjects) {
303
298
  const id = stakeObject.objectId;
304
299
  const type = stakeObject.type!;
@@ -421,12 +416,9 @@ export const getStakePool = async (
421
416
  ) => {
422
417
  const poolId = query.address.get(`spool.pools.${marketCoinName}.id`);
423
418
  let stakePool: StakePool | undefined = undefined;
424
- const stakePoolObjectResponse = await query.suiKit.client().getObject({
425
- id: poolId,
426
- options: {
427
- showContent: true,
428
- showType: true,
429
- },
419
+ const stakePoolObjectResponse = await query.cache.queryGetObject(poolId, {
420
+ showContent: true,
421
+ showType: true,
430
422
  });
431
423
  if (stakePoolObjectResponse.data) {
432
424
  const stakePoolObject = stakePoolObjectResponse.data;
@@ -482,13 +474,14 @@ export const getStakeRewardPool = async (
482
474
  `spool.pools.${marketCoinName}.rewardPoolId`
483
475
  );
484
476
  let stakeRewardPool: StakeRewardPool | undefined = undefined;
485
- const stakeRewardPoolObjectResponse = await query.suiKit.client().getObject({
486
- id: poolId,
487
- options: {
477
+ const stakeRewardPoolObjectResponse = await query.cache.queryGetObject(
478
+ poolId,
479
+ {
488
480
  showContent: true,
489
481
  showType: true,
490
- },
491
- });
482
+ }
483
+ );
484
+
492
485
  if (stakeRewardPoolObjectResponse.data) {
493
486
  const stakeRewardPoolObject = stakeRewardPoolObjectResponse.data;
494
487
  const id = stakeRewardPoolObject.objectId;