@across-protocol/sdk 4.0.0-beta.34 → 4.0.0-beta.4

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 (127) hide show
  1. package/dist/cjs/clients/BundleDataClient/BundleDataClient.d.ts +4 -5
  2. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js +175 -355
  3. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  4. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.d.ts +2 -1
  5. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js +2 -1
  6. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -1
  7. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.d.ts +1 -5
  8. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js +1 -47
  9. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js.map +1 -1
  10. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.d.ts +4 -4
  11. package/dist/cjs/clients/SpokePoolClient.d.ts +0 -1
  12. package/dist/cjs/clients/SpokePoolClient.js +1 -10
  13. package/dist/cjs/clients/SpokePoolClient.js.map +1 -1
  14. package/dist/cjs/clients/mocks/MockSpokePoolClient.d.ts +1 -2
  15. package/dist/cjs/clients/mocks/MockSpokePoolClient.js +1 -11
  16. package/dist/cjs/clients/mocks/MockSpokePoolClient.js.map +1 -1
  17. package/dist/cjs/constants.d.ts +1 -1
  18. package/dist/cjs/constants.js +2 -2
  19. package/dist/cjs/constants.js.map +1 -1
  20. package/dist/cjs/providers/index.d.ts +0 -1
  21. package/dist/cjs/providers/index.js +0 -2
  22. package/dist/cjs/providers/index.js.map +1 -1
  23. package/dist/cjs/utils/AddressUtils.d.ts +0 -2
  24. package/dist/cjs/utils/AddressUtils.js +1 -19
  25. package/dist/cjs/utils/AddressUtils.js.map +1 -1
  26. package/dist/cjs/utils/DepositUtils.d.ts +1 -2
  27. package/dist/cjs/utils/DepositUtils.js +1 -10
  28. package/dist/cjs/utils/DepositUtils.js.map +1 -1
  29. package/dist/cjs/utils/EventUtils.js +0 -21
  30. package/dist/cjs/utils/EventUtils.js.map +1 -1
  31. package/dist/cjs/utils/NetworkUtils.d.ts +0 -1
  32. package/dist/cjs/utils/NetworkUtils.js +1 -6
  33. package/dist/cjs/utils/NetworkUtils.js.map +1 -1
  34. package/dist/cjs/utils/SpokeUtils.d.ts +0 -1
  35. package/dist/cjs/utils/SpokeUtils.js +8 -15
  36. package/dist/cjs/utils/SpokeUtils.js.map +1 -1
  37. package/dist/cjs/utils/common.d.ts +0 -1
  38. package/dist/cjs/utils/common.js +1 -2
  39. package/dist/cjs/utils/common.js.map +1 -1
  40. package/dist/esm/clients/BundleDataClient/BundleDataClient.d.ts +4 -5
  41. package/dist/esm/clients/BundleDataClient/BundleDataClient.js +257 -459
  42. package/dist/esm/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  43. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.d.ts +2 -1
  44. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js +3 -2
  45. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -1
  46. package/dist/esm/clients/BundleDataClient/utils/FillUtils.d.ts +1 -5
  47. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js +1 -54
  48. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js.map +1 -1
  49. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.d.ts +4 -4
  50. package/dist/esm/clients/SpokePoolClient.d.ts +0 -8
  51. package/dist/esm/clients/SpokePoolClient.js +1 -17
  52. package/dist/esm/clients/SpokePoolClient.js.map +1 -1
  53. package/dist/esm/clients/mocks/MockSpokePoolClient.d.ts +1 -2
  54. package/dist/esm/clients/mocks/MockSpokePoolClient.js +1 -11
  55. package/dist/esm/clients/mocks/MockSpokePoolClient.js.map +1 -1
  56. package/dist/esm/constants.d.ts +1 -1
  57. package/dist/esm/constants.js +2 -2
  58. package/dist/esm/constants.js.map +1 -1
  59. package/dist/esm/providers/index.d.ts +0 -1
  60. package/dist/esm/providers/index.js +0 -2
  61. package/dist/esm/providers/index.js.map +1 -1
  62. package/dist/esm/utils/AddressUtils.d.ts +0 -2
  63. package/dist/esm/utils/AddressUtils.js +0 -25
  64. package/dist/esm/utils/AddressUtils.js.map +1 -1
  65. package/dist/esm/utils/DepositUtils.d.ts +1 -2
  66. package/dist/esm/utils/DepositUtils.js +2 -11
  67. package/dist/esm/utils/DepositUtils.js.map +1 -1
  68. package/dist/esm/utils/EventUtils.js +1 -29
  69. package/dist/esm/utils/EventUtils.js.map +1 -1
  70. package/dist/esm/utils/NetworkUtils.d.ts +0 -6
  71. package/dist/esm/utils/NetworkUtils.js +0 -10
  72. package/dist/esm/utils/NetworkUtils.js.map +1 -1
  73. package/dist/esm/utils/SpokeUtils.d.ts +0 -1
  74. package/dist/esm/utils/SpokeUtils.js +8 -14
  75. package/dist/esm/utils/SpokeUtils.js.map +1 -1
  76. package/dist/esm/utils/common.d.ts +0 -1
  77. package/dist/esm/utils/common.js +0 -1
  78. package/dist/esm/utils/common.js.map +1 -1
  79. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts +4 -5
  80. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts.map +1 -1
  81. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts +2 -1
  82. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts.map +1 -1
  83. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts +1 -5
  84. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts.map +1 -1
  85. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts +4 -4
  86. package/dist/types/clients/SpokePoolClient.d.ts +0 -8
  87. package/dist/types/clients/SpokePoolClient.d.ts.map +1 -1
  88. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts +1 -2
  89. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts.map +1 -1
  90. package/dist/types/constants.d.ts +1 -1
  91. package/dist/types/constants.d.ts.map +1 -1
  92. package/dist/types/providers/index.d.ts +0 -1
  93. package/dist/types/providers/index.d.ts.map +1 -1
  94. package/dist/types/utils/AddressUtils.d.ts +0 -2
  95. package/dist/types/utils/AddressUtils.d.ts.map +1 -1
  96. package/dist/types/utils/DepositUtils.d.ts +1 -2
  97. package/dist/types/utils/DepositUtils.d.ts.map +1 -1
  98. package/dist/types/utils/EventUtils.d.ts.map +1 -1
  99. package/dist/types/utils/NetworkUtils.d.ts +0 -6
  100. package/dist/types/utils/NetworkUtils.d.ts.map +1 -1
  101. package/dist/types/utils/SpokeUtils.d.ts +0 -1
  102. package/dist/types/utils/SpokeUtils.d.ts.map +1 -1
  103. package/dist/types/utils/common.d.ts +0 -1
  104. package/dist/types/utils/common.d.ts.map +1 -1
  105. package/package.json +1 -1
  106. package/src/clients/BundleDataClient/BundleDataClient.ts +227 -433
  107. package/src/clients/BundleDataClient/utils/DataworkerUtils.ts +8 -0
  108. package/src/clients/BundleDataClient/utils/FillUtils.ts +2 -66
  109. package/src/clients/SpokePoolClient.ts +3 -16
  110. package/src/clients/mocks/MockSpokePoolClient.ts +1 -14
  111. package/src/constants.ts +3 -3
  112. package/src/providers/index.ts +0 -1
  113. package/src/utils/AddressUtils.ts +0 -26
  114. package/src/utils/DepositUtils.ts +2 -11
  115. package/src/utils/EventUtils.ts +1 -29
  116. package/src/utils/NetworkUtils.ts +0 -11
  117. package/src/utils/SpokeUtils.ts +9 -22
  118. package/src/utils/common.ts +0 -2
  119. package/dist/cjs/providers/mockProvider.d.ts +0 -19
  120. package/dist/cjs/providers/mockProvider.js +0 -70
  121. package/dist/cjs/providers/mockProvider.js.map +0 -1
  122. package/dist/esm/providers/mockProvider.d.ts +0 -23
  123. package/dist/esm/providers/mockProvider.js +0 -73
  124. package/dist/esm/providers/mockProvider.js.map +0 -1
  125. package/dist/types/providers/mockProvider.d.ts +0 -24
  126. package/dist/types/providers/mockProvider.d.ts.map +0 -1
  127. package/src/providers/mockProvider.ts +0 -77
@@ -1,18 +1,13 @@
1
1
  import { __assign, __awaiter, __generator } from "tslib";
2
2
  import _ from "lodash";
3
3
  import { FillType, FillStatus, } from "../../interfaces";
