@lifi/sdk 1.4.1 → 1.6.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 (44) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/Lifi.d.ts +7 -0
  3. package/dist/Lifi.js +72 -30
  4. package/dist/cjs/Lifi.d.ts +7 -0
  5. package/dist/cjs/Lifi.js +72 -30
  6. package/dist/cjs/execution/StatusManager.d.ts +2 -2
  7. package/dist/cjs/execution/StatusManager.js +14 -7
  8. package/dist/cjs/execution/StepExecutor.d.ts +4 -2
  9. package/dist/cjs/execution/StepExecutor.js +27 -16
  10. package/dist/cjs/execution/allowance.execute.d.ts +2 -2
  11. package/dist/cjs/execution/allowance.execute.js +14 -21
  12. package/dist/cjs/execution/bridges/bridge.execute.d.ts +2 -2
  13. package/dist/cjs/execution/bridges/bridge.execute.js +101 -90
  14. package/dist/cjs/execution/exchanges/swap.execute.d.ts +2 -2
  15. package/dist/cjs/execution/exchanges/swap.execute.js +38 -52
  16. package/dist/cjs/execution/switchChain.js +3 -3
  17. package/dist/cjs/execution/utils.js +4 -5
  18. package/dist/cjs/helpers.js +1 -1
  19. package/dist/cjs/services/ConfigService.js +1 -0
  20. package/dist/cjs/types/internal.types.d.ts +5 -1
  21. package/dist/cjs/utils/parseError.js +4 -5
  22. package/dist/cjs/utils/preRestart.js +1 -1
  23. package/dist/cjs/version.d.ts +1 -1
  24. package/dist/cjs/version.js +1 -1
  25. package/dist/execution/StatusManager.d.ts +2 -2
  26. package/dist/execution/StatusManager.js +14 -7
  27. package/dist/execution/StepExecutor.d.ts +4 -2
  28. package/dist/execution/StepExecutor.js +27 -16
  29. package/dist/execution/allowance.execute.d.ts +2 -2
  30. package/dist/execution/allowance.execute.js +14 -21
  31. package/dist/execution/bridges/bridge.execute.d.ts +2 -2
  32. package/dist/execution/bridges/bridge.execute.js +102 -91
  33. package/dist/execution/exchanges/swap.execute.d.ts +2 -2
  34. package/dist/execution/exchanges/swap.execute.js +39 -53
  35. package/dist/execution/switchChain.js +3 -3
  36. package/dist/execution/utils.js +4 -5
  37. package/dist/helpers.js +1 -1
  38. package/dist/services/ConfigService.js +1 -0
  39. package/dist/types/internal.types.d.ts +5 -1
  40. package/dist/utils/parseError.js +4 -5
  41. package/dist/utils/preRestart.js +1 -1
  42. package/dist/version.d.ts +1 -1
  43. package/dist/version.js +1 -1
  44. package/package.json +12 -12
@@ -7,13 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { constants } from 'ethers';
11
10
  import ApiService from '../../services/ApiService';
12
11
  import ChainsService from '../../services/ChainsService';
13
12
  import { LifiErrorCode, TransactionError } from '../../utils/errors';
14
13
  import { getProvider } from '../../utils/getProvider';
15
14
  import { getTransactionFailedMessage, parseError } from '../../utils/parseError';
16
- import { personalizeStep } from '../../utils/utils';
15
+ import { isZeroAddress, personalizeStep } from '../../utils/utils';
17
16
  import { checkAllowance } from '../allowance.execute';
18
17
  import { balanceCheck } from '../balanceCheck.execute';
19
18
  import { stepComparison } from '../stepComparison';
@@ -21,86 +20,72 @@ import { switchChain } from '../switchChain';
21
20
  import { waitForReceivingTransaction } from '../utils';
