@lifi/sdk 1.1.2 → 1.1.5

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 (96) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/Lifi.js +179 -285
  3. package/dist/allowance/index.js +56 -148
  4. package/dist/allowance/utils.js +51 -116
  5. package/dist/balances/index.js +29 -92
  6. package/dist/balances/utils.js +108 -218
  7. package/dist/cjs/Lifi.d.ts +200 -0
  8. package/dist/cjs/Lifi.js +376 -0
  9. package/dist/cjs/allowance/index.d.ts +22 -0
  10. package/dist/cjs/allowance/index.js +78 -0
  11. package/dist/cjs/allowance/utils.d.ts +14 -0
  12. package/dist/cjs/allowance/utils.js +82 -0
  13. package/dist/cjs/balances/index.d.ts +11 -0
  14. package/dist/cjs/balances/index.js +46 -0
  15. package/dist/cjs/balances/utils.d.ts +5 -0
  16. package/dist/cjs/balances/utils.js +150 -0
  17. package/dist/cjs/connectors.d.ts +6 -0
  18. package/dist/cjs/connectors.js +77 -0
  19. package/dist/cjs/execution/StatusManager.d.ts +65 -0
  20. package/dist/cjs/execution/StatusManager.js +167 -0
  21. package/dist/cjs/execution/StepExecutor.d.ts +15 -0
  22. package/dist/cjs/execution/StepExecutor.js +74 -0
  23. package/dist/cjs/execution/allowance.execute.d.ts +4 -0
  24. package/dist/cjs/execution/allowance.execute.js +97 -0
  25. package/dist/cjs/execution/balanceCheck.execute.d.ts +3 -0
  26. package/dist/cjs/execution/balanceCheck.execute.js +48 -0
  27. package/dist/cjs/execution/bridges/bridge.execute.d.ts +7 -0
  28. package/dist/cjs/execution/bridges/bridge.execute.js +154 -0
  29. package/dist/cjs/execution/exchanges/swap.execute.d.ts +7 -0
  30. package/dist/cjs/execution/exchanges/swap.execute.js +164 -0
  31. package/dist/cjs/execution/index.d.ts +1 -0
  32. package/dist/cjs/execution/index.js +17 -0
  33. package/dist/cjs/execution/stepComparison.d.ts +14 -0
  34. package/dist/cjs/execution/stepComparison.js +46 -0
  35. package/dist/cjs/execution/switchChain.d.ts +16 -0
  36. package/dist/cjs/execution/switchChain.js +58 -0
  37. package/dist/cjs/execution/utils.d.ts +6 -0
  38. package/dist/cjs/execution/utils.js +137 -0
  39. package/dist/cjs/helpers.d.ts +18 -0
  40. package/dist/cjs/helpers.js +54 -0
  41. package/dist/cjs/index.d.ts +6 -0
  42. package/dist/cjs/index.js +27 -0
  43. package/dist/cjs/services/ApiService.d.ts +15 -0
  44. package/dist/cjs/services/ApiService.js +272 -0
  45. package/dist/cjs/services/ChainsService.d.ts +11 -0
  46. package/dist/cjs/services/ChainsService.js +54 -0
  47. package/dist/cjs/services/ConfigService.d.ts +23 -0
  48. package/dist/cjs/services/ConfigService.js +98 -0
  49. package/dist/cjs/typeguards.d.ts +4 -0
  50. package/dist/cjs/typeguards.js +53 -0
  51. package/dist/cjs/types/ERC20.d.ts +22 -0
  52. package/dist/cjs/types/ERC20.js +53 -0
  53. package/dist/cjs/types/index.d.ts +4 -0
  54. package/dist/cjs/types/index.js +22 -0
  55. package/dist/cjs/types/internal.types.d.ts +85 -0
  56. package/dist/cjs/types/internal.types.js +2 -0
  57. package/dist/cjs/utils/errors.d.ts +75 -0
  58. package/dist/cjs/utils/errors.js +115 -0
  59. package/dist/cjs/utils/getProvider.d.ts +3 -0
  60. package/dist/cjs/utils/getProvider.js +11 -0
  61. package/dist/cjs/utils/multicall.d.ts +10 -0
  62. package/dist/cjs/utils/multicall.js +77 -0
  63. package/dist/cjs/utils/multicallAbi.json +313 -0
  64. package/dist/cjs/utils/parseError.d.ts +38 -0
  65. package/dist/cjs/utils/parseError.js +141 -0
  66. package/dist/cjs/utils/preRestart.d.ts +2 -0
  67. package/dist/cjs/utils/preRestart.js +31 -0
  68. package/dist/cjs/utils/utils.d.ts +26 -0
  69. package/dist/cjs/utils/utils.js +120 -0
  70. package/dist/connectors.js +50 -133
  71. package/dist/execution/StatusManager.js +34 -41
  72. package/dist/execution/StepExecutor.js +54 -123
  73. package/dist/execution/allowance.execute.js +76 -142
  74. package/dist/execution/balanceCheck.execute.js +29 -74
  75. package/dist/execution/bridges/bridge.execute.js +132 -221
  76. package/dist/execution/exchanges/swap.execute.js +142 -225
  77. package/dist/execution/index.js +1 -17
  78. package/dist/execution/stepComparison.js +22 -61
  79. package/dist/execution/switchChain.js +33 -81
  80. package/dist/execution/utils.js +60 -119
  81. package/dist/helpers.js +15 -53
  82. package/dist/index.js +6 -25
  83. package/dist/services/ApiService.js +248 -385
  84. package/dist/services/ChainsService.js +29 -89
  85. package/dist/services/ConfigService.js +47 -86
  86. package/dist/typeguards.js +13 -21
  87. package/dist/types/ERC20.js +1 -4
  88. package/dist/types/index.js +4 -22
  89. package/dist/types/internal.types.js +1 -2
  90. package/dist/utils/errors.js +47 -93
  91. package/dist/utils/getProvider.js +3 -7
  92. package/dist/utils/multicall.js +61 -117
  93. package/dist/utils/parseError.js +73 -141
  94. package/dist/utils/preRestart.js +14 -21
  95. package/dist/utils/utils.js +47 -130
  96. package/package.json +34 -11