4
- import { BigNumber, bnZero, queryHistoricalDepositForFill, assign, assert, fixedPointAdjustment, isDefined, toBN, forEachAsync, getBlockRangeForChain, getImpliedBundleBlockRanges, isSlowFill, mapAsync, filterAsync, bnUint32Max, isZeroValueDeposit, findFillEvent, isZeroValueFillOrSlowFillRequest, chainIsEvm, isValidEvmAddress, } from "../../utils";
5
- import { _buildPoolRebalanceRoot, BundleDataSS, getEndBlockBuffers, getRefundInformationFromFill, getRefundsFromBundle, getWidestPossibleExpectedBlockRange, isChainDisabled, isEvmRepaymentValid, prettyPrintV3SpokePoolEvents, verifyFillRepayment, } from "./utils";
6
- import { PRE_FILL_MIN_CONFIG_STORE_VERSION } from "../../constants";
4
+ import { BigNumber, bnZero, queryHistoricalDepositForFill, assign, assert, fixedPointAdjustment, isDefined, toBN, forEachAsync, getBlockRangeForChain, getImpliedBundleBlockRanges, isSlowFill, mapAsync, bnUint32Max, isZeroValueDeposit, findFillEvent, isZeroValueFillOrSlowFillRequest, } from "../../utils";
5
+ import { _buildPoolRebalanceRoot, BundleDataSS, getEndBlockBuffers, getRefundInformationFromFill, getRefundsFromBundle, getWidestPossibleExpectedBlockRange, isChainDisabled, prettyPrintV3SpokePoolEvents, } from "./utils";
7
6
  // max(uint256) - 1
8
7
  export var INFINITE_FILL_DEADLINE = bnUint32Max;
9
8
  // V3 dictionary helper functions
10
9
  function updateExpiredDepositsV3(dict, deposit) {
11
10
  var _a;
12
- // A deposit refund for a deposit is invalid if the depositor has a bytes32 address input for an EVM chain. It is valid otherwise.
13
- if (chainIsEvm(deposit.originChainId) && !isValidEvmAddress(deposit.depositor)) {
14
- return;
15
- }
16
11
  var originChainId = deposit.originChainId, inputToken = deposit.inputToken;
17
12
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[originChainId]) === null || _a === void 0 ? void 0 : _a[inputToken])) {
18
13
  assign(dict, [originChainId, inputToken], []);
@@ -27,10 +22,8 @@ function updateBundleDepositsV3(dict, deposit) {
27
22
  }
28
23
  dict[originChainId][inputToken].push(deposit);
29
24
  }
30
- function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken, repaymentAddress) {
25
+ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken) {
31
26
  var _a, _b;
32
- // We shouldn't pass any unrepayable fills into this function, so we perform an extra safety check.
33
- assert(chainIsEvm(repaymentChainId) && isEvmRepaymentValid(fill, repaymentChainId), "validatedBundleV3Fills dictionary should only contain fills with valid repayment information");
34
27
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[repaymentChainId]) === null || _a === void 0 ? void 0 : _a[repaymentToken])) {
35
28
  assign(dict, [repaymentChainId, repaymentToken], {
36
29
  fills: [],
@@ -39,26 +32,26 @@ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentTo
39
32
  refunds: {},
40
33
  });
41
34
  }
42
- var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct, relayer: repaymentAddress });
35
+ var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct });
43
36
  // Add all fills, slow and fast, to dictionary.
44
37
  assign(dict, [repaymentChainId, repaymentToken, "fills"], [bundleFill]);
45
38
  // All fills update the bundle LP fees.
46
39
  var refundObj = dict[repaymentChainId][repaymentToken];
47
- var realizedLpFee = bundleFill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
40
+ var realizedLpFee = fill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
48
41
  refundObj.realizedLpFees = refundObj.realizedLpFees ? refundObj.realizedLpFees.add(realizedLpFee) : realizedLpFee;
49
42
  // Only fast fills get refunded.
50
- if (!isSlowFill(bundleFill)) {
51
- var refundAmount = bundleFill.inputAmount.mul(fixedPointAdjustment.sub(lpFeePct)).div(fixedPointAdjustment);
43
+ if (!isSlowFill(fill)) {
44
+ var refundAmount = fill.inputAmount.mul(fixedPointAdjustment.sub(lpFeePct)).div(fixedPointAdjustment);
52
45
  refundObj.totalRefundAmount = refundObj.totalRefundAmount
53
46
  ? refundObj.totalRefundAmount.add(refundAmount)
54
47
  : refundAmount;
55
48
  // Instantiate dictionary if it doesn't exist.
56
49
  (_b = refundObj.refunds) !== null && _b !== void 0 ? _b : (refundObj.refunds = {});
57
- if (refundObj.refunds[bundleFill.relayer]) {
58
- refundObj.refunds[bundleFill.relayer] = refundObj.refunds[bundleFill.relayer].add(refundAmount);
50
+ if (refundObj.refunds[fill.relayer]) {
51
+ refundObj.refunds[fill.relayer] = refundObj.refunds[fill.relayer].add(refundAmount);
59
52
  }
60
53
  else {
61
- refundObj.refunds[bundleFill.relayer] = refundAmount;
54
+ refundObj.refunds[fill.relayer] = refundAmount;
62
55
  }
63
56
  }
64
57
  }
@@ -72,9 +65,6 @@ function updateBundleExcessSlowFills(dict, deposit) {
72
65
  }
73
66
  function updateBundleSlowFills(dict, deposit) {
74
67
  var _a;
75
- if (chainIsEvm(deposit.destinationChainId) && !isValidEvmAddress(deposit.recipient)) {
76
- return;
77
- }
78
68
  var destinationChainId = deposit.destinationChainId, outputToken = deposit.outputToken;
79
69
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[destinationChainId]) === null || _a === void 0 ? void 0 : _a[outputToken])) {
80
70
  assign(dict, [destinationChainId, outputToken], []);
@@ -172,7 +162,8 @@ var BundleDataClient = /** @class */ (function () {
172
162
  at: "BundleDataClient#loadPersistedDataFromArweave",
173
163
  message: "Loaded persisted data from Arweave in ".concat(Math.round(performance.now() - start) / 1000, "s."),
174
164
  blockRanges: JSON.stringify(blockRangesForChains),
175
- bundleData: prettyPrintV3SpokePoolEvents(bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.expiredDepositsToRefundV3, bundleData.unexecutableSlowFills),
165
+ bundleData: prettyPrintV3SpokePoolEvents(bundleData.bundleDepositsV3, bundleData.bundleFillsV3, [], // Invalid fills are not persisted to Arweave.
166
+ bundleData.bundleSlowFillsV3, bundleData.expiredDepositsToRefundV3, bundleData.unexecutableSlowFills),
176
167
  });
177
168
  return [2 /*return*/, bundleData];
178
169
  }
@@ -218,124 +209,77 @@ var BundleDataClient = /** @class */ (function () {
218
209
  return [4 /*yield*/, this.loadArweaveData(bundleEvaluationBlockRanges)];
219
210
  case 1:
220
211
  arweaveData = _a.sent();
221
- if (!(arweaveData === undefined)) return [3 /*break*/, 3];
222
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges)];
223
- case 2:
224
- combinedRefunds = _a.sent();
225
- return [3 /*break*/, 4];
226
- case 3:
227
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
228
- combinedRefunds = getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3);
229
- // If we don't have a spoke pool client for a chain, then we won't be able to deduct refunds correctly for this
230
- // chain. For most of the pending bundle's liveness period, these past refunds are already executed so this is
231
- // a reasonable assumption. This empty refund chain also matches what the alternative
232
- // `getApproximateRefundsForBlockRange` would return.
233
- Object.keys(combinedRefunds).forEach(function (chainId) {
234
- if (_this.spokePoolClients[Number(chainId)] === undefined) {
235
- delete combinedRefunds[Number(chainId)];
236
- }
237
- });
238
- _a.label = 4;
239
- case 4:
240
- // The latest proposed bundle's refund leaves might have already been partially or entirely executed.
241
- // We have to deduct the executed amounts from the total refund amounts.
242
- return [2 /*return*/, this.deductExecutedRefunds(combinedRefunds, bundle)];
212
+ if (arweaveData === undefined) {
213
+ combinedRefunds = this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges);
214
+ }
215
+ else {
216
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
217
+ combinedRefunds = getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3);
218
+ // If we don't have a spoke pool client for a chain, then we won't be able to deduct refunds correctly for this
219
+ // chain. For most of the pending bundle's liveness period, these past refunds are already executed so this is
220
+ // a reasonable assumption. This empty refund chain also matches what the alternative
221
+ // `getApproximateRefundsForBlockRange` would return.
222
+ Object.keys(combinedRefunds).forEach(function (chainId) {
223
+ if (_this.spokePoolClients[Number(chainId)] === undefined) {
224
+ delete combinedRefunds[Number(chainId)];
225
+ }
226
+ });
227
+ }
228
+ // The latest proposed bundle's refund leaves might have already been partially or entirely executed.
229
+ // We have to deduct the executed amounts from the total refund amounts.
230
+ return [2 /*return*/, this.deductExecutedRefunds(combinedRefunds, bundle)];
243
231
  }
244
232
  });
245
233
  });
246
234
  };
247
235
  // @dev This helper function should probably be moved to the InventoryClient
