@across-protocol/sdk 4.0.0-beta.9 → 4.0.1

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 (132) hide show
  1. package/dist/cjs/clients/BundleDataClient/BundleDataClient.d.ts +5 -4
  2. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js +345 -187
  3. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  4. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.d.ts +1 -2
  5. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js +1 -2
  6. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -1
  7. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.d.ts +5 -1
  8. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js +47 -1
  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 +1 -0
  12. package/dist/cjs/clients/SpokePoolClient.js +10 -1
  13. package/dist/cjs/clients/SpokePoolClient.js.map +1 -1
  14. package/dist/cjs/clients/mocks/MockSpokePoolClient.d.ts +2 -1
  15. package/dist/cjs/clients/mocks/MockSpokePoolClient.js +11 -0
  16. package/dist/cjs/clients/mocks/MockSpokePoolClient.js.map +1 -1
  17. package/dist/cjs/constants.d.ts +0 -1
  18. package/dist/cjs/constants.js +1 -2
  19. package/dist/cjs/constants.js.map +1 -1
  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 +6 -1
  28. package/dist/cjs/utils/AddressUtils.js.map +1 -1
  29. package/dist/cjs/utils/BlockUtils.js +1 -0
  30. package/dist/cjs/utils/BlockUtils.js.map +1 -1
  31. package/dist/cjs/utils/DepositUtils.js +1 -1
  32. package/dist/cjs/utils/DepositUtils.js.map +1 -1
  33. package/dist/cjs/utils/EventUtils.js +21 -0
  34. package/dist/cjs/utils/EventUtils.js.map +1 -1
  35. package/dist/cjs/utils/NetworkUtils.js +2 -1
  36. package/dist/cjs/utils/NetworkUtils.js.map +1 -1
  37. package/dist/cjs/utils/SpokeUtils.d.ts +1 -0
  38. package/dist/cjs/utils/SpokeUtils.js +15 -8
  39. package/dist/cjs/utils/SpokeUtils.js.map +1 -1
  40. package/dist/cjs/utils/common.d.ts +1 -0
  41. package/dist/cjs/utils/common.js +2 -1
  42. package/dist/cjs/utils/common.js.map +1 -1
  43. package/dist/esm/clients/BundleDataClient/BundleDataClient.d.ts +5 -4
  44. package/dist/esm/clients/BundleDataClient/BundleDataClient.js +448 -281
  45. package/dist/esm/clients/BundleDataClient/BundleDataClient.js.map +1 -1
  46. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.d.ts +1 -2
  47. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js +2 -3
  48. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -1
  49. package/dist/esm/clients/BundleDataClient/utils/FillUtils.d.ts +5 -1
  50. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js +54 -1
  51. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js.map +1 -1
  52. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.d.ts +4 -4
  53. package/dist/esm/clients/SpokePoolClient.d.ts +8 -0
  54. package/dist/esm/clients/SpokePoolClient.js +17 -1
  55. package/dist/esm/clients/SpokePoolClient.js.map +1 -1
  56. package/dist/esm/clients/mocks/MockSpokePoolClient.d.ts +2 -1
  57. package/dist/esm/clients/mocks/MockSpokePoolClient.js +11 -0
  58. package/dist/esm/clients/mocks/MockSpokePoolClient.js.map +1 -1
  59. package/dist/esm/constants.d.ts +0 -1
  60. package/dist/esm/constants.js +0 -1
  61. package/dist/esm/constants.js.map +1 -1
  62. package/dist/esm/providers/index.d.ts +1 -0
  63. package/dist/esm/providers/index.js +2 -0
  64. package/dist/esm/providers/index.js.map +1 -1
  65. package/dist/esm/providers/mockProvider.d.ts +23 -0
  66. package/dist/esm/providers/mockProvider.js +73 -0
  67. package/dist/esm/providers/mockProvider.js.map +1 -0
  68. package/dist/esm/utils/AddressUtils.d.ts +1 -0
  69. package/dist/esm/utils/AddressUtils.js +9 -0
  70. package/dist/esm/utils/AddressUtils.js.map +1 -1
  71. package/dist/esm/utils/BlockUtils.js +1 -0
  72. package/dist/esm/utils/BlockUtils.js.map +1 -1
  73. package/dist/esm/utils/DepositUtils.js +2 -2
  74. package/dist/esm/utils/DepositUtils.js.map +1 -1
  75. package/dist/esm/utils/EventUtils.js +29 -1
  76. package/dist/esm/utils/EventUtils.js.map +1 -1
  77. package/dist/esm/utils/NetworkUtils.js +2 -1
  78. package/dist/esm/utils/NetworkUtils.js.map +1 -1
  79. package/dist/esm/utils/SpokeUtils.d.ts +1 -0
  80. package/dist/esm/utils/SpokeUtils.js +13 -7
  81. package/dist/esm/utils/SpokeUtils.js.map +1 -1
  82. package/dist/esm/utils/abi/typechain/Multicall3.d.ts +4 -1
  83. package/dist/esm/utils/abi/typechain/factories/Multicall3__factory.js.map +1 -1
  84. package/dist/esm/utils/common.d.ts +1 -0
  85. package/dist/esm/utils/common.js +1 -0
  86. package/dist/esm/utils/common.js.map +1 -1
  87. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts +5 -4
  88. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts.map +1 -1
  89. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts +1 -2
  90. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts.map +1 -1
  91. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts +5 -1
  92. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts.map +1 -1
  93. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts +4 -4
  94. package/dist/types/clients/SpokePoolClient.d.ts +8 -0
  95. package/dist/types/clients/SpokePoolClient.d.ts.map +1 -1
  96. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts +2 -1
  97. package/dist/types/clients/mocks/MockSpokePoolClient.d.ts.map +1 -1
  98. package/dist/types/constants.d.ts +0 -1
  99. package/dist/types/constants.d.ts.map +1 -1
  100. package/dist/types/providers/index.d.ts +1 -0
  101. package/dist/types/providers/index.d.ts.map +1 -1
  102. package/dist/types/providers/mockProvider.d.ts +24 -0
  103. package/dist/types/providers/mockProvider.d.ts.map +1 -0
  104. package/dist/types/utils/AddressUtils.d.ts +1 -0
  105. package/dist/types/utils/AddressUtils.d.ts.map +1 -1
  106. package/dist/types/utils/BlockUtils.d.ts.map +1 -1
  107. package/dist/types/utils/EventUtils.d.ts.map +1 -1
  108. package/dist/types/utils/NetworkUtils.d.ts.map +1 -1
  109. package/dist/types/utils/SpokeUtils.d.ts +1 -0
  110. package/dist/types/utils/SpokeUtils.d.ts.map +1 -1
  111. package/dist/types/utils/abi/typechain/Multicall3.d.ts +4 -1
  112. package/dist/types/utils/abi/typechain/Multicall3.d.ts.map +1 -1
  113. package/dist/types/utils/abi/typechain/common.d.ts.map +1 -1
  114. package/dist/types/utils/abi/typechain/factories/Multicall3__factory.d.ts.map +1 -1
  115. package/dist/types/utils/common.d.ts +1 -0
  116. package/dist/types/utils/common.d.ts.map +1 -1
  117. package/package.json +3 -3
  118. package/src/clients/BundleDataClient/BundleDataClient.ts +406 -249
  119. package/src/clients/BundleDataClient/utils/DataworkerUtils.ts +0 -8
  120. package/src/clients/BundleDataClient/utils/FillUtils.ts +66 -2
  121. package/src/clients/SpokePoolClient.ts +16 -3
  122. package/src/clients/mocks/MockSpokePoolClient.ts +14 -0
  123. package/src/constants.ts +0 -2
  124. package/src/providers/index.ts +1 -0
  125. package/src/providers/mockProvider.ts +77 -0
  126. package/src/utils/AddressUtils.ts +10 -0
  127. package/src/utils/BlockUtils.ts +1 -0
  128. package/src/utils/DepositUtils.ts +2 -2
  129. package/src/utils/EventUtils.ts +29 -1
  130. package/src/utils/NetworkUtils.ts +2 -1
  131. package/src/utils/SpokeUtils.ts +21 -8
  132. package/src/utils/common.ts +2 -0
