@ichidao/ichi-vaults-sdk 0.1.42 → 2.0.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 (215) hide show
  1. package/README.md +61 -208
  2. package/dist/abis/types/AlgebraIntegralPool.d.ts +702 -793
  3. package/dist/abis/types/AlgebraPool.d.ts +693 -730
  4. package/dist/abis/types/ClPool.d.ts +789 -871
  5. package/dist/abis/types/DepositGuard.d.ts +193 -178
  6. package/dist/abis/types/DepositGuardWithHtsWrapping.d.ts +300 -297
  7. package/dist/abis/types/ERC20.d.ts +120 -147
  8. package/dist/abis/types/ERC20Wrapper.d.ts +147 -155
  9. package/dist/abis/types/IchiVault.d.ts +598 -728
  10. package/dist/abis/types/MultiFeeDistributer.d.ts +314 -423
  11. package/dist/abis/types/Multicall.d.ts +58 -73
  12. package/dist/abis/types/UniswapV3Pool.d.ts +654 -693
  13. package/dist/abis/types/common.d.ts +40 -12
  14. package/dist/abis/types/factories/AlgebraIntegralPool__factory.d.ts +2 -3
  15. package/dist/abis/types/factories/AlgebraIntegralPool__factory.js +10 -13
  16. package/dist/abis/types/factories/AlgebraIntegralPool__factory.js.map +1 -1
  17. package/dist/abis/types/factories/AlgebraPool__factory.d.ts +2 -3
  18. package/dist/abis/types/factories/AlgebraPool__factory.js +10 -13
  19. package/dist/abis/types/factories/AlgebraPool__factory.js.map +1 -1
  20. package/dist/abis/types/factories/ClPool__factory.d.ts +2 -3
  21. package/dist/abis/types/factories/ClPool__factory.js +10 -13
  22. package/dist/abis/types/factories/ClPool__factory.js.map +1 -1
  23. package/dist/abis/types/factories/DepositGuardWithHtsWrapping__factory.d.ts +2 -3
  24. package/dist/abis/types/factories/DepositGuardWithHtsWrapping__factory.js +10 -13
  25. package/dist/abis/types/factories/DepositGuardWithHtsWrapping__factory.js.map +1 -1
  26. package/dist/abis/types/factories/DepositGuard__factory.d.ts +2 -3
  27. package/dist/abis/types/factories/DepositGuard__factory.js +10 -13
  28. package/dist/abis/types/factories/DepositGuard__factory.js.map +1 -1
  29. package/dist/abis/types/factories/ERC20Wrapper__factory.d.ts +2 -3
  30. package/dist/abis/types/factories/ERC20Wrapper__factory.js +10 -13
  31. package/dist/abis/types/factories/ERC20Wrapper__factory.js.map +1 -1
  32. package/dist/abis/types/factories/ERC20__factory.d.ts +2 -3
  33. package/dist/abis/types/factories/ERC20__factory.js +10 -13
  34. package/dist/abis/types/factories/ERC20__factory.js.map +1 -1
  35. package/dist/abis/types/factories/IchiVault__factory.d.ts +2 -3
  36. package/dist/abis/types/factories/IchiVault__factory.js +10 -13
  37. package/dist/abis/types/factories/IchiVault__factory.js.map +1 -1
  38. package/dist/abis/types/factories/MultiFeeDistributer__factory.d.ts +2 -3
  39. package/dist/abis/types/factories/MultiFeeDistributer__factory.js +10 -13
  40. package/dist/abis/types/factories/MultiFeeDistributer__factory.js.map +1 -1
  41. package/dist/abis/types/factories/Multicall__factory.d.ts +2 -3
  42. package/dist/abis/types/factories/Multicall__factory.js +10 -13
  43. package/dist/abis/types/factories/Multicall__factory.js.map +1 -1
  44. package/dist/abis/types/factories/UniswapV3Pool__factory.d.ts +2 -3
  45. package/dist/abis/types/factories/UniswapV3Pool__factory.js +10 -13
  46. package/dist/abis/types/factories/UniswapV3Pool__factory.js.map +1 -1
  47. package/dist/src/__tests__/index.test.js +376 -700
  48. package/dist/src/__tests__/index.test.js.map +1 -1
  49. package/dist/src/abis/types/AlgebraIntegralPool.d.ts +702 -793
  50. package/dist/src/abis/types/AlgebraPool.d.ts +693 -730
  51. package/dist/src/abis/types/ClPool.d.ts +789 -871
  52. package/dist/src/abis/types/DepositGuard.d.ts +193 -178
  53. package/dist/src/abis/types/DepositGuardWithHtsWrapping.d.ts +300 -297
  54. package/dist/src/abis/types/ERC20.d.ts +120 -147
  55. package/dist/src/abis/types/ERC20Wrapper.d.ts +147 -155
  56. package/dist/src/abis/types/IchiVault.d.ts +598 -728
  57. package/dist/src/abis/types/MultiFeeDistributer.d.ts +314 -423
  58. package/dist/src/abis/types/Multicall.d.ts +58 -73
  59. package/dist/src/abis/types/UniswapV3Pool.d.ts +654 -693
  60. package/dist/src/abis/types/common.d.ts +40 -12
  61. package/dist/src/abis/types/factories/AlgebraIntegralPool__factory.d.ts +2 -3
  62. package/dist/src/abis/types/factories/AlgebraPool__factory.d.ts +2 -3
  63. package/dist/src/abis/types/factories/ClPool__factory.d.ts +2 -3
  64. package/dist/src/abis/types/factories/DepositGuardWithHtsWrapping__factory.d.ts +2 -3
  65. package/dist/src/abis/types/factories/DepositGuard__factory.d.ts +2 -3
  66. package/dist/src/abis/types/factories/ERC20Wrapper__factory.d.ts +2 -3
  67. package/dist/src/abis/types/factories/ERC20__factory.d.ts +2 -3
  68. package/dist/src/abis/types/factories/IchiVault__factory.d.ts +2 -3
  69. package/dist/src/abis/types/factories/MultiFeeDistributer__factory.d.ts +2 -3
  70. package/dist/src/abis/types/factories/Multicall__factory.d.ts +2 -3
  71. package/dist/src/abis/types/factories/UniswapV3Pool__factory.d.ts +2 -3
  72. package/dist/src/contracts/index.d.ts +1 -1
  73. package/dist/src/contracts/index.js +12 -12
  74. package/dist/src/contracts/index.js.map +1 -1
  75. package/dist/src/functions/_totalBalances.d.ts +2 -3
  76. package/dist/src/functions/_totalBalances.js +46 -117
  77. package/dist/src/functions/_totalBalances.js.map +1 -1
  78. package/dist/src/functions/_vaultEvents.js +347 -488
  79. package/dist/src/functions/_vaultEvents.js.map +1 -1
  80. package/dist/src/functions/_withdrawHelpers.d.ts +2 -3
  81. package/dist/src/functions/_withdrawHelpers.js +13 -62
  82. package/dist/src/functions/_withdrawHelpers.js.map +1 -1
  83. package/dist/src/functions/calculateApr.d.ts +1 -1
  84. package/dist/src/functions/calculateApr.js +77 -150
  85. package/dist/src/functions/calculateApr.js.map +1 -1
  86. package/dist/src/functions/calculateDtr.d.ts +1 -1
  87. package/dist/src/functions/calculateDtr.js +84 -169
  88. package/dist/src/functions/calculateDtr.js.map +1 -1
  89. package/dist/src/functions/calculateFees.d.ts +1 -1
  90. package/dist/src/functions/calculateFees.js +97 -172
  91. package/dist/src/functions/calculateFees.js.map +1 -1
  92. package/dist/src/functions/chains.js +6 -6
  93. package/dist/src/functions/chains.js.map +1 -1
  94. package/dist/src/functions/claimRewards.d.ts +3 -4
  95. package/dist/src/functions/claimRewards.js +29 -92
  96. package/dist/src/functions/claimRewards.js.map +1 -1
  97. package/dist/src/functions/deposit.d.ts +7 -9
  98. package/dist/src/functions/deposit.js +199 -364
  99. package/dist/src/functions/deposit.js.map +1 -1
  100. package/dist/src/functions/depositWithHtsWrapping.d.ts +4 -6
  101. package/dist/src/functions/depositWithHtsWrapping.js +109 -226
  102. package/dist/src/functions/depositWithHtsWrapping.js.map +1 -1
  103. package/dist/src/functions/getFeeAprs.d.ts +1 -1
  104. package/dist/src/functions/getFeeAprs.js +55 -111
  105. package/dist/src/functions/getFeeAprs.js.map +1 -1
  106. package/dist/src/functions/priceFromPool.d.ts +3 -4
  107. package/dist/src/functions/priceFromPool.js +99 -208
  108. package/dist/src/functions/priceFromPool.js.map +1 -1
  109. package/dist/src/functions/rewardInfo.js +79 -159
  110. package/dist/src/functions/rewardInfo.js.map +1 -1
  111. package/dist/src/functions/totalBalances.d.ts +2 -3
  112. package/dist/src/functions/totalBalances.js +14 -67
  113. package/dist/src/functions/totalBalances.js.map +1 -1
  114. package/dist/src/functions/userBalances.d.ts +2 -3
  115. package/dist/src/functions/userBalances.js +244 -368
  116. package/dist/src/functions/userBalances.js.map +1 -1
  117. package/dist/src/functions/userRewards.d.ts +1 -1
  118. package/dist/src/functions/userRewards.js +87 -145
  119. package/dist/src/functions/userRewards.js.map +1 -1
  120. package/dist/src/functions/vault.d.ts +1 -1
  121. package/dist/src/functions/vault.js +203 -383
  122. package/dist/src/functions/vault.js.map +1 -1
  123. package/dist/src/functions/vaultBasics.js +6 -7
  124. package/dist/src/functions/vaultBasics.js.map +1 -1
  125. package/dist/src/functions/vaultMetrics.d.ts +1 -1
  126. package/dist/src/functions/vaultMetrics.js +111 -189
  127. package/dist/src/functions/vaultMetrics.js.map +1 -1
  128. package/dist/src/functions/vaultPositions.d.ts +1 -1
  129. package/dist/src/functions/vaultPositions.js +69 -134
  130. package/dist/src/functions/vaultPositions.js.map +1 -1
  131. package/dist/src/functions/vaultTvl.d.ts +1 -1
  132. package/dist/src/functions/vaultTvl.js +80 -165
  133. package/dist/src/functions/vaultTvl.js.map +1 -1
  134. package/dist/src/functions/withdraw.d.ts +6 -8
  135. package/dist/src/functions/withdraw.js +165 -295
  136. package/dist/src/functions/withdraw.js.map +1 -1
  137. package/dist/src/functions/withdrawWithErc20Wrapping.d.ts +2 -4
  138. package/dist/src/functions/withdrawWithErc20Wrapping.js +59 -126
  139. package/dist/src/functions/withdrawWithErc20Wrapping.js.map +1 -1
  140. package/dist/src/graphql/constants.js +211 -212
  141. package/dist/src/graphql/constants.js.map +1 -1
  142. package/dist/src/graphql/functions.js +30 -102
  143. package/dist/src/graphql/functions.js.map +1 -1
  144. package/dist/src/graphql/queries.js +407 -42
  145. package/dist/src/graphql/queries.js.map +1 -1
  146. package/dist/src/index.cjs.js +3 -2
  147. package/dist/src/index.d.ts +1 -0
  148. package/dist/src/index.esm.js +3 -2
  149. package/dist/src/index.js +1 -0
  150. package/dist/src/index.js.map +1 -1
  151. package/dist/src/src/contracts/index.d.ts +1 -1
  152. package/dist/src/src/functions/_totalBalances.d.ts +2 -3
  153. package/dist/src/src/functions/_withdrawHelpers.d.ts +2 -3
  154. package/dist/src/src/functions/calculateApr.d.ts +1 -1
  155. package/dist/src/src/functions/calculateDtr.d.ts +1 -1
  156. package/dist/src/src/functions/calculateFees.d.ts +1 -1
  157. package/dist/src/src/functions/claimRewards.d.ts +3 -4
  158. package/dist/src/src/functions/deposit.d.ts +7 -9
  159. package/dist/src/src/functions/depositWithHtsWrapping.d.ts +4 -6
  160. package/dist/src/src/functions/getFeeAprs.d.ts +1 -1
  161. package/dist/src/src/functions/priceFromPool.d.ts +3 -4
  162. package/dist/src/src/functions/totalBalances.d.ts +2 -3
  163. package/dist/src/src/functions/userBalances.d.ts +2 -3
  164. package/dist/src/src/functions/userRewards.d.ts +1 -1
  165. package/dist/src/src/functions/vault.d.ts +1 -1
  166. package/dist/src/src/functions/vaultMetrics.d.ts +1 -1
  167. package/dist/src/src/functions/vaultPositions.d.ts +1 -1
  168. package/dist/src/src/functions/vaultTvl.d.ts +1 -1
  169. package/dist/src/src/functions/withdraw.d.ts +6 -8
  170. package/dist/src/src/functions/withdrawWithErc20Wrapping.d.ts +2 -4
  171. package/dist/src/src/index.d.ts +1 -0
  172. package/dist/src/src/types/calculateGasMargin.d.ts +1 -2
  173. package/dist/src/src/types/index.d.ts +11 -13
  174. package/dist/src/src/utils/amountWithSlippage.d.ts +1 -2
  175. package/dist/src/src/utils/config/addresses.d.ts +1 -1
  176. package/dist/src/src/utils/formatBigInt.d.ts +1 -2
  177. package/dist/src/src/utils/getConfigByFactory.d.ts +33 -0
  178. package/dist/src/src/utils/getPrice.d.ts +1 -2
  179. package/dist/src/src/utils/multicallUtils.d.ts +9 -10
  180. package/dist/src/src/utils/parseBigInt.d.ts +1 -2
  181. package/dist/src/types/calculateGasMargin.d.ts +1 -2
  182. package/dist/src/types/calculateGasMargin.js +4 -5
  183. package/dist/src/types/calculateGasMargin.js.map +1 -1
  184. package/dist/src/types/index.d.ts +11 -13
  185. package/dist/src/types/index.js.map +1 -1
  186. package/dist/src/utils/amountWithSlippage.d.ts +1 -2
  187. package/dist/src/utils/amountWithSlippage.js +1 -1
  188. package/dist/src/utils/amountWithSlippage.js.map +1 -1
  189. package/dist/src/utils/cache.js +2 -2
  190. package/dist/src/utils/cache.js.map +1 -1
  191. package/dist/src/utils/config/addresses.d.ts +1 -1
  192. package/dist/src/utils/config/addresses.js +267 -268
  193. package/dist/src/utils/config/addresses.js.map +1 -1
  194. package/dist/src/utils/formatBigInt.d.ts +1 -2
  195. package/dist/src/utils/formatBigInt.js +4 -5
  196. package/dist/src/utils/formatBigInt.js.map +1 -1
  197. package/dist/src/utils/getConfigByFactory.d.ts +33 -0
  198. package/dist/src/utils/getConfigByFactory.js +79 -0
  199. package/dist/src/utils/getConfigByFactory.js.map +1 -0
  200. package/dist/src/utils/getGraphUrls.js +8 -12
  201. package/dist/src/utils/getGraphUrls.js.map +1 -1
  202. package/dist/src/utils/getPrice.d.ts +1 -2
  203. package/dist/src/utils/getPrice.js +5 -6
  204. package/dist/src/utils/getPrice.js.map +1 -1
  205. package/dist/src/utils/isVelodrome.js +2 -3
  206. package/dist/src/utils/isVelodrome.js.map +1 -1
  207. package/dist/src/utils/multicallUtils.d.ts +9 -10
  208. package/dist/src/utils/multicallUtils.js +38 -93
  209. package/dist/src/utils/multicallUtils.js.map +1 -1
  210. package/dist/src/utils/parseBigInt.d.ts +1 -2
  211. package/dist/src/utils/parseBigInt.js +5 -7
  212. package/dist/src/utils/parseBigInt.js.map +1 -1
  213. package/dist/src/utils/positionKey.js +2 -4
  214. package/dist/src/utils/positionKey.js.map +1 -1
  215. package/package.json +5 -17
