@across-protocol/sdk 4.0.0-beta.33 → 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 +174 -336
  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 +256 -439
  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 +224 -418
  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,15 +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
- assert(chainIsEvm(deposit.originChainId) && isValidEvmAddress(deposit.depositor), "expired depositor cannot be refunded");
13
11
  var originChainId = deposit.originChainId, inputToken = deposit.inputToken;
14
12
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[originChainId]) === null || _a === void 0 ? void 0 : _a[inputToken])) {
15
13
  assign(dict, [originChainId, inputToken], []);
@@ -24,10 +22,8 @@ function updateBundleDepositsV3(dict, deposit) {
24
22
  }
25
23
  dict[originChainId][inputToken].push(deposit);
26
24
  }
27
- function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken, repaymentAddress) {
25
+ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken) {
28
26
  var _a, _b;
29
- // We shouldn't pass any unrepayable fills into this function, so we perform an extra safety check.
30
- assert(chainIsEvm(repaymentChainId) && isEvmRepaymentValid(fill, repaymentChainId), "validatedBundleV3Fills dictionary should only contain fills with valid repayment information");
31
27
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[repaymentChainId]) === null || _a === void 0 ? void 0 : _a[repaymentToken])) {
32
28
  assign(dict, [repaymentChainId, repaymentToken], {
33
29
  fills: [],
@@ -36,26 +32,26 @@ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentTo
36
32
  refunds: {},
37
33
  });
38
34
  }
39
- var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct, relayer: repaymentAddress });
35
+ var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct });
40
36
  // Add all fills, slow and fast, to dictionary.
41
37
  assign(dict, [repaymentChainId, repaymentToken, "fills"], [bundleFill]);
42
38
  // All fills update the bundle LP fees.
43
39
  var refundObj = dict[repaymentChainId][repaymentToken];
44
- var realizedLpFee = bundleFill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
40
+ var realizedLpFee = fill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
45
41
  refundObj.realizedLpFees = refundObj.realizedLpFees ? refundObj.realizedLpFees.add(realizedLpFee) : realizedLpFee;
46
42
  // Only fast fills get refunded.
47
- if (!isSlowFill(bundleFill)) {
48
- 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);
49
45
  refundObj.totalRefundAmount = refundObj.totalRefundAmount
50
46
  ? refundObj.totalRefundAmount.add(refundAmount)
51
47
  : refundAmount;
52
48
  // Instantiate dictionary if it doesn't exist.
53
49
  (_b = refundObj.refunds) !== null && _b !== void 0 ? _b : (refundObj.refunds = {});
54
- if (refundObj.refunds[bundleFill.relayer]) {
55
- 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);
56
52
  }
57
53
  else {
58
- refundObj.refunds[bundleFill.relayer] = refundAmount;
54
+ refundObj.refunds[fill.relayer] = refundAmount;
59
55
  }
60
56
  }
61
57
  }
@@ -69,7 +65,6 @@ function updateBundleExcessSlowFills(dict, deposit) {
69
65
  }
70
66
  function updateBundleSlowFills(dict, deposit) {
71
67
  var _a;
72
- assert(chainIsEvm(deposit.destinationChainId) && isValidEvmAddress(deposit.recipient), "slow fill recipient cannot be paid");
73
68
  var destinationChainId = deposit.destinationChainId, outputToken = deposit.outputToken;
74
69
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[destinationChainId]) === null || _a === void 0 ? void 0 : _a[outputToken])) {
75
70
  assign(dict, [destinationChainId, outputToken], []);
@@ -167,7 +162,8 @@ var BundleDataClient = /** @class */ (function () {
167
162
  at: "BundleDataClient#loadPersistedDataFromArweave",
168
163
  message: "Loaded persisted data from Arweave in ".concat(Math.round(performance.now() - start) / 1000, "s."),
169
164
  blockRanges: JSON.stringify(blockRangesForChains),
170
- 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),
171
167
  });
172
168
  return [2 /*return*/, bundleData];
173
169
  }
@@ -213,124 +209,77 @@ var BundleDataClient = /** @class */ (function () {
213
209
  return [4 /*yield*/, this.loadArweaveData(bundleEvaluationBlockRanges)];
214
210
  case 1:
215
211
  arweaveData = _a.sent();
216
- if (!(arweaveData === undefined)) return [3 /*break*/, 3];
217
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges)];
218
- case 2:
219
- combinedRefunds = _a.sent();
220
- return [3 /*break*/, 4];
221
- case 3:
222
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
223
- combinedRefunds = getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3);
224
- // If we don't have a spoke pool client for a chain, then we won't be able to deduct refunds correctly for this
225
- // chain. For most of the pending bundle's liveness period, these past refunds are already executed so this is
226
- // a reasonable assumption. This empty refund chain also matches what the alternative
227
- // `getApproximateRefundsForBlockRange` would return.
228
- Object.keys(combinedRefunds).forEach(function (chainId) {
229
- if (_this.spokePoolClients[Number(chainId)] === undefined) {
230
- delete combinedRefunds[Number(chainId)];
231
- }
232
- });
233
- _a.label = 4;
234
- case 4:
235
- // The latest proposed bundle's refund leaves might have already been partially or entirely executed.
236
- // We have to deduct the executed amounts from the total refund amounts.
237
- 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)];
238
231
  }
239
232
  });
240
233
  });
241
234
  };
242
235
  // @dev This helper function should probably be moved to the InventoryClient