@@ -27,11 +27,9 @@ function updateBundleDepositsV3(dict, deposit) {
27
27
  }
28
28
  dict[originChainId][inputToken].push(deposit);
29
29
  }
30
- function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken) {
30
+ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken, repaymentAddress) {
31
31
  var _a, _b;
32
- if ((0, utils_1.chainIsEvm)(fill.repaymentChainId) && !(0, utils_1.isValidEvmAddress)(fill.relayer)) {
33
- return;
34
- }
32
+ (0, utils_1.assert)((0, utils_1.chainIsEvm)(repaymentChainId) && (0, utils_2.isEvmRepaymentValid)(fill, repaymentChainId), "validatedBundleV3Fills dictionary should only contain fills with valid repayment information");
35
33
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[repaymentChainId]) === null || _a === void 0 ? void 0 : _a[repaymentToken])) {
36
34
  (0, utils_1.assign)(dict, [repaymentChainId, repaymentToken], {
37
35
  fills: [],
@@ -40,22 +38,22 @@ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentTo
40
38
  refunds: {},
41
39
  });
42
40
  }
43
- var bundleFill = tslib_1.__assign(tslib_1.__assign({}, fill), { lpFeePct: lpFeePct });
41
+ var bundleFill = tslib_1.__assign(tslib_1.__assign({}, fill), { lpFeePct: lpFeePct, relayer: repaymentAddress });
44
42
  (0, utils_1.assign)(dict, [repaymentChainId, repaymentToken, "fills"], [bundleFill]);
45
43
  var refundObj = dict[repaymentChainId][repaymentToken];
46
- var realizedLpFee = fill.inputAmount.mul(bundleFill.lpFeePct).div(utils_1.fixedPointAdjustment);
44
+ var realizedLpFee = bundleFill.inputAmount.mul(bundleFill.lpFeePct).div(utils_1.fixedPointAdjustment);
47
45
  refundObj.realizedLpFees = refundObj.realizedLpFees ? refundObj.realizedLpFees.add(realizedLpFee) : realizedLpFee;
48
- if (!(0, utils_1.isSlowFill)(fill)) {
49
- var refundAmount = fill.inputAmount.mul(utils_1.fixedPointAdjustment.sub(lpFeePct)).div(utils_1.fixedPointAdjustment);
46
+ if (!(0, utils_1.isSlowFill)(bundleFill)) {
47
+ var refundAmount = bundleFill.inputAmount.mul(utils_1.fixedPointAdjustment.sub(lpFeePct)).div(utils_1.fixedPointAdjustment);
50
48
  refundObj.totalRefundAmount = refundObj.totalRefundAmount
51
49
  ? refundObj.totalRefundAmount.add(refundAmount)
52
50
  : refundAmount;
53
51
  (_b = refundObj.refunds) !== null && _b !== void 0 ? _b : (refundObj.refunds = {});
54
- if (refundObj.refunds[fill.relayer]) {
55
- refundObj.refunds[fill.relayer] = refundObj.refunds[fill.relayer].add(refundAmount);
52
+ if (refundObj.refunds[bundleFill.relayer]) {
53
+ refundObj.refunds[bundleFill.relayer] = refundObj.refunds[bundleFill.relayer].add(refundAmount);
56
54
  }
57
55
  else {
58
- refundObj.refunds[fill.relayer] = refundAmount;
56
+ refundObj.refunds[bundleFill.relayer] = refundAmount;
59
57
  }
60
58
  }
61
59
  }
@@ -69,6 +67,9 @@ function updateBundleExcessSlowFills(dict, deposit) {
69
67
  }
70
68
  function updateBundleSlowFills(dict, deposit) {
71
69
  var _a;
70
+ if ((0, utils_1.chainIsEvm)(deposit.destinationChainId) && !(0, utils_1.isValidEvmAddress)(deposit.recipient)) {
71
+ return;
72
+ }
72
73
  var destinationChainId = deposit.destinationChainId, outputToken = deposit.outputToken;
73
74
  if (!((_a = dict === null || dict === void 0 ? void 0 : dict[destinationChainId]) === null || _a === void 0 ? void 0 : _a[outputToken])) {
74
75
  (0, utils_1.assign)(dict, [destinationChainId, outputToken], []);
@@ -153,7 +154,7 @@ var BundleDataClient = (function () {
153
154
  at: "BundleDataClient#loadPersistedDataFromArweave",
154
155
  message: "Loaded persisted data from Arweave in ".concat(Math.round(performance.now() - start) / 1000, "s."),
155
156
  blockRanges: JSON.stringify(blockRangesForChains),
156
- bundleData: (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleData.bundleDepositsV3, bundleData.bundleFillsV3, [], bundleData.bundleSlowFillsV3, bundleData.expiredDepositsToRefundV3, bundleData.unexecutableSlowFills),
157
+ bundleData: (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.expiredDepositsToRefundV3, bundleData.unexecutableSlowFills),
157
158
  });
158
159
  return [2, bundleData];
159
160
  }
@@ -195,64 +196,107 @@ var BundleDataClient = (function () {
195
196
  return [4, this.loadArweaveData(bundleEvaluationBlockRanges)];
196
197
  case 1:
197
198
  arweaveData = _a.sent();
198
- if (arweaveData === undefined) {
199
- combinedRefunds = this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges);
200
- }
201
- else {
202
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
203
- combinedRefunds = (0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3);
204
- Object.keys(combinedRefunds).forEach(function (chainId) {
205
- if (_this.spokePoolClients[Number(chainId)] === undefined) {
206
- delete combinedRefunds[Number(chainId)];
207
- }
208
- });
209
- }
210
- return [2, this.deductExecutedRefunds(combinedRefunds, bundle)];
199
+ if (!(arweaveData === undefined)) return [3, 3];
200
+ return [4, this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges)];
201
+ case 2:
202
+ combinedRefunds = _a.sent();
203
+ return [3, 4];
204
+ case 3:
205
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
206
+ combinedRefunds = (0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3);
207
+ Object.keys(combinedRefunds).forEach(function (chainId) {
208
+ if (_this.spokePoolClients[Number(chainId)] === undefined) {
209
+ delete combinedRefunds[Number(chainId)];
210
+ }
211
+ });
212
+ _a.label = 4;
213
+ case 4: return [2, this.deductExecutedRefunds(combinedRefunds, bundle)];
211
214
  }
212
215
  });
