@lifi/sdk 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/dist/cjs/Lifi.d.ts +200 -0
  3. package/dist/cjs/Lifi.js +376 -0
  4. package/dist/cjs/allowance/index.d.ts +22 -0
  5. package/dist/cjs/allowance/index.js +78 -0
  6. package/dist/cjs/allowance/utils.d.ts +14 -0
  7. package/dist/cjs/allowance/utils.js +82 -0
  8. package/dist/cjs/balances/index.d.ts +11 -0
  9. package/dist/cjs/balances/index.js +46 -0
  10. package/dist/cjs/balances/utils.d.ts +5 -0
  11. package/dist/cjs/balances/utils.js +150 -0
  12. package/dist/cjs/connectors.d.ts +6 -0
  13. package/dist/cjs/connectors.js +77 -0
  14. package/dist/cjs/execution/StatusManager.d.ts +65 -0
  15. package/dist/cjs/execution/StatusManager.js +167 -0
  16. package/dist/cjs/execution/StepExecutor.d.ts +15 -0
  17. package/dist/cjs/execution/StepExecutor.js +74 -0
  18. package/dist/cjs/execution/allowance.execute.d.ts +4 -0
  19. package/dist/cjs/execution/allowance.execute.js +97 -0
  20. package/dist/cjs/execution/balanceCheck.execute.d.ts +3 -0
  21. package/dist/cjs/execution/balanceCheck.execute.js +48 -0
  22. package/dist/cjs/execution/bridges/bridge.execute.d.ts +7 -0
  23. package/dist/cjs/execution/bridges/bridge.execute.js +154 -0
  24. package/dist/cjs/execution/exchanges/swap.execute.d.ts +7 -0
  25. package/dist/cjs/execution/exchanges/swap.execute.js +164 -0
  26. package/dist/cjs/execution/index.d.ts +1 -0
  27. package/dist/cjs/execution/index.js +17 -0
  28. package/dist/cjs/execution/stepComparison.d.ts +14 -0
  29. package/dist/cjs/execution/stepComparison.js +46 -0
  30. package/dist/cjs/execution/switchChain.d.ts +16 -0
  31. package/dist/cjs/execution/switchChain.js +58 -0
  32. package/dist/cjs/execution/utils.d.ts +6 -0
  33. package/dist/cjs/execution/utils.js +137 -0
  34. package/dist/cjs/helpers.d.ts +18 -0
  35. package/dist/cjs/helpers.js +54 -0
  36. package/dist/cjs/index.d.ts +6 -0
  37. package/dist/cjs/index.js +27 -0
  38. package/dist/cjs/services/ApiService.d.ts +15 -0
  39. package/dist/cjs/services/ApiService.js +272 -0
  40. package/dist/cjs/services/ChainsService.d.ts +11 -0
  41. package/dist/cjs/services/ChainsService.js +54 -0
  42. package/dist/cjs/services/ConfigService.d.ts +23 -0
  43. package/dist/cjs/services/ConfigService.js +98 -0
  44. package/dist/cjs/typeguards.d.ts +4 -0
  45. package/dist/cjs/typeguards.js +53 -0
  46. package/dist/cjs/types/ERC20.d.ts +22 -0
  47. package/dist/cjs/types/ERC20.js +53 -0
  48. package/dist/cjs/types/index.d.ts +4 -0
  49. package/dist/cjs/types/index.js +22 -0
  50. package/dist/cjs/types/internal.types.d.ts +85 -0
  51. package/dist/cjs/types/internal.types.js +2 -0
  52. package/dist/cjs/utils/errors.d.ts +75 -0
  53. package/dist/cjs/utils/errors.js +115 -0
  54. package/dist/cjs/utils/getProvider.d.ts +3 -0
  55. package/dist/cjs/utils/getProvider.js +11 -0
  56. package/dist/cjs/utils/multicall.d.ts +10 -0
  57. package/dist/cjs/utils/multicall.js +77 -0
  58. package/dist/cjs/utils/multicallAbi.json +313 -0
  59. package/dist/cjs/utils/parseError.d.ts +38 -0
  60. package/dist/cjs/utils/parseError.js +141 -0
  61. package/dist/cjs/utils/preRestart.d.ts +2 -0
  62. package/dist/cjs/utils/preRestart.js +31 -0
  63. package/dist/cjs/utils/utils.d.ts +26 -0
  64. package/dist/cjs/utils/utils.js +120 -0
  65. package/package.json +34 -11