243
236
  BundleDataClient.prototype.getApproximateRefundsForBlockRange = function (chainIds, blockRanges) {
244
- return __awaiter(this, void 0, void 0, function () {
245
- var refundsForChain, _loop_1, this_1, _i, chainIds_1, chainId;
246
- var _this = this;
247
- return __generator(this, function (_a) {
248
- switch (_a.label) {
249
- case 0:
250
- refundsForChain = {};
251
- _loop_1 = function (chainId) {
252
- var chainIndex, fillsToCount;
253
- return __generator(this, function (_b) {
254
- switch (_b.label) {
255
- case 0:
256
- if (this_1.spokePoolClients[chainId] === undefined) {
257
- return [2 /*return*/, "continue"];
258
- }
259
- chainIndex = chainIds.indexOf(chainId);
260
- return [4 /*yield*/, filterAsync(this_1.spokePoolClients[chainId].getFills(), function (fill) { return __awaiter(_this, void 0, void 0, function () {
261
- var matchingDeposit, hasMatchingDeposit, validRepayment;
262
- return __generator(this, function (_a) {
263
- switch (_a.label) {
264
- case 0:
265
- if (fill.blockNumber < blockRanges[chainIndex][0] ||
266
- fill.blockNumber > blockRanges[chainIndex][1] ||
267
- isZeroValueFillOrSlowFillRequest(fill)) {
268
- return [2 /*return*/, false];
269
- }
270
- // If origin spoke pool client isn't defined, we can't validate it.
271
- if (this.spokePoolClients[fill.originChainId] === undefined) {
272
- return [2 /*return*/, false];
273
- }
274
- matchingDeposit = this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
275
- hasMatchingDeposit = matchingDeposit !== undefined &&
276
- this.getRelayHashFromEvent(fill) === this.getRelayHashFromEvent(matchingDeposit);
277
- if (!hasMatchingDeposit) return [3 /*break*/, 2];
278
- return [4 /*yield*/, verifyFillRepayment(fill, this.spokePoolClients[fill.destinationChainId].spokePool.provider, matchingDeposit,
279
- // @dev: to get valid repayment chain ID's, get all chain IDs for the bundle block range and remove
280
- // disabled block ranges.
281
- this.clients.configStoreClient
282
- .getChainIdIndicesForBlock(blockRanges[0][1])
283
- .filter(function (_chainId, i) { return !isChainDisabled(blockRanges[i]); }))];
284
- case 1:
285
- validRepayment = _a.sent();
286
- if (!isDefined(validRepayment)) {
287
- return [2 /*return*/, false];
288
- }
289
- _a.label = 2;
290
- case 2: return [2 /*return*/, hasMatchingDeposit];
291
- }
292
- });
293
- }); })];
294
- case 1:
295
- fillsToCount = _b.sent();
296
- fillsToCount.forEach(function (fill) {
297
- var _a, _b, _c;
298
- var _d;
299
- var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
300
- assert(isDefined(matchingDeposit), "Deposit not found for fill.");
301
- var _e = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain // Use ! because we've already asserted that matchingDeposit is defined.
302
- ), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
303
- // Assume that lp fees are 0 for the sake of speed. In the future we could batch compute
304
- // these or make hardcoded assumptions based on the origin-repayment chain direction. This might result
305
- // in slight over estimations of refunds, but its not clear whether underestimating or overestimating is
306
- // worst from the relayer's perspective.
307
- var relayer = fill.relayer, refundAmount = fill.inputAmount;
308
- (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
309
- (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
310
- var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : bnZero;
311
- refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
312
- });
313
- return [2 /*return*/];
314
- }
315
- });
316
- };
317
- this_1 = this;
318
- _i = 0, chainIds_1 = chainIds;
319
- _a.label = 1;
320
- case 1:
321
- if (!(_i < chainIds_1.length)) return [3 /*break*/, 4];
322
- chainId = chainIds_1[_i];
323
- return [5 /*yield**/, _loop_1(chainId)];
324
- case 2:
325
- _a.sent();
326
- _a.label = 3;
327
- case 3:
328
- _i++;
329
- return [3 /*break*/, 1];
330
- 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;
331
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);
332
275
  });
333
- });
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;
334
283
  };
335
284
  BundleDataClient.prototype.getUpcomingDepositAmount = function (chainId, l2Token, latestBlockToSearch) {
336
285
  if (this.spokePoolClients[chainId] === undefined) {
@@ -375,10 +324,12 @@ var BundleDataClient = /** @class */ (function () {
375
324
  case 1:
376
325
  _a = _b.sent(), bundleData = _a.bundleData, blockRanges = _a.blockRanges;
377
326
  hubPoolClient = this.clients.hubPoolClient;
378
- root = _buildPoolRebalanceRoot(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
379
- hubPoolClient: hubPoolClient,
380
- configStoreClient: hubPoolClient.configStoreClient,
381
- });
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();
382
333
  return [2 /*return*/, {
383
334
  root: root,
384
335
  blockRanges: blockRanges,
@@ -420,40 +371,33 @@ var BundleDataClient = /** @class */ (function () {
420
371
  return [initialBlockRange[1] + 1, blockRange[1]];
421
372
  });
422
373
  }
423
- var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, _a, _b, bundleFillsV3, expiredDepositsToRefundV3, start, _c, _d;
424
- return __generator(this, function (_e) {
425
- 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) {
426
377
  case 0:
427
378
  hubPoolClient = this.clients.hubPoolClient;
428
379
  nextBundleMainnetStartBlock = hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, hubPoolClient.latestBlockSearched, hubPoolClient.chainId);
429
380
  chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
430
381
  combinedRefunds = [];
431
382
  widestBundleBlockRanges = getWidestPossibleExpectedBlockRange(chainIds, this.spokePoolClients, getEndBlockBuffers(chainIds, this.blockRangeEndBlockBuffer), this.clients, this.clients.hubPoolClient.latestBlockSearched, this.clients.configStoreClient.getEnabledChains(this.clients.hubPoolClient.latestBlockSearched));
432
- if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 5];
383
+ if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 2];
433
384
  pendingBundleBlockRanges = getImpliedBundleBlockRanges(hubPoolClient, this.clients.configStoreClient, hubPoolClient.getLatestProposedRootBundle());
434
385
  return [4 /*yield*/, this.loadArweaveData(pendingBundleBlockRanges)];
435
386
  case 1:
436
- arweaveData = _e.sent();
437
- if (!(arweaveData === undefined)) return [3 /*break*/, 3];
438
- _b = (_a = combinedRefunds).push;
439
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges)];
440
- case 2:
441
- _b.apply(_a, [_e.sent()]);
442
- return [3 /*break*/, 4];
443
- case 3:
444
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
445
- combinedRefunds.push(getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3));
446
- _e.label = 4;
447
- 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
+ }
448
395
  // Shorten the widestBundleBlockRanges now to not double count the pending bundle blocks.