213
216
  });
214
217
  };
215
218
  BundleDataClient.prototype.getApproximateRefundsForBlockRange = function (chainIds, blockRanges) {
216
- var _this = this;
217
- var refundsForChain = {};
218
- var _loop_1 = function (chainId) {
219
- if (this_1.spokePoolClients[chainId] === undefined) {
220
- return "continue";
221
- }
222
- var chainIndex = chainIds.indexOf(chainId);
223
- this_1.spokePoolClients[chainId]
224
- .getFills()
225
- .filter(function (fill) {
226
- if (fill.blockNumber < blockRanges[chainIndex][0] || fill.blockNumber > blockRanges[chainIndex][1]) {
227
- return false;
228
- }
229
- if (_this.spokePoolClients[fill.originChainId] === undefined) {
230
- return false;
219
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
220
+ var refundsForChain, _loop_1, this_1, _i, chainIds_1, chainId;
221
+ var _this = this;
222
+ return tslib_1.__generator(this, function (_a) {
223
+ switch (_a.label) {
224
+ case 0:
225
+ refundsForChain = {};
226
+ _loop_1 = function (chainId) {
227
+ var chainIndex, fillsToCount;
228
+ return tslib_1.__generator(this, function (_b) {
229
+ switch (_b.label) {
230
+ case 0:
231
+ if (this_1.spokePoolClients[chainId] === undefined) {
232
+ return [2, "continue"];
233
+ }
234
+ chainIndex = chainIds.indexOf(chainId);
235
+ return [4, (0, utils_1.filterAsync)(this_1.spokePoolClients[chainId].getFills(), function (fill) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
236
+ var matchingDeposit, hasMatchingDeposit, validRepayment;
237
+ return tslib_1.__generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0:
240
+ if (fill.blockNumber < blockRanges[chainIndex][0] ||
241
+ fill.blockNumber > blockRanges[chainIndex][1] ||
242
+ (0, utils_1.isZeroValueFillOrSlowFillRequest)(fill)) {
243
+ return [2, false];
244
+ }
245
+ if (this.spokePoolClients[fill.originChainId] === undefined) {
246
+ return [2, false];
247
+ }
248
+ matchingDeposit = this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
249
+ hasMatchingDeposit = matchingDeposit !== undefined &&
250
+ this.getRelayHashFromEvent(fill) === this.getRelayHashFromEvent(matchingDeposit);
251
+ if (!hasMatchingDeposit) return [3, 2];
252
+ return [4, (0, utils_2.verifyFillRepayment)(fill, this.spokePoolClients[fill.destinationChainId].spokePool.provider, matchingDeposit, this.clients.configStoreClient
253
+ .getChainIdIndicesForBlock(blockRanges[0][1])
254
+ .filter(function (_chainId, i) { return !(0, utils_2.isChainDisabled)(blockRanges[i]); }))];
255
+ case 1:
256
+ validRepayment = _a.sent();
257
+ if (!(0, utils_1.isDefined)(validRepayment)) {
258
+ return [2, false];
259
+ }
260
+ _a.label = 2;
261
+ case 2: return [2, hasMatchingDeposit];
262
+ }
263
+ });
264
+ }); })];
265
+ case 1:
266
+ fillsToCount = _b.sent();
267
+ fillsToCount.forEach(function (fill) {
268
+ var _a, _b, _c;
269
+ var _d;
270
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
271
+ (0, utils_1.assert)((0, utils_1.isDefined)(matchingDeposit), "Deposit not found for fill.");
272
+ var _e = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
273
+ var relayer = fill.relayer, refundAmount = fill.inputAmount;
274
+ (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
275
+ (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
276
+ var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : utils_1.bnZero;
277
+ refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
278
+ });
279
+ return [2];
280
+ }
281
+ });
282
+ };
283
+ this_1 = this;
284
+ _i = 0, chainIds_1 = chainIds;
285
+ _a.label = 1;
286
+ case 1:
287
+ if (!(_i < chainIds_1.length)) return [3, 4];
288
+ chainId = chainIds_1[_i];
289
+ return [5, _loop_1(chainId)];
290
+ case 2:
291
+ _a.sent();
292
+ _a.label = 3;
293
+ case 3:
294
+ _i++;
295
+ return [3, 1];
296
+ case 4: return [2, refundsForChain];
231
297
  }
232
- var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
233
- var hasMatchingDeposit = matchingDeposit !== undefined &&
234
- _this.getRelayHashFromEvent(fill) === _this.getRelayHashFromEvent(matchingDeposit);
235
- return hasMatchingDeposit;
236
- })
237
- .forEach(function (fill) {
238
- var _a, _b, _c;
239
- var _d;
240
- var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
241
- (0, utils_1.assert)((0, utils_1.isDefined)(matchingDeposit), "Deposit not found for fill.");
242
- var _e = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
243
- var relayer = fill.relayer, refundAmount = fill.inputAmount;
244
- (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
245
- (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
246
- var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : utils_1.bnZero;
247
- refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
248
298
  });
249
- };
250
- var this_1 = this;
251
- for (var _i = 0, chainIds_1 = chainIds; _i < chainIds_1.length; _i++) {
252
- var chainId = chainIds_1[_i];
253
- _loop_1(chainId);
254
- }
255
- return refundsForChain;
299
+ });
256
300
  };
