@lifi/sdk 1.0.2 → 1.1.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 +17 -0
- package/dist/Lifi.d.ts +15 -1
- package/dist/Lifi.js +25 -10
- package/dist/execution/StatusManager.d.ts +3 -1
- package/dist/execution/StatusManager.js +16 -1
- package/dist/execution/bridges/bridge.execute.js +63 -45
- package/dist/execution/exchanges/swap.execute.js +68 -50
- package/dist/execution/stepComparison.d.ts +14 -0
- package/dist/execution/stepComparison.js +81 -0
- package/dist/execution/utils.d.ts +1 -0
- package/dist/execution/utils.js +12 -1
- package/dist/services/ApiService.d.ts +3 -2
- package/dist/services/ApiService.js +126 -72
- package/dist/services/ConfigService.js +1 -0
- package/dist/types/internal.types.d.ts +13 -3
- package/dist/utils/errors.d.ts +3 -1
- package/dist/utils/errors.js +2 -0
- package/dist/utils/parseError.d.ts +1 -0
- package/dist/utils/parseError.js +40 -26
- package/dist/utils/preRestart.d.ts +2 -0
- package/dist/utils/preRestart.js +34 -0
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Route, RouteOptions, Step, Token } from '@lifi/types';
|
|
2
2
|
import BigNumber from 'bignumber.js';
|
|
3
3
|
import { Signer } from 'ethers';
|
|
4
4
|
import { ChainId } from '.';
|
|
@@ -23,10 +23,10 @@ interface ExecutionParams {
|
|
|
23
23
|
settings: InternalExecutionSettings;
|
|
24
24
|
}
|
|
25
25
|
export interface ExecuteSwapParams extends ExecutionParams {
|
|
26
|
-
step:
|
|
26
|
+
step: Step;
|
|
27
27
|
}
|
|
28
28
|
export interface ExecuteCrossParams extends ExecutionParams {
|
|
29
|
-
step:
|
|
29
|
+
step: Step;
|
|
30
30
|
}
|
|
31
31
|
export declare type CallbackFunction = (updatedRoute: Route) => void;
|
|
32
32
|
export declare type Config = {
|
|
@@ -44,6 +44,14 @@ export declare type ConfigUpdate = {
|
|
|
44
44
|
defaultRouteOptions?: RouteOptions;
|
|
45
45
|
};
|
|
46
46
|
export declare type SwitchChainHook = (requiredChainId: number) => Promise<Signer | undefined>;
|
|
47
|
+
export interface AcceptSlippageUpdateHookParams {
|
|
48
|
+
toToken: Token;
|
|
49
|
+
oldToAmount: string;
|
|
50
|
+
newToAmount: string;
|
|
51
|
+
oldSlippage: number;
|
|
52
|
+
newSlippage: number;
|
|
53
|
+
}
|
|
54
|
+
export declare type AcceptSlippageUpdateHook = (params: AcceptSlippageUpdateHookParams) => Promise<boolean | undefined>;
|
|
47
55
|
export interface ExecutionData {
|
|
48
56
|
route: Route;
|
|
49
57
|
executors: StepExecutor[];
|
|
@@ -52,11 +60,13 @@ export interface ExecutionData {
|
|
|
52
60
|
export interface ExecutionSettings {
|
|
53
61
|
updateCallback?: CallbackFunction;
|
|
54
62
|
switchChainHook?: SwitchChainHook;
|
|
63
|
+
acceptSlippageUpdateHook?: AcceptSlippageUpdateHook;
|
|
55
64
|
infiniteApproval?: boolean;
|
|
56
65
|
}
|
|
57
66
|
export interface InternalExecutionSettings extends ExecutionSettings {
|
|
58
67
|
updateCallback: CallbackFunction;
|
|
59
68
|
switchChainHook: SwitchChainHook;
|
|
69
|
+
acceptSlippageUpdateHook: AcceptSlippageUpdateHook;
|
|
60
70
|
infiniteApproval: boolean;
|
|
61
71
|
}
|
|
62
72
|
export declare type EnforcedObjectProperties<T> = T & {
|
package/dist/utils/errors.d.ts
CHANGED
|
@@ -18,7 +18,9 @@ export declare enum LifiErrorCode {
|
|
|
18
18
|
ProviderUnavailable = 1005,
|
|
19
19
|
NotFound = 1006,
|
|
20
20
|
ChainSwitchError = 1007,
|
|
21
|
-
|
|
21
|
+
SlippageNotMet = 1008,
|
|
22
|
+
SlippageError = 1008,
|
|
23
|
+
GasLimitError = 1009
|
|
22
24
|
}
|
|
23
25
|
export declare enum MetaMaskRPCErrorCode {
|
|
24
26
|
invalidInput = -32000,
|
package/dist/utils/errors.js
CHANGED
|
@@ -38,7 +38,9 @@ var LifiErrorCode;
|
|
|
38
38
|
LifiErrorCode[LifiErrorCode["ProviderUnavailable"] = 1005] = "ProviderUnavailable";
|
|
39
39
|
LifiErrorCode[LifiErrorCode["NotFound"] = 1006] = "NotFound";
|
|
40
40
|
LifiErrorCode[LifiErrorCode["ChainSwitchError"] = 1007] = "ChainSwitchError";
|
|
41
|
+
LifiErrorCode[LifiErrorCode["SlippageNotMet"] = 1008] = "SlippageNotMet";
|
|
41
42
|
LifiErrorCode[LifiErrorCode["SlippageError"] = 1008] = "SlippageError";
|
|
43
|
+
LifiErrorCode[LifiErrorCode["GasLimitError"] = 1009] = "GasLimitError";
|
|
42
44
|
})(LifiErrorCode = exports.LifiErrorCode || (exports.LifiErrorCode = {}));
|
|
43
45
|
var MetaMaskRPCErrorCode;
|
|
44
46
|
(function (MetaMaskRPCErrorCode) {
|
|
@@ -35,3 +35,4 @@ export declare const getTransactionNotSentMessage: (step?: Step, process?: Proce
|
|
|
35
35
|
export declare const getTransactionFailedMessage: (step: Step, txLink?: string) => string;
|
|
36
36
|
export declare const parseError: (e: any, step?: Step, process?: Process) => Promise<LifiError>;
|
|
37
37
|
export declare const parseBackendError: (e: any) => LifiError;
|
|
38
|
+
export declare const getSlippageNotMetMessage: (step: Step) => string;
|
package/dist/utils/parseError.js
CHANGED
|
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.parseBackendError = exports.parseError = exports.getTransactionFailedMessage = exports.getTransactionNotSentMessage = void 0;
|
|
42
|
+
exports.getSlippageNotMetMessage = exports.parseBackendError = exports.parseError = exports.getTransactionFailedMessage = exports.getTransactionNotSentMessage = void 0;
|
|
43
43
|
var types_1 = require("@lifi/types");
|
|
44
44
|
var eth_rpc_errors_1 = require("eth-rpc-errors");
|
|
45
45
|
var ChainsService_1 = __importDefault(require("../services/ChainsService"));
|
|
@@ -110,16 +110,17 @@ var getTransactionFailedMessage = function (step, txLink) {
|
|
|
110
110
|
};
|
|
111
111
|
exports.getTransactionFailedMessage = getTransactionFailedMessage;
|
|
112
112
|
var parseError = function (e, step, process) { return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
114
|
+
var _o, _p;
|
|
115
|
+
return __generator(this, function (_q) {
|
|
116
|
+
switch (_q.label) {
|
|
116
117
|
case 0:
|
|
117
118
|
if (e instanceof errors_1.LifiError) {
|
|
118
119
|
return [2 /*return*/, e];
|
|
119
120
|
}
|
|
120
|
-
if (!e.code) return [3 /*break*/,
|
|
121
|
-
if (!(typeof e.code === 'number')) return [3 /*break*/,
|
|
122
|
-
if (!Object.values(eth_rpc_errors_1.errorCodes.rpc).includes(e.code)) return [3 /*break*/,
|
|
121
|
+
if (!e.code) return [3 /*break*/, 13];
|
|
122
|
+
if (!(typeof e.code === 'number')) return [3 /*break*/, 8];
|
|
123
|
+
if (!Object.values(eth_rpc_errors_1.errorCodes.rpc).includes(e.code)) return [3 /*break*/, 6];
|
|
123
124
|
if (!(e.code === eth_rpc_errors_1.errorCodes.rpc.internal &&
|
|
124
125
|
e.message &&
|
|
125
126
|
e.message.includes('underpriced'))) return [3 /*break*/, 2];
|
|
@@ -127,40 +128,48 @@ var parseError = function (e, step, process) { return __awaiter(void 0, void 0,
|
|
|
127
128
|
_b = [void 0, errors_1.LifiErrorCode.TransactionUnderpriced,
|
|
128
129
|
'Transaction is underpriced.'];
|
|
129
130
|
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
130
|
-
case 1: return [2 /*return*/, new (_a.apply(errors_1.RPCError, _b.concat([
|
|
131
|
+
case 1: return [2 /*return*/, new (_a.apply(errors_1.RPCError, _b.concat([_q.sent(), e.stack])))()];
|
|
131
132
|
case 2:
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
if (!(((_o = e.message) === null || _o === void 0 ? void 0 : _o.includes('intrinsic gas too low')) ||
|
|
134
|
+
((_p = e.message) === null || _p === void 0 ? void 0 : _p.includes('out of gas')))) return [3 /*break*/, 4];
|
|
135
|
+
_c = errors_1.TransactionError.bind;
|
|
136
|
+
_d = [void 0, errors_1.LifiErrorCode.GasLimitError,
|
|
137
|
+
'Gas limit is too low.'];
|
|
135
138
|
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
136
|
-
case 3: return [2 /*return*/, new (_c.apply(errors_1.
|
|
139
|
+
case 3: return [2 /*return*/, new (_c.apply(errors_1.TransactionError, _d.concat([_q.sent(), e.stack])))()];
|
|
137
140
|
case 4:
|
|
138
|
-
|
|
139
|
-
_e = errors_1.ProviderError.bind;
|
|
141
|
+
_e = errors_1.RPCError.bind;
|
|
140
142
|
_f = [void 0, e.code,
|
|
141
143
|
(0, eth_rpc_errors_1.getMessageFromCode)(e.code)];
|
|
142
144
|
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
143
|
-
case 5: return [2 /*return*/, new (_e.apply(errors_1.
|
|
145
|
+
case 5: return [2 /*return*/, new (_e.apply(errors_1.RPCError, _f.concat([_q.sent(), e.stack])))()];
|
|
144
146
|
case 6:
|
|
145
|
-
if (!(e.code
|
|
147
|
+
if (!Object.values(eth_rpc_errors_1.errorCodes.provider).includes(e.code)) return [3 /*break*/, 8];
|
|
146
148
|
_g = errors_1.ProviderError.bind;
|
|
147
|
-
_h = [void 0,
|
|
148
|
-
e.
|
|
149
|
+
_h = [void 0, e.code,
|
|
150
|
+
(0, eth_rpc_errors_1.getMessageFromCode)(e.code)];
|
|
149
151
|
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
150
|
-
case 7: return [2 /*return*/, new (_g.apply(errors_1.ProviderError, _h.concat([
|
|
152
|
+
case 7: return [2 /*return*/, new (_g.apply(errors_1.ProviderError, _h.concat([_q.sent(), e.stack])))()];
|
|
151
153
|
case 8:
|
|
152
|
-
if (!(e.code ===
|
|
153
|
-
_j = errors_1.
|
|
154
|
-
_k = [void 0, errors_1.LifiErrorCode.
|
|
155
|
-
e.
|
|
154
|
+
if (!(e.code === 'CALL_EXCEPTION')) return [3 /*break*/, 10];
|
|
155
|
+
_j = errors_1.ProviderError.bind;
|
|
156
|
+
_k = [void 0, errors_1.LifiErrorCode.TransactionFailed,
|
|
157
|
+
e.reason];
|
|
156
158
|
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
157
|
-
case 9: return [2 /*return*/, new (_j.apply(errors_1.
|
|
159
|
+
case 9: return [2 /*return*/, new (_j.apply(errors_1.ProviderError, _k.concat([_q.sent(), e.stack])))()];
|
|
158
160
|
case 10:
|
|
161
|
+
if (!(e.code === errors_1.LifiErrorCode.TransactionUnprepared)) return [3 /*break*/, 12];
|
|
162
|
+
_l = errors_1.TransactionError.bind;
|
|
163
|
+
_m = [void 0, errors_1.LifiErrorCode.TransactionUnprepared,
|
|
164
|
+
e.message];
|
|
165
|
+
return [4 /*yield*/, (0, exports.getTransactionNotSentMessage)(step, process)];
|
|
166
|
+
case 11: return [2 /*return*/, new (_l.apply(errors_1.TransactionError, _m.concat([_q.sent(), e.stack])))()];
|
|
167
|
+
case 12:
|
|
159
168
|
if (e.code === errors_1.LifiErrorCode.ValidationError) {
|
|
160
169
|
return [2 /*return*/, new errors_1.TransactionError(errors_1.LifiErrorCode.ValidationError, e.message, e.htmlMessage)];
|
|
161
170
|
}
|
|
162
|
-
|
|
163
|
-
case
|
|
171
|
+
_q.label = 13;
|
|
172
|
+
case 13: return [2 /*return*/, new errors_1.UnknownError(errors_1.LifiErrorCode.InternalError, e.message || 'Unknown error occurred.', undefined, e.stack)];
|
|
164
173
|
}
|
|
165
174
|
});
|
|
166
175
|
}); };
|
|
@@ -182,3 +191,8 @@ var parseBackendError = function (e) {
|
|
|
182
191
|
return new errors_1.ServerError('Something went wrong.', undefined, e.stack);
|
|
183
192
|
};
|
|
184
193
|
exports.parseBackendError = parseBackendError;
|
|
194
|
+
var getSlippageNotMetMessage = function (step) {
|
|
195
|
+
var slippage = step.action.slippage;
|
|
196
|
+
return "Transaction was not sent, your funds are still in your wallet.\n The updated quote for the current transaction does not meet your set slippage of ".concat(slippage * 100, "%.");
|
|
197
|
+
};
|
|
198
|
+
exports.getSlippageNotMetMessage = getSlippageNotMetMessage;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handlePreRestart = void 0;
|
|
4
|
+
var errors_1 = require("./errors");
|
|
5
|
+
var handlePreRestart = function (route) {
|
|
6
|
+
var _a;
|
|
7
|
+
for (var index = 0; index < route.steps.length; index++) {
|
|
8
|
+
var stepHasFailed = ((_a = route.steps[index].execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED';
|
|
9
|
+
if (stepHasFailed) {
|
|
10
|
+
handleErrorType(route, index);
|
|
11
|
+
popLastProcess(route, index);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.handlePreRestart = handlePreRestart;
|
|
16
|
+
var handleErrorType = function (route, index) {
|
|
17
|
+
var _a, _b, _c, _d;
|
|
18
|
+
var isGasLimitError = (_a = route.steps[index].execution) === null || _a === void 0 ? void 0 : _a.process.some(function (p) { var _a; return ((_a = p.error) === null || _a === void 0 ? void 0 : _a.code) === errors_1.LifiErrorCode.GasLimitError; });
|
|
19
|
+
var isGasPriceError = (_b = route.steps[index].execution) === null || _b === void 0 ? void 0 : _b.process.some(function (p) { var _a; return ((_a = p.error) === null || _a === void 0 ? void 0 : _a.code) === errors_1.LifiErrorCode.TransactionUnderpriced; });
|
|
20
|
+
if (isGasLimitError) {
|
|
21
|
+
(_c = route.steps[index].estimate.gasCosts) === null || _c === void 0 ? void 0 : _c.forEach(function (gasCost) {
|
|
22
|
+
return (gasCost.limit = "".concat(Math.round(Number(gasCost.limit) * 1.25)));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (isGasPriceError) {
|
|
26
|
+
(_d = route.steps[index].estimate.gasCosts) === null || _d === void 0 ? void 0 : _d.forEach(function (gasCost) {
|
|
27
|
+
return (gasCost.price = "".concat(Math.round(Number(gasCost.price) * 1.25)));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var popLastProcess = function (route, index) {
|
|
32
|
+
var _a;
|
|
33
|
+
(_a = route.steps[index].execution) === null || _a === void 0 ? void 0 : _a.process.pop();
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/sdk",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "LI.FI Any-to-Any Cross-Chain-Swap SDK",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@ethersproject/abi": "^5.6.4",
|
|
54
54
|
"@ethersproject/contracts": "^5.6.2",
|
|
55
|
-
"@lifi/types": "^1.
|
|
55
|
+
"@lifi/types": "^1.3.0",
|
|
56
56
|
"axios": "^0.27.2",
|
|
57
57
|
"bignumber.js": "^9.0.1",
|
|
58
58
|
"eth-rpc-errors": "^4.0.3",
|