@lifi/sdk 1.0.0
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 +354 -0
- package/LICENSE +1 -0
- package/README.md +16 -0
- package/dist/Lifi.d.ts +186 -0
- package/dist/Lifi.js +461 -0
- package/dist/allowance/index.d.ts +22 -0
- package/dist/allowance/index.js +160 -0
- package/dist/allowance/utils.d.ts +14 -0
- package/dist/allowance/utils.js +137 -0
- package/dist/balances/index.d.ts +11 -0
- package/dist/balances/index.js +104 -0
- package/dist/balances/utils.d.ts +5 -0
- package/dist/balances/utils.js +255 -0
- package/dist/connectors.d.ts +6 -0
- package/dist/connectors.js +150 -0
- package/dist/execution/StatusManager.d.ts +63 -0
- package/dist/execution/StatusManager.js +150 -0
- package/dist/execution/StepExecutor.d.ts +15 -0
- package/dist/execution/StepExecutor.js +139 -0
- package/dist/execution/allowance.execute.d.ts +4 -0
- package/dist/execution/allowance.execute.js +156 -0
- package/dist/execution/balanceCheck.execute.d.ts +3 -0
- package/dist/execution/balanceCheck.execute.js +86 -0
- package/dist/execution/bridges/bridge.execute.d.ts +7 -0
- package/dist/execution/bridges/bridge.execute.js +218 -0
- package/dist/execution/exchanges/swap.execute.d.ts +7 -0
- package/dist/execution/exchanges/swap.execute.js +222 -0
- package/dist/execution/index.d.ts +1 -0
- package/dist/execution/index.js +17 -0
- package/dist/execution/switchChain.d.ts +16 -0
- package/dist/execution/switchChain.js +102 -0
- package/dist/execution/utils.d.ts +5 -0
- package/dist/execution/utils.js +175 -0
- package/dist/helpers.d.ts +18 -0
- package/dist/helpers.js +85 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +27 -0
- package/dist/services/ApiService.d.ts +14 -0
- package/dist/services/ApiService.js +350 -0
- package/dist/services/ChainsService.d.ts +11 -0
- package/dist/services/ChainsService.js +108 -0
- package/dist/services/ConfigService.d.ts +23 -0
- package/dist/services/ConfigService.js +133 -0
- package/dist/typeguards.d.ts +4 -0
- package/dist/typeguards.js +55 -0
- package/dist/types/ERC20.d.ts +22 -0
- package/dist/types/ERC20.js +53 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +22 -0
- package/dist/types/internal.types.d.ts +75 -0
- package/dist/types/internal.types.js +2 -0
- package/dist/utils/errors.d.ts +73 -0
- package/dist/utils/errors.js +147 -0
- package/dist/utils/getProvider.d.ts +3 -0
- package/dist/utils/getProvider.js +11 -0
- package/dist/utils/multicall.d.ts +10 -0
- package/dist/utils/multicall.js +111 -0
- package/dist/utils/multicallAbi.json +313 -0
- package/dist/utils/parseError.d.ts +37 -0
- package/dist/utils/parseError.js +184 -0
- package/dist/utils/utils.d.ts +26 -0
- package/dist/utils/utils.js +188 -0
- package/package.json +90 -0
package/dist/Lifi.js
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (_) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var allowance_1 = require("./allowance");
|
|
54
|
+
var balances_1 = __importDefault(require("./balances"));
|
|
55
|
+
var StatusManager_1 = require("./execution/StatusManager");
|
|
56
|
+
var StepExecutor_1 = require("./execution/StepExecutor");
|
|
57
|
+
var ApiService_1 = __importDefault(require("./services/ApiService"));
|
|
58
|
+
var ChainsService_1 = __importDefault(require("./services/ChainsService"));
|
|
59
|
+
var ConfigService_1 = __importDefault(require("./services/ConfigService"));
|
|
60
|
+
var typeguards_1 = require("./typeguards");
|
|
61
|
+
var errors_1 = require("./utils/errors");
|
|
62
|
+
var utils_1 = require("./utils/utils");
|
|
63
|
+
var LIFI = /** @class */ (function () {
|
|
64
|
+
function LIFI(configUpdate) {
|
|
65
|
+
var _this = this;
|
|
66
|
+
this.activeRouteDictionary = {};
|
|
67
|
+
/**
|
|
68
|
+
* Get the current configuration of the SDK
|
|
69
|
+
* @return {Config} - The config object
|
|
70
|
+
*/
|
|
71
|
+
this.getConfig = function () {
|
|
72
|
+
return _this.configService.getConfig();
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Get the SDK configuration after all setup calls are finished
|
|
76
|
+
* @return {Promise<Config>} - The config object
|
|
77
|
+
*/
|
|
78
|
+
this.getConfigAsync = function () {
|
|
79
|
+
return _this.configService.getConfigAsync();
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Set a new confuration for the SDK
|
|
83
|
+
* @param {ConfigUpdate} configUpdate - An object containing the configuration fields that should be updated.
|
|
84
|
+
* @return {Config} The renewed config object
|
|
85
|
+
*/
|
|
86
|
+
this.setConfig = function (configUpdate) {
|
|
87
|
+
return _this.configService.updateConfig(configUpdate);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Get a set of current possibilities based on a request that specifies which chains, exchanges and bridges are preferred or unwanted.
|
|
91
|
+
* @param {PossibilitiesRequest} request - Object defining preferences regarding chain, exchanges and bridges
|
|
92
|
+
* @return {Promise<PossibilitiesResponse>} Object listing current possibilities for any-to-any cross-chain-swaps based on the provided preferences.
|
|
93
|
+
* @throws {LifiError} Throws a LifiError if request fails.
|
|
94
|
+
*/
|
|
95
|
+
this.getPossibilities = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
96
|
+
return __generator(this, function (_a) {
|
|
97
|
+
return [2 /*return*/, ApiService_1.default.getPossibilities(request, options)];
|
|
98
|
+
});
|
|
99
|
+
}); };
|
|
100
|
+
/**
|
|
101
|
+
* Fetch information about a Token
|
|
102
|
+
* @param {ChainKey | ChainId} chain - Id or key of the chain that contains the token
|
|
103
|
+
* @param {string} token - Address or symbol of the token on the requested chain
|
|
104
|
+
* @throws {LifiError} - Throws a LifiError if request fails
|
|
105
|
+
*/
|
|
106
|
+
this.getToken = function (chain, token, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
107
|
+
return __generator(this, function (_a) {
|
|
108
|
+
return [2 /*return*/, ApiService_1.default.getToken(chain, token, options)];
|
|
109
|
+
});
|
|
110
|
+
}); };
|
|
111
|
+
/**
|
|
112
|
+
* Get a quote for a token transfer
|
|
113
|
+
* @param {QuoteRequest} request - The configuration of the requested quote
|
|
114
|
+
* @throws {LifiError} - Throws a LifiError if request fails
|
|
115
|
+
*/
|
|
116
|
+
this.getQuote = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
117
|
+
return __generator(this, function (_a) {
|
|
118
|
+
return [2 /*return*/, ApiService_1.default.getQuote(request, options)];
|
|
119
|
+
});
|
|
120
|
+
}); };
|
|
121
|
+
/**
|
|
122
|
+
* Check the status of a transfer. For cross chain transfers, the "bridge" parameter is required.
|
|
123
|
+
* @param {GetStatusRequest} request - Configuration of the requested status
|
|
124
|
+
* @throws {LifiError} - Throws a LifiError if request fails
|
|
125
|
+
*/
|
|
126
|
+
this.getStatus = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
127
|
+
return __generator(this, function (_a) {
|
|
128
|
+
return [2 /*return*/, ApiService_1.default.getStatus(request, options)];
|
|
129
|
+
});
|
|
130
|
+
}); };
|
|
131
|
+
/**
|
|
132
|
+
* Get the available tools to bridge and swap tokens.
|
|
133
|
+
* @param {ToolsRequest?} request - The configuration of the requested tools
|
|
134
|
+
* @returns The tools that are available on the requested chains
|
|
135
|
+
*/
|
|
136
|
+
this.getTools = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
return [2 /*return*/, ApiService_1.default.getTools(request || {}, options)];
|
|
139
|
+
});
|
|
140
|
+
}); };
|
|
141
|
+
/**
|
|
142
|
+
* Get all known tokens.
|
|
143
|
+
* @param {TokensRequest?} request - The configuration of the requested tokens
|
|
144
|
+
* @returns The tokens that are available on the requested chains
|
|
145
|
+
*/
|
|
146
|
+
this.getTokens = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
147
|
+
return __generator(this, function (_a) {
|
|
148
|
+
return [2 /*return*/, ApiService_1.default.getTokens(request || {}, options)];
|
|
149
|
+
});
|
|
150
|
+
}); };
|
|
151
|
+
/**
|
|
152
|
+
* Get all available chains
|
|
153
|
+
* @return {Promise<Chain[]>} A list of all available chains
|
|
154
|
+
* @throws {LifiError} Throws a LifiError if request fails.
|
|
155
|
+
*/
|
|
156
|
+
this.getChains = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
157
|
+
return __generator(this, function (_a) {
|
|
158
|
+
return [2 /*return*/, this.chainsService.getChains()];
|
|
159
|
+
});
|
|
160
|
+
}); };
|
|
161
|
+
/**
|
|
162
|
+
* Get a set of routes for a request that describes a transfer of tokens.
|
|
163
|
+
* @param {RoutesRequest} routesRequest - A description of the transfer.
|
|
164
|
+
* @return {Promise<RoutesResponse>} The resulting routes that can be used to realize the described transfer of tokens.
|
|
165
|
+
* @throws {LifiError} Throws a LifiError if request fails.
|
|
166
|
+
*/
|
|
167
|
+
this.getRoutes = function (request, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
168
|
+
return __generator(this, function (_a) {
|
|
169
|
+
return [2 /*return*/, ApiService_1.default.getRoutes(request, options)];
|
|
170
|
+
});
|
|
171
|
+
}); };
|
|
172
|
+
/**
|
|
173
|
+
* Get the transaction data for a single step of a route
|
|
174
|
+
* @param {Step} step - The step object.
|
|
175
|
+
* @return {Promise<Step>} The step populated with the transaction data.
|
|
176
|
+
* @throws {LifiError} Throws a LifiError if request fails.
|
|
177
|
+
*/
|
|
178
|
+
this.getStepTransaction = function (step, options) { return __awaiter(_this, void 0, void 0, function () {
|
|
179
|
+
return __generator(this, function (_a) {
|
|
180
|
+
return [2 /*return*/, ApiService_1.default.getStepTransaction(step, options)];
|
|
181
|
+
});
|
|
182
|
+
}); };
|
|
183
|
+
/**
|
|
184
|
+
* Stops the execution of an active route.
|
|
185
|
+
* @param {Route} route - A route that is currently in execution.
|
|
186
|
+
* @return {Route} The stopped route.
|
|
187
|
+
*/
|
|
188
|
+
this.stopExecution = function (route) {
|
|
189
|
+
if (!_this.activeRouteDictionary[route.id]) {
|
|
190
|
+
return route;
|
|
191
|
+
}
|
|
192
|
+
for (var _i = 0, _a = _this.activeRouteDictionary[route.id].executors; _i < _a.length; _i++) {
|
|
193
|
+
var executor = _a[_i];
|
|
194
|
+
executor.stopStepExecution({ allowUpdates: false });
|
|
195
|
+
}
|
|
196
|
+
delete _this.activeRouteDictionary[route.id];
|
|
197
|
+
return route;
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
|
|
201
|
+
* @param {Route} route - A route that is currently in execution.
|
|
202
|
+
*/
|
|
203
|
+
this.moveExecutionToBackground = function (route) {
|
|
204
|
+
if (!_this.activeRouteDictionary[route.id]) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
for (var _i = 0, _a = _this.activeRouteDictionary[route.id].executors; _i < _a.length; _i++) {
|
|
208
|
+
var executor = _a[_i];
|
|
209
|
+
executor.stopStepExecution({ allowUpdates: true });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Execute a route.
|
|
214
|
+
* @param {Signer} signer - The signer required to send the transactions.
|
|
215
|
+
* @param {Route} route - The route that should be executed. Cannot be an active route.
|
|
216
|
+
* @param {ExecutionSettings} settings - An object containing settings and callbacks.
|
|
217
|
+
* @return {Promise<Route>} The executed route.
|
|
218
|
+
* @throws {LifiError} Throws a LifiError if the execution fails.
|
|
219
|
+
*/
|
|
220
|
+
this.executeRoute = function (signer, route, settings) { return __awaiter(_this, void 0, void 0, function () {
|
|
221
|
+
var clonedRoute;
|
|
222
|
+
return __generator(this, function (_a) {
|
|
223
|
+
clonedRoute = (0, utils_1.deepClone)(route) // deep clone to prevent side effects
|
|
224
|
+
;
|
|
225
|
+
// check if route is already running
|
|
226
|
+
if (this.activeRouteDictionary[clonedRoute.id]) {
|
|
227
|
+
// TODO: maybe inform user why nothing happens?
|
|
228
|
+
return [2 /*return*/, clonedRoute];
|
|
229
|
+
}
|
|
230
|
+
return [2 /*return*/, this.executeSteps(signer, clonedRoute, settings)];
|
|
231
|
+
});
|
|
232
|
+
}); };
|
|
233
|
+
/**
|
|
234
|
+
* Resume the execution of a route that has been stopped or had an error while executing.
|
|
235
|
+
* @param {Signer} signer - The signer required to send the transactions.
|
|
236
|
+
* @param {Route} route - The route that is to be executed. Cannot be an active route.
|
|
237
|
+
* @param {ExecutionSettings} settings - An object containing settings and callbacks.
|
|
238
|
+
* @return {Promise<Route>} The executed route.
|
|
239
|
+
* @throws {LifiError} Throws a LifiError if the execution fails.
|
|
240
|
+
*/
|
|
241
|
+
this.resumeRoute = function (signer, route, settings) { return __awaiter(_this, void 0, void 0, function () {
|
|
242
|
+
var clonedRoute, activeRoute, executionHalted, index, stepHasFailed;
|
|
243
|
+
var _a, _b;
|
|
244
|
+
return __generator(this, function (_c) {
|
|
245
|
+
clonedRoute = (0, utils_1.deepClone)(route) // deep clone to prevent side effects
|
|
246
|
+
;
|
|
247
|
+
activeRoute = this.activeRouteDictionary[clonedRoute.id];
|
|
248
|
+
if (activeRoute) {
|
|
249
|
+
executionHalted = activeRoute.executors.some(function (executor) { return executor.executionStopped; });
|
|
250
|
+
if (!executionHalted) {
|
|
251
|
+
return [2 /*return*/, clonedRoute];
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// remove last (failed) process
|
|
255
|
+
for (index = 0; index < clonedRoute.steps.length; index++) {
|
|
256
|
+
stepHasFailed = ((_a = clonedRoute.steps[index].execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED';
|
|
257
|
+
if (stepHasFailed) {
|
|
258
|
+
(_b = clonedRoute.steps[index].execution) === null || _b === void 0 ? void 0 : _b.process.pop();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return [2 /*return*/, this.executeSteps(signer, clonedRoute, settings)];
|
|
262
|
+
});
|
|
263
|
+
}); };
|
|
264
|
+
this.executeSteps = function (signer, route, settings) { return __awaiter(_this, void 0, void 0, function () {
|
|
265
|
+
var config, execData, statusManager, index, step, previousStep, stepExecutor, e_1;
|
|
266
|
+
var _this = this;
|
|
267
|
+
return __generator(this, function (_a) {
|
|
268
|
+
switch (_a.label) {
|
|
269
|
+
case 0:
|
|
270
|
+
config = this.configService.getConfig();
|
|
271
|
+
execData = {
|
|
272
|
+
route: route,
|
|
273
|
+
executors: [],
|
|
274
|
+
settings: __assign(__assign({}, config.defaultExecutionSettings), settings),
|
|
275
|
+
};
|
|
276
|
+
this.activeRouteDictionary[route.id] = execData;
|
|
277
|
+
statusManager = new StatusManager_1.StatusManager(route, this.activeRouteDictionary[route.id].settings, function (route) { return (_this.activeRouteDictionary[route.id].route = route); });
|
|
278
|
+
index = 0;
|
|
279
|
+
_a.label = 1;
|
|
280
|
+
case 1:
|
|
281
|
+
if (!(index < route.steps.length)) return [3 /*break*/, 7];
|
|
282
|
+
//check if execution has stopped in meantime
|
|
283
|
+
if (!this.activeRouteDictionary[route.id]) {
|
|
284
|
+
return [3 /*break*/, 7];
|
|
285
|
+
}
|
|
286
|
+
step = route.steps[index];
|
|
287
|
+
previousStep = index !== 0 ? route.steps[index - 1] : undefined;
|
|
288
|
+
// check if step already done
|
|
289
|
+
if (step.execution && step.execution.status === 'DONE') {
|
|
290
|
+
return [3 /*break*/, 6];
|
|
291
|
+
}
|
|
292
|
+
// update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
|
|
293
|
+
if (previousStep &&
|
|
294
|
+
previousStep.execution &&
|
|
295
|
+
previousStep.execution.toAmount) {
|
|
296
|
+
step.action.fromAmount = previousStep.execution.toAmount;
|
|
297
|
+
}
|
|
298
|
+
stepExecutor = void 0;
|
|
299
|
+
_a.label = 2;
|
|
300
|
+
case 2:
|
|
301
|
+
_a.trys.push([2, 4, , 5]);
|
|
302
|
+
stepExecutor = new StepExecutor_1.StepExecutor(statusManager, this.activeRouteDictionary[route.id].settings);
|
|
303
|
+
this.activeRouteDictionary[route.id].executors.push(stepExecutor);
|
|
304
|
+
return [4 /*yield*/, stepExecutor.executeStep(signer, step)];
|
|
305
|
+
case 3:
|
|
306
|
+
_a.sent();
|
|
307
|
+
return [3 /*break*/, 5];
|
|
308
|
+
case 4:
|
|
309
|
+
e_1 = _a.sent();
|
|
310
|
+
this.stopExecution(route);
|
|
311
|
+
throw e_1;
|
|
312
|
+
case 5:
|
|
313
|
+
// execution stopped during the current step, we don't want to continue to the next step so we return already
|
|
314
|
+
if (stepExecutor.executionStopped) {
|
|
315
|
+
return [2 /*return*/, route];
|
|
316
|
+
}
|
|
317
|
+
_a.label = 6;
|
|
318
|
+
case 6:
|
|
319
|
+
index++;
|
|
320
|
+
return [3 /*break*/, 1];
|
|
321
|
+
case 7:
|
|
322
|
+
//clean up after execution
|
|
323
|
+
delete this.activeRouteDictionary[route.id];
|
|
324
|
+
return [2 /*return*/, route];
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}); };
|
|
328
|
+
/**
|
|
329
|
+
* Update the ExecutionSettings for an active route.
|
|
330
|
+
* @param {ExecutionSettings} settings - An object with execution settings.
|
|
331
|
+
* @param {Route} route - The active route that gets the new execution settings.
|
|
332
|
+
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
333
|
+
*/
|
|
334
|
+
this.updateExecutionSettings = function (settings, route) {
|
|
335
|
+
if (!_this.activeRouteDictionary[route.id]) {
|
|
336
|
+
throw new errors_1.ValidationError("Can't set ExecutionSettings for the inactive route.");
|
|
337
|
+
}
|
|
338
|
+
var config = _this.configService.getConfig();
|
|
339
|
+
_this.activeRouteDictionary[route.id].settings = __assign(__assign({}, config.defaultExecutionSettings), settings);
|
|
340
|
+
};
|
|
341
|
+
/**
|
|
342
|
+
* Get the list of active routes.
|
|
343
|
+
* @return {Route[]} A list of routes.
|
|
344
|
+
*/
|
|
345
|
+
this.getActiveRoutes = function () {
|
|
346
|
+
return Object.values(_this.activeRouteDictionary).map(function (dict) { return dict.route; });
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Return the current route information for given route. The route has to be active.
|
|
350
|
+
* @param {Route} route - A route object.
|
|
351
|
+
* @return {Route} The updated route.
|
|
352
|
+
*/
|
|
353
|
+
this.getActiveRoute = function (route) {
|
|
354
|
+
return _this.activeRouteDictionary[route.id].route;
|
|
355
|
+
};
|
|
356
|
+
/**
|
|
357
|
+
* Returns the balances of a specific token a wallet holds across all aggregated chains.
|
|
358
|
+
* @param {string} walletAddress - A wallet address.
|
|
359
|
+
* @param {Token} token - A Token object.
|
|
360
|
+
* @return {Promise<TokenAmount | null>} An object containing the token and the amounts on different chains.
|
|
361
|
+
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
362
|
+
*/
|
|
363
|
+
this.getTokenBalance = function (walletAddress, token) { return __awaiter(_this, void 0, void 0, function () {
|
|
364
|
+
return __generator(this, function (_a) {
|
|
365
|
+
if (!walletAddress) {
|
|
366
|
+
throw new errors_1.ValidationError('Missing walletAddress.');
|
|
367
|
+
}
|
|
368
|
+
if (!(0, typeguards_1.isToken)(token)) {
|
|
369
|
+
throw new errors_1.ValidationError("Invalid token passed: address \"".concat(token.address, "\" on chainId \"").concat(token.chainId, "\""));
|
|
370
|
+
}
|
|
371
|
+
return [2 /*return*/, balances_1.default.getTokenBalance(walletAddress, token)];
|
|
372
|
+
});
|
|
373
|
+
}); };
|
|
374
|
+
/**
|
|
375
|
+
* Returns the balances for a list tokens a wallet holds across all aggregated chains.
|
|
376
|
+
* @param {string} walletAddress - A wallet address.
|
|
377
|
+
* @param {Token[]} tokens - A list of Token objects.
|
|
378
|
+
* @return {Promise<TokenAmount[]>} A list of objects containing the tokens and the amounts on different chains.
|
|
379
|
+
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
380
|
+
*/
|
|
381
|
+
this.getTokenBalances = function (walletAddress, tokens) { return __awaiter(_this, void 0, void 0, function () {
|
|
382
|
+
var invalidTokens;
|
|
383
|
+
return __generator(this, function (_a) {
|
|
384
|
+
if (!walletAddress) {
|
|
385
|
+
throw new errors_1.ValidationError('Missing walletAddress.');
|
|
386
|
+
}
|
|
387
|
+
invalidTokens = tokens.filter(function (token) { return !(0, typeguards_1.isToken)(token); });
|
|
388
|
+
if (invalidTokens.length) {
|
|
389
|
+
throw new errors_1.ValidationError("Invalid token passed: address \"".concat(invalidTokens[0].address, "\" on chainId \"").concat(invalidTokens[0].chainId, "\""));
|
|
390
|
+
}
|
|
391
|
+
return [2 /*return*/, balances_1.default.getTokenBalances(walletAddress, tokens)];
|
|
392
|
+
});
|
|
393
|
+
}); };
|
|
394
|
+
/**
|
|
395
|
+
* This method queries the balances of tokens for a specific list of chains for a given wallet.
|
|
396
|
+
* @param {string} walletAddress - A walletaddress.
|
|
397
|
+
* @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
|
|
398
|
+
* @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
399
|
+
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
400
|
+
*/
|
|
401
|
+
this.getTokenBalancesForChains = function (walletAddress, tokensByChain) { return __awaiter(_this, void 0, void 0, function () {
|
|
402
|
+
var tokenList, invalidTokens;
|
|
403
|
+
return __generator(this, function (_a) {
|
|
404
|
+
if (!walletAddress) {
|
|
405
|
+
throw new errors_1.ValidationError('Missing walletAddress.');
|
|
406
|
+
}
|
|
407
|
+
tokenList = Object.values(tokensByChain).flat();
|
|
408
|
+
invalidTokens = tokenList.filter(function (token) { return !(0, typeguards_1.isToken)(token); });
|
|
409
|
+
if (invalidTokens.length) {
|
|
410
|
+
throw new errors_1.ValidationError("Invalid token passed: address \"".concat(invalidTokens[0].address, "\" on chainId \"").concat(invalidTokens[0].chainId, "\""));
|
|
411
|
+
}
|
|
412
|
+
return [2 /*return*/, balances_1.default.getTokenBalancesForChains(walletAddress, tokensByChain)];
|
|
413
|
+
});
|
|
414
|
+
}); };
|
|
415
|
+
/**
|
|
416
|
+
* Get the current approval for a certain token.
|
|
417
|
+
* @param signer - The signer owning the token
|
|
418
|
+
* @param token - The token that should be checked
|
|
419
|
+
* @param approvalAddress - The address that has be approved
|
|
420
|
+
*/
|
|
421
|
+
this.getTokenApproval = function (signer, token, approvalAddress) { return __awaiter(_this, void 0, void 0, function () {
|
|
422
|
+
return __generator(this, function (_a) {
|
|
423
|
+
return [2 /*return*/, (0, allowance_1.getTokenApproval)(signer, token, approvalAddress)];
|
|
424
|
+
});
|
|
425
|
+
}); };
|
|
426
|
+
/**
|
|
427
|
+
* Get the current approval for a list of token / approval address pairs.
|
|
428
|
+
* @param signer - The signer owning the tokens
|
|
429
|
+
* @param tokenData - A list of token and approval address pairs
|
|
430
|
+
*/
|
|
431
|
+
this.bulkGetTokenApproval = function (signer, tokenData) { return __awaiter(_this, void 0, void 0, function () {
|
|
432
|
+
return __generator(this, function (_a) {
|
|
433
|
+
return [2 /*return*/, (0, allowance_1.bulkGetTokenApproval)(signer, tokenData)];
|
|
434
|
+
});
|
|
435
|
+
}); };
|
|
436
|
+
/**
|
|
437
|
+
* Set approval for a certain token and amount.
|
|
438
|
+
* @param { ApproveTokenRequest } request - The approval request
|
|
439
|
+
*/
|
|
440
|
+
this.approveToken = function (request) {
|
|
441
|
+
return (0, allowance_1.approveToken)(request);
|
|
442
|
+
};
|
|
443
|
+
/**
|
|
444
|
+
* Revoke approval for a certain token.
|
|
445
|
+
* @param { RevokeApprovalRequest } request - The revoke request
|
|
446
|
+
*/
|
|
447
|
+
this.revokeTokenApproval = function (request) {
|
|
448
|
+
return (0, allowance_1.revokeTokenApproval)(request);
|
|
449
|
+
};
|
|
450
|
+
this.configService = ConfigService_1.default.getInstance();
|
|
451
|
+
if (configUpdate) {
|
|
452
|
+
this.configService.updateConfig(configUpdate); // update API urls before we request chains
|
|
453
|
+
}
|
|
454
|
+
this.chainsService = ChainsService_1.default.getInstance();
|
|
455
|
+
this.chainsService.getChains().then(function (chains) {
|
|
456
|
+
_this.configService.updateChains(chains);
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
return LIFI;
|
|
460
|
+
}());
|
|
461
|
+
exports.default = LIFI;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Token } from '@lifi/types';
|
|
2
|
+
import { Signer } from 'ethers';
|
|
3
|
+
import { RevokeTokenData } from '../types';
|
|
4
|
+
export interface ApproveTokenRequest {
|
|
5
|
+
signer: Signer;
|
|
6
|
+
token: Token;
|
|
7
|
+
approvalAddress: string;
|
|
8
|
+
amount: string;
|
|
9
|
+
infiniteApproval?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface RevokeApprovalRequest {
|
|
12
|
+
signer: Signer;
|
|
13
|
+
token: Token;
|
|
14
|
+
approvalAddress: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const getTokenApproval: (signer: Signer, token: Token, approvalAddress: string) => Promise<string | undefined>;
|
|
17
|
+
export declare const bulkGetTokenApproval: (signer: Signer, tokenData: RevokeTokenData[]) => Promise<{
|
|
18
|
+
token: Token;
|
|
19
|
+
approval: string | undefined;
|
|
20
|
+
}[]>;
|
|
21
|
+
export declare const approveToken: ({ signer, token, approvalAddress, amount, infiniteApproval, }: ApproveTokenRequest) => Promise<void>;
|
|
22
|
+
export declare const revokeTokenApproval: ({ signer, token, approvalAddress, }: RevokeApprovalRequest) => Promise<void>;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
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.revokeTokenApproval = exports.approveToken = exports.bulkGetTokenApproval = exports.getTokenApproval = void 0;
|
|
43
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
|
+
var ethers_1 = require("ethers");
|
|
45
|
+
var helpers_1 = require("../helpers");
|
|
46
|
+
var utils_1 = require("../utils/utils");
|
|
47
|
+
var utils_2 = require("./utils");
|
|
48
|
+
var getTokenApproval = function (signer, token, approvalAddress) { return __awaiter(void 0, void 0, void 0, function () {
|
|
49
|
+
var approved;
|
|
50
|
+
return __generator(this, function (_a) {
|
|
51
|
+
switch (_a.label) {
|
|
52
|
+
case 0:
|
|
53
|
+
// native token don't need approval
|
|
54
|
+
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
55
|
+
return [2 /*return*/];
|
|
56
|
+
}
|
|
57
|
+
return [4 /*yield*/, (0, utils_2.getApproved)(signer, token.address, approvalAddress)];
|
|
58
|
+
case 1:
|
|
59
|
+
approved = _a.sent();
|
|
60
|
+
return [2 /*return*/, approved.toString()];
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}); };
|
|
64
|
+
exports.getTokenApproval = getTokenApproval;
|
|
65
|
+
var bulkGetTokenApproval = function (signer, tokenData) { return __awaiter(void 0, void 0, void 0, function () {
|
|
66
|
+
var filteredTokenData, tokenDataByChain, approvalPromises, approvalsByChain, approvals;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
filteredTokenData = tokenData.filter(function (_a) {
|
|
71
|
+
var token = _a.token;
|
|
72
|
+
return !(0, utils_1.isNativeTokenAddress)(token.address);
|
|
73
|
+
});
|
|
74
|
+
tokenDataByChain = (0, utils_2.groupByChain)(filteredTokenData);
|
|
75
|
+
approvalPromises = Object.keys(tokenDataByChain).map(function (chainId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
76
|
+
var parsedChainId;
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
parsedChainId = Number.parseInt(chainId);
|
|
79
|
+
// get allowances for current chain and token list
|
|
80
|
+
return [2 /*return*/, (0, utils_2.getAllowanceViaMulticall)(signer, parsedChainId, tokenDataByChain[parsedChainId])];
|
|
81
|
+
});
|
|
82
|
+
}); });
|
|
83
|
+
return [4 /*yield*/, Promise.all(approvalPromises)];
|
|
84
|
+
case 1:
|
|
85
|
+
approvalsByChain = _a.sent();
|
|
86
|
+
approvals = approvalsByChain.flat();
|
|
87
|
+
return [2 /*return*/, tokenData.map(function (_a) {
|
|
88
|
+
var token = _a.token;
|
|
89
|
+
// native token don't need approval
|
|
90
|
+
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
91
|
+
return { token: token, approval: undefined };
|
|
92
|
+
}
|
|
93
|
+
var approved = approvals.find(function (approval) {
|
|
94
|
+
return (0, helpers_1.isSameToken)(approval.token, token);
|
|
95
|
+
});
|
|
96
|
+
return { token: token, approval: approved === null || approved === void 0 ? void 0 : approved.approvedAmount.toString() };
|
|
97
|
+
})];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}); };
|
|
101
|
+
exports.bulkGetTokenApproval = bulkGetTokenApproval;
|
|
102
|
+
var approveToken = function (_a) {
|
|
103
|
+
var signer = _a.signer, token = _a.token, approvalAddress = _a.approvalAddress, amount = _a.amount, _b = _a.infiniteApproval, infiniteApproval = _b === void 0 ? false : _b;
|
|
104
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
105
|
+
var approvedAmount, approvalAmount, approveTx;
|
|
106
|
+
return __generator(this, function (_c) {
|
|
107
|
+
switch (_c.label) {
|
|
108
|
+
case 0:
|
|
109
|
+
// native token don't need approval
|
|
110
|
+
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
111
|
+
return [2 /*return*/];
|
|
112
|
+
}
|
|
113
|
+
return [4 /*yield*/, (0, utils_2.getApproved)(signer, token.address, approvalAddress)];
|
|
114
|
+
case 1:
|
|
115
|
+
approvedAmount = _c.sent();
|
|
116
|
+
if (!new bignumber_js_1.default(amount).gt(approvedAmount)) return [3 /*break*/, 4];
|
|
117
|
+
approvalAmount = infiniteApproval
|
|
118
|
+
? ethers_1.constants.MaxUint256.toString()
|
|
119
|
+
: amount;
|
|
120
|
+
return [4 /*yield*/, (0, utils_2.setApproval)(signer, token.address, approvalAddress, approvalAmount)];
|
|
121
|
+
case 2:
|
|
122
|
+
approveTx = _c.sent();
|
|
123
|
+
return [4 /*yield*/, approveTx.wait()];
|
|
124
|
+
case 3:
|
|
125
|
+
_c.sent();
|
|
126
|
+
_c.label = 4;
|
|
127
|
+
case 4: return [2 /*return*/];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
exports.approveToken = approveToken;
|
|
133
|
+
var revokeTokenApproval = function (_a) {
|
|
134
|
+
var signer = _a.signer, token = _a.token, approvalAddress = _a.approvalAddress;
|
|
135
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
136
|
+
var approvedAmount, approveTx;
|
|
137
|
+
return __generator(this, function (_b) {
|
|
138
|
+
switch (_b.label) {
|
|
139
|
+
case 0:
|
|
140
|
+
// native token don't need approval
|
|
141
|
+
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
142
|
+
return [2 /*return*/];
|
|
143
|
+
}
|
|
144
|
+
return [4 /*yield*/, (0, utils_2.getApproved)(signer, token.address, approvalAddress)];
|
|
145
|
+
case 1:
|
|
146
|
+
approvedAmount = _b.sent();
|
|
147
|
+
if (!!approvedAmount.isZero()) return [3 /*break*/, 4];
|
|
148
|
+
return [4 /*yield*/, (0, utils_2.setApproval)(signer, token.address, approvalAddress, '0')];
|
|
149
|
+
case 2:
|
|
150
|
+
approveTx = _b.sent();
|
|
151
|
+
return [4 /*yield*/, approveTx.wait()];
|
|
152
|
+
case 3:
|
|
153
|
+
_b.sent();
|
|
154
|
+
_b.label = 4;
|
|
155
|
+
case 4: return [2 /*return*/];
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
exports.revokeTokenApproval = revokeTokenApproval;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ChainId, Token } from '@lifi/types';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
3
|
+
import { ContractTransaction, Signer } from 'ethers';
|
|
4
|
+
import { RevokeTokenData } from '../types';
|
|
5
|
+
export declare const getApproved: (signer: Signer, tokenAddress: string, contractAddress: string) => Promise<BigNumber>;
|
|
6
|
+
export declare const setApproval: (signer: Signer, tokenAddress: string, contractAddress: string, amount: string) => Promise<ContractTransaction>;
|
|
7
|
+
export declare const getAllowanceViaMulticall: (signer: Signer, chainId: ChainId, tokenData: RevokeTokenData[]) => Promise<{
|
|
8
|
+
token: Token;
|
|
9
|
+
approvalAddress: string;
|
|
10
|
+
approvedAmount: BigNumber;
|
|
11
|
+
}[]>;
|
|
12
|
+
export declare const groupByChain: (tokenDataList: RevokeTokenData[]) => {
|
|
13
|
+
[chainId: number]: RevokeTokenData[];
|
|
14
|
+
};
|