@lifi/sdk 2.0.0-beta.8 → 2.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.
Files changed (61) hide show
  1. package/dist/LiFi.d.ts +9 -9
  2. package/dist/LiFi.js +11 -11
  3. package/dist/allowance/checkAllowance.js +18 -2
  4. package/dist/allowance/utils.d.ts +2 -1
  5. package/dist/allowance/utils.js +7 -2
  6. package/dist/cjs/LiFi.d.ts +9 -9
  7. package/dist/cjs/LiFi.js +12 -13
  8. package/dist/cjs/allowance/checkAllowance.js +18 -2
  9. package/dist/cjs/allowance/utils.d.ts +2 -1
  10. package/dist/cjs/allowance/utils.js +7 -2
  11. package/dist/cjs/connectors.d.ts +1 -1
  12. package/dist/cjs/connectors.js +7 -9
  13. package/dist/cjs/execution/RouteExecutionManager.d.ts +1 -1
  14. package/dist/cjs/execution/RouteExecutionManager.js +1 -1
  15. package/dist/cjs/execution/StatusManager.js +1 -1
  16. package/dist/cjs/execution/StepExecutionManager.js +22 -0
  17. package/dist/cjs/execution/StepExecutor.js +13 -5
  18. package/dist/cjs/execution/stepComparison.d.ts +1 -1
  19. package/dist/cjs/execution/stepComparison.js +2 -5
  20. package/dist/cjs/helpers.d.ts +2 -0
  21. package/dist/cjs/helpers.js +6 -1
  22. package/dist/cjs/services/ApiService.d.ts +1 -1
  23. package/dist/cjs/services/ConfigService.d.ts +1 -1
  24. package/dist/cjs/services/ConfigService.js +1 -2
  25. package/dist/cjs/typeguards.d.ts +2 -2
  26. package/dist/cjs/types/internal.types.d.ts +47 -8
  27. package/dist/cjs/utils/errors.d.ts +22 -1
  28. package/dist/cjs/utils/errors.js +28 -4
  29. package/dist/cjs/utils/parseError.js +31 -12
  30. package/dist/cjs/utils/preRestart.d.ts +2 -1
  31. package/dist/cjs/utils/preRestart.js +24 -3
  32. package/dist/cjs/version.d.ts +1 -1
  33. package/dist/cjs/version.js +1 -1
  34. package/dist/connectors.d.ts +1 -1
  35. package/dist/connectors.js +7 -9
  36. package/dist/execution/RouteExecutionManager.d.ts +1 -1
  37. package/dist/execution/RouteExecutionManager.js +1 -1
  38. package/dist/execution/StatusManager.js +1 -1
  39. package/dist/execution/StepExecutionManager.js +22 -0
  40. package/dist/execution/StepExecutor.js +13 -5
  41. package/dist/execution/stepComparison.d.ts +1 -1
  42. package/dist/execution/stepComparison.js +2 -5
  43. package/dist/helpers.d.ts +2 -0
  44. package/dist/helpers.js +4 -0
  45. package/dist/services/ApiService.d.ts +1 -1
  46. package/dist/services/ConfigService.d.ts +1 -1
  47. package/dist/services/ConfigService.js +1 -2
  48. package/dist/typeguards.d.ts +2 -2
  49. package/dist/types/internal.types.d.ts +47 -8
  50. package/dist/utils/errors.d.ts +22 -1
  51. package/dist/utils/errors.js +24 -0
  52. package/dist/utils/parseError.js +32 -13
  53. package/dist/utils/preRestart.d.ts +2 -1
  54. package/dist/utils/preRestart.js +24 -3
  55. package/dist/version.d.ts +1 -1
  56. package/dist/version.js +1 -1
  57. package/package.json +15 -15
  58. package/dist/cjs/services/ApiService.unit.handlers.d.ts +0 -1
  59. package/dist/cjs/services/ApiService.unit.handlers.js +0 -50
  60. package/dist/services/ApiService.unit.handlers.d.ts +0 -1
  61. package/dist/services/ApiService.unit.handlers.js +0 -44
