@lifi/sdk 1.1.1 → 1.1.4
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 +16 -0
- package/dist/Lifi.d.ts +1 -1
- 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 +38 -40
- 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 +3 -3
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var types_1 = require("../types");
|
|
5
|
-
var utils_1 = require("../utils/utils");
|
|
6
|
-
var utils_2 = require("./utils");
|
|
1
|
+
import { emptyExecution, } from '../types';
|
|
2
|
+
import { deepClone } from '../utils/utils';
|
|
3
|
+
import { getProcessMessage } from './utils';
|
|
7
4
|
/**
|
|
8
5
|
* Manages status updates of a route and provides various functions for tracking processes
|
|
9
6
|
* @param {Route} route The route this StatusManger belongs to.
|
|
@@ -11,21 +8,25 @@ var utils_2 = require("./utils");
|
|
|
11
8
|
* @param {InternalUpdateRouteCallback} internalUpdateRouteCallback Internal callback to propage route changes.
|
|
12
9
|
* @return {StatusManager} An instance of StatusManager.
|
|
13
10
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var _this = this;
|
|
11
|
+
export class StatusManager {
|
|
12
|
+
constructor(route, settings, internalUpdateRouteCallback) {
|
|
17
13
|
this.shouldUpdate = true;
|
|
18
14
|
/**
|
|
19
15
|
* Initializes the execution object of a Step.
|
|
20
16
|
* @param {Step} step The current step in execution
|
|
21
17
|
* @return {Execution} The initialized execution object for this step and a function to update this step
|
|
22
18
|
*/
|
|
23
|
-
this.initExecutionObject =
|
|
24
|
-
|
|
19
|
+
this.initExecutionObject = (step) => {
|
|
20
|
+
const currentExecution = step.execution || deepClone(emptyExecution);
|
|
25
21
|
if (!step.execution) {
|
|
26
22
|
step.execution = currentExecution;
|
|
27
23
|
step.execution.status = 'PENDING';
|
|
28
|
-
|
|
24
|
+
this.updateStepInRoute(step);
|
|
25
|
+
}
|
|
26
|
+
// Change status to PENDING after resuming from FAILED
|
|
27
|
+
if (currentExecution.status === 'FAILED') {
|
|
28
|
+
currentExecution.status = 'PENDING';
|
|
29
|
+
this.updateStepInRoute(step);
|
|
29
30
|
}
|
|
30
31
|
return currentExecution;
|
|
31
32
|
};
|
|
@@ -36,22 +37,22 @@ var StatusManager = /** @class */ (function () {
|
|
|
36
37
|
* @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
|
|
37
38
|
* @return {Process}
|
|
38
39
|
*/
|
|
39
|
-
this.findOrCreateProcess =
|
|
40
|
+
this.findOrCreateProcess = (type, step, status) => {
|
|
40
41
|
if (!step.execution || !step.execution.process) {
|
|
41
42
|
throw new Error("Execution hasn't been initialized.");
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
+
const process = step.execution.process.find((p) => p.type === type);
|
|
44
45
|
if (process) {
|
|
45
46
|
return process;
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
+
const newProcess = {
|
|
48
49
|
type: type,
|
|
49
50
|
startedAt: Date.now(),
|
|
50
|
-
message:
|
|
51
|
+
message: getProcessMessage(type, status !== null && status !== void 0 ? status : 'STARTED'),
|
|
51
52
|
status: status !== null && status !== void 0 ? status : 'STARTED',
|
|
52
53
|
};
|
|
53
54
|
step.execution.process.push(newProcess);
|
|
54
|
-
|
|
55
|
+
this.updateStepInRoute(step);
|
|
55
56
|
return newProcess;
|
|
56
57
|
};
|
|
57
58
|
/**
|
|
@@ -62,12 +63,12 @@ var StatusManager = /** @class */ (function () {
|
|
|
62
63
|
* @param {object} [params] Additional parameters to append to the process.
|
|
63
64
|
* @return {Process} The update process
|
|
64
65
|
*/
|
|
65
|
-
this.updateProcess =
|
|
66
|
+
this.updateProcess = (step, type, status, params) => {
|
|
66
67
|
var _a;
|
|
67
68
|
if (!step.execution) {
|
|
68
69
|
throw new Error("Can't update an empty step execution.");
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
+
const currentProcess = (_a = step === null || step === void 0 ? void 0 : step.execution) === null || _a === void 0 ? void 0 : _a.process.find((p) => p.type === type);
|
|
71
72
|
if (!currentProcess) {
|
|
72
73
|
throw new Error("Can't find a process for the given type.");
|
|
73
74
|
}
|
|
@@ -95,15 +96,14 @@ var StatusManager = /** @class */ (function () {
|
|
|
95
96
|
break;
|
|
96
97
|
}
|
|
97
98
|
currentProcess.status = status;
|
|
98
|
-
currentProcess.message =
|
|
99
|
+
currentProcess.message = getProcessMessage(type, status);
|
|
99
100
|
// set extra parameters or overwritte the standard params set in the switch statement
|
|
100
101
|
if (params) {
|
|
101
|
-
for (
|
|
102
|
-
var _c = _b[_i], key = _c[0], value = _c[1];
|
|
102
|
+
for (const [key, value] of Object.entries(params)) {
|
|
103
103
|
currentProcess[key] = value;
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
-
|
|
106
|
+
this.updateStepInRoute(step); // updates the step in the route
|
|
107
107
|
return currentProcess;
|
|
108
108
|
};
|
|
109
109
|
/**
|
|
@@ -112,26 +112,26 @@ var StatusManager = /** @class */ (function () {
|
|
|
112
112
|
* @param {ProcessType} type The process type to remove
|
|
113
113
|
* @return {void}
|
|
114
114
|
*/
|
|
115
|
-
this.removeProcess =
|
|
115
|
+
this.removeProcess = (step, type) => {
|
|
116
116
|
if (!step.execution) {
|
|
117
117
|
throw new Error("Execution hasn't been initialized.");
|
|
118
118
|
}
|
|
119
|
-
|
|
119
|
+
const index = step.execution.process.findIndex((p) => p.type === type);
|
|
120
120
|
step.execution.process.splice(index, 1);
|
|
121
|
-
|
|
121
|
+
this.updateStepInRoute(step);
|
|
122
122
|
};
|
|
123
|
-
this.updateStepInRoute =
|
|
124
|
-
if (!
|
|
123
|
+
this.updateStepInRoute = (step) => {
|
|
124
|
+
if (!this.shouldUpdate) {
|
|
125
125
|
return step;
|
|
126
126
|
}
|
|
127
|
-
|
|
127
|
+
const stepIndex = this.route.steps.findIndex((routeStep) => routeStep.id === step.id);
|
|
128
128
|
if (stepIndex === -1) {
|
|
129
129
|
throw new Error("Couldn't find a step to update.");
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return
|
|
131
|
+
this.route.steps[stepIndex] = Object.assign(this.route.steps[stepIndex], step);
|
|
132
|
+
this.settings.updateCallback(this.route);
|
|
133
|
+
this.internalUpdateRouteCallback(this.route);
|
|
134
|
+
return this.route.steps[stepIndex];
|
|
135
135
|
};
|
|
136
136
|
this.route = route;
|
|
137
137
|
this.settings = settings;
|
|
@@ -144,7 +144,7 @@ var StatusManager = /** @class */ (function () {
|
|
|
144
144
|
* @param {Receipt} receipt Optional. Information about received tokens
|
|
145
145
|
* @return {Step} The step with the updated execution object
|
|
146
146
|
*/
|
|
147
|
-
|
|
147
|
+
updateExecution(step, status, receipt) {
|
|
148
148
|
if (!step.execution) {
|
|
149
149
|
throw Error("Can't update empty execution.");
|
|
150
150
|
}
|
|
@@ -156,10 +156,8 @@ var StatusManager = /** @class */ (function () {
|
|
|
156
156
|
}
|
|
157
157
|
this.updateStepInRoute(step);
|
|
158
158
|
return step;
|
|
159
|
-
}
|
|
160
|
-
|
|
159
|
+
}
|
|
160
|
+
setShouldUpdate(value) {
|
|
161
161
|
this.shouldUpdate = value;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
}());
|
|
165
|
-
exports.StatusManager = StatusManager;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -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,121 +7,64 @@ 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
|
-
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.StepExecutor = void 0;
|
|
51
|
-
var bridge_execute_1 = require("./bridges/bridge.execute");
|
|
52
|
-
var swap_execute_1 = require("./exchanges/swap.execute");
|
|
53
|
-
var switchChain_1 = require("./switchChain");
|
|
54
|
-
var defaultExecutionHaltSettings = {
|
|
10
|
+
import { BridgeExecutionManager } from './bridges/bridge.execute';
|
|
11
|
+
import { SwapExecutionManager } from './exchanges/swap.execute';
|
|
12
|
+
import { switchChain } from './switchChain';
|
|
13
|
+
const defaultExecutionHaltSettings = {
|
|
55
14
|
allowUpdates: true,
|
|
56
15
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
this.bridgeExecutionManager = new bridge_execute_1.BridgeExecutionManager();
|
|
16
|
+
export class StepExecutor {
|
|
17
|
+
constructor(statusManager, settings) {
|
|
18
|
+
this.swapExecutionManager = new SwapExecutionManager();
|
|
19
|
+
this.bridgeExecutionManager = new BridgeExecutionManager();
|
|
62
20
|
this.executionStopped = false;
|
|
63
|
-
this.stopStepExecution =
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
21
|
+
this.stopStepExecution = (settings) => {
|
|
22
|
+
const haltingSettings = Object.assign(Object.assign({}, defaultExecutionHaltSettings), settings);
|
|
23
|
+
this.swapExecutionManager.setShouldContinue(false);
|
|
24
|
+
this.bridgeExecutionManager.setShouldContinue(false);
|
|
25
|
+
this.statusManager.setShouldUpdate(haltingSettings.allowUpdates);
|
|
26
|
+
this.executionStopped = true;
|
|
69
27
|
};
|
|
70
|
-
this.executeStep =
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
step: step,
|
|
110
|
-
settings: this.settings,
|
|
111
|
-
statusManager: this.statusManager,
|
|
112
|
-
};
|
|
113
|
-
return [4 /*yield*/, this.swapExecutionManager.execute(swapParams)];
|
|
114
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}); };
|
|
118
|
-
this.executeCross = function (signer, step) { return __awaiter(_this, void 0, void 0, function () {
|
|
119
|
-
var crossParams;
|
|
120
|
-
return __generator(this, function (_a) {
|
|
121
|
-
switch (_a.label) {
|
|
122
|
-
case 0:
|
|
123
|
-
crossParams = {
|
|
124
|
-
signer: signer,
|
|
125
|
-
step: step,
|
|
126
|
-
settings: this.settings,
|
|
127
|
-
statusManager: this.statusManager,
|
|
128
|
-
};
|
|
129
|
-
return [4 /*yield*/, this.bridgeExecutionManager.execute(crossParams)];
|
|
130
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}); };
|
|
28
|
+
this.executeStep = (signer, step) => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
// check if signer is for correct chain
|
|
30
|
+
const updatedSigner = yield switchChain(signer, this.statusManager, step, this.settings.switchChainHook, !this.executionStopped);
|
|
31
|
+
if (!updatedSigner) {
|
|
32
|
+
// chain switch was not successful, stop execution here
|
|
33
|
+
return step;
|
|
34
|
+
}
|
|
35
|
+
signer = updatedSigner;
|
|
36
|
+
switch (step.type) {
|
|
37
|
+
case 'lifi':
|
|
38
|
+
case 'cross':
|
|
39
|
+
yield this.executeCross(signer, step);
|
|
40
|
+
break;
|
|
41
|
+
case 'swap':
|
|
42
|
+
yield this.executeSwap(signer, step);
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error('Unsupported step type.');
|
|
46
|
+
}
|
|
47
|
+
return step;
|
|
48
|
+
});
|
|
49
|
+
this.executeSwap = (signer, step) => __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const swapParams = {
|
|
51
|
+
signer,
|
|
52
|
+
step,
|
|
53
|
+
settings: this.settings,
|
|
54
|
+
statusManager: this.statusManager,
|
|
55
|
+
};
|
|
56
|
+
return yield this.swapExecutionManager.execute(swapParams);
|
|
57
|
+
});
|
|
58
|
+
this.executeCross = (signer, step) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const crossParams = {
|
|
60
|
+
signer,
|
|
61
|
+
step,
|
|
62
|
+
settings: this.settings,
|
|
63
|
+
statusManager: this.statusManager,
|
|
64
|
+
};
|
|
65
|
+
return yield this.bridgeExecutionManager.execute(crossParams);
|
|
66
|
+
});
|
|
134
67
|
this.statusManager = statusManager;
|
|
135
68
|
this.settings = settings;
|
|
136
69
|
}
|
|
137
|
-
|
|
138
|
-
}());
|
|
139
|
-
exports.StepExecutor = StepExecutor;
|
|
70
|
+
}
|
|
@@ -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,149 +7,84 @@ 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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.checkAllowance = void 0;
|
|
43
|
-
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
-
var ethers_1 = require("ethers");
|
|
45
|
-
var utils_1 = require("../allowance/utils");
|
|
46
|
-
var getProvider_1 = require("../utils/getProvider");
|
|
47
|
-
var parseError_1 = require("../utils/parseError");
|
|
48
|
-
var checkAllowance = function (signer, step, chain, token, amount, spenderAddress, statusManager, infiniteApproval, allowUserInteraction
|
|
10
|
+
import BigNumber from 'bignumber.js';
|
|
11
|
+
import { constants } from 'ethers';
|
|
12
|
+
import { getApproved, setApproval } from '../allowance/utils';
|
|
13
|
+
import { getProvider } from '../utils/getProvider';
|
|
14
|
+
import { parseError } from '../utils/parseError';
|
|
15
|
+
export const checkAllowance = (signer, step, chain, token, amount, spenderAddress, statusManager, infiniteApproval = false, allowUserInteraction = false
|
|
49
16
|
// eslint-disable-next-line max-params
|
|
50
|
-
) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (!new bignumber_js_1.default(amount).gt(approved)) return [3 /*break*/, 8];
|
|
77
|
-
if (!allowUserInteraction) {
|
|
78
|
-
return [2 /*return*/];
|
|
79
|
-
}
|
|
80
|
-
approvalAmount = infiniteApproval
|
|
81
|
-
? ethers_1.constants.MaxUint256.toString()
|
|
82
|
-
: amount;
|
|
83
|
-
return [4 /*yield*/, (0, utils_1.setApproval)(signer, token.address, spenderAddress, approvalAmount)
|
|
84
|
-
// update currentExecution
|
|
85
|
-
];
|
|
86
|
-
case 6:
|
|
87
|
-
approveTx = _a.sent();
|
|
88
|
-
// update currentExecution
|
|
89
|
-
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'PENDING', {
|
|
90
|
-
txHash: approveTx.hash,
|
|
91
|
-
txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + approveTx.hash,
|
|
92
|
-
});
|
|
93
|
-
// wait for transcation
|
|
94
|
-
return [4 /*yield*/, approveTx.wait()];
|
|
95
|
-
case 7:
|
|
96
|
-
// wait for transcation
|
|
97
|
-
_a.sent();
|
|
98
|
-
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
99
|
-
return [3 /*break*/, 9];
|
|
100
|
-
case 8:
|
|
101
|
-
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
102
|
-
_a.label = 9;
|
|
103
|
-
case 9: return [3 /*break*/, 15];
|
|
104
|
-
case 10:
|
|
105
|
-
e_1 = _a.sent();
|
|
106
|
-
if (!(e_1.code === 'TRANSACTION_REPLACED' && e_1.replacement)) return [3 /*break*/, 12];
|
|
107
|
-
return [4 /*yield*/, transactionReplaced(e_1.replacement, allowanceProcess, step, chain, statusManager)];
|
|
108
|
-
case 11:
|
|
109
|
-
_a.sent();
|
|
110
|
-
return [3 /*break*/, 14];
|
|
111
|
-
case 12: return [4 /*yield*/, (0, parseError_1.parseError)(e_1, step, allowanceProcess)];
|
|
112
|
-
case 13:
|
|
113
|
-
error = _a.sent();
|
|
114
|
-
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'FAILED', {
|
|
115
|
-
error: {
|
|
116
|
-
message: error.message,
|
|
117
|
-
htmlMessage: error.htmlMessage,
|
|
118
|
-
code: error.code,
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
statusManager.updateExecution(step, 'FAILED');
|
|
122
|
-
throw error;
|
|
123
|
-
case 14: return [3 /*break*/, 15];
|
|
124
|
-
case 15: return [2 /*return*/];
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
exports.checkAllowance = checkAllowance;
|
|
130
|
-
var transactionReplaced = function (replacementTx, allowanceProcess, step, chain, statusManager) { return __awaiter(void 0, void 0, void 0, function () {
|
|
131
|
-
var e_2;
|
|
132
|
-
return __generator(this, function (_a) {
|
|
133
|
-
switch (_a.label) {
|
|
134
|
-
case 0:
|
|
135
|
-
_a.trys.push([0, 2, , 5]);
|
|
17
|
+
) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
// Ask user to set allowance
|
|
19
|
+
// -> set currentExecution
|
|
20
|
+
let allowanceProcess = statusManager.findOrCreateProcess('TOKEN_ALLOWANCE', step);
|
|
21
|
+
// -> check allowance
|
|
22
|
+
try {
|
|
23
|
+
if (allowanceProcess.txHash) {
|
|
24
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'PENDING');
|
|
25
|
+
yield getProvider(signer).waitForTransaction(allowanceProcess.txHash);
|
|
26
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
27
|
+
// TODO: Do we need this check?
|
|
28
|
+
}
|
|
29
|
+
else if (allowanceProcess.status === 'DONE') {
|
|
30
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const approved = yield getApproved(signer, token.address, spenderAddress);
|
|
34
|
+
if (new BigNumber(amount).gt(approved)) {
|
|
35
|
+
if (!allowUserInteraction) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const approvalAmount = infiniteApproval
|
|
39
|
+
? constants.MaxUint256.toString()
|
|
40
|
+
: amount;
|
|
41
|
+
const approveTx = yield setApproval(signer, token.address, spenderAddress, approvalAmount);
|
|
42
|
+
// update currentExecution
|
|
136
43
|
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'PENDING', {
|
|
137
|
-
txHash:
|
|
138
|
-
txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' +
|
|
44
|
+
txHash: approveTx.hash,
|
|
45
|
+
txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + approveTx.hash,
|
|
139
46
|
});
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
47
|
+
// wait for transcation
|
|
48
|
+
yield approveTx.wait();
|
|
49
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
143
52
|
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
144
|
-
|
|
145
|
-
case 2:
|
|
146
|
-
e_2 = _a.sent();
|
|
147
|
-
if (!(e_2.code === 'TRANSACTION_REPLACED' && e_2.replacement)) return [3 /*break*/, 4];
|
|
148
|
-
return [4 /*yield*/, transactionReplaced(e_2.replacement, allowanceProcess, step, chain, statusManager)];
|
|
149
|
-
case 3:
|
|
150
|
-
_a.sent();
|
|
151
|
-
_a.label = 4;
|
|
152
|
-
case 4: throw e_2;
|
|
153
|
-
case 5: return [2 /*return*/];
|
|
53
|
+
}
|
|
154
54
|
}
|
|
155
|
-
}
|
|
156
|
-
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
// -> set status
|
|
58
|
+
if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
|
|
59
|
+
yield transactionReplaced(e.replacement, allowanceProcess, step, chain, statusManager);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const error = yield parseError(e, step, allowanceProcess);
|
|
63
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'FAILED', {
|
|
64
|
+
error: {
|
|
65
|
+
message: error.message,
|
|
66
|
+
htmlMessage: error.htmlMessage,
|
|
67
|
+
code: error.code,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
statusManager.updateExecution(step, 'FAILED');
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const transactionReplaced = (replacementTx, allowanceProcess, step, chain, statusManager) => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
try {
|
|
77
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'PENDING', {
|
|
78
|
+
txHash: replacementTx.hash,
|
|
79
|
+
txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + replacementTx.hash,
|
|
80
|
+
});
|
|
81
|
+
yield replacementTx.wait();
|
|
82
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
|
|
86
|
+
yield transactionReplaced(e.replacement, allowanceProcess, step, chain, statusManager);
|
|
87
|
+
}
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
});
|