257
301
  BundleDataClient.prototype.getUpcomingDepositAmount = function (chainId, l2Token, latestBlockToSearch) {
258
302
  if (this.spokePoolClients[chainId] === undefined) {
@@ -295,12 +339,10 @@ var BundleDataClient = (function () {
295
339
  case 1:
296
340
  _a = _b.sent(), bundleData = _a.bundleData, blockRanges = _a.blockRanges;
297
341
  hubPoolClient = this.clients.hubPoolClient;
298
- return [4, (0, utils_2._buildPoolRebalanceRoot)(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
299
- hubPoolClient: hubPoolClient,
300
- configStoreClient: hubPoolClient.configStoreClient,
301
- })];
302
- case 2:
303
- root = _b.sent();
342
+ root = (0, utils_2._buildPoolRebalanceRoot)(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
343
+ hubPoolClient: hubPoolClient,
344
+ configStoreClient: hubPoolClient.configStoreClient,
345
+ });
304
346
  return [2, {
305
347
  root: root,
306
348
  blockRanges: blockRanges,
@@ -324,32 +366,39 @@ var BundleDataClient = (function () {
324
366
  return [initialBlockRange[1] + 1, blockRange[1]];
325
367
  });
326
368
  }
327
- var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, bundleFillsV3, expiredDepositsToRefundV3, start;
328
- return tslib_1.__generator(this, function (_a) {
329
- switch (_a.label) {
369
+ var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, _a, _b, bundleFillsV3, expiredDepositsToRefundV3, start, _c, _d;
370
+ return tslib_1.__generator(this, function (_e) {
371
+ switch (_e.label) {
330
372
  case 0:
331
373
  hubPoolClient = this.clients.hubPoolClient;
332
374
  nextBundleMainnetStartBlock = hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, hubPoolClient.latestBlockSearched, hubPoolClient.chainId);
333
375
  chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
334
376
  combinedRefunds = [];
335
377
  widestBundleBlockRanges = (0, utils_2.getWidestPossibleExpectedBlockRange)(chainIds, this.spokePoolClients, (0, utils_2.getEndBlockBuffers)(chainIds, this.blockRangeEndBlockBuffer), this.clients, this.clients.hubPoolClient.latestBlockSearched, this.clients.configStoreClient.getEnabledChains(this.clients.hubPoolClient.latestBlockSearched));
336
- if (!hubPoolClient.hasPendingProposal()) return [3, 2];
378
+ if (!hubPoolClient.hasPendingProposal()) return [3, 5];
337
379
  pendingBundleBlockRanges = (0, utils_1.getImpliedBundleBlockRanges)(hubPoolClient, this.clients.configStoreClient, hubPoolClient.getLatestProposedRootBundle());
338
380
  return [4, this.loadArweaveData(pendingBundleBlockRanges)];
339
381
  case 1:
340
- arweaveData = _a.sent();
341
- if (arweaveData === undefined) {
342
- combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges));
343
- }
344
- else {
345
- bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
346
- combinedRefunds.push((0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3));
347
- }
348
- widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
349
- _a.label = 2;
382
+ arweaveData = _e.sent();
383
+ if (!(arweaveData === undefined)) return [3, 3];
384
+ _b = (_a = combinedRefunds).push;
385
+ return [4, this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges)];
350
386
  case 2:
387
+ _b.apply(_a, [_e.sent()]);
388
+ return [3, 4];
389
+ case 3:
390
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
391
+ combinedRefunds.push((0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3));
392
+ _e.label = 4;
393
+ case 4:
394
+ widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
395
+ _e.label = 5;
396
+ case 5:
351
397
  start = performance.now();
352
- combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges));
398
+ _d = (_c = combinedRefunds).push;
399
+ return [4, this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges)];
400
+ case 6:
401
+ _d.apply(_c, [_e.sent()]);
353
402
  this.logger.debug({
354
403
  at: "BundleDataClient#getNextBundleRefunds",
355
404
  message: "Loading approximate refunds for next bundle in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -480,7 +529,7 @@ var BundleDataClient = (function () {
480
529
  };
481
530
  BundleDataClient.prototype.loadDataFromScratch = function (blockRangesForChains, spokePoolClients) {
482
531
  return tslib_1.__awaiter(this, void 0, void 0, function () {
483
- 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;
532
+ var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleUnrepayableFillsV3, bundleInvalidSlowFillRequests, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, _canCreateSlowFillLeaf, _depositIsExpired, _getFillStatusForDeposit, allChainIds, _cachedBundleTimestamps, bundleBlockTimestamps, v3RelayHashes, bundleDepositHashes, olderDepositHashes, decodeBundleDepositHash, startBlockForMainnet, versionAtProposalBlock, canRefundPrefills, _loop_2, _i, allChainIds_1, originChainId, validatedBundleV3Fills, validatedBundleSlowFills, validatedBundleUnexecutableSlowFills, fillCounter, _loop_3, _a, allChainIds_2, originChainId, promises, _b, v3FillLpFees, v3SlowFillLpFees, v3UnexecutableSlowFillLpFees, v3SpokeEventsReadable;
484
533
  var _this = this;
485
534
  return tslib_1.__generator(this, function (_c) {
486
535
  switch (_c.label) {
@@ -500,6 +549,8 @@ var BundleDataClient = (function () {
500
549
  bundleDepositsV3 = {};
501
550
  bundleFillsV3 = {};
502
551
  bundleInvalidFillsV3 = [];
552
+ bundleUnrepayableFillsV3 = [];
553
+ bundleInvalidSlowFillRequests = [];
503
554
  bundleSlowFillsV3 = {};
504
555
  expiredDepositsToRefundV3 = {};
505
556
  unexecutableSlowFills = {};
@@ -548,10 +599,13 @@ var BundleDataClient = (function () {
548
599
  v3RelayHashes = {};
549
600
  bundleDepositHashes = [];
550
601
  olderDepositHashes = [];
602
+ decodeBundleDepositHash = function (depositHash) {
603
+ var _a = depositHash.split("@"), relayDataHash = _a[0], i = _a[1];
604
+ return { relayDataHash: relayDataHash, index: Number(i) };
605
+ };
551
606
  startBlockForMainnet = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, this.clients.hubPoolClient.chainId, this.chainIdListForBundleEvaluationBlockNumbers)[0];
552
607
  versionAtProposalBlock = this.clients.configStoreClient.getConfigStoreVersionForBlock(startBlockForMainnet);
553
608
  canRefundPrefills = versionAtProposalBlock >= constants_1.PRE_FILL_MIN_CONFIG_STORE_VERSION || process.env.FORCE_REFUND_PREFILLS === "true";
554
- depositCounter = 0;
555
609
  _loop_2 = function (originChainId) {
556
610
  var originClient = spokePoolClients[originChainId];
557
611
  var originChainBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, originChainId, chainIds);
@@ -564,24 +618,27 @@ var BundleDataClient = (function () {
564
618
  if (deposit.blockNumber > originChainBlockRange[1] || (0, utils_1.isZeroValueDeposit)(deposit)) {
565
619
  return;
566
620
  }
567
- depositCounter++;
568
621
  var relayDataHash = _this.getRelayHashFromEvent(deposit);
569
622
  if (!v3RelayHashes[relayDataHash]) {
570
623
  v3RelayHashes[relayDataHash] = {
571
- deposit: deposit,
624
+ deposits: [deposit],
572
625
  fill: undefined,
573
626
  slowFillRequest: undefined,
574
627
  };
575
628
  }
576
- if (deposit.inputAmount.eq(0)) {
577
- return;
629
+ else {
630
+ v3RelayHashes[relayDataHash].deposits.push(deposit);
578
631
  }
632
+ var newBundleDepositHash = "".concat(relayDataHash, "@").concat(v3RelayHashes[relayDataHash].deposits.length - 1);
633
+ var decodedBundleDepositHash = decodeBundleDepositHash(newBundleDepositHash);
634
+ (0, utils_1.assert)(decodedBundleDepositHash.relayDataHash === relayDataHash &&
635
+ decodedBundleDepositHash.index === v3RelayHashes[relayDataHash].deposits.length - 1, "Not using correct bundle deposit hash key");
579
636
  if (deposit.blockNumber >= originChainBlockRange[0]) {
580
- bundleDepositHashes.push(relayDataHash);
637
+ bundleDepositHashes.push(newBundleDepositHash);
581
638
  updateBundleDepositsV3(bundleDepositsV3, deposit);
582
639
  }
583
640
  else if (deposit.blockNumber < originChainBlockRange[0]) {
584
- olderDepositHashes.push(relayDataHash);
641
+ olderDepositHashes.push(newBundleDepositHash);
585
642
  }
586
643
  });
587
644
  }
@@ -592,7 +649,7 @@ var BundleDataClient = (function () {
592
649
  }
593
650
  this.logger.debug({
594
651
  at: "BundleDataClient#loadData",
595
- message: "Processed ".concat(depositCounter, " deposits in ").concat(performance.now() - start, "ms."),
652
+ message: "Processed ".concat(bundleDepositHashes.length + olderDepositHashes.length, " deposits in ").concat(performance.now() - start, "ms."),
596
653
  });
597
654
  start = performance.now();
598
655
  validatedBundleV3Fills = [];
@@ -606,7 +663,7 @@ var BundleDataClient = (function () {
606
663
  case 0:
607
664
  originClient = spokePoolClients[originChainId];
608
665
  _loop_4 = function (destinationChainId) {
609
- var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills;
666
+ var destinationClient, destinationChainBlockRange, originChainBlockRange, fastFillsReplacingSlowFills;
610
667
  return tslib_1.__generator(this, function (_g) {
611
668
  switch (_g.label) {
612
669
  case 0:
@@ -615,69 +672,90 @@ var BundleDataClient = (function () {
615
672
  }
616
673
  destinationClient = spokePoolClients[destinationChainId];
617
674
  destinationChainBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
675
+ originChainBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, originChainId, chainIds);
618
676
  fastFillsReplacingSlowFills = [];
619
677
  return [4, (0, utils_1.forEachAsync)(destinationClient
620
678
  .getFillsForOriginChain(originChainId)
621
679
  .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1] && !(0, utils_1.isZeroValueFillOrSlowFillRequest)(fill); }), function (fill) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
622
- var relayDataHash, fillTransaction, originRelayer, historicalDeposit, matchedDeposit;
680
+ var relayDataHash, fillToRefund_1, duplicateDeposits, historicalDeposit, matchedDeposit, fillToRefund;
623
681
  return tslib_1.__generator(this, function (_a) {
624
682
  switch (_a.label) {
625
683
  case 0:
626
- relayDataHash = this.getRelayHashFromEvent(fill);
627
684
  fillCounter++;
628
- if (v3RelayHashes[relayDataHash]) {
629
- if (!v3RelayHashes[relayDataHash].fill) {
630
- (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
631
- v3RelayHashes[relayDataHash].fill = fill;
632
- if (fill.blockNumber >= destinationChainBlockRange[0]) {
633
- validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fill), { quoteTimestamp: v3RelayHashes[relayDataHash].deposit.quoteTimestamp }));
634
- if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill &&
635
- _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposit)) {
636
- fastFillsReplacingSlowFills.push(relayDataHash);
637
- }
685
+ relayDataHash = this.getRelayHashFromEvent(fill);
686
+ if (!v3RelayHashes[relayDataHash]) return [3, 5];
687
+ if (!!v3RelayHashes[relayDataHash].fill) return [3, 3];
688
+ (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
689
+ v3RelayHashes[relayDataHash].fill = fill;
690
+ if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3, 2];
691
+ return [4, (0, utils_2.verifyFillRepayment)(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
692
+ case 1:
693
+ fillToRefund_1 = _a.sent();
694
+ if (!(0, utils_1.isDefined)(fillToRefund_1)) {
695
+ bundleUnrepayableFillsV3.push(fill);
696
+ }
697
+ else {
698
+ v3RelayHashes[relayDataHash].fill = fillToRefund_1;
699
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fillToRefund_1), { quoteTimestamp: v3RelayHashes[relayDataHash].deposits[0].quoteTimestamp }));
700
+ duplicateDeposits = v3RelayHashes[relayDataHash].deposits.slice(1);
701
+ duplicateDeposits.forEach(function (duplicateDeposit) {
702
+ if ((0, utils_1.isSlowFill)(fill)) {
703
+ updateExpiredDepositsV3(expiredDepositsToRefundV3, duplicateDeposit);
638
704
  }
639
- }
640
- return [2];
705
+ else {
706
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fillToRefund_1), { quoteTimestamp: duplicateDeposit.quoteTimestamp }));
707
+ }
708
+ });
641
709
  }
710
+ if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill &&
711
+ _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposits[0])) {
712
+ fastFillsReplacingSlowFills.push(relayDataHash);
713
+ }
714
+ _a.label = 2;
715
+ case 2: return [3, 4];
716
+ case 3: throw new Error("Duplicate fill detected");
717
+ case 4: return [2];
718
+ case 5:
642
719
  v3RelayHashes[relayDataHash] = {
643
- deposit: undefined,
720
+ deposits: undefined,
644
721
  fill: fill,
645
722
  slowFillRequest: undefined,
646
723
  };
647
- if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3, 4];
724
+ if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3, 9];
648
725
  if (!exports.INFINITE_FILL_DEADLINE.eq(fill.fillDeadline)) {
649
726
  bundleInvalidFillsV3.push(fill);
650
727
  return [2];
651
728
  }
652
- if (!((0, utils_1.chainIsEvm)(fill.repaymentChainId) && !(0, utils_1.isValidEvmAddress)(fill.relayer))) return [3, 2];
653
- return [4, originClient.spokePool.provider.getTransaction(fill.transactionHash)];
654
- case 1:
655
- fillTransaction = _a.sent();
656
- originRelayer = fillTransaction.from;
657
- if (!(0, utils_1.isValidEvmAddress)(originRelayer)) {
729
+ return [4, (0, utils_1.queryHistoricalDepositForFill)(originClient, fill)];
730
+ case 6:
731
+ historicalDeposit = _a.sent();
732
+ if (!!historicalDeposit.found) return [3, 7];
733
+ bundleInvalidFillsV3.push(fill);
734
+ return [3, 9];
735
+ case 7:
736
+ matchedDeposit = historicalDeposit.deposit;
737
+ if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
658
738
  bundleInvalidFillsV3.push(fill);
659
739
  return [2];
660
740
  }
661
- fill.relayer = originRelayer;
662
- _a.label = 2;
663
- case 2: return [4, (0, utils_1.queryHistoricalDepositForFill)(originClient, fill)];
664
- case 3:
665
- historicalDeposit = _a.sent();
666
- if (!historicalDeposit.found) {
667
- bundleInvalidFillsV3.push(fill);
741
+ v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
742
+ return [4, (0, utils_2.verifyFillRepayment)(fill, destinationClient.spokePool.provider, matchedDeposit, allChainIds)];
743
+ case 8:
744
+ fillToRefund = _a.sent();
745
+ if (!(0, utils_1.isDefined)(fillToRefund)) {
746
+ bundleUnrepayableFillsV3.push(fill);
668
747
  }
669
748
  else {
670
- matchedDeposit = historicalDeposit.deposit;
671
749
  (0, utils_1.assert)(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
672
- validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fill), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
673
- v3RelayHashes[relayDataHash].deposit = matchedDeposit;
674
- if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill &&
675
- _canCreateSlowFillLeaf(matchedDeposit)) {
676
- fastFillsReplacingSlowFills.push(relayDataHash);
677
- }
750
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fillToRefund), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
751
+ v3RelayHashes[relayDataHash].fill = fillToRefund;
678
752
  }
679
- _a.label = 4;
680
- case 4: return [2];
753
+ if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill &&
754
+ _canCreateSlowFillLeaf(matchedDeposit)) {
755
+ fastFillsReplacingSlowFills.push(relayDataHash);
756
+ }
757
+ _a.label = 9;
758
+ case 9: return [2];
681
759
  }
682
760
  });
683
761
  }); })];
@@ -699,34 +777,44 @@ var BundleDataClient = (function () {
699
777
  if (v3RelayHashes[relayDataHash].fill) {
700
778
  return [2];
701
779
  }
702
- (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
703
- matchedDeposit = v3RelayHashes[relayDataHash].deposit;
780
+ (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits.length > 0, "Deposit should exist in relay hash dictionary.");
781
+ matchedDeposit = v3RelayHashes[relayDataHash].deposits[0];
704
782
  if (slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
705
783
  _canCreateSlowFillLeaf(matchedDeposit) &&
706
784
  !_depositIsExpired(matchedDeposit)) {
707
785
  validatedBundleSlowFills.push(matchedDeposit);
708
786
  }
709
787
  }
788
+ else {
789
+ throw new Error("Duplicate slow fill request detected.");
790
+ }
710
791
  return [2];
711
792
  }
712
793
  v3RelayHashes[relayDataHash] = {
713
- deposit: undefined,
794
+ deposits: undefined,
714
795
  fill: undefined,
715
796
  slowFillRequest: slowFillRequest,
716
797
  };
717
- if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
718
- exports.INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline))) return [3, 2];
798
+ if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0])) return [3, 2];
799
+ if (!exports.INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline)) {
800
+ bundleInvalidSlowFillRequests.push(slowFillRequest);
801
+ return [2];
802
+ }
719
803
  return [4, (0, utils_1.queryHistoricalDepositForFill)(originClient, slowFillRequest)];
