@alphafi/alphafi-sdk 0.0.34 → 0.0.36

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 (131) hide show
  1. package/dist/cjs/common/coins.d.ts +1 -1
  2. package/dist/cjs/common/coins.d.ts.map +1 -1
  3. package/dist/cjs/common/coins.js +15 -11
  4. package/dist/cjs/common/coins.js.map +1 -1
  5. package/dist/cjs/common/constants.d.ts +436 -32
  6. package/dist/cjs/common/constants.d.ts.map +1 -1
  7. package/dist/cjs/common/constants.js +488 -56
  8. package/dist/cjs/common/constants.js.map +1 -1
  9. package/dist/cjs/common/maps.d.ts +46 -28
  10. package/dist/cjs/common/maps.d.ts.map +1 -1
  11. package/dist/cjs/common/maps.js +358 -227
  12. package/dist/cjs/common/maps.js.map +1 -1
  13. package/dist/cjs/common/types.d.ts +59 -47
  14. package/dist/cjs/common/types.d.ts.map +1 -1
  15. package/dist/cjs/common/types.js.map +1 -1
  16. package/dist/cjs/getVaultBalances.d.ts +2 -1
  17. package/dist/cjs/getVaultBalances.d.ts.map +1 -1
  18. package/dist/cjs/getVaultBalances.js +16 -1
  19. package/dist/cjs/getVaultBalances.js.map +1 -1
  20. package/dist/cjs/graphql/getMultiReceipts.d.ts.map +1 -1
  21. package/dist/cjs/graphql/getMultiReceipts.js +4 -0
  22. package/dist/cjs/graphql/getMultiReceipts.js.map +1 -1
  23. package/dist/cjs/index.d.ts +4 -4
  24. package/dist/cjs/index.d.ts.map +1 -1
  25. package/dist/cjs/index.js +13 -5
  26. package/dist/cjs/index.js.map +1 -1
  27. package/dist/cjs/package.json +1 -0
  28. package/dist/cjs/sui-sdk/events/fetchAfterTransactionEvents.d.ts.map +1 -1
  29. package/dist/cjs/sui-sdk/events/fetchAfterTransactionEvents.js +2 -2
  30. package/dist/cjs/sui-sdk/events/fetchAfterTransactionEvents.js.map +1 -1
  31. package/dist/cjs/sui-sdk/events/fetchAutoCompoundingEvents.d.ts.map +1 -1
  32. package/dist/cjs/sui-sdk/events/fetchAutoCompoundingEvents.js +2 -2
  33. package/dist/cjs/sui-sdk/events/fetchAutoCompoundingEvents.js.map +1 -1
  34. package/dist/cjs/sui-sdk/events/fetchRebalanceEvents.d.ts.map +1 -1
  35. package/dist/cjs/sui-sdk/events/fetchRebalanceEvents.js +5 -7
  36. package/dist/cjs/sui-sdk/events/fetchRebalanceEvents.js.map +1 -1
  37. package/dist/cjs/sui-sdk/functions/fetchUserVaultBalances.d.ts +1 -1
  38. package/dist/cjs/sui-sdk/functions/fetchUserVaultBalances.d.ts.map +1 -1
  39. package/dist/cjs/sui-sdk/functions/fetchUserVaultBalances.js +11 -20
  40. package/dist/cjs/sui-sdk/functions/fetchUserVaultBalances.js.map +1 -1
  41. package/dist/cjs/sui-sdk/functions/fetchUserVaults.d.ts.map +1 -1
  42. package/dist/cjs/sui-sdk/functions/fetchUserVaults.js +8 -7
  43. package/dist/cjs/sui-sdk/functions/fetchUserVaults.js.map +1 -1
  44. package/dist/cjs/sui-sdk/functions/getPortfolioAmounts.d.ts +8 -23
  45. package/dist/cjs/sui-sdk/functions/getPortfolioAmounts.d.ts.map +1 -1
  46. package/dist/cjs/sui-sdk/functions/getPortfolioAmounts.js +157 -141
  47. package/dist/cjs/sui-sdk/functions/getPortfolioAmounts.js.map +1 -1
  48. package/dist/cjs/sui-sdk/functions/getReceipts.d.ts +15 -10
  49. package/dist/cjs/sui-sdk/functions/getReceipts.d.ts.map +1 -1
  50. package/dist/cjs/sui-sdk/functions/getReceipts.js +274 -190
  51. package/dist/cjs/sui-sdk/functions/getReceipts.js.map +1 -1
  52. package/dist/cjs/utils/clmm/prices.js +9 -9
  53. package/dist/cjs/utils/clmm/prices.js.map +1 -1
  54. package/dist/cjs/utils/getHoldersFromTransactionsUtils.d.ts.map +1 -1
  55. package/dist/cjs/utils/getHoldersFromTransactionsUtils.js +7 -7
  56. package/dist/cjs/utils/getHoldersFromTransactionsUtils.js.map +1 -1
  57. package/dist/cjs/utils/hop.js +3 -3
  58. package/dist/cjs/utils/hop.js.map +1 -1
  59. package/dist/cjs/utils/prices.d.ts +2 -0
  60. package/dist/cjs/utils/prices.d.ts.map +1 -1
  61. package/dist/cjs/utils/prices.js +39 -0
  62. package/dist/cjs/utils/prices.js.map +1 -1
  63. package/dist/cjs/utils/userHoldings.d.ts.map +1 -1
  64. package/dist/cjs/utils/userHoldings.js +18 -18
  65. package/dist/cjs/utils/userHoldings.js.map +1 -1
  66. package/dist/esm/common/coins.d.ts +1 -1
  67. package/dist/esm/common/coins.d.ts.map +1 -1
  68. package/dist/esm/common/coins.js +14 -10
  69. package/dist/esm/common/coins.js.map +1 -1
  70. package/dist/esm/common/constants.d.ts +436 -32
  71. package/dist/esm/common/constants.d.ts.map +1 -1
  72. package/dist/esm/common/constants.js +488 -56
  73. package/dist/esm/common/constants.js.map +1 -1
  74. package/dist/esm/common/maps.d.ts +46 -28
  75. package/dist/esm/common/maps.d.ts.map +1 -1
  76. package/dist/esm/common/maps.js +355 -226
  77. package/dist/esm/common/maps.js.map +1 -1
  78. package/dist/esm/common/types.d.ts +59 -47
  79. package/dist/esm/common/types.d.ts.map +1 -1
  80. package/dist/esm/common/types.js.map +1 -1
  81. package/dist/esm/getVaultBalances.d.ts +2 -1
  82. package/dist/esm/getVaultBalances.d.ts.map +1 -1
  83. package/dist/esm/getVaultBalances.js +15 -1
  84. package/dist/esm/getVaultBalances.js.map +1 -1
  85. package/dist/esm/graphql/getMultiReceipts.d.ts.map +1 -1
  86. package/dist/esm/graphql/getMultiReceipts.js +4 -0
  87. package/dist/esm/graphql/getMultiReceipts.js.map +1 -1
  88. package/dist/esm/index.d.ts +4 -4
  89. package/dist/esm/index.d.ts.map +1 -1
  90. package/dist/esm/index.js +4 -4
  91. package/dist/esm/index.js.map +1 -1
  92. package/dist/esm/package.json +1 -0
  93. package/dist/esm/sui-sdk/events/fetchAfterTransactionEvents.d.ts.map +1 -1
  94. package/dist/esm/sui-sdk/events/fetchAfterTransactionEvents.js +4 -4
  95. package/dist/esm/sui-sdk/events/fetchAfterTransactionEvents.js.map +1 -1
  96. package/dist/esm/sui-sdk/events/fetchAutoCompoundingEvents.d.ts.map +1 -1
  97. package/dist/esm/sui-sdk/events/fetchAutoCompoundingEvents.js +4 -4
  98. package/dist/esm/sui-sdk/events/fetchAutoCompoundingEvents.js.map +1 -1
  99. package/dist/esm/sui-sdk/events/fetchRebalanceEvents.d.ts.map +1 -1
  100. package/dist/esm/sui-sdk/events/fetchRebalanceEvents.js +7 -9
  101. package/dist/esm/sui-sdk/events/fetchRebalanceEvents.js.map +1 -1
  102. package/dist/esm/sui-sdk/functions/fetchUserVaultBalances.d.ts +1 -1
  103. package/dist/esm/sui-sdk/functions/fetchUserVaultBalances.d.ts.map +1 -1
  104. package/dist/esm/sui-sdk/functions/fetchUserVaultBalances.js +11 -20
  105. package/dist/esm/sui-sdk/functions/fetchUserVaultBalances.js.map +1 -1
  106. package/dist/esm/sui-sdk/functions/fetchUserVaults.d.ts.map +1 -1
  107. package/dist/esm/sui-sdk/functions/fetchUserVaults.js +9 -8
  108. package/dist/esm/sui-sdk/functions/fetchUserVaults.js.map +1 -1
  109. package/dist/esm/sui-sdk/functions/getPortfolioAmounts.d.ts +8 -23
  110. package/dist/esm/sui-sdk/functions/getPortfolioAmounts.d.ts.map +1 -1
  111. package/dist/esm/sui-sdk/functions/getPortfolioAmounts.js +158 -144
  112. package/dist/esm/sui-sdk/functions/getPortfolioAmounts.js.map +1 -1
  113. package/dist/esm/sui-sdk/functions/getReceipts.d.ts +15 -10
  114. package/dist/esm/sui-sdk/functions/getReceipts.d.ts.map +1 -1
  115. package/dist/esm/sui-sdk/functions/getReceipts.js +266 -186
  116. package/dist/esm/sui-sdk/functions/getReceipts.js.map +1 -1
  117. package/dist/esm/utils/clmm/prices.js +10 -10
  118. package/dist/esm/utils/clmm/prices.js.map +1 -1
  119. package/dist/esm/utils/getHoldersFromTransactionsUtils.d.ts.map +1 -1
  120. package/dist/esm/utils/getHoldersFromTransactionsUtils.js +9 -9
  121. package/dist/esm/utils/getHoldersFromTransactionsUtils.js.map +1 -1
  122. package/dist/esm/utils/hop.js +4 -4
  123. package/dist/esm/utils/hop.js.map +1 -1
  124. package/dist/esm/utils/prices.d.ts +2 -0
  125. package/dist/esm/utils/prices.d.ts.map +1 -1
  126. package/dist/esm/utils/prices.js +37 -0
  127. package/dist/esm/utils/prices.js.map +1 -1
  128. package/dist/esm/utils/userHoldings.d.ts.map +1 -1
  129. package/dist/esm/utils/userHoldings.js +20 -20
  130. package/dist/esm/utils/userHoldings.js.map +1 -1
  131. package/package.json +2 -2