@@ -0,0 +1,313 @@
1
+ [
2
+ {
3
+ "inputs": [
4
+ {
5
+ "components": [
6
+ {
7
+ "internalType": "address",
8
+ "name": "target",
9
+ "type": "address"
10
+ },
11
+ {
12
+ "internalType": "bytes",
13
+ "name": "callData",
14
+ "type": "bytes"
15
+ }
16
+ ],
17
+ "internalType": "struct Multicall2.Call[]",
18
+ "name": "calls",
19
+ "type": "tuple[]"
20
+ }
21
+ ],
22
+ "name": "aggregate",
23
+ "outputs": [
24
+ {
25
+ "internalType": "uint256",
26
+ "name": "blockNumber",
27
+ "type": "uint256"
28
+ },
29
+ {
30
+ "internalType": "bytes[]",
31
+ "name": "returnData",
32
+ "type": "bytes[]"
33
+ }
34
+ ],
35
+ "stateMutability": "view",
36
+ "type": "function"
37
+ },
38
+ {
39
+ "inputs": [
40
+ {
41
+ "components": [
42
+ {
43
+ "internalType": "address",
44
+ "name": "target",
45
+ "type": "address"
46
+ },
47
+ {
48
+ "internalType": "bytes",
49
+ "name": "callData",
50
+ "type": "bytes"
51
+ }
52
+ ],
53
+ "internalType": "struct Multicall2.Call[]",
54
+ "name": "calls",
55
+ "type": "tuple[]"
56
+ }
57
+ ],
58
+ "name": "blockAndAggregate",
59
+ "outputs": [
60
+ {
61
+ "internalType": "uint256",
62
+ "name": "blockNumber",
63
+ "type": "uint256"
64
+ },
65
+ {
66
+ "internalType": "bytes32",
67
+ "name": "blockHash",
68
+ "type": "bytes32"
69
+ },
70
+ {
71
+ "components": [
72
+ {
73
+ "internalType": "bool",
74
+ "name": "success",
75
+ "type": "bool"
76
+ },
77
+ {
78
+ "internalType": "bytes",
79
+ "name": "returnData",
80
+ "type": "bytes"
81
+ }
82
+ ],
83
+ "internalType": "struct Multicall2.Result[]",
84
+ "name": "returnData",
85
+ "type": "tuple[]"
86
+ }
87
+ ],
88
+ "stateMutability": "nonpayable",
89
+ "type": "function"
90
+ },
91
+ {
92
+ "inputs": [
93
+ {
94
+ "internalType": "uint256",
95
+ "name": "blockNumber",
96
+ "type": "uint256"
97
+ }
98
+ ],
99
+ "name": "getBlockHash",
100
+ "outputs": [
101
+ {
102
+ "internalType": "bytes32",
103
+ "name": "blockHash",
104
+ "type": "bytes32"
105
+ }
106
+ ],
107
+ "stateMutability": "view",
108
+ "type": "function"
109
+ },
110
+ {
111
+ "inputs": [],
112
+ "name": "getBlockNumber",
113
+ "outputs": [
114
+ {
115
+ "internalType": "uint256",
116
+ "name": "blockNumber",
117
+ "type": "uint256"
118
+ }
119
+ ],
120
+ "stateMutability": "view",
121
+ "type": "function"
122
+ },
123
+ {
124
+ "inputs": [],
125
+ "name": "getCurrentBlockCoinbase",
126
+ "outputs": [
127
+ {
128
+ "internalType": "address",
129
+ "name": "coinbase",
130
+ "type": "address"
131
+ }
132
+ ],
133
+ "stateMutability": "view",
134
+ "type": "function"
135
+ },
136
+ {
137
+ "inputs": [],
138
+ "name": "getCurrentBlockDifficulty",
139
+ "outputs": [
140
+ {
141
+ "internalType": "uint256",
142
+ "name": "difficulty",
143
+ "type": "uint256"
144
+ }
145
+ ],
146
+ "stateMutability": "view",
147
+ "type": "function"
148
+ },
149
+ {
150
+ "inputs": [],
151
+ "name": "getCurrentBlockGasLimit",
152
+ "outputs": [
153
+ {
154
+ "internalType": "uint256",
155
+ "name": "gaslimit",
156
+ "type": "uint256"
157
+ }
158
+ ],
159
+ "stateMutability": "view",
160
+ "type": "function"
161
+ },
162
+ {
163
+ "inputs": [],
164
+ "name": "getCurrentBlockTimestamp",
165
+ "outputs": [
166
+ {
167
+ "internalType": "uint256",
168
+ "name": "timestamp",
169
+ "type": "uint256"
170
+ }
171
+ ],
172
+ "stateMutability": "view",
173
+ "type": "function"
174
+ },
175
+ {
176
+ "inputs": [
177
+ {
178
+ "internalType": "address",
179
+ "name": "addr",
180
+ "type": "address"
181
+ }
182
+ ],
183
+ "name": "getEthBalance",
184
+ "outputs": [
185
+ {
186
+ "internalType": "uint256",
187
+ "name": "balance",
188
+ "type": "uint256"
189
+ }
190
+ ],
191
+ "stateMutability": "view",
192
+ "type": "function"
193
+ },
194
+ {
195
+ "inputs": [],
196
+ "name": "getLastBlockHash",
197
+ "outputs": [
198
+ {
199
+ "internalType": "bytes32",
200
+ "name": "blockHash",
201
+ "type": "bytes32"
202
+ }
203
+ ],
204
+ "stateMutability": "view",
205
+ "type": "function"
206
+ },
207
+ {
208
+ "inputs": [
209
+ {
210
+ "internalType": "bool",
211
+ "name": "requireSuccess",
212
+ "type": "bool"
213
+ },
214
+ {
215
+ "components": [
216
+ {
217
+ "internalType": "address",
218
+ "name": "target",
219
+ "type": "address"
220
+ },
221
+ {
222
+ "internalType": "bytes",
223
+ "name": "callData",
224
+ "type": "bytes"
225
+ }
226
+ ],
227
+ "internalType": "struct Multicall2.Call[]",
228
+ "name": "calls",
229
+ "type": "tuple[]"
230
+ }
231
+ ],
232
+ "name": "tryAggregate",
233
+ "outputs": [
234
+ {
235
+ "components": [
236
+ {
237
+ "internalType": "bool",
238
+ "name": "success",
239
+ "type": "bool"
240
+ },
241
+ {
242
+ "internalType": "bytes",
243
+ "name": "returnData",
244
+ "type": "bytes"
245
+ }
246
+ ],
247
+ "internalType": "struct Multicall2.Result[]",
248
+ "name": "returnData",
249
+ "type": "tuple[]"
250
+ }
251
+ ],
252
+ "stateMutability": "view",
253
+ "type": "function"
254
+ },
255
+ {
256
+ "inputs": [
257
+ {
258
+ "internalType": "bool",
259
+ "name": "requireSuccess",
260
+ "type": "bool"
261
+ },
262
+ {
263
+ "components": [
264
+ {
265
+ "internalType": "address",
266
+ "name": "target",
267
+ "type": "address"
268
+ },
269
+ {
270
+ "internalType": "bytes",
271
+ "name": "callData",
272
+ "type": "bytes"
273
+ }
274
+ ],
275
+ "internalType": "struct Multicall2.Call[]",
276
+ "name": "calls",
277
+ "type": "tuple[]"
278
+ }
279
+ ],
280
+ "name": "tryBlockAndAggregate",
281
+ "outputs": [
282
+ {
283
+ "internalType": "uint256",
284
+ "name": "blockNumber",
285
+ "type": "uint256"
286
+ },
287
+ {
288
+ "internalType": "bytes32",
289
+ "name": "blockHash",
290
+ "type": "bytes32"
291
+ },
292
+ {
293
+ "components": [
294
+ {
295
+ "internalType": "bool",
296
+ "name": "success",
297
+ "type": "bool"
298
+ },
299
+ {
300
+ "internalType": "bytes",
301
+ "name": "returnData",
302
+ "type": "bytes"
303
+ }
304
+ ],
305
+ "internalType": "struct Multicall2.Result[]",
306
+ "name": "returnData",
307
+ "type": "tuple[]"
308
+ }
309
+ ],
310
+ "stateMutability": "view",
311
+ "type": "function"
312
+ }
313
+ ]
@@ -0,0 +1,38 @@
1
+ import { Process, Step } from '@lifi/types';
2
+ import { LifiError } from './errors';
3
+ /**
4
+ * Available MetaMask error codes:
5
+ *
6
+ * export const errorCodes: ErrorCodes = {
7
+ rpc: {
8
+ invalidInput: -32000,
9
+ resourceNotFound: -32001,
10
+ resourceUnavailable: -32002,
11
+ transactionRejected: -32003,
12
+ methodNotSupported: -32004,
13
+ limitExceeded: -32005,
14
+ parse: -32700,
15
+ invalidRequest: -32600,
16
+ methodNotFound: -32601,
17
+ invalidParams: -32602,
18
+ internal: -32603,
19
+ },
20
+ provider: {
21
+ userRejectedRequest: 4001,
22
+ unauthorized: 4100,
23
+ unsupportedMethod: 4200,
24
+ disconnected: 4900,
25
+ chainDisconnected: 4901,
26
+ },
27
+ };
28
+ *
29
+ * For more information about error codes supported by metamask check
30
+ * https://github.com/MetaMask/eth-rpc-errors
31
+ * https://eips.ethereum.org/EIPS/eip-1474#error-codes
32
+ * https://eips.ethereum.org/EIPS/eip-1193#provider-errors
33
+ */
34
+ export declare const getTransactionNotSentMessage: (step?: Step, process?: Process) => Promise<string>;
35
+ export declare const getTransactionFailedMessage: (step: Step, txLink?: string) => string;
36
+ export declare const parseError: (e: any, step?: Step, process?: Process) => Promise<LifiError>;
37
+ export declare const parseBackendError: (e: any) => LifiError;
38
+ export declare const getSlippageNotMetMessage: (step: Step) => string;
@@ -0,0 +1,141 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getSlippageNotMetMessage = exports.parseBackendError = exports.parseError = exports.getTransactionFailedMessage = exports.getTransactionNotSentMessage = void 0;
16
+ const types_1 = require("@lifi/types");
17
+ const eth_rpc_errors_1 = require("eth-rpc-errors");
18
+ const ChainsService_1 = __importDefault(require("../services/ChainsService"));
19
+ const errors_1 = require("./errors");
20
+ const utils_1 = require("./utils");
21
+ /**
22
+ * Available MetaMask error codes:
23
+ *
24
+ * export const errorCodes: ErrorCodes = {
25
+ rpc: {
26
+ invalidInput: -32000,
27
+ resourceNotFound: -32001,
28
+ resourceUnavailable: -32002,
29
+ transactionRejected: -32003,
30
+ methodNotSupported: -32004,
31
+ limitExceeded: -32005,
32
+ parse: -32700,
33
+ invalidRequest: -32600,
34
+ methodNotFound: -32601,
35
+ invalidParams: -32602,
36
+ internal: -32603,
37
+ },
38
+ provider: {
39
+ userRejectedRequest: 4001,
40
+ unauthorized: 4100,
41
+ unsupportedMethod: 4200,
42
+ disconnected: 4900,
43
+ chainDisconnected: 4901,
44
+ },
45
+ };
46
+ *
47
+ * For more information about error codes supported by metamask check
48
+ * https://github.com/MetaMask/eth-rpc-errors
49
+ * https://eips.ethereum.org/EIPS/eip-1474#error-codes
50
+ * https://eips.ethereum.org/EIPS/eip-1193#provider-errors
51
+ */
52
+ const getTransactionNotSentMessage = (step, process) => __awaiter(void 0, void 0, void 0, function* () {
53
+ let transactionNotSend = 'Transaction was not sent, your funds are still in your wallet';
54
+ // add information about funds if available
55
+ if (step) {
56
+ const chainService = ChainsService_1.default.getInstance();
57
+ const chain = yield chainService.getChainById(step.action.fromChainId);
58
+ transactionNotSend += ` (${(0, utils_1.formatTokenAmountOnly)(step.action.fromToken, step.action.fromAmount)} ${step.action.fromToken.symbol} on ${chain.name})`;
59
+ }
60
+ transactionNotSend +=
61
+ ", please retry.<br/>If it still doesn't work, it is safe to delete this transfer and start a new one.";
62
+ // add transaction explorer link if available
63
+ transactionNotSend +=
64
+ process && process.txLink
65
+ ? `<br>You can check the failed transaction&nbsp;<a href="${process.txLink}" target="_blank" rel="nofollow noreferrer">here</a>.`
66
+ : '';
67
+ return transactionNotSend;
68
+ });
69
+ exports.getTransactionNotSentMessage = getTransactionNotSentMessage;
70
+ const getTransactionFailedMessage = (step, txLink) => {
71
+ const baseString = `It appears that your transaction may not have been successful.
72
+ However, to confirm this, please check your ${(0, types_1.getChainById)(step.action.toChainId).name} wallet for ${step.action.toToken.symbol}.`;
73
+ return txLink
74
+ ? `${baseString}
75
+ You can also check the&nbsp;<a href="${txLink}" target="_blank" rel="nofollow noreferrer">block explorer</a> for more information.`
76
+ : baseString;
77
+ };
78
+ exports.getTransactionFailedMessage = getTransactionFailedMessage;
79
+ const parseError = (e, step, process) => __awaiter(void 0, void 0, void 0, function* () {
80
+ var _a, _b;
81
+ if (e instanceof errors_1.LifiError) {
82
+ return e;
83
+ }
84
+ if (e.code) {
85
+ // MetaMask errors have a numeric error code
86
+ if (typeof e.code === 'number') {
87
+ if (Object.values(eth_rpc_errors_1.errorCodes.rpc).includes(e.code)) {
88
+ // rpc errors
89
+ // underpriced errors are sent as internal errors, so we need to parse the message manually
90
+ if (e.code === eth_rpc_errors_1.errorCodes.rpc.internal &&
91
+ e.message &&
92
+ e.message.includes('underpriced')) {
93
+ return new errors_1.RPCError(errors_1.LifiErrorCode.TransactionUnderpriced, 'Transaction is underpriced.', yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
94
+ }
95
+ if (((_a = e.message) === null || _a === void 0 ? void 0 : _a.includes('intrinsic gas too low')) ||
96
+ ((_b = e.message) === null || _b === void 0 ? void 0 : _b.includes('out of gas'))) {
97
+ return new errors_1.TransactionError(errors_1.LifiErrorCode.GasLimitError, 'Gas limit is too low.', yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
98
+ }
99
+ return new errors_1.RPCError(e.code, (0, eth_rpc_errors_1.getMessageFromCode)(e.code), yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
100
+ }
101
+ // provider errors
102
+ if (Object.values(eth_rpc_errors_1.errorCodes.provider).includes(e.code)) {
103
+ return new errors_1.ProviderError(e.code, (0, eth_rpc_errors_1.getMessageFromCode)(e.code), yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
104
+ }
105
+ }
106
+ if (e.code === 'CALL_EXCEPTION') {
107
+ return new errors_1.ProviderError(errors_1.LifiErrorCode.TransactionFailed, e.reason, yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
108
+ }
109
+ if (e.code === errors_1.LifiErrorCode.TransactionUnprepared) {
110
+ return new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, e.message, yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
111
+ }
112
+ if (e.code === errors_1.LifiErrorCode.ValidationError) {
113
+ return new errors_1.TransactionError(errors_1.LifiErrorCode.ValidationError, e.message, e.htmlMessage);
114
+ }
115
+ }
116
+ return new errors_1.UnknownError(errors_1.LifiErrorCode.InternalError, e.message || 'Unknown error occurred.', undefined, e.stack);
117
+ });
118
+ exports.parseError = parseError;
119
+ const parseBackendError = (e) => {
120
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
121
+ if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 400) {
122
+ return new errors_1.ValidationError(((_c = (_b = e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || ((_d = e.response) === null || _d === void 0 ? void 0 : _d.statusText), undefined, e.stack);
123
+ }
124
+ if (((_e = e.response) === null || _e === void 0 ? void 0 : _e.status) === 404) {
125
+ return new errors_1.NotFoundError(((_g = (_f = e.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.message) || ((_h = e.response) === null || _h === void 0 ? void 0 : _h.statusText), undefined, e.stack);
126
+ }
127
+ if (((_j = e.response) === null || _j === void 0 ? void 0 : _j.status) === 409) {
128
+ return new errors_1.SlippageError(((_l = (_k = e.response) === null || _k === void 0 ? void 0 : _k.data) === null || _l === void 0 ? void 0 : _l.message) || ((_m = e.response) === null || _m === void 0 ? void 0 : _m.statusText), 'The slippage is larger than the defined threshold. Please request a new route to get a fresh quote.', e.stack);
129
+ }
130
+ if (((_o = e.response) === null || _o === void 0 ? void 0 : _o.status) === 500) {
131
+ return new errors_1.ServerError(((_q = (_p = e.response) === null || _p === void 0 ? void 0 : _p.data) === null || _q === void 0 ? void 0 : _q.message) || ((_r = e.response) === null || _r === void 0 ? void 0 : _r.statusText), undefined, e.stack);
132
+ }
133
+ return new errors_1.ServerError('Something went wrong.', undefined, e.stack);
134
+ };
135
+ exports.parseBackendError = parseBackendError;
136
+ const getSlippageNotMetMessage = (step) => {
137
+ const { slippage } = step.action;
138
+ return `Transaction was not sent, your funds are still in your wallet.
139
+ The updated quote for the current transaction does not meet your set slippage of ${slippage * 100}%.`;
140
+ };
141
+ exports.getSlippageNotMetMessage = getSlippageNotMetMessage;
@@ -0,0 +1,2 @@
1
+ import { Route } from '../types';
2
+ export declare const handlePreRestart: (route: Route) => void;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handlePreRestart = void 0;
4
+ const errors_1 = require("./errors");
5
+ const handlePreRestart = (route) => {
6
+ var _a;
7
+ for (let index = 0; index < route.steps.length; index++) {
8
+ const stepHasFailed = ((_a = route.steps[index].execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED';
9
+ if (stepHasFailed) {
10
+ handleErrorType(route, index);
11
+ deleteFailedProcesses(route, index);
12
+ }
13
+ }
14
+ };
15
+ exports.handlePreRestart = handlePreRestart;
16
+ const handleErrorType = (route, index) => {
17
+ var _a, _b, _c, _d;
18
+ const isGasLimitError = (_a = route.steps[index].execution) === null || _a === void 0 ? void 0 : _a.process.some((p) => { var _a; return ((_a = p.error) === null || _a === void 0 ? void 0 : _a.code) === errors_1.LifiErrorCode.GasLimitError; });
19
+ const isGasPriceError = (_b = route.steps[index].execution) === null || _b === void 0 ? void 0 : _b.process.some((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((gasCost) => (gasCost.limit = `${Math.round(Number(gasCost.limit) * 1.25)}`));
22
+ }
23
+ if (isGasPriceError) {
24
+ (_d = route.steps[index].estimate.gasCosts) === null || _d === void 0 ? void 0 : _d.forEach((gasCost) => (gasCost.price = `${Math.round(Number(gasCost.price) * 1.25)}`));
25
+ }
26
+ };
27
+ const deleteFailedProcesses = (route, index) => {
28
+ if (route.steps[index].execution) {
29
+ route.steps[index].execution.process = route.steps[index].execution.process.filter((process) => process.status !== 'FAILED');
30
+ }
31
+ };
@@ -0,0 +1,26 @@
1
+ import { TransactionReceipt } from '@ethersproject/providers';
2
+ import { Token } from '@lifi/types';
3
+ import BigNumber from 'bignumber.js';
4
+ import { Signer } from 'ethers';
5
+ import { ChainId, Step } from '../types';
6
+ export declare const deepClone: <T>(src: T) => T;
7
+ export declare const sleep: (mills: number) => Promise<undefined>;
8
+ export declare const personalizeStep: (signer: Signer, step: Step) => Promise<Step>;
9
+ export declare const splitListIntoChunks: <T>(list: T[], chunkSize: number) => T[][];
10
+ export declare const formatTokenAmountOnly: (token: Token, amount: string | BigNumber | undefined) => string;
11
+ /**
12
+ * Repeatedly calls a given asynchronous function until it resolves with a value
13
+ * @param toRepeat The function that should be repeated
14
+ * @param timeout The timeout in milliseconds between retries, defaults to 5000
15
+ * @returns The result of the toRepeat function
16
+ */
17
+ export declare const repeatUntilDone: <T>(toRepeat: () => Promise<T | undefined>, timeout?: number) => Promise<T>;
18
+ /**
19
+ * Loads a transaction receipt using the rpc for the given chain id
20
+ * @param chainId The chain id where the transaction should be loaded from
21
+ * @param txHash The hash of the transaction
22
+ * @returns TransactionReceipt
23
+ */
24
+ export declare const loadTransactionReceipt: (chainId: ChainId, txHash: string) => Promise<TransactionReceipt>;
25
+ export declare const isZeroAddress: (address: string) => boolean;
26
+ export declare const isNativeTokenAddress: (address: string) => boolean;
@@ -0,0 +1,120 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.isNativeTokenAddress = exports.isZeroAddress = exports.loadTransactionReceipt = exports.repeatUntilDone = exports.formatTokenAmountOnly = exports.splitListIntoChunks = exports.personalizeStep = exports.sleep = exports.deepClone = void 0;
16
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
17
+ const ethers_1 = require("ethers");
18
+ const connectors_1 = require("../connectors");
19
+ const deepClone = (src) => {
20
+ return JSON.parse(JSON.stringify(src));
21
+ };
22
+ exports.deepClone = deepClone;
23
+ const sleep = (mills) => {
24
+ return new Promise((resolve) => {
25
+ setTimeout(resolve, mills);
26
+ });
27
+ };
28
+ exports.sleep = sleep;
29
+ const personalizeStep = (signer, step) => __awaiter(void 0, void 0, void 0, function* () {
30
+ if (step.action.toAddress && step.action.fromAddress) {
31
+ return step;
32
+ }
33
+ const address = yield signer.getAddress();
34
+ const fromAddress = step.action.fromAddress || address;
35
+ const toAddress = step.action.toAddress || address;
36
+ return Object.assign(Object.assign({}, step), { action: Object.assign(Object.assign({}, step.action), { fromAddress,
37
+ toAddress }) });
38
+ });
39
+ exports.personalizeStep = personalizeStep;
40
+ const splitListIntoChunks = (list, chunkSize) => list.reduce((resultList, item, index) => {
41
+ const chunkIndex = Math.floor(index / chunkSize);
42
+ if (!resultList[chunkIndex]) {
43
+ resultList[chunkIndex] = []; // start a new chunk
44
+ }
45
+ resultList[chunkIndex].push(item);
46
+ return resultList;
47
+ }, []);
48
+ exports.splitListIntoChunks = splitListIntoChunks;
49
+ const formatTokenAmountOnly = (token, amount) => {
50
+ if (!amount) {
51
+ return '0.0';
52
+ }
53
+ let floated;
54
+ if (typeof amount === 'string') {
55
+ if (amount === '0') {
56
+ return '0.0';
57
+ }
58
+ floated = new bignumber_js_1.default(amount).shiftedBy(-token.decimals);
59
+ }
60
+ else {
61
+ floated = amount;
62
+ if (floated.isZero()) {
63
+ return '0.0';
64
+ }
65
+ }
66
+ // show at least 4 decimal places and at least two non-zero digests
67
+ let decimalPlaces = 3;
68
+ while (floated.lt(1 / Math.pow(10, decimalPlaces))) {
69
+ decimalPlaces++;
70
+ }
71
+ return floated.toFixed(decimalPlaces + 1, 1);
72
+ };
73
+ exports.formatTokenAmountOnly = formatTokenAmountOnly;
74
+ /**
75
+ * Repeatedly calls a given asynchronous function until it resolves with a value
76
+ * @param toRepeat The function that should be repeated
77
+ * @param timeout The timeout in milliseconds between retries, defaults to 5000
78
+ * @returns The result of the toRepeat function
79
+ */
80
+ const repeatUntilDone = (toRepeat, timeout = 5000) => __awaiter(void 0, void 0, void 0, function* () {
81
+ let result;
82
+ while (!result) {
83
+ result = yield toRepeat();
84
+ if (!result) {
85
+ yield (0, exports.sleep)(timeout);
86
+ }
87
+ }
88
+ return result;
89
+ });
90
+ exports.repeatUntilDone = repeatUntilDone;
91
+ /**
92
+ * Loads a transaction receipt using the rpc for the given chain id
93
+ * @param chainId The chain id where the transaction should be loaded from
94
+ * @param txHash The hash of the transaction
95
+ * @returns TransactionReceipt
96
+ */
97
+ const loadTransactionReceipt = (chainId, txHash) => __awaiter(void 0, void 0, void 0, function* () {
98
+ const rpc = yield (0, connectors_1.getRpcProvider)(chainId);
99
+ const tx = yield rpc.getTransaction(txHash);
100
+ return tx.wait();
101
+ });
102
+ exports.loadTransactionReceipt = loadTransactionReceipt;
103
+ const isZeroAddress = (address) => {
104
+ if (address === ethers_1.constants.AddressZero ||
105
+ address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
106
+ return true;
107
+ }
108
+ return false;
109
+ };
110
+ exports.isZeroAddress = isZeroAddress;
111
+ const isNativeTokenAddress = (address) => {
112
+ if (address === ethers_1.constants.AddressZero ||
113
+ address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' ||
114
+ // CELO native token
115
+ address === '0x471ece3750da237f93b8e339c536989b8978a438') {
116
+ return true;
117
+ }
118
+ return false;
119
+ };
120
+ exports.isNativeTokenAddress = isNativeTokenAddress;