720
804
  case 1:
721
805
  historicalDeposit = _a.sent();
722
806
  if (!historicalDeposit.found) {
807
+ bundleInvalidSlowFillRequests.push(slowFillRequest);
723
808
  return [2];
724
809
  }
725
810
  matchedDeposit = historicalDeposit.deposit;
811
+ if (matchedDeposit.blockNumber > originChainBlockRange[1]) {
812
+ bundleInvalidSlowFillRequests.push(slowFillRequest);
813
+ return [2];
814
+ }
726
815
  (0, utils_1.assert)(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Deposit relay hashes should match.");
727
- v3RelayHashes[relayDataHash].deposit = matchedDeposit;
728
- if (!_canCreateSlowFillLeaf(matchedDeposit) ||
729
- _depositIsExpired(matchedDeposit)) {
816
+ v3RelayHashes[relayDataHash].deposits = [matchedDeposit];
817
+ if (!_canCreateSlowFillLeaf(matchedDeposit) || _depositIsExpired(matchedDeposit)) {
730
818
  return [2];
731
819
  }
732
820
  validatedBundleSlowFills.push(matchedDeposit);
@@ -737,72 +825,105 @@ var BundleDataClient = (function () {
737
825
  }); })];
738
826
  case 2:
739
827
  _g.sent();
