@lifi/sdk 1.1.2 → 1.1.3
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.
- package/CHANGELOG.md +7 -0
- package/dist/Lifi.js +179 -285
- package/dist/allowance/index.js +56 -148
- package/dist/allowance/utils.js +51 -116
- package/dist/balances/index.js +29 -92
- package/dist/balances/utils.js +108 -218
- package/dist/connectors.js +50 -133
- package/dist/execution/StatusManager.js +34 -41
- package/dist/execution/StepExecutor.js +54 -123
- package/dist/execution/allowance.execute.js +76 -142
- package/dist/execution/balanceCheck.execute.js +29 -74
- package/dist/execution/bridges/bridge.execute.js +132 -221
- package/dist/execution/exchanges/swap.execute.js +142 -225
- package/dist/execution/index.js +1 -17
- package/dist/execution/stepComparison.js +22 -61
- package/dist/execution/switchChain.js +33 -81
- package/dist/execution/utils.js +60 -119
- package/dist/helpers.js +15 -53
- package/dist/index.js +6 -25
- package/dist/services/ApiService.js +248 -385
- package/dist/services/ChainsService.js +29 -89
- package/dist/services/ConfigService.js +47 -86
- package/dist/typeguards.js +13 -21
- package/dist/types/ERC20.js +1 -4
- package/dist/types/index.js +4 -22
- package/dist/types/internal.types.js +1 -2
- package/dist/utils/errors.js +47 -93
- package/dist/utils/getProvider.js +3 -7
- package/dist/utils/multicall.js +61 -117
- package/dist/utils/parseError.js +73 -141
- package/dist/utils/preRestart.js +14 -21
- package/dist/utils/utils.js +47 -130
- package/package.json +1 -1
|
@@ -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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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 =
|
|
71
|
-
|
|
25
|
+
this.setShouldContinue = (val) => {
|
|
26
|
+
this.shouldContinue = val;
|
|
72
27
|
};
|
|
73
|
-
this.execute = function (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
239
|
-
}());
|
|
240
|
-
exports.SwapExecutionManager = SwapExecutionManager;
|
|
157
|
+
}
|
package/dist/execution/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
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
|
+
});
|