@@ -1,15 +1,3 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,222 +7,151 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
20
8
  });
21
9
  };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
51
- };
52
- Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.SwapExecutionManager = void 0;
54
- var ethers_1 = require("ethers");
55
- var ApiService_1 = __importDefault(require("../../services/ApiService"));
56
- var ChainsService_1 = __importDefault(require("../../services/ChainsService"));
57
- var errors_1 = require("../../utils/errors");
58
- var getProvider_1 = require("../../utils/getProvider");
59
- var parseError_1 = require("../../utils/parseError");
60
- var utils_1 = require("../../utils/utils");
61
- var allowance_execute_1 = require("../allowance.execute");
62
- var balanceCheck_execute_1 = require("../balanceCheck.execute");
63
- var stepComparison_1 = require("../stepComparison");
64
- var switchChain_1 = require("../switchChain");
65
- var utils_2 = require("../utils");
66
- var SwapExecutionManager = /** @class */ (function () {
67
- function SwapExecutionManager() {
68
- var _this = this;
10
+ import { constants } from 'ethers';
11
+ import ApiService from '../../services/ApiService';
12
+ import ChainsService from '../../services/ChainsService';
13
+ import { LifiErrorCode, TransactionError } from '../../utils/errors';
14
+ import { getProvider } from '../../utils/getProvider';
15
+ import { getTransactionFailedMessage, parseError } from '../../utils/parseError';
16
+ import { personalizeStep } from '../../utils/utils';
17
+ import { checkAllowance } from '../allowance.execute';
18
+ import { balanceCheck } from '../balanceCheck.execute';
19
+ import { stepComparison } from '../stepComparison';
20
+ import { switchChain } from '../switchChain';
21
+ import { waitForReceivingTransaction } from '../utils';
22
+ export class SwapExecutionManager {
23
+ constructor() {
69
24
  this.shouldContinue = true;
70
- this.setShouldContinue = function (val) {
71
- _this.shouldContinue = val;
25
+ this.setShouldContinue = (val) => {
26
+ this.shouldContinue = val;
72
27
  };
73
- this.execute = function (_a) {
74
- var signer = _a.signer, step = _a.step, statusManager = _a.statusManager, settings = _a.settings;
75
- return __awaiter(_this, void 0, void 0, function () {
76
- var action, estimate, chainsService, fromChain, swapProcess, tx, personalizedStep, updatedStep, _b, transactionRequest, updatedSigner, e_1, error, receipt, e_2, error, statusResponse, e_3;
77
- var _c, _d, _e, _f;
78
- return __generator(this, function (_g) {
79
- switch (_g.label) {
80
- case 0:
81
- action = step.action, estimate = step.estimate;
82
- step.execution = statusManager.initExecutionObject(step);
83
- chainsService = ChainsService_1.default.getInstance();
84
- return [4 /*yield*/, chainsService.getChainById(action.fromChainId)
85
- // Approval
86
- ];
87
- case 1:
88
- fromChain = _g.sent();
89
- if (!(action.fromToken.address !== ethers_1.constants.AddressZero)) return [3 /*break*/, 3];
90
- return [4 /*yield*/, (0, allowance_execute_1.checkAllowance)(signer, step, fromChain, action.fromToken, action.fromAmount, estimate.approvalAddress, statusManager, settings.infiniteApproval, this.shouldContinue)];
91
- case 2:
92
- _g.sent();
93
- _g.label = 3;
94
- case 3:
95
- swapProcess = statusManager.findOrCreateProcess('SWAP', step);
96
- _g.label = 4;
97
- case 4:
98
- _g.trys.push([4, 14, , 16]);
99
- if (!swapProcess.txHash) return [3 /*break*/, 6];
100
- return [4 /*yield*/, (0, getProvider_1.getProvider)(signer).getTransaction(swapProcess.txHash)];
101
- case 5:
102
- // -> restore existing tx
103
- tx = _g.sent();
104
- return [3 /*break*/, 13];
105
- case 6:
106
- // -> check balance
107
- return [4 /*yield*/, (0, balanceCheck_execute_1.balanceCheck)(signer, step)
108
- // -> get tx from backend
109
- ];
110
- case 7:
111
- // -> check balance
112
- _g.sent();
113
- return [4 /*yield*/, (0, utils_1.personalizeStep)(signer, step)];
114
- case 8:
115
- personalizedStep = _g.sent();
116
- return [4 /*yield*/, ApiService_1.default.getStepTransaction(personalizedStep)];
117
- case 9:
118
- updatedStep = _g.sent();
119
- _b = [{}];
120
- return [4 /*yield*/, (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.shouldContinue)];
121
- case 10:
122
- step = __assign.apply(void 0, [__assign.apply(void 0, _b.concat([(_g.sent())])), { execution: step.execution }]);
123
- transactionRequest = step.transactionRequest;
124
- if (!transactionRequest) {
125
- throw new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
126
- }
127
- return [4 /*yield*/, (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.shouldContinue)];
128
- case 11:
129
- updatedSigner = _g.sent();
130
- if (!updatedSigner) {
131
- // chain switch was not successful, stop execution here
132
- return [2 /*return*/, step.execution];
133
- }
134
- signer = updatedSigner;
135
- // -> set step.execution
136
- swapProcess = swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
137
- if (!this.shouldContinue) {
138
- return [2 /*return*/, step.execution]; // stop before user interaction is needed
139
- }
140
- return [4 /*yield*/, signer.sendTransaction(transactionRequest)];
141
- case 12:
142
- // -> submit tx
143
- tx = _g.sent();
144
- _g.label = 13;
145
- case 13: return [3 /*break*/, 16];
146
- case 14:
147
- e_1 = _g.sent();
148
- return [4 /*yield*/, (0, parseError_1.parseError)(e_1, step, swapProcess)];
149
- case 15:
150
- error = _g.sent();
151
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
152
- error: {
153
- message: error.message,
154
- htmlMessage: error.htmlMessage,
155
- code: error.code,
156
- },
157
- });
158
- statusManager.updateExecution(step, 'FAILED');
159
- throw error;
160
- case 16:
161
- // Wait for Transaction
162
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
163
- txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash,
164
- txHash: tx.hash,
165
- });
166
- _g.label = 17;
167
- case 17:
168
- _g.trys.push([17, 19, , 23]);
169
- return [4 /*yield*/, tx.wait()];
170
- case 18:
171
- receipt = _g.sent();
172
- return [3 /*break*/, 23];
173
- case 19:
174
- e_2 = _g.sent();
175
- if (!(e_2.code === 'TRANSACTION_REPLACED' && e_2.replacement)) return [3 /*break*/, 20];
176
- receipt = e_2.replacement;
177
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
178
- txHash: e_2.replacement.hash,
179
- txLink: fromChain.metamask.blockExplorerUrls[0] +
180
- 'tx/' +
181
- e_2.replacement.hash,
182
- });
183
- return [3 /*break*/, 22];
184
- case 20: return [4 /*yield*/, (0, parseError_1.parseError)(e_2)];
185
- case 21:
186
- error = _g.sent();
187
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
188
- error: {
189
- message: error.message,
190
- htmlMessage: error.htmlMessage,
191
- code: error.code,
192
- },
193
- });
194
- statusManager.updateExecution(step, 'FAILED');
195
- throw error;
196
- case 22: return [3 /*break*/, 23];
197
- case 23:
198
- _g.trys.push([23, 25, , 26]);
199
- if (!swapProcess.txHash) {
200
- throw new Error('Transaction hash is undefined.');
201
- }
202
- return [4 /*yield*/, (0, utils_2.waitForReceivingTransaction)(swapProcess.txHash, statusManager, swapProcess.type, step)];
203
- case 24:
204
- statusResponse = _g.sent();
205
- return [3 /*break*/, 26];
206
- case 25:
207
- e_3 = _g.sent();
208
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
209
- error: {
210
- code: errors_1.LifiErrorCode.TransactionFailed,
211
- message: 'Failed while waiting for receiving chain.',
212
- htmlMessage: (0, parseError_1.getTransactionFailedMessage)(step, swapProcess.txLink),
213
- },
214
- });
215
- statusManager.updateExecution(step, 'FAILED');
216
- throw e_3;
217
- case 26:
218
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'DONE', {
219
- txHash: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.txHash,
220
- txLink: fromChain.metamask.blockExplorerUrls[0] +
221
- 'tx/' +
222
- ((_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.txHash),
223
- });
224
- statusManager.updateExecution(step, 'DONE', {
225
- fromAmount: statusResponse.sending.amount,
226
- toAmount: (_e = statusResponse.receiving) === null || _e === void 0 ? void 0 : _e.amount,
227
- toToken: (_f = statusResponse.receiving) === null || _f === void 0 ? void 0 : _f.token,
228
- gasUsed: statusResponse.sending.gasUsed,
229
- gasPrice: statusResponse.sending.gasPrice,
230
- });
231
- // DONE
232
- return [2 /*return*/, step.execution];
28
+ this.execute = ({ signer, step, statusManager, settings, }) => __awaiter(this, void 0, void 0, function* () {
29
+ // setup
30
+ var _a, _b, _c, _d;
31
+ const { action, estimate } = step;
32
+ step.execution = statusManager.initExecutionObject(step);
33
+ const chainsService = ChainsService.getInstance();
34
+ const fromChain = yield chainsService.getChainById(action.fromChainId);
35
+ // Approval
36
+ if (action.fromToken.address !== constants.AddressZero) {
37
+ yield checkAllowance(signer, step, fromChain, action.fromToken, action.fromAmount, estimate.approvalAddress, statusManager, settings.infiniteApproval, this.shouldContinue);
38
+ }
39
+ // Start Swap
40
+ // -> set step.execution
41
+ let swapProcess = statusManager.findOrCreateProcess('SWAP', step);
42
+ // -> swapping
43
+ let tx;
44
+ try {
45
+ if (swapProcess.txHash) {
46
+ // -> restore existing tx
47
+ tx = yield getProvider(signer).getTransaction(swapProcess.txHash);
48
+ }
49
+ else {
50
+ // -> check balance
51
+ yield balanceCheck(signer, step);
52
+ // -> get tx from backend
53
+ const personalizedStep = yield personalizeStep(signer, step);
54
+ const updatedStep = yield ApiService.getStepTransaction(personalizedStep);
55
+ step = Object.assign(Object.assign({}, (yield stepComparison(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.shouldContinue))), { execution: step.execution });
56
+ const { transactionRequest } = step;
57
+ if (!transactionRequest) {
58
+ throw new TransactionError(LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
59
+ }
60
+ // make sure that chain is still correct
61
+ const updatedSigner = yield switchChain(signer, statusManager, step, settings.switchChainHook, this.shouldContinue);
62
+ if (!updatedSigner) {
63
+ // chain switch was not successful, stop execution here
64
+ return step.execution;
233
65
  }
66
+ signer = updatedSigner;
67
+ // -> set step.execution
68
+ swapProcess = swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
69
+ if (!this.shouldContinue) {
70
+ return step.execution; // stop before user interaction is needed
71
+ }
72
+ // -> submit tx
73
+ tx = yield signer.sendTransaction(transactionRequest);
74
+ }
75
+ }
76
+ catch (e) {
77
+ const error = yield parseError(e, step, swapProcess);
78
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
79
+ error: {
80
+ message: error.message,
81
+ htmlMessage: error.htmlMessage,
82
+ code: error.code,
83
+ },
234
84
  });
85
+ statusManager.updateExecution(step, 'FAILED');
86
+ throw error;
87
+ }
88
+ // Wait for Transaction
89
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
90
+ txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash,
91
+ txHash: tx.hash,
235
92
  });
236
- };
93
+ // -> waiting
94
+ let receipt;
95
+ try {
96
+ receipt = yield tx.wait();
97
+ }
98
+ catch (e) {
99
+ // -> set status
100
+ if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
101
+ receipt = e.replacement;
102
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
103
+ txHash: e.replacement.hash,
104
+ txLink: fromChain.metamask.blockExplorerUrls[0] +
105
+ 'tx/' +
106
+ e.replacement.hash,
107
+ });
108
+ }
109
+ else {
110
+ const error = yield parseError(e);
111
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
112
+ error: {
113
+ message: error.message,
114
+ htmlMessage: error.htmlMessage,
115
+ code: error.code,
116
+ },
117
+ });
118
+ statusManager.updateExecution(step, 'FAILED');
119
+ throw error;
120
+ }
121
+ }
122
+ let statusResponse;
123
+ try {
124
+ if (!swapProcess.txHash) {
125
+ throw new Error('Transaction hash is undefined.');
126
+ }
127
+ statusResponse = yield waitForReceivingTransaction(swapProcess.txHash, statusManager, swapProcess.type, step);
128
+ }
129
+ catch (e) {
130
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
131
+ error: {
132
+ code: LifiErrorCode.TransactionFailed,
133
+ message: 'Failed while waiting for receiving chain.',
134
+ htmlMessage: getTransactionFailedMessage(step, swapProcess.txLink),
135
+ },
136
+ });
137
+ statusManager.updateExecution(step, 'FAILED');
138
+ throw e;
139
+ }
140
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'DONE', {
141
+ txHash: (_a = statusResponse.receiving) === null || _a === void 0 ? void 0 : _a.txHash,
142
+ txLink: fromChain.metamask.blockExplorerUrls[0] +
143
+ 'tx/' +
144
+ ((_b = statusResponse.receiving) === null || _b === void 0 ? void 0 : _b.txHash),
145
+ });
146
+ statusManager.updateExecution(step, 'DONE', {
147
+ fromAmount: statusResponse.sending.amount,
148
+ toAmount: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.amount,
149
+ toToken: (_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.token,
150
+ gasUsed: statusResponse.sending.gasUsed,
151
+ gasPrice: statusResponse.sending.gasPrice,
152
+ });
153
+ // DONE
154
+ return step.execution;
155
+ });
237
156
  }