740
- return [4, (0, utils_1.mapAsync)(bundleDepositHashes.filter(function (depositHash) {
741
- var deposit = v3RelayHashes[depositHash].deposit;
742
- return (deposit && deposit.originChainId === originChainId && deposit.destinationChainId === destinationChainId);
743
- }), function (depositHash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
744
- var _a, deposit, fill, slowFillRequest, fillStatus, prefill;
745
- return tslib_1.__generator(this, function (_b) {
746
- switch (_b.label) {
828
+ return [4, (0, utils_1.mapAsync)(bundleDepositHashes, function (depositHash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
829
+ var _a, relayDataHash, index, _b, deposits, fill, slowFillRequest, deposit, fillToRefund, fillStatus, prefill, verifiedFill;
830
+ var _this = this;
831
+ return tslib_1.__generator(this, function (_c) {
832
+ switch (_c.label) {
747
833
  case 0:
748
- _a = v3RelayHashes[depositHash], deposit = _a.deposit, fill = _a.fill, slowFillRequest = _a.slowFillRequest;
834
+ _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
835
+ _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, fill = _b.fill, slowFillRequest = _b.slowFillRequest;
836
+ if (!deposits || deposits.length === 0) {
837
+ throw new Error("Deposits should exist in relay hash dictionary.");
838
+ }
839
+ deposit = deposits[index];
749
840
  if (!deposit)
750
841
  throw new Error("Deposit should exist in relay hash dictionary.");
751
- if (fill) {
752
- if (canRefundPrefills && fill.blockNumber < destinationChainBlockRange[0] && !(0, utils_1.isSlowFill)(fill)) {
753
- validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fill), { quoteTimestamp: deposit.quoteTimestamp }));
754
- }
842
+ if (deposit.originChainId !== originChainId || deposit.destinationChainId !== destinationChainId) {
755
843
  return [2];
756
844
  }
845
+ if (!fill) return [3, 3];
846
+ if (!(canRefundPrefills && fill.blockNumber < destinationChainBlockRange[0])) return [3, 2];
847
+ return [4, (0, utils_2.verifyFillRepayment)(fill, destinationClient.spokePool.provider, v3RelayHashes[relayDataHash].deposits[0], allChainIds)];
848
+ case 1:
849
+ fillToRefund = _c.sent();
850
+ if (!(0, utils_1.isDefined)(fillToRefund)) {
851
+ bundleUnrepayableFillsV3.push(fill);
852
+ }
853
+ else if (!(0, utils_1.isSlowFill)(fill)) {
854
+ v3RelayHashes[relayDataHash].fill = fillToRefund;
855
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fillToRefund), { quoteTimestamp: deposit.quoteTimestamp }));
856
+ }
857
+ else {
858
+ updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
859
+ }
860
+ _c.label = 2;
861
+ case 2: return [2];
862
+ case 3:
757
863
  if (slowFillRequest) {
758
864
  if (_depositIsExpired(deposit)) {
759
865
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
760
866
  }
761
867
  else if (canRefundPrefills &&
762
868
  slowFillRequest.blockNumber < destinationChainBlockRange[0] &&
763
- _canCreateSlowFillLeaf(deposit)) {
869
+ _canCreateSlowFillLeaf(deposit) &&
870
+ validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
764
871
  validatedBundleSlowFills.push(deposit);
765
872
  }
766
873
  return [2];
767
874
  }
768
875
  return [4, _getFillStatusForDeposit(deposit, destinationChainBlockRange[1])];
769
- case 1:
770
- fillStatus = _b.sent();
771
- if (!(fillStatus === interfaces_1.FillStatus.Filled)) return [3, 3];
772
- return [4, (0, utils_1.findFillEvent)(destinationClient.spokePool, deposit, destinationClient.deploymentBlock, destinationClient.latestBlockSearched)];
773
- case 2:
774
- prefill = (_b.sent());
775
- if (canRefundPrefills && !(0, utils_1.isSlowFill)(prefill)) {
776
- validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, prefill), { quoteTimestamp: deposit.quoteTimestamp }));
876
+ case 4:
877
+ fillStatus = _c.sent();
878
+ if (!(fillStatus === interfaces_1.FillStatus.Filled)) return [3, 8];
879
+ return [4, this.findMatchingFillEvent(deposit, destinationClient)];
880
+ case 5:
881
+ prefill = _c.sent();
882
+ (0, utils_1.assert)((0, utils_1.isDefined)(prefill), "findFillEvent# Cannot find prefill: ".concat(relayDataHash));
883
+ (0, utils_1.assert)(this.getRelayHashFromEvent(prefill) === relayDataHash, "Relay hashes should match.");
884
+ if (!canRefundPrefills) return [3, 7];
885
+ return [4, (0, utils_2.verifyFillRepayment)(prefill, destinationClient.spokePool.provider, deposit, allChainIds)];
886
+ case 6:
887
+ verifiedFill = _c.sent();
888
+ if (!(0, utils_1.isDefined)(verifiedFill)) {
889
+ bundleUnrepayableFillsV3.push(prefill);
777
890
  }
778
- return [3, 4];
779
- case 3:
891
+ else if (!(0, utils_1.isSlowFill)(verifiedFill)) {
892
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, verifiedFill), { quoteTimestamp: deposit.quoteTimestamp }));
893
+ }
894
+ else {
895
+ updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
896
+ }
897
+ _c.label = 7;
898
+ case 7: return [3, 9];
899
+ case 8:
780
900
  if (_depositIsExpired(deposit)) {
781
901
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
782
902
  }
783
- else if (fillStatus === interfaces_1.FillStatus.RequestedSlowFill) {
903
+ else if (fillStatus === interfaces_1.FillStatus.RequestedSlowFill &&
904
+ validatedBundleSlowFills.every(function (d) { return _this.getRelayHashFromEvent(d) !== relayDataHash; })) {
784
905
  if (canRefundPrefills && _canCreateSlowFillLeaf(deposit)) {
785
906
  validatedBundleSlowFills.push(deposit);
786
907
  }
787
908
  }
788
- _b.label = 4;
789
- case 4: return [2];
909
+ _c.label = 9;
910
+ case 9: return [2];
790
911
  }
791
912
  });
