@across-protocol/sdk 3.1.26 → 3.1.28-beta.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 (181) hide show
  1. package/dist/cjs/clients/BundleDataClient/BundleDataClient.d.ts +59 -0
  2. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js +926 -0
  3. package/dist/cjs/clients/BundleDataClient/BundleDataClient.js.map +1 -0
  4. package/dist/cjs/clients/BundleDataClient/index.d.ts +2 -0
  5. package/dist/cjs/clients/BundleDataClient/index.js +6 -0
  6. package/dist/cjs/clients/BundleDataClient/index.js.map +1 -0
  7. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.d.ts +15 -0
  8. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js +131 -0
  9. package/dist/cjs/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -0
  10. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.d.ts +6 -0
  11. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js +19 -0
  12. package/dist/cjs/clients/BundleDataClient/utils/FillUtils.js.map +1 -0
  13. package/dist/cjs/clients/BundleDataClient/utils/MerkleTreeUtils.d.ts +3 -0
  14. package/dist/cjs/clients/BundleDataClient/utils/MerkleTreeUtils.js +23 -0
  15. package/dist/cjs/clients/BundleDataClient/utils/MerkleTreeUtils.js.map +1 -0
  16. package/dist/cjs/clients/BundleDataClient/utils/PoolRebalanceUtils.d.ts +24 -0
  17. package/dist/cjs/clients/BundleDataClient/utils/PoolRebalanceUtils.js +129 -0
  18. package/dist/cjs/clients/BundleDataClient/utils/PoolRebalanceUtils.js.map +1 -0
  19. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.d.ts +302 -0
  20. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.js +77 -0
  21. package/dist/cjs/clients/BundleDataClient/utils/SuperstructUtils.js.map +1 -0
  22. package/dist/cjs/clients/BundleDataClient/utils/index.d.ts +6 -0
  23. package/dist/cjs/clients/BundleDataClient/utils/index.js +10 -0
  24. package/dist/cjs/clients/BundleDataClient/utils/index.js.map +1 -0
  25. package/dist/cjs/clients/BundleDataClient/utils/shims.d.ts +8 -0
  26. package/dist/cjs/clients/BundleDataClient/utils/shims.js +3 -0
  27. package/dist/cjs/clients/BundleDataClient/utils/shims.js.map +1 -0
  28. package/dist/cjs/clients/index.d.ts +1 -0
  29. package/dist/cjs/clients/index.js +2 -1
  30. package/dist/cjs/clients/index.js.map +1 -1
  31. package/dist/cjs/interfaces/BundleData.d.ts +63 -0
  32. package/dist/cjs/interfaces/BundleData.js +3 -0
  33. package/dist/cjs/interfaces/BundleData.js.map +1 -0
  34. package/dist/cjs/interfaces/index.d.ts +1 -0
  35. package/dist/cjs/interfaces/index.js +1 -0
  36. package/dist/cjs/interfaces/index.js.map +1 -1
  37. package/dist/cjs/providers/alchemy.d.ts +2 -0
  38. package/dist/cjs/providers/alchemy.js +28 -0
  39. package/dist/cjs/providers/alchemy.js.map +1 -0
  40. package/dist/cjs/providers/index.d.ts +1 -0
  41. package/dist/cjs/providers/index.js +1 -0
  42. package/dist/cjs/providers/index.js.map +1 -1
  43. package/dist/cjs/providers/infura.d.ts +2 -0
  44. package/dist/cjs/providers/infura.js +23 -0
  45. package/dist/cjs/providers/infura.js.map +1 -0
  46. package/dist/cjs/providers/types.d.ts +2 -0
  47. package/dist/cjs/providers/types.js +3 -0
  48. package/dist/cjs/providers/types.js.map +1 -0
  49. package/dist/cjs/providers/utils.d.ts +3 -0
  50. package/dist/cjs/providers/utils.js +21 -2
  51. package/dist/cjs/providers/utils.js.map +1 -1
  52. package/dist/cjs/utils/AddressUtils.d.ts +1 -0
  53. package/dist/cjs/utils/AddressUtils.js +15 -1
  54. package/dist/cjs/utils/AddressUtils.js.map +1 -1
  55. package/dist/cjs/utils/ContractUtils.d.ts +1 -0
  56. package/dist/cjs/utils/ContractUtils.js +12 -0
  57. package/dist/cjs/utils/ContractUtils.js.map +1 -0
  58. package/dist/cjs/utils/ObjectUtils.d.ts +18 -0
  59. package/dist/cjs/utils/ObjectUtils.js +27 -1
  60. package/dist/cjs/utils/ObjectUtils.js.map +1 -1
  61. package/dist/esm/clients/BundleDataClient/BundleDataClient.d.ts +59 -0
  62. package/dist/esm/clients/BundleDataClient/BundleDataClient.js +1090 -0
  63. package/dist/esm/clients/BundleDataClient/BundleDataClient.js.map +1 -0
  64. package/dist/esm/clients/BundleDataClient/index.d.ts +2 -0
  65. package/dist/esm/clients/BundleDataClient/index.js +3 -0
  66. package/dist/esm/clients/BundleDataClient/index.js.map +1 -0
  67. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.d.ts +15 -0
  68. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js +182 -0
  69. package/dist/esm/clients/BundleDataClient/utils/DataworkerUtils.js.map +1 -0
  70. package/dist/esm/clients/BundleDataClient/utils/FillUtils.d.ts +6 -0
  71. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js +20 -0
  72. package/dist/esm/clients/BundleDataClient/utils/FillUtils.js.map +1 -0
  73. package/dist/esm/clients/BundleDataClient/utils/MerkleTreeUtils.d.ts +3 -0
  74. package/dist/esm/clients/BundleDataClient/utils/MerkleTreeUtils.js +20 -0
  75. package/dist/esm/clients/BundleDataClient/utils/MerkleTreeUtils.js.map +1 -0
  76. package/dist/esm/clients/BundleDataClient/utils/PoolRebalanceUtils.d.ts +24 -0
  77. package/dist/esm/clients/BundleDataClient/utils/PoolRebalanceUtils.js +157 -0
  78. package/dist/esm/clients/BundleDataClient/utils/PoolRebalanceUtils.js.map +1 -0
  79. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.d.ts +302 -0
  80. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.js +79 -0
  81. package/dist/esm/clients/BundleDataClient/utils/SuperstructUtils.js.map +1 -0
  82. package/dist/esm/clients/BundleDataClient/utils/index.d.ts +6 -0
  83. package/dist/esm/clients/BundleDataClient/utils/index.js +7 -0
  84. package/dist/esm/clients/BundleDataClient/utils/index.js.map +1 -0
  85. package/dist/esm/clients/BundleDataClient/utils/shims.d.ts +8 -0
  86. package/dist/esm/clients/BundleDataClient/utils/shims.js +2 -0
  87. package/dist/esm/clients/BundleDataClient/utils/shims.js.map +1 -0
  88. package/dist/esm/clients/index.d.ts +1 -0
  89. package/dist/esm/clients/index.js +2 -0
  90. package/dist/esm/clients/index.js.map +1 -1
  91. package/dist/esm/interfaces/BundleData.d.ts +63 -0
  92. package/dist/esm/interfaces/BundleData.js +2 -0
  93. package/dist/esm/interfaces/BundleData.js.map +1 -0
  94. package/dist/esm/interfaces/index.d.ts +1 -0
  95. package/dist/esm/interfaces/index.js +1 -0
  96. package/dist/esm/interfaces/index.js.map +1 -1
  97. package/dist/esm/providers/alchemy.d.ts +2 -0
  98. package/dist/esm/providers/alchemy.js +25 -0
  99. package/dist/esm/providers/alchemy.js.map +1 -0
  100. package/dist/esm/providers/index.d.ts +1 -0
  101. package/dist/esm/providers/index.js +1 -0
  102. package/dist/esm/providers/index.js.map +1 -1
  103. package/dist/esm/providers/infura.d.ts +2 -0
  104. package/dist/esm/providers/infura.js +20 -0
  105. package/dist/esm/providers/infura.js.map +1 -0
  106. package/dist/esm/providers/types.d.ts +2 -0
  107. package/dist/esm/providers/types.js +2 -0
  108. package/dist/esm/providers/types.js.map +1 -0
  109. package/dist/esm/providers/utils.d.ts +16 -0
  110. package/dist/esm/providers/utils.js +35 -2
  111. package/dist/esm/providers/utils.js.map +1 -1
  112. package/dist/esm/utils/AddressUtils.d.ts +1 -0
  113. package/dist/esm/utils/AddressUtils.js +16 -1
  114. package/dist/esm/utils/AddressUtils.js.map +1 -1
  115. package/dist/esm/utils/ContractUtils.d.ts +1 -0
  116. package/dist/esm/utils/ContractUtils.js +8 -0
  117. package/dist/esm/utils/ContractUtils.js.map +1 -0
  118. package/dist/esm/utils/ObjectUtils.d.ts +18 -0
  119. package/dist/esm/utils/ObjectUtils.js +24 -0
  120. package/dist/esm/utils/ObjectUtils.js.map +1 -1
  121. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts +60 -0
  122. package/dist/types/clients/BundleDataClient/BundleDataClient.d.ts.map +1 -0
  123. package/dist/types/clients/BundleDataClient/index.d.ts +3 -0
  124. package/dist/types/clients/BundleDataClient/index.d.ts.map +1 -0
  125. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts +16 -0
  126. package/dist/types/clients/BundleDataClient/utils/DataworkerUtils.d.ts.map +1 -0
  127. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts +7 -0
  128. package/dist/types/clients/BundleDataClient/utils/FillUtils.d.ts.map +1 -0
  129. package/dist/types/clients/BundleDataClient/utils/MerkleTreeUtils.d.ts +4 -0
  130. package/dist/types/clients/BundleDataClient/utils/MerkleTreeUtils.d.ts.map +1 -0
  131. package/dist/types/clients/BundleDataClient/utils/PoolRebalanceUtils.d.ts +25 -0
  132. package/dist/types/clients/BundleDataClient/utils/PoolRebalanceUtils.d.ts.map +1 -0
  133. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts +303 -0
  134. package/dist/types/clients/BundleDataClient/utils/SuperstructUtils.d.ts.map +1 -0
  135. package/dist/types/clients/BundleDataClient/utils/index.d.ts +7 -0
  136. package/dist/types/clients/BundleDataClient/utils/index.d.ts.map +1 -0
  137. package/dist/types/clients/BundleDataClient/utils/shims.d.ts +9 -0
  138. package/dist/types/clients/BundleDataClient/utils/shims.d.ts.map +1 -0
  139. package/dist/types/clients/index.d.ts +1 -0
  140. package/dist/types/clients/index.d.ts.map +1 -1
  141. package/dist/types/interfaces/BundleData.d.ts +64 -0
  142. package/dist/types/interfaces/BundleData.d.ts.map +1 -0
  143. package/dist/types/interfaces/index.d.ts +1 -0
  144. package/dist/types/interfaces/index.d.ts.map +1 -1
  145. package/dist/types/providers/alchemy.d.ts +3 -0
  146. package/dist/types/providers/alchemy.d.ts.map +1 -0
  147. package/dist/types/providers/index.d.ts +1 -0
  148. package/dist/types/providers/index.d.ts.map +1 -1
  149. package/dist/types/providers/infura.d.ts +3 -0
  150. package/dist/types/providers/infura.d.ts.map +1 -0
  151. package/dist/types/providers/types.d.ts +3 -0
  152. package/dist/types/providers/types.d.ts.map +1 -0
  153. package/dist/types/providers/utils.d.ts +16 -0
  154. package/dist/types/providers/utils.d.ts.map +1 -1
  155. package/dist/types/utils/AddressUtils.d.ts +1 -0
  156. package/dist/types/utils/AddressUtils.d.ts.map +1 -1
  157. package/dist/types/utils/ContractUtils.d.ts +2 -0
  158. package/dist/types/utils/ContractUtils.d.ts.map +1 -0
  159. package/dist/types/utils/ObjectUtils.d.ts +18 -0
  160. package/dist/types/utils/ObjectUtils.d.ts.map +1 -1
  161. package/package.json +1 -1
  162. package/src/clients/BundleDataClient/BundleDataClient.ts +1297 -0
  163. package/src/clients/BundleDataClient/index.ts +2 -0
  164. package/src/clients/BundleDataClient/utils/DataworkerUtils.ts +268 -0
  165. package/src/clients/BundleDataClient/utils/FillUtils.ts +46 -0
  166. package/src/clients/BundleDataClient/utils/MerkleTreeUtils.ts +26 -0
  167. package/src/clients/BundleDataClient/utils/PoolRebalanceUtils.ts +238 -0
  168. package/src/clients/BundleDataClient/utils/SuperstructUtils.ts +132 -0
  169. package/src/clients/BundleDataClient/utils/index.ts +6 -0
  170. package/src/clients/BundleDataClient/utils/shims.ts +10 -0
  171. package/src/clients/index.ts +1 -0
  172. package/src/interfaces/BundleData.ts +68 -0
  173. package/src/interfaces/index.ts +1 -0
  174. package/src/providers/alchemy.ts +28 -0
  175. package/src/providers/index.ts +1 -0
  176. package/src/providers/infura.ts +23 -0
  177. package/src/providers/types.ts +2 -0
  178. package/src/providers/utils.ts +42 -2
  179. package/src/utils/AddressUtils.ts +15 -1
  180. package/src/utils/ContractUtils.ts +8 -0
  181. package/src/utils/ObjectUtils.ts +23 -0