449
396
  widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
450
- _e.label = 5;
451
- case 5:
397
+ _a.label = 2;
398
+ case 2:
452
399
  start = performance.now();
453
- _d = (_c = combinedRefunds).push;
454
- return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges)];
455
- case 6:
456
- _d.apply(_c, [_e.sent()]);
400
+ combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges));
457
401
  this.logger.debug({
458
402
  at: "BundleDataClient#getNextBundleRefunds",
459
403
  message: "Loading approximate refunds for next bundle in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -596,7 +540,7 @@ var BundleDataClient = /** @class */ (function () {
596
540
  };
597
541
  BundleDataClient.prototype.loadDataFromScratch = function (blockRangesForChains, spokePoolClients) {
598
542
  return __awaiter(this, void 0, void 0, function () {
599
- var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleUnrepayableFillsV3, 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;
600
544
  var _this = this;
601
545
  return __generator(this, function (_c) {
602
546
  switch (_c.label) {
@@ -616,7 +560,6 @@ var BundleDataClient = /** @class */ (function () {
616
560
  bundleDepositsV3 = {};
617
561
  bundleFillsV3 = {};
618
562
  bundleInvalidFillsV3 = [];
619
- bundleUnrepayableFillsV3 = [];
620
563
  bundleSlowFillsV3 = {};
621
564
  expiredDepositsToRefundV3 = {};
622
565
  unexecutableSlowFills = {};
@@ -674,13 +617,7 @@ var BundleDataClient = /** @class */ (function () {
674
617
  v3RelayHashes = {};
675
618
  bundleDepositHashes = [];
676
619
  olderDepositHashes = [];
677
- decodeBundleDepositHash = function (depositHash) {
678
- var _a = depositHash.split("@"), relayDataHash = _a[0], i = _a[1];
679
- return { relayDataHash: relayDataHash, index: Number(i) };
680
- };
681
- startBlockForMainnet = getBlockRangeForChain(blockRangesForChains, this.clients.hubPoolClient.chainId, this.chainIdListForBundleEvaluationBlockNumbers)[0];
682
- versionAtProposalBlock = this.clients.configStoreClient.getConfigStoreVersionForBlock(startBlockForMainnet);
683
- canRefundPrefills = versionAtProposalBlock >= PRE_FILL_MIN_CONFIG_STORE_VERSION || process.env.FORCE_REFUND_PREFILLS === "true";
620
+ depositCounter = 0;
684
621
  _loop_2 = function (originChainId) {
685
622
  var originClient = spokePoolClients[originChainId];
686
623
  var originChainBlockRange = getBlockRangeForChain(blockRangesForChains, originChainId, chainIds);
@@ -690,45 +627,48 @@ var BundleDataClient = /** @class */ (function () {
690
627
  continue;
691
628
  }
692
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.
693
633
  if (deposit.blockNumber > originChainBlockRange[1] || isZeroValueDeposit(deposit)) {
694
634
  return;
695
635
  }
636
+ depositCounter++;
696
637
  var relayDataHash = _this.getRelayHashFromEvent(deposit);
638
+ // Duplicate deposits are treated like normal deposits.
697
639
  if (!v3RelayHashes[relayDataHash]) {
698
640
  v3RelayHashes[relayDataHash] = {
699
- deposits: [deposit],
641
+ deposit: deposit,
700
642
  fill: undefined,
701
643
  slowFillRequest: undefined,
702
644
  };
703
645
  }
704
- else {
705
- 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;
706
652
  }
707
- // Account for duplicate deposits by concatenating the relayDataHash with the count of the number of times
708
- // we have seen it so far.
709
- var newBundleDepositHash = "".concat(relayDataHash, "@").concat(v3RelayHashes[relayDataHash].deposits.length - 1);
710
- var decodedBundleDepositHash = decodeBundleDepositHash(newBundleDepositHash);
711
- assert(decodedBundleDepositHash.relayDataHash === relayDataHash &&
712
- 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.
713
655
  if (deposit.blockNumber >= originChainBlockRange[0]) {
714
- bundleDepositHashes.push(newBundleDepositHash);
656
+ bundleDepositHashes.push(relayDataHash);
715
657
  updateBundleDepositsV3(bundleDepositsV3, deposit);
716
658
  }
717
659
  else if (deposit.blockNumber < originChainBlockRange[0]) {
718
- olderDepositHashes.push(newBundleDepositHash);
660
+ olderDepositHashes.push(relayDataHash);
719
661
  }
720
662
  });
721
663
  }
722
664
  };
723
- // Prerequisite step: Load all deposit events from the current or older bundles into the v3RelayHashes dictionary
724
- // for convenient matching with fills.
725
665
  for (_i = 0, allChainIds_1 = allChainIds; _i < allChainIds_1.length; _i++) {
726
666
  originChainId = allChainIds_1[_i];
727
667
  _loop_2(originChainId);
728
668
  }
729
669
  this.logger.debug({
730
670
  at: "BundleDataClient#loadData",
731
- 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."),
732
672
  });
733
673
  start = performance.now();
734
674
  validatedBundleV3Fills = [];
@@ -742,7 +682,7 @@ var BundleDataClient = /** @class */ (function () {
742
682
  case 0:
743
683
  originClient = spokePoolClients[originChainId];
744
684
  _loop_4 = function (destinationChainId) {
745
- var destinationClient, destinationChainBlockRange, originChainBlockRange, fastFillsReplacingSlowFills;
685
+ var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills;
746
686
  return __generator(this, function (_g) {
747
687
  switch (_g.label) {
748
688
  case 0:
@@ -751,7 +691,6 @@ var BundleDataClient = /** @class */ (function () {
751
691
  }
752
692
  destinationClient = spokePoolClients[destinationChainId];
753
693
  destinationChainBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
754
- originChainBlockRange = getBlockRangeForChain(blockRangesForChains, originChainId, chainIds);
755
694
  fastFillsReplacingSlowFills = [];
756
695
  return [4 /*yield*/, forEachAsync(destinationClient
757
696
  .getFillsForOriginChain(originChainId)
@@ -759,58 +698,40 @@ var BundleDataClient = /** @class */ (function () {
759
698
  // tokens to the filler. We can't remove non-empty message deposit here in case there is a slow fill
760
699
  // request for the deposit, we'd want to see the fill took place.
761
700
  .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1] && !isZeroValueFillOrSlowFillRequest(fill); }), function (fill) { return __awaiter(_this, void 0, void 0, function () {
762
- var relayDataHash, fillToRefund_1, duplicateDeposits, historicalDeposit, matchedDeposit, fillToRefund;
701
+ var relayDataHash, historicalDeposit, matchedDeposit;
763
702
  return __generator(this, function (_a) {
764
703
  switch (_a.label) {
765
704
  case 0:
766
- fillCounter++;
767
705
  relayDataHash = this.getRelayHashFromEvent(fill);
768
- if (!v3RelayHashes[relayDataHash]) return [3 /*break*/, 5];
769
- if (!!v3RelayHashes[relayDataHash].fill) return [3 /*break*/, 3];
770
- assert(isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
771
- v3RelayHashes[relayDataHash].fill = fill;
772
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
773
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
774
- case 1:
775
- fillToRefund_1 = _a.sent();
776
- if (!isDefined(fillToRefund_1)) {
777
- bundleUnrepayableFillsV3.push(fill);
778
- // We don't return here yet because we still need to mark unexecutable slow fill leaves
779
- // or duplicate deposits. However, we won't issue a fast fill refund.
780
- }
781
- else {
782
- v3RelayHashes[relayDataHash].fill = fillToRefund_1;
783
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund_1), { quoteTimestamp: v3RelayHashes[relayDataHash].deposits[0].quoteTimestamp }));
784
- duplicateDeposits = v3RelayHashes[relayDataHash].deposits.slice(1);
785
- duplicateDeposits.forEach(function (duplicateDeposit) {
786
- if (isSlowFill(fill)) {
787
- updateExpiredDepositsV3(expiredDepositsToRefundV3, duplicateDeposit);
788
- }
789
- else {
790
- 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
+ }
791
723
  }
792
- });
793
- }
794
- // If fill replaced a slow fill request, then mark it as one that might have created an
795
- // unexecutable slow fill. We can't know for sure until we check the slow fill request
796
- // events.
797
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
798
- _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposits[0])) {
799
- fastFillsReplacingSlowFills.push(relayDataHash);
724
+ }
725
+ return [2 /*return*/];
800
726
  }
801
- _a.label = 2;
802
- case 2: return [3 /*break*/, 4];
803
- case 3: throw new Error("Duplicate fill detected");
804
- case 4: return [2 /*return*/];
805
- case 5:
806
727
  // At this point, there is no relay hash dictionary entry for this fill, so we need to
807
- // instantiate the entry. We won't modify the fill.relayer until we match it with a deposit.
728
+ // instantiate the entry.
808
729
  v3RelayHashes[relayDataHash] = {
809
- deposits: undefined,
730
+ deposit: undefined,
810
731
  fill: fill,
811
732
  slowFillRequest: undefined,
812
733
  };
813
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 9];
734
+ if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
814
735
  // Fill has a non-infinite expiry, and we can assume our spoke pool clients have old enough deposits
815
736
  // to conclude that this fill is invalid if we haven't found a matching deposit in memory, so
816
737
  // skip the historical query.
@@ -819,57 +740,35 @@ var BundleDataClient = /** @class */ (function () {
819
740
  return [2 /*return*/];
820
741
  }
821
742
  return [4 /*yield*/, queryHistoricalDepositForFill(originClient, fill)];
822
- case 6:
743
+ case 1:
823
744
  historicalDeposit = _a.sent();
824
- if (!!historicalDeposit.found) return [3 /*break*/, 7];
825
- bundleInvalidFillsV3.push(fill);
826
- return [3 /*break*/, 9];
827
- case 7:
828
- matchedDeposit = historicalDeposit.deposit;
829
- // If deposit is in a following bundle, then this fill will have to be refunded once that deposit
830
- // is in the current bundle.
831
- if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
745
+ if (!historicalDeposit.found) {
832
746
  bundleInvalidFillsV3.push(fill);
833
- return [2 /*return*/];
834
- }
835
- v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
836
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, matchedDeposit, allChainIds)];
837
- case 8:
838
- fillToRefund = _a.sent();
839
- if (!isDefined(fillToRefund)) {
840
- bundleUnrepayableFillsV3.push(fill);
841
- // Don't return yet as we still need to mark down any unexecutable slow fill leaves
842
- // in case this fast fill replaced a slow fill request.
843
747
  }
844
748
  else {
749
+ matchedDeposit = historicalDeposit.deposit;
845
750
  // @dev Since queryHistoricalDepositForFill validates the fill by checking individual
846
751
  // object property values against the deposit's, we
847
752
  // sanity check it here by comparing the full relay hashes. If there's an error here then the
848
753
  // historical deposit query is not working as expected.
849
754
  assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
850
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
851
- v3RelayHashes[relayDataHash].fill = fillToRefund;
852
- // No need to check for duplicate deposits here since duplicate deposits with
853
- // infinite deadlines are impossible to send via unsafeDeposit().
854
- }
855
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
856
- _canCreateSlowFillLeaf(matchedDeposit)) {
857
- 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
+ }
858
762
  }
859
- _a.label = 9;
860
- case 9: return [2 /*return*/];
763
+ _a.label = 2;
764
+ case 2: return [2 /*return*/];
861
765
  }
862
766
  });
863
767
  }); })];
864
768
  case 1:
865
769
  _g.sent();
866
- // Process slow fill requests and produce slow fill leaves while maintaining the following the invariants:
867
- // - Slow fill leaves cannot be produced for deposits that have expired in this bundle.
868
- // - Slow fill leaves cannot be produced for deposits that have been filled.
869
- // Assumptions about fills:
870
- // - Duplicate slow fill requests for the same relay data hash are impossible to send.
871
- // - Slow fill requests can only be sent before the deposit's fillDeadline.
872
- // - 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.
873
772
  return [4 /*yield*/, forEachAsync(destinationClient
874
773
  .getSlowFillRequestsForOriginChain(originChainId)
875
774
  .filter(function (request) {
@@ -882,53 +781,55 @@ var BundleDataClient = /** @class */ (function () {
882
781
  relayDataHash = this.getRelayHashFromEvent(slowFillRequest);
883
782
  if (v3RelayHashes[relayDataHash]) {
884
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.
885
786
  v3RelayHashes[relayDataHash].slowFillRequest = slowFillRequest;
886
787
  if (v3RelayHashes[relayDataHash].fill) {
887
- // Exiting here assumes that slow fill requests must precede fills, so if there was a fill
888
- // following this slow fill request, then we would have already seen it. We don't need to check
889
- // 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.
890
790
  return [2 /*return*/];
891
791
  }
892
- assert(isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
893
- 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.
894
797
  if (slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
895
798
  _canCreateSlowFillLeaf(matchedDeposit) &&
799
+ // Deposit must not have expired in this bundle.
896
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.
897
803
  validatedBundleSlowFills.push(matchedDeposit);
898
804
  }
899
805
  }
900
- else {
901
- throw new Error("Duplicate slow fill request detected.");
902
- }
903
806
  return [2 /*return*/];
904
807
  }
905
808
  // Instantiate dictionary if there is neither a deposit nor fill matching it.
906
809
  v3RelayHashes[relayDataHash] = {
907
- deposits: undefined,
810
+ deposit: undefined,
908
811
  fill: undefined,
909
812
  slowFillRequest: slowFillRequest,
910
813
  };
911
- if (!(INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline) &&
912
- slowFillRequest.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
814
+ if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
815
+ INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline))) return [3 /*break*/, 2];
913
816
  return [4 /*yield*/, queryHistoricalDepositForFill(originClient, slowFillRequest)];
914
817
  case 1:
915
818
  historicalDeposit = _a.sent();
916
819
  if (!historicalDeposit.found) {
820
+ // TODO: Invalid slow fill request. Maybe worth logging.
917
821
  return [2 /*return*/];
918
822
  }
919
823
  matchedDeposit = historicalDeposit.deposit;
920
- // If deposit is in a following bundle, then this slow fill request will have to be created
921
- // once that deposit is in the current bundle.
922
- if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
923
- return [2 /*return*/];
924
- }
925
824
  // @dev Since queryHistoricalDepositForFill validates the slow fill request by checking individual
926
825
  // object property values against the deposit's, we
927
826
  // sanity check it here by comparing the full relay hashes. If there's an error here then the
928
827
  // historical deposit query is not working as expected.
929
828
  assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Deposit relay hashes should match.");
930
- v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
931
- 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)) {
932
833
  return [2 /*return*/];
933
834
  }
934
835
  validatedBundleSlowFills.push(matchedDeposit);
@@ -938,167 +839,116 @@ var BundleDataClient = /** @class */ (function () {
938
839
  });
939
840
  }); })];
940
841
  case 2:
941
- // Process slow fill requests and produce slow fill leaves while maintaining the following the invariants:
942
- // - Slow fill leaves cannot be produced for deposits that have expired in this bundle.
943
- // - Slow fill leaves cannot be produced for deposits that have been filled.
944
- // Assumptions about fills:
945
- // - Duplicate slow fill requests for the same relay data hash are impossible to send.
946
- // - Slow fill requests can only be sent before the deposit's fillDeadline.
947
- // - 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.
948
844
  _g.sent();
949
- // Process deposits and maintain the following invariants:
950
- // - Deposits matching fills that are not type SlowFill from previous bundle block ranges should produce
951
- // refunds for those fills.
952
- // - Deposits matching fills that are type SlowFill from previous bundle block ranges should be refunded to the
953
- // depositor.
954
- // - All deposits expiring in this bundle, even those sent in prior bundle block ranges, should be refunded
955
- // to the depositor.
956
- // - An expired deposit cannot be refunded if the deposit was filled.
957
- // - If a deposit from a prior bundle expired in this bundle, had a slow fill request created for it
958
- // in a prior bundle, and has not been filled yet, then an unexecutable slow fill leaf has been created
959
- // and needs to be refunded to the HubPool.
960
- // - Deposits matching slow fill requests from previous bundle block ranges should produce slow fills
961
- // if the deposit has not been filled.
962
- // Assumptions:
963
- // - If the deposit has a matching fill or slow fill request in the bundle then we have already stored
964
- // it in the relay hashes dictionary.
965
- // - We've created refunds for all fills in this bundle matching a deposit.
966
- // - We've created slow fill leaves for all slow fill requests in this bundle matching an unfilled deposit.
967
- // - Deposits for the same relay data hash can be sent an arbitrary amount of times.
968
- // - Deposits can be sent an arbitrary amount of time after a fill has been sent for the matching relay data.
969
- return [4 /*yield*/, mapAsync(bundleDepositHashes, function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
970
- var _a, relayDataHash, index, _b, deposits, fill, slowFillRequest, deposit, fillToRefund, fillStatus, prefill, verifiedFill;
971
- var _this = this;
972
- return __generator(this, function (_c) {
973
- 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) {
974
859
  case 0:
975
- _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
976
- _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, fill = _b.fill, slowFillRequest = _b.slowFillRequest;
977
- if (!deposits || deposits.length === 0) {
978
- throw new Error("Deposits should exist in relay hash dictionary.");
979
- }
980
- deposit = deposits[index];
860
+ _a = v3RelayHashes[depositHash], deposit = _a.deposit, fill = _a.fill, slowFillRequest = _a.slowFillRequest;
981
861
  if (!deposit)
982
862
  throw new Error("Deposit should exist in relay hash dictionary.");
983
- 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
+ }
984
877
  return [2 /*return*/];
985
878
  }
986
- if (!fill) return [3 /*break*/, 3];
987
- if (!(canRefundPrefills && fill.blockNumber < destinationChainBlockRange[0])) return [3 /*break*/, 2];
988
- return [4 /*yield*/, verifyFillRepayment(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
989
- case 1:
990
- fillToRefund = _c.sent();
991
- if (!isDefined(fillToRefund)) {
992
- bundleUnrepayableFillsV3.push(fill);
993
- }
994
- else if (!isSlowFill(fill)) {
995
- v3RelayHashes[relayDataHash].fill = fillToRefund;
996
- validatedBundleV3Fills.push(__assign(__assign({}, fillToRefund), { quoteTimestamp: deposit.quoteTimestamp }));
997
- }
998
- else {
999
- updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1000
- }
1001
- _c.label = 2;
1002
- case 2: return [2 /*return*/];
1003
- case 3:
1004
879
  // If a slow fill request exists in memory, then we know the deposit has not been filled because fills
1005
- // must follow slow fill requests and we would have seen the fill already if it existed.,
1006
- // We can conclude that either the deposit has expired or we need to create a slow fill leaf for the
1007
- // deposit because it has not been filled. Slow fill leaves were already created for requests sent
1008
- // 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.
1009
884
  if (slowFillRequest) {
1010
885
  if (_depositIsExpired(deposit)) {
1011
886
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1012
887
  }
1013
- else if (canRefundPrefills &&
1014
- slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
1015
- _canCreateSlowFillLeaf(deposit) &&
1016
- validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
888
+ else if (slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
889
+ _canCreateSlowFillLeaf(deposit)) {
1017
890
  validatedBundleSlowFills.push(deposit);
1018
891
  }
1019
892
  return [2 /*return*/];
1020
893
  }
1021
894
  return [4 /*yield*/, _getFillStatusForDeposit(deposit, destinationChainBlockRange[1])];
1022
- case 4:
1023
- fillStatus = _c.sent();
1024
- if (!(fillStatus === FillStatus.Filled)) return [3 /*break*/, 8];
1025
- return [4 /*yield*/, this.findMatchingFillEvent(deposit, destinationClient)];
1026
- case 5:
1027
- prefill = _c.sent();
1028
- assert(isDefined(prefill), "findFillEvent# Cannot find prefill: ".concat(relayDataHash));
1029
- assert(this.getRelayHashFromEvent(prefill) === relayDataHash, "Relay hashes should match.");
1030
- if (!canRefundPrefills) return [3 /*break*/, 7];
1031
- return [4 /*yield*/, verifyFillRepayment(prefill, destinationClient.spokePool.provider, deposit, allChainIds)];
1032
- case 6:
1033
- verifiedFill = _c.sent();
1034
- if (!isDefined(verifiedFill)) {
1035
- bundleUnrepayableFillsV3.push(prefill);
1036
- }
1037
- else if (!isSlowFill(verifiedFill)) {
1038
- validatedBundleV3Fills.push(__assign(__assign({}, verifiedFill), { quoteTimestamp: deposit.quoteTimestamp }));
1039
- }
1040
- else {
1041
- 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 }));
1042
903
  }
1043
- _c.label = 7;
1044
- case 7: return [3 /*break*/, 9];
1045
- case 8:
904
+ return [3 /*break*/, 4];
905
+ case 3:
1046
906
  if (_depositIsExpired(deposit)) {
1047
907
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
1048
908
  }
1049
- else if (fillStatus === FillStatus.RequestedSlowFill &&
1050
- // Don't create duplicate slow fill requests for the same deposit.
1051
- validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
1052
- 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.
1053
916
  validatedBundleSlowFills.push(deposit);
1054
917
  }
1055
918
  }
1056
- _c.label = 9;
1057
- case 9: return [2 /*return*/];
919
+ _b.label = 4;
920
+ case 4: return [2 /*return*/];
1058
921
  }
1059
922
  });