238
- return SwapExecutionManager;
239
- }());
240
- exports.SwapExecutionManager = SwapExecutionManager;
157
+ }
@@ -1,17 +1 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./StatusManager"), exports);
1
+ export * from './StatusManager';
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,38 +7,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.stepComparison = void 0;
40
- var errors_1 = require("../utils/errors");
41
- var parseError_1 = require("../utils/parseError");
42
- var utils_1 = require("./utils");
10
+ import { LifiErrorCode, TransactionError } from '../utils/errors';
11
+ import { getSlippageNotMetMessage } from '../utils/parseError';
12
+ import { updatedStepMeetsSlippageConditions } from './utils';
43
13
  /**
44
14
  * This method checks whether the new and updated Step meets the required slippage conditions.
45
15
  * If yes it returns the updated Step.
@@ -51,31 +21,22 @@ var utils_1 = require("./utils");
51
21
  * @param acceptSlippageUpdateHook
52
22
  * @param allowUserInteraction
53
23
  */
54
- var stepComparison = function (statusManager, oldStep, newStep, acceptSlippageUpdateHook, allowUserInteraction) { return __awaiter(void 0, void 0, void 0, function () {
55
- var allowStepUpdate;
56
- return __generator(this, function (_a) {
57
- switch (_a.label) {
58
- case 0:
59
- if ((0, utils_1.updatedStepMeetsSlippageConditions)(oldStep, newStep)) {
60
- return [2 /*return*/, statusManager.updateStepInRoute(newStep)];
61
- }
62
- if (!allowUserInteraction) return [3 /*break*/, 2];
63
- return [4 /*yield*/, acceptSlippageUpdateHook({
64
- oldToAmount: oldStep.estimate.toAmount,
65
- newToAmount: newStep.estimate.toAmount,
66
- toToken: newStep.action.toToken,
67
- oldSlippage: oldStep.action.slippage,
68
- newSlippage: newStep.action.slippage,
69
- })];
70
- case 1:
71
- allowStepUpdate = _a.sent();
72
- _a.label = 2;
73
- case 2:
74
- if (!allowStepUpdate) {
75
- throw new errors_1.TransactionError(errors_1.LifiErrorCode.SlippageNotMet, 'Slippage conditions not met!', (0, parseError_1.getSlippageNotMetMessage)(oldStep));
76
- }
77
- return [2 /*return*/, statusManager.updateStepInRoute(newStep)];
78
- }
79
- });
80
- }); };
81
- exports.stepComparison = stepComparison;
24
+ export const stepComparison = (statusManager, oldStep, newStep, acceptSlippageUpdateHook, allowUserInteraction) => __awaiter(void 0, void 0, void 0, function* () {
25
+ if (updatedStepMeetsSlippageConditions(oldStep, newStep)) {
26
+ return statusManager.updateStepInRoute(newStep);
27
+ }
28
+ let allowStepUpdate;
29
+ if (allowUserInteraction) {
30
+ allowStepUpdate = yield acceptSlippageUpdateHook({
31
+ oldToAmount: oldStep.estimate.toAmount,
32
+ newToAmount: newStep.estimate.toAmount,
33
+ toToken: newStep.action.toToken,
34
+ oldSlippage: oldStep.action.slippage,
35
+ newSlippage: newStep.action.slippage,
36
+ });
37
+ }
38
+ if (!allowStepUpdate) {
39
+ throw new TransactionError(LifiErrorCode.SlippageNotMet, 'Slippage conditions not met!', getSlippageNotMetMessage(oldStep));
40
+ }
41
+ return statusManager.updateStepInRoute(newStep);
42
+ });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,36 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.switchChain = void 0;
40
- var errors_1 = require("../utils/errors");
10
+ import { LifiErrorCode, ProviderError } from '../utils/errors';
41
11
  /**
42
12
  * This method checks whether the signer is configured for the correct chain.
43
13
  * If yes it returns the signer.
@@ -49,54 +19,36 @@ var errors_1 = require("../utils/errors");
49
19
  * @param switchChainHook
50
20
  * @param allowUserInteraction
51
21
  */