@@ -1,26 +1,126 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertReceiptGQLToReceipt = convertReceiptGQLToReceipt;
4
+ exports.getMultiReceipts = getMultiReceipts;
3
5
  exports.getReceipts = getReceipts;
4
6
  exports.getPoolExchangeRate = getPoolExchangeRate;
5
- exports.getCoinAmountsFromLiquidity = getCoinAmountsFromLiquidity;
7
+ exports.getMultiPool = getMultiPool;
6
8
  exports.getPool = getPool;
7
- exports.getCetusPool = getCetusPool;
8
- exports.getCetusInvestor = getCetusInvestor;
9
- exports.getNaviInvestor = getNaviInvestor;
9
+ exports.getMultiParentPool = getMultiParentPool;
10
+ exports.getParentPool = getParentPool;
11
+ exports.getMultiInvestor = getMultiInvestor;
12
+ exports.getInvestor = getInvestor;
10
13
  exports.fetchVoloExchangeRate = fetchVoloExchangeRate;
14
+ exports.getCoinAmountsFromLiquidity = getCoinAmountsFromLiquidity;
11
15
  exports.multiGetNaviInvestor = multiGetNaviInvestor;
12
16
  const tslib_1 = require("tslib");
17
+ const index_js_1 = require("../../index.js");
13
18
  const maps_js_1 = require("../../common/maps.js");
