@magmaprotocol/magma-clmm-sdk 0.5.56 → 0.5.58

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
@@ -10510,6 +10510,7 @@ import { get_real_id_from_price_x128 as get_real_id_from_price_x1282, get_storag
10510
10510
  import Decimal10 from "decimal.js";
10511
10511
  var DlmmModule = class {
10512
10512
  _sdk;
10513
+ _cache = {};
10513
10514
  constructor(sdk) {
10514
10515
  this._sdk = sdk;
10515
10516
  }
@@ -10896,20 +10897,32 @@ var DlmmModule = class {
10896
10897
  });
10897
10898
  return res;
10898
10899
  }
10899
- async getUserPositions(who, showDisplay = true) {
10900
- const ownerRes = await this._sdk.fullClient.getOwnedObjectsByPage(who, {
10900
+ /**
10901
+ * Gets a list of positions for the given account address.
10902
+ * @param accountAddress The account address to get positions for.
10903
+ * @param assignPoolIds An array of pool IDs to filter the positions by.
10904
+ * @returns array of Position objects.
10905
+ */
10906
+ async getUserPositions(accountAddress, assignPoolIds = [], showDisplay = true) {
10907
+ const allPosition = [];
10908
+ const ownerRes = await this._sdk.fullClient.getOwnedObjectsByPage(accountAddress, {
10901
10909
  options: { showType: true, showContent: true, showDisplay, showOwner: true },
10902
10910
  filter: { Package: this._sdk.sdkOptions.dlmm_pool.package_id }
10903
10911
  });
10904
- const positions = [];
10912
+ const hasAssignPoolIds = assignPoolIds.length > 0;
10905
10913
  const pools = [];
10906
10914
  for (const item of ownerRes.data) {
10907
10915
  const type = extractStructTagFromType(item.data.type);
10908
10916
  if (type.full_address === this.buildPositionType()) {
10909
10917
  const position = this.buildPosition(item);
10910
- positions.push(position);
10911
- if (!pools.includes(position.pool)) {
10912
- pools.push(position.pool);
10918
+ const cacheKey = `${position.pos_object_id}_getPositionList`;
10919
+ this.updateCache(cacheKey, position, cacheTime24h);
10920
+ if (hasAssignPoolIds) {
10921
+ if (assignPoolIds.includes(position.pool)) {
10922
+ allPosition.push(position);
10923
+ }
10924
+ } else {
10925
+ allPosition.push(position);
10913
10926
  }
10914
10927
  }
10915
10928
  }
@@ -10922,9 +10935,16 @@ var DlmmModule = class {
10922
10935
  coin_b: value[1]
10923
10936
  });
10924
10937
  }
10938
+ const poolMap = /* @__PURE__ */ new Set();
10939
+ for (const item of allPosition) {
10940
+ poolMap.add(item.pool);
10941
+ }
10942
+ const poolList = await this.getPoolInfo(Array.from(poolMap));
10943
+ this.updateCache(`${DlmmScript}_positionList_poolList`, poolList, cacheTime24h);
10925
10944
  const pool_reward_coins = await this.getPairRewarders(_params);
10926
10945
  const out = [];
10927
- for (const item of positions) {
10946
+ for (const item of allPosition) {
10947
+ const pool = poolList.find((pool2) => pool2.pool_id === item.pool);
10928
10948
  const coins = pool_coins.get(item.pool) || ["", ""];
10929
10949
  const positionLiquidity = await this.getPositionLiquidity({
10930
10950
  pair: item.pool,
@@ -10933,7 +10953,7 @@ var DlmmModule = class {
10933
10953
  coinTypeB: coins[1]
10934
10954
  });
10935
10955
  const rewards_token = pool_reward_coins.get(item.pool) || [];
10936
- let positionRewards = { position_id: item.pool, reward: [], amount: [] };
10956
+ let positionRewards = { position_id: item.pos_object_id, reward: [], amount: [] };
10937
10957
  if (rewards_token.length > 0) {
10938
10958
  positionRewards = await this.getEarnedRewards({
10939
10959
  pool_id: item.pool,
@@ -10953,7 +10973,8 @@ var DlmmModule = class {
10953
10973
  position: item,
10954
10974
  liquidity: positionLiquidity,
10955
10975
  rewards: positionRewards,
10956
- fees: positionFees
10976
+ fees: positionFees,
10977
+ contractPool: pool
10957
10978
  });
10958
10979
  }
10959
10980
  return out;
@@ -11192,6 +11213,41 @@ var DlmmModule = class {
11192
11213
  });
11193
11214
  return out;
11194
11215
  }
11216
+ /**
11217
+ * Updates the cache for the given key.
11218
+ *
11219
+ * @param key The key of the cache entry to update.
11220
+ * @param data The data to store in the cache.
11221
+ * @param time The time in minutes after which the cache entry should expire.
11222
+ */
11223
+ updateCache(key, data, time = cacheTime5min) {
11224
+ let cacheData = this._cache[key];
11225
+ if (cacheData) {
11226
+ cacheData.overdueTime = getFutureTime(time);
11227
+ cacheData.value = data;
11228
+ } else {
11229
+ cacheData = new CachedContent(data, getFutureTime(time));
11230
+ }
11231
+ this._cache[key] = cacheData;
11232
+ }
11233
+ /**
11234
+ * Gets the cache entry for the given key.
11235
+ *
11236
+ * @param key The key of the cache entry to get.
11237
+ * @param forceRefresh Whether to force a refresh of the cache entry.
11238
+ * @returns The cache entry for the given key, or undefined if the cache entry does not exist or is expired.
11239
+ */
11240
+ getCache(key, forceRefresh = false) {
11241
+ const cacheData = this._cache[key];
11242
+ const isValid = cacheData?.isValid();
11243
+ if (!forceRefresh && isValid) {
11244
+ return cacheData.value;
11245
+ }
11246
+ if (!isValid) {
11247
+ delete this._cache[key];
11248
+ }
11249
+ return void 0;
11250
+ }
11195
11251
  };
11196
11252
 
11197
11253
  // src/sdk.ts