@across-protocol/sdk 4.0.0-beta.0 → 4.0.0-beta.10

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 (139) hide show
  1. package/dist/cjs/clients/BundleDataClient/BundleDataClient.d.ts +1 -1
  2. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js +218 -148
  3. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  4. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.d.ts +3 -1
  5. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js +33 -1
  6. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js.map +1 -1
  7. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.d.ts +12 -10
  8. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.js +3 -3
  9. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.js.map +1 -1
  10. package/dist/cjs/clients/SpokePoolClient.d.ts +12 -12
  11. package/dist/cjs/clients/SpokePoolClient.js +20 -18
  12. package/dist/cjs/clients/SpokePoolClient.js.map +1 -1
  13. package/dist/cjs/clients/mocks/MockSpokePoolClient.d.ts +5 -4
  14. package/dist/cjs/clients/mocks/MockSpokePoolClient.js +6 -6
  15. package/dist/cjs/clients/mocks/MockSpokePoolClient.js.map +1 -1
  16. package/dist/cjs/constants.d.ts +4 -2
  17. package/dist/cjs/constants.js +5 -3
  18. package/dist/cjs/constants.js.map +1 -1
  19. package/dist/cjs/interfaces/SpokePool.d.ts +2 -2
  20. package/dist/cjs/providers/index.d.ts +1 -0
  21. package/dist/cjs/providers/index.js +2 -0
  22. package/dist/cjs/providers/index.js.map +1 -1
  23. package/dist/cjs/providers/mockProvider.d.ts +19 -0
  24. package/dist/cjs/providers/mockProvider.js +70 -0
  25. package/dist/cjs/providers/mockProvider.js.map +1 -0
  26. package/dist/cjs/utils/AddressUtils.d.ts +1 -0
  27. package/dist/cjs/utils/AddressUtils.js +14 -1
  28. package/dist/cjs/utils/AddressUtils.js.map +1 -1
  29. package/dist/cjs/utils/BigNumberUtils.d.ts +1 -0
  30. package/dist/cjs/utils/BigNumberUtils.js +5 -1
  31. package/dist/cjs/utils/BigNumberUtils.js.map +1 -1
  32. package/dist/cjs/utils/CachingUtils.js +1 -1
  33. package/dist/cjs/utils/CachingUtils.js.map +1 -1
  34. package/dist/cjs/utils/DepositUtils.d.ts +4 -1
  35. package/dist/cjs/utils/DepositUtils.js +23 -6
  36. package/dist/cjs/utils/DepositUtils.js.map +1 -1
  37. package/dist/cjs/utils/EventUtils.js +4 -0
  38. package/dist/cjs/utils/EventUtils.js.map +1 -1
  39. package/dist/cjs/utils/NetworkUtils.d.ts +1 -0
  40. package/dist/cjs/utils/NetworkUtils.js +6 -1
  41. package/dist/cjs/utils/NetworkUtils.js.map +1 -1
  42. package/dist/cjs/utils/SpokeUtils.d.ts +5 -3
  43. package/dist/cjs/utils/SpokeUtils.js +18 -11
  44. package/dist/cjs/utils/SpokeUtils.js.map +1 -1
  45. package/dist/cjs/utils/ValidatorUtils.js +1 -1
  46. package/dist/cjs/utils/ValidatorUtils.js.map +1 -1
  47. package/dist/esm/clients/BundleDataClient/BundleDataClient.d.ts +1 -1
  48. package/dist/esm/clients/BundleDataClient/BundleDataClient.js +275 -186
  49. package/dist/esm/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  50. package/dist/esm/clients/BundleDataClient/utils/FillUtils.d.ts +3 -1
  51. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js +42 -1
  52. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js.map +1 -1
  53. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.d.ts +12 -10
  54. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.js +4 -4
  55. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.js.map +1 -1
  56. package/dist/esm/clients/SpokePoolClient.d.ts +12 -12
  57. package/dist/esm/clients/SpokePoolClient.js +21 -19
  58. package/dist/esm/clients/SpokePoolClient.js.map +1 -1
  59. package/dist/esm/clients/mocks/MockSpokePoolClient.d.ts +5 -4
  60. package/dist/esm/clients/mocks/MockSpokePoolClient.js +7 -7
  61. package/dist/esm/clients/mocks/MockSpokePoolClient.js.map +1 -1
  62. package/dist/esm/constants.d.ts +4 -2
  63. package/dist/esm/constants.js +5 -3
  64. package/dist/esm/constants.js.map +1 -1
  65. package/dist/esm/interfaces/SpokePool.d.ts +2 -2
  66. package/dist/esm/providers/index.d.ts +1 -0
  67. package/dist/esm/providers/index.js +2 -0
  68. package/dist/esm/providers/index.js.map +1 -1
  69. package/dist/esm/providers/mockProvider.d.ts +23 -0
  70. package/dist/esm/providers/mockProvider.js +73 -0
  71. package/dist/esm/providers/mockProvider.js.map +1 -0
  72. package/dist/esm/utils/AddressUtils.d.ts +1 -0
  73. package/dist/esm/utils/AddressUtils.js +16 -0
  74. package/dist/esm/utils/AddressUtils.js.map +1 -1
  75. package/dist/esm/utils/BigNumberUtils.d.ts +8 -0
  76. package/dist/esm/utils/BigNumberUtils.js +10 -0
  77. package/dist/esm/utils/BigNumberUtils.js.map +1 -1
  78. package/dist/esm/utils/CachingUtils.js +1 -1
  79. package/dist/esm/utils/CachingUtils.js.map +1 -1
  80. package/dist/esm/utils/DepositUtils.d.ts +4 -1
  81. package/dist/esm/utils/DepositUtils.js +21 -6
  82. package/dist/esm/utils/DepositUtils.js.map +1 -1
  83. package/dist/esm/utils/EventUtils.js +8 -0
  84. package/dist/esm/utils/EventUtils.js.map +1 -1
  85. package/dist/esm/utils/NetworkUtils.d.ts +6 -0
  86. package/dist/esm/utils/NetworkUtils.js +10 -0
  87. package/dist/esm/utils/NetworkUtils.js.map +1 -1
  88. package/dist/esm/utils/SpokeUtils.d.ts +5 -3
  89. package/dist/esm/utils/SpokeUtils.js +21 -13
  90. package/dist/esm/utils/SpokeUtils.js.map +1 -1
  91. package/dist/esm/utils/ValidatorUtils.js +1 -1
  92. package/dist/esm/utils/ValidatorUtils.js.map +1 -1
  93. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts +1 -1
  94. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts.map +1 -1
  95. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts +3 -1
  96. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts.map +1 -1
  97. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts +12 -10
  98. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts.map +1 -1
  99. package/dist/types/clients/SpokePoolClient.d.ts +12 -12
  100. package/dist/types/clients/SpokePoolClient.d.ts.map +1 -1
  101. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts +5 -4
  102. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts.map +1 -1
  103. package/dist/types/constants.d.ts +4 -2
  104. package/dist/types/constants.d.ts.map +1 -1
  105. package/dist/types/interfaces/SpokePool.d.ts +2 -2
  106. package/dist/types/interfaces/SpokePool.d.ts.map +1 -1
  107. package/dist/types/providers/index.d.ts +1 -0
  108. package/dist/types/providers/index.d.ts.map +1 -1
  109. package/dist/types/providers/mockProvider.d.ts +24 -0
  110. package/dist/types/providers/mockProvider.d.ts.map +1 -0
  111. package/dist/types/utils/AddressUtils.d.ts +1 -0
  112. package/dist/types/utils/AddressUtils.d.ts.map +1 -1
  113. package/dist/types/utils/BigNumberUtils.d.ts +8 -0
  114. package/dist/types/utils/BigNumberUtils.d.ts.map +1 -1
  115. package/dist/types/utils/DepositUtils.d.ts +4 -1
  116. package/dist/types/utils/DepositUtils.d.ts.map +1 -1
  117. package/dist/types/utils/EventUtils.d.ts.map +1 -1
  118. package/dist/types/utils/NetworkUtils.d.ts +6 -0
  119. package/dist/types/utils/NetworkUtils.d.ts.map +1 -1
  120. package/dist/types/utils/SpokeUtils.d.ts +5 -3
  121. package/dist/types/utils/SpokeUtils.d.ts.map +1 -1
  122. package/package.json +2 -2
  123. package/src/clients/BundleDataClient/BundleDataClient.ts +179 -114
  124. package/src/clients/BundleDataClient/utils/FillUtils.ts +47 -2
  125. package/src/clients/BundleDataClient/utils/SuperstructUtils.ts +4 -2
  126. package/src/clients/SpokePoolClient.ts +30 -26
  127. package/src/clients/mocks/MockSpokePoolClient.ts +10 -10
  128. package/src/constants.ts +7 -3
  129. package/src/interfaces/SpokePool.ts +2 -2
  130. package/src/providers/index.ts +1 -0
  131. package/src/providers/mockProvider.ts +77 -0
  132. package/src/utils/AddressUtils.ts +16 -0
  133. package/src/utils/BigNumberUtils.ts +11 -0
  134. package/src/utils/CachingUtils.ts +1 -1
  135. package/src/utils/DepositUtils.ts +23 -6
  136. package/src/utils/EventUtils.ts +8 -0
  137. package/src/utils/NetworkUtils.ts +11 -0
  138. package/src/utils/SpokeUtils.ts +31 -19
  139. package/src/utils/ValidatorUtils.ts +1 -1