52
- var switchChain = function (signer, statusManager, step, switchChainHook, allowUserInteraction) { return __awaiter(void 0, void 0, void 0, function () {
53
- var switchProcess, updatedSigner, _a, error_1;
54
- return __generator(this, function (_b) {
55
- switch (_b.label) {
56
- case 0: return [4 /*yield*/, signer.getChainId()];
57
- case 1:
58
- // if we are already on the correct chain we can proceed directly
59
- if ((_b.sent()) === step.action.fromChainId) {
60
- return [2 /*return*/, signer];
61
- }
62
- // -> set status message
63
- step.execution = statusManager.initExecutionObject(step);
64
- statusManager.updateExecution(step, 'CHAIN_SWITCH_REQUIRED');
65
- switchProcess = statusManager.findOrCreateProcess('SWITCH_CHAIN', step, 'PENDING');
66
- if (!allowUserInteraction) {
67
- return [2 /*return*/];
68
- }
69
- _b.label = 2;
70
- case 2:
71
- _b.trys.push([2, 6, , 7]);
72
- return [4 /*yield*/, switchChainHook(step.action.fromChainId)];
73
- case 3:
74
- updatedSigner = _b.sent();
75
- _a = !updatedSigner;
76
- if (_a) return [3 /*break*/, 5];
77
- return [4 /*yield*/, updatedSigner.getChainId()];
78
- case 4:
79
- _a = (_b.sent()) !== step.action.fromChainId;
80
- _b.label = 5;
81
- case 5:
82
- if (_a) {
83
- throw new errors_1.ProviderError(errors_1.LifiErrorCode.ChainSwitchError, 'Chain switch required.');
84
- }
85
- switchProcess = statusManager.updateProcess(step, switchProcess.type, 'DONE');
86
- statusManager.updateExecution(step, 'PENDING');
87
- return [2 /*return*/, updatedSigner];
88
- case 6:
89
- error_1 = _b.sent();
90
- switchProcess = statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
91
- error: {
92
- message: error_1.message,
93
- code: error_1.code,
94
- },
95
- });
96
- statusManager.updateExecution(step, 'FAILED');
97
- throw error_1;
98
- case 7: return [2 /*return*/];
22
+ export const switchChain = (signer, statusManager, step, switchChainHook, allowUserInteraction) => __awaiter(void 0, void 0, void 0, function* () {
23
+ // if we are already on the correct chain we can proceed directly
24
+ if ((yield signer.getChainId()) === step.action.fromChainId) {
25
+ return signer;
26
+ }
27
+ // -> set status message
28
+ step.execution = statusManager.initExecutionObject(step);
29
+ statusManager.updateExecution(step, 'CHAIN_SWITCH_REQUIRED');
30
+ let switchProcess = statusManager.findOrCreateProcess('SWITCH_CHAIN', step, 'PENDING');
31
+ if (!allowUserInteraction) {
32
+ return;
33
+ }
34
+ try {
35
+ const updatedSigner = yield switchChainHook(step.action.fromChainId);
36
+ const updatedChainId = yield (updatedSigner === null || updatedSigner === void 0 ? void 0 : updatedSigner.getChainId());
37
+ if (updatedChainId !== step.action.fromChainId) {
38
+ throw new ProviderError(LifiErrorCode.ChainSwitchError, 'Chain switch required.');
99
39
  }
100
- });
101
- }); };
102
- exports.switchChain = switchChain;
40
+ switchProcess = statusManager.updateProcess(step, switchProcess.type, 'DONE');
41
+ statusManager.updateExecution(step, 'PENDING');
42
+ return updatedSigner;
43
+ }
44
+ catch (error) {
45
+ switchProcess = statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
46
+ error: {
47
+ message: error.message,
48
+ code: error.code,
49
+ },
50
+ });
51
+ statusManager.updateExecution(step, 'FAILED');
52
+ throw error;
53
+ }
54
+ });