1060
923
  }); })];
1061
924
  case 3:
1062
- // Process deposits and maintain the following invariants:
1063
- // - Deposits matching fills that are not type SlowFill from previous bundle block ranges should produce
1064
- // refunds for those fills.
1065
- // - Deposits matching fills that are type SlowFill from previous bundle block ranges should be refunded to the
1066
- // depositor.
1067
- // - All deposits expiring in this bundle, even those sent in prior bundle block ranges, should be refunded
1068
- // to the depositor.
1069
- // - An expired deposit cannot be refunded if the deposit was filled.
1070
- // - If a deposit from a prior bundle expired in this bundle, had a slow fill request created for it
1071
- // in a prior bundle, and has not been filled yet, then an unexecutable slow fill leaf has been created
1072
- // and needs to be refunded to the HubPool.
1073
- // - Deposits matching slow fill requests from previous bundle block ranges should produce slow fills
1074
- // if the deposit has not been filled.
1075
- // Assumptions:
1076
- // - If the deposit has a matching fill or slow fill request in the bundle then we have already stored
1077
- // it in the relay hashes dictionary.
1078
- // - We've created refunds for all fills in this bundle matching a deposit.
1079
- // - We've created slow fill leaves for all slow fill requests in this bundle matching an unfilled deposit.
1080
- // - Deposits for the same relay data hash can be sent an arbitrary amount of times.
1081
- // - 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.
1082
932
  _g.sent();