@@ -1,13 +1,18 @@
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, bnUint32Max, isZeroValueDeposit, findFillEvent, } from "../../utils";
5
- import { _buildPoolRebalanceRoot, BundleDataSS, getEndBlockBuffers, getRefundInformationFromFill, getRefundsFromBundle, getWidestPossibleExpectedBlockRange, isChainDisabled, prettyPrintV3SpokePoolEvents, } from "./utils";
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, prettyPrintV3SpokePoolEvents, verifyFillRepayment, } from "./utils";
6
+ import { PRE_FILL_MIN_CONFIG_STORE_VERSION } from "../../constants";
6
7
  // max(uint256) - 1
7
8
  export var INFINITE_FILL_DEADLINE = bnUint32Max;
8
9
  // V3 dictionary helper functions
9
10
  function updateExpiredDepositsV3(dict, deposit) {
10
11
  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
+ }
11
16
  var originChainId = deposit.originChainId, inputToken = deposit.inputToken;
12
17
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[originChainId]) === null || _a === void 0 ? void 0 : _a[inputToken])) {
13
18
  assign(dict, [originChainId, inputToken], []);
@@ -22,8 +27,12 @@ function updateBundleDepositsV3(dict, deposit) {
22
27
  }
23
28
  dict[originChainId][inputToken].push(deposit);
24
29
  }
25
- function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken) {
30
+ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken, repaymentAddress) {
26
31
  var _a, _b;
32
+ // It is impossible to refund a deposit if the repayment chain is EVM and the relayer is a non-evm address.
33
+ if (chainIsEvm(repaymentChainId) && !isValidEvmAddress(repaymentAddress)) {
34
+ return;
35
+ }
27
36
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[repaymentChainId]) === null || _a === void 0 ? void 0 : _a[repaymentToken])) {
28
37
  assign(dict, [repaymentChainId, repaymentToken], {
29
38
  fills: [],
@@ -32,26 +41,26 @@ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentTo
32
41
  refunds: {},
33
42
  });
34
43
  }
35
- var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct });
44
+ var bundleFill = __assign(__assign({}, fill), { lpFeePct: lpFeePct, relayer: repaymentAddress });
36
45
  // Add all fills, slow and fast, to dictionary.