792
913
  }); })];
793
914
  case 3:
794
915
  _g.sent();
795
916
  fastFillsReplacingSlowFills.forEach(function (relayDataHash) {
796
- var _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
917
+ var _a = v3RelayHashes[relayDataHash], deposits = _a.deposits, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
797
918
  (0, utils_1.assert)((fill === null || fill === void 0 ? void 0 : fill.relayExecutionInfo.fillType) === interfaces_1.FillType.ReplacedSlowFill, "Fill type should be ReplacedSlowFill.");
798
- if (!deposit) {
919
+ if (!deposits || deposits.length < 1) {
799
920
  throw new Error("Deposit should exist in relay hash dictionary.");
800
921
  }
801
- (0, utils_1.assert)(_canCreateSlowFillLeaf(deposit), "fastFillsReplacingSlowFills should contain only deposits that can be slow filled");
922
+ (0, utils_1.assert)(_canCreateSlowFillLeaf(deposits[0]), "fastFillsReplacingSlowFills should contain only deposits that can be slow filled");
802
923
  var destinationBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
803
924
  if (!slowFillRequest ||
804
925
  slowFillRequest.blockNumber < destinationBlockRange[0]) {
805
- validatedBundleUnexecutableSlowFills.push(deposit);
926
+ validatedBundleUnexecutableSlowFills.push(deposits[0]);
806
927
  }
807
928
  });
808
929
  return [2];
@@ -843,13 +964,17 @@ var BundleDataClient = (function () {
843
964
  message: "Processed ".concat(fillCounter, " fills in ").concat(performance.now() - start, "ms."),
844
965
  });
845
966
  start = performance.now();
846
- return [4, (0, utils_1.forEachAsync)(olderDepositHashes, function (relayDataHash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
847
- var _a, deposit, slowFillRequest, fill, destinationChainId, destinationBlockRange, fillStatus;
848
- return tslib_1.__generator(this, function (_b) {
849
- switch (_b.label) {
967
+ return [4, (0, utils_1.forEachAsync)(olderDepositHashes, function (depositHash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
968
+ var _a, relayDataHash, index, _b, deposits, slowFillRequest, fill, deposit, destinationChainId, destinationBlockRange, fillStatus;
969
+ return tslib_1.__generator(this, function (_c) {
970
+ switch (_c.label) {
850
971
  case 0:
851
- _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
852
- (0, utils_1.assert)((0, utils_1.isDefined)(deposit), "Deposit should exist in relay hash dictionary.");
972
+ _a = decodeBundleDepositHash(depositHash), relayDataHash = _a.relayDataHash, index = _a.index;
973
+ _b = v3RelayHashes[relayDataHash], deposits = _b.deposits, slowFillRequest = _b.slowFillRequest, fill = _b.fill;
974
+ if (!deposits || deposits.length < 1) {
975
+ throw new Error("Deposit should exist in relay hash dictionary.");
976
+ }
977
+ deposit = deposits[index];
853
978
  destinationChainId = deposit.destinationChainId;
854
979
  destinationBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
855
980
  if (!(!fill &&
@@ -859,7 +984,7 @@ var BundleDataClient = (function () {
859
984
  spokePoolClients[destinationChainId] !== undefined)) return [3, 2];
860
985
  return [4, _getFillStatusForDeposit(deposit, destinationBlockRange[1])];
861
986
  case 1:
862
- fillStatus = _b.sent();
987
+ fillStatus = _c.sent();
863
988
  if (fillStatus !== interfaces_1.FillStatus.Filled) {
864
989
  updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
865
990
  }
@@ -869,7 +994,7 @@ var BundleDataClient = (function () {
869
994
  if (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0]) {
870
995
  validatedBundleUnexecutableSlowFills.push(deposit);
871
996
  }
872
- _b.label = 2;
997
+ _c.label = 2;
873
998
  case 2: return [2];
874
999
  }
875
1000
  });
@@ -880,7 +1005,7 @@ var BundleDataClient = (function () {
880
1005
  promises = [
881
1006
  validatedBundleV3Fills.length > 0
882
1007
  ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleV3Fills.map(function (fill) {
883
- var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1008
+ var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
884
1009
  (0, utils_1.assert)((0, utils_1.isDefined)(matchedDeposit), "Deposit should exist in relay hash dictionary.");
885
1010
  var paymentChainId = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, matchedDeposit.fromLiteChain).chainToSendRefundTo;
886
1011
  return tslib_1.__assign(tslib_1.__assign({}, fill), { paymentChainId: paymentChainId });
@@ -907,14 +1032,19 @@ var BundleDataClient = (function () {
907
1032
  v3FillLpFees.forEach(function (_a, idx) {
908
1033
  var realizedLpFeePct = _a.realizedLpFeePct;
909
1034
  var fill = validatedBundleV3Fills[idx];
910
- var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
1035
+ var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposits[0];
911
1036
  (0, utils_1.assert)((0, utils_1.isDefined)(associatedDeposit), "Deposit should exist in relay hash dictionary.");
912
1037
  var _b = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, associatedDeposit.fromLiteChain), chainToSendRefundTo = _b.chainToSendRefundTo, repaymentToken = _b.repaymentToken;
913
- updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken);
1038
+ updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken, fill.relayer);
914
1039
  });
915
1040
  v3SlowFillLpFees.forEach(function (_a, idx) {
916
1041
  var lpFeePct = _a.realizedLpFeePct;
917
1042
  var deposit = validatedBundleSlowFills[idx];
1043
+ var relayDataHash = _this.getRelayHashFromEvent(deposit);
1044
+ if (validatedBundleSlowFills.slice(0, idx).some(function (d) { return _this.getRelayHashFromEvent(d) === relayDataHash; })) {
1045
+ return;
1046
+ }
1047
+ (0, utils_1.assert)(!_depositIsExpired(deposit), "Cannot create slow fill leaf for expired deposit.");
918
1048
  updateBundleSlowFills(bundleSlowFillsV3, tslib_1.__assign(tslib_1.__assign({}, deposit), { lpFeePct: lpFeePct }));
919
1049
  });
920
1050
  v3UnexecutableSlowFillLpFees.forEach(function (_a, idx) {
@@ -922,15 +1052,31 @@ var BundleDataClient = (function () {
922
1052
  var deposit = validatedBundleUnexecutableSlowFills[idx];
923
1053
  updateBundleExcessSlowFills(unexecutableSlowFills, tslib_1.__assign(tslib_1.__assign({}, deposit), { lpFeePct: lpFeePct }));
924
1054
  });
925
- v3SpokeEventsReadable = (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
1055
+ v3SpokeEventsReadable = (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleDepositsV3, bundleFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
926
1056
  if (bundleInvalidFillsV3.length > 0) {
927
1057
  this.logger.debug({
928
1058
  at: "BundleDataClient#loadData",
929
- message: "Finished loading V3 spoke pool data and found some invalid V3 fills in range",
1059
+ message: "Finished loading V3 spoke pool data and found some invalid fills in range",
930
1060
  blockRangesForChains: blockRangesForChains,
931
1061
  bundleInvalidFillsV3: bundleInvalidFillsV3,
932
1062
  });
933
1063
  }
1064
+ if (bundleUnrepayableFillsV3.length > 0) {
1065
+ this.logger.debug({
1066
+ at: "BundleDataClient#loadData",
1067
+ message: "Finished loading V3 spoke pool data and found some unrepayable fills in range",
1068
+ blockRangesForChains: blockRangesForChains,
1069
+ bundleUnrepayableFillsV3: bundleUnrepayableFillsV3,
1070
+ });
1071
+ }
1072
+ if (bundleInvalidSlowFillRequests.length > 0) {
1073
+ this.logger.debug({
1074
+ at: "BundleDataClient#loadData",
1075
+ message: "Finished loading V3 spoke pool data and found some invalid slow fill requests in range",
1076
+ blockRangesForChains: blockRangesForChains,
1077
+ bundleInvalidSlowFillRequests: bundleInvalidSlowFillRequests,
1078
+ });
1079
+ }
934
1080
  this.logger.debug({
935
1081
  at: "BundleDataClient#loadDataFromScratch",
936
1082
  message: "Computed bundle data in ".concat(Math.round(performance.now() - start) / 1000, "s."),
@@ -951,6 +1097,16 @@ var BundleDataClient = (function () {
951
1097
  BundleDataClient.prototype.getRelayHashFromEvent = function (event) {
952
1098
  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);
953
1099
  };
1100
+ BundleDataClient.prototype.findMatchingFillEvent = function (deposit, spokePoolClient) {
1101
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1102
+ return tslib_1.__generator(this, function (_a) {
1103
+ switch (_a.label) {
1104
+ case 0: return [4, (0, utils_1.findFillEvent)(spokePoolClient.spokePool, deposit, spokePoolClient.deploymentBlock, spokePoolClient.latestBlockSearched)];
1105
+ case 1: return [2, _a.sent()];
1106
+ }
1107
+ });
1108
+ });
1109
+ };
954
1110
  BundleDataClient.prototype.getBundleBlockTimestamps = function (chainIds, blockRangesForChains, spokePoolClients) {
955
1111
  return tslib_1.__awaiter(this, void 0, void 0, function () {
956
1112
  var _a, _b;
@@ -960,7 +1116,7 @@ var BundleDataClient = (function () {
960
1116
  case 0:
961
1117
  _b = (_a = Object).fromEntries;
962
1118
  return [4, (0, utils_1.mapAsync)(chainIds, function (chainId, index) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
963
- var blockRangeForChain, _startBlockForChain, _endBlockForChain, spokePoolClient, startBlockForChain, endBlockForChain, _a, startTime, endTime, _b;
1119
+ var blockRangeForChain, _startBlockForChain, _endBlockForChain, spokePoolClient, startBlockForChain, endBlockForChain, _a, startTime, _endTime, _b, endBlockDelta, endTime;
964
1120
  return tslib_1.__generator(this, function (_c) {
965
1121
  switch (_c.label) {
966
1122
  case 0:
@@ -974,7 +1130,7 @@ var BundleDataClient = (function () {
974
1130
  return [2];
975
1131
  }
976
1132
  startBlockForChain = Math.min(_startBlockForChain, spokePoolClient.latestBlockSearched);
977
- endBlockForChain = Math.min(_endBlockForChain, spokePoolClient.latestBlockSearched);
1133
+ endBlockForChain = Math.min(_endBlockForChain + 1, spokePoolClient.latestBlockSearched);
978
1134
  return [4, spokePoolClient.getTimestampForBlock(startBlockForChain)];
979
1135
  case 1:
980
1136
  _b = [
@@ -984,8 +1140,10 @@ var BundleDataClient = (function () {
984
1140
  case 2:
985
1141
  _a = _b.concat([
986
1142
  _c.sent()
987
- ]), startTime = _a[0], endTime = _a[1];
988
- (0, utils_1.assert)(endTime >= startTime, "End time should be greater than start time.");
1143
+ ]), startTime = _a[0], _endTime = _a[1];
1144
+ endBlockDelta = endBlockForChain > startBlockForChain ? 1 : 0;
1145
+ endTime = Math.max(0, _endTime - endBlockDelta);
1146
+ (0, utils_1.assert)(endTime >= startTime, "End time for block ".concat(endBlockForChain, " should be greater than start time for block ").concat(startBlockForChain, ": ").concat(endTime, " >= ").concat(startTime, "."));
989
1147
  (0, utils_1.assert)(startBlockForChain === 0 || startTime > 0, "Start timestamp must be greater than 0 if the start block is greater than 0.");
990
1148
  return [2, [chainId, [startTime, endTime]]];
991
1149
  }