@@ -1,6 +1,7 @@
1
1
  import { errorCodes as MetaMaskErrorCodes, getMessageFromCode, } from 'eth-rpc-errors';
2
+ import { fetchTxErrorDetails } from '../helpers';
2
3
  import ChainsService from '../services/ChainsService';
3
- import { LifiError, LifiErrorCode, MetaMaskProviderErrorCode, NotFoundError, ProviderError, RPCError, ServerError, SlippageError, TransactionError, UnknownError, ValidationError, } from './errors';
4
+ import { ErrorMessage, EthersErrorMessage, EthersErrorType, LifiError, LifiErrorCode, MetaMaskProviderErrorCode, NotFoundError, ProviderError, RPCError, ServerError, SlippageError, TransactionError, UnknownError, ValidationError, } from './errors';
4
5
  import { formatTokenAmountOnly } from './utils';
5
6
  /**
6
7
  * Available MetaMask error codes:
@@ -71,13 +72,13 @@ export const parseError = async (e, step, process) => {
71
72
  // rpc errors
72
73
  // underpriced errors are sent as internal errors, so we need to parse the message manually
73
74
  if (e.code === MetaMaskErrorCodes.rpc.internal &&
74
- (e.message?.includes('underpriced') ||
75
- e.message?.includes('replacement fee too low'))) {
76
- return new RPCError(LifiErrorCode.TransactionUnderpriced, 'Transaction is underpriced.', await getTransactionNotSentMessage(step, process), e.stack);
75
+ (e.message?.includes(EthersErrorMessage.Underpriced) ||
76
+ e.message?.includes(EthersErrorMessage.LowReplacementFee))) {
77
+ return new RPCError(LifiErrorCode.TransactionUnderpriced, ErrorMessage.TransactionUnderpriced, await getTransactionNotSentMessage(step, process), e.stack);
77
78
  }
78
- if (e.message?.includes('intrinsic gas too low') ||
79
- e.message?.includes('out of gas')) {
80
- return new TransactionError(LifiErrorCode.GasLimitError, 'Gas limit is too low.', await getTransactionNotSentMessage(step, process), e.stack);
79
+ if (e.message?.includes(EthersErrorMessage.LowGas) ||
80
+ e.message?.includes(EthersErrorMessage.OutOfGas)) {
81
+ return new TransactionError(LifiErrorCode.GasLimitError, ErrorMessage.GasLimitLow, await getTransactionNotSentMessage(step, process), e.stack);
81
82
  }
82
83
  return new RPCError(e.code, getMessageFromCode(e.code), await getTransactionNotSentMessage(step, process), e.stack);
83
84
  }
@@ -88,9 +89,27 @@ export const parseError = async (e, step, process) => {
88
89
  }
89
90
  }
90
91
  switch (e.code) {
91
- case 'CALL_EXCEPTION':
92
- return new ProviderError(LifiErrorCode.TransactionFailed, e.reason, await getTransactionNotSentMessage(step, process), e.stack);
93
- case 'ACTION_REJECTED':
92
+ case EthersErrorType.CallExecption:
93
+ const defaultErrorMessage = await getTransactionNotSentMessage(step, process);
94
+ try {
95
+ if (!step?.action.fromChainId) {
96
+ throw new Error('Signer is not defined.');
97
+ }
98
+ const response = await fetchTxErrorDetails(e.transactionHash, step?.action.fromChainId);
99
+ const errorMessage = response?.error_message ?? e.reason;
100
+ const isAllowanceError = response?.error_message?.includes(EthersErrorMessage.ERC20Allowance) || e.reason?.includes(EthersErrorMessage.ERC20Allowance);
101
+ if (isAllowanceError) {
102
+ return new TransactionError(LifiErrorCode.AllowanceRequired, e.reason, errorMessage, e.stack);
103
+ }
104
+ // Error messages other than allowance error will be handled in catch block
105
+ throw new Error(e);
106
+ }
107
+ catch (error) {
108
+ return new ProviderError(LifiErrorCode.TransactionFailed, e.reason, defaultErrorMessage, e.stack);
109
+ }
110
+ case EthersErrorType.InsufficientFunds:
111
+ return new TransactionError(LifiErrorCode.InsufficientFunds, e.message, await getTransactionNotSentMessage(step, process), e.stack);
112
+ case EthersErrorType.ActionRejected:
94
113
  case MetaMaskProviderErrorCode.userRejectedRequest:
95
114
  return new TransactionError(LifiErrorCode.TransactionRejected, e.message, await getTransactionNotSentMessage(step, process), e.stack);
96
115
  case LifiErrorCode.TransactionUnprepared:
@@ -98,7 +117,7 @@ export const parseError = async (e, step, process) => {
98
117
  case LifiErrorCode.ValidationError:
99
118
  return new TransactionError(LifiErrorCode.ValidationError, e.message, e.htmlMessage);
100
119
  default:
101
- return new UnknownError(LifiErrorCode.InternalError, e.message || 'Unknown error occurred.', undefined, e.stack);
120
+ return new UnknownError(LifiErrorCode.InternalError, e.message || ErrorMessage.UnknownError, undefined, e.stack);
102
121
  }
103
122
  };
104
123
  export const parseBackendError = async (e) => {
@@ -116,10 +135,10 @@ export const parseBackendError = async (e) => {
116
135
  return new NotFoundError(data?.message || e.response?.statusText, undefined, e.stack);
117
136
  }
118
137
  if (e.response?.status === 409) {
119
- return new SlippageError(data?.message || e.response?.statusText, 'The slippage is larger than the defined threshold. Please request a new route to get a fresh quote.', e.stack);
138
+ return new SlippageError(data?.message || e.response?.statusText, ErrorMessage.SlippageError, e.stack);
120
139
  }
121
140
  if (e.response?.status === 500) {
122
141
  return new ServerError(data?.message || e.response?.statusText, undefined, e.stack);
123
142
  }
124
- return new ServerError('Something went wrong.', undefined, e.stack);
143
+ return new ServerError(ErrorMessage.Default, undefined, e.stack);
125
144
  };
@@ -1,2 +1,3 @@
1
+ import { Signer } from 'ethers';
1
2
  import { Route } from '../types';
2
- export declare const handlePreRestart: (route: Route) => void;
3
+ export declare const handlePreRestart: (route: Route, signer: Signer) => Promise<void>;
@@ -1,21 +1,42 @@
1
1
  import { LifiErrorCode } from './errors';
2
- export const handlePreRestart = (route) => {
2
+ export const handlePreRestart = async (route, signer) => {
3
3
  for (let index = 0; index < route.steps.length; index++) {
4
4
  const stepHasFailed = route.steps[index].execution?.status === 'FAILED';
5
5
  if (stepHasFailed) {
6
- handleErrorType(route, index);
6
+ await handleErrorType(route, index, signer);
7
7
  deleteFailedProcesses(route, index);
8
8
  deleteTransactionData(route, index);
9
9
  }
10
10
  }
11
11
  };
12
- const handleErrorType = (route, index) => {
12
+ const handleErrorType = async (route, index, signer) => {
13
13
  const isGasLimitError = route.steps[index].execution?.process.some((p) => p.error?.code === LifiErrorCode.GasLimitError);
14
14
  const isGasPriceError = route.steps[index].execution?.process.some((p) => p.error?.code === LifiErrorCode.TransactionUnderpriced);
15
+ const { transactionRequest } = route.steps[index];
15
16
  if (isGasLimitError) {
17
+ if (transactionRequest) {
18
+ let gasLimit = transactionRequest.gasLimit;
19
+ try {
20
+ gasLimit = await signer.estimateGas(transactionRequest);
21
+ }
22
+ catch (error) { }
23
+ if (gasLimit) {
24
+ transactionRequest.gasLimit = `${(BigInt(gasLimit.toString()) * 125n) / 100n}`;
25
+ }
26
+ }
16
27
  route.steps[index].estimate.gasCosts?.forEach((gasCost) => (gasCost.limit = `${Math.round(Number(gasCost.limit) * 1.25)}`));
17
28
  }
18
29
  if (isGasPriceError) {
30
+ if (transactionRequest) {
31
+ let gasPrice = transactionRequest.gasPrice;
32
+ try {
33
+ gasPrice = await signer.getGasPrice();
34
+ }
35
+ catch (error) { }
36
+ if (gasPrice) {
37
+ transactionRequest.gasPrice = `${(BigInt(gasPrice.toString()) * 125n) / 100n}`;
38
+ }
39
+ }
19
40
  route.steps[index].estimate.gasCosts?.forEach((gasCost) => (gasCost.price = `${Math.round(Number(gasCost.price) * 1.25)}`));
20
41
  }
21
42
  };
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare const name = "@lifi/sdk";
2
- export declare const version = "2.0.0-beta.8";
2
+ export declare const version = "2.0.0";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk';
2
- export const version = '2.0.0-beta.8';
2
+ export const version = '2.0.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/sdk",
3
- "version": "2.0.0-beta.8",
3
+ "version": "2.0.0",
4
4
  "description": "LI.FI Any-to-Any Cross-Chain-Swap SDK",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/index.js",
@@ -76,34 +76,34 @@
76
76
  "dependencies": {
77
77
  "@ethersproject/abi": "^5.7.0",
78
78
  "@ethersproject/contracts": "^5.7.0",
79
- "@lifi/types": "^3.2.1",
79
+ "@lifi/types": "^6.0.0",
80
80
  "bignumber.js": "^9.1.1",
81
81
  "eth-rpc-errors": "^4.0.3",
82
82
  "ethers": "^5.7.2"
83
83
  },
84
84
  "devDependencies": {
85
- "@commitlint/cli": "^17.5.1",
86
- "@commitlint/config-conventional": "^17.4.4",
87
- "@typescript-eslint/eslint-plugin": "^5.58.0",
88
- "@typescript-eslint/parser": "^5.58.0",
89
- "@vitest/coverage-c8": "^0.30.0",
90
- "cross-fetch": "^3.1.5",
91
- "eslint": "^8.38.0",
85
+ "@commitlint/cli": "^17.6.5",
86
+ "@commitlint/config-conventional": "^17.6.5",
87
+ "@mswjs/interceptors": "^0.22.15",
88
+ "@typescript-eslint/eslint-plugin": "^5.59.9",
89
+ "@typescript-eslint/parser": "^5.59.9",
90
+ "@vitest/coverage-c8": "^0.32.0",
91
+ "cross-fetch": "^3.1.6",
92
+ "eslint": "^8.42.0",
92
93
  "eslint-config-prettier": "^8.8.0",
93
94
  "eslint-plugin-prettier": "^4.2.1",
94
95
  "husky": "^8.0.3",
95
- "lint-staged": "^13.2.1",
96
+ "lint-staged": "^13.2.2",
96
97
  "msw": "1.0.1",
97
98
  "npm-run-all": "^4.1.5",
98
99
  "pinst": "^3.0.0",
99
- "prettier": "^2.8.7",
100
+ "prettier": "^2.8.8",
100
101
  "standard-version": "^9.5.0",
101
- "ts-loader": "^9.4.2",
102
- "typescript": "^5.0.4",
103
- "vitest": "^0.30.0"
102
+ "typescript": "^5.1.3",
103
+ "vitest": "^0.32.0"
104
104
  },
105
105
  "directories": {
106
106
  "test": "test"
107
107
  },
108
- "packageManager": "yarn@3.5.0"
108
+ "packageManager": "yarn@3.5.1"
109
109
  }
@@ -1 +0,0 @@
1
- export declare const handlers: import("msw").RestHandler<import("msw/lib/glossary-de6278a9").M<import("msw/lib/glossary-de6278a9").h>>[];
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handlers = void 0;
7
- const types_1 = require("@lifi/types");
8
- const msw_1 = require("msw");
9
- const ConfigService_1 = __importDefault(require("./ConfigService"));
10
- const config = ConfigService_1.default.getInstance().getConfig();
11
- exports.handlers = [
12
- msw_1.rest.post(`${config.apiUrl}/advanced/routes`, async (request, response, context) => {
13
- const data = await request.json();
14
- if (isNaN(parseFloat(data.fromAmount))) {
15
- return response(context.status(500), context.json({ message: `Oops` }));
16
- }
17
- return response(context.json({}));
18
- }),
19
- msw_1.rest.post(`${config.apiUrl}/advanced/possibilities`, async (request, response, context) => {
20
- return response(context.json({}));
21
- }),
22
- msw_1.rest.get(`${config.apiUrl}/token`, async (request, response, context) => {
23
- return response(context.json({}));
24
- }),
25
- msw_1.rest.get(`${config.apiUrl}/quote`, async (request, response, context) => {
26
- return response(context.json({}));
27
- }),
28
- msw_1.rest.get(`${config.apiUrl}/status`, async (request, response, context) => {
29
- return response(context.json({}));
30
- }),
31
- msw_1.rest.get(`${config.apiUrl}/chains`, async (request, response, context) => {
32
- return response(context.json({ chains: [{ id: 1 }] }));
33
- }),
34
- msw_1.rest.get(`${config.apiUrl}/tools`, async (request, response, context) => {
35
- return response(context.json({ bridges: [], exchanges: [] }));
36
- }),
37
- msw_1.rest.get(`${config.apiUrl}/tokens`, async (request, response, context) => {
38
- return response(context.json({
39
- tokens: {
40
- [types_1.ChainId.ETH]: [(0, types_1.findDefaultToken)(types_1.CoinKey.ETH, types_1.ChainId.ETH)],
41
- },
42
- }));
43
- }),
44
- msw_1.rest.post(`${config.apiUrl}/advanced/stepTransaction`, async (request, response, context) => {
45
- return response(context.json({}));
46
- }),
47
- msw_1.rest.get(`${config.apiUrl}/gas/suggestion/${types_1.ChainId.OPT}`, async (request, response, context) => {
48
- return response(context.json({}));
49
- }),
50
- ];
@@ -1 +0,0 @@
1
- export declare const handlers: import("msw").RestHandler<import("msw/lib/glossary-de6278a9").M<import("msw/lib/glossary-de6278a9").h>>[];
@@ -1,44 +0,0 @@
1
- import { ChainId, CoinKey, findDefaultToken } from '@lifi/types';
2
- import { rest } from 'msw';
3
- import ConfigService from './ConfigService';
4
- const config = ConfigService.getInstance().getConfig();
5
- export const handlers = [
6
- rest.post(`${config.apiUrl}/advanced/routes`, async (request, response, context) => {
7
- const data = await request.json();
8
- if (isNaN(parseFloat(data.fromAmount))) {
9
- return response(context.status(500), context.json({ message: `Oops` }));
10
- }
11
- return response(context.json({}));
12
- }),
13
- rest.post(`${config.apiUrl}/advanced/possibilities`, async (request, response, context) => {
14
- return response(context.json({}));
15
- }),
16
- rest.get(`${config.apiUrl}/token`, async (request, response, context) => {
17
- return response(context.json({}));
18
- }),
19
- rest.get(`${config.apiUrl}/quote`, async (request, response, context) => {
20
- return response(context.json({}));
21
- }),
22
- rest.get(`${config.apiUrl}/status`, async (request, response, context) => {
23
- return response(context.json({}));
24
- }),
25
- rest.get(`${config.apiUrl}/chains`, async (request, response, context) => {
26
- return response(context.json({ chains: [{ id: 1 }] }));
27
- }),
28
- rest.get(`${config.apiUrl}/tools`, async (request, response, context) => {
29
- return response(context.json({ bridges: [], exchanges: [] }));
30
- }),
31
- rest.get(`${config.apiUrl}/tokens`, async (request, response, context) => {
32
- return response(context.json({
33
- tokens: {
34
- [ChainId.ETH]: [findDefaultToken(CoinKey.ETH, ChainId.ETH)],
35
- },
36
- }));
37
- }),
38
- rest.post(`${config.apiUrl}/advanced/stepTransaction`, async (request, response, context) => {
39
- return response(context.json({}));
40
- }),
41
- rest.get(`${config.apiUrl}/gas/suggestion/${ChainId.OPT}`, async (request, response, context) => {
42
- return response(context.json({}));
43
- }),
44
- ];