37
46
  assign(dict, [repaymentChainId, repaymentToken, "fills"], [bundleFill]);
38
47
  // All fills update the bundle LP fees.
39
48
  var refundObj = dict[repaymentChainId][repaymentToken];
40
- var realizedLpFee = fill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
49
+ var realizedLpFee = bundleFill.inputAmount.mul(bundleFill.lpFeePct).div(fixedPointAdjustment);
41
50
  refundObj.realizedLpFees = refundObj.realizedLpFees ? refundObj.realizedLpFees.add(realizedLpFee) : realizedLpFee;
42
51
  // Only fast fills get refunded.
43
- if (!isSlowFill(fill)) {
44
- var refundAmount = fill.inputAmount.mul(fixedPointAdjustment.sub(lpFeePct)).div(fixedPointAdjustment);
52
+ if (!isSlowFill(bundleFill)) {
53
+ var refundAmount = bundleFill.inputAmount.mul(fixedPointAdjustment.sub(lpFeePct)).div(fixedPointAdjustment);
45
54
  refundObj.totalRefundAmount = refundObj.totalRefundAmount
46
55
  ? refundObj.totalRefundAmount.add(refundAmount)
47
56
  : refundAmount;
48
57
  // Instantiate dictionary if it doesn't exist.
49
58
  (_b = refundObj.refunds) !== null && _b !== void 0 ? _b : (refundObj.refunds = {});
50
- if (refundObj.refunds[fill.relayer]) {
51
- refundObj.refunds[fill.relayer] = refundObj.refunds[fill.relayer].add(refundAmount);
59
+ if (refundObj.refunds[bundleFill.relayer]) {
60
+ refundObj.refunds[bundleFill.relayer] = refundObj.refunds[bundleFill.relayer].add(refundAmount);
52
61
  }
53
62
  else {
54
- refundObj.refunds[fill.relayer] = refundAmount;
63
+ refundObj.refunds[bundleFill.relayer] = refundAmount;
55
64
  }
56
65
  }
57
66
  }
@@ -209,77 +218,122 @@ var BundleDataClient = /** @class */ (function () {
209
218
  return [4 /*yield*/, this.loadArweaveData(bundleEvaluationBlockRanges)];
210
219
  case 1:
211
220
  arweaveData = _a.sent();
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)];
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)];
231
243
  }
232
244
  });
233
245
  });
234
246
  };
235
247
  // @dev This helper function should probably be moved to the InventoryClient
236
248
  BundleDataClient.prototype.getApproximateRefundsForBlockRange = function (chainIds, blockRanges) {
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;
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;
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] || fill.blockNumber > blockRanges[chainIndex][1]) {
271
+ return [2 /*return*/, false];
272
+ }
273
+ // If origin spoke pool client isn't defined, we can't validate it.
274
+ if (this.spokePoolClients[fill.originChainId] === undefined) {
275
+ return [2 /*return*/, false];
276
+ }
277
+ matchingDeposit = this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
278
+ hasMatchingDeposit = matchingDeposit !== undefined &&
279
+ this.getRelayHashFromEvent(fill) === this.getRelayHashFromEvent(matchingDeposit);
280
+ if (!hasMatchingDeposit) return [3 /*break*/, 2];
281
+ return [4 /*yield*/, verifyFillRepayment(fill, this.spokePoolClients[fill.destinationChainId].spokePool.provider, matchingDeposit,
282
+ // @dev: to get valid repayment chain ID's, get all chain IDs for the bundle block range and remove
283
+ // disabled block ranges.
284
+ this.clients.configStoreClient
285
+ .getChainIdIndicesForBlock(blockRanges[0][1])
286
+ .filter(function (_chainId, i) { return !isChainDisabled(blockRanges[i]); }))];
287
+ case 1:
288
+ validRepayment = _a.sent();
289
+ if (!isDefined(validRepayment)) {
290
+ return [2 /*return*/, false];
291
+ }
292
+ _a.label = 2;
293
+ case 2: return [2 /*return*/, hasMatchingDeposit];
294
+ }
295
+ });
296
+ }); })];
297
+ case 1:
298
+ fillsToCount = _b.sent();
299
+ fillsToCount.forEach(function (fill) {
300
+ var _a, _b, _c;
301
+ var _d;
302
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
303
+ assert(isDefined(matchingDeposit), "Deposit not found for fill.");
304
+ var _e = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain // Use ! because we've already asserted that matchingDeposit is defined.
305
+ ), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
306
+ // Assume that lp fees are 0 for the sake of speed. In the future we could batch compute
307
+ // these or make hardcoded assumptions based on the origin-repayment chain direction. This might result
308
+ // in slight over estimations of refunds, but its not clear whether underestimating or overestimating is
309
+ // worst from the relayer's perspective.
310
+ var relayer = fill.relayer, refundAmount = fill.inputAmount;
311
+ (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
312
+ (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
313
+ var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : bnZero;
314
+ refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
315
+ });
316
+ return [2 /*return*/];
317
+ }
318
+ });
319
+ };
320
+ this_1 = this;
321
+ _i = 0, chainIds_1 = chainIds;
322
+ _a.label = 1;
323
+ case 1:
324
+ if (!(_i < chainIds_1.length)) return [3 /*break*/, 4];
325
+ chainId = chainIds_1[_i];
326
+ return [5 /*yield**/, _loop_1(chainId)];
327
+ case 2:
328
+ _a.sent();
329
+ _a.label = 3;
330
+ case 3:
331
+ _i++;
332
+ return [3 /*break*/, 1];
333
+ case 4: return [2 /*return*/, refundsForChain];
253
334
  }
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);
275
335
  });
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;
336
+ });
283
337
  };
