@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,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,143 +7,61 @@ 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
- var _a;
42
- Object.defineProperty(exports, "__esModule", { value: true });
43
- exports.getMulticallAddress = exports.getRpcProvider = exports.getRpcUrls = exports.getRpcUrl = void 0;
44
- var ethers_1 = require("ethers");
45
- var _1 = require(".");
46
- var types_1 = require("./types");
47
- var ConfigService_1 = __importDefault(require("./services/ConfigService"));
10
+ import { providers } from 'ethers';
11
+ import { getRandomNumber, ServerError } from '.';
12
+ import { ChainId } from './types';
13
+ import ConfigService from './services/ConfigService';
48
14
  // cached providers
49
- var chainProviders = {};
15
+ const chainProviders = {};
50
16
  // Archive RPC Provider
51
- var archiveRpcs = (_a = {},
52
- _a[types_1.ChainId.ETH] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/eth/mainnet/archive',
53
- _a[types_1.ChainId.BSC] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/bsc/mainnet/archive',
54
- _a[types_1.ChainId.POL] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/polygon/mainnet/archive',
55
- _a[types_1.ChainId.FTM] = 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/fantom/mainnet',
56
- _a);
57
- // RPC Urls
58
- var getRpcUrl = function (chainId, archive) {
59
- if (archive === void 0) { archive = false; }
60
- return __awaiter(void 0, void 0, void 0, function () {
61
- var rpcUrls;
62
- return __generator(this, function (_a) {
63
- switch (_a.label) {
64
- case 0: return [4 /*yield*/, (0, exports.getRpcUrls)(chainId, archive)];
65
- case 1:
66
- rpcUrls = _a.sent();
67
- return [2 /*return*/, rpcUrls[0]];
68
- }
69
- });
70
- });
71
- };
72
- exports.getRpcUrl = getRpcUrl;
73
- var getRpcUrls = function (chainId, archive) {
74
- if (archive === void 0) { archive = false; }
75
- return __awaiter(void 0, void 0, void 0, function () {
76
- var configService, config;
77
- return __generator(this, function (_a) {
78
- switch (_a.label) {
79
- case 0:
80
- if (archive && archiveRpcs[chainId]) {
81
- return [2 /*return*/, [archiveRpcs[chainId]]];
82
- }
83
- configService = ConfigService_1.default.getInstance();
84
- return [4 /*yield*/, configService.getConfigAsync()];
85
- case 1:
86
- config = _a.sent();
87
- return [2 /*return*/, config.rpcs[chainId]];
88
- }
89
- });
90
- });
17
+ const archiveRpcs = {
18
+ [ChainId.ETH]: 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/eth/mainnet/archive',
19
+ [ChainId.BSC]: 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/bsc/mainnet/archive',
20
+ [ChainId.POL]: 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/polygon/mainnet/archive',
21
+ [ChainId.FTM]: 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/fantom/mainnet',
91
22
  };
92
- exports.getRpcUrls = getRpcUrls;
93
- var getRandomProvider = function (providerList) {
94
- var index = (0, _1.getRandomNumber)(0, providerList.length - 1);
23
+ // RPC Urls
24
+ export const getRpcUrl = (chainId, archive = false) => __awaiter(void 0, void 0, void 0, function* () {
25
+ const rpcUrls = yield getRpcUrls(chainId, archive);
26
+ return rpcUrls[0];
27
+ });
28
+ export const getRpcUrls = (chainId, archive = false) => __awaiter(void 0, void 0, void 0, function* () {
29
+ if (archive && archiveRpcs[chainId]) {
30
+ return [archiveRpcs[chainId]];
31
+ }
32
+ const configService = ConfigService.getInstance();
33
+ const config = yield configService.getConfigAsync();
34
+ return config.rpcs[chainId];
35
+ });
36
+ const getRandomProvider = (providerList) => {
37
+ const index = getRandomNumber(0, providerList.length - 1);
95
38
  return providerList[index];
96
39
  };
97
40
  // Provider
98
- var getRpcProvider = function (chainId, archive) {
99
- if (archive === void 0) { archive = false; }
100
- return __awaiter(void 0, void 0, void 0, function () {
101
- var _a, _b, _c, _d, urls;
102
- return __generator(this, function (_e) {
103
- switch (_e.label) {
104
- case 0:
105
- if (!(archive && archiveRpcs[chainId])) return [3 /*break*/, 2];
106
- _b = (_a = ethers_1.providers.FallbackProvider).bind;
107
- _d = (_c = ethers_1.providers.StaticJsonRpcProvider).bind;
108
- return [4 /*yield*/, (0, exports.getRpcUrl)(chainId, archive)];
109
- case 1:
110
- // return archive PRC, but don't cache it
111
- return [2 /*return*/, new (_b.apply(_a, [void 0, [
112
- new (_d.apply(_c, [void 0, _e.sent(), chainId]))()
113
- ]]))()];
114
- case 2:
115
- if (!!chainProviders[chainId]) return [3 /*break*/, 4];
116
- chainProviders[chainId] = [];
117
- return [4 /*yield*/, (0, exports.getRpcUrls)(chainId, archive)];
118
- case 3:
119
- urls = _e.sent();
120
- urls.forEach(function (url) {
121
- chainProviders[chainId].push(new ethers_1.providers.FallbackProvider([
122
- new ethers_1.providers.StaticJsonRpcProvider(url, chainId),
123
- ]));
124
- });
125
- _e.label = 4;
126
- case 4:
127
- if (!chainProviders[chainId].length) {
128
- throw new _1.ServerError("Unable to configure provider for chain ".concat(chainId));
129
- }
130
- return [2 /*return*/, getRandomProvider(chainProviders[chainId])];
131
- }
41
+ export const getRpcProvider = (chainId, archive = false) => __awaiter(void 0, void 0, void 0, function* () {
42
+ if (archive && archiveRpcs[chainId]) {
43
+ // return archive PRC, but don't cache it
44
+ return new providers.FallbackProvider([
45
+ new providers.StaticJsonRpcProvider(yield getRpcUrl(chainId, archive), chainId),
46
+ ]);
47
+ }
48
+ if (!chainProviders[chainId]) {
49
+ chainProviders[chainId] = [];
50
+ const urls = yield getRpcUrls(chainId, archive);
51
+ urls.forEach((url) => {
52
+ chainProviders[chainId].push(new providers.FallbackProvider([
53
+ new providers.StaticJsonRpcProvider(url, chainId),
54
+ ]));
132
55
  });
133
- });
134
- };
135
- exports.getRpcProvider = getRpcProvider;
56
+ }
57
+ if (!chainProviders[chainId].length) {
58
+ throw new ServerError(`Unable to configure provider for chain ${chainId}`);
59
+ }
60
+ return getRandomProvider(chainProviders[chainId]);
61
+ });
136
62
  // Multicall
137
- var getMulticallAddress = function (chainId) { return __awaiter(void 0, void 0, void 0, function () {
138
- var configService, config;
139
- return __generator(this, function (_a) {
140
- switch (_a.label) {
141
- case 0:
142
- configService = ConfigService_1.default.getInstance();
143
- return [4 /*yield*/, configService.getConfigAsync()];
144
- case 1:
145
- config = _a.sent();
146
- return [2 /*return*/, config.multicallAddresses[chainId]];
147
- }
148
- });
149
- }); };
150
- exports.getMulticallAddress = getMulticallAddress;
63
+ export const getMulticallAddress = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
64
+ const configService = ConfigService.getInstance();
65
+ const config = yield configService.getConfigAsync();
66
+ return config.multicallAddresses[chainId];
67
+ });
@@ -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
+ }