@@ -1,519 +1,378 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.getVaultStateAt = exports._getAllVaultEvents = exports._getWithdraws = exports._getDeposits = exports._getFeesCollectedEvents = exports._getRebalances = exports._getAllEvents = void 0;
43
7
  // eslint-disable import/no-cycle
44
- var constants_1 = require("../graphql/constants");
45
- var queries_1 = require("../graphql/queries");
46
- var timestamps_1 = require("../utils/timestamps");
47
- var getGraphUrls_1 = require("../utils/getGraphUrls");
48
- var cache_1 = __importDefault(require("../utils/cache"));
49
- var functions_1 = require("../graphql/functions");
50
- function _getAllEvents(vaultAddress, chainId, dex, days) {
51
- var _a;
52
- return __awaiter(this, void 0, void 0, function () {
53
- var key, cachedData, ttl, _b, publishedUrl, url, currTimestamp, startTimestamp, supportsCollectFees, query, allEvents, endOfData, lastTimestamps, hasMoreData, result, error_1, error2_1;
54
- return __generator(this, function (_c) {
55
- switch (_c.label) {
56
- case 0:
57
- key = "allevents-".concat(chainId, "-").concat(vaultAddress, "-").concat(days);
58
- cachedData = cache_1.default.get(key);
59
- if (cachedData) {
60
- return [2 /*return*/, cachedData];
61
- }
62
- ttl = 120000;
63
- _b = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true), publishedUrl = _b.publishedUrl, url = _b.url;
64
- currTimestamp = Date.now();
65
- startTimestamp = days
66
- ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
67
- : '0';
68
- supportsCollectFees = (_a = constants_1.graphUrls[chainId][dex]) === null || _a === void 0 ? void 0 : _a.supportsCollectFees;
69
- query = supportsCollectFees ? queries_1.allEventsQuery : queries_1.allEventsNoCollectFeesQuery;
70
- allEvents = [];
71
- endOfData = false;
72
- lastTimestamps = {
73
- rebalances: startTimestamp,
74
- collectFees: startTimestamp,
75
- deposits: startTimestamp,
76
- withdraws: startTimestamp,
77
- };
78
- hasMoreData = {
79
- rebalances: true,
80
- collectFees: supportsCollectFees,
81
- deposits: true,
82
- withdraws: true,
83
- };
84
- _c.label = 1;
85
- case 1:
86
- if (!!endOfData) return [3 /*break*/, 12];
87
- result = void 0;
88
- _c.label = 2;
89
- case 2:
90
- _c.trys.push([2, 6, , 11]);
91
- if (!publishedUrl) return [3 /*break*/, 4];
92
- return [4 /*yield*/, (0, functions_1.sendAllEventsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, query(lastTimestamps))];
93
- case 3:
94
- result = _c.sent();
95
- return [3 /*break*/, 5];
96
- case 4: throw new Error("Published URL is invalid for dex ".concat(dex, " on chain ").concat(chainId));
97
- case 5: return [3 /*break*/, 11];
98
- case 6:
99
- error_1 = _c.sent();
100
- if (publishedUrl) {
101
- console.error('Request to published graph URL failed:', error_1);
102
- }
103
- _c.label = 7;
104
- case 7:
105
- _c.trys.push([7, 9, , 10]);
106
- return [4 /*yield*/, (0, functions_1.sendAllEventsQueryRequest)(url, vaultAddress, startTimestamp, query(lastTimestamps))];
107
- case 8:
108
- result = _c.sent();
109
- return [3 /*break*/, 10];
110
- case 9:
111
- error2_1 = _c.sent();
112
- console.error('Request to public graph URL failed:', error2_1);
113
- throw new Error("Could not get events for vault ".concat(vaultAddress, " on chain ").concat(chainId));
114
- case 10: return [3 /*break*/, 11];
115
- case 11:
116
- if (result) {
117
- // Add the results to our collection
118
- if (result.vaultRebalances && result.vaultRebalances.length > 0) {
119
- allEvents.push.apply(allEvents, result.vaultRebalances);
120
- // Update cursor for next query
121
- lastTimestamps.rebalances = result.vaultRebalances[result.vaultRebalances.length - 1].createdAtTimestamp;
122
- // Check if we've reached the end for this entity
123
- if (result.vaultRebalances.length < 1000) {
124
- hasMoreData.rebalances = false;
125
- }
126
- }
127
- else {
128
- hasMoreData.rebalances = false;
129
- }
130
- if (supportsCollectFees && result.vaultCollectFees && result.vaultCollectFees.length > 0) {
131
- allEvents.push.apply(allEvents, result.vaultCollectFees);
132
- // Update cursor for next query
133
- lastTimestamps.collectFees = result.vaultCollectFees[result.vaultCollectFees.length - 1].createdAtTimestamp;
134
- // Check if we've reached the end for this entity
135
- if (result.vaultCollectFees.length < 1000) {
136
- hasMoreData.collectFees = false;
137
- }
138
- }
139
- else {
140
- hasMoreData.collectFees = false;
141
- }
142
- if (result.vaultDeposits && result.vaultDeposits.length > 0) {
143
- allEvents.push.apply(allEvents, result.vaultDeposits);
144
- // Update cursor for next query
145
- lastTimestamps.deposits = result.vaultDeposits[result.vaultDeposits.length - 1].createdAtTimestamp;
146
- // Check if we've reached the end for this entity
147
- if (result.vaultDeposits.length < 1000) {
148
- hasMoreData.deposits = false;
149
- }
150
- }
151
- else {
152
- hasMoreData.deposits = false;
153
- }
154
- if (result.vaultWithdraws && result.vaultWithdraws.length > 0) {
155
- allEvents.push.apply(allEvents, result.vaultWithdraws);
156
- // Update cursor for next query
157
- lastTimestamps.withdraws = result.vaultWithdraws[result.vaultWithdraws.length - 1].createdAtTimestamp;
158
- // Check if we've reached the end for this entity
159
- if (result.vaultWithdraws.length < 1000) {
160
- hasMoreData.withdraws = false;
161
- }
162
- }
163
- else {
164
- hasMoreData.withdraws = false;
165
- }
166
- // Check if we've reached the end for all entity types
167
- endOfData = !Object.values(hasMoreData).some(function (hasMore) { return hasMore; });
168
- }
169
- else {
170
- endOfData = true;
171
- }
172
- return [3 /*break*/, 1];
173
- case 12:
174
- cache_1.default.set(key, allEvents, ttl);
175
- return [2 /*return*/, allEvents];
176
- }
177
- });
178
- });
8
+ const constants_1 = require("../graphql/constants");
9
+ const queries_1 = require("../graphql/queries");
10
+ const timestamps_1 = require("../utils/timestamps");
11
+ const getGraphUrls_1 = require("../utils/getGraphUrls");
12
+ const cache_1 = __importDefault(require("../utils/cache"));
13
+ const functions_1 = require("../graphql/functions");
14
+ async function _getAllEvents(vaultAddress, chainId, dex, days) {
15
+ const key = `allevents-${chainId}-${vaultAddress}-${days}`;
16
+ const cachedData = cache_1.default.get(key);
17
+ if (cachedData) {
18
+ return cachedData;
19
+ }
20
+ const ttl = 120000;
21
+ const { publishedUrl, url } = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true);
22
+ const currTimestamp = Date.now();
23
+ const startTimestamp = days
24
+ ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
25
+ : '0';
26
+ const supportsCollectFees = constants_1.graphUrls[chainId][dex]?.supportsCollectFees;
27
+ const query = supportsCollectFees ? queries_1.allEventsQuery : queries_1.allEventsNoCollectFeesQuery;
28
+ const allEvents = [];
29
+ let endOfData = false;
30
+ // Track last timestamp for each entity type
31
+ const lastTimestamps = {
32
+ rebalances: startTimestamp,
33
+ collectFees: startTimestamp,
34
+ deposits: startTimestamp,
35
+ withdraws: startTimestamp,
36
+ };
37
+ // Track whether each entity type has more data
38
+ const hasMoreData = {
39
+ rebalances: true,
40
+ collectFees: supportsCollectFees,
41
+ deposits: true,
42
+ withdraws: true,
43
+ };
44
+ while (!endOfData) {
45
+ let result;
46
+ try {
47
+ if (publishedUrl) {
48
+ result = await (0, functions_1.sendAllEventsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, query(lastTimestamps));
49
+ }
50
+ else {
51
+ throw new Error(`Published URL is invalid for dex ${dex} on chain ${chainId}`);
52
+ }
53
+ }
54
+ catch (error) {
55
+ if (publishedUrl) {
56
+ console.error('Request to published graph URL failed:', error);
57
+ }
58
+ try {
59
+ result = await (0, functions_1.sendAllEventsQueryRequest)(url, vaultAddress, startTimestamp, query(lastTimestamps));
60
+ }
61
+ catch (error2) {
62
+ console.error('Request to public graph URL failed:', error2);
63
+ throw new Error(`Could not get events for vault ${vaultAddress} on chain ${chainId}`);
64
+ }
65
+ }
66
+ if (result) {
67
+ // Add the results to our collection
68
+ if (result.vaultRebalances && result.vaultRebalances.length > 0) {
69
+ allEvents.push(...result.vaultRebalances);
70
+ // Update cursor for next query
71
+ lastTimestamps.rebalances = result.vaultRebalances[result.vaultRebalances.length - 1].createdAtTimestamp;
72
+ // Check if we've reached the end for this entity
73
+ if (result.vaultRebalances.length < 1000) {
74
+ hasMoreData.rebalances = false;
75
+ }
76
+ }
77
+ else {
78
+ hasMoreData.rebalances = false;
79
+ }
80
+ if (supportsCollectFees && result.vaultCollectFees && result.vaultCollectFees.length > 0) {
81
+ allEvents.push(...result.vaultCollectFees);
82
+ // Update cursor for next query
83
+ lastTimestamps.collectFees = result.vaultCollectFees[result.vaultCollectFees.length - 1].createdAtTimestamp;
84
+ // Check if we've reached the end for this entity
85
+ if (result.vaultCollectFees.length < 1000) {
86
+ hasMoreData.collectFees = false;
87
+ }
88
+ }
89
+ else {
90
+ hasMoreData.collectFees = false;
91
+ }
92
+ if (result.vaultDeposits && result.vaultDeposits.length > 0) {
93
+ allEvents.push(...result.vaultDeposits);
94
+ // Update cursor for next query
95
+ lastTimestamps.deposits = result.vaultDeposits[result.vaultDeposits.length - 1].createdAtTimestamp;
96
+ // Check if we've reached the end for this entity
97
+ if (result.vaultDeposits.length < 1000) {
98
+ hasMoreData.deposits = false;
99
+ }
100
+ }
101
+ else {
102
+ hasMoreData.deposits = false;
103
+ }
104
+ if (result.vaultWithdraws && result.vaultWithdraws.length > 0) {
105
+ allEvents.push(...result.vaultWithdraws);
106
+ // Update cursor for next query
107
+ lastTimestamps.withdraws = result.vaultWithdraws[result.vaultWithdraws.length - 1].createdAtTimestamp;
108
+ // Check if we've reached the end for this entity
109
+ if (result.vaultWithdraws.length < 1000) {
110
+ hasMoreData.withdraws = false;
111
+ }
112
+ }
113
+ else {
114
+ hasMoreData.withdraws = false;
115
+ }
116
+ // Check if we've reached the end for all entity types
117
+ endOfData = !Object.values(hasMoreData).some((hasMore) => hasMore);
118
+ }
119
+ else {
120
+ endOfData = true;
121
+ }
122
+ }
123
+ cache_1.default.set(key, allEvents, ttl);
124
+ return allEvents;
179
125
  }