248
236
  BundleDataClient.prototype.getApproximateRefundsForBlockRange = function (chainIds, blockRanges) {
249
- return __awaiter(this, void 0, void 0, function () {
250
- var refundsForChain, _loop_1, this_1, _i, chainIds_1, chainId;
251
- var _this = this;
252
- return __generator(this, function (_a) {
253
- switch (_a.label) {
254
- case 0:
255
- refundsForChain = {};
256
- _loop_1 = function (chainId) {
257
- var chainIndex, fillsToCount;
258
- return __generator(this, function (_b) {
259
- switch (_b.label) {
260
- case 0:
261
- if (this_1.spokePoolClients[chainId] === undefined) {
262
- return [2 /*return*/, "continue"];
263
- }
264
- chainIndex = chainIds.indexOf(chainId);
265
- return [4 /*yield*/, filterAsync(this_1.spokePoolClients[chainId].getFills(), function (fill) { return __awaiter(_this, void 0, void 0, function () {
266
- var matchingDeposit, hasMatchingDeposit, validRepayment;
267
- return __generator(this, function (_a) {
268
- switch (_a.label) {
269
- case 0:
270
- if (fill.blockNumber < blockRanges[chainIndex][0] ||
271
- fill.blockNumber > blockRanges[chainIndex][1] ||
272
- isZeroValueFillOrSlowFillRequest(fill)) {
273
- return [2 /*return*/, false];
274
- }
275
- // If origin spoke pool client isn't defined, we can't validate it.
276
- if (this.spokePoolClients[fill.originChainId] === undefined) {
277
- return [2 /*return*/, false];
278
- }
279
- matchingDeposit = this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
280
- hasMatchingDeposit = matchingDeposit !== undefined &&
281
- this.getRelayHashFromEvent(fill) === this.getRelayHashFromEvent(matchingDeposit);
282
- if (!hasMatchingDeposit) return [3 /*break*/, 2];
283
- return [4 /*yield*/, verifyFillRepayment(fill, this.spokePoolClients[fill.destinationChainId].spokePool.provider, matchingDeposit,
284
- // @dev: to get valid repayment chain ID's, get all chain IDs for the bundle block range and remove
285
- // disabled block ranges.
286
- this.clients.configStoreClient
287
- .getChainIdIndicesForBlock(blockRanges[0][1])
288
- .filter(function (_chainId, i) { return !isChainDisabled(blockRanges[i]); }))];
289
- case 1:
290
- validRepayment = _a.sent();
291
- if (!isDefined(validRepayment)) {
292
- return [2 /*return*/, false];
293
- }
294
- _a.label = 2;
295
- case 2: return [2 /*return*/, hasMatchingDeposit];
296
- }
297
- });
298
- }); })];
299
- case 1:
300
- fillsToCount = _b.sent();
301
- fillsToCount.forEach(function (fill) {
302
- var _a, _b, _c;
303
- var _d;
304
- var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
305
- assert(isDefined(matchingDeposit), "Deposit not found for fill.");
306
- var _e = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain // Use ! because we've already asserted that matchingDeposit is defined.
307
- ), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
308
- // Assume that lp fees are 0 for the sake of speed. In the future we could batch compute
309
- // these or make hardcoded assumptions based on the origin-repayment chain direction. This might result
310
- // in slight over estimations of refunds, but its not clear whether underestimating or overestimating is
311
- // worst from the relayer's perspective.
312
- var relayer = fill.relayer, refundAmount = fill.inputAmount;
313
- (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
314
- (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
315
- var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : bnZero;
316
- refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
317
- });
318
- return [2 /*return*/];
319
- }
320
- });
321
- };
322
- this_1 = this;
323
- _i = 0, chainIds_1 = chainIds;
324
- _a.label = 1;
325
- case 1:
326
- if (!(_i < chainIds_1.length)) return [3 /*break*/, 4];
327
- chainId = chainIds_1[_i];
328
- return [5 /*yield**/, _loop_1(chainId)];
329
- case 2:
330
- _a.sent();
331
- _a.label = 3;
332
- case 3:
333
- _i++;
334
- return [3 /*break*/, 1];
335
- case 4: return [2 /*return*/, refundsForChain];
237
+ var _this = this;
238
+ var refundsForChain = {};
239
+ var _loop_1 = function (chainId) {
240
+ if (this_1.spokePoolClients[chainId] === undefined) {
241
+ return "continue";
242
+ }
243
+ var chainIndex = chainIds.indexOf(chainId);
244
+ this_1.spokePoolClients[chainId]
245
+ .getFills()
246
+ .filter(function (fill) {
247
+ if (fill.blockNumber < blockRanges[chainIndex][0] || fill.blockNumber > blockRanges[chainIndex][1]) {
248
+ return false;
336
249
  }
250
+ // If origin spoke pool client isn't defined, we can't validate it.
251
+ if (_this.spokePoolClients[fill.originChainId] === undefined) {
252
+ return false;
253
+ }
254
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
255
+ var hasMatchingDeposit = matchingDeposit !== undefined &&
256
+ _this.getRelayHashFromEvent(fill) === _this.getRelayHashFromEvent(matchingDeposit);
257
+ return hasMatchingDeposit;
258
+ })
259
+ .forEach(function (fill) {
260
+ var _a, _b, _c;
261
+ var _d;
262
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
263
+ assert(isDefined(matchingDeposit), "Deposit not found for fill.");
264
+ var _e = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain // Use ! because we've already asserted that matchingDeposit is defined.
265
+ ), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
266
+ // Assume that lp fees are 0 for the sake of speed. In the future we could batch compute
267
+ // these or make hardcoded assumptions based on the origin-repayment chain direction. This might result
268
+ // in slight over estimations of refunds, but its not clear whether underestimating or overestimating is
269
+ // worst from the relayer's perspective.
270
+ var relayer = fill.relayer, refundAmount = fill.inputAmount;
271
+ (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
272
+ (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
273
+ var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : bnZero;
274
+ refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
337
275
  });
338
- });
276
+ };
277
+ var this_1 = this;
278
+ for (var _i = 0, chainIds_1 = chainIds; _i < chainIds_1.length; _i++) {
279
+ var chainId = chainIds_1[_i];
280
+ _loop_1(chainId);
281
+ }
282
+ return refundsForChain;
339
283
  };
340
284
  BundleDataClient.prototype.getUpcomingDepositAmount = function (chainId, l2Token, latestBlockToSearch) {
341
285
  if (this.spokePoolClients[chainId] === undefined) {
@@ -380,10 +324,12 @@ var BundleDataClient = /** @class */ (function () {
380
324
  case 1:
381
325
  _a = _b.sent(), bundleData = _a.bundleData, blockRanges = _a.blockRanges;
382
326
  hubPoolClient = this.clients.hubPoolClient;
383
- root = _buildPoolRebalanceRoot(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
384
- hubPoolClient: hubPoolClient,
385
- configStoreClient: hubPoolClient.configStoreClient,
386
- });
327
+ return [4 /*yield*/, _buildPoolRebalanceRoot(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
328
+ hubPoolClient: hubPoolClient,
329
+ configStoreClient: hubPoolClient.configStoreClient,
330
+ })];
331
+ case 2:
332
+ root = _b.sent();
387
333
  return [2 /*return*/, {
388
334
  root: root,
389
335
  blockRanges: blockRanges,
@@ -425,40 +371,33 @@ var BundleDataClient = /** @class */ (function () {
425
371
  return [initialBlockRange[1] + 1, blockRange[1]];
426
372
  });
427
373
  }
428
- var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, _a, _b, bundleFillsV3, expiredDepositsToRefundV3, start, _c, _d;
429
- return __generator(this, function (_e) {
430
- switch (_e.label) {
374
+ var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, bundleFillsV3, expiredDepositsToRefundV3, start;
375
+ return __generator(this, function (_a) {
376
+ switch (_a.label) {
431
377
  case 0:
432
378
  hubPoolClient = this.clients.hubPoolClient;
433
379
  nextBundleMainnetStartBlock = hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, hubPoolClient.latestBlockSearched, hubPoolClient.chainId);
434
380
  chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
435
381
  combinedRefunds = [];
436
382
  widestBundleBlockRanges = getWidestPossibleExpectedBlockRange(chainIds, this.spokePoolClients, getEndBlockBuffers(chainIds, this.blockRangeEndBlockBuffer), this.clients, this.clients.hubPoolClient.latestBlockSearched, this.clients.configStoreClient.getEnabledChains(this.clients.hubPoolClient.latestBlockSearched));
437
- if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 5];
383
+ if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 2];
438
384
  pendingBundleBlockRanges = getImpliedBundleBlockRanges(hubPoolClient, this.clients.configStoreClient, hubPoolClient.getLatestProposedRootBundle());
439
385
  return [4 /*yield*/, this.loadArweaveData(pendingBundleBlockRanges)];
440
386
  case 1:
441
- arweaveData = _e.sent();
442
- if (!(arweaveData === undefined)) return [3 /*break*/, 3];
443
- _b = (_a = combinedRefunds).push;
444
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges)];
445
- case 2:
446
- _b.apply(_a, [_e.sent()]);
447
- return [3 /*break*/, 4];
448
- case 3:
449
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
450
- combinedRefunds.push(getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3));
451
- _e.label = 4;
452
- case 4:
387
+ arweaveData = _a.sent();
388
+ if (arweaveData === undefined) {
389
+ combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges));
390
+ }
391
+ else {
392
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
393
+ combinedRefunds.push(getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3));
394
+ }
453
395
  // Shorten the widestBundleBlockRanges now to not double count the pending bundle blocks.
454
396
  widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
455
- _e.label = 5;
456
- case 5:
397
+ _a.label = 2;
398
+ case 2:
457
399
  start = performance.now();
458
- _d = (_c = combinedRefunds).push;
459
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges)];
460
- case 6:
461
- _d.apply(_c, [_e.sent()]);
400
+ combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges));
462
401
  this.logger.debug({
463
402
  at: "BundleDataClient#getNextBundleRefunds",
464
403
  message: "Loading approximate refunds for next bundle in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -601,7 +540,7 @@ var BundleDataClient = /** @class */ (function () {
601
540
  };
602
541
  BundleDataClient.prototype.loadDataFromScratch = function (blockRangesForChains, spokePoolClients) {
603
542
  return __awaiter(this, void 0, void 0, function () {
604
- var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleUnrepayableFillsV3, bundleInvalidSlowFillRequests, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, _canCreateSlowFillLeaf, _depositIsExpired, _getFillStatusForDeposit, allChainIds, _cachedBundleTimestamps, bundleBlockTimestamps, v3RelayHashes, bundleDepositHashes, olderDepositHashes, decodeBundleDepositHash, startBlockForMainnet, versionAtProposalBlock, canRefundPrefills, _loop_2, _i, allChainIds_1, originChainId, validatedBundleV3Fills, validatedBundleSlowFills, validatedBundleUnexecutableSlowFills, fillCounter, _loop_3, _a, allChainIds_2, originChainId, promises, _b, v3FillLpFees, v3SlowFillLpFees, v3UnexecutableSlowFillLpFees, v3SpokeEventsReadable;
543
+ var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, _canCreateSlowFillLeaf, _depositIsExpired, _getFillStatusForDeposit, allChainIds, _cachedBundleTimestamps, bundleBlockTimestamps, v3RelayHashes, bundleDepositHashes, olderDepositHashes, depositCounter, _loop_2, _i, allChainIds_1, originChainId, validatedBundleV3Fills, validatedBundleSlowFills, validatedBundleUnexecutableSlowFills, fillCounter, _loop_3, _a, allChainIds_2, originChainId, promises, _b, v3FillLpFees, v3SlowFillLpFees, v3UnexecutableSlowFillLpFees, v3SpokeEventsReadable;
605
544
  var _this = this;
606
545
  return __generator(this, function (_c) {
607
546
  switch (_c.label) {
@@ -621,8 +560,6 @@ var BundleDataClient = /** @class */ (function () {
621
560
  bundleDepositsV3 = {};
622
561
  bundleFillsV3 = {};
623
562
  bundleInvalidFillsV3 = [];
624
- bundleUnrepayableFillsV3 = [];
625
- bundleInvalidSlowFillRequests = [];
626
563
  bundleSlowFillsV3 = {};
627
564
  expiredDepositsToRefundV3 = {};
628
565
  unexecutableSlowFills = {};
@@ -680,13 +617,7 @@ var BundleDataClient = /** @class */ (function () {
680
617
  v3RelayHashes = {};
681
618
  bundleDepositHashes = [];
682
619
  olderDepositHashes = [];
683
- decodeBundleDepositHash = function (depositHash) {
684
- var _a = depositHash.split("@"), relayDataHash = _a[0], i = _a[1];
685
- return { relayDataHash: relayDataHash, index: Number(i) };
686
- };
687
- startBlockForMainnet = getBlockRangeForChain(blockRangesForChains, this.clients.hubPoolClient.chainId, this.chainIdListForBundleEvaluationBlockNumbers)[0];
688
- versionAtProposalBlock = this.clients.configStoreClient.getConfigStoreVersionForBlock(startBlockForMainnet);
689
- canRefundPrefills = versionAtProposalBlock >= PRE_FILL_MIN_CONFIG_STORE_VERSION || process.env.FORCE_REFUND_PREFILLS === "true";
620
+ depositCounter = 0;
690
621
  _loop_2 = function (originChainId) {
691
622
  var originClient = spokePoolClients[originChainId];
692
623
  var originChainBlockRange = getBlockRangeForChain(blockRangesForChains, originChainId, chainIds);
@@ -696,45 +627,48 @@ var BundleDataClient = /** @class */ (function () {
696
627
  continue;
697
628
  }
698
629
  originClient.getDepositsForDestinationChainWithDuplicates(destinationChainId).forEach(function (deposit) {
630
+ // Only evaluate deposits that are in this bundle or in previous bundles. This means we cannot issue fill
631
+ // refunds or slow fills here for deposits that are in future bundles (i.e. "pre-fills"). Instead, we'll
632
+ // evaluate these pre-fills once the deposit is inside the "current" bundle block range.
699
633
  if (deposit.blockNumber > originChainBlockRange[1] || isZeroValueDeposit(deposit)) {
700
634
  return;
701
635
  }
636
+ depositCounter++;
702
637
  var relayDataHash = _this.getRelayHashFromEvent(deposit);
638
+ // Duplicate deposits are treated like normal deposits.
703
639
  if (!v3RelayHashes[relayDataHash]) {
704
640
  v3RelayHashes[relayDataHash] = {
705
- deposits: [deposit],
641
+ deposit: deposit,
706
642
  fill: undefined,
707
643
  slowFillRequest: undefined,
708
644
  };
709
645
  }
710
- else {
711
- v3RelayHashes[relayDataHash].deposits.push(deposit);
646
+ // Once we've saved the deposit hash into v3RelayHashes, then we can exit early here if the inputAmount
647
+ // is 0 because there can be no expired amount to refund and no unexecutable slow fill amount to return
648
+ // if this deposit did expire. Input amount can only be zero at this point if the message is non-empty,
649
+ // but the message doesn't matter for expired deposits and unexecutable slow fills.
650
+ if (deposit.inputAmount.eq(0)) {
651
+ return;
712
652
  }
713
- // Account for duplicate deposits by concatenating the relayDataHash with the count of the number of times
714
- // we have seen it so far.
715
- var newBundleDepositHash = "".concat(relayDataHash, "@").concat(v3RelayHashes[relayDataHash].deposits.length - 1);
716
- var decodedBundleDepositHash = decodeBundleDepositHash(newBundleDepositHash);
717
- assert(decodedBundleDepositHash.relayDataHash === relayDataHash &&
718
- decodedBundleDepositHash.index === v3RelayHashes[relayDataHash].deposits.length - 1, "Not using correct bundle deposit hash key");
653
+ // Evaluate all expired deposits after fetching fill statuses,
654
+ // since we can't know for certain whether an expired deposit was filled a long time ago.
719
655
  if (deposit.blockNumber >= originChainBlockRange[0]) {
720
- bundleDepositHashes.push(newBundleDepositHash);
656
+ bundleDepositHashes.push(relayDataHash);
721
657
  updateBundleDepositsV3(bundleDepositsV3, deposit);
722
658
  }
723
659
  else if (deposit.blockNumber < originChainBlockRange[0]) {
724
- olderDepositHashes.push(newBundleDepositHash);
660
+ olderDepositHashes.push(relayDataHash);
725
661
  }
726
662
  });
727
663
  }
728
664
  };
729
- // Prerequisite step: Load all deposit events from the current or older bundles into the v3RelayHashes dictionary
730
- // for convenient matching with fills.
731
665
  for (_i = 0, allChainIds_1 = allChainIds; _i < allChainIds_1.length; _i++) {
732
666
  originChainId = allChainIds_1[_i];
733
667
  _loop_2(originChainId);
734
668
  }
735
669
  this.logger.debug({
736
670
  at: "BundleDataClient#loadData",
737
- message: "Processed ".concat(bundleDepositHashes.length + olderDepositHashes.length, " deposits in ").concat(performance.now() - start, "ms."),
671
+ message: "Processed ".concat(depositCounter, " deposits in ").concat(performance.now() - start, "ms."),
738
672
  });
739
673
  start = performance.now();
740
674
  validatedBundleV3Fills = [];
@@ -748,7 +682,7 @@ var BundleDataClient = /** @class */ (function () {
748
682
  case 0:
749
683
  originClient = spokePoolClients[originChainId];
750
684
  _loop_4 = function (destinationChainId) {
751
- var destinationClient, destinationChainBlockRange, originChainBlockRange, fastFillsReplacingSlowFills;
685
+ var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills;
752
686
  return __generator(this, function (_g) {
753
687
  switch (_g.label) {
754
688
  case 0:
@@ -757,7 +691,6 @@ var BundleDataClient = /** @class */ (function () {
757
691
  }
758
692
  destinationClient = spokePoolClients[destinationChainId];
759
693
  destinationChainBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
760
- originChainBlockRange = getBlockRangeForChain(blockRangesForChains, originChainId, chainIds);
761
694
  fastFillsReplacingSlowFills = [];
762
695
  return [4 /*yield*/, forEachAsync(destinationClient
763
696
  .getFillsForOriginChain(originChainId)
@@ -765,58 +698,40 @@ var BundleDataClient = /** @class */ (function () {
765
698
  // tokens to the filler. We can't remove non-empty message deposit here in case there is a slow fill
766
699
  // request for the deposit, we'd want to see the fill took place.
767
700
  .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1] && !isZeroValueFillOrSlowFillRequest(fill); }), function (fill) { return __awaiter(_this, void 0, void 0, function () {
768
- var relayDataHash, fillToRefund_1, duplicateDeposits, historicalDeposit, matchedDeposit, fillToRefund;
701
+ var relayDataHash, historicalDeposit, matchedDeposit;
769
702
  return __generator(this, function (_a) {
770
703
  switch (_a.label) {
771
704
  case 0:
772
- fillCounter++;
773
705
  relayDataHash = this.getRelayHashFromEvent(fill);
774
- if (!v3RelayHashes[relayDataHash]) return [3 /*break*/, 5];
775
- if (!!v3RelayHashes[relayDataHash].fill) return [3 /*break*/, 3];
776
- assert(isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
777
- v3RelayHashes[relayDataHash].fill = fill;
778
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
779
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
780
- case 1:
781
- fillToRefund_1 = _a.sent();
782
- if (!isDefined(fillToRefund_1)) {
783
- bundleUnrepayableFillsV3.push(fill);
784
- // We don't return here yet because we still need to mark unexecutable slow fill leaves
785
- // or duplicate deposits. However, we won't issue a fast fill refund.
786
- }
787
- else {
788
- v3RelayHashes[relayDataHash].fill = fillToRefund_1;
789
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund_1), { quoteTimestamp: v3RelayHashes[relayDataHash].deposits[0].quoteTimestamp }));
790
- duplicateDeposits = v3RelayHashes[relayDataHash].deposits.slice(1);
791
- duplicateDeposits.forEach(function (duplicateDeposit) {
792
- if (isSlowFill(fill)) {
793
- updateExpiredDepositsV3(expiredDepositsToRefundV3, duplicateDeposit);
794
- }
795
- else {
796
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund_1), { quoteTimestamp: duplicateDeposit.quoteTimestamp }));
706
+ fillCounter++;
707
+ if (v3RelayHashes[relayDataHash]) {
708
+ if (!v3RelayHashes[relayDataHash].fill) {
709
+ assert(isDefined(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
710
+ // At this point, the v3RelayHashes entry already existed meaning that there is a matching deposit,
711
+ // so this fill is validated.
712
+ v3RelayHashes[relayDataHash].fill = fill;
713
+ if (fill.blockNumber >= destinationChainBlockRange[0]) {
714
+ validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: v3RelayHashes[relayDataHash].deposit.quoteTimestamp }));
715
+ // If fill replaced a slow fill request, then mark it as one that might have created an
716
+ // unexecutable slow fill. We can't know for sure until we check the slow fill request
717
+ // events.
718
+ // slow fill requests for deposits from or to lite chains are considered invalid
719
+ if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
720
+ _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposit)) {
721
+ fastFillsReplacingSlowFills.push(relayDataHash);
722
+ }
797
723
  }
798
- });
799
- }
800
- // If fill replaced a slow fill request, then mark it as one that might have created an
801
- // unexecutable slow fill. We can't know for sure until we check the slow fill request
802
- // events.
803
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
804
- _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposits[0])) {
805
- fastFillsReplacingSlowFills.push(relayDataHash);
724
+ }
725
+ return [2 /*return*/];
806
726
  }
807
- _a.label = 2;
808
- case 2: return [3 /*break*/, 4];
809
- case 3: throw new Error("Duplicate fill detected");
810
- case 4: return [2 /*return*/];
811
- case 5:
812
727
  // At this point, there is no relay hash dictionary entry for this fill, so we need to
813
- // instantiate the entry. We won't modify the fill.relayer until we match it with a deposit.
728
+ // instantiate the entry.
814
729
  v3RelayHashes[relayDataHash] = {
815
- deposits: undefined,
730
+ deposit: undefined,
816
731
  fill: fill,
817
732
  slowFillRequest: undefined,
818
733
  };
819
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 9];
734
+ if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
820
735
  // Fill has a non-infinite expiry, and we can assume our spoke pool clients have old enough deposits
821
736
  // to conclude that this fill is invalid if we haven't found a matching deposit in memory, so
822
737
  // skip the historical query.
@@ -825,57 +740,35 @@ var BundleDataClient = /** @class */ (function () {
825
740
  return [2 /*return*/];
826
741
  }
827
742
  return [4 /*yield*/, queryHistoricalDepositForFill(originClient, fill)];
828
- case 6:
743
+ case 1:
829
744
  historicalDeposit = _a.sent();
830
- if (!!historicalDeposit.found) return [3 /*break*/, 7];
831
- bundleInvalidFillsV3.push(fill);
832
- return [3 /*break*/, 9];
833
- case 7:
834
- matchedDeposit = historicalDeposit.deposit;
835
- // If deposit is in a following bundle, then this fill will have to be refunded once that deposit
836
- // is in the current bundle.
837
- if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
745
+ if (!historicalDeposit.found) {
838
746
  bundleInvalidFillsV3.push(fill);
839
- return [2 /*return*/];
840
- }
841
- v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
842
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, matchedDeposit, allChainIds)];
843
- case 8:
844
- fillToRefund = _a.sent();
845
- if (!isDefined(fillToRefund)) {
846
- bundleUnrepayableFillsV3.push(fill);
847
- // Don't return yet as we still need to mark down any unexecutable slow fill leaves
848
- // in case this fast fill replaced a slow fill request.
849
747
  }
850
748
  else {
749
+ matchedDeposit = historicalDeposit.deposit;
851
750
  // @dev Since queryHistoricalDepositForFill validates the fill by checking individual
852
751
  // object property values against the deposit's, we
853
752
  // sanity check it here by comparing the full relay hashes. If there's an error here then the
854
753
  // historical deposit query is not working as expected.
855
754
  assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
856
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
857
- v3RelayHashes[relayDataHash].fill = fillToRefund;
858
- // No need to check for duplicate deposits here since duplicate deposits with
859
- // infinite deadlines are impossible to send via unsafeDeposit().
860
- }
861
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
862
- _canCreateSlowFillLeaf(matchedDeposit)) {
863
- fastFillsReplacingSlowFills.push(relayDataHash);
755
+ validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
756
+ v3RelayHashes[relayDataHash].deposit = matchedDeposit;
757
+ // slow fill requests for deposits from or to lite chains are considered invalid
758
+ if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
759
+ _canCreateSlowFillLeaf(matchedDeposit)) {
760
+ fastFillsReplacingSlowFills.push(relayDataHash);
761
+ }
864
762
  }
865
- _a.label = 9;
866
- case 9: return [2 /*return*/];
763
+ _a.label = 2;
764
+ case 2: return [2 /*return*/];
867
765
  }
868
766
  });
869
767
  }); })];
870
768
  case 1:
871
769
  _g.sent();
872
- // Process slow fill requests and produce slow fill leaves while maintaining the following the invariants:
873
- // - Slow fill leaves cannot be produced for deposits that have expired in this bundle.
874
- // - Slow fill leaves cannot be produced for deposits that have been filled.
875
- // Assumptions about fills:
876
- // - Duplicate slow fill requests for the same relay data hash are impossible to send.
877
- // - Slow fill requests can only be sent before the deposit's fillDeadline.
878
- // - Slow fill requests for a deposit that has been filled.
770
+ // Process slow fill requests. One invariant we need to maintain is that we cannot create slow fill requests
771
+ // for deposits that would expire in this bundle.
879
772
  return [4 /*yield*/, forEachAsync(destinationClient
880
773
  .getSlowFillRequestsForOriginChain(originChainId)
881
774
  .filter(function (request) {
@@ -888,58 +781,55 @@ var BundleDataClient = /** @class */ (function () {
888
781
  relayDataHash = this.getRelayHashFromEvent(slowFillRequest);
889
782
  if (v3RelayHashes[relayDataHash]) {
890
783
  if (!v3RelayHashes[relayDataHash].slowFillRequest) {
784
+ // At this point, the v3RelayHashes entry already existed meaning that there is either a matching
785
+ // fill or deposit.
891
786
  v3RelayHashes[relayDataHash].slowFillRequest = slowFillRequest;
892
787
  if (v3RelayHashes[relayDataHash].fill) {
893
- // Exiting here assumes that slow fill requests must precede fills, so if there was a fill
894
- // following this slow fill request, then we would have already seen it. We don't need to check
895
- // for a fill older than this slow fill request.
788
+ // If there is a fill matching the relay hash, then this slow fill request can't be used
789
+ // to create a slow fill for a filled deposit.
896
790
  return [2 /*return*/];
897
791
  }
898
- assert(isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
899
- matchedDeposit = v3RelayHashes[relayDataHash].deposits[0];
792
+ assert(isDefined(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
793
+ matchedDeposit = v3RelayHashes[relayDataHash].deposit;
794
+ // If there is no fill matching the relay hash, then this might be a valid slow fill request
795
+ // that we should produce a slow fill leaf for. Check if the slow fill request is in the
796
+ // destination chain block range.
900
797
  if (slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
901
798
  _canCreateSlowFillLeaf(matchedDeposit) &&
799
+ // Deposit must not have expired in this bundle.
902
800
  !_depositIsExpired(matchedDeposit)) {
801
+ // At this point, the v3RelayHashes entry already existed meaning that there is a matching deposit,
802
+ // so this slow fill request relay data is correct.
903
803
  validatedBundleSlowFills.push(matchedDeposit);
904
804
  }
905
805
  }
906
- else {
907
- throw new Error("Duplicate slow fill request detected.");
908
- }
909
806
  return [2 /*return*/];
910
807
  }
911
808
  // Instantiate dictionary if there is neither a deposit nor fill matching it.
912
809
  v3RelayHashes[relayDataHash] = {
913
- deposits: undefined,
810
+ deposit: undefined,
914
811
  fill: undefined,
915
812
  slowFillRequest: slowFillRequest,
916
813
  };
917
- if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
918
- if (!INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline)) {
919
- bundleInvalidSlowFillRequests.push(slowFillRequest);
920
- return [2 /*return*/];
921
- }
814
+ if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
815
+ INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline))) return [3 /*break*/, 2];
922
816
  return [4 /*yield*/, queryHistoricalDepositForFill(originClient, slowFillRequest)];
923
817
  case 1:
924
818
  historicalDeposit = _a.sent();
925
819
  if (!historicalDeposit.found) {
926
- bundleInvalidSlowFillRequests.push(slowFillRequest);
820
+ // TODO: Invalid slow fill request. Maybe worth logging.
927
821
  return [2 /*return*/];
928
822
  }
929
823
  matchedDeposit = historicalDeposit.deposit;
930
- // If deposit is in a following bundle, then this slow fill request will have to be created
931
- // once that deposit is in the current bundle.
932
- if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
933
- bundleInvalidSlowFillRequests.push(slowFillRequest);
934
- return [2 /*return*/];
935
- }
936
824
  // @dev Since queryHistoricalDepositForFill validates the slow fill request by checking individual
937
825
  // object property values against the deposit's, we
938
826
  // sanity check it here by comparing the full relay hashes. If there's an error here then the
939
827
  // historical deposit query is not working as expected.
940
828
  assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Deposit relay hashes should match.");
941
- v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
942
- if (!_canCreateSlowFillLeaf(matchedDeposit) || _depositIsExpired(matchedDeposit)) {
829
+ v3RelayHashes[relayDataHash].deposit = matchedDeposit;
830
+ if (!_canCreateSlowFillLeaf(matchedDeposit) ||
831
+ // Deposit must not have expired in this bundle.
832
+ _depositIsExpired(matchedDeposit)) {
943
833
  return [2 /*return*/];
944
834
  }
945
835
  validatedBundleSlowFills.push(matchedDeposit);
@@ -949,167 +839,116 @@ var BundleDataClient = /** @class */ (function () {
949
839
  });
950
840
  }); })];
951
841
  case 2:
952
- // Process slow fill requests and produce slow fill leaves while maintaining the following the invariants:
953
- // - Slow fill leaves cannot be produced for deposits that have expired in this bundle.
954
- // - Slow fill leaves cannot be produced for deposits that have been filled.
955
- // Assumptions about fills:
956
- // - Duplicate slow fill requests for the same relay data hash are impossible to send.
957
- // - Slow fill requests can only be sent before the deposit's fillDeadline.
958
- // - Slow fill requests for a deposit that has been filled.
842
+ // Process slow fill requests. One invariant we need to maintain is that we cannot create slow fill requests
843
+ // for deposits that would expire in this bundle.
959
844
  _g.sent();
960
- // Process deposits and maintain the following invariants:
961
- // - Deposits matching fills that are not type SlowFill from previous bundle block ranges should produce
962
- // refunds for those fills.
963
- // - Deposits matching fills that are type SlowFill from previous bundle block ranges should be refunded to the
964
- // depositor.
965
- // - All deposits expiring in this bundle, even those sent in prior bundle block ranges, should be refunded
966
- // to the depositor.
967
- // - An expired deposit cannot be refunded if the deposit was filled.
968
- // - If a deposit from a prior bundle expired in this bundle, had a slow fill request created for it
969
- // in a prior bundle, and has not been filled yet, then an unexecutable slow fill leaf has been created
970
- // and needs to be refunded to the HubPool.
971
- // - Deposits matching slow fill requests from previous bundle block ranges should produce slow fills
972
- // if the deposit has not been filled.
973
- // Assumptions:
974
- // - If the deposit has a matching fill or slow fill request in the bundle then we have already stored
975
- // it in the relay hashes dictionary.
976
- // - We've created refunds for all fills in this bundle matching a deposit.
977
- // - We've created slow fill leaves for all slow fill requests in this bundle matching an unfilled deposit.
978
- // - Deposits for the same relay data hash can be sent an arbitrary amount of times.
979
- // - Deposits can be sent an arbitrary amount of time after a fill has been sent for the matching relay data.
980
- return [4 /*yield*/, mapAsync(bundleDepositHashes, function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
981
- var _a, relayDataHash, index, _b, deposits, fill, slowFillRequest, deposit, fillToRefund, fillStatus, prefill, verifiedFill;
982
- var _this = this;
983
- return __generator(this, function (_c) {
984
- switch (_c.label) {
845
+ // Deposits can be submitted an arbitrary amount of time after matching fills and slow fill requests.
846
+ // Therefore, let's go through each deposit in this bundle again and check a few things in order:
847
+ // - Has the deposit been filled ? If so, then we need to issue a relayer refund for
848
+ // this "pre-fill" if the fill took place in a previous bundle.
849
+ // - Or, has the deposit expired in this bundle? If so, then we need to issue an expiry refund.
850
+ // - And finally, has the deposit been slow filled? If so, then we need to issue a slow fill leaf
851
+ // for this "pre-slow-fill-request" if this request took place in a previous bundle.
852
+ return [4 /*yield*/, mapAsync(bundleDepositHashes.filter(function (depositHash) {
853
+ var deposit = v3RelayHashes[depositHash].deposit;
854
+ return (deposit && deposit.originChainId === originChainId && deposit.destinationChainId === destinationChainId);
855
+ }), function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
856
+ var _a, deposit, fill, slowFillRequest, fillStatus, prefill;
857
+ return __generator(this, function (_b) {
858
+ switch (_b.label) {
985
859
  case 0:
986
- _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
987
- _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, fill = _b.fill, slowFillRequest = _b.slowFillRequest;
988
- if (!deposits || deposits.length === 0) {
989
- throw new Error("Deposits should exist in relay hash dictionary.");
990
- }
991
- deposit = deposits[index];
860
+ _a = v3RelayHashes[depositHash], deposit = _a.deposit, fill = _a.fill, slowFillRequest = _a.slowFillRequest;
992
861
  if (!deposit)
993
862
  throw new Error("Deposit should exist in relay hash dictionary.");
994
- if (deposit.originChainId !== originChainId || deposit.destinationChainId !== destinationChainId) {
863
+ // We are willing to refund a pre-fill multiple times for each duplicate deposit.
864
+ // This is because a duplicate deposit for a pre-fill cannot get
865
+ // refunded to the depositor anymore because its fill status on-chain has changed to Filled. Therefore
866
+ // any duplicate deposits result in a net loss of funds for the depositor and effectively pay out
867
+ // the pre-filler.
868
+ // If fill exists in memory, then the only case in which we need to create a refund is if the
869
+ // the fill occurred in a previous bundle. There are no expiry refunds for filled deposits.
870
+ if (fill) {
871
+ if (fill.blockNumber < destinationChainBlockRange[0] && !isSlowFill(fill)) {
872
+ // If fill is in the current bundle then we can assume there is already a refund for it, so only
873
+ // include this pre fill if the fill is in an older bundle. If fill is after this current bundle, then
874
+ // we won't consider it, following the previous treatment of fills after the bundle block range.
875
+ validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: deposit.quoteTimestamp }));
876
+ }
995
877
  return [2 /*return*/];
996
878
  }
997
- if (!fill) return [3 /*break*/, 3];
998
- if (!(canRefundPrefills && fill.blockNumber < destinationChainBlockRange[0])) return [3 /*break*/, 2];
999
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
1000
- case 1:
1001
- fillToRefund = _c.sent();
1002
- if (!isDefined(fillToRefund)) {
1003
- bundleUnrepayableFillsV3.push(fill);
1004
- }
1005
- else if (!isSlowFill(fill)) {
1006
- v3RelayHashes[relayDataHash].fill = fillToRefund;
1007
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund), { quoteTimestamp: deposit.quoteTimestamp }));
1008
- }
1009
- else {
1010
- updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1011
- }
1012
- _c.label = 2;
1013
- case 2: return [2 /*return*/];
1014
- case 3:
1015
879
  // If a slow fill request exists in memory, then we know the deposit has not been filled because fills
1016
- // must follow slow fill requests and we would have seen the fill already if it existed.,
1017
- // We can conclude that either the deposit has expired or we need to create a slow fill leaf for the
1018
- // deposit because it has not been filled. Slow fill leaves were already created for requests sent
1019
- // in the current bundle so only create new slow fill leaves for prior bundle deposits.
880
+ // must follow slow fill requests and we would have seen the fill already if it existed. Therefore,
881
+ // we can conclude that either the deposit has expired and we need to create a deposit expiry refund, or
882
+ // we need to create a slow fill leaf for the deposit. The latter should only happen if the slow fill request
883
+ // took place in a prior bundle otherwise we would have already created a slow fill leaf for it.
1020
884
  if (slowFillRequest) {
1021
885
  if (_depositIsExpired(deposit)) {
1022
886
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1023
887
  }
1024
- else if (canRefundPrefills &&
1025
- slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
1026
- _canCreateSlowFillLeaf(deposit) &&
1027
- validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
888
+ else if (slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
889
+ _canCreateSlowFillLeaf(deposit)) {
1028
890
  validatedBundleSlowFills.push(deposit);
1029
891
  }
1030
892
  return [2 /*return*/];
1031
893
  }
1032
894
  return [4 /*yield*/, _getFillStatusForDeposit(deposit, destinationChainBlockRange[1])];
1033
- case 4:
1034
- fillStatus = _c.sent();
1035
- if (!(fillStatus === FillStatus.Filled)) return [3 /*break*/, 8];
1036
- return [4 /*yield*/, this.findMatchingFillEvent(deposit, destinationClient)];
1037
- case 5:
1038
- prefill = _c.sent();
1039
- assert(isDefined(prefill), "findFillEvent# Cannot find prefill: ".concat(relayDataHash));
1040
- assert(this.getRelayHashFromEvent(prefill) === relayDataHash, "Relay hashes should match.");
1041
- if (!canRefundPrefills) return [3 /*break*/, 7];
1042
- return [4 /*yield*/, verifyFillRepayment(prefill, destinationClient.spokePool.provider, deposit, allChainIds)];
1043
- case 6:
1044
- verifiedFill = _c.sent();
1045
- if (!isDefined(verifiedFill)) {
1046
- bundleUnrepayableFillsV3.push(prefill);
1047
- }
1048
- else if (!isSlowFill(verifiedFill)) {
1049
- validatedBundleV3Fills.push(__assign(__assign({}, verifiedFill), { quoteTimestamp: deposit.quoteTimestamp }));
1050
- }
1051
- else {
1052
- updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
895
+ case 1:
896
+ fillStatus = _b.sent();
897
+ if (!(fillStatus === FillStatus.Filled)) return [3 /*break*/, 3];
898
+ return [4 /*yield*/, findFillEvent(destinationClient.spokePool, deposit, destinationClient.deploymentBlock, destinationClient.latestBlockSearched)];
899
+ case 2:
900
+ prefill = (_b.sent());
901
+ if (!isSlowFill(prefill)) {
902
+ validatedBundleV3Fills.push(__assign(__assign({}, prefill), { quoteTimestamp: deposit.quoteTimestamp }));
1053
903
  }
1054
- _c.label = 7;
1055
- case 7: return [3 /*break*/, 9];
1056
- case 8:
904
+ return [3 /*break*/, 4];
905
+ case 3:
1057
906
  if (_depositIsExpired(deposit)) {
1058
907
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1059
908
  }
1060
- else if (fillStatus === FillStatus.RequestedSlowFill &&
1061
- // Don't create duplicate slow fill requests for the same deposit.
1062
- validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
1063
- if (canRefundPrefills && _canCreateSlowFillLeaf(deposit)) {
909
+ // If slow fill requested, then issue a slow fill leaf for the deposit.
910
+ else if (fillStatus === FillStatus.RequestedSlowFill) {
911
+ // Input and Output tokens must be equivalent on the deposit for this to be slow filled.
912
+ // Slow fill requests for deposits from or to lite chains are considered invalid
913
+ if (_canCreateSlowFillLeaf(deposit)) {
914
+ // If deposit newly expired, then we can't create a slow fill leaf for it but we can
915
+ // create a deposit refund for it.
1064
916
  validatedBundleSlowFills.push(deposit);
1065
917
  }
1066
918
  }
1067
- _c.label = 9;
1068
- case 9: return [2 /*return*/];
919
+ _b.label = 4;
920
+ case 4: return [2 /*return*/];
1069
921
  }
1070
922
  });
1071
923
  }); })];
1072
924
  case 3:
1073
- // Process deposits and maintain the following invariants:
1074
- // - Deposits matching fills that are not type SlowFill from previous bundle block ranges should produce
1075
- // refunds for those fills.
1076
- // - Deposits matching fills that are type SlowFill from previous bundle block ranges should be refunded to the
1077
- // depositor.
1078
- // - All deposits expiring in this bundle, even those sent in prior bundle block ranges, should be refunded
1079
- // to the depositor.
1080
- // - An expired deposit cannot be refunded if the deposit was filled.
1081
- // - If a deposit from a prior bundle expired in this bundle, had a slow fill request created for it
1082
- // in a prior bundle, and has not been filled yet, then an unexecutable slow fill leaf has been created
1083
- // and needs to be refunded to the HubPool.
1084
- // - Deposits matching slow fill requests from previous bundle block ranges should produce slow fills
1085
- // if the deposit has not been filled.
1086
- // Assumptions:
1087
- // - If the deposit has a matching fill or slow fill request in the bundle then we have already stored
1088
- // it in the relay hashes dictionary.
1089
- // - We've created refunds for all fills in this bundle matching a deposit.
1090
- // - We've created slow fill leaves for all slow fill requests in this bundle matching an unfilled deposit.
1091
- // - Deposits for the same relay data hash can be sent an arbitrary amount of times.
1092
- // - Deposits can be sent an arbitrary amount of time after a fill has been sent for the matching relay data.
925
+ // Deposits can be submitted an arbitrary amount of time after matching fills and slow fill requests.
926
+ // Therefore, let's go through each deposit in this bundle again and check a few things in order:
927
+ // - Has the deposit been filled ? If so, then we need to issue a relayer refund for
928
+ // this "pre-fill" if the fill took place in a previous bundle.
929
+ // - Or, has the deposit expired in this bundle? If so, then we need to issue an expiry refund.
930
+ // - And finally, has the deposit been slow filled? If so, then we need to issue a slow fill leaf
931
+ // for this "pre-slow-fill-request" if this request took place in a previous bundle.
1093
932
  _g.sent();
1094
933
  // For all fills that came after a slow fill request, we can now check if the slow fill request
1095
934
  // was a valid one and whether it was created in a previous bundle. If so, then it created a slow fill
1096
935
  // leaf that is now unexecutable.
1097
936
  fastFillsReplacingSlowFills.forEach(function (relayDataHash) {
1098
- var _a = v3RelayHashes[relayDataHash], deposits = _a.deposits, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
937
+ var _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
1099
938
  assert((fill === null || fill === void 0 ? void 0 : fill.relayExecutionInfo.fillType) === FillType.ReplacedSlowFill, "Fill type should be ReplacedSlowFill.");
1100
939
  // Needed for TSC - are implicitely checking that deposit exists by making it to this point.
1101
- if (!deposits || deposits.length < 1) {
940
+ if (!deposit) {
1102
941
  throw new Error("Deposit should exist in relay hash dictionary.");
1103
942
  }
1104
943
  // We should never push fast fills involving lite chains here because slow fill requests for them are invalid:
1105
- assert(_canCreateSlowFillLeaf(deposits[0]), "fastFillsReplacingSlowFills should contain only deposits that can be slow filled");
944
+ assert(_canCreateSlowFillLeaf(deposit), "fastFillsReplacingSlowFills should contain only deposits that can be slow filled");
1106
945
  var destinationBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
1107
946
  if (
1108
947
  // If there is a slow fill request in this bundle that matches the relay hash, then there was no slow fill
1109
948
  // created that would be considered excess.
1110
949
  !slowFillRequest ||
1111
950
  slowFillRequest.blockNumber < destinationBlockRange[0]) {
1112
- validatedBundleUnexecutableSlowFills.push(deposits[0]);
951
+ validatedBundleUnexecutableSlowFills.push(deposit);
1113
952
  }
1114
953
  });
1115
954
  return [2 /*return*/];
@@ -1153,17 +992,13 @@ var BundleDataClient = /** @class */ (function () {
1153
992
  // For all deposits older than this bundle, we need to check if they expired in this bundle and if they did,
1154
993
  // whether there was a slow fill created for it in a previous bundle that is now unexecutable and replaced
1155
994
  // by a new expired deposit refund.
1156
- return [4 /*yield*/, forEachAsync(olderDepositHashes, function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
1157
- var _a, relayDataHash, index, _b, deposits, slowFillRequest, fill, deposit, destinationChainId, destinationBlockRange, fillStatus;
1158
- return __generator(this, function (_c) {
1159
- switch (_c.label) {
995
+ return [4 /*yield*/, forEachAsync(olderDepositHashes, function (relayDataHash) { return __awaiter(_this, void 0, void 0, function () {
996
+ var _a, deposit, slowFillRequest, fill, destinationChainId, destinationBlockRange, fillStatus;
997
+ return __generator(this, function (_b) {
998
+ switch (_b.label) {
1160
999
  case 0:
1161
- _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
1162
- _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, slowFillRequest = _b.slowFillRequest, fill = _b.fill;
1163
- if (!deposits || deposits.length < 1) {
1164
- throw new Error("Deposit should exist in relay hash dictionary.");
1165
- }
1166
- deposit = deposits[index];
1000
+ _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
1001
+ assert(isDefined(deposit), "Deposit should exist in relay hash dictionary.");
1167
1002
  destinationChainId = deposit.destinationChainId;
1168
1003
  destinationBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
1169
1004
  if (!
@@ -1177,7 +1012,7 @@ var BundleDataClient = /** @class */ (function () {
1177
1012
  return [3 /*break*/, 2];
1178
1013
  return [4 /*yield*/, _getFillStatusForDeposit(deposit, destinationBlockRange[1])];
1179
1014
  case 1:
1180
- fillStatus = _c.sent();
1015
+ fillStatus = _b.sent();
1181
1016
  // If there is no matching fill and the deposit expired in this bundle and the fill status on-chain is not
1182
1017
  // Filled, then we can to refund it as an expired deposit.
1183
1018
  if (fillStatus !== FillStatus.Filled) {
@@ -1199,7 +1034,7 @@ var BundleDataClient = /** @class */ (function () {
1199
1034
  if (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0]) {
1200
1035
  validatedBundleUnexecutableSlowFills.push(deposit);
1201
1036
  }
1202
- _c.label = 2;
1037
+ _b.label = 2;
1203
1038
  case 2: return [2 /*return*/];
1204
1039
  }
1205
1040
  });
@@ -1214,7 +1049,7 @@ var BundleDataClient = /** @class */ (function () {
1214
1049
  promises = [
1215
1050
  validatedBundleV3Fills.length > 0
1216
1051
  ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleV3Fills.map(function (fill) {
1217
- var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
1052
+ var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1218
1053
  assert(isDefined(matchedDeposit), "Deposit should exist in relay hash dictionary.");
1219
1054
  var paymentChainId = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, matchedDeposit.fromLiteChain).chainToSendRefundTo;
1220
1055
  return __assign(__assign({}, fill), { paymentChainId: paymentChainId });
@@ -1241,23 +1076,14 @@ var BundleDataClient = /** @class */ (function () {
1241
1076
  v3FillLpFees.forEach(function (_a, idx) {
1242
1077
  var realizedLpFeePct = _a.realizedLpFeePct;
1243
1078
  var fill = validatedBundleV3Fills[idx];
1244
- var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
1079
+ var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1245
1080
  assert(isDefined(associatedDeposit), "Deposit should exist in relay hash dictionary.");
1246
1081
  var _b = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, associatedDeposit.fromLiteChain), chainToSendRefundTo = _b.chainToSendRefundTo, repaymentToken = _b.repaymentToken;
1247
- updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken, fill.relayer);
1082
+ updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken);
1248
1083
  });
1249
1084
  v3SlowFillLpFees.forEach(function (_a, idx) {
1250
1085
  var lpFeePct = _a.realizedLpFeePct;
1251
1086
  var deposit = validatedBundleSlowFills[idx];
1252
- // We should not create slow fill leaves for duplicate deposit hashes and we should only create a slow
1253
- // fill leaf for the first deposit (the quote timestamp of the deposit determines the LP fee, so its
1254
- // important we pick out the correct deposit). Deposits are pushed into validatedBundleSlowFills in ascending
1255
- // order so the following slice will only match the first deposit.
1256
- var relayDataHash = _this.getRelayHashFromEvent(deposit);
1257
- if (validatedBundleSlowFills.slice(0, idx).some(function (d) { return _this.getRelayHashFromEvent(d) === relayDataHash; })) {
1258
- return;
1259
- }
1260
- assert(!_depositIsExpired(deposit), "Cannot create slow fill leaf for expired deposit.");
1261
1087
  updateBundleSlowFills(bundleSlowFillsV3, __assign(__assign({}, deposit), { lpFeePct: lpFeePct }));
1262
1088
  });
1263
1089
  v3UnexecutableSlowFillLpFees.forEach(function (_a, idx) {
@@ -1265,31 +1091,15 @@ var BundleDataClient = /** @class */ (function () {
1265
1091
  var deposit = validatedBundleUnexecutableSlowFills[idx];
1266
1092
  updateBundleExcessSlowFills(unexecutableSlowFills, __assign(__assign({}, deposit), { lpFeePct: lpFeePct }));
1267
1093
  });
1268
- v3SpokeEventsReadable = prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
1094
+ v3SpokeEventsReadable = prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
1269
1095
  if (bundleInvalidFillsV3.length > 0) {
1270
1096
  this.logger.debug({
1271
1097
  at: "BundleDataClient#loadData",
1272
- message: "Finished loading V3 spoke pool data and found some invalid fills in range",
1098
+ message: "Finished loading V3 spoke pool data and found some invalid V3 fills in range",
1273
1099
  blockRangesForChains: blockRangesForChains,
1274
1100
  bundleInvalidFillsV3: bundleInvalidFillsV3,
1275
1101
  });
1276
1102
  }
1277
- if (bundleUnrepayableFillsV3.length > 0) {
1278
- this.logger.debug({
1279
- at: "BundleDataClient#loadData",
1280
- message: "Finished loading V3 spoke pool data and found some unrepayable fills in range",
1281
- blockRangesForChains: blockRangesForChains,
1282
- bundleUnrepayableFillsV3: bundleUnrepayableFillsV3,
1283
- });
1284
- }
1285
- if (bundleInvalidSlowFillRequests.length > 0) {
1286
- this.logger.debug({
1287
- at: "BundleDataClient#loadData",
1288
- message: "Finished loading V3 spoke pool data and found some invalid slow fill requests in range",
1289
- blockRangesForChains: blockRangesForChains,
1290
- bundleInvalidSlowFillRequests: bundleInvalidSlowFillRequests,
1291
- });
1292
- }
1293
1103
  this.logger.debug({
1294
1104
  at: "BundleDataClient#loadDataFromScratch",
1295
1105
  message: "Computed bundle data in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -1314,16 +1124,6 @@ var BundleDataClient = /** @class */ (function () {
1314
1124
  BundleDataClient.prototype.getRelayHashFromEvent = function (event) {
1315
1125
  return "".concat(event.depositor, "-").concat(event.recipient, "-").concat(event.exclusiveRelayer, "-").concat(event.inputToken, "-").concat(event.outputToken, "-").concat(event.inputAmount, "-").concat(event.outputAmount, "-").concat(event.originChainId, "-").concat(event.depositId.toString(), "-").concat(event.fillDeadline, "-").concat(event.exclusivityDeadline, "-").concat(event.message, "-").concat(event.destinationChainId);
1316
1126
  };
1317
- BundleDataClient.prototype.findMatchingFillEvent = function (deposit, spokePoolClient) {
1318
- return __awaiter(this, void 0, void 0, function () {
1319
- return __generator(this, function (_a) {
1320
- switch (_a.label) {
1321
- case 0: return [4 /*yield*/, findFillEvent(spokePoolClient.spokePool, deposit, spokePoolClient.deploymentBlock, spokePoolClient.latestBlockSearched)];
1322
- case 1: return [2 /*return*/, _a.sent()];
1323
- }
1324
- });
1325
- });
1326
- };
1327
1127
  BundleDataClient.prototype.getBundleBlockTimestamps = function (chainIds, blockRangesForChains, spokePoolClients) {
1328
1128
  return __awaiter(this, void 0, void 0, function () {
1329
1129
  var _a, _b;
@@ -1333,7 +1133,7 @@ var BundleDataClient = /** @class */ (function () {
1333
1133
  case 0:
1334
1134
  _b = (_a = Object).fromEntries;
1335
1135
  return [4 /*yield*/, mapAsync(chainIds, function (chainId, index) { return __awaiter(_this, void 0, void 0, function () {
1336
- var blockRangeForChain, _startBlockForChain, _endBlockForChain, spokePoolClient, startBlockForChain, endBlockForChain, _a, startTime, _endTime, _b, endBlockDelta, endTime;
1136
+ var blockRangeForChain, _startBlockForChain, _endBlockForChain, spokePoolClient, startBlockForChain, endBlockForChain, _a, startTime, endTime, _b;
1337
1137
  return __generator(this, function (_c) {
1338
1138
  switch (_c.label) {
1339
1139
  case 0:
@@ -1348,7 +1148,7 @@ var BundleDataClient = /** @class */ (function () {
1348
1148
  return [2 /*return*/];
1349
1149
  }
1350
1150
  startBlockForChain = Math.min(_startBlockForChain, spokePoolClient.latestBlockSearched);
1351
- endBlockForChain = Math.min(_endBlockForChain + 1, spokePoolClient.latestBlockSearched);
1151
+ endBlockForChain = Math.min(_endBlockForChain, spokePoolClient.latestBlockSearched);
1352
1152
  return [4 /*yield*/, spokePoolClient.getTimestampForBlock(startBlockForChain)];
1353
1153
  case 1:
1354
1154
  _b = [
@@ -1358,11 +1158,9 @@ var BundleDataClient = /** @class */ (function () {
1358
1158
  case 2:
1359
1159
  _a = _b.concat([
1360
1160
  _c.sent()
1361
- ]), startTime = _a[0], _endTime = _a[1];
1362
- endBlockDelta = endBlockForChain > startBlockForChain ? 1 : 0;
1363
- endTime = Math.max(0, _endTime - endBlockDelta);
1161
+ ]), startTime = _a[0], endTime = _a[1];
1364
1162
  // Sanity checks:
1365
- assert(endTime >= startTime, "End time for block ".concat(endBlockForChain, " should be greater than start time for block ").concat(startBlockForChain, ": ").concat(endTime, " >= ").concat(startTime, "."));
1163
+ assert(endTime >= startTime, "End time should be greater than start time.");
1366
1164
  assert(startBlockForChain === 0 || startTime > 0, "Start timestamp must be greater than 0 if the start block is greater than 0.");
1367
1165
  return [2 /*return*/, [chainId, [startTime, endTime]]];
1368
1166
  }