284
338
  BundleDataClient.prototype.getUpcomingDepositAmount = function (chainId, l2Token, latestBlockToSearch) {
285
339
  if (this.spokePoolClients[chainId] === undefined) {
@@ -371,33 +425,40 @@ var BundleDataClient = /** @class */ (function () {
371
425
  return [initialBlockRange[1] + 1, blockRange[1]];
372
426
  });
373
427
  }
374
- var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, bundleFillsV3, expiredDepositsToRefundV3, start;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
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) {
377
431
  case 0:
378
432
  hubPoolClient = this.clients.hubPoolClient;
379
433
  nextBundleMainnetStartBlock = hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, hubPoolClient.latestBlockSearched, hubPoolClient.chainId);
380
434
  chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
381
435
  combinedRefunds = [];
382
436
  widestBundleBlockRanges = getWidestPossibleExpectedBlockRange(chainIds, this.spokePoolClients, getEndBlockBuffers(chainIds, this.blockRangeEndBlockBuffer), this.clients, this.clients.hubPoolClient.latestBlockSearched, this.clients.configStoreClient.getEnabledChains(this.clients.hubPoolClient.latestBlockSearched));
383
- if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 2];
437
+ if (!hubPoolClient.hasPendingProposal()) return [3 /*break*/, 5];
384
438
  pendingBundleBlockRanges = getImpliedBundleBlockRanges(hubPoolClient, this.clients.configStoreClient, hubPoolClient.getLatestProposedRootBundle());
385
439
  return [4 /*yield*/, this.loadArweaveData(pendingBundleBlockRanges)];
386
440
  case 1:
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
- }
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:
395
453
  // Shorten the widestBundleBlockRanges now to not double count the pending bundle blocks.
396
454
  widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
397
- _a.label = 2;
398
- case 2:
455
+ _e.label = 5;
456
+ case 5:
399
457
  start = performance.now();