180
126
  exports._getAllEvents = _getAllEvents;
181
127
  // eslint-disable-next-line no-underscore-dangle
182
- function _getRebalances(vaultAddress, chainId, dex, days) {
183
- return __awaiter(this, void 0, void 0, function () {
184
- var key, cachedData, ttl, _a, publishedUrl, url, currTimestamp, startTimestamp, rebalances, endOfData, lastTimestamp, result, error_2, error2_2;
185
- return __generator(this, function (_b) {
186
- switch (_b.label) {
187
- case 0:
188
- key = "rebalances-".concat(chainId, "-").concat(vaultAddress, "-").concat(days);
189
- cachedData = cache_1.default.get(key);
190
- if (cachedData) {
191
- return [2 /*return*/, cachedData];
192
- }
193
- ttl = 120000;
194
- _a = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true), publishedUrl = _a.publishedUrl, url = _a.url;
195
- currTimestamp = Date.now();
196
- startTimestamp = days
197
- ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
198
- : '0';
199
- rebalances = [];
200
- endOfData = false;
201
- lastTimestamp = startTimestamp;
202
- _b.label = 1;
203
- case 1:
204
- if (!!endOfData) return [3 /*break*/, 12];
205
- result = void 0;
206
- _b.label = 2;
207
- case 2:
208
- _b.trys.push([2, 6, , 11]);
209
- if (!publishedUrl) return [3 /*break*/, 4];
210
- return [4 /*yield*/, (0, functions_1.sendRebalancesQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.rebalancesQuery)(lastTimestamp))];
211
- case 3:
212
- result = _b.sent();
213
- return [3 /*break*/, 5];
214
- case 4: throw new Error("Published URL is invalid for dex ".concat(dex, " on chain ").concat(chainId));
215
- case 5: return [3 /*break*/, 11];
216
- case 6:
217
- error_2 = _b.sent();
218
- if (publishedUrl) {
219
- console.error('Request to published graph URL failed:', error_2);
220
- }
221
- _b.label = 7;
222
- case 7:
223
- _b.trys.push([7, 9, , 10]);
224
- return [4 /*yield*/, (0, functions_1.sendRebalancesQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.rebalancesQuery)(lastTimestamp))];
225
- case 8:
226
- result = _b.sent();
227
- return [3 /*break*/, 10];
228
- case 9:
229
- error2_2 = _b.sent();
230
- console.error('Request to public graph URL failed:', error2_2);
231
- throw new Error("Could not get rebalances for vault ".concat(vaultAddress, " on chain ").concat(chainId));
232
- case 10: return [3 /*break*/, 11];
233
- case 11:
234
- if (result && result.length > 0) {
235
- rebalances.push.apply(rebalances, result);
236
- if (result.length < 1000) {
237
- endOfData = true;
238
- }
239
- else {
240
- // Update the cursor to the timestamp of the last item
241
- lastTimestamp = result[result.length - 1].createdAtTimestamp;
242
- }
243
- }
244
- else {
245
- endOfData = true;
246
- }
247
- return [3 /*break*/, 1];
248
- case 12:
249
- cache_1.default.set(key, rebalances, ttl);
250
- return [2 /*return*/, rebalances];
251
- }
252
- });
253
- });
128
+ async function _getRebalances(vaultAddress, chainId, dex, days) {
129
+ const key = `rebalances-${chainId}-${vaultAddress}-${days}`;
130
+ const cachedData = cache_1.default.get(key);
131
+ if (cachedData) {
132
+ return cachedData;
133
+ }
134
+ const ttl = 120000;
135
+ const { publishedUrl, url } = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true);
136
+ const currTimestamp = Date.now();
137
+ const startTimestamp = days
138
+ ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
139
+ : '0';
140
+ const rebalances = [];
141
+ let endOfData = false;
142
+ let lastTimestamp = startTimestamp;
143
+ while (!endOfData) {
144
+ let result;
145
+ try {
146
+ if (publishedUrl) {
147
+ result = await (0, functions_1.sendRebalancesQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.rebalancesQuery)(lastTimestamp));
148
+ }
149
+ else {
150
+ throw new Error(`Published URL is invalid for dex ${dex} on chain ${chainId}`);
151
+ }
152
+ }
153
+ catch (error) {
154
+ if (publishedUrl) {
155
+ console.error('Request to published graph URL failed:', error);
156
+ }
157
+ try {
158
+ result = await (0, functions_1.sendRebalancesQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.rebalancesQuery)(lastTimestamp));
159
+ }
160
+ catch (error2) {
161
+ console.error('Request to public graph URL failed:', error2);
162
+ throw new Error(`Could not get rebalances for vault ${vaultAddress} on chain ${chainId}`);
163
+ }
164
+ }
165
+ if (result && result.length > 0) {
166
+ rebalances.push(...result);
167
+ if (result.length < 1000) {
168
+ endOfData = true;
169
+ }
170
+ else {
171
+ // Update the cursor to the timestamp of the last item
172
+ lastTimestamp = result[result.length - 1].createdAtTimestamp;
173
+ }
174
+ }
175
+ else {
176
+ endOfData = true;
177
+ }
178
+ }
179
+ cache_1.default.set(key, rebalances, ttl);
180
+ return rebalances;
254
181
  }
