@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.
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StatusManager = void 0;
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,26 +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
- var StatusManager = /** @class */ (function () {
15
- function StatusManager(route, settings, internalUpdateRouteCallback) {
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 = function (step) {
24
- var currentExecution = step.execution || (0, utils_1.deepClone)(types_1.emptyExecution);
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
- _this.updateStepInRoute(step);
24
+ this.updateStepInRoute(step);
29
25
  }
30
26
  // Change status to PENDING after resuming from FAILED
31
27
  if (currentExecution.status === 'FAILED') {
32
28
  currentExecution.status = 'PENDING';
33
- _this.updateStepInRoute(step);
29
+ this.updateStepInRoute(step);
34
30
  }
35
31
  return currentExecution;
36
32
  };
@@ -41,22 +37,22 @@ var StatusManager = /** @class */ (function () {
41
37
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
42
38
  * @return {Process}
43
39
  */
44
- this.findOrCreateProcess = function (type, step, status) {
40
+ this.findOrCreateProcess = (type, step, status) => {
45
41
  if (!step.execution || !step.execution.process) {
46
42
  throw new Error("Execution hasn't been initialized.");
47
43
  }
48
- var process = step.execution.process.find(function (p) { return p.type === type; });
44
+ const process = step.execution.process.find((p) => p.type === type);
49
45
  if (process) {
50
46
  return process;
51
47
  }
52
- var newProcess = {
48
+ const newProcess = {
53
49
  type: type,
54
50
  startedAt: Date.now(),
55
- message: (0, utils_2.getProcessMessage)(type, status !== null && status !== void 0 ? status : 'STARTED'),
51
+ message: getProcessMessage(type, status !== null && status !== void 0 ? status : 'STARTED'),
56
52
  status: status !== null && status !== void 0 ? status : 'STARTED',
57
53
  };
58
54
  step.execution.process.push(newProcess);
59
- _this.updateStepInRoute(step);
55
+ this.updateStepInRoute(step);
60
56
  return newProcess;
61
57
  };
62
58
  /**
@@ -67,12 +63,12 @@ var StatusManager = /** @class */ (function () {
67
63
  * @param {object} [params] Additional parameters to append to the process.
68
64
  * @return {Process} The update process
69
65
  */
70
- this.updateProcess = function (step, type, status, params) {
66
+ this.updateProcess = (step, type, status, params) => {
71
67
  var _a;
72
68
  if (!step.execution) {
73
69
  throw new Error("Can't update an empty step execution.");
74
70
  }
75
- var currentProcess = (_a = step === null || step === void 0 ? void 0 : step.execution) === null || _a === void 0 ? void 0 : _a.process.find(function (p) { return p.type === type; });
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);
76
72
  if (!currentProcess) {
77
73
  throw new Error("Can't find a process for the given type.");
78
74
  }
@@ -100,15 +96,14 @@ var StatusManager = /** @class */ (function () {
100
96
  break;
101
97
  }
102
98
  currentProcess.status = status;
103
- currentProcess.message = (0, utils_2.getProcessMessage)(type, status);
99
+ currentProcess.message = getProcessMessage(type, status);
104
100
  // set extra parameters or overwritte the standard params set in the switch statement
105
101
  if (params) {
106
- for (var _i = 0, _b = Object.entries(params); _i < _b.length; _i++) {
107
- var _c = _b[_i], key = _c[0], value = _c[1];
102
+ for (const [key, value] of Object.entries(params)) {
108
103
  currentProcess[key] = value;
109
104
  }
110
105
  }
111
- _this.updateStepInRoute(step); // updates the step in the route
106
+ this.updateStepInRoute(step); // updates the step in the route
112
107
  return currentProcess;
113
108
  };
114
109
  /**
@@ -117,26 +112,26 @@ var StatusManager = /** @class */ (function () {
117
112
  * @param {ProcessType} type The process type to remove
118
113
  * @return {void}
119
114
  */
120
- this.removeProcess = function (step, type) {
115
+ this.removeProcess = (step, type) => {
121
116
  if (!step.execution) {
122
117
  throw new Error("Execution hasn't been initialized.");
123
118
  }
124
- var index = step.execution.process.findIndex(function (p) { return p.type === type; });
119
+ const index = step.execution.process.findIndex((p) => p.type === type);
125
120
  step.execution.process.splice(index, 1);
126
- _this.updateStepInRoute(step);
121
+ this.updateStepInRoute(step);
127
122
  };
128
- this.updateStepInRoute = function (step) {
129
- if (!_this.shouldUpdate) {
123
+ this.updateStepInRoute = (step) => {
124
+ if (!this.shouldUpdate) {
130
125
  return step;
131
126
  }
132
- var stepIndex = _this.route.steps.findIndex(function (routeStep) { return routeStep.id === step.id; });
127
+ const stepIndex = this.route.steps.findIndex((routeStep) => routeStep.id === step.id);
133
128
  if (stepIndex === -1) {
134
129
  throw new Error("Couldn't find a step to update.");
135
130
  }
136
- _this.route.steps[stepIndex] = Object.assign(_this.route.steps[stepIndex], step);
137
- _this.settings.updateCallback(_this.route);
138
- _this.internalUpdateRouteCallback(_this.route);
139
- return _this.route.steps[stepIndex];
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];
140
135
  };
141
136
  this.route = route;
142
137
  this.settings = settings;
@@ -149,7 +144,7 @@ var StatusManager = /** @class */ (function () {
149
144
  * @param {Receipt} receipt Optional. Information about received tokens
150
145
  * @return {Step} The step with the updated execution object
151
146
  */
152
- StatusManager.prototype.updateExecution = function (step, status, receipt) {
147
+ updateExecution(step, status, receipt) {
153
148
  if (!step.execution) {
154
149
  throw Error("Can't update empty execution.");
155
150
  }
@@ -161,10 +156,8 @@ var StatusManager = /** @class */ (function () {
161
156
  }
162
157
  this.updateStepInRoute(step);
163
158
  return step;
164
- };
165
- StatusManager.prototype.setShouldUpdate = function (value) {
159
+ }
160
+ setShouldUpdate(value) {
166
161
  this.shouldUpdate = value;
167
- };
168
- return StatusManager;
169
- }());
170
- 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
- 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
- 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
- var StepExecutor = /** @class */ (function () {
58
- function StepExecutor(statusManager, settings) {
59
- var _this = this;
60
- this.swapExecutionManager = new swap_execute_1.SwapExecutionManager();
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 = function (settings) {
64
- var haltingSettings = __assign(__assign({}, defaultExecutionHaltSettings), settings);
65
- _this.swapExecutionManager.setShouldContinue(false);
66
- _this.bridgeExecutionManager.setShouldContinue(false);
67
- _this.statusManager.setShouldUpdate(haltingSettings.allowUpdates);
68
- _this.executionStopped = true;
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 = function (signer, step) { return __awaiter(_this, void 0, void 0, function () {
71
- var updatedSigner, _a;
72
- return __generator(this, function (_b) {
73
- switch (_b.label) {
74
- case 0: return [4 /*yield*/, (0, switchChain_1.switchChain)(signer, this.statusManager, step, this.settings.switchChainHook, !this.executionStopped)];
75
- case 1:
76
- updatedSigner = _b.sent();
77
- if (!updatedSigner) {
78
- // chain switch was not successful, stop execution here
79
- return [2 /*return*/, step];
80
- }
81
- signer = updatedSigner;
82
- _a = step.type;
83
- switch (_a) {
84
- case 'lifi': return [3 /*break*/, 2];
85
- case 'cross': return [3 /*break*/, 2];
86
- case 'swap': return [3 /*break*/, 4];
87
- }
88
- return [3 /*break*/, 6];
89
- case 2: return [4 /*yield*/, this.executeCross(signer, step)];
90
- case 3:
91
- _b.sent();
92
- return [3 /*break*/, 7];
93
- case 4: return [4 /*yield*/, this.executeSwap(signer, step)];
94
- case 5:
95
- _b.sent();
96
- return [3 /*break*/, 7];
97
- case 6: throw new Error('Unsupported step type.');
98
- case 7: return [2 /*return*/, step];
99
- }
100
- });
101
- }); };
102
- this.executeSwap = function (signer, step) { return __awaiter(_this, void 0, void 0, function () {
103
- var swapParams;
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0:
107
- swapParams = {
108
- signer: signer,
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
- return StepExecutor;
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
- 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
- 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
- if (infiniteApproval === void 0) { infiniteApproval = false; }
52
- if (allowUserInteraction === void 0) { allowUserInteraction = false; }
53
- return __awaiter(void 0, void 0, void 0, function () {
54
- var allowanceProcess, approved, approvalAmount, approveTx, e_1, error;
55
- return __generator(this, function (_a) {
56
- switch (_a.label) {
57
- case 0:
58
- allowanceProcess = statusManager.findOrCreateProcess('TOKEN_ALLOWANCE', step);
59
- _a.label = 1;
60
- case 1:
61
- _a.trys.push([1, 10, , 15]);
62
- if (!allowanceProcess.txHash) return [3 /*break*/, 3];
63
- allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'PENDING');
64
- return [4 /*yield*/, (0, getProvider_1.getProvider)(signer).waitForTransaction(allowanceProcess.txHash)];
65
- case 2:
66
- _a.sent();
67
- allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
68
- return [3 /*break*/, 9];
69
- case 3:
70
- if (!(allowanceProcess.status === 'DONE')) return [3 /*break*/, 4];
71
- allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
72
- return [3 /*break*/, 9];
73
- case 4: return [4 /*yield*/, (0, utils_1.getApproved)(signer, token.address, spenderAddress)];
74
- case 5:
75
- approved = _a.sent();
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: replacementTx.hash,
138
- txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + replacementTx.hash,
44
+ txHash: approveTx.hash,
45
+ txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + approveTx.hash,
139
46
  });
140
- return [4 /*yield*/, replacementTx.wait()];
141
- case 1:
142
- _a.sent();
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
- return [3 /*break*/, 5];
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
+ });