@scallop-io/sui-scallop-sdk 0.46.56 → 0.46.58-alpha.1

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/index.mjs CHANGED
@@ -265,7 +265,7 @@ import {
265
265
  var DEFAULT_CACHE_OPTIONS = {
266
266
  defaultOptions: {
267
267
  queries: {
268
- staleTime: 1500
268
+ staleTime: 2e3
269
269
  }
270
270
  }
271
271
  };
@@ -814,8 +814,8 @@ var findClosestUnlockRound = (unlockAtInSecondTimestamp) => {
814
814
  };
815
815
 
816
816
  // src/constants/tokenBucket.ts
817
- var DEFAULT_TOKENS_PER_INTERVAL = 10;
818
- var DEFAULT_INTERVAL_IN_MS = 1e3;
817
+ var DEFAULT_TOKENS_PER_INTERVAL = 50;
818
+ var DEFAULT_INTERVAL_IN_MS = 300;
819
819
 
820
820
  // src/utils/tokenBucket.ts
821
821
  var TokenBucket = class {
@@ -843,14 +843,31 @@ var TokenBucket = class {
843
843
  return false;
844
844
  }
845
845
  };
846
- var callWithRateLimit = async (tokenBucket, fn, retryDelayInMs = DEFAULT_INTERVAL_IN_MS, maxRetries = 5) => {
846
+ var callWithRateLimit = async (tokenBucket, fn, retryDelayInMs = DEFAULT_INTERVAL_IN_MS, maxRetries = 15, backoffFactor = 1.25) => {
847
847
  let retries = 0;
848
848
  const tryRequest = async () => {
849
849
  if (tokenBucket.removeTokens(1)) {
850
- return await fn();
850
+ try {
851
+ const result = await fn();
852
+ if (result && result.status === 429) {
853
+ throw new Error("Unexpected status code: 429");
854
+ }
855
+ return result;
856
+ } catch (error) {
857
+ if (error.message === "Unexpected status code: 429" && retries < maxRetries) {
858
+ retries++;
859
+ const delay = retryDelayInMs * Math.pow(backoffFactor, retries);
860
+ await new Promise((resolve) => setTimeout(resolve, delay));
861
+ return tryRequest();
862
+ } else {
863
+ console.error("An error occurred:", error);
864
+ return null;
865
+ }
866
+ }
851
867
  } else if (retries < maxRetries) {
852
868
  retries++;
853
- await new Promise((resolve) => setTimeout(resolve, retryDelayInMs));
869
+ const delay = retryDelayInMs * Math.pow(backoffFactor, retries);
870
+ await new Promise((resolve) => setTimeout(resolve, delay));
854
871
  return tryRequest();
855
872
  } else {
856
873
  console.error("Maximum retries reached");
@@ -862,10 +879,11 @@ var callWithRateLimit = async (tokenBucket, fn, retryDelayInMs = DEFAULT_INTERVA
862
879
 
863
880
  // src/models/scallopCache.ts
864
881
  var ScallopCache = class {
865
- constructor(suiKit, cacheOptions, tokenBucket) {
882
+ constructor(suiKit, walletAddress, cacheOptions, tokenBucket) {
866
883
  this.queryClient = new QueryClient(cacheOptions ?? DEFAULT_CACHE_OPTIONS);
867
884
  this._suiKit = suiKit;
868
885
  this.tokenBucket = tokenBucket ?? new TokenBucket(DEFAULT_TOKENS_PER_INTERVAL, DEFAULT_INTERVAL_IN_MS);
886
+ this.walletAddress = walletAddress ?? suiKit.currentAddress();
869
887
  }
870
888
  get suiKit() {
871
889
  if (!this._suiKit) {
@@ -955,7 +973,7 @@ var ScallopCache = class {
955
973
  * @returns Promise<SuiObjectResponse>
956
974
  */
957
975
  async queryGetObject(objectId, options) {
958
- const queryKey = ["getObject", objectId, this.suiKit.currentAddress()];
976
+ const queryKey = ["getObject", objectId, this.walletAddress];
959
977
  if (options) {
960
978
  queryKey.push(JSON.stringify(options));
961
979
  }
@@ -977,13 +995,15 @@ var ScallopCache = class {
977
995
  * @param objectIds
978
996
  * @returns Promise<SuiObjectData[]>
979
997
  */
980
- async queryGetObjects(objectIds, options) {
998
+ async queryGetObjects(objectIds, options = {
999
+ showContent: true
1000
+ }) {
981
1001
  if (objectIds.length === 0)
982
1002
  return [];
983
1003
  const queryKey = [
984
1004
  "getObjects",
985
1005
  JSON.stringify(objectIds),
986
- this.suiKit.currentAddress()
1006
+ this.walletAddress
987
1007
  ];
988
1008
  if (options) {
989
1009
  queryKey.push(JSON.stringify(options));
@@ -1856,6 +1876,7 @@ var ScallopAddress = class {
1856
1876
  const { id, auth, network } = params;
1857
1877
  this.cache = instance?.cache ?? new ScallopCache(
1858
1878
  instance?.suiKit ?? new SuiKit({}),
1879
+ void 0,
1859
1880
  DEFAULT_CACHE_OPTIONS
1860
1881
  );
1861
1882
  this._requestClient = axios.create({
@@ -2617,7 +2638,8 @@ var getObligations = async ({
2617
2638
  filter: {
2618
2639
  StructType: `${protocolObjectId}::obligation::ObligationKey`
2619
2640
  },
2620
- cursor: nextCursor
2641
+ cursor: nextCursor,
2642
+ limit: 10
2621
2643
  });
2622
2644
  if (!paginatedKeyObjectsResponse)
2623
2645
  break;
@@ -2667,7 +2689,7 @@ var queryObligation = async ({
2667
2689
  { queryTarget, args }
2668
2690
  // txBlock
2669
2691
  );
2670
- return queryResult?.events[0].parsedJson;
2692
+ return queryResult?.events[0]?.parsedJson;
2671
2693
  };
2672
2694
  var getCoinAmounts = async (query, assetCoinNames, ownerAddress) => {
2673
2695
  assetCoinNames = assetCoinNames || [...SUPPORT_POOLS];
@@ -2750,7 +2772,7 @@ var getFlashLoanFees = async (query, assetNames) => {
2750
2772
  const flashloanFeesTableId = vault.fields.flash_loan_fees.fields.table.fields.id.id;
2751
2773
  const balanceSheetDynamicFields = await query.cache.queryGetDynamicFields({
2752
2774
  parentId: flashloanFeesTableId,
2753
- limit: 50
2775
+ limit: 10
2754
2776
  });
2755
2777
  const dynamicFieldObjectIds = balanceSheetDynamicFields?.data.filter((field) => {
2756
2778
  const assetType = field.name.value.name;
@@ -2931,10 +2953,10 @@ var getStakeAccounts = async ({
2931
2953
  owner,
2932
2954
  filter: { StructType: stakeAccountType },
2933
2955
  options: {
2934
- showContent: true,
2935
- showType: true
2956
+ showContent: true
2936
2957
  },
2937
- cursor: nextCursor
2958
+ cursor: nextCursor,
2959
+ limit: 10
2938
2960
  });
2939
2961
  if (!paginatedStakeObjectsResponse)
2940
2962
  continue;
@@ -2967,7 +2989,10 @@ var getStakeAccounts = async ({
2967
2989
  {}
2968
2990
  );
2969
2991
  const stakeObjectIds = stakeObjectsResponse.map((ref) => ref?.data?.objectId).filter((id) => id !== void 0);
2970
- const stakeObjects = await utils.cache.queryGetObjects(stakeObjectIds);
2992
+ const stakeObjects = await utils.cache.queryGetObjects(stakeObjectIds, {
2993
+ showContent: true,
2994
+ showType: true
2995
+ });
2971
2996
  for (const stakeObject of stakeObjects) {
2972
2997
  const id = stakeObject.objectId;
2973
2998
  const type = stakeObject.type;
@@ -3263,7 +3288,7 @@ var queryBorrowIncentiveAccounts = async ({
3263
3288
  const queryTarget = `${queryPkgId}::incentive_account_query::incentive_account_data`;
3264
3289
  const args = [incentiveAccountsId, obligationId];
3265
3290
  const queryResult = await utils.cache.queryInspectTxn({ queryTarget, args });
3266
- const borrowIncentiveAccountsQueryData = queryResult?.events[0].parsedJson;
3291
+ const borrowIncentiveAccountsQueryData = queryResult?.events[0]?.parsedJson;
3267
3292
  const borrowIncentiveAccounts = Object.values(
3268
3293
  borrowIncentiveAccountsQueryData?.pool_records ?? []
3269
3294
  ).reduce((accounts, accountData) => {
@@ -3953,7 +3978,8 @@ var getVescaKeys = async (utils, ownerAddress) => {
3953
3978
  filter: {
3954
3979
  StructType: veScaKeyType
3955
3980
  },
3956
- cursor: nextCursor
3981
+ cursor: nextCursor,
3982
+ limit: 10
3957
3983
  });
3958
3984
  if (!paginatedKeyObjectsResponse)
3959
3985
  continue;
@@ -4200,12 +4226,17 @@ var ScallopUtils = class {
4200
4226
  ...params
4201
4227
  };
4202
4228
  this.suiKit = instance?.suiKit ?? instance?.address?.cache._suiKit ?? new SuiKit2(params);
4229
+ this.walletAddress = params.walletAddress ?? this.suiKit.currentAddress();
4203
4230
  if (instance?.address) {
4204
4231
  this.address = instance.address;
4205
4232
  this.cache = this.address.cache;
4206
4233
  this.suiKit = this.address.cache._suiKit;
4207
4234
  } else {
4208
- this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
4235
+ this.cache = new ScallopCache(
4236
+ this.suiKit,
4237
+ this.walletAddress,
4238
+ DEFAULT_CACHE_OPTIONS
4239
+ );
4209
4240
  this.address = instance?.address ?? new ScallopAddress(
4210
4241
  {
4211
4242
  id: params?.addressesId || ADDRESSES_ID,
@@ -4418,7 +4449,7 @@ var ScallopUtils = class {
4418
4449
  * @param coinType
4419
4450
  * @param sender
4420
4451
  */
4421
- async mergeSimilarCoins(txBlock, dest, coinType, sender) {
4452
+ async mergeSimilarCoins(txBlock, dest, coinType, sender = this.walletAddress) {
4422
4453
  try {
4423
4454
  const existingCoins = await this.selectCoins(
4424
4455
  Number.MAX_SAFE_INTEGER,
@@ -6235,7 +6266,7 @@ import { SuiKit as SuiKit3 } from "@scallop-io/sui-kit";
6235
6266
  var ScallopIndexer = class {
6236
6267
  constructor(params, instance) {
6237
6268
  this.params = params;
6238
- this.cache = instance?.cache ?? new ScallopCache(new SuiKit3({}), DEFAULT_CACHE_OPTIONS);
6269
+ this.cache = instance?.cache ?? new ScallopCache(new SuiKit3({}), void 0, DEFAULT_CACHE_OPTIONS);
6239
6270
  this._requestClient = axios2.create({
6240
6271
  baseURL: SDK_API_BASE_URL,
6241
6272
  headers: {
@@ -6498,12 +6529,19 @@ var ScallopQuery = class {
6498
6529
  constructor(params, instance) {
6499
6530
  this.params = params;
6500
6531
  this.suiKit = instance?.suiKit ?? instance?.utils?.suiKit ?? new SuiKit4(params);
6532
+ this.walletAddress = normalizeSuiAddress2(
6533
+ params.walletAddress || this.suiKit.currentAddress()
6534
+ );
6501
6535
  if (instance?.utils) {
6502
6536
  this.utils = instance.utils;
6503
6537
  this.address = instance.utils.address;
6504
6538
  this.cache = this.address.cache;
6505
6539
  } else {
6506
- this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
6540
+ this.cache = new ScallopCache(
6541
+ this.suiKit,
6542
+ this.walletAddress,
6543
+ DEFAULT_CACHE_OPTIONS
6544
+ );
6507
6545
  this.address = new ScallopAddress(
6508
6546
  {
6509
6547
  id: params?.addressesId || ADDRESSES_ID,
@@ -6518,9 +6556,6 @@ var ScallopQuery = class {
6518
6556
  });
6519
6557
  }
6520
6558
  this.indexer = instance?.indexer ?? new ScallopIndexer(this.params, { cache: this.cache });
6521
- this.walletAddress = normalizeSuiAddress2(
6522
- params.walletAddress || this.suiKit.currentAddress()
6523
- );
6524
6559
  }
6525
6560
  /**
6526
6561
  * Request the scallop API to initialize data.
@@ -6976,7 +7011,11 @@ var ScallopBuilder = class {
6976
7011
  this.address = this.utils.address;
6977
7012
  this.cache = this.address.cache;
6978
7013
  } else {
6979
- this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
7014
+ this.cache = new ScallopCache(
7015
+ this.suiKit,
7016
+ this.walletAddress,
7017
+ DEFAULT_CACHE_OPTIONS
7018
+ );
6980
7019
  this.address = new ScallopAddress(
6981
7020
  {
6982
7021
  id: params?.addressesId || ADDRESSES_ID,
@@ -7034,10 +7073,15 @@ var ScallopBuilder = class {
7034
7073
  * @return Take coin and left coin.
7035
7074
  */
7036
7075
  async selectCoin(txBlock, assetCoinName, amount, sender = this.walletAddress) {
7037
- const coinType = this.utils.parseCoinType(assetCoinName);
7038
- const coins = await this.utils.selectCoins(amount, coinType, sender);
7039
- const [takeCoin, leftCoin] = txBlock.takeAmountFromCoins(coins, amount);
7040
- return { takeCoin, leftCoin };
7076
+ if (assetCoinName === "sui") {
7077
+ const [takeCoin] = txBlock.splitSUIFromGas([amount]);
7078
+ return { takeCoin };
7079
+ } else {
7080
+ const coinType = this.utils.parseCoinType(assetCoinName);
7081
+ const coins = await this.utils.selectCoins(amount, coinType, sender);
7082
+ const [takeCoin, leftCoin] = txBlock.takeAmountFromCoins(coins, amount);
7083
+ return { takeCoin, leftCoin };
7084
+ }
7041
7085
  }
7042
7086
  /**
7043
7087
  * Specifying the sender's amount of market coins to get coins args from transaction result.
@@ -7114,7 +7158,11 @@ var ScallopClient = class {
7114
7158
  this.address = this.utils.address;
7115
7159
  this.cache = this.address.cache;
7116
7160
  } else {
7117
- this.cache = new ScallopCache(this.suiKit, DEFAULT_CACHE_OPTIONS);
7161
+ this.cache = new ScallopCache(
7162
+ this.suiKit,
7163
+ this.walletAddress,
7164
+ DEFAULT_CACHE_OPTIONS
7165
+ );
7118
7166
  this.address = new ScallopAddress(
7119
7167
  {
7120
7168
  id: params?.addressesId || ADDRESSES_ID,
@@ -7770,6 +7818,7 @@ var Scallop = class {
7770
7818
  this.suiKit = new SuiKit7(params);
7771
7819
  this.cache = new ScallopCache(
7772
7820
  this.suiKit,
7821
+ params.walletAddress,
7773
7822
  cacheOptions ?? DEFAULT_CACHE_OPTIONS,
7774
7823
  tokenBucket ?? new TokenBucket(DEFAULT_TOKENS_PER_INTERVAL, DEFAULT_INTERVAL_IN_MS)
7775
7824
  );