255
182
  exports._getRebalances = _getRebalances;
256
183
  // eslint-disable-next-line no-underscore-dangle
257
- function _getFeesCollectedEvents(vaultAddress, chainId, dex, days) {
258
- var _a;
259
- return __awaiter(this, void 0, void 0, function () {
260
- var key, cachedData, ttl, _b, publishedUrl, url, supportsCollectFees, result, currTimestamp, startTimestamp, otherFees, endOfData, lastTimestamp, result, error_3, error2_3;
261
- return __generator(this, function (_c) {
262
- switch (_c.label) {
263
- case 0:
264
- key = "fees-".concat(chainId, "-").concat(vaultAddress, "-").concat(days);
265
- cachedData = cache_1.default.get(key);
266
- if (cachedData) {
267
- return [2 /*return*/, cachedData];
268
- }
269
- ttl = 120000;
270
- _b = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true), publishedUrl = _b.publishedUrl, url = _b.url;
271
- supportsCollectFees = (_a = constants_1.graphUrls[chainId][dex]) === null || _a === void 0 ? void 0 : _a.supportsCollectFees;
272
- if (!supportsCollectFees) {
273
- result = [];
274
- cache_1.default.set(key, result, 24 * 60 * 60 * 1000);
275
- return [2 /*return*/, result];
276
- }
277
- currTimestamp = Date.now();
278
- startTimestamp = days
279
- ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
280
- : '0';
281
- otherFees = [];
282
- endOfData = false;
283
- lastTimestamp = startTimestamp;
284
- _c.label = 1;
285
- case 1:
286
- if (!!endOfData) return [3 /*break*/, 12];
287
- result = void 0;
288
- _c.label = 2;
289
- case 2:
290
- _c.trys.push([2, 6, , 11]);
291
- if (!publishedUrl) return [3 /*break*/, 4];
292
- return [4 /*yield*/, (0, functions_1.sendCollectFeesQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultCollectFeesQuery)(lastTimestamp))];
293
- case 3:
294
- result = _c.sent();
295
- return [3 /*break*/, 5];
296
- case 4: throw new Error("Published URL is invalid for dex ".concat(dex, " on chain ").concat(chainId));
297
- case 5: return [3 /*break*/, 11];
298
- case 6:
299
- error_3 = _c.sent();
300
- if (publishedUrl) {
301
- console.error('Request to published graph URL failed:', error_3);
302
- }
303
- _c.label = 7;
304
- case 7:
305
- _c.trys.push([7, 9, , 10]);
306
- return [4 /*yield*/, (0, functions_1.sendCollectFeesQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultCollectFeesQuery)(lastTimestamp))];
307
- case 8:
308
- result = _c.sent();
309
- return [3 /*break*/, 10];
310
- case 9:
311
- error2_3 = _c.sent();
312
- console.error('Request to public graph URL failed:', error2_3);
313
- throw new Error("Could not get collected fees for vault ".concat(vaultAddress, " on chain ").concat(chainId));
314
- case 10: return [3 /*break*/, 11];
315
- case 11:
316
- if (result && result.length > 0) {
317
- otherFees.push.apply(otherFees, result);
318
- if (result.length < 1000) {
319
- endOfData = true;
320
- }
321
- else {
322
- // Update the cursor to the timestamp of the last item
323
- lastTimestamp = result[result.length - 1].createdAtTimestamp;
324
- }
325
- }
326
- else {
327
- endOfData = true;
328
- }
329
- return [3 /*break*/, 1];
330
- case 12:
331
- cache_1.default.set(key, otherFees, ttl);
332
- return [2 /*return*/, otherFees];
333
- }
334
- });
335
- });
184
+ async function _getFeesCollectedEvents(vaultAddress, chainId, dex, days) {
185
+ const key = `fees-${chainId}-${vaultAddress}-${days}`;
186
+ const cachedData = cache_1.default.get(key);
187
+ if (cachedData) {
188
+ return cachedData;
189
+ }
190
+ const ttl = 120000;
191
+ const { publishedUrl, url } = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true);
192
+ const supportsCollectFees = constants_1.graphUrls[chainId][dex]?.supportsCollectFees;
193
+ if (!supportsCollectFees) {
194
+ const result = [];
195
+ cache_1.default.set(key, result, 24 * 60 * 60 * 1000);
196
+ return result;
197
+ }
198
+ const currTimestamp = Date.now();
199
+ const startTimestamp = days
200
+ ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
201
+ : '0';
202
+ const otherFees = [];
203
+ let endOfData = false;
204
+ let lastTimestamp = startTimestamp;
205
+ while (!endOfData) {
206
+ let result;
207
+ try {
208
+ if (publishedUrl) {
209
+ result = await (0, functions_1.sendCollectFeesQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultCollectFeesQuery)(lastTimestamp));
210
+ }
211
+ else {
212
+ throw new Error(`Published URL is invalid for dex ${dex} on chain ${chainId}`);
213
+ }
214
+ }
215
+ catch (error) {
216
+ if (publishedUrl) {
217
+ console.error('Request to published graph URL failed:', error);
218
+ }
219
+ try {
220
+ result = await (0, functions_1.sendCollectFeesQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultCollectFeesQuery)(lastTimestamp));
221
+ }
222
+ catch (error2) {
223
+ console.error('Request to public graph URL failed:', error2);
224
+ throw new Error(`Could not get collected fees for vault ${vaultAddress} on chain ${chainId}`);
225
+ }
226
+ }
227
+ if (result && result.length > 0) {
228
+ otherFees.push(...result);
229
+ if (result.length < 1000) {
230
+ endOfData = true;
231
+ }
232
+ else {
233
+ // Update the cursor to the timestamp of the last item
234
+ lastTimestamp = result[result.length - 1].createdAtTimestamp;
235
+ }
236
+ }
237
+ else {
238
+ endOfData = true;
239
+ }
240
+ }
241
+ cache_1.default.set(key, otherFees, ttl);
242
+ return otherFees;
336
243
  }