@@ -0,0 +1,926 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BundleDataClient = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var lodash_1 = tslib_1.__importDefault(require("lodash"));
6
+ var interfaces_1 = require("../../interfaces");
7
+ var utils_1 = require("../../utils");
8
+ var ethers_1 = require("ethers");
9
+ var utils_2 = require("./utils");
10
+ function updateExpiredDepositsV3(dict, deposit) {
11
+ var _a;
12
+ var originChainId = deposit.originChainId, inputToken = deposit.inputToken;
13
+ if (!((_a = dict === null || dict === void 0 ? void 0 : dict[originChainId]) === null || _a === void 0 ? void 0 : _a[inputToken])) {
14
+ (0, utils_1.assign)(dict, [originChainId, inputToken], []);
15
+ }
16
+ dict[originChainId][inputToken].push(deposit);
17
+ }
18
+ function updateBundleDepositsV3(dict, deposit) {
19
+ var _a;
20
+ var originChainId = deposit.originChainId, inputToken = deposit.inputToken;
21
+ if (!((_a = dict === null || dict === void 0 ? void 0 : dict[originChainId]) === null || _a === void 0 ? void 0 : _a[inputToken])) {
22
+ (0, utils_1.assign)(dict, [originChainId, inputToken], []);
23
+ }
24
+ dict[originChainId][inputToken].push(deposit);
25
+ }
26
+ function updateBundleFillsV3(dict, fill, lpFeePct, repaymentChainId, repaymentToken) {
27
+ var _a, _b;
28
+ if (!((_a = dict === null || dict === void 0 ? void 0 : dict[repaymentChainId]) === null || _a === void 0 ? void 0 : _a[repaymentToken])) {
29
+ (0, utils_1.assign)(dict, [repaymentChainId, repaymentToken], {
30
+ fills: [],
31
+ totalRefundAmount: utils_1.bnZero,
32
+ realizedLpFees: utils_1.bnZero,
33
+ refunds: {},
34
+ });
35
+ }
36
+ var bundleFill = tslib_1.__assign(tslib_1.__assign({}, fill), { lpFeePct: lpFeePct });
37
+ (0, utils_1.assign)(dict, [repaymentChainId, repaymentToken, "fills"], [bundleFill]);
38
+ var refundObj = dict[repaymentChainId][repaymentToken];
39
+ var realizedLpFee = fill.inputAmount.mul(bundleFill.lpFeePct).div(utils_1.fixedPointAdjustment);
40
+ refundObj.realizedLpFees = refundObj.realizedLpFees ? refundObj.realizedLpFees.add(realizedLpFee) : realizedLpFee;
41
+ if (!(0, utils_1.isSlowFill)(fill)) {
42
+ var refundAmount = fill.inputAmount.mul(utils_1.fixedPointAdjustment.sub(lpFeePct)).div(utils_1.fixedPointAdjustment);
43
+ refundObj.totalRefundAmount = refundObj.totalRefundAmount
44
+ ? refundObj.totalRefundAmount.add(refundAmount)
45
+ : refundAmount;
46
+ (_b = refundObj.refunds) !== null && _b !== void 0 ? _b : (refundObj.refunds = {});
47
+ if (refundObj.refunds[fill.relayer]) {
48
+ refundObj.refunds[fill.relayer] = refundObj.refunds[fill.relayer].add(refundAmount);
49
+ }
50
+ else {
51
+ refundObj.refunds[fill.relayer] = refundAmount;
52
+ }
53
+ }
54
+ }
55
+ function updateBundleExcessSlowFills(dict, deposit) {
56
+ var _a;
57
+ var destinationChainId = deposit.destinationChainId, outputToken = deposit.outputToken;
58
+ if (!((_a = dict === null || dict === void 0 ? void 0 : dict[destinationChainId]) === null || _a === void 0 ? void 0 : _a[outputToken])) {
59
+ (0, utils_1.assign)(dict, [destinationChainId, outputToken], []);
60
+ }
61
+ dict[destinationChainId][outputToken].push(deposit);
62
+ }
63
+ function updateBundleSlowFills(dict, deposit) {
64
+ var _a;
65
+ var destinationChainId = deposit.destinationChainId, outputToken = deposit.outputToken;
66
+ if (!((_a = dict === null || dict === void 0 ? void 0 : dict[destinationChainId]) === null || _a === void 0 ? void 0 : _a[outputToken])) {
67
+ (0, utils_1.assign)(dict, [destinationChainId, outputToken], []);
68
+ }
69
+ dict[destinationChainId][outputToken].push(deposit);
70
+ }
71
+ var BundleDataClient = (function () {
72
+ function BundleDataClient(logger, clients, spokePoolClients, chainIdListForBundleEvaluationBlockNumbers, blockRangeEndBlockBuffer) {
73
+ if (blockRangeEndBlockBuffer === void 0) { blockRangeEndBlockBuffer = {}; }
74
+ this.logger = logger;
75
+ this.clients = clients;
76
+ this.spokePoolClients = spokePoolClients;
77
+ this.chainIdListForBundleEvaluationBlockNumbers = chainIdListForBundleEvaluationBlockNumbers;
78
+ this.blockRangeEndBlockBuffer = blockRangeEndBlockBuffer;
79
+ this.loadDataCache = {};
80
+ this.arweaveDataCache = {};
81
+ this.bundleTimestampCache = {};
82
+ }
83
+ BundleDataClient.prototype.clearCache = function () {
84
+ this.loadDataCache = {};
85
+ };
86
+ BundleDataClient.prototype.loadDataFromCache = function (key) {
87
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
88
+ var _a, _b;
89
+ return tslib_1.__generator(this, function (_c) {
90
+ switch (_c.label) {
91
+ case 0:
92
+ _b = (_a = lodash_1.default).cloneDeep;
93
+ return [4, this.loadDataCache[key]];
94
+ case 1: return [2, _b.apply(_a, [_c.sent()])];
95
+ }
96
+ });
97
+ });
98
+ };
99
+ BundleDataClient.prototype.getBundleTimestampsFromCache = function (key) {
100
+ if (this.bundleTimestampCache[key]) {
101
+ return lodash_1.default.cloneDeep(this.bundleTimestampCache[key]);
102
+ }
103
+ return undefined;
104
+ };
105
+ BundleDataClient.prototype.setBundleTimestampsInCache = function (key, timestamps) {
106
+ this.bundleTimestampCache[key] = timestamps;
107
+ };
108
+ BundleDataClient.prototype.getArweaveClientKey = function (blockRangesForChains) {
109
+ return "bundles-".concat(blockRangesForChains);
110
+ };
111
+ BundleDataClient.prototype.loadPersistedDataFromArweave = function (blockRangesForChains) {
112
+ var _a;
113
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
114
+ var start, persistedData, convertTypedStringRecordIntoNumericRecord, data, bundleData;
115
+ return tslib_1.__generator(this, function (_b) {
116
+ switch (_b.label) {
117
+ case 0:
118
+ if (!(0, utils_1.isDefined)((_a = this.clients) === null || _a === void 0 ? void 0 : _a.arweaveClient)) {
119
+ return [2, undefined];
120
+ }
121
+ start = performance.now();
122
+ return [4, this.clients.arweaveClient.getByTopic(this.getArweaveClientKey(blockRangesForChains), utils_2.BundleDataSS)];
123
+ case 1:
124
+ persistedData = _b.sent();
125
+ if (!(0, utils_1.isDefined)(persistedData) || persistedData.length < 1) {
126
+ return [2, undefined];
127
+ }
128
+ convertTypedStringRecordIntoNumericRecord = function (data) {
129
+ return Object.keys(data).reduce(function (acc, chainId) {
130
+ acc[Number(chainId)] = data[chainId];
131
+ return acc;
132
+ }, {});
133
+ };
134
+ data = persistedData[0].data;
135
+ bundleData = {
136
+ bundleFillsV3: convertTypedStringRecordIntoNumericRecord(data.bundleFillsV3),
137
+ expiredDepositsToRefundV3: convertTypedStringRecordIntoNumericRecord(data.expiredDepositsToRefundV3),
138
+ bundleDepositsV3: convertTypedStringRecordIntoNumericRecord(data.bundleDepositsV3),
139
+ unexecutableSlowFills: convertTypedStringRecordIntoNumericRecord(data.unexecutableSlowFills),
140
+ bundleSlowFillsV3: convertTypedStringRecordIntoNumericRecord(data.bundleSlowFillsV3),
141
+ };
142
+ this.logger.debug({
143
+ at: "BundleDataClient#loadPersistedDataFromArweave",
144
+ message: "Loaded persisted data from Arweave in ".concat(Math.round(performance.now() - start) / 1000, "s."),
145
+ blockRanges: JSON.stringify(blockRangesForChains),
146
+ bundleData: (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleData.bundleDepositsV3, bundleData.bundleFillsV3, [], bundleData.bundleSlowFillsV3, bundleData.expiredDepositsToRefundV3, bundleData.unexecutableSlowFills),
147
+ });
148
+ return [2, bundleData];
149
+ }
150
+ });
151
+ });
152
+ };
153
+ BundleDataClient.prototype.getPendingRefundsFromValidBundles = function () {
154
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
155
+ var refunds, bundle, _a, _b;
156
+ return tslib_1.__generator(this, function (_c) {
157
+ switch (_c.label) {
158
+ case 0:
159
+ refunds = [];
160
+ if (!this.clients.hubPoolClient.isUpdated) {
161
+ throw new Error("BundleDataClient::getPendingRefundsFromValidBundles HubPoolClient not updated.");
162
+ }
163
+ bundle = this.clients.hubPoolClient.getLatestFullyExecutedRootBundle(this.clients.hubPoolClient.latestBlockSearched);
164
+ if (!(bundle !== undefined)) return [3, 2];
165
+ _b = (_a = refunds).push;
166
+ return [4, this.getPendingRefundsFromBundle(bundle)];
167
+ case 1:
168
+ _b.apply(_a, [_c.sent()]);
169
+ _c.label = 2;
170
+ case 2: return [2, refunds];
171
+ }
172
+ });
173
+ });
174
+ };
175
+ BundleDataClient.prototype.getPendingRefundsFromBundle = function (bundle) {
176
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
177
+ var nextBundleMainnetStartBlock, chainIds, bundleEvaluationBlockRanges, combinedRefunds, arweaveData, bundleFillsV3, expiredDepositsToRefundV3;
178
+ var _this = this;
179
+ return tslib_1.__generator(this, function (_a) {
180
+ switch (_a.label) {
181
+ case 0:
182
+ nextBundleMainnetStartBlock = this.clients.hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, this.clients.hubPoolClient.latestBlockSearched, this.clients.hubPoolClient.chainId);
183
+ chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
184
+ bundleEvaluationBlockRanges = (0, utils_1.getImpliedBundleBlockRanges)(this.clients.hubPoolClient, this.clients.configStoreClient, bundle);
185
+ return [4, this.loadArweaveData(bundleEvaluationBlockRanges)];
186
+ case 1:
187
+ arweaveData = _a.sent();
188
+ if (arweaveData === undefined) {
189
+ combinedRefunds = this.getApproximateRefundsForBlockRange(chainIds, bundleEvaluationBlockRanges);
190
+ }
191
+ else {
192
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
193
+ combinedRefunds = (0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3);
194
+ Object.keys(combinedRefunds).forEach(function (chainId) {
195
+ if (_this.spokePoolClients[Number(chainId)] === undefined) {
196
+ delete combinedRefunds[Number(chainId)];
197
+ }
198
+ });
199
+ }
200
+ return [2, this.deductExecutedRefunds(combinedRefunds, bundle)];
201
+ }
202
+ });
203
+ });
204
+ };
205
+ BundleDataClient.prototype.getApproximateRefundsForBlockRange = function (chainIds, blockRanges) {
206
+ var _this = this;
207
+ var refundsForChain = {};
208
+ var _loop_1 = function (chainId) {
209
+ if (this_1.spokePoolClients[chainId] === undefined) {
210
+ return "continue";
211
+ }
212
+ var chainIndex = chainIds.indexOf(chainId);
213
+ this_1.spokePoolClients[chainId]
214
+ .getFills()
215
+ .filter(function (fill) {
216
+ if (fill.blockNumber < blockRanges[chainIndex][0] || fill.blockNumber > blockRanges[chainIndex][1]) {
217
+ return false;
218
+ }
219
+ if (_this.spokePoolClients[fill.originChainId] === undefined) {
220
+ return false;
221
+ }
222
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
223
+ var hasMatchingDeposit = matchingDeposit !== undefined &&
224
+ _this.getRelayHashFromEvent(fill) === _this.getRelayHashFromEvent(matchingDeposit);
225
+ return hasMatchingDeposit;
226
+ })
227
+ .forEach(function (fill) {
228
+ var _a, _b, _c;
229
+ var _d;
230
+ var matchingDeposit = _this.spokePoolClients[fill.originChainId].getDeposit(fill.depositId);
231
+ (0, utils_1.assert)((0, utils_1.isDefined)(matchingDeposit), "Deposit not found for fill.");
232
+ var _e = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRanges, _this.chainIdListForBundleEvaluationBlockNumbers, matchingDeposit.fromLiteChain), chainToSendRefundTo = _e.chainToSendRefundTo, repaymentToken = _e.repaymentToken;
233
+ var relayer = fill.relayer, refundAmount = fill.inputAmount;
234
+ (_a = refundsForChain[chainToSendRefundTo]) !== null && _a !== void 0 ? _a : (refundsForChain[chainToSendRefundTo] = {});
235
+ (_b = (_d = refundsForChain[chainToSendRefundTo])[repaymentToken]) !== null && _b !== void 0 ? _b : (_d[repaymentToken] = {});
236
+ var existingRefundAmount = (_c = refundsForChain[chainToSendRefundTo][repaymentToken][relayer]) !== null && _c !== void 0 ? _c : utils_1.bnZero;
237
+ refundsForChain[chainToSendRefundTo][repaymentToken][relayer] = existingRefundAmount.add(refundAmount);
238
+ });
239
+ };
240
+ var this_1 = this;
241
+ for (var _i = 0, chainIds_1 = chainIds; _i < chainIds_1.length; _i++) {
242
+ var chainId = chainIds_1[_i];
243
+ _loop_1(chainId);
244
+ }
245
+ return refundsForChain;
246
+ };
247
+ BundleDataClient.prototype.getUpcomingDepositAmount = function (chainId, l2Token, latestBlockToSearch) {
248
+ if (this.spokePoolClients[chainId] === undefined) {
249
+ return (0, utils_1.toBN)(0);
250
+ }
251
+ return this.spokePoolClients[chainId]
252
+ .getDeposits()
253
+ .filter(function (deposit) { return deposit.blockNumber > latestBlockToSearch && deposit.inputToken === l2Token; })
254
+ .reduce(function (acc, deposit) {
255
+ return acc.add(deposit.inputAmount);
256
+ }, (0, utils_1.toBN)(0));
257
+ };
258
+ BundleDataClient.prototype.getLatestProposedBundleData = function () {
259
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
260
+ var hubPoolClient, bundleBlockRanges;
261
+ var _a;
262
+ return tslib_1.__generator(this, function (_b) {
263
+ switch (_b.label) {
264
+ case 0:
265
+ hubPoolClient = this.clients.hubPoolClient;
266
+ bundleBlockRanges = (0, utils_1.getImpliedBundleBlockRanges)(hubPoolClient, this.clients.configStoreClient, hubPoolClient.hasPendingProposal()
267
+ ? hubPoolClient.getLatestProposedRootBundle()
268
+ : hubPoolClient.getLatestFullyExecutedRootBundle(hubPoolClient.latestBlockSearched));
269
+ _a = {
270
+ blockRanges: bundleBlockRanges
271
+ };
272
+ return [4, this.loadData(bundleBlockRanges, this.spokePoolClients, true)];
273
+ case 1: return [2, (_a.bundleData = _b.sent(),
274
+ _a)];
275
+ }
276
+ });
277
+ });
278
+ };
279
+ BundleDataClient.prototype.getLatestPoolRebalanceRoot = function () {
280
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
281
+ var _a, bundleData, blockRanges, hubPoolClient, root;
282
+ return tslib_1.__generator(this, function (_b) {
283
+ switch (_b.label) {
284
+ case 0: return [4, this.getLatestProposedBundleData()];
285
+ case 1:
286
+ _a = _b.sent(), bundleData = _a.bundleData, blockRanges = _a.blockRanges;
287
+ hubPoolClient = this.clients.hubPoolClient;
288
+ return [4, (0, utils_2._buildPoolRebalanceRoot)(hubPoolClient.latestBlockSearched, blockRanges[0][1], bundleData.bundleDepositsV3, bundleData.bundleFillsV3, bundleData.bundleSlowFillsV3, bundleData.unexecutableSlowFills, bundleData.expiredDepositsToRefundV3, {
289
+ hubPoolClient: hubPoolClient,
290
+ configStoreClient: hubPoolClient.configStoreClient,
291
+ })];
292
+ case 2:
293
+ root = _b.sent();
294
+ return [2, {
295
+ root: root,
296
+ blockRanges: blockRanges,
297
+ }];
298
+ }
299
+ });
300
+ });
301
+ };
302
+ BundleDataClient.prototype.getNextBundleRefunds = function () {
303
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
304
+ function getBlockRangeDelta(_pendingBlockRanges) {
305
+ return widestBundleBlockRanges.map(function (blockRange, index) {
306
+ var _a;
307
+ var initialBlockRange = (_a = _pendingBlockRanges[index]) !== null && _a !== void 0 ? _a : blockRange;
308
+ if (initialBlockRange[0] === initialBlockRange[1]) {
309
+ return initialBlockRange;
310
+ }
311
+ if (!(0, utils_1.isDefined)(blockRange[1]) || initialBlockRange[1] >= blockRange[1]) {
312
+ return [initialBlockRange[1], initialBlockRange[1]];
313
+ }
314
+ return [initialBlockRange[1] + 1, blockRange[1]];
315
+ });
316
+ }
317
+ var hubPoolClient, nextBundleMainnetStartBlock, chainIds, combinedRefunds, widestBundleBlockRanges, pendingBundleBlockRanges, arweaveData, bundleFillsV3, expiredDepositsToRefundV3, start;
318
+ return tslib_1.__generator(this, function (_a) {
319
+ switch (_a.label) {
320
+ case 0:
321
+ hubPoolClient = this.clients.hubPoolClient;
322
+ nextBundleMainnetStartBlock = hubPoolClient.getNextBundleStartBlockNumber(this.chainIdListForBundleEvaluationBlockNumbers, hubPoolClient.latestBlockSearched, hubPoolClient.chainId);
323
+ chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(nextBundleMainnetStartBlock);
324
+ combinedRefunds = [];
325
+ 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));
326
+ if (!hubPoolClient.hasPendingProposal()) return [3, 2];
327
+ pendingBundleBlockRanges = (0, utils_1.getImpliedBundleBlockRanges)(hubPoolClient, this.clients.configStoreClient, hubPoolClient.getLatestProposedRootBundle());
328
+ return [4, this.loadArweaveData(pendingBundleBlockRanges)];
329
+ case 1:
330
+ arweaveData = _a.sent();
331
+ if (arweaveData === undefined) {
332
+ combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, pendingBundleBlockRanges));
333
+ }
334
+ else {
335
+ bundleFillsV3 = arweaveData.bundleFillsV3, expiredDepositsToRefundV3 = arweaveData.expiredDepositsToRefundV3;
336
+ combinedRefunds.push((0, utils_2.getRefundsFromBundle)(bundleFillsV3, expiredDepositsToRefundV3));
337
+ }
338
+ widestBundleBlockRanges = getBlockRangeDelta(pendingBundleBlockRanges);
339
+ _a.label = 2;
340
+ case 2:
341
+ start = performance.now();
342
+ combinedRefunds.push(this.getApproximateRefundsForBlockRange(chainIds, widestBundleBlockRanges));
343
+ this.logger.debug({
344
+ at: "BundleDataClient#getNextBundleRefunds",
345
+ message: "Loading approximate refunds for next bundle in ".concat(Math.round(performance.now() - start) / 1000, "s."),
346
+ blockRanges: JSON.stringify(widestBundleBlockRanges),
347
+ });
348
+ return [2, combinedRefunds];
349
+ }
350
+ });
351
+ });
352
+ };
353
+ BundleDataClient.prototype.getExecutedRefunds = function (spokePoolClient, relayerRefundRoot) {
354
+ if (!(0, utils_1.isDefined)(spokePoolClient)) {
355
+ return {};
356
+ }
357
+ var bundle = lodash_1.default.findLast(spokePoolClient.getRootBundleRelays(), function (bundle) { return bundle.relayerRefundRoot === relayerRefundRoot; });
358
+ if (bundle === undefined) {
359
+ return {};
360
+ }
361
+ var executedRefundLeaves = spokePoolClient
362
+ .getRelayerRefundExecutions()
363
+ .filter(function (leaf) { return leaf.rootBundleId === bundle.rootBundleId; });
364
+ var executedRefunds = {};
365
+ for (var _i = 0, executedRefundLeaves_1 = executedRefundLeaves; _i < executedRefundLeaves_1.length; _i++) {
366
+ var refundLeaf = executedRefundLeaves_1[_i];
367
+ var tokenAddress = refundLeaf.l2TokenAddress;
368
+ if (executedRefunds[tokenAddress] === undefined) {
369
+ executedRefunds[tokenAddress] = {};
370
+ }
371
+ var executedTokenRefunds = executedRefunds[tokenAddress];
372
+ for (var i = 0; i < refundLeaf.refundAddresses.length; i++) {
373
+ var relayer = refundLeaf.refundAddresses[i];
374
+ var refundAmount = refundLeaf.refundAmounts[i];
375
+ if (executedTokenRefunds[relayer] === undefined) {
376
+ executedTokenRefunds[relayer] = utils_1.bnZero;
377
+ }
378
+ executedTokenRefunds[relayer] = executedTokenRefunds[relayer].add(refundAmount);
379
+ }
380
+ }
381
+ return executedRefunds;
382
+ };
383
+ BundleDataClient.prototype.deductExecutedRefunds = function (allRefunds, bundleContainingRefunds) {
384
+ for (var _i = 0, _a = Object.keys(allRefunds); _i < _a.length; _i++) {
385
+ var chainIdStr = _a[_i];
386
+ var chainId = Number(chainIdStr);
387
+ if (!(0, utils_1.isDefined)(this.spokePoolClients[chainId])) {
388
+ continue;
389
+ }
390
+ var executedRefunds = this.getExecutedRefunds(this.spokePoolClients[chainId], bundleContainingRefunds.relayerRefundRoot);
391
+ for (var _b = 0, _c = Object.keys(allRefunds[chainId]); _b < _c.length; _b++) {
392
+ var tokenAddress = _c[_b];
393
+ var refunds = allRefunds[chainId][tokenAddress];
394
+ if (executedRefunds[tokenAddress] === undefined || refunds === undefined) {
395
+ continue;
396
+ }
397
+ for (var _d = 0, _e = Object.keys(refunds); _d < _e.length; _d++) {
398
+ var relayer = _e[_d];
399
+ var executedAmount = executedRefunds[tokenAddress][relayer];
400
+ if (executedAmount === undefined) {
401
+ continue;
402
+ }
403
+ refunds[relayer] = utils_1.bnZero;
404
+ }
405
+ }
406
+ }
407
+ return allRefunds;
408
+ };
409
+ BundleDataClient.prototype.getRefundsFor = function (bundleRefunds, relayer, chainId, token) {
410
+ if (!bundleRefunds[chainId] || !bundleRefunds[chainId][token]) {
411
+ return ethers_1.BigNumber.from(0);
412
+ }
413
+ var allRefunds = bundleRefunds[chainId][token];
414
+ return allRefunds && allRefunds[relayer] ? allRefunds[relayer] : ethers_1.BigNumber.from(0);
415
+ };
416
+ BundleDataClient.prototype.getTotalRefund = function (refunds, relayer, chainId, refundToken) {
417
+ var _this = this;
418
+ return refunds.reduce(function (totalRefund, refunds) {
419
+ return totalRefund.add(_this.getRefundsFor(refunds, relayer, chainId, refundToken));
420
+ }, utils_1.bnZero);
421
+ };
422
+ BundleDataClient.prototype.loadArweaveData = function (blockRangesForChains) {
423
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
424
+ var arweaveKey, arweaveData, _a, _b;
425
+ return tslib_1.__generator(this, function (_c) {
426
+ switch (_c.label) {
427
+ case 0:
428
+ arweaveKey = this.getArweaveClientKey(blockRangesForChains);
429
+ if (!this.arweaveDataCache[arweaveKey]) {
430
+ this.arweaveDataCache[arweaveKey] = this.loadPersistedDataFromArweave(blockRangesForChains);
431
+ }
432
+ _b = (_a = lodash_1.default).cloneDeep;
433
+ return [4, this.arweaveDataCache[arweaveKey]];
434
+ case 1:
435
+ arweaveData = _b.apply(_a, [_c.sent()]);
436
+ return [2, arweaveData];
437
+ }
438
+ });
439
+ });
440
+ };
441
+ BundleDataClient.prototype.loadData = function (blockRangesForChains, spokePoolClients, attemptArweaveLoad) {
442
+ if (attemptArweaveLoad === void 0) { attemptArweaveLoad = false; }
443
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
444
+ var key, arweaveData, data;
445
+ return tslib_1.__generator(this, function (_a) {
446
+ switch (_a.label) {
447
+ case 0:
448
+ key = JSON.stringify(blockRangesForChains);
449
+ if (!!this.loadDataCache[key]) return [3, 4];
450
+ arweaveData = void 0;
451
+ if (!attemptArweaveLoad) return [3, 2];
452
+ return [4, this.loadArweaveData(blockRangesForChains)];
453
+ case 1:
454
+ arweaveData = _a.sent();
455
+ return [3, 3];
456
+ case 2:
457
+ arweaveData = undefined;
458
+ _a.label = 3;
459
+ case 3:
460
+ data = (0, utils_1.isDefined)(arweaveData)
461
+ ?
462
+ Promise.resolve(arweaveData)
463
+ : this.loadDataFromScratch(blockRangesForChains, spokePoolClients);
464
+ this.loadDataCache[key] = data;
465
+ _a.label = 4;
466
+ case 4: return [2, this.loadDataFromCache(key)];
467
+ }
468
+ });
469
+ });
470
+ };
471
+ BundleDataClient.prototype.loadDataFromScratch = function (blockRangesForChains, spokePoolClients) {
472
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
473
+ var start, key, chainIds, bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills, _isChainDisabled, allChainIds, _cachedBundleTimestamps, bundleBlockTimestamps, v3RelayHashes, expiredBundleDepositHashes, olderDepositHashes, depositCounter, _loop_2, _i, allChainIds_1, originChainId, validatedBundleV3Fills, validatedBundleSlowFills, validatedBundleUnexecutableSlowFills, fillCounter, _loop_3, _a, allChainIds_2, originChainId, promises, _b, v3FillLpFees, v3SlowFillLpFees, v3UnexecutableSlowFillLpFees, v3SpokeEventsReadable;
474
+ var _this = this;
475
+ return tslib_1.__generator(this, function (_c) {
476
+ switch (_c.label) {
477
+ case 0:
478
+ start = performance.now();
479
+ key = JSON.stringify(blockRangesForChains);
480
+ if (!this.clients.configStoreClient.isUpdated) {
481
+ throw new Error("ConfigStoreClient not updated");
482
+ }
483
+ else if (!this.clients.hubPoolClient.isUpdated) {
484
+ throw new Error("HubPoolClient not updated");
485
+ }
486
+ chainIds = this.clients.configStoreClient.getChainIdIndicesForBlock(blockRangesForChains[0][0]);
487
+ if (blockRangesForChains.length > chainIds.length) {
488
+ throw new Error("Unexpected block range list length of ".concat(blockRangesForChains.length, ", should be <= ").concat(chainIds.length));
489
+ }
490
+ bundleDepositsV3 = {};
491
+ bundleFillsV3 = {};
492
+ bundleInvalidFillsV3 = [];
493
+ bundleSlowFillsV3 = {};
494
+ expiredDepositsToRefundV3 = {};
495
+ unexecutableSlowFills = {};
496
+ _isChainDisabled = function (chainId) {
497
+ var blockRangeForChain = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, chainId, chainIds);
498
+ return (0, utils_2.isChainDisabled)(blockRangeForChain);
499
+ };
500
+ allChainIds = blockRangesForChains
501
+ .map(function (_blockRange, index) { return chainIds[index]; })
502
+ .filter(function (chainId) { return !_isChainDisabled(chainId) && spokePoolClients[chainId] !== undefined; });
503
+ allChainIds.forEach(function (chainId) {
504
+ var spokePoolClient = spokePoolClients[chainId];
505
+ if (!spokePoolClient.isUpdated) {
506
+ throw new Error("SpokePoolClient for chain ".concat(chainId, " not updated."));
507
+ }
508
+ });
509
+ _cachedBundleTimestamps = this.getBundleTimestampsFromCache(key);
510
+ bundleBlockTimestamps = {};
511
+ if (!!_cachedBundleTimestamps) return [3, 2];
512
+ return [4, this.getBundleBlockTimestamps(chainIds, blockRangesForChains, spokePoolClients)];
513
+ case 1:
514
+ bundleBlockTimestamps = _c.sent();
515
+ this.setBundleTimestampsInCache(key, bundleBlockTimestamps);
516
+ this.logger.debug({
517
+ at: "BundleDataClient#loadData",
518
+ message: "Bundle block timestamps",
519
+ bundleBlockTimestamps: bundleBlockTimestamps,
520
+ blockRangesForChains: JSON.stringify(blockRangesForChains),
521
+ });
522
+ return [3, 3];
523
+ case 2:
524
+ bundleBlockTimestamps = _cachedBundleTimestamps;
525
+ _c.label = 3;
526
+ case 3:
527
+ v3RelayHashes = {};
528
+ expiredBundleDepositHashes = new Set();
529
+ olderDepositHashes = new Set();
530
+ depositCounter = 0;
531
+ _loop_2 = function (originChainId) {
532
+ var originClient = spokePoolClients[originChainId];
533
+ var originChainBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, originChainId, chainIds);
534
+ var _loop_4 = function (destinationChainId) {
535
+ if (originChainId === destinationChainId) {
536
+ return "continue";
537
+ }
538
+ originClient
539
+ .getDepositsForDestinationChain(destinationChainId)
540
+ .filter(function (deposit) { return deposit.blockNumber <= originChainBlockRange[1]; })
541
+ .forEach(function (deposit) {
542
+ depositCounter++;
543
+ var relayDataHash = _this.getRelayHashFromEvent(deposit);
544
+ if (v3RelayHashes[relayDataHash]) {
545
+ return;
546
+ }
547
+ v3RelayHashes[relayDataHash] = {
548
+ deposit: deposit,
549
+ fill: undefined,
550
+ slowFillRequest: undefined,
551
+ };
552
+ if (deposit.blockNumber >= originChainBlockRange[0]) {
553
+ updateBundleDepositsV3(bundleDepositsV3, deposit);
554
+ if (deposit.fillDeadline < bundleBlockTimestamps[destinationChainId][1]) {
555
+ expiredBundleDepositHashes.add(relayDataHash);
556
+ }
557
+ }
558
+ else {
559
+ olderDepositHashes.add(relayDataHash);
560
+ }
561
+ });
562
+ };
563
+ for (var _d = 0, allChainIds_3 = allChainIds; _d < allChainIds_3.length; _d++) {
564
+ var destinationChainId = allChainIds_3[_d];
565
+ _loop_4(destinationChainId);
566
+ }
567
+ };
568
+ for (_i = 0, allChainIds_1 = allChainIds; _i < allChainIds_1.length; _i++) {
569
+ originChainId = allChainIds_1[_i];
570
+ _loop_2(originChainId);
571
+ }
572
+ this.logger.debug({
573
+ at: "BundleDataClient#loadData",
574
+ message: "Processed ".concat(depositCounter, " deposits in ").concat(performance.now() - start, "ms."),
575
+ });
576
+ start = performance.now();
577
+ validatedBundleV3Fills = [];
578
+ validatedBundleSlowFills = [];
579
+ validatedBundleUnexecutableSlowFills = [];
580
+ fillCounter = 0;
581
+ _loop_3 = function (originChainId) {
582
+ var originClient, _loop_5, _e, allChainIds_4, destinationChainId;
583
+ return tslib_1.__generator(this, function (_f) {
584
+ switch (_f.label) {
585
+ case 0:
586
+ originClient = spokePoolClients[originChainId];
587
+ _loop_5 = function (destinationChainId) {
588
+ var destinationClient, destinationChainBlockRange, fastFillsReplacingSlowFills;
589
+ return tslib_1.__generator(this, function (_g) {
590
+ switch (_g.label) {
591
+ case 0:
592
+ if (originChainId === destinationChainId) {
593
+ return [2, "continue"];
594
+ }
595
+ destinationClient = spokePoolClients[destinationChainId];
596
+ destinationChainBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
597
+ fastFillsReplacingSlowFills = [];
598
+ return [4, (0, utils_1.forEachAsync)(destinationClient
599
+ .getFillsForOriginChain(originChainId)
600
+ .filter(function (fill) { return fill.blockNumber <= destinationChainBlockRange[1]; }), function (fill) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
601
+ var relayDataHash, historicalDeposit, matchedDeposit;
602
+ return tslib_1.__generator(this, function (_a) {
603
+ switch (_a.label) {
604
+ case 0:
605
+ relayDataHash = this.getRelayHashFromEvent(fill);
606
+ fillCounter++;
607
+ if (v3RelayHashes[relayDataHash]) {
608
+ if (!v3RelayHashes[relayDataHash].fill) {
609
+ (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
610
+ v3RelayHashes[relayDataHash].fill = fill;
611
+ if (fill.blockNumber >= destinationChainBlockRange[0]) {
612
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fill), { quoteTimestamp: v3RelayHashes[relayDataHash].deposit.quoteTimestamp }));
613
+ if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill) {
614
+ fastFillsReplacingSlowFills.push(relayDataHash);
615
+ }
616
+ }
617
+ }
618
+ return [2];
619
+ }
620
+ v3RelayHashes[relayDataHash] = {
621
+ deposit: undefined,
622
+ fill: fill,
623
+ slowFillRequest: undefined,
624
+ };
625
+ if (!(fill.blockNumber >= destinationChainBlockRange[0])) return [3, 2];
626
+ return [4, (0, utils_1.queryHistoricalDepositForFill)(originClient, fill)];
627
+ case 1:
628
+ historicalDeposit = _a.sent();
629
+ if (!historicalDeposit.found) {
630
+ bundleInvalidFillsV3.push(fill);
631
+ }
632
+ else {
633
+ matchedDeposit = historicalDeposit.deposit;
634
+ (0, utils_1.assert)(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Relay hashes should match.");
635
+ validatedBundleV3Fills.push(tslib_1.__assign(tslib_1.__assign({}, fill), { quoteTimestamp: matchedDeposit.quoteTimestamp }));
636
+ v3RelayHashes[relayDataHash].deposit = matchedDeposit;
637
+ if (fill.relayExecutionInfo.fillType === interfaces_1.FillType.ReplacedSlowFill) {
638
+ fastFillsReplacingSlowFills.push(relayDataHash);
639
+ }
640
+ }
641
+ _a.label = 2;
642
+ case 2: return [2];
643
+ }
644
+ });
645
+ }); })];
646
+ case 1:
647
+ _g.sent();
648
+ return [4, (0, utils_1.forEachAsync)(destinationClient
649
+ .getSlowFillRequestsForOriginChain(originChainId)
650
+ .filter(function (request) { return request.blockNumber <= destinationChainBlockRange[1]; }), function (slowFillRequest) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
651
+ var relayDataHash, matchedDeposit, historicalDeposit, matchedDeposit;
652
+ var _a, _b;
653
+ return tslib_1.__generator(this, function (_c) {
654
+ switch (_c.label) {
655
+ case 0:
656
+ relayDataHash = this.getRelayHashFromEvent(slowFillRequest);
657
+ if (v3RelayHashes[relayDataHash]) {
658
+ if (!v3RelayHashes[relayDataHash].slowFillRequest) {
659
+ v3RelayHashes[relayDataHash].slowFillRequest = slowFillRequest;
660
+ if (v3RelayHashes[relayDataHash].fill) {
661
+ return [2];
662
+ }
663
+ (0, utils_1.assert)((0, utils_1.isDefined)(v3RelayHashes[relayDataHash].deposit), "Deposit should exist in relay hash dictionary.");
664
+ matchedDeposit = v3RelayHashes[relayDataHash].deposit;
665
+ if (!this.clients.hubPoolClient.areTokensEquivalent(matchedDeposit.inputToken, matchedDeposit.originChainId, matchedDeposit.outputToken, matchedDeposit.destinationChainId, matchedDeposit.quoteBlockNumber)) {
666
+ return [2];
667
+ }
668
+ if (((_a = v3RelayHashes[relayDataHash].deposit) === null || _a === void 0 ? void 0 : _a.fromLiteChain) ||
669
+ ((_b = v3RelayHashes[relayDataHash].deposit) === null || _b === void 0 ? void 0 : _b.toLiteChain)) {
670
+ return [2];
671
+ }
672
+ if (slowFillRequest.blockNumber >= destinationChainBlockRange[0] &&
673
+ slowFillRequest.fillDeadline >= bundleBlockTimestamps[destinationChainId][1]) {
674
+ validatedBundleSlowFills.push(matchedDeposit);
675
+ }
676
+ }
677
+ return [2];
678
+ }
679
+ v3RelayHashes[relayDataHash] = {
680
+ deposit: undefined,
681
+ fill: undefined,
682
+ slowFillRequest: slowFillRequest,
683
+ };
684
+ if (!(slowFillRequest.blockNumber >= destinationChainBlockRange[0])) return [3, 2];
685
+ return [4, (0, utils_1.queryHistoricalDepositForFill)(originClient, slowFillRequest)];
686
+ case 1:
687
+ historicalDeposit = _c.sent();
688
+ if (!historicalDeposit.found) {
689
+ return [2];
690
+ }
691
+ matchedDeposit = historicalDeposit.deposit;
692
+ (0, utils_1.assert)(this.getRelayHashFromEvent(matchedDeposit) === relayDataHash, "Deposit relay hashes should match.");
693
+ if (matchedDeposit.fromLiteChain || matchedDeposit.toLiteChain) {
694
+ return [2];
695
+ }
696
+ v3RelayHashes[relayDataHash].deposit = matchedDeposit;
697
+ if (!this.clients.hubPoolClient.areTokensEquivalent(matchedDeposit.inputToken, matchedDeposit.originChainId, matchedDeposit.outputToken, matchedDeposit.destinationChainId, matchedDeposit.quoteBlockNumber) ||
698
+ slowFillRequest.fillDeadline < bundleBlockTimestamps[destinationChainId][1]) {
699
+ return [2];
700
+ }
701
+ validatedBundleSlowFills.push(matchedDeposit);
702
+ _c.label = 2;
703
+ case 2: return [2];
704
+ }
705
+ });
706
+ }); })];
707
+ case 2:
708
+ _g.sent();
709
+ fastFillsReplacingSlowFills.forEach(function (relayDataHash) {
710
+ var _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
711
+ (0, utils_1.assert)((fill === null || fill === void 0 ? void 0 : fill.relayExecutionInfo.fillType) === interfaces_1.FillType.ReplacedSlowFill, "Fill type should be ReplacedSlowFill.");
712
+ if (!deposit) {
713
+ throw new Error("Deposit should exist in relay hash dictionary.");
714
+ }
715
+ var destinationBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
716
+ if (_this.clients.hubPoolClient.areTokensEquivalent(deposit.inputToken, deposit.originChainId, deposit.outputToken, deposit.destinationChainId, deposit.quoteBlockNumber) &&
717
+ (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0])) {
718
+ validatedBundleUnexecutableSlowFills.push(deposit);
719
+ }
720
+ });
721
+ return [2];
722
+ }
723
+ });
724
+ };
725
+ _e = 0, allChainIds_4 = allChainIds;
726
+ _f.label = 1;
727
+ case 1:
728
+ if (!(_e < allChainIds_4.length)) return [3, 4];
729
+ destinationChainId = allChainIds_4[_e];
730
+ return [5, _loop_5(destinationChainId)];
731
+ case 2:
732
+ _f.sent();
733
+ _f.label = 3;
734
+ case 3:
735
+ _e++;
736
+ return [3, 1];
737
+ case 4: return [2];
738
+ }
739
+ });
740
+ };
741
+ _a = 0, allChainIds_2 = allChainIds;
742
+ _c.label = 4;
743
+ case 4:
744
+ if (!(_a < allChainIds_2.length)) return [3, 7];
745
+ originChainId = allChainIds_2[_a];
746
+ return [5, _loop_3(originChainId)];
747
+ case 5:
748
+ _c.sent();
749
+ _c.label = 6;
750
+ case 6:
751
+ _a++;
752
+ return [3, 4];
753
+ case 7:
754
+ this.logger.debug({
755
+ at: "BundleDataClient#loadData",
756
+ message: "Processed ".concat(fillCounter, " fills in ").concat(performance.now() - start, "ms."),
757
+ });
758
+ start = performance.now();
759
+ expiredBundleDepositHashes.forEach(function (relayDataHash) {
760
+ var _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, fill = _a.fill;
761
+ (0, utils_1.assert)((0, utils_1.isDefined)(deposit), "Deposit should exist in relay hash dictionary.");
762
+ if (!fill &&
763
+ (0, utils_1.isDefined)(deposit)) {
764
+ updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
765
+ }
766
+ });
767
+ return [4, (0, utils_1.forEachAsync)(Array.from(olderDepositHashes), function (relayDataHash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
768
+ var _a, deposit, slowFillRequest, fill, destinationChainId, destinationBlockRange, fillStatus;
769
+ return tslib_1.__generator(this, function (_b) {
770
+ switch (_b.label) {
771
+ case 0:
772
+ _a = v3RelayHashes[relayDataHash], deposit = _a.deposit, slowFillRequest = _a.slowFillRequest, fill = _a.fill;
773
+ (0, utils_1.assert)((0, utils_1.isDefined)(deposit), "Deposit should exist in relay hash dictionary.");
774
+ destinationChainId = deposit.destinationChainId;
775
+ destinationBlockRange = (0, utils_1.getBlockRangeForChain)(blockRangesForChains, destinationChainId, chainIds);
776
+ if (!(!fill &&
777
+ (0, utils_1.isDefined)(deposit) &&
778
+ deposit.fillDeadline < bundleBlockTimestamps[destinationChainId][1] &&
779
+ deposit.fillDeadline >= bundleBlockTimestamps[destinationChainId][0] &&
780
+ spokePoolClients[destinationChainId] !== undefined)) return [3, 2];
781
+ return [4, (0, utils_1.relayFillStatus)(spokePoolClients[destinationChainId].spokePool, deposit, Math.min(destinationBlockRange[1], spokePoolClients[destinationChainId].latestBlockSearched), destinationChainId)];
782
+ case 1:
783
+ fillStatus = _b.sent();
784
+ if (fillStatus !== interfaces_1.FillStatus.Filled) {
785
+ updateExpiredDepositsV3(expiredDepositsToRefundV3, deposit);
786
+ }
787
+ if (fillStatus !== interfaces_1.FillStatus.RequestedSlowFill) {
788
+ return [2];
789
+ }
790
+ if (this.clients.hubPoolClient.areTokensEquivalent(deposit.inputToken, deposit.originChainId, deposit.outputToken, deposit.destinationChainId, deposit.quoteBlockNumber) &&
791
+ (!slowFillRequest || slowFillRequest.blockNumber < destinationBlockRange[0])) {
792
+ validatedBundleUnexecutableSlowFills.push(deposit);
793
+ }
794
+ _b.label = 2;
795
+ case 2: return [2];
796
+ }
797
+ });
798
+ }); })];
799
+ case 8:
800
+ _c.sent();
801
+ start = performance.now();
802
+ promises = [
803
+ validatedBundleV3Fills.length > 0
804
+ ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleV3Fills.map(function (fill) {
805
+ var matchedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
806
+ (0, utils_1.assert)((0, utils_1.isDefined)(matchedDeposit), "Deposit should exist in relay hash dictionary.");
807
+ var paymentChainId = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, matchedDeposit.fromLiteChain).chainToSendRefundTo;
808
+ return tslib_1.__assign(tslib_1.__assign({}, fill), { paymentChainId: paymentChainId });
809
+ }))
810
+ : [],
811
+ validatedBundleSlowFills.length > 0
812
+ ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleSlowFills.map(function (deposit) {
813
+ return tslib_1.__assign(tslib_1.__assign({}, deposit), { paymentChainId: deposit.destinationChainId });
814
+ }))
815
+ : [],
816
+ validatedBundleUnexecutableSlowFills.length > 0
817
+ ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct(validatedBundleUnexecutableSlowFills.map(function (deposit) {
818
+ return tslib_1.__assign(tslib_1.__assign({}, deposit), { paymentChainId: deposit.destinationChainId });
819
+ }))
820
+ : [],
821
+ ];
822
+ return [4, Promise.all(promises)];
823
+ case 9:
824
+ _b = _c.sent(), v3FillLpFees = _b[0], v3SlowFillLpFees = _b[1], v3UnexecutableSlowFillLpFees = _b[2];
825
+ this.logger.debug({
826
+ at: "BundleDataClient#loadData",
827
+ message: "Computed batch async LP fees in ".concat(performance.now() - start, "ms."),
828
+ });
829
+ v3FillLpFees.forEach(function (_a, idx) {
830
+ var realizedLpFeePct = _a.realizedLpFeePct;
831
+ var fill = validatedBundleV3Fills[idx];
832
+ var associatedDeposit = v3RelayHashes[_this.getRelayHashFromEvent(fill)].deposit;
833
+ (0, utils_1.assert)((0, utils_1.isDefined)(associatedDeposit), "Deposit should exist in relay hash dictionary.");
834
+ var _b = (0, utils_2.getRefundInformationFromFill)(fill, _this.clients.hubPoolClient, blockRangesForChains, chainIds, associatedDeposit.fromLiteChain), chainToSendRefundTo = _b.chainToSendRefundTo, repaymentToken = _b.repaymentToken;
835
+ updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken);
836
+ });
837
+ v3SlowFillLpFees.forEach(function (_a, idx) {
838
+ var lpFeePct = _a.realizedLpFeePct;
839
+ var deposit = validatedBundleSlowFills[idx];
840
+ updateBundleSlowFills(bundleSlowFillsV3, tslib_1.__assign(tslib_1.__assign({}, deposit), { lpFeePct: lpFeePct }));
841
+ });
842
+ v3UnexecutableSlowFillLpFees.forEach(function (_a, idx) {
843
+ var lpFeePct = _a.realizedLpFeePct;
844
+ var deposit = validatedBundleUnexecutableSlowFills[idx];
845
+ updateBundleExcessSlowFills(unexecutableSlowFills, tslib_1.__assign(tslib_1.__assign({}, deposit), { lpFeePct: lpFeePct }));
846
+ });
847
+ v3SpokeEventsReadable = (0, utils_2.prettyPrintV3SpokePoolEvents)(bundleDepositsV3, bundleFillsV3, bundleInvalidFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills);
848
+ if (bundleInvalidFillsV3.length > 0) {
849
+ this.logger.debug({
850
+ at: "BundleDataClient#loadData",
851
+ message: "Finished loading V3 spoke pool data and found some invalid V3 fills in range",
852
+ blockRangesForChains: blockRangesForChains,
853
+ bundleInvalidFillsV3: bundleInvalidFillsV3,
854
+ });
855
+ }
856
+ this.logger.debug({
857
+ at: "BundleDataClient#loadDataFromScratch",
858
+ message: "Computed bundle data in ".concat(Math.round(performance.now() - start) / 1000, "s."),
859
+ blockRangesForChains: JSON.stringify(blockRangesForChains),
860
+ v3SpokeEventsReadable: v3SpokeEventsReadable,
861
+ });
862
+ return [2, {
863
+ bundleDepositsV3: bundleDepositsV3,
864
+ expiredDepositsToRefundV3: expiredDepositsToRefundV3,
865
+ bundleFillsV3: bundleFillsV3,
866
+ unexecutableSlowFills: unexecutableSlowFills,
867
+ bundleSlowFillsV3: bundleSlowFillsV3,
868
+ }];
869
+ }
870
+ });
871
+ });
872
+ };
873
+ BundleDataClient.prototype.getRelayHashFromEvent = function (event) {
874
+ 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);
875
+ };
876
+ BundleDataClient.prototype.getBundleBlockTimestamps = function (chainIds, blockRangesForChains, spokePoolClients) {
877
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
878
+ var _a, _b;
879
+ var _this = this;
880
+ return tslib_1.__generator(this, function (_c) {
881
+ switch (_c.label) {
882
+ case 0:
883
+ _b = (_a = Object).fromEntries;
884
+ return [4, (0, utils_1.mapAsync)(chainIds, function (chainId, index) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
885
+ var blockRangeForChain, _startBlockForChain, _endBlockForChain, spokePoolClient, startBlockForChain, endBlockForChain, _a, startTime, endTime, _b, _c, _d;
886
+ return tslib_1.__generator(this, function (_e) {
887
+ switch (_e.label) {
888
+ case 0:
889
+ blockRangeForChain = blockRangesForChains[index];
890
+ if (!(0, utils_1.isDefined)(blockRangeForChain) || (0, utils_2.isChainDisabled)(blockRangeForChain)) {
891
+ return [2];
892
+ }
893
+ _startBlockForChain = blockRangeForChain[0], _endBlockForChain = blockRangeForChain[1];
894
+ spokePoolClient = spokePoolClients[chainId];
895
+ if (!(0, utils_1.isDefined)(spokePoolClient)) {
896
+ return [2];
897
+ }
898
+ startBlockForChain = Math.min(_startBlockForChain, spokePoolClient.latestBlockSearched);
899
+ endBlockForChain = Math.min(_endBlockForChain, spokePoolClient.latestBlockSearched);
900
+ _b = Number;
901
+ return [4, spokePoolClient.spokePool.provider.getBlock(startBlockForChain)];
902
+ case 1:
903
+ _c = [
904
+ _b.apply(void 0, [(_e.sent()).timestamp])
905
+ ];
906
+ _d = Number;
907
+ return [4, spokePoolClient.spokePool.provider.getBlock(endBlockForChain)];
908
+ case 2:
909
+ _a = _c.concat([
910
+ _d.apply(void 0, [(_e.sent()).timestamp])
911
+ ]), startTime = _a[0], endTime = _a[1];
912
+ (0, utils_1.assert)(endTime >= startTime, "End time should be greater than start time.");
913
+ (0, utils_1.assert)(startTime > 0, "Start time should be greater than 0.");
914
+ return [2, [chainId, [startTime, endTime]]];
915
+ }
916
+ });
917
+ }); })];
918
+ case 1: return [2, _b.apply(_a, [(_c.sent()).filter(utils_1.isDefined)])];
919
+ }
920
+ });
921
+ });
922
+ };
923
+ return BundleDataClient;
924
+ }());
925
+ exports.BundleDataClient = BundleDataClient;
926
+ //# sourceMappingURL=BundleDataClient.js.map