@lifi/sdk 1.0.0 → 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.
@@ -41,6 +41,7 @@ var DefaultExecutionSettings = {
41
41
  /* eslint-disable-next-line @typescript-eslint/no-empty-function */
42
42
  updateCallback: function () { },
43
43
  switchChainHook: function () { return Promise.resolve(undefined); },
44
+ acceptSlippageUpdateHook: function () { return Promise.resolve(undefined); },
44
45
  infiniteApproval: false,
45
46
  };
46
47
  var ConfigService = /** @class */ (function () {
@@ -1,4 +1,4 @@
1
- import { CrossStep, LifiStep, Route, RouteOptions, Step, SwapStep, Token } from '@lifi/types';
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: SwapStep;
26
+ step: Step;
27
27
  }
28
28
  export interface ExecuteCrossParams extends ExecutionParams {
29
- step: CrossStep | LifiStep;
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 & {
@@ -18,7 +18,9 @@ export declare enum LifiErrorCode {
18
18
  ProviderUnavailable = 1005,
19
19
  NotFound = 1006,
20
20
  ChainSwitchError = 1007,
21
- SlippageError = 1008
21
+ SlippageNotMet = 1008,
22
+ SlippageError = 1008,
23
+ GasLimitError = 1009
22
24
  }
23
25
  export declare enum MetaMaskRPCErrorCode {
24
26
  invalidInput = -32000,
@@ -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) {
@@ -79,11 +79,25 @@ var fetchDataUsingMulticall = function (calls, abi, chainId, multicallAddress, r
79
79
  return [2 /*return*/, returnData
80
80
  .map(function (_a, i) {
81
81
  var success = _a.success, returnData = _a.returnData;
82
- if (success) {
82
+ if (!success) {
83
+ // requested function failed
84
+ console.error("Multicall unsuccessful for address \"".concat(chunkedCalls[i].address, "\", ") +
85
+ "function \"".concat(chunkedCalls[i].name, "\", chainId \"").concat(chainId, "\""));
86
+ return [];
87
+ }
88
+ if (returnData.toString() === '0x') {
89
+ // requested function does probably not exist
90
+ console.error("Multicall no response for address \"".concat(chunkedCalls[i].address, "\", ") +
91
+ "function \"".concat(chunkedCalls[i].name, "\", chainId \"").concat(chainId, "\""));
92
+ return [];
93
+ }
94
+ try {
83
95
  return abiInterface.decodeFunctionResult(chunkedCalls[i].name, returnData);
84
96
  }
85
- else {
86
- console.error("Multicall unsuccessful for address \"".concat(chunkedCalls[i].address, "\", function \"").concat(chunkedCalls[i].name, "\", chainId \"").concat(chainId, "\""));
97
+ catch (e) {
98
+ // requested function returns other data than expected
99
+ console.error("Multicall parsing unsuccessful for address \"".concat(chunkedCalls[i].address, "\", ") +
100
+ "function \"".concat(chunkedCalls[i].name, "\", chainId \"").concat(chainId, "\""));
87
101
  return [];
88
102
  }
89
103
  })
@@ -95,6 +109,7 @@ var fetchDataUsingMulticall = function (calls, abi, chainId, multicallAddress, r
95
109
  })];
96
110
  case 3:
97
111
  e_1 = _b.sent();
112
+ // whole rpc call failed, probably an rpc issue
98
113
  console.error("Multicall failed on chainId \"".concat(chainId, "\""), chunkedList, e_1);
99
114
  return [2 /*return*/, []];
100
115
  case 4: return [2 /*return*/];
@@ -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;
@@ -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
- return __generator(this, function (_l) {
115
- switch (_l.label) {
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*/, 11];
121
- if (!(typeof e.code === 'number')) return [3 /*break*/, 6];
122
- if (!Object.values(eth_rpc_errors_1.errorCodes.rpc).includes(e.code)) return [3 /*break*/, 4];
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([_l.sent(), e.stack])))()];
131
+ case 1: return [2 /*return*/, new (_a.apply(errors_1.RPCError, _b.concat([_q.sent(), e.stack])))()];
131
132
  case 2:
132
- _c = errors_1.RPCError.bind;
133
- _d = [void 0, e.code,
134
- (0, eth_rpc_errors_1.getMessageFromCode)(e.code)];
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.RPCError, _d.concat([_l.sent(), e.stack])))()];
139
+ case 3: return [2 /*return*/, new (_c.apply(errors_1.TransactionError, _d.concat([_q.sent(), e.stack])))()];
137
140
  case 4:
138
- if (!Object.values(eth_rpc_errors_1.errorCodes.provider).includes(e.code)) return [3 /*break*/, 6];
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.ProviderError, _f.concat([_l.sent(), e.stack])))()];
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 === 'CALL_EXCEPTION')) return [3 /*break*/, 8];
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, errors_1.LifiErrorCode.TransactionFailed,
148
- e.reason];
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([_l.sent(), e.stack])))()];
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 === errors_1.LifiErrorCode.TransactionUnprepared)) return [3 /*break*/, 10];
153
- _j = errors_1.TransactionError.bind;
154
- _k = [void 0, errors_1.LifiErrorCode.TransactionUnprepared,
155
- e.message];
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.TransactionError, _k.concat([_l.sent(), e.stack])))()];
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
- _l.label = 11;
163
- case 11: return [2 /*return*/, new errors_1.UnknownError(errors_1.LifiErrorCode.InternalError, e.message || 'Unknown error occurred.', undefined, e.stack)];
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,2 @@
1
+ import { Route } from '../types';
2
+ export declare const handlePreRestart: (route: Route) => void;
@@ -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.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",
@@ -40,7 +40,7 @@
40
40
  "access": "public"
41
41
  },
42
42
  "author": "Max Klenk <max@li.finance>",
43
- "license": "UNLICENSED",
43
+ "license": "Apache-2.0",
44
44
  "homepage": "https://github.com/lifinance/sdk",
45
45
  "repository": {
46
46
  "type": "git",
@@ -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.0.0",
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",
package/LICENSE DELETED
@@ -1 +0,0 @@
1
- UNLICENSED