337
244
  exports._getFeesCollectedEvents = _getFeesCollectedEvents;
338
245
  // eslint-disable-next-line no-underscore-dangle
339
- function _getDeposits(vaultAddress, chainId, dex, days) {
340
- return __awaiter(this, void 0, void 0, function () {
341
- var key, cachedData, ttl, _a, publishedUrl, url, currTimestamp, startTimestamp, depositEvents, endOfData, lastTimestamp, result, error_4, error2_4;
342
- return __generator(this, function (_b) {
343
- switch (_b.label) {
344
- case 0:
345
- key = "deposits-".concat(chainId, "-").concat(vaultAddress, "-").concat(days);
346
- cachedData = cache_1.default.get(key);
347
- if (cachedData) {
348
- return [2 /*return*/, cachedData];
349
- }
350
- ttl = 120000;
351
- _a = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true), publishedUrl = _a.publishedUrl, url = _a.url;
352
- currTimestamp = Date.now();
353
- startTimestamp = days
354
- ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
355
- : '0';
356
- depositEvents = [];
357
- endOfData = false;
358
- lastTimestamp = startTimestamp;
359
- _b.label = 1;
360
- case 1:
361
- if (!!endOfData) return [3 /*break*/, 12];
362
- result = void 0;
363
- _b.label = 2;
364
- case 2:
365
- _b.trys.push([2, 6, , 11]);
366
- if (!publishedUrl) return [3 /*break*/, 4];
367
- return [4 /*yield*/, (0, functions_1.sendDepositsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultDepositsQuery)(lastTimestamp))];
368
- case 3:
369
- result = _b.sent();
370
- return [3 /*break*/, 5];
371
- case 4: throw new Error("Published URL is invalid for dex ".concat(dex, " on chain ").concat(chainId));
372
- case 5: return [3 /*break*/, 11];
373
- case 6:
374
- error_4 = _b.sent();
375
- if (publishedUrl) {
376
- console.error('Request to published graph URL failed:', error_4);
377
- }
378
- _b.label = 7;
379
- case 7:
380
- _b.trys.push([7, 9, , 10]);
381
- return [4 /*yield*/, (0, functions_1.sendDepositsQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultDepositsQuery)(lastTimestamp))];
382
- case 8:
383
- result = _b.sent();
384
- return [3 /*break*/, 10];
385
- case 9:
386
- error2_4 = _b.sent();
387
- console.error('Request to public graph URL failed:', error2_4);
388
- throw new Error("Could not get deposits for vault ".concat(vaultAddress, " on chain ").concat(chainId));
389
- case 10: return [3 /*break*/, 11];
390
- case 11:
391
- if (result && result.length > 0) {
392
- depositEvents.push.apply(depositEvents, result);
393
- if (result.length < 1000) {
394
- endOfData = true;
395
- }
396
- else {
397
- // Update the cursor to the timestamp of the last item
398
- lastTimestamp = result[result.length - 1].createdAtTimestamp;
399
- }
400
- }
401
- else {
402
- endOfData = true;
403
- }
404
- return [3 /*break*/, 1];
405
- case 12:
406
- cache_1.default.set(key, depositEvents, ttl);
407
- return [2 /*return*/, depositEvents];
408
- }
409
- });
410
- });
246
+ async function _getDeposits(vaultAddress, chainId, dex, days) {
247
+ const key = `deposits-${chainId}-${vaultAddress}-${days}`;
248
+ const cachedData = cache_1.default.get(key);
249
+ if (cachedData) {
250
+ return cachedData;
251
+ }
252
+ const ttl = 120000;
253
+ const { publishedUrl, url } = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true);
254
+ const currTimestamp = Date.now();
255
+ const startTimestamp = days
256
+ ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
257
+ : '0';
258
+ const depositEvents = [];
259
+ let endOfData = false;
260
+ let lastTimestamp = startTimestamp;
261
+ while (!endOfData) {
262
+ let result;
263
+ try {
264
+ if (publishedUrl) {
265
+ result = await (0, functions_1.sendDepositsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultDepositsQuery)(lastTimestamp));
266
+ }
267
+ else {
268
+ throw new Error(`Published URL is invalid for dex ${dex} on chain ${chainId}`);
269
+ }
270
+ }
271
+ catch (error) {
272
+ if (publishedUrl) {
273
+ console.error('Request to published graph URL failed:', error);
274
+ }
275
+ try {
276
+ result = await (0, functions_1.sendDepositsQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultDepositsQuery)(lastTimestamp));
277
+ }
278
+ catch (error2) {
279
+ console.error('Request to public graph URL failed:', error2);
280
+ throw new Error(`Could not get deposits for vault ${vaultAddress} on chain ${chainId}`);
281
+ }
282
+ }
283
+ if (result && result.length > 0) {
284
+ depositEvents.push(...result);
285
+ if (result.length < 1000) {
286
+ endOfData = true;
287
+ }
288
+ else {
289
+ // Update the cursor to the timestamp of the last item
290
+ lastTimestamp = result[result.length - 1].createdAtTimestamp;
291
+ }
292
+ }
293
+ else {
294
+ endOfData = true;
295
+ }
296
+ }
297
+ cache_1.default.set(key, depositEvents, ttl);
298
+ return depositEvents;
411
299
  }