14
- const simpleCache_js_1 = require("../../utils/simpleCache.js");
15
19
  const cetus_sui_clmm_sdk_1 = require("@cetusprotocol/cetus-sui-clmm-sdk");
16
20
  const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
17
21
  const decimal_js_1 = require("decimal.js");
18
22
  const client_js_1 = require("../client.js");
19
- const receiptsCache = new simpleCache_js_1.SimpleCache();
20
- const receiptsPromiseCache = new simpleCache_js_1.SimpleCache();
21
- async function getReceipts(poolName, address, ignoreCache = false) {
23
+ const simpleCache_js_1 = require("../../utils/simpleCache.js");
24
+ function convertReceiptGQLToReceipt(receipts) {
25
+ const res = receipts.map((receipt) => {
26
+ return {
27
+ objectId: receipt.address,
28
+ version: receipt.version,
29
+ digest: receipt.digest,
30
+ content: {
31
+ dataType: "moveObject", // Assuming a fixed value as it's not available in ReceiptA
32
+ type: receipt.contents.type.repr,
33
+ hasPublicTransfer: receipt.hasPublicTransfer,
34
+ fields: {
35
+ id: { id: receipt.contents.json.id },
36
+ image_url: receipt.contents.json.image_url,
37
+ last_acc_reward_per_xtoken: {
38
+ type: "unknown", // Assuming fixed value
39
+ fields: {
40
+ contents: receipt.contents.json.last_acc_reward_per_xtoken.contents.map((item) => ({
41
+ type: "unknown", // Assuming fixed value
42
+ fields: {
43
+ value: item.value,
44
+ key: {
45
+ type: "unknown", // Assuming fixed value
46
+ fields: {
47
+ name: item.key.name,
48
+ },
49
+ },
50
+ },
51
+ })),
52
+ },
53
+ },
54
+ locked_balance: receipt.contents.json.locked_balance
55
+ ? {
56
+ type: "unknown", // Assuming fixed value
57
+ fields: {
58
+ head: receipt.contents.json.locked_balance.head,
59
+ id: { id: receipt.contents.json.locked_balance.id },
60
+ size: receipt.contents.json.locked_balance.size,
61
+ tail: receipt.contents.json.locked_balance.tail,
62
+ },
63
+ }
64
+ : undefined,
65
+ name: receipt.contents.json.name,
66
+ owner: receipt.contents.json.owner,
67
+ pending_rewards: {
68
+ type: "unknown", // Assuming fixed value
69
+ fields: {
70
+ contents: receipt.contents.json.pending_rewards.contents.map((item) => ({
71
+ type: "unknown", // Assuming fixed value
72
+ fields: {
73
+ key: {
74
+ type: "unknown", // Assuming fixed value
75
+ fields: {
76
+ name: item.key.name,
77
+ },
78
+ },
79
+ value: item.value,
80
+ },
81
+ })),
82
+ },
83
+ },
84
+ pool_id: receipt.contents.json.pool_id,
85
+ xTokenBalance: receipt.contents.json.xTokenBalance,
86
+ unlocked_xtokens: receipt.contents.json.unlocked_xtokens ?? undefined,
87
+ },
88
+ },
89
+ };
90
+ });
91
+ return res;
92
+ }
93
+ const receiptsCache = new simpleCache_js_1.SimpleCache(3600000);
94
+ const receiptsPromiseCache = new simpleCache_js_1.SimpleCache(3600000);
95
+ async function getMultiReceipts(address) {
96
+ try {
97
+ const receiptMap = await (0, index_js_1.fetchMultiReceipts)(address);
98
+ for (const pool of Object.keys(maps_js_1.poolInfo)) {
99
+ const cacheKey = `getReceipts-${maps_js_1.poolInfo[pool].receiptName}-${address}`;
100
+ let receipt = [];
101
+ if (receiptMap.has(maps_js_1.poolInfo[pool].receiptName)) {
102
+ receipt = convertReceiptGQLToReceipt(receiptMap.get(maps_js_1.poolInfo[pool].receiptName));
103
+ }
104
+ receiptsCache.set(cacheKey, receipt);
105
+ }
106
+ }
107
+ catch (error) {
108
+ console.error("Error fetching receipts from graphQL:", error);
109
+ // get receipts individually
110
+ const pools = Object.keys(maps_js_1.poolInfo);
111
+ const receiptPromises = pools.map((pool) => {
112
+ return getReceipts(pool, address, true);
113
+ });
114
+ const receipts = await Promise.all(receiptPromises);
115
+ receipts.forEach((receipt, index) => {
116
+ const cacheKey = `getReceipts-${maps_js_1.poolInfo[pools[index]].receiptName}-${address}`;
117
+ receiptsCache.set(cacheKey, receipt);
118
+ });
119
+ }
120
+ }
121
+ async function getReceipts(poolName, address, ignoreCache) {
22
122
  const suiClient = (0, client_js_1.getSuiClient)();
23
- const receiptsCacheKey = `getReceipts-${poolName}-${address}`;
123
+ const receiptsCacheKey = `getReceipts-${maps_js_1.poolInfo[poolName].receiptName}-${address}`;
24
124
  if (ignoreCache) {
25
125
  receiptsCache.delete(receiptsCacheKey);
26
126
  receiptsPromiseCache.delete(receiptsCacheKey);
@@ -29,16 +129,16 @@ async function getReceipts(poolName, address, ignoreCache = false) {
29
129
  if (cachedResponse) {
30
130
  return cachedResponse;
31
131
  }
132
+ //
32
133
  const nfts = [];
33
- if (maps_js_1.poolInfo[poolName].receiptType === "") {
134
+ if (maps_js_1.poolInfo[poolName].receiptType == "") {
34
135
  return nfts;
35
136
  }
36
137
  let cachedPromise = receiptsPromiseCache.get(receiptsCacheKey);
37
138
  if (!cachedPromise) {
38
139
  cachedPromise = (async () => {
39
- // const first_package = conf[CONF_ENV].ALPHA_FIRST_PACKAGE_ID;
140
+ // const first_package = getConf().ALPHA_FIRST_PACKAGE_ID;
40
141
  let currentCursor = null;
41
- /* eslint-disable-next-line no-constant-condition */
42
142
  while (true) {
43
143
  const paginatedObjects = await suiClient.getOwnedObjects({
44
144
  owner: address,
@@ -48,7 +148,6 @@ async function getReceipts(poolName, address, ignoreCache = false) {
48
148
  StructType: maps_js_1.poolInfo[poolName].receiptType,
49
149
  },
50
150
  options: {
51
- showType: true,
52
151
  showContent: true,
53
152
  },
54
153
  });
@@ -67,6 +166,7 @@ async function getReceipts(poolName, address, ignoreCache = false) {
67
166
  }
68
167
  else {
69
168
  // No more pages available
169
+ // console.log("No more receipts available.");
70
170
  break;
71
171
  }
72
172
  }
@@ -74,90 +174,75 @@ async function getReceipts(poolName, address, ignoreCache = false) {
74
174
  receiptsPromiseCache.delete(receiptsCacheKey);
75
175
  return nfts;
76
176
  })().catch((error) => {
77
- // TODO: Jugaad
78
- if (maps_js_1.poolInfo[poolName].parentProtocolName === "NAVI") {
79
- return nfts;
80
- }
81
- else {
82
- receiptsPromiseCache.delete(receiptsCacheKey); // Remove the promise from cache
83
- throw error;
84
- }
177
+ receiptsPromiseCache.delete(receiptsCacheKey); // Remove the promise from cache
178
+ throw error;
85
179
  });
180
+ // const data = await cachedPromise;
181
+ // console.log("received cached data", data);
86
182
  receiptsPromiseCache.set(receiptsCacheKey, cachedPromise);
87
183
  }
88
184
  return cachedPromise;
89
185
  }
90
- const poolExchangeRateCache = new simpleCache_js_1.SimpleCache();
91
- async function getPoolExchangeRate(poolName, ignoreCache = false) {
92
- const poolExchangeRateCacheKey = `getPoolExchangeRate-${poolName}`;
93
- if (ignoreCache) {
94
- poolExchangeRateCache.delete(poolExchangeRateCacheKey);
95
- }
96
- const cachedResponse = poolExchangeRateCache.get(poolExchangeRateCacheKey);
97
- if (cachedResponse) {
98
- return cachedResponse;
99
- }
186
+ async function getPoolExchangeRate(poolName, ignoreCache) {
100
187
  let pool;
101
188
  try {
102
- if (poolName === "ALPHA") {
103
- pool = await getPool("ALPHA");
104
- }
105
- else if (poolName === "ALPHA-SUI" ||
106
- poolName === "USDT-WUSDC" ||
107
- poolName === "USDY-WUSDC" ||
108
- poolName === "HASUI-SUI" ||
109
- poolName === "WUSDC-SUI" ||
110
- poolName === "WETH-WUSDC") {
111
- pool = await getPool(poolName);
112
- }
113
- else {
114
- pool = await getPool(poolName);
115
- }
189
+ pool = await getPool(poolName, ignoreCache);
116
190
  if (pool) {
117
191
  const xTokenSupply = new decimal_js_1.Decimal(pool.content.fields.xTokenSupply);
118
192
  let tokensInvested = new decimal_js_1.Decimal(pool.content.fields.tokensInvested);
119
193
  if (poolName == "ALPHA") {
120
194
  tokensInvested = new decimal_js_1.Decimal(pool.content.fields.alpha_bal);
121
195
  }
196
+ else if (maps_js_1.poolInfo[poolName].parentProtocolName == "CETUS") {
197
+ const investor = (await getInvestor(poolName, ignoreCache));
198
+ if (!investor) {
199
+ throw new Error(`couldnt fetch investor object for pool: ${poolName}`);
200
+ }
201
+ tokensInvested = new decimal_js_1.Decimal(pool.content.fields.tokensInvested);
202
+ }
122
203
  // Check for division by zero
123
204
  if (xTokenSupply.eq(0)) {
124
205
  console.error("Division by zero error: tokensInvested is zero.");
125
206
  return undefined;
126
207
  }
127
208
  const poolExchangeRate = tokensInvested.div(xTokenSupply);
128
- poolExchangeRateCache.set(poolExchangeRateCacheKey, poolExchangeRate);
129
209
  return poolExchangeRate;
130
210
  }
131
211
  }
132
212
  catch (e) {
133
- console.error(`getPoolExchangeRate failed for poolName: ${poolName}`);
213
+ console.log(`getPoolExchangeRate failed for poolName: ${poolName}, with error ${e}`);
134
214
  }
135
215
  return undefined;
136
216
  }
137
- async function getCoinAmountsFromLiquidity(poolName, liquidity) {
138
- const cetus_pool = await getCetusPool(poolName);
139
- const cetusInvestor = await getCetusInvestor(poolName);
140
- const upper_bound = 443636;
141
- let lower_tick = Number(cetusInvestor.content.fields.lower_tick);
142
- let upper_tick = Number(cetusInvestor.content.fields.upper_tick);
143
- if (lower_tick > upper_bound) {
144
- lower_tick = -~(lower_tick - 1);
145
- }
146
- if (upper_tick > upper_bound) {
147
- upper_tick = -~(upper_tick - 1);
148
- }
149
- if (cetus_pool) {
150
- const liquidityInt = Math.floor(liquidity);
151
- const coin_amounts = cetus_sui_clmm_sdk_1.ClmmPoolUtil.getCoinAmountFromLiquidity(new bn_js_1.default(`${liquidityInt}`), new bn_js_1.default(cetus_pool.content.fields.current_sqrt_price), cetus_sui_clmm_sdk_1.TickMath.tickIndexToSqrtPriceX64(lower_tick), cetus_sui_clmm_sdk_1.TickMath.tickIndexToSqrtPriceX64(upper_tick), true);
152
- return [coin_amounts.coinA.toNumber(), coin_amounts.coinB.toNumber()];
217
+ const poolCache = new simpleCache_js_1.SimpleCache();
218
+ const poolPromiseCache = new simpleCache_js_1.SimpleCache();
219
+ async function getMultiPool() {
220
+ let pools = Object.keys(maps_js_1.poolInfo);
221
+ pools = pools.filter((pool) => {
222
+ return maps_js_1.poolInfo[pool].poolId !== "";
223
+ });
224
+ const poolIds = pools.map((pool) => {
225
+ return maps_js_1.poolInfo[pool].poolId;
226
+ });
227
+ try {
228
+ const o = await (0, client_js_1.getSuiClient)().multiGetObjects({
229
+ ids: poolIds,
230
+ options: {
231
+ showContent: true,
232
+ },
233
+ });
234
+ for (let i = 0; i < pools.length; i = i + 1) {
235
+ const poolData = o[i].data;
236
+ const cacheKey = `pool_${maps_js_1.poolInfo[pools[i]].poolId}`;
237
+ poolCache.set(cacheKey, poolData);
238
+ }
153
239
  }
154
- else {
155
- return [0, 0];
240
+ catch (e) {
241
+ console.error(`Error getting multiPools`);
156
242
  }
157
243
  }
158
- const poolCache = new simpleCache_js_1.SimpleCache();
159
- const poolPromiseCache = new simpleCache_js_1.SimpleCache();
160
- async function getPool(poolName, ignoreCache = false) {
244
+ async function getPool(poolName, ignoreCache) {
245
+ const suiClient = (0, client_js_1.getSuiClient)();
161
246
  const cacheKey = `pool_${maps_js_1.poolInfo[poolName.toUpperCase()].poolId}`;
162
247
  if (ignoreCache) {
163
248
  poolCache.delete(cacheKey);
@@ -175,7 +260,6 @@ async function getPool(poolName, ignoreCache = false) {
175
260
  }
176
261
  // If not, create a new promise and cache it
177
262
  poolPromise = (async () => {
178
- const suiClient = (0, client_js_1.getSuiClient)();
179
263
  try {
180
264
  const o = await suiClient.getObject({
181
265
  id: maps_js_1.poolInfo[poolName].poolId,
@@ -203,8 +287,34 @@ async function getPool(poolName, ignoreCache = false) {
203
287
  }
204
288
  const cetusPoolCache = new simpleCache_js_1.SimpleCache();
205
289
  const cetusPoolPromiseCache = new simpleCache_js_1.SimpleCache();
206
- async function getCetusPool(poolName, ignoreCache = false) {
207
- const cacheKey = `pool_${maps_js_1.cetusPoolMap[poolName.toUpperCase()]}`;
290
+ async function getMultiParentPool() {
291
+ let pools = Object.keys(maps_js_1.poolInfo);
292
+ pools = pools.filter((pool) => {
293
+ return maps_js_1.poolInfo[pool].poolId !== "";
294
+ });
295
+ const poolIds = pools.map((pool) => {
296
+ return maps_js_1.poolInfo[pool].parentPoolId;
297
+ });
298
+ try {
299
+ const o = await (0, client_js_1.getSuiClient)().multiGetObjects({
300
+ ids: poolIds,
301
+ options: {
302
+ showContent: true,
303
+ },
304
+ });
305
+ for (let i = 0; i < pools.length; i = i + 1) {
306
+ const poolData = o[i].data;
307
+ const cacheKey = `pool_${maps_js_1.poolInfo[pools[i]].parentPoolId}`;
308
+ cetusPoolCache.set(cacheKey, poolData);
309
+ }
310
+ }
311
+ catch (e) {
312
+ console.error(`Error getting multiPools`);
313
+ }
314
+ }
315
+ async function getParentPool(poolName, ignoreCache) {
316
+ const suiClient = (0, client_js_1.getSuiClient)();
317
+ const cacheKey = `pool_${index_js_1.cetusPoolMap[poolName.toUpperCase()]}`;
208
318
  if (ignoreCache) {
209
319
  cetusPoolCache.delete(cacheKey);
210
320
  cetusPoolPromiseCache.delete(cacheKey);
@@ -222,9 +332,13 @@ async function getCetusPool(poolName, ignoreCache = false) {
222
332
  // If not, create a new promise and cache it
223
333
  cetusPoolPromise = (async () => {
224
334
  try {
225
- const suiClient = (0, client_js_1.getSuiClient)();
335
+ const id = maps_js_1.poolInfo[poolName]
336
+ ? maps_js_1.poolInfo[poolName].parentPoolId
337
+ : index_js_1.cetusPoolMap[poolName]
338
+ ? index_js_1.cetusPoolMap[poolName]
339
+ : index_js_1.bluefinPoolMap[poolName];
226
340
  const o = await suiClient.getObject({
227
- id: maps_js_1.cetusPoolMap[poolName.toUpperCase()],
341
+ id: id,
228
342
  options: {
229
343
  showContent: true,
230
344
  },
@@ -236,7 +350,6 @@ async function getCetusPool(poolName, ignoreCache = false) {
236
350
  }
237
351
  catch (e) {
238
352
  console.error(`getCetusPool failed for poolName: ${poolName}`);
239
- console.error(e);
240
353
  return undefined;
241
354
  }
242
355
  finally {
@@ -248,176 +361,147 @@ async function getCetusPool(poolName, ignoreCache = false) {
248
361
  cetusPoolPromiseCache.set(cacheKey, cetusPoolPromise);
249
362
  return cetusPoolPromise;
250
363
  }
251
- const cetusInvestorCache = new simpleCache_js_1.SimpleCache();
252
- const cetusInvestorPromiseCache = new simpleCache_js_1.SimpleCache();
253
- async function getCetusInvestor(poolName, ignoreCache = false) {
364
+ const investorCache = new simpleCache_js_1.SimpleCache();
365
+ const investorPromiseCache = new simpleCache_js_1.SimpleCache();
366
+ async function getMultiInvestor() {
367
+ let pools = Object.keys(maps_js_1.poolInfo);
368
+ pools = pools.filter((pool) => {
369
+ return maps_js_1.poolInfo[pool].investorId !== "";
370
+ });
371
+ const investorIds = pools.map((pool) => {
372
+ return maps_js_1.poolInfo[pool].investorId;
373
+ });
374
+ try {
375
+ const o = await (0, client_js_1.getSuiClient)().multiGetObjects({
376
+ ids: investorIds,
377
+ options: {
378
+ showContent: true,
379
+ },
380
+ });
381
+ for (let i = 0; i < pools.length; i = i + 1) {
382
+ const investorData = o[i].data;
383
+ const cacheKey = `investor_${maps_js_1.poolInfo[pools[i]].investorId}`;
384
+ investorCache.set(cacheKey, investorData);
385
+ }
386
+ }
387
+ catch (e) {
388
+ console.error(`Error getting multiPools`);
389
+ }
390
+ }
391
+ async function getInvestor(poolName, ignoreCache) {
392
+ const suiClient = (0, client_js_1.getSuiClient)();
254
393
  const cacheKey = `investor_${maps_js_1.poolInfo[poolName.toUpperCase()].investorId}`;
255
394
  if (ignoreCache) {
256
- cetusInvestorCache.delete(cacheKey);
257
- cetusInvestorPromiseCache.delete(cacheKey);
395
+ investorCache.delete(cacheKey);
396
+ investorPromiseCache.delete(cacheKey);
258
397
  }
259
398
  // Check if the investor is already in the cache
260
- const cachedInvestor = cetusInvestorCache.get(cacheKey);
399
+ const cachedInvestor = investorCache.get(cacheKey);
261
400
  if (cachedInvestor) {
262
401
  return cachedInvestor;
263
402
  }
264
403
  // Check if there is already a promise in the cache
265
- let cetusInvestorPromise = cetusInvestorPromiseCache.get(cacheKey);
404
+ let cetusInvestorPromise = investorPromiseCache.get(cacheKey);
266
405
  if (cetusInvestorPromise) {
267
406
  return cetusInvestorPromise;
268
407
  }
269
408
  // If not, create a new promise and cache it
270
409
  cetusInvestorPromise = (async () => {
271
410
  try {
272
- const suiClient = (0, client_js_1.getSuiClient)();
273
411
  const o = await suiClient.getObject({
274
412
  id: maps_js_1.poolInfo[poolName.toUpperCase()].investorId,
275
413
  options: {
276
414
  showContent: true,
277
415
  },
278
416
  });
279
- const cetus_investor = o.data;
417
+ let cetusInvestor;
418
+ if (maps_js_1.poolInfo[poolName].parentProtocolName == "NAVI") {
419
+ cetusInvestor = o.data;
420
+ }
421
+ else if (maps_js_1.poolInfo[poolName].parentProtocolName == "BUCKET") {
422
+ cetusInvestor = o.data;
423
+ }
424
+ else if (maps_js_1.poolInfo[poolName].parentProtocolName == "BLUEFIN") {
425
+ cetusInvestor = o.data;
426
+ }
427
+ else {
428
+ cetusInvestor = o.data;
429
+ }
280
430
  // Cache the investor object
281
- cetusInvestorCache.set(cacheKey, cetus_investor);
282
- return cetus_investor;
431
+ investorCache.set(cacheKey, cetusInvestor);
432
+ return cetusInvestor;
283
433
  }
284
434
  catch (e) {
285
- console.error(`getCetusInvestor failed for pool: ${poolName}`);
435
+ console.error(`getInvestor failed for pool: ${poolName}`);
286
436
  return undefined;
287
437
  }
288
438
  finally {
289
439
  // Remove the promise from the cache after it resolves
290
- cetusInvestorPromiseCache.delete(cacheKey);
440
+ investorPromiseCache.delete(cacheKey);
291
441
  }
292
442
  })();
293
443
  // Cache the promise
294
- cetusInvestorPromiseCache.set(cacheKey, cetusInvestorPromise);
444
+ investorPromiseCache.set(cacheKey, cetusInvestorPromise);
295
445
  return cetusInvestorPromise;
296
446
  }
297
- const naviInvestorCache = new simpleCache_js_1.SimpleCache();
298
- const naviInvestorPromiseCache = new simpleCache_js_1.SimpleCache();
299
- async function getNaviInvestor(poolName, ignoreCache = false) {
300
- const cacheKey = `investor_${maps_js_1.poolInfo[poolName.toUpperCase()].investorId}`;
301
- if (ignoreCache) {
302
- naviInvestorCache.delete(cacheKey);
303
- naviInvestorPromiseCache.delete(cacheKey);
304
- }
305
- // Check if the investor is already in the cache
306
- const cachedInvestor = naviInvestorCache.get(cacheKey);
307
- if (cachedInvestor) {
308
- return cachedInvestor;
309
- }
310
- // Check if there is already a promise in the cache
311
- let naviInvestorPromise = naviInvestorPromiseCache.get(cacheKey);
312
- if (naviInvestorPromise) {
313
- return naviInvestorPromise;
314
- }
315
- // If not, create a new promise and cache it
316
- naviInvestorPromise = (async () => {
317
- try {
318
- const suiClient = (0, client_js_1.getSuiClient)();
319
- const o = await suiClient.getObject({
320
- id: maps_js_1.poolInfo[poolName.toUpperCase()].investorId,
321
- options: {
322
- showContent: true,
323
- },
324
- });
325
- const navi_investor = o.data;
326
- // Cache the investor object
327
- naviInvestorCache.set(cacheKey, navi_investor);
328
- return navi_investor;
329
- }
330
- catch (e) {
331
- console.error(`getNaviInvestor failed for pool: ${poolName}`);
332
- return undefined;
333
- }
334
- finally {
335
- // Remove the promise from the cache after it resolves
336
- naviInvestorPromiseCache.delete(cacheKey);
447
+ async function fetchVoloExchangeRate() {
448
+ const apiUrl = "https://open-api.naviprotocol.io/api/volo/stats";
449
+ const NaviVoloDetails = await fetch(apiUrl)
450
+ .then(async (response) => {
451
+ if (!response.ok) {
452
+ throw new Error(`HTTP error! status: ${response.status}`);
337
453
  }
338
- })();
339
- // Cache the promise
340
- naviInvestorPromiseCache.set(cacheKey, naviInvestorPromise);
341
- return naviInvestorPromise;
454
+ const data = (await response.json()); // Parse the JSON response
455
+ return data;
456
+ })
457
+ .catch((error) => {
458
+ console.log("failed to fetch Navi-volo details", error);
459
+ throw error;
460
+ });
461
+ return NaviVoloDetails;
342
462
  }
343
- const naviVoloExchangeRateCache = new simpleCache_js_1.SimpleCache();
344
- const naviVoloExchangeRatePromiseCache = new simpleCache_js_1.SimpleCache();
345
- async function fetchVoloExchangeRate(ignoreCache = false) {
346
- const apiUrl = "https://open-api.naviprotocol.io/api/volo/stats";
347
- let NaviVoloDetails;
348
- if (ignoreCache) {
349
- naviVoloExchangeRateCache.clear();
350
- naviVoloExchangeRatePromiseCache.delete(apiUrl);
463
+ async function getCoinAmountsFromLiquidity(poolName, liquidity, ignoreCache) {
464
+ const clmmPool = await getParentPool(poolName, ignoreCache);
465
+ const investor = (await getInvestor(poolName, ignoreCache));
466
+ const upper_bound = 443636;
467
+ let lower_tick = Number(investor.content.fields.lower_tick);
468
+ let upper_tick = Number(investor.content.fields.upper_tick);
469
+ if (lower_tick > upper_bound) {
470
+ lower_tick = -~(lower_tick - 1);
351
471
  }
352
- const cachedResponse = naviVoloExchangeRateCache.get(apiUrl);
353
- if (cachedResponse) {
354
- NaviVoloDetails = cachedResponse;
472
+ if (upper_tick > upper_bound) {
473
+ upper_tick = -~(upper_tick - 1);
474
+ }
475
+ if (clmmPool) {
476
+ const liquidityInt = Math.floor(parseFloat(liquidity));
477
+ const coin_amounts = cetus_sui_clmm_sdk_1.ClmmPoolUtil.getCoinAmountFromLiquidity(new bn_js_1.default(`${liquidityInt}`), new bn_js_1.default(clmmPool.content.fields.current_sqrt_price), cetus_sui_clmm_sdk_1.TickMath.tickIndexToSqrtPriceX64(lower_tick), cetus_sui_clmm_sdk_1.TickMath.tickIndexToSqrtPriceX64(upper_tick), true);
478
+ return [coin_amounts.coinA.toString(), coin_amounts.coinB.toString()];
355
479
  }
356
480
  else {
357
- let cachedPromise = naviVoloExchangeRatePromiseCache.get(apiUrl);
358
- if (!cachedPromise) {
359
- cachedPromise = fetch(apiUrl)
360
- .then(async (response) => {
361
- if (!response.ok) {
362
- throw new Error(`HTTP error! status: ${response.status}`);
363
- }
364
- const data = (await response.json()); // Parse the JSON response
365
- naviVoloExchangeRateCache.set(apiUrl, data); // Cache the response
366
- naviVoloExchangeRatePromiseCache.delete(apiUrl); // Remove the promise from the cache
367
- return data;
368
- })
369
- .catch((error) => {
370
- naviVoloExchangeRatePromiseCache.delete(apiUrl); // Ensure the promise is removed on error
371
- throw error;
372
- });
373
- naviVoloExchangeRatePromiseCache.set(apiUrl, cachedPromise);
374
- NaviVoloDetails = await cachedPromise;
375
- }
376
- return cachedPromise;
481
+ return ["0", "0"];
377
482
  }
378
- return NaviVoloDetails;
379
483
  }
380
- async function multiGetNaviInvestor(poolNames, ignoreCache = false) {
484
+ async function multiGetNaviInvestor(poolNames) {
381
485
  const results = {};
382
- const missingPoolInvestorIds = [];
383
- const missingPoolNames = [];
486
+ const poolInvestorIds = [];
384
487
  for (const poolName of poolNames) {
385
- const cacheKey = `investor_${maps_js_1.poolInfo[poolName.toUpperCase()].investorId}`;
386
- if (ignoreCache) {
387
- naviInvestorCache.delete(cacheKey);
388
- naviInvestorPromiseCache.delete(cacheKey);
389
- }
390
- // Check if the investor is already cached
391
- const cachedInvestor = naviInvestorCache.get(cacheKey);
392
- if (cachedInvestor) {
393
- results[poolName] = cachedInvestor;
394
- continue;
395
- }
396
- else {
397
- // Add to missing list if not in cache
398
- if (maps_js_1.poolInfo[poolName.toUpperCase()].investorId) {
399
- missingPoolNames.push(poolName);
400
- missingPoolInvestorIds.push(maps_js_1.poolInfo[poolName.toUpperCase()].investorId);
401
- }
402
- }
488
+ poolInvestorIds.push(maps_js_1.poolInfo[poolName.toUpperCase()].investorId);
403
489
  }
404
490
  try {
405
491
  const suiClient = (0, client_js_1.getSuiClient)();
406
492
  const objects = await suiClient.multiGetObjects({
407
- ids: missingPoolInvestorIds,
493
+ ids: poolInvestorIds,
408
494
  options: { showContent: true },
409
495
  });
410
496
  for (let i = 0; i < objects.length; i++) {
411
497
  const investor = objects[i].data;
412
- const cacheKey = `investor_${missingPoolInvestorIds[i]}`;
413
- naviInvestorCache.set(cacheKey, investor);
414
- results[missingPoolNames[i]] = investor;
498
+ results[poolNames[i]] = investor;
415
499
  }
416
500
  return results;
417
501
  }
418
502
  catch (err) {
419
503
  //improve
420
- console.error("multiGetNaviInvestor failed for poolNames: ", missingPoolNames.join(", "));
504
+ console.error("multiGetNaviInvestor failed for poolNames: ", poolNames.join(", "));
421
505
  throw err;
422
506
  }
423
507
  }