400
- combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges));
458
+ _d = (_c = combinedRefunds).push;
459
+ return [4 /*yield*/, this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges)];
460
+ case 6:
461
+ _d.apply(_c, [_e.sent()]);
401
462
  this.logger.debug({
402
463
  at: "BundleDataClient#getNextBundleRefunds",
403
464
  message: "Loading approximate refunds for next bundle in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -540,7 +601,7 @@ var BundleDataClient = /** @class */ (function () {
540
601
  };
541
602
  BundleDataClient.prototype.loadDataFromScratch = function (blockRangesForChains, spokePoolClients) {
542
603
  return __awaiter(this, void 0, void 0, function () {
543
- var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, _canCreateSlowFillLeaf, _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;
604
+ var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, _canCreateSlowFillLeaf, _depositIsExpired, _getFillStatusForDeposit, allChainIds, _cachedBundleTimestamps, bundleBlockTimestamps, v3RelayHashes, bundleDepositHashes, olderDepositHashes, startBlockForMainnet, versionAtProposalBlock, canRefundPrefills, depositCounter, _loop_2, _i, allChainIds_1, originChainId, validatedBundleV3Fills, validatedBundleSlowFills, validatedBundleUnexecutableSlowFills, fillCounter, _loop_3, _a, allChainIds_2, originChainId, promises, _b, v3FillLpFees, v3SlowFillLpFees, v3UnexecutableSlowFillLpFees, v3SpokeEventsReadable;
544
605
  var _this = this;
545
606
  return __generator(this, function (_c) {
546
607
  switch (_c.label) {
@@ -575,6 +636,9 @@ var BundleDataClient = /** @class */ (function () {
575
636
  !deposit.fromLiteChain &&
576
637
  !deposit.toLiteChain);
577
638
  };
639
+ _depositIsExpired = function (deposit) {
640
+ return deposit.fillDeadline < bundleBlockTimestamps[deposit.destinationChainId][1];
641
+ };
578
642
  _getFillStatusForDeposit = function (deposit, queryBlock) {
579
643
  return spokePoolClients[deposit.destinationChainId].relayFillStatus(deposit,
580
644
  // We can assume that in production
@@ -614,6 +678,9 @@ var BundleDataClient = /** @class */ (function () {
614
678
  v3RelayHashes = {};
615
679
  bundleDepositHashes = [];
616
680
  olderDepositHashes = [];
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";
617
684
  depositCounter = 0;
618
685
  _loop_2 = function (originChainId) {
619
686
  var originClient = spokePoolClients[originChainId];
@@ -627,11 +694,12 @@ var BundleDataClient = /** @class */ (function () {
627
694
  // Only evaluate deposits that are in this bundle or in previous bundles. This means we cannot issue fill
628
695
  // refunds or slow fills here for deposits that are in future bundles (i.e. "pre-fills"). Instead, we'll
629
696
  // evaluate these pre-fills once the deposit is inside the "current" bundle block range.
630
- if (isZeroValueDeposit(deposit) || deposit.blockNumber > originChainBlockRange[1]) {
697
+ if (deposit.blockNumber > originChainBlockRange[1] || isZeroValueDeposit(deposit)) {
631
698
  return;
632
699
  }
633
700
  depositCounter++;
634
701
  var relayDataHash = _this.getRelayHashFromEvent(deposit);
702
+ // Duplicate deposits are treated like normal deposits.
635
703
  if (!v3RelayHashes[relayDataHash]) {
636
704
  v3RelayHashes[relayDataHash] = {
637
705
  deposit: deposit,
@@ -646,6 +714,8 @@ var BundleDataClient = /** @class */ (function () {
646
714
  if (deposit.inputAmount.eq(0)) {
647
715
  return;
648
716
  }
717
+ // Evaluate all expired deposits after fetching fill statuses,
718
+ // since we can't know for certain whether an expired deposit was filled a long time ago.
649
719
  if (deposit.blockNumber >= originChainBlockRange[0]) {
650
720
  bundleDepositHashes.push(relayDataHash);
651
721
  updateBundleDepositsV3(bundleDepositsV3, deposit);
@@ -676,7 +746,7 @@ var BundleDataClient = /** @class */ (function () {
676
746
  case 0:
677
747
  originClient = spokePoolClients[originChainId];
678
748
  _loop_4 = function (destinationChainId) {
679
- var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills, originBlockRange;
749
+ var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills;
680
750
  return __generator(this, function (_g) {
681
751
  switch (_g.label) {
682
752
  case 0:
@@ -691,73 +761,85 @@ var BundleDataClient = /** @class */ (function () {
691
761
  // We can remove fills for deposits with input amount equal to zero because these will result in 0 refunded
692
762
  // tokens to the filler. We can't remove non-empty message deposit here in case there is a slow fill
693
763
  // request for the deposit, we'd want to see the fill took place.
694
- .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1] && !isZeroValueDeposit(fill); }), function (fill) { return __awaiter(_this, void 0, void 0, function () {
695
- var relayDataHash, historicalDeposit, matchedDeposit;
764
+ .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1] && !isZeroValueDeposit(fill); }), function (_fill) { return __awaiter(_this, void 0, void 0, function () {
765
+ var relayDataHash, fill, historicalDeposit, matchedDeposit, fill;
696
766
  return __generator(this, function (_a) {
697
767
  switch (_a.label) {
698
768
  case 0:
699
- relayDataHash = this.getRelayHashFromEvent(fill);
700
769
  fillCounter++;
701
- if (v3RelayHashes[relayDataHash]) {
702
- if (!v3RelayHashes[relayDataHash].fill) {
703
- assert(isDefined(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
704
- // At this point, the v3RelayHashes entry already existed meaning that there is a matching deposit,
705
- // so this fill is validated.
706
- v3RelayHashes[relayDataHash].fill = fill;
707
- if (fill.blockNumber >= destinationChainBlockRange[0]) {
708
- validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: v3RelayHashes[relayDataHash].deposit.quoteTimestamp }));
709
- // If fill replaced a slow fill request, then mark it as one that might have created an
710
- // unexecutable slow fill. We can't know for sure until we check the slow fill request
711
- // events.
712
- // slow fill requests for deposits from or to lite chains are considered invalid
713
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
714
- !v3RelayHashes[relayDataHash].deposit.fromLiteChain &&
715
- !v3RelayHashes[relayDataHash].deposit.toLiteChain) {
716
- fastFillsReplacingSlowFills.push(relayDataHash);
717
- }
718
- }
719
- }
770
+ relayDataHash = this.getRelayHashFromEvent(_fill);
771
+ if (!v3RelayHashes[relayDataHash]) return [3 /*break*/, 3];
772
+ if (!!v3RelayHashes[relayDataHash].fill) return [3 /*break*/, 2];
773
+ assert(isDefined(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
774
+ return [4 /*yield*/, verifyFillRepayment(_fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposit, allChainIds)];
775
+ case 1:
776
+ fill = _a.sent();
777
+ if (!isDefined(fill)) {
778
+ bundleInvalidFillsV3.push(_fill);
720
779
  return [2 /*return*/];
721
780
  }
781
+ // At this point, the v3RelayHashes entry already existed meaning that there is a matching deposit,
782
+ // so this fill is validated.
783
+ v3RelayHashes[relayDataHash].fill = fill;
784
+ if (fill.blockNumber >= destinationChainBlockRange[0]) {
785
+ validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: v3RelayHashes[relayDataHash].deposit.quoteTimestamp }));
786
+ // If fill replaced a slow fill request, then mark it as one that might have created an
787
+ // unexecutable slow fill. We can't know for sure until we check the slow fill request
788
+ // events.
789
+ // slow fill requests for deposits from or to lite chains are considered invalid
790
+ if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
791
+ _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposit)) {
792
+ fastFillsReplacingSlowFills.push(relayDataHash);
793
+ }
794
+ }
795
+ _a.label = 2;
796
+ case 2: return [2 /*return*/];
797
+ case 3:
722
798
  // At this point, there is no relay hash dictionary entry for this fill, so we need to
723
- // instantiate the entry.
799
+ // instantiate the entry. We won't modify the fill.relayer until we match it with a deposit.
724
800
  v3RelayHashes[relayDataHash] = {
725
801
  deposit: undefined,
726
- fill: fill,
802
+ fill: _fill,
727
803
  slowFillRequest: undefined,
728
804
  };
729
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 2];
805
+ if (!(_fill.blockNumber >= destinationChainBlockRange[0])) return [3 /*break*/, 7];
730
806
  // Fill has a non-infinite expiry, and we can assume our spoke pool clients have old enough deposits
731
807
  // to conclude that this fill is invalid if we haven't found a matching deposit in memory, so
732
808
  // skip the historical query.
733
- if (!INFINITE_FILL_DEADLINE.eq(fill.fillDeadline)) {
734
- bundleInvalidFillsV3.push(fill);
809
+ if (!INFINITE_FILL_DEADLINE.eq(_fill.fillDeadline)) {
810
+ bundleInvalidFillsV3.push(_fill);
735
811
  return [2 /*return*/];
736
812
  }
737
- return [4 /*yield*/, queryHistoricalDepositForFill(originClient, fill)];
738
- case 1:
813
+ return [4 /*yield*/, queryHistoricalDepositForFill(originClient, _fill)];
814
+ case 4:
739
815
  historicalDeposit = _a.sent();
740
- if (!historicalDeposit.found) {
741
- bundleInvalidFillsV3.push(fill);
816
+ if (!!historicalDeposit.found) return [3 /*break*/, 5];
817
+ bundleInvalidFillsV3.push(_fill);
818
+ return [3 /*break*/, 7];
819
+ case 5:
820
+ matchedDeposit = historicalDeposit.deposit;
821
+ return [4 /*yield*/, verifyFillRepayment(_fill, destinationClient.spokePool.provider, matchedDeposit, allChainIds)];
822
+ case 6:
823
+ fill = _a.sent();
824
+ if (!isDefined(fill)) {
825
+ bundleInvalidFillsV3.push(_fill);
826
+ return [2 /*return*/];
742
827
  }
743
- else {
744
- matchedDeposit = historicalDeposit.deposit;
745
- // @dev Since queryHistoricalDepositForFill validates the fill by checking individual
746
- // object property values against the deposit's, we
747
- // sanity check it here by comparing the full relay hashes. If there's an error here then the
748
- // historical deposit query is not working as expected.
749
- assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
750
- validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
751
- v3RelayHashes[relayDataHash].deposit = matchedDeposit;
752
- // slow fill requests for deposits from or to lite chains are considered invalid
753
- if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
754
- !matchedDeposit.fromLiteChain &&
755
- !matchedDeposit.toLiteChain) {
756
- fastFillsReplacingSlowFills.push(relayDataHash);
757
- }
828
+ v3RelayHashes[relayDataHash].fill = fill;
829
+ // @dev Since queryHistoricalDepositForFill validates the fill by checking individual
830
+ // object property values against the deposit's, we
831
+ // sanity check it here by comparing the full relay hashes. If there's an error here then the
832
+ // historical deposit query is not working as expected.
833
+ assert(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
834
+ validatedBundleV3Fills.push(__assign(__assign({}, fill), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
835
+ v3RelayHashes[relayDataHash].deposit = matchedDeposit;
836
+ // slow fill requests for deposits from or to lite chains are considered invalid
837
+ if (fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill &&
838
+ _canCreateSlowFillLeaf(matchedDeposit)) {
839
+ fastFillsReplacingSlowFills.push(relayDataHash);
758
840
  }
759
- _a.label = 2;
760
- case 2: return [2 /*return*/];
841
+ _a.label = 7;
842
+ case 7: return [2 /*return*/];
761
843
  }
762
844
  });
763
845
  }); })];
@@ -767,7 +849,9 @@ var BundleDataClient = /** @class */ (function () {
767
849
  // for deposits that would expire in this bundle.
768
850
  return [4 /*yield*/, forEachAsync(destinationClient
769
851
  .getSlowFillRequestsForOriginChain(originChainId)
770
- .filter(function (request) { return request.blockNumber <= destinationChainBlockRange[1] && !isZeroValueDeposit(request); }), function (slowFillRequest) { return __awaiter(_this, void 0, void 0, function () {
852
+ .filter(function (request) {
853
+ return request.blockNumber <= destinationChainBlockRange[1] && !isZeroValueFillOrSlowFillRequest(request);
854
+ }), function (slowFillRequest) { return __awaiter(_this, void 0, void 0, function () {
771
855
  var relayDataHash, matchedDeposit, historicalDeposit, matchedDeposit;
772
856
  return __generator(this, function (_a) {
773
857
  switch (_a.label) {
@@ -785,15 +869,13 @@ var BundleDataClient = /** @class */ (function () {
785
869
  }
786
870
  assert(isDefined(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
787
871
  matchedDeposit = v3RelayHashes[relayDataHash].deposit;
788
- if (!_canCreateSlowFillLeaf(matchedDeposit)) {
789
- return [2 /*return*/];
790
- }
791
872
  // If there is no fill matching the relay hash, then this might be a valid slow fill request
792
873
  // that we should produce a slow fill leaf for. Check if the slow fill request is in the
793
874
  // destination chain block range.
794
875
  if (slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
876
+ _canCreateSlowFillLeaf(matchedDeposit) &&
795
877
  // Deposit must not have expired in this bundle.
796
- slowFillRequest.fillDeadline >= bundleBlockTimestamps[destinationChainId][1]) {
878
+ !_depositIsExpired(matchedDeposit)) {
797
879
  // At this point, the v3RelayHashes entry already existed meaning that there is a matching deposit,
798
880
  // so this slow fill request relay data is correct.
799
881
  validatedBundleSlowFills.push(matchedDeposit);
@@ -825,7 +907,7 @@ var BundleDataClient = /** @class */ (function () {
825
907
  v3RelayHashes[relayDataHash].deposit = matchedDeposit;
826
908
  if (!_canCreateSlowFillLeaf(matchedDeposit) ||
827
909
  // Deposit must not have expired in this bundle.
828
- slowFillRequest.fillDeadline < bundleBlockTimestamps[destinationChainId][1]) {
910
+ _depositIsExpired(matchedDeposit)) {
829
911
  return [2 /*return*/];
830
912
  }
831
913
  validatedBundleSlowFills.push(matchedDeposit);
@@ -838,17 +920,20 @@ var BundleDataClient = /** @class */ (function () {
838
920
  // Process slow fill requests. One invariant we need to maintain is that we cannot create slow fill requests
839
921
  // for deposits that would expire in this bundle.
840
922
  _g.sent();
841
- originBlockRange = getBlockRangeForChain(blockRangesForChains, originChainId, chainIds);
923
+ // Deposits can be submitted an arbitrary amount of time after matching fills and slow fill requests.
924
+ // Therefore, let's go through each deposit in this bundle again and check a few things in order:
925
+ // - Has the deposit been filled ? If so, then we need to issue a relayer refund for
926
+ // this "pre-fill" if the fill took place in a previous bundle.
927
+ // - Or, has the deposit expired in this bundle? If so, then we need to issue an expiry refund.
928
+ // - And finally, has the deposit been slow filled? If so, then we need to issue a slow fill leaf
929
+ // for this "pre-slow-fill-request" if this request took place in a previous bundle.
930
+ // @todo Only start refunding pre-fills and slow fill requests after a config store version is activated. We
931
+ // should remove this check once we've advanced far beyond the version bump block.
842
932
  return [4 /*yield*/, mapAsync(bundleDepositHashes.filter(function (depositHash) {
843
933
  var deposit = v3RelayHashes[depositHash].deposit;
844
- return (deposit &&
845
- deposit.originChainId === originChainId &&
846
- deposit.destinationChainId === destinationChainId &&
847
- deposit.blockNumber >= originBlockRange[0] &&
848
- deposit.blockNumber <= originBlockRange[1] &&
849
- !isZeroValueDeposit(deposit));
934
+ return (deposit && deposit.originChainId === originChainId && deposit.destinationChainId === destinationChainId);
850
935
  }), function (depositHash) { return __awaiter(_this, void 0, void 0, function () {
851
- var _a, deposit, fill, slowFillRequest, fillStatus, prefill;
936
+ var _a, deposit, fill, slowFillRequest, fillStatus, prefill, verifiedFill;
852
937
  return __generator(this, function (_b) {
853
938
  switch (_b.label) {
854
939
  case 0:
@@ -863,7 +948,7 @@ var BundleDataClient = /** @class */ (function () {
863
948
  // If fill exists in memory, then the only case in which we need to create a refund is if the
864
949
  // the fill occurred in a previous bundle. There are no expiry refunds for filled deposits.
865
950
  if (fill) {
866
- if (!isSlowFill(fill) && fill.blockNumber < destinationChainBlockRange[0]) {
951
+ if (canRefundPrefills && fill.blockNumber < destinationChainBlockRange[0] && !isSlowFill(fill)) {
867
952
  // If fill is in the current bundle then we can assume there is already a refund for it, so only
868
953
  // include this pre fill if the fill is in an older bundle. If fill is after this current bundle, then
869
954
  // we won't consider it, following the previous treatment of fills after the bundle block range.
@@ -877,11 +962,12 @@ var BundleDataClient = /** @class */ (function () {
877
962
  // we need to create a slow fill leaf for the deposit. The latter should only happen if the slow fill request
878
963
  // took place in a prior bundle otherwise we would have already created a slow fill leaf for it.
879
964
  if (slowFillRequest) {
880
- if (deposit.fillDeadline < bundleBlockTimestamps[destinationChainId][1]) {
965
+ if (_depositIsExpired(deposit)) {
881
966
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
882
967
  }
883
- else if (_canCreateSlowFillLeaf(deposit) &&
884
- slowFillRequest.blockNumber < destinationChainBlockRange[0]) {
968
+ else if (canRefundPrefills &&
969
+ slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
970
+ _canCreateSlowFillLeaf(deposit)) {
885
971
  validatedBundleSlowFills.push(deposit);
886
972
  }
887
973
  return [2 /*return*/];
@@ -889,34 +975,46 @@ var BundleDataClient = /** @class */ (function () {
889
975
  return [4 /*yield*/, _getFillStatusForDeposit(deposit, destinationChainBlockRange[1])];
890
976
  case 1:
891
977
  fillStatus = _b.sent();
892
- if (!(fillStatus === FillStatus.Filled)) return [3 /*break*/, 3];
978
+ if (!(fillStatus === FillStatus.Filled)) return [3 /*break*/, 4];
893
979
  return [4 /*yield*/, findFillEvent(destinationClient.spokePool, deposit, destinationClient.deploymentBlock, destinationClient.latestBlockSearched)];
894
980
  case 2:
895
981
  prefill = (_b.sent());
896
- if (!isSlowFill(prefill)) {
982
+ return [4 /*yield*/, verifyFillRepayment(prefill, destinationClient.spokePool.provider, deposit, allChainIds)];
983
+ case 3:
984
+ verifiedFill = _b.sent();
985
+ if (canRefundPrefills && isDefined(verifiedFill) && !isSlowFill(prefill)) {
897
986
  validatedBundleV3Fills.push(__assign(__assign({}, prefill), { quoteTimestamp: deposit.quoteTimestamp }));
898
987
  }
899
- return [3 /*break*/, 4];
900
- case 3:
901
- if (deposit.fillDeadline < bundleBlockTimestamps[destinationChainId][1]) {
988
+ return [3 /*break*/, 5];
989
+ case 4:
990
+ if (_depositIsExpired(deposit)) {
902
991
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
903
992
  }
904
993
  // If slow fill requested, then issue a slow fill leaf for the deposit.
905
994
  else if (fillStatus === FillStatus.RequestedSlowFill) {
906
995
  // Input and Output tokens must be equivalent on the deposit for this to be slow filled.
907
996
  // Slow fill requests for deposits from or to lite chains are considered invalid
908
- if (_canCreateSlowFillLeaf(deposit)) {
997
+ if (canRefundPrefills && _canCreateSlowFillLeaf(deposit)) {
909
998
  // If deposit newly expired, then we can't create a slow fill leaf for it but we can
910
999
  // create a deposit refund for it.
911
1000
  validatedBundleSlowFills.push(deposit);
912
1001
  }
913
1002
  }
914
- _b.label = 4;
915
- case 4: return [2 /*return*/];
1003
+ _b.label = 5;
1004
+ case 5: return [2 /*return*/];
916
1005
  }
917
1006
  });
918
1007
  }); })];
919
1008
  case 3:
1009
+ // Deposits can be submitted an arbitrary amount of time after matching fills and slow fill requests.
1010
+ // Therefore, let's go through each deposit in this bundle again and check a few things in order:
1011
+ // - Has the deposit been filled ? If so, then we need to issue a relayer refund for
1012
+ // this "pre-fill" if the fill took place in a previous bundle.
1013
+ // - Or, has the deposit expired in this bundle? If so, then we need to issue an expiry refund.
1014
+ // - And finally, has the deposit been slow filled? If so, then we need to issue a slow fill leaf
1015
+ // for this "pre-slow-fill-request" if this request took place in a previous bundle.
1016
+ // @todo Only start refunding pre-fills and slow fill requests after a config store version is activated. We
1017
+ // should remove this check once we've advanced far beyond the version bump block.
920
1018
  _g.sent();
921
1019
  // For all fills that came after a slow fill request, we can now check if the slow fill request
922
1020
  // was a valid one and whether it was created in a previous bundle. If so, then it created a slow fill
@@ -929,16 +1027,13 @@ var BundleDataClient = /** @class */ (function () {
929
1027
  throw new Error("Deposit should exist in relay hash dictionary.");
930
1028
  }
931
1029
  // We should never push fast fills involving lite chains here because slow fill requests for them are invalid:
932
- assert(!deposit.fromLiteChain && !deposit.toLiteChain, "fastFillsReplacingSlowFills should not contain lite chain deposits");
1030
+ assert(_canCreateSlowFillLeaf(deposit), "fastFillsReplacingSlowFills should contain only deposits that can be slow filled");
933
1031
  var destinationBlockRange = getBlockRangeForChain(blockRangesForChains, destinationChainId, chainIds);
934
1032
  if (
935
- // If the slow fill request that was replaced by this fill was in an older bundle, then we don't
936
- // need to check if the slow fill request was valid since we can assume all bundles in the past
937
- // were validated. However, we might as well double check.
938
- _this.clients.hubPoolClient.areTokensEquivalent(deposit.inputToken, deposit.originChainId, deposit.outputToken, deposit.destinationChainId, deposit.quoteBlockNumber) &&
939
- // If there is a slow fill request in this bundle that matches the relay hash, then there was no slow fill
940
- // created that would be considered excess.
941
- (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0])) {
1033
+ // If there is a slow fill request in this bundle that matches the relay hash, then there was no slow fill
1034
+ // created that would be considered excess.
1035
+ !slowFillRequest ||
1036
+ slowFillRequest.blockNumber < destinationBlockRange[0]) {
942
1037
  validatedBundleUnexecutableSlowFills.push(deposit);
943
1038
  }
944
1039
  });
@@ -996,7 +1091,7 @@ var BundleDataClient = /** @class */ (function () {
996
1091
  // If there is a valid fill that we saw matching this deposit, then it does not need a refund.
997
1092
  (!fill &&
998
1093
  isDefined(deposit) && // Needed for TSC - we check this above.
999
- deposit.fillDeadline < bundleBlockTimestamps[destinationChainId][1] &&
1094
+ _depositIsExpired(deposit) &&
1000
1095
  deposit.fillDeadline >= bundleBlockTimestamps[destinationChainId][0] &&
1001
1096
  spokePoolClients[destinationChainId] !== undefined))
1002
1097
  // If there is a valid fill that we saw matching this deposit, then it does not need a refund.
@@ -1012,8 +1107,7 @@ var BundleDataClient = /** @class */ (function () {
1012
1107
  // If fill status is RequestedSlowFill, then we might need to mark down an unexecutable
1013
1108
  // slow fill that we're going to replace with an expired deposit refund.
1014
1109
  // If deposit cannot be slow filled, then exit early.
1015
- // slow fill requests for deposits from or to lite chains are considered invalid
1016
- if (fillStatus !== FillStatus.RequestedSlowFill || deposit.fromLiteChain || deposit.toLiteChain) {
1110
+ if (fillStatus !== FillStatus.RequestedSlowFill || !_canCreateSlowFillLeaf(deposit)) {
1017
1111
  return [2 /*return*/];
1018
1112
  }
1019
1113
  // Now, check if there was a slow fill created for this deposit in a previous bundle which would now be
@@ -1021,14 +1115,9 @@ var BundleDataClient = /** @class */ (function () {
1021
1115
  // slow fill request or the matching slow fill request took place in a previous bundle.
1022
1116
  // If there is a slow fill request in this bundle, then the expired deposit refund will supercede
1023
1117
  // the slow fill request. If there is no slow fill request seen or its older than this bundle, then we can
1024
- // assume a slow fill leaf was created for it because its tokens are equivalent. The slow fill request was
1025
- // also sent before the fill deadline expired since we checked that above.
1026
- if (
1027
- // Since this deposit was requested for a slow fill in an older bundle at this point, we don't
1028
- // technically need to check if the slow fill request was valid since we can assume all bundles in the past
1029
- // were validated. However, we might as well double check.
1030
- this.clients.hubPoolClient.areTokensEquivalent(deposit.inputToken, deposit.originChainId, deposit.outputToken, deposit.destinationChainId, deposit.quoteBlockNumber) &&
1031
- (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0])) {
1118
+ // assume a slow fill leaf was created for it because of the previous _canCreateSlowFillLeaf check.
1119
+ // The slow fill request was also sent before the fill deadline expired since we checked that above.
1120
+ if (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0]) {
1032
1121
  validatedBundleUnexecutableSlowFills.push(deposit);
1033
1122
  }
1034
1123
  _b.label = 2;
@@ -1076,7 +1165,7 @@ var BundleDataClient = /** @class */ (function () {
1076
1165
  var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1077
1166
  assert(isDefined(associatedDeposit), "Deposit should exist in relay hash dictionary.");
1078
1167
  var _b = getRefundInformationFromFill(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, associatedDeposit.fromLiteChain), chainToSendRefundTo = _b.chainToSendRefundTo, repaymentToken = _b.repaymentToken;
1079
- updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken);
1168
+ updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken, fill.relayer);
1080
1169
  });
1081
1170
  v3SlowFillLpFees.forEach(function (_a, idx) {
1082
1171
  var lpFeePct = _a.realizedLpFeePct;
@@ -1119,7 +1208,7 @@ var BundleDataClient = /** @class */ (function () {
1119
1208
  // spoke pool contract. However, this internal function is used to uniquely identify a bridging event
1120
1209
  // for speed since its easier to build a string from the event data than to hash it.
1121
1210
  BundleDataClient.prototype.getRelayHashFromEvent = function (event) {
1122
- 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, "-").concat(event.fillDeadline, "-").concat(event.exclusivityDeadline, "-").concat(event.message, "-").concat(event.destinationChainId);
1211
+ 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);
1123
1212
  };
1124
1213
  BundleDataClient.prototype.getBundleBlockTimestamps = function (chainIds, blockRangesForChains, spokePoolClients) {
1125
1214
  return __awaiter(this, void 0, void 0, function () {