412
300
  exports._getDeposits = _getDeposits;
413
301
  // eslint-disable-next-line no-underscore-dangle
414
- function _getWithdraws(vaultAddress, chainId, dex, days) {
415
- return __awaiter(this, void 0, void 0, function () {
416
- var key, cachedData, ttl, _a, publishedUrl, url, currTimestamp, startTimestamp, withdrawEvents, endOfData, lastTimestamp, result, error_5, error2_5;
417
- return __generator(this, function (_b) {
418
- switch (_b.label) {
419
- case 0:
420
- key = "withdraws-".concat(chainId, "-").concat(vaultAddress, "-").concat(days);
421
- cachedData = cache_1.default.get(key);
422
- if (cachedData) {
423
- return [2 /*return*/, cachedData];
424
- }
425
- ttl = 120000;
426
- _a = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true), publishedUrl = _a.publishedUrl, url = _a.url;
427
- currTimestamp = Date.now();
428
- startTimestamp = days
429
- ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
430
- : '0';
431
- withdrawEvents = [];
432
- endOfData = false;
433
- lastTimestamp = startTimestamp;
434
- _b.label = 1;
435
- case 1:
436
- if (!!endOfData) return [3 /*break*/, 12];
437
- result = void 0;
438
- _b.label = 2;
439
- case 2:
440
- _b.trys.push([2, 6, , 11]);
441
- if (!publishedUrl) return [3 /*break*/, 4];
442
- return [4 /*yield*/, (0, functions_1.sendWithdrawsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultWithdrawsQuery)(lastTimestamp))];
443
- case 3:
444
- result = _b.sent();
445
- return [3 /*break*/, 5];
446
- case 4: throw new Error("Published URL is invalid for dex ".concat(dex, " on chain ").concat(chainId));
447
- case 5: return [3 /*break*/, 11];
448
- case 6:
449
- error_5 = _b.sent();
450
- if (publishedUrl) {
451
- console.error('Request to published graph URL failed:', error_5);
452
- }
453
- _b.label = 7;
454
- case 7:
455
- _b.trys.push([7, 9, , 10]);
456
- return [4 /*yield*/, (0, functions_1.sendWithdrawsQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultWithdrawsQuery)(lastTimestamp))];
457
- case 8:
458
- result = _b.sent();
459
- return [3 /*break*/, 10];
460
- case 9:
461
- error2_5 = _b.sent();
462
- console.error('Request to public graph URL failed:', error2_5);
463
- throw new Error("Could not get withdraws for vault ".concat(vaultAddress, " on chain ").concat(chainId));
464
- case 10: return [3 /*break*/, 11];
465
- case 11:
466
- if (result && result.length > 0) {
467
- withdrawEvents.push.apply(withdrawEvents, result);
468
- if (result.length < 1000) {
469
- endOfData = true;
470
- }
471
- else {
472
- // Update the cursor to the timestamp of the last item
473
- lastTimestamp = result[result.length - 1].createdAtTimestamp;
474
- }
475
- }
476
- else {
477
- endOfData = true;
478
- }
479
- return [3 /*break*/, 1];
480
- case 12:
481
- cache_1.default.set(key, withdrawEvents, ttl);
482
- return [2 /*return*/, withdrawEvents];
483
- }
484
- });
485
- });
302
+ async function _getWithdraws(vaultAddress, chainId, dex, days) {
303
+ const key = `withdraws-${chainId}-${vaultAddress}-${days}`;
304
+ const cachedData = cache_1.default.get(key);
305
+ if (cachedData) {
306
+ return cachedData;
307
+ }
308
+ const ttl = 120000;
309
+ const { publishedUrl, url } = (0, getGraphUrls_1.getGraphUrls)(chainId, dex, true);
310
+ const currTimestamp = Date.now();
311
+ const startTimestamp = days
312
+ ? parseInt(((currTimestamp - (0, timestamps_1.daysToMilliseconds)(days)) / 1000).toString()).toString()
313
+ : '0';
314
+ const withdrawEvents = [];
315
+ let endOfData = false;
316
+ let lastTimestamp = startTimestamp;
317
+ while (!endOfData) {
318
+ let result;
319
+ try {
320
+ if (publishedUrl) {
321
+ result = await (0, functions_1.sendWithdrawsQueryRequest)(publishedUrl, vaultAddress, startTimestamp, (0, queries_1.vaultWithdrawsQuery)(lastTimestamp));
322
+ }
323
+ else {
324
+ throw new Error(`Published URL is invalid for dex ${dex} on chain ${chainId}`);
325
+ }
326
+ }
327
+ catch (error) {
328
+ if (publishedUrl) {
329
+ console.error('Request to published graph URL failed:', error);
330
+ }
331
+ try {
332
+ result = await (0, functions_1.sendWithdrawsQueryRequest)(url, vaultAddress, startTimestamp, (0, queries_1.vaultWithdrawsQuery)(lastTimestamp));
333
+ }
334
+ catch (error2) {
335
+ console.error('Request to public graph URL failed:', error2);
336
+ throw new Error(`Could not get withdraws for vault ${vaultAddress} on chain ${chainId}`);
337
+ }
338
+ }
339
+ if (result && result.length > 0) {
340
+ withdrawEvents.push(...result);
341
+ if (result.length < 1000) {
342
+ endOfData = true;
343
+ }
344
+ else {
345
+ // Update the cursor to the timestamp of the last item
346
+ lastTimestamp = result[result.length - 1].createdAtTimestamp;
347
+ }
348
+ }
349
+ else {
350
+ endOfData = true;
351
+ }
352
+ }
353
+ cache_1.default.set(key, withdrawEvents, ttl);
354
+ return withdrawEvents;
486
355
  }