1083
933
  // For all fills that came after a slow fill request, we can now check if the slow fill request
1084
934
  // was a valid one and whether it was created in a previous bundle. If so, then it created a slow fill
1085
935
  // leaf that is now unexecutable.
1086
936
  fastFillsReplacingSlowFills.forEach(function (relayDataHash) {
1087
- 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;
1088
938
  assert((fill === null || fill === void 0 ? void 0 : fill.relayExecutionInfo.fillType) === FillType.ReplacedSlowFill, "Fill type should be ReplacedSlowFill.");
1089
939
  // Needed for TSC - are implicitely checking that deposit exists by making it to this point.
1090
- if (!deposits || deposits.length < 1) {
940
+ if (!deposit) {
1091
941
  throw new Error("Deposit should exist in relay hash dictionary.");
1092
942
  }
1093
943
  // We should never push fast fills involving lite chains here because slow fill requests for them are invalid:
1094
- 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");
1095
945
  var destinationBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
1096
946
  if (
1097
947
  // If there is a slow fill request in this bundle that matches the relay hash, then there was no slow fill
1098
948
  // created that would be considered excess.
1099
949
  !slowFillRequest ||
1100
950
  slowFillRequest.blockNumber < destinationBlockRange[0]) {
1101
- validatedBundleUnexecutableSlowFills.push(deposits[0]);
951
+ validatedBundleUnexecutableSlowFills.push(deposit);
1102
952
  }
1103
953
  });
1104
954
  return [2 /*return*/];
@@ -1142,17 +992,13 @@ var BundleDataClient = /** @class */ (function () {
1142
992
  // For all deposits older than this bundle, we need to check if they expired in this bundle and if they did,
1143
993
  // whether there was a slow fill created for it in a previous bundle that is now unexecutable and replaced
1144
994
  // by a new expired deposit refund.
1145
- return [4 /*yield*/, forEachAsync(olderDepositHashes, function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
1146
- var _a, relayDataHash, index, _b, deposits, slowFillRequest, fill, deposit, destinationChainId, destinationBlockRange, fillStatus;
1147
- return __generator(this, function (_c) {
1148
- 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) {
1149
999
  case 0:
1150
- _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
1151
- _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, slowFillRequest = _b.slowFillRequest, fill = _b.fill;
1152
- if (!deposits || deposits.length < 1) {
1153
- throw new Error("Deposit should exist in relay hash dictionary.");
1154
- }
1155
- 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.");
1156
1002
  destinationChainId = deposit.destinationChainId;
1157
1003
  destinationBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
1158
1004
  if (!
@@ -1166,7 +1012,7 @@ var BundleDataClient = /** @class */ (function () {
1166
1012
  return [3 /*break*/, 2];
1167
1013
  return [4 /*yield*/, _getFillStatusForDeposit(deposit, destinationBlockRange[1])];
1168
1014
  case 1:
1169
- fillStatus = _c.sent();
1015
+ fillStatus = _b.sent();
1170
1016
  // If there is no matching fill and the deposit expired in this bundle and the fill status on-chain is not
1171
1017
  // Filled, then we can to refund it as an expired deposit.
1172
1018
  if (fillStatus !== FillStatus.Filled) {
@@ -1188,7 +1034,7 @@ var BundleDataClient = /** @class */ (function () {
1188
1034
  if (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0]) {
1189
1035
  validatedBundleUnexecutableSlowFills.push(deposit);
1190
1036
  }
1191
- _c.label = 2;
1037
+ _b.label = 2;
1192
1038
  case 2: return [2 /*return*/];
1193
1039
  }
1194
1040
  });
@@ -1203,7 +1049,7 @@ var BundleDataClient = /** @class */ (function () {
1203
1049
  promises = [
1204
1050
  validatedBundleV3Fills.length > 0
1205
1051
  ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleV3Fills.map(function (fill) {
1206
- var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
1052
+ var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1207
1053
  assert(isDefined(matchedDeposit), "Deposit should exist in relay hash dictionary.");
1208
1054
  var paymentChainId = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, matchedDeposit.fromLiteChain).chainToSendRefundTo;
1209
1055
  return __assign(__assign({}, fill), { paymentChainId: paymentChainId });
@@ -1230,23 +1076,14 @@ var BundleDataClient = /** @class */ (function () {
1230
1076
  v3FillLpFees.forEach(function (_a, idx) {
1231
1077
  var realizedLpFeePct = _a.realizedLpFeePct;
1232
1078
  var fill = validatedBundleV3Fills[idx];
1233
- var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
1079
+ var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1234
1080
  assert(isDefined(associatedDeposit), "Deposit should exist in relay hash dictionary.");
1235
1081
  var _b = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, associatedDeposit.fromLiteChain), chainToSendRefundTo = _b.chainToSendRefundTo, repaymentToken = _b.repaymentToken;
1236
- updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken, fill.relayer);
1082
+ updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken);
1237
1083
  });
1238
1084
  v3SlowFillLpFees.forEach(function (_a, idx) {
1239
1085
  var lpFeePct = _a.realizedLpFeePct;
1240
1086
  var deposit = validatedBundleSlowFills[idx];
1241
- // We should not create slow fill leaves for duplicate deposit hashes and we should only create a slow
1242
- // fill leaf for the first deposit (the quote timestamp of the deposit determines the LP fee, so its
1243
- // important we pick out the correct deposit). Deposits are pushed into validatedBundleSlowFills in ascending
1244
- // order so the following slice will only match the first deposit.
1245
- var relayDataHash = _this.getRelayHashFromEvent(deposit);
1246
- if (validatedBundleSlowFills.slice(0, idx).some(function (d) { return _this.getRelayHashFromEvent(d) === relayDataHash; })) {
1247
- return;
1248
- }
1249
- assert(!_depositIsExpired(deposit), "Cannot create slow fill leaf for expired deposit.");
1250
1087
  updateBundleSlowFills(bundleSlowFillsV3, __assign(__assign({}, deposit), { lpFeePct: lpFeePct }));
1251
1088
  });
1252
1089
  v3UnexecutableSlowFillLpFees.forEach(function (_a, idx) {
@@ -1254,7 +1091,7 @@ var BundleDataClient = /** @class */ (function () {
1254
1091
  var deposit = validatedBundleUnexecutableSlowFills[idx];
1255
1092
  updateBundleExcessSlowFills(unexecutableSlowFills, __assign(__assign({}, deposit), { lpFeePct: lpFeePct }));
1256
1093
  });
1257
- v3SpokeEventsReadable = prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
1094
+ v3SpokeEventsReadable = prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
1258
1095
  if (bundleInvalidFillsV3.length > 0) {
1259
1096
  this.logger.debug({
1260
1097
  at: "BundleDataClient#loadData",
@@ -1263,14 +1100,6 @@ var BundleDataClient = /** @class */ (function () {
1263
1100
  bundleInvalidFillsV3: bundleInvalidFillsV3,
1264
1101
  });
1265
1102
  }
1266
- if (bundleUnrepayableFillsV3.length > 0) {
1267
- this.logger.debug({
1268
- at: "BundleDataClient#loadData",
1269
- message: "Finished loading V3 spoke pool data and found some unrepayable V3 fills in range",
1270
- blockRangesForChains: blockRangesForChains,
1271
- bundleUnrepayableFillsV3: bundleUnrepayableFillsV3,
1272
- });
1273
- }
1274
1103
  this.logger.debug({
1275
1104
  at: "BundleDataClient#loadDataFromScratch",
1276
1105
  message: "Computed bundle data in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -1295,16 +1124,6 @@ var BundleDataClient = /** @class */ (function () {
1295
1124
  BundleDataClient.prototype.getRelayHashFromEvent = function (event) {
1296
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);
1297
1126
  };
1298
- BundleDataClient.prototype.findMatchingFillEvent = function (deposit, spokePoolClient) {
1299
- return __awaiter(this, void 0, void 0, function () {
1300
- return __generator(this, function (_a) {
1301
- switch (_a.label) {
1302
- case 0: return [4 /*yield*/, findFillEvent(spokePoolClient.spokePool, deposit, spokePoolClient.deploymentBlock, spokePoolClient.latestBlockSearched)];
1303
- case 1: return [2 /*return*/, _a.sent()];
1304
- }
1305
- });
1306
- });
1307
- };
1308
1127
  BundleDataClient.prototype.getBundleBlockTimestamps = function (chainIds, blockRangesForChains, spokePoolClients) {
1309
1128
  return __awaiter(this, void 0, void 0, function () {
1310
1129
  var _a, _b;
@@ -1314,7 +1133,7 @@ var BundleDataClient = /** @class */ (function () {
1314
1133
  case 0:
1315
1134
  _b = (_a = Object).fromEntries;
1316
1135
  return [4 /*yield*/, mapAsync(chainIds, function (chainId, index) { return __awaiter(_this, void 0, void 0, function () {
1317
- 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;
1318
1137
  return __generator(this, function (_c) {
1319
1138
  switch (_c.label) {
1320
1139
  case 0:
@@ -1329,7 +1148,7 @@ var BundleDataClient = /** @class */ (function () {
1329
1148
  return [2 /*return*/];
1330
1149
  }
1331
1150
  startBlockForChain = Math.min(_startBlockForChain, spokePoolClient.latestBlockSearched);
1332
- endBlockForChain = Math.min(_endBlockForChain + 1, spokePoolClient.latestBlockSearched);
1151
+ endBlockForChain = Math.min(_endBlockForChain, spokePoolClient.latestBlockSearched);
1333
1152
  return [4 /*yield*/, spokePoolClient.getTimestampForBlock(startBlockForChain)];
1334
1153
  case 1:
1335
1154
  _b = [
@@ -1339,11 +1158,9 @@ var BundleDataClient = /** @class */ (function () {
1339
1158
  case 2:
1340
1159
  _a = _b.concat([
1341
1160
  _c.sent()
1342
- ]), startTime = _a[0], _endTime = _a[1];
1343
- endBlockDelta = endBlockForChain > startBlockForChain ? 1 : 0;
1344
- endTime = Math.max(0, _endTime - endBlockDelta);
1161
+ ]), startTime = _a[0], endTime = _a[1];
1345
1162
  // Sanity checks:
1346
- 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.");
1347
1164
  assert(startBlockForChain === 0 || startTime > 0, "Start timestamp must be greater than 0 if the start block is greater than 0.");
1348
1165
  return [2 /*return*/, [chainId, [startTime, endTime]]];
1349
1166
  }