@magmaprotocol/magma-clmm-sdk 0.5.55 → 0.5.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/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,19 @@ 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 (let index = 0; index < allPosition.length; index++) {
10947
+ const item = allPosition[index];
10948
+ }
10949
+ for (const item of allPosition) {
10950
+ const pool = poolList.find((pool2) => pool2.pool_id === item.pool);
10928
10951
  const coins = pool_coins.get(item.pool) || ["", ""];
10929
10952
  const positionLiquidity = await this.getPositionLiquidity({
10930
10953
  pair: item.pool,
@@ -10933,7 +10956,7 @@ var DlmmModule = class {
10933
10956
  coinTypeB: coins[1]
10934
10957
  });
10935
10958
  const rewards_token = pool_reward_coins.get(item.pool) || [];
10936
- let positionRewards = { position_id: item.pool, reward: [], amount: [] };
10959
+ let positionRewards = { position_id: item.pos_object_id, reward: [], amount: [] };
10937
10960
  if (rewards_token.length > 0) {
10938
10961
  positionRewards = await this.getEarnedRewards({
10939
10962
  pool_id: item.pool,
@@ -10953,7 +10976,8 @@ var DlmmModule = class {
10953
10976
  position: item,
10954
10977
  liquidity: positionLiquidity,
10955
10978
  rewards: positionRewards,
10956
- fees: positionFees
10979
+ fees: positionFees,
10980
+ contractPool: pool
10957
10981
  });
10958
10982
  }
10959
10983
  return out;
@@ -11073,7 +11097,7 @@ var DlmmModule = class {
11073
11097
  const tx = new Transaction12();
11074
11098
  const { integrate, simulationAccount } = this.sdk.sdkOptions;
11075
11099
  const typeArguments = [params.coin_a, params.coin_b];
11076
- const args = [tx.object(params.pool_id), tx.object(params.position_id), tx.object(CLOCK_ADDRESS)];
11100
+ const args = [tx.object(params.pool_id), tx.object(params.position_id)];
11077
11101
  tx.moveCall({
11078
11102
  target: `${integrate.published_at}::${DlmmScript}::earned_fees`,
11079
11103
  arguments: args,
@@ -11192,6 +11216,41 @@ var DlmmModule = class {
11192
11216
  });
11193
11217
  return out;
11194
11218
  }
11219
+ /**
11220
+ * Updates the cache for the given key.
11221
+ *
11222
+ * @param key The key of the cache entry to update.
11223
+ * @param data The data to store in the cache.
11224
+ * @param time The time in minutes after which the cache entry should expire.
11225
+ */
11226
+ updateCache(key, data, time = cacheTime5min) {
11227
+ let cacheData = this._cache[key];
11228
+ if (cacheData) {
11229
+ cacheData.overdueTime = getFutureTime(time);
11230
+ cacheData.value = data;
11231
+ } else {
11232
+ cacheData = new CachedContent(data, getFutureTime(time));
11233
+ }
11234
+ this._cache[key] = cacheData;
11235
+ }
11236
+ /**
11237
+ * Gets the cache entry for the given key.
11238
+ *
11239
+ * @param key The key of the cache entry to get.
11240
+ * @param forceRefresh Whether to force a refresh of the cache entry.
11241
+ * @returns The cache entry for the given key, or undefined if the cache entry does not exist or is expired.
11242
+ */
11243
+ getCache(key, forceRefresh = false) {
11244
+ const cacheData = this._cache[key];
11245
+ const isValid = cacheData?.isValid();
11246
+ if (!forceRefresh && isValid) {
11247
+ return cacheData.value;
11248
+ }
11249
+ if (!isValid) {
11250
+ delete this._cache[key];
11251
+ }
11252
+ return void 0;
11253
+ }
11195
11254
  };
11196
11255
 
11197
11256
  // src/sdk.ts