487
356
  exports._getWithdraws = _getWithdraws;
488
357
  // eslint-disable-next-line no-underscore-dangle
489
- function _getAllVaultEvents(vaultAddress, chainId, dex, days) {
490
- return __awaiter(this, void 0, void 0, function () {
491
- var key, cachedData, ttl, allEvents, result;
492
- return __generator(this, function (_a) {
493
- switch (_a.label) {
494
- case 0:
495
- key = "allEvents-".concat(chainId, "-").concat(vaultAddress);
496
- cachedData = cache_1.default.get(key);
497
- if (cachedData) {
498
- return [2 /*return*/, cachedData];
499
- }
500
- ttl = 120000;
501
- return [4 /*yield*/, _getAllEvents(vaultAddress, chainId, dex, days)];
502
- case 1:
503
- allEvents = _a.sent();
504
- result = allEvents.sort(function (a, b) { return Number(b.createdAtTimestamp) - Number(a.createdAtTimestamp); });
505
- cache_1.default.set(key, result, ttl);
506
- return [2 /*return*/, result];
507
- }
508
- });
509
- });
358
+ async function _getAllVaultEvents(vaultAddress, chainId, dex, days) {
359
+ const key = `allEvents-${chainId}-${vaultAddress}`;
360
+ const cachedData = cache_1.default.get(key);
361
+ if (cachedData) {
362
+ return cachedData;
363
+ }
364
+ const ttl = 120000;
365
+ const allEvents = await _getAllEvents(vaultAddress, chainId, dex, days);
366
+ const result = allEvents.sort((a, b) => Number(b.createdAtTimestamp) - Number(a.createdAtTimestamp));
367
+ cache_1.default.set(key, result, ttl);
368
+ return result;
510
369
  }
511
370
  exports._getAllVaultEvents = _getAllVaultEvents;
512
371
  function getVaultStateAt(vaultEvents, daysAgo) {
513
372
  if (vaultEvents.length === 0) {
514
373
  return null;
515
374
  }
516
- var eventsBefore = vaultEvents.filter(function (e) { return Number(e.createdAtTimestamp) * 1000 <= Date.now() - (0, timestamps_1.daysToMilliseconds)(daysAgo); });
375
+ const eventsBefore = vaultEvents.filter((e) => Number(e.createdAtTimestamp) * 1000 <= Date.now() - (0, timestamps_1.daysToMilliseconds)(daysAgo));
517
376
  if (eventsBefore.length > 0) {
518
377
  return eventsBefore[0];
519
378
  }