22
21
  export class SwapExecutionManager {
23
22
  constructor() {
24
- this.shouldContinue = true;
25
- this.setShouldContinue = (val) => {
26
- this.shouldContinue = val;
23
+ this.allowUserInteraction = true;
24
+ this.allowInteraction = (value) => {
25
+ this.allowUserInteraction = value;
27
26
  };
28
27
  this.execute = ({ signer, step, statusManager, settings, }) => __awaiter(this, void 0, void 0, function* () {
29
- // setup
30
28
  var _a, _b, _c, _d;
31
- const { action, estimate } = step;
32
29
  step.execution = statusManager.initExecutionObject(step);
33
30
  const chainsService = ChainsService.getInstance();
34
- const fromChain = yield chainsService.getChainById(action.fromChainId);
35
- // Approval
36
- if (action.fromToken.address !== constants.AddressZero) {
37
- yield checkAllowance(signer, step, fromChain, action.fromToken, action.fromAmount, estimate.approvalAddress, statusManager, settings.infiniteApproval, this.shouldContinue);
31
+ const fromChain = yield chainsService.getChainById(step.action.fromChainId);
32
+ // STEP 1: Check allowance
33
+ if (!isZeroAddress(step.action.fromToken.address)) {
34
+ yield checkAllowance(signer, step, statusManager, settings, fromChain, this.allowUserInteraction);
38
35
  }
39
- // Start Swap
40
- // -> set step.execution
41
- let swapProcess = statusManager.findOrCreateProcess('SWAP', step);
42
- // -> swapping
43
- let tx;
36
+ // STEP 2: Get transaction
37
+ let swapProcess = statusManager.findOrCreateProcess(step, 'SWAP');
38
+ let transaction;
44
39
  try {
45
40
  if (swapProcess.txHash) {
46
- // -> restore existing tx
47
- tx = yield getProvider(signer).getTransaction(swapProcess.txHash);
41
+ // Make sure that the chain is still correct
42
+ const updatedSigner = yield switchChain(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
43
+ if (!updatedSigner) {
44
+ // Chain switch was not successful, stop execution here
45
+ return step.execution;
46
+ }
47
+ signer = updatedSigner;
48
+ // Load exiting transaction
49
+ transaction = yield getProvider(signer).getTransaction(swapProcess.txHash);
48
50
  }
49
51
  else {
50
- // -> check balance
52
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'STARTED');
53
+ // Check balance
51
54
  yield balanceCheck(signer, step);
52
- // -> get tx from backend
55
+ // Create new transaction
53
56
  if (!step.transactionRequest) {
54
57
  const personalizedStep = yield personalizeStep(signer, step);
55
58
  const updatedStep = yield ApiService.getStepTransaction(personalizedStep);
56
- step = Object.assign(Object.assign({}, (yield stepComparison(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.shouldContinue))), { execution: step.execution });
59
+ step = Object.assign(Object.assign({}, (yield stepComparison(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.allowUserInteraction))), { execution: step.execution });
57
60
  }
58
61
  const { transactionRequest } = step;
59
62
  if (!transactionRequest) {
60
63
  throw new TransactionError(LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
61
64
  }
62
- // make sure that chain is still correct
63
- const updatedSigner = yield switchChain(signer, statusManager, step, settings.switchChainHook, this.shouldContinue);
65
+ // STEP 3: Send the transaction
66
+ // Make sure that the chain is still correct
67
+ const updatedSigner = yield switchChain(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
64
68
  if (!updatedSigner) {
65
- // chain switch was not successful, stop execution here
69
+ // Chain switch was not successful, stop execution here
66
70
  return step.execution;
67
71
  }
68
72
  signer = updatedSigner;
69
- // -> set step.execution
70
- swapProcess = swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
71
- if (!this.shouldContinue) {
72
- return step.execution; // stop before user interaction is needed
73
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
74
+ if (!this.allowUserInteraction) {
75
+ return step.execution;
73
76
  }
74
- // -> submit tx
75
- tx = yield signer.sendTransaction(transactionRequest);
77
+ // Submit the transaction
78
+ transaction = yield signer.sendTransaction(transactionRequest);
76
79
  }
77
- }
78
- catch (e) {
79
- const error = yield parseError(e, step, swapProcess);
80
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
81
- error: {
82
- message: error.message,
83
- htmlMessage: error.htmlMessage,
84
- code: error.code,
85
- },
80
+ // STEP 4: Wait for the transaction
81
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
82
+ txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + transaction.hash,
83
+ txHash: transaction.hash,
86
84
  });
87
- statusManager.updateExecution(step, 'FAILED');
88
- throw error;
89
- }
90
- // Wait for Transaction
91
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
92
- txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash,
93
- txHash: tx.hash,
94
- });
95
- // -> waiting
96
- let receipt;
97
- try {
98
- receipt = yield tx.wait();
85
+ yield transaction.wait();
99
86
  }
100
87
  catch (e) {
101
- // -> set status
102
88
  if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
103
- receipt = e.replacement;
104
89
  swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
105
90
  txHash: e.replacement.hash,
106
91
  txLink: fromChain.metamask.blockExplorerUrls[0] +
@@ -121,6 +106,7 @@ export class SwapExecutionManager {
121
106
  throw error;
122
107
  }
123
108
  }
109
+ // STEP 5: Wait for the receiving chain
124
110
  let statusResponse;
125
111
  try {
126
112
  if (!swapProcess.txHash) {
@@ -26,8 +26,8 @@ export const switchChain = (signer, statusManager, step, switchChainHook, allowU
26
26
  }
27
27
  // -> set status message
28
28
  step.execution = statusManager.initExecutionObject(step);
29
- statusManager.updateExecution(step, 'CHAIN_SWITCH_REQUIRED');
30
- let switchProcess = statusManager.findOrCreateProcess('SWITCH_CHAIN', step, 'PENDING');
29
+ statusManager.updateExecution(step, 'ACTION_REQUIRED');
30
+ let switchProcess = statusManager.findOrCreateProcess(step, 'SWITCH_CHAIN', 'ACTION_REQUIRED');
31
31
  if (!allowUserInteraction) {
32
32
  return;
33
33
  }
@@ -45,7 +45,7 @@ export const switchChain = (signer, statusManager, step, switchChainHook, allowU
45
45
  switchProcess = statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
46
46
  error: {
47
47
  message: error.message,
48
- code: error.code,
48
+ code: LifiErrorCode.ChainSwitchError,
49
49
  },
50
50
  });
51
51
  statusManager.updateExecution(step, 'FAILED');
@@ -32,11 +32,10 @@ export function waitForReceivingTransaction(txHash, statusManager, processType,
32
32
  case 'DONE':
33
33
  return resolve(statusResponse);
34
34
  case 'PENDING':
35
- statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', {
36
- substatus: statusResponse.substatus,
37
- substatusMessage: statusResponse.substatusMessage ||
38
- getSubstatusMessage(statusResponse.status, statusResponse.substatus),
39
- });
35
+ statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', Object.assign({ substatus: statusResponse.substatus, substatusMessage: statusResponse.substatusMessage ||
36
+ getSubstatusMessage(statusResponse.status, statusResponse.substatus) }, (statusResponse.bridgeExplorerLink && {
37
+ txLink: statusResponse.bridgeExplorerLink,
38
+ })));
40
39
  return resolve(undefined);
41
40
  case 'NOT_FOUND':
42
41
  return resolve(undefined);
package/dist/helpers.js CHANGED
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { name, version } from './version';
11
11
  const ethereumRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
12
12
  // If ethereum.request() exists, the provider is probably EIP-1193 compliant.
13
- if (!ethereum || !ethereum.request) {
13
+ if (!(ethereum === null || ethereum === void 0 ? void 0 : ethereum.request)) {
14
14
  throw new Error('Provider not available.');
15
15
  }
16
16
  return ethereum.request({
@@ -14,6 +14,7 @@ const DefaultExecutionSettings = {
14
14
  switchChainHook: () => Promise.resolve(undefined),
15
15
  acceptSlippageUpdateHook: () => Promise.resolve(undefined),
16
16
  infiniteApproval: false,
17
+ executeInBackground: false,
17
18
  };
18
19
  export default class ConfigService {
19
20
  constructor() {
@@ -64,12 +64,14 @@ export interface ExecutionSettings {
64
64
  switchChainHook?: SwitchChainHook;
65
65
  acceptSlippageUpdateHook?: AcceptSlippageUpdateHook;
66
66
  infiniteApproval?: boolean;
67
+ executeInBackground?: boolean;
67
68
  }
68
69
  export interface InternalExecutionSettings extends ExecutionSettings {
69
70
  updateCallback: CallbackFunction;
70
71
  switchChainHook: SwitchChainHook;
71
72
  acceptSlippageUpdateHook: AcceptSlippageUpdateHook;
72
73
  infiniteApproval: boolean;
74
+ executeInBackground: boolean;
73
75
  }
74
76
  export declare type EnforcedObjectProperties<T> = T & {
75
77
  [P in keyof T]-?: T[P];
@@ -81,7 +83,9 @@ export declare type RevokeTokenData = {
81
83
  token: Token;
82
84
  approvalAddress: string;
83
85
  };
84
- export interface HaltingSettings {
86
+ export interface InteractionSettings {
87
+ allowInteraction?: boolean;
85
88
  allowUpdates?: boolean;
89
+ stopExecution?: boolean;
86
90
  }
87
91
  export {};
@@ -69,7 +69,7 @@ export const getTransactionFailedMessage = (step, txLink) => {
69
69
  : baseString;
70
70
  };
71
71
  export const parseError = (e, step, process) => __awaiter(void 0, void 0, void 0, function* () {
72
- var _a, _b;
72
+ var _a, _b, _c;
73
73
  if (e instanceof LifiError) {
74
74
  return e;
75
75
  }
@@ -80,12 +80,11 @@ export const parseError = (e, step, process) => __awaiter(void 0, void 0, void 0
80
80
  // rpc errors
81
81
  // underpriced errors are sent as internal errors, so we need to parse the message manually
82
82
  if (e.code === MetaMaskErrorCodes.rpc.internal &&
83
- e.message &&
84
- e.message.includes('underpriced')) {
83
+ ((_a = e.message) === null || _a === void 0 ? void 0 : _a.includes('underpriced'))) {
85
84
  return new RPCError(LifiErrorCode.TransactionUnderpriced, 'Transaction is underpriced.', yield getTransactionNotSentMessage(step, process), e.stack);
86
85
  }
87
- if (((_a = e.message) === null || _a === void 0 ? void 0 : _a.includes('intrinsic gas too low')) ||
88
- ((_b = e.message) === null || _b === void 0 ? void 0 : _b.includes('out of gas'))) {
86
+ if (((_b = e.message) === null || _b === void 0 ? void 0 : _b.includes('intrinsic gas too low')) ||
87
+ ((_c = e.message) === null || _c === void 0 ? void 0 : _c.includes('out of gas'))) {
89
88
  return new TransactionError(LifiErrorCode.GasLimitError, 'Gas limit is too low.', yield getTransactionNotSentMessage(step, process), e.stack);
90
89
  }
91
90
  return new RPCError(e.code, getMessageFromCode(e.code), yield getTransactionNotSentMessage(step, process), e.stack);
@@ -23,7 +23,7 @@ const handleErrorType = (route, index) => {
23
23
  };
24
24
  const deleteFailedProcesses = (route, index) => {
25
25
  if (route.steps[index].execution) {
26
- route.steps[index].execution.process = route.steps[index].execution.process.filter((process) => process.status !== 'FAILED');
26
+ route.steps[index].execution.process = route.steps[index].execution.process.filter((process) => process.status === 'DONE');
27
27
  }
28
28
  };
29
29
  const deleteTransactionData = (route, index) => {
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare const name = "@lifi/sdk";
2
- export declare const version = "1.4.1";
2
+ export declare const version = "1.6.0";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk';
2
- export const version = '1.4.1';
2
+ export const version = '1.6.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/sdk",
3
- "version": "1.4.1",
3
+ "version": "1.6.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,35 +76,35 @@
76
76
  "dependencies": {
77
77
  "@ethersproject/abi": "^5.7.0",
78
78
  "@ethersproject/contracts": "^5.7.0",
79
- "@lifi/types": "^1.6.0",
79
+ "@lifi/types": "^1.7.0",
80
80
  "axios": "^0.27.2",
81
81
  "bignumber.js": "^9.1.0",
82
82
  "eth-rpc-errors": "^4.0.3",
83
- "ethers": "^5.7.0"
83
+ "ethers": "^5.7.1"
84
84
  },
85
85
  "devDependencies": {
86
- "@commitlint/cli": "^17.0.3",
87
- "@commitlint/config-conventional": "^17.0.3",
86
+ "@commitlint/cli": "^17.1.2",
87
+ "@commitlint/config-conventional": "^17.1.0",
88
88
  "@types/bip39": "^3.0.0",
89
89
  "@types/chai": "^4.3.3",
90
90
  "@types/hdkey": "^2.0.0",
91
- "@types/jest": "^28.1.8",
91
+ "@types/jest": "^29.0.2",
92
92
  "@types/websocket": "^1.0.4",
93
- "@typescript-eslint/eslint-plugin": "^5.34.0",
94
- "@typescript-eslint/parser": "^5.34.0",
95
- "eslint": "^8.22.0",
93
+ "@typescript-eslint/eslint-plugin": "^5.37.0",
94
+ "@typescript-eslint/parser": "^5.37.0",
95
+ "eslint": "^8.23.1",
96
96
  "eslint-config-prettier": "^8.3.0",
97
97
  "eslint-plugin-prettier": "^4.2.1",
98
98
  "husky": "^8.0.1",
99
- "jest": "^28.1.2",
99
+ "jest": "^29.0.3",
100
100
  "lint-staged": "^13.0.3",
101
101
  "npm-run-all": "^4.1.5",
102
102
  "pinst": "^3.0.0",
103
103
  "prettier": "^2.7.1",
104
104
  "standard-version": "^9.5.0",
105
- "ts-jest": "^28.0.8",
105
+ "ts-jest": "^29.0.1",
106
106
  "ts-loader": "^9.3.1",
107
- "typescript": "^4.7.4",
107
+ "typescript": "^4.8.3",
108
108
  "webpack": "^5.73.0",
109
109
  "webpack-cli": "^4.10.0"
110
110
  },