@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
|
@@ -0,0 +1,350 @@
|
|
|
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 axios_1 = __importDefault(require("axios"));
|
|
54
|
+
var typeguards_1 = require("../typeguards");
|
|
55
|
+
var errors_1 = require("../utils/errors");
|
|
56
|
+
var parseError_1 = require("../utils/parseError");
|
|
57
|
+
var ConfigService_1 = __importDefault(require("./ConfigService"));
|
|
58
|
+
var getPossibilities = function (request, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
59
|
+
var configService, config, result, e_1;
|
|
60
|
+
return __generator(this, function (_a) {
|
|
61
|
+
switch (_a.label) {
|
|
62
|
+
case 0:
|
|
63
|
+
if (!request) {
|
|
64
|
+
request = {};
|
|
65
|
+
}
|
|
66
|
+
configService = ConfigService_1.default.getInstance();
|
|
67
|
+
config = configService.getConfig();
|
|
68
|
+
// apply defaults
|
|
69
|
+
request.bridges = request.bridges || config.defaultRouteOptions.bridges;
|
|
70
|
+
request.exchanges = request.exchanges || config.defaultRouteOptions.exchanges;
|
|
71
|
+
_a.label = 1;
|
|
72
|
+
case 1:
|
|
73
|
+
_a.trys.push([1, 3, , 4]);
|
|
74
|
+
return [4 /*yield*/, axios_1.default.post(config.apiUrl + 'advanced/possibilities', request, {
|
|
75
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
76
|
+
})];
|
|
77
|
+
case 2:
|
|
78
|
+
result = _a.sent();
|
|
79
|
+
return [2 /*return*/, result.data];
|
|
80
|
+
case 3:
|
|
81
|
+
e_1 = _a.sent();
|
|
82
|
+
throw (0, parseError_1.parseBackendError)(e_1);
|
|
83
|
+
case 4: return [2 /*return*/];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}); };
|
|
87
|
+
var getToken = function (chain, token, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
88
|
+
var configService, config, result, e_2;
|
|
89
|
+
return __generator(this, function (_a) {
|
|
90
|
+
switch (_a.label) {
|
|
91
|
+
case 0:
|
|
92
|
+
if (!chain) {
|
|
93
|
+
throw new errors_1.ValidationError('Required parameter "chain" is missing.');
|
|
94
|
+
}
|
|
95
|
+
if (!token) {
|
|
96
|
+
throw new errors_1.ValidationError('Required parameter "token" is missing.');
|
|
97
|
+
}
|
|
98
|
+
configService = ConfigService_1.default.getInstance();
|
|
99
|
+
config = configService.getConfig();
|
|
100
|
+
_a.label = 1;
|
|
101
|
+
case 1:
|
|
102
|
+
_a.trys.push([1, 3, , 4]);
|
|
103
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'token', {
|
|
104
|
+
params: {
|
|
105
|
+
chain: chain,
|
|
106
|
+
token: token,
|
|
107
|
+
},
|
|
108
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
109
|
+
})];
|
|
110
|
+
case 2:
|
|
111
|
+
result = _a.sent();
|
|
112
|
+
return [2 /*return*/, result.data];
|
|
113
|
+
case 3:
|
|
114
|
+
e_2 = _a.sent();
|
|
115
|
+
throw (0, parseError_1.parseBackendError)(e_2);
|
|
116
|
+
case 4: return [2 /*return*/];
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}); };
|
|
120
|
+
var getQuote = function (_a, options) {
|
|
121
|
+
var fromChain = _a.fromChain, fromToken = _a.fromToken, fromAddress = _a.fromAddress, fromAmount = _a.fromAmount, toChain = _a.toChain, toToken = _a.toToken, toAddress = _a.toAddress, order = _a.order, slippage = _a.slippage, integrator = _a.integrator, referrer = _a.referrer, allowBridges = _a.allowBridges, denyBridges = _a.denyBridges, preferBridges = _a.preferBridges, allowExchanges = _a.allowExchanges, denyExchanges = _a.denyExchanges, preferExchanges = _a.preferExchanges;
|
|
122
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
123
|
+
var configService, config, result, e_3;
|
|
124
|
+
return __generator(this, function (_b) {
|
|
125
|
+
switch (_b.label) {
|
|
126
|
+
case 0:
|
|
127
|
+
if (!fromChain) {
|
|
128
|
+
throw new errors_1.ValidationError('Required parameter "fromChain" is missing.');
|
|
129
|
+
}
|
|
130
|
+
if (!fromToken) {
|
|
131
|
+
throw new errors_1.ValidationError('Required parameter "fromToken" is missing.');
|
|
132
|
+
}
|
|
133
|
+
if (!fromAddress) {
|
|
134
|
+
throw new errors_1.ValidationError('Required parameter "fromAddress" is missing.');
|
|
135
|
+
}
|
|
136
|
+
if (!fromAmount) {
|
|
137
|
+
throw new errors_1.ValidationError('Required parameter "fromAmount" is missing.');
|
|
138
|
+
}
|
|
139
|
+
if (!toChain) {
|
|
140
|
+
throw new errors_1.ValidationError('Required parameter "toChain" is missing.');
|
|
141
|
+
}
|
|
142
|
+
if (!toToken) {
|
|
143
|
+
throw new errors_1.ValidationError('Required parameter "toToken" is missing.');
|
|
144
|
+
}
|
|
145
|
+
configService = ConfigService_1.default.getInstance();
|
|
146
|
+
config = configService.getConfig();
|
|
147
|
+
_b.label = 1;
|
|
148
|
+
case 1:
|
|
149
|
+
_b.trys.push([1, 3, , 4]);
|
|
150
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'quote', {
|
|
151
|
+
params: {
|
|
152
|
+
fromChain: fromChain,
|
|
153
|
+
toChain: toChain,
|
|
154
|
+
fromToken: fromToken,
|
|
155
|
+
toToken: toToken,
|
|
156
|
+
fromAddress: fromAddress,
|
|
157
|
+
toAddress: toAddress,
|
|
158
|
+
fromAmount: fromAmount,
|
|
159
|
+
order: order,
|
|
160
|
+
slippage: slippage,
|
|
161
|
+
integrator: integrator,
|
|
162
|
+
referrer: referrer,
|
|
163
|
+
allowBridges: allowBridges,
|
|
164
|
+
denyBridges: denyBridges,
|
|
165
|
+
preferBridges: preferBridges,
|
|
166
|
+
allowExchanges: allowExchanges,
|
|
167
|
+
denyExchanges: denyExchanges,
|
|
168
|
+
preferExchanges: preferExchanges,
|
|
169
|
+
},
|
|
170
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
171
|
+
})];
|
|
172
|
+
case 2:
|
|
173
|
+
result = _b.sent();
|
|
174
|
+
return [2 /*return*/, result.data];
|
|
175
|
+
case 3:
|
|
176
|
+
e_3 = _b.sent();
|
|
177
|
+
throw (0, parseError_1.parseBackendError)(e_3);
|
|
178
|
+
case 4: return [2 /*return*/];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
var getStatus = function (_a, options) {
|
|
184
|
+
var bridge = _a.bridge, fromChain = _a.fromChain, toChain = _a.toChain, txHash = _a.txHash;
|
|
185
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
186
|
+
var configService, config, result, e_4;
|
|
187
|
+
return __generator(this, function (_b) {
|
|
188
|
+
switch (_b.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
if (fromChain !== toChain && !bridge) {
|
|
191
|
+
throw new errors_1.ValidationError('Parameter "bridge" is required for cross chain transfers.');
|
|
192
|
+
}
|
|
193
|
+
if (!fromChain) {
|
|
194
|
+
throw new errors_1.ValidationError('Required parameter "fromChain" is missing.');
|
|
195
|
+
}
|
|
196
|
+
if (!toChain) {
|
|
197
|
+
throw new errors_1.ValidationError('Required parameter "toChain" is missing.');
|
|
198
|
+
}
|
|
199
|
+
if (!txHash) {
|
|
200
|
+
throw new errors_1.ValidationError('Required parameter "txHash" is missing.');
|
|
201
|
+
}
|
|
202
|
+
configService = ConfigService_1.default.getInstance();
|
|
203
|
+
config = configService.getConfig();
|
|
204
|
+
_b.label = 1;
|
|
205
|
+
case 1:
|
|
206
|
+
_b.trys.push([1, 3, , 4]);
|
|
207
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'status', {
|
|
208
|
+
params: {
|
|
209
|
+
bridge: bridge,
|
|
210
|
+
fromChain: fromChain,
|
|
211
|
+
toChain: toChain,
|
|
212
|
+
txHash: txHash,
|
|
213
|
+
},
|
|
214
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
215
|
+
})];
|
|
216
|
+
case 2:
|
|
217
|
+
result = _b.sent();
|
|
218
|
+
return [2 /*return*/, result.data];
|
|
219
|
+
case 3:
|
|
220
|
+
e_4 = _b.sent();
|
|
221
|
+
throw (0, parseError_1.parseBackendError)(e_4);
|
|
222
|
+
case 4: return [2 /*return*/];
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
var getChains = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
228
|
+
var configService, config, result, e_5;
|
|
229
|
+
return __generator(this, function (_a) {
|
|
230
|
+
switch (_a.label) {
|
|
231
|
+
case 0:
|
|
232
|
+
configService = ConfigService_1.default.getInstance();
|
|
233
|
+
config = configService.getConfig();
|
|
234
|
+
_a.label = 1;
|
|
235
|
+
case 1:
|
|
236
|
+
_a.trys.push([1, 3, , 4]);
|
|
237
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'chains', {
|
|
238
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
239
|
+
})];
|
|
240
|
+
case 2:
|
|
241
|
+
result = _a.sent();
|
|
242
|
+
return [2 /*return*/, result.data.chains];
|
|
243
|
+
case 3:
|
|
244
|
+
e_5 = _a.sent();
|
|
245
|
+
throw (0, parseError_1.parseBackendError)(e_5);
|
|
246
|
+
case 4: return [2 /*return*/];
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}); };
|
|
250
|
+
var getRoutes = function (request, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
251
|
+
var configService, config, result, e_6;
|
|
252
|
+
return __generator(this, function (_a) {
|
|
253
|
+
switch (_a.label) {
|
|
254
|
+
case 0:
|
|
255
|
+
if (!(0, typeguards_1.isRoutesRequest)(request)) {
|
|
256
|
+
throw new errors_1.ValidationError('Invalid routes request.');
|
|
257
|
+
}
|
|
258
|
+
configService = ConfigService_1.default.getInstance();
|
|
259
|
+
config = configService.getConfig();
|
|
260
|
+
// apply defaults
|
|
261
|
+
request.options = __assign(__assign({}, config.defaultRouteOptions), request.options);
|
|
262
|
+
_a.label = 1;
|
|
263
|
+
case 1:
|
|
264
|
+
_a.trys.push([1, 3, , 4]);
|
|
265
|
+
return [4 /*yield*/, axios_1.default.post(config.apiUrl + 'advanced/routes', request, {
|
|
266
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
267
|
+
})];
|
|
268
|
+
case 2:
|
|
269
|
+
result = _a.sent();
|
|
270
|
+
return [2 /*return*/, result.data];
|
|
271
|
+
case 3:
|
|
272
|
+
e_6 = _a.sent();
|
|
273
|
+
throw (0, parseError_1.parseBackendError)(e_6);
|
|
274
|
+
case 4: return [2 /*return*/];
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}); };
|
|
278
|
+
var getStepTransaction = function (step, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
279
|
+
var configService, config, result, e_7;
|
|
280
|
+
return __generator(this, function (_a) {
|
|
281
|
+
switch (_a.label) {
|
|
282
|
+
case 0:
|
|
283
|
+
if (!(0, typeguards_1.isStep)(step)) {
|
|
284
|
+
// While the validation fails for some users we should not enforce it
|
|
285
|
+
// eslint-disable-next-line no-console
|
|
286
|
+
console.warn('SDK Validation: Invalid Step', step);
|
|
287
|
+
}
|
|
288
|
+
configService = ConfigService_1.default.getInstance();
|
|
289
|
+
config = configService.getConfig();
|
|
290
|
+
_a.label = 1;
|
|
291
|
+
case 1:
|
|
292
|
+
_a.trys.push([1, 3, , 4]);
|
|
293
|
+
return [4 /*yield*/, axios_1.default.post(config.apiUrl + 'advanced/stepTransaction', step, {
|
|
294
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
295
|
+
})];
|
|
296
|
+
case 2:
|
|
297
|
+
result = _a.sent();
|
|
298
|
+
return [2 /*return*/, result.data];
|
|
299
|
+
case 3:
|
|
300
|
+
e_7 = _a.sent();
|
|
301
|
+
throw (0, parseError_1.parseBackendError)(e_7);
|
|
302
|
+
case 4: return [2 /*return*/];
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}); };
|
|
306
|
+
var getTools = function (request, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
307
|
+
var configService, config, r;
|
|
308
|
+
return __generator(this, function (_a) {
|
|
309
|
+
switch (_a.label) {
|
|
310
|
+
case 0:
|
|
311
|
+
configService = ConfigService_1.default.getInstance();
|
|
312
|
+
config = configService.getConfig();
|
|
313
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'tools', {
|
|
314
|
+
params: request,
|
|
315
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
316
|
+
})];
|
|
317
|
+
case 1:
|
|
318
|
+
r = _a.sent();
|
|
319
|
+
return [2 /*return*/, r.data];
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}); };
|
|
323
|
+
var getTokens = function (request, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
324
|
+
var configService, config, r;
|
|
325
|
+
return __generator(this, function (_a) {
|
|
326
|
+
switch (_a.label) {
|
|
327
|
+
case 0:
|
|
328
|
+
configService = ConfigService_1.default.getInstance();
|
|
329
|
+
config = configService.getConfig();
|
|
330
|
+
return [4 /*yield*/, axios_1.default.get(config.apiUrl + 'tokens', {
|
|
331
|
+
params: request,
|
|
332
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
333
|
+
})];
|
|
334
|
+
case 1:
|
|
335
|
+
r = _a.sent();
|
|
336
|
+
return [2 /*return*/, r.data];
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}); };
|
|
340
|
+
exports.default = {
|
|
341
|
+
getPossibilities: getPossibilities,
|
|
342
|
+
getToken: getToken,
|
|
343
|
+
getQuote: getQuote,
|
|
344
|
+
getStatus: getStatus,
|
|
345
|
+
getChains: getChains,
|
|
346
|
+
getRoutes: getRoutes,
|
|
347
|
+
getStepTransaction: getStepTransaction,
|
|
348
|
+
getTools: getTools,
|
|
349
|
+
getTokens: getTokens,
|
|
350
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Chain, ChainId } from '@lifi/types';
|
|
2
|
+
export default class ChainsService {
|
|
3
|
+
private static instance;
|
|
4
|
+
private readonly loadingPromise;
|
|
5
|
+
private chains;
|
|
6
|
+
constructor();
|
|
7
|
+
private loadAvailableChains;
|
|
8
|
+
static getInstance(): ChainsService;
|
|
9
|
+
getChainById(chainId: ChainId): Promise<Chain>;
|
|
10
|
+
getChains(): Promise<Chain[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
var errors_1 = require("../utils/errors");
|
|
43
|
+
var ApiService_1 = __importDefault(require("./ApiService"));
|
|
44
|
+
var ChainsService = /** @class */ (function () {
|
|
45
|
+
function ChainsService() {
|
|
46
|
+
this.chains = [];
|
|
47
|
+
this.loadingPromise = this.loadAvailableChains();
|
|
48
|
+
}
|
|
49
|
+
ChainsService.prototype.loadAvailableChains = function () {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
var _a;
|
|
52
|
+
return __generator(this, function (_b) {
|
|
53
|
+
switch (_b.label) {
|
|
54
|
+
case 0:
|
|
55
|
+
_a = this;
|
|
56
|
+
return [4 /*yield*/, ApiService_1.default.getChains()];
|
|
57
|
+
case 1:
|
|
58
|
+
_a.chains = _b.sent();
|
|
59
|
+
return [2 /*return*/];
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
ChainsService.getInstance = function () {
|
|
65
|
+
if (!this.instance) {
|
|
66
|
+
this.instance = new ChainsService();
|
|
67
|
+
}
|
|
68
|
+
return this.instance;
|
|
69
|
+
};
|
|
70
|
+
ChainsService.prototype.getChainById = function (chainId) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
+
var chain;
|
|
73
|
+
return __generator(this, function (_a) {
|
|
74
|
+
switch (_a.label) {
|
|
75
|
+
case 0:
|
|
76
|
+
if (!this.loadingPromise) return [3 /*break*/, 2];
|
|
77
|
+
return [4 /*yield*/, this.loadingPromise];
|
|
78
|
+
case 1:
|
|
79
|
+
_a.sent();
|
|
80
|
+
_a.label = 2;
|
|
81
|
+
case 2:
|
|
82
|
+
chain = this.chains.find(function (chain) { return chain.id === chainId; });
|
|
83
|
+
if (!chain) {
|
|
84
|
+
throw new errors_1.ValidationError("Unknown chainId passed: ".concat(chainId, "."));
|
|
85
|
+
}
|
|
86
|
+
return [2 /*return*/, chain];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
ChainsService.prototype.getChains = function () {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
switch (_a.label) {
|
|
95
|
+
case 0:
|
|
96
|
+
if (!this.loadingPromise) return [3 /*break*/, 2];
|
|
97
|
+
return [4 /*yield*/, this.loadingPromise];
|
|
98
|
+
case 1:
|
|
99
|
+
_a.sent();
|
|
100
|
+
_a.label = 2;
|
|
101
|
+
case 2: return [2 /*return*/, this.chains];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
return ChainsService;
|
|
107
|
+
}());
|
|
108
|
+
exports.default = ChainsService;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Chain, Config, ConfigUpdate } from '../types';
|
|
2
|
+
export default class ConfigService {
|
|
3
|
+
private static instance;
|
|
4
|
+
private readonly config;
|
|
5
|
+
private readonly setupPromise;
|
|
6
|
+
private resolveSetupPromise;
|
|
7
|
+
constructor();
|
|
8
|
+
private static chainIdToObject;
|
|
9
|
+
private static getDefaultConfig;
|
|
10
|
+
static getInstance(): ConfigService;
|
|
11
|
+
/**
|
|
12
|
+
* This call immediately returns the current config. It does not make sure that all chain data is already loaded
|
|
13
|
+
* Use this if you need access to basic information like API urls or settings
|
|
14
|
+
*/
|
|
15
|
+
getConfig: () => Config;
|
|
16
|
+
/**
|
|
17
|
+
* This call waits for all setup promises to be done.
|
|
18
|
+
* Use this if you need access to chain data (RPCs or multicalls)
|
|
19
|
+
*/
|
|
20
|
+
getConfigAsync: () => Promise<Config>;
|
|
21
|
+
updateConfig: (configUpdate: ConfigUpdate) => Config;
|
|
22
|
+
updateChains: (chains: Chain[]) => Config;
|
|
23
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var types_1 = require("../types");
|
|
40
|
+
var DefaultExecutionSettings = {
|
|
41
|
+
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
|
|
42
|
+
updateCallback: function () { },
|
|
43
|
+
switchChainHook: function () { return Promise.resolve(undefined); },
|
|
44
|
+
infiniteApproval: false,
|
|
45
|
+
};
|
|
46
|
+
var ConfigService = /** @class */ (function () {
|
|
47
|
+
function ConfigService() {
|
|
48
|
+
var _this = this;
|
|
49
|
+
this.resolveSetupPromise = undefined;
|
|
50
|
+
/**
|
|
51
|
+
* This call immediately returns the current config. It does not make sure that all chain data is already loaded
|
|
52
|
+
* Use this if you need access to basic information like API urls or settings
|
|
53
|
+
*/
|
|
54
|
+
this.getConfig = function () {
|
|
55
|
+
return _this.config;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* This call waits for all setup promises to be done.
|
|
59
|
+
* Use this if you need access to chain data (RPCs or multicalls)
|
|
60
|
+
*/
|
|
61
|
+
this.getConfigAsync = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
62
|
+
return __generator(this, function (_a) {
|
|
63
|
+
switch (_a.label) {
|
|
64
|
+
case 0: return [4 /*yield*/, this.setupPromise];
|
|
65
|
+
case 1:
|
|
66
|
+
_a.sent();
|
|
67
|
+
return [2 /*return*/, this.config];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}); };
|
|
71
|
+
this.updateConfig = function (configUpdate) {
|
|
72
|
+
// API
|
|
73
|
+
_this.config.apiUrl = configUpdate.apiUrl || _this.config.apiUrl;
|
|
74
|
+
// RPCS
|
|
75
|
+
_this.config.rpcs = Object.assign(_this.config.rpcs, configUpdate.rpcs);
|
|
76
|
+
// MULTICALL
|
|
77
|
+
_this.config.multicallAddresses = Object.assign(_this.config.multicallAddresses, configUpdate.multicallAddresses);
|
|
78
|
+
// SETTINGS
|
|
79
|
+
_this.config.defaultExecutionSettings = Object.assign(_this.config.defaultExecutionSettings, configUpdate.defaultExecutionSettings);
|
|
80
|
+
// OPTIONS
|
|
81
|
+
_this.config.defaultRouteOptions = Object.assign(_this.config.defaultRouteOptions, configUpdate.defaultRouteOptions);
|
|
82
|
+
return _this.config;
|
|
83
|
+
};
|
|
84
|
+
this.updateChains = function (chains) {
|
|
85
|
+
var _a;
|
|
86
|
+
for (var _i = 0, chains_1 = chains; _i < chains_1.length; _i++) {
|
|
87
|
+
var chain = chains_1[_i];
|
|
88
|
+
var chainId = chain.id;
|
|
89
|
+
// set RPCs if they were not configured by the user before
|
|
90
|
+
if (!_this.config.rpcs[chainId].length) {
|
|
91
|
+
_this.config.rpcs[chainId] = chain.metamask.rpcUrls;
|
|
92
|
+
}
|
|
93
|
+
// set multicall addresses if they exist and were not configured by the user before
|
|
94
|
+
if (chain.multicallAddress && !_this.config.multicallAddresses[chainId]) {
|
|
95
|
+
_this.config.multicallAddresses[chainId] = chain.multicallAddress;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
(_a = _this.resolveSetupPromise) === null || _a === void 0 ? void 0 : _a.call(_this);
|
|
99
|
+
return _this.config;
|
|
100
|
+
};
|
|
101
|
+
this.config = ConfigService.getDefaultConfig();
|
|
102
|
+
this.setupPromise = new Promise(function (resolve) {
|
|
103
|
+
_this.resolveSetupPromise = resolve;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
ConfigService.chainIdToObject = function (val) {
|
|
107
|
+
var result = {};
|
|
108
|
+
var values = Object.values(types_1.ChainId);
|
|
109
|
+
values.forEach(function (chainId) {
|
|
110
|
+
if (typeof chainId !== 'string') {
|
|
111
|
+
result[chainId] = val ? JSON.parse(JSON.stringify(val)) : val;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
116
|
+
ConfigService.getInstance = function () {
|
|
117
|
+
if (!this.instance) {
|
|
118
|
+
this.instance = new ConfigService();
|
|
119
|
+
}
|
|
120
|
+
return this.instance;
|
|
121
|
+
};
|
|
122
|
+
ConfigService.getDefaultConfig = function () {
|
|
123
|
+
return {
|
|
124
|
+
apiUrl: 'https://li.quest/v1/',
|
|
125
|
+
rpcs: ConfigService.chainIdToObject([]),
|
|
126
|
+
multicallAddresses: ConfigService.chainIdToObject(undefined),
|
|
127
|
+
defaultExecutionSettings: DefaultExecutionSettings,
|
|
128
|
+
defaultRouteOptions: {},
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
return ConfigService;
|
|
132
|
+
}());
|
|
133
|
+
exports.default = ConfigService;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { RoutesRequest, Step, Token } from './types';
|
|
2
|
+
export declare const isRoutesRequest: (routesRequest: RoutesRequest) => routesRequest is RoutesRequest;
|
|
3
|
+
export declare const isStep: (step: Step) => step is Step;
|
|
4
|
+
export declare const isToken: (token: Token) => token is Token;
|