@lifi/sdk 1.4.1 → 1.5.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 (42) hide show
  1. package/CHANGELOG.md +7 -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/helpers.js +1 -1
  18. package/dist/cjs/services/ConfigService.js +1 -0
  19. package/dist/cjs/types/internal.types.d.ts +5 -1
  20. package/dist/cjs/utils/parseError.js +4 -5
  21. package/dist/cjs/utils/preRestart.js +1 -1
  22. package/dist/cjs/version.d.ts +1 -1
  23. package/dist/cjs/version.js +1 -1
  24. package/dist/execution/StatusManager.d.ts +2 -2
  25. package/dist/execution/StatusManager.js +14 -7
  26. package/dist/execution/StepExecutor.d.ts +4 -2
  27. package/dist/execution/StepExecutor.js +27 -16
  28. package/dist/execution/allowance.execute.d.ts +2 -2
  29. package/dist/execution/allowance.execute.js +14 -21
  30. package/dist/execution/bridges/bridge.execute.d.ts +2 -2
  31. package/dist/execution/bridges/bridge.execute.js +102 -91
  32. package/dist/execution/exchanges/swap.execute.d.ts +2 -2
  33. package/dist/execution/exchanges/swap.execute.js +39 -53
  34. package/dist/execution/switchChain.js +3 -3
  35. package/dist/helpers.js +1 -1
  36. package/dist/services/ConfigService.js +1 -0
  37. package/dist/types/internal.types.d.ts +5 -1
  38. package/dist/utils/parseError.js +4 -5
  39. package/dist/utils/preRestart.js +1 -1
  40. package/dist/version.d.ts +1 -1
  41. package/dist/version.js +1 -1
  42. package/package.json +9 -9
@@ -1,7 +1,7 @@
1
1
  import { Execution } from '@lifi/types';
2
2
  import { ExecuteCrossParams } from '../../types';
3
3
  export declare class BridgeExecutionManager {
4
- shouldContinue: boolean;
5
- setShouldContinue: (val: boolean) => void;
4
+ allowUserInteraction: boolean;
5
+ allowInteraction: (value: boolean) => void;
6
6
  execute: ({ signer, step, statusManager, settings, }: ExecuteCrossParams) => Promise<Execution>;
7
7
  }
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.BridgeExecutionManager = void 0;
16
- const ethers_1 = require("ethers");
17
16
  const ApiService_1 = __importDefault(require("../../services/ApiService"));
18
17
  const ChainsService_1 = __importDefault(require("../../services/ChainsService"));
19
18
  const errors_1 = require("../../utils/errors");
@@ -27,98 +26,125 @@ const switchChain_1 = require("../switchChain");
27
26
  const utils_2 = require("../utils");
28
27
  class BridgeExecutionManager {
29
28
  constructor() {
30
- this.shouldContinue = true;
31
- this.setShouldContinue = (val) => {
32
- this.shouldContinue = val;
29
+ this.allowUserInteraction = true;
30
+ this.allowInteraction = (value) => {
31
+ this.allowUserInteraction = value;
33
32
  };
34
33
  this.execute = ({ signer, step, statusManager, settings, }) => __awaiter(this, void 0, void 0, function* () {
35
34
  var _a, _b, _c, _d;
36
- const { action, estimate } = step;
37
35
  step.execution = statusManager.initExecutionObject(step);
38
36
  const chainsService = ChainsService_1.default.getInstance();
39
- const fromChain = yield chainsService.getChainById(action.fromChainId);
40
- const toChain = yield chainsService.getChainById(action.toChainId);
41
- // STEP 1: Check Allowance ////////////////////////////////////////////////
42
- // approval still needed?
37
+ const fromChain = yield chainsService.getChainById(step.action.fromChainId);
38
+ const toChain = yield chainsService.getChainById(step.action.toChainId);
39
+ // STEP 1: Check allowance
43
40
  const oldCrossProcess = step.execution.process.find((p) => p.type === 'CROSS_CHAIN');
44
- if (!(oldCrossProcess === null || oldCrossProcess === void 0 ? void 0 : oldCrossProcess.txHash)) {
45
- if (action.fromToken.address !== ethers_1.constants.AddressZero) {
46
- // Check Token Approval only if fromToken is not the native token => no approval needed in that case
47
- yield (0, allowance_execute_1.checkAllowance)(signer, step, fromChain, action.fromToken, action.fromAmount, estimate.approvalAddress, statusManager, settings.infiniteApproval, this.shouldContinue);
48
- }
41
+ // Check token approval only if fromToken is not the native token => no approval needed in that case
42
+ if (!(oldCrossProcess === null || oldCrossProcess === void 0 ? void 0 : oldCrossProcess.txHash) &&
43
+ !(0, utils_1.isZeroAddress)(step.action.fromToken.address)) {
44
+ yield (0, allowance_execute_1.checkAllowance)(signer, step, statusManager, settings, fromChain, this.allowUserInteraction);
49
45
  }
50
- // STEP 2: Get Transaction ////////////////////////////////////////////////
51
- let crossChainProcess = statusManager.findOrCreateProcess('CROSS_CHAIN', step);
52
- try {
53
- let tx;
54
- if (crossChainProcess.txHash) {
55
- // load exiting transaction
56
- tx = yield (0, getProvider_1.getProvider)(signer).getTransaction(crossChainProcess.txHash);
57
- }
58
- else {
59
- // check balance
60
- yield (0, balanceCheck_execute_1.balanceCheck)(signer, step);
61
- if (!step.transactionRequest) {
62
- const personalizedStep = yield (0, utils_1.personalizeStep)(signer, step);
63
- const updatedStep = yield ApiService_1.default.getStepTransaction(personalizedStep);
64
- step = Object.assign(Object.assign({}, (yield (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.shouldContinue))), { execution: step.execution });
46
+ // STEP 2: Get transaction
47
+ let crossChainProcess = statusManager.findOrCreateProcess(step, 'CROSS_CHAIN');
48
+ if (crossChainProcess.status !== 'DONE') {
49
+ try {
50
+ let transaction;
51
+ if (crossChainProcess.txHash) {
52
+ // Make sure that the chain is still correct
53
+ const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
54
+ if (!updatedSigner) {
55
+ // Chain switch was not successful, stop execution here
56
+ return step.execution;
57
+ }
58
+ signer = updatedSigner;
59
+ // Load exiting transaction
60
+ transaction = yield (0, getProvider_1.getProvider)(signer).getTransaction(crossChainProcess.txHash);
65
61
  }
66
- const { transactionRequest } = step;
67
- if (!transactionRequest) {
68
- throw new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
62
+ else {
63
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'STARTED');
64
+ // Check balance
65
+ yield (0, balanceCheck_execute_1.balanceCheck)(signer, step);
66
+ // Create new transaction
67
+ if (!step.transactionRequest) {
68
+ const personalizedStep = yield (0, utils_1.personalizeStep)(signer, step);
69
+ const updatedStep = yield ApiService_1.default.getStepTransaction(personalizedStep);
70
+ step = Object.assign(Object.assign({}, (yield (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.allowUserInteraction))), { execution: step.execution });
71
+ }
72
+ const { transactionRequest } = step;
73
+ if (!transactionRequest) {
74
+ throw new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
75
+ }
76
+ // STEP 3: Send the transaction
77
+ // Make sure that the chain is still correct
78
+ const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
79
+ if (!updatedSigner) {
80
+ // Chain switch was not successful, stop execution here
81
+ return step.execution;
82
+ }
83
+ signer = updatedSigner;
84
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'ACTION_REQUIRED');
85
+ if (!this.allowUserInteraction) {
86
+ return step.execution;
87
+ }
88
+ // Submit the transaction
89
+ transaction = yield signer.sendTransaction(transactionRequest);
90
+ // STEP 4: Wait for the transaction
91
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', {
92
+ txHash: transaction.hash,
93
+ txLink: fromChain.metamask.blockExplorerUrls[0] +
94
+ 'tx/' +
95
+ transaction.hash,
96
+ });
69
97
  }
70
- // STEP 3: Send Transaction ///////////////////////////////////////////////
71
- // make sure that chain is still correct
72
- const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.shouldContinue);
73
- if (!updatedSigner) {
74
- // chain switch was not successful, stop execution here
75
- return step.execution;
98
+ yield transaction.wait();
99
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'DONE');
100
+ }
101
+ catch (e) {
102
+ if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
103
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'DONE', {
104
+ txHash: e.replacement.hash,
105
+ txLink: fromChain.metamask.blockExplorerUrls[0] +
106
+ 'tx/' +
107
+ e.replacement.hash,
108
+ });
76
109
  }
77
- signer = updatedSigner;
78
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'ACTION_REQUIRED');
79
- if (!this.shouldContinue) {
80
- return step.execution;
110
+ else {
111
+ const error = yield (0, parseError_1.parseError)(e, step, crossChainProcess);
112
+ crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'FAILED', {
113
+ error: {
114
+ message: error.message,
115
+ htmlMessage: error.htmlMessage,
116
+ code: error.code,
117
+ },
118
+ });
119
+ statusManager.updateExecution(step, 'FAILED');
120
+ throw error;
81
121
  }
82
- tx = yield signer.sendTransaction(transactionRequest);
83
- // STEP 4: Wait for Transaction ///////////////////////////////////////////
84
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', {
85
- txHash: tx.hash,
86
- txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash,
87
- });
88
- }
89
- yield tx.wait();
90
- }
91
- catch (e) {
92
- if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
93
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', {
94
- txHash: e.replacement.hash,
95
- txLink: fromChain.metamask.blockExplorerUrls[0] +
96
- 'tx/' +
97
- e.replacement.hash,
98
- });
99
- }
100
- else {
101
- const error = yield (0, parseError_1.parseError)(e, step, crossChainProcess);
102
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'FAILED', {
103
- error: {
104
- message: error.message,
105
- htmlMessage: error.htmlMessage,
106
- code: error.code,
107
- },
108
- });
109
- statusManager.updateExecution(step, 'FAILED');
110
- throw error;
111
122
  }
112
123
  }
113
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'DONE');
114
- // STEP 5: Wait for Receiver //////////////////////////////////////
115
- let receivingChainProcess = statusManager.findOrCreateProcess('RECEIVING_CHAIN', step, 'PENDING');
124
+ // STEP 5: Wait for the receiving chain
125
+ let receivingChainProcess = statusManager.findOrCreateProcess(step, 'RECEIVING_CHAIN', 'PENDING');
116
126
  let statusResponse;
117
127
  try {
118
128
  if (!crossChainProcess.txHash) {
119
129
  throw new Error('Transaction hash is undefined.');
120
130
  }
121
131
  statusResponse = yield (0, utils_2.waitForReceivingTransaction)(crossChainProcess.txHash, statusManager, receivingChainProcess.type, step);
132
+ receivingChainProcess = statusManager.updateProcess(step, receivingChainProcess.type, 'DONE', {
133
+ substatus: statusResponse.substatus,
134
+ substatusMessage: statusResponse.substatusMessage ||
135
+ (0, utils_2.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
136
+ txHash: (_a = statusResponse.receiving) === null || _a === void 0 ? void 0 : _a.txHash,
137
+ txLink: toChain.metamask.blockExplorerUrls[0] +
138
+ 'tx/' +
139
+ ((_b = statusResponse.receiving) === null || _b === void 0 ? void 0 : _b.txHash),
140
+ });
141
+ statusManager.updateExecution(step, 'DONE', {
142
+ fromAmount: statusResponse.sending.amount,
143
+ toAmount: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.amount,
144
+ toToken: (_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.token,
145
+ gasUsed: statusResponse.sending.gasUsed,
146
+ gasPrice: statusResponse.sending.gasPrice,
147
+ });
122
148
  }
123
149
  catch (e) {
124
150
  receivingChainProcess = statusManager.updateProcess(step, receivingChainProcess.type, 'FAILED', {
@@ -129,24 +155,9 @@ class BridgeExecutionManager {
129
155
  },
130
156
  });
131
157
  statusManager.updateExecution(step, 'FAILED');
158
+ console.warn(e);
132
159
  throw e;
133
160
  }
134
- receivingChainProcess = statusManager.updateProcess(step, receivingChainProcess.type, 'DONE', {
135
- substatus: statusResponse.substatus,
136
- substatusMessage: statusResponse.substatusMessage ||
137
- (0, utils_2.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
138
- txHash: (_a = statusResponse.receiving) === null || _a === void 0 ? void 0 : _a.txHash,
139
- txLink: toChain.metamask.blockExplorerUrls[0] +
140
- 'tx/' +
141
- ((_b = statusResponse.receiving) === null || _b === void 0 ? void 0 : _b.txHash),
142
- });
143
- statusManager.updateExecution(step, 'DONE', {
144
- fromAmount: statusResponse.sending.amount,
145
- toAmount: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.amount,
146
- toToken: (_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.token,
147
- gasUsed: statusResponse.sending.gasUsed,
148
- gasPrice: statusResponse.sending.gasPrice,
149
- });
150
161
  // DONE
151
162
  return step.execution;
152
163
  });
@@ -1,7 +1,7 @@
1
1
  import { Execution } from '@lifi/types';
2
2
  import { ExecuteSwapParams } from '../../types';
3
3
  export declare class SwapExecutionManager {
4
- shouldContinue: boolean;
5
- setShouldContinue: (val: boolean) => void;
4
+ allowUserInteraction: boolean;
5
+ allowInteraction: (value: boolean) => void;
6
6
  execute: ({ signer, step, statusManager, settings, }: ExecuteSwapParams) => Promise<Execution>;
7
7
  }
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SwapExecutionManager = void 0;
16
- const ethers_1 = require("ethers");
17
16
  const ApiService_1 = __importDefault(require("../../services/ApiService"));
18
17
  const ChainsService_1 = __importDefault(require("../../services/ChainsService"));
19
18
  const errors_1 = require("../../utils/errors");
@@ -27,86 +26,72 @@ const switchChain_1 = require("../switchChain");
27
26
  const utils_2 = require("../utils");
28
27
  class SwapExecutionManager {
29
28
  constructor() {
30
- this.shouldContinue = true;
31
- this.setShouldContinue = (val) => {
32
- this.shouldContinue = val;
29
+ this.allowUserInteraction = true;
30
+ this.allowInteraction = (value) => {
31
+ this.allowUserInteraction = value;
33
32
  };
34
33
  this.execute = ({ signer, step, statusManager, settings, }) => __awaiter(this, void 0, void 0, function* () {
35
- // setup
36
34
  var _a, _b, _c, _d;
37
- const { action, estimate } = step;
38
35
  step.execution = statusManager.initExecutionObject(step);
39
36
  const chainsService = ChainsService_1.default.getInstance();
40
- const fromChain = yield chainsService.getChainById(action.fromChainId);
41
- // Approval
42
- if (action.fromToken.address !== ethers_1.constants.AddressZero) {
43
- yield (0, allowance_execute_1.checkAllowance)(signer, step, fromChain, action.fromToken, action.fromAmount, estimate.approvalAddress, statusManager, settings.infiniteApproval, this.shouldContinue);
37
+ const fromChain = yield chainsService.getChainById(step.action.fromChainId);
38
+ // STEP 1: Check allowance
39
+ if (!(0, utils_1.isZeroAddress)(step.action.fromToken.address)) {
40
+ yield (0, allowance_execute_1.checkAllowance)(signer, step, statusManager, settings, fromChain, this.allowUserInteraction);
44
41
  }
45
- // Start Swap
46
- // -> set step.execution
47
- let swapProcess = statusManager.findOrCreateProcess('SWAP', step);
48
- // -> swapping
49
- let tx;
42
+ // STEP 2: Get transaction
43
+ let swapProcess = statusManager.findOrCreateProcess(step, 'SWAP');
44
+ let transaction;
50
45
  try {
51
46
  if (swapProcess.txHash) {
52
- // -> restore existing tx
53
- tx = yield (0, getProvider_1.getProvider)(signer).getTransaction(swapProcess.txHash);
47
+ // Make sure that the chain is still correct
48
+ const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
49
+ if (!updatedSigner) {
50
+ // Chain switch was not successful, stop execution here
51
+ return step.execution;
52
+ }
53
+ signer = updatedSigner;
54
+ // Load exiting transaction
55
+ transaction = yield (0, getProvider_1.getProvider)(signer).getTransaction(swapProcess.txHash);
54
56
  }
55
57
  else {
56
- // -> check balance
58
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'STARTED');
59
+ // Check balance
57
60
  yield (0, balanceCheck_execute_1.balanceCheck)(signer, step);
58
- // -> get tx from backend
61
+ // Create new transaction
59
62
  if (!step.transactionRequest) {
60
63
  const personalizedStep = yield (0, utils_1.personalizeStep)(signer, step);
61
64
  const updatedStep = yield ApiService_1.default.getStepTransaction(personalizedStep);
62
- step = Object.assign(Object.assign({}, (yield (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.shouldContinue))), { execution: step.execution });
65
+ step = Object.assign(Object.assign({}, (yield (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.allowUserInteraction))), { execution: step.execution });
63
66
  }
64
67
  const { transactionRequest } = step;
65
68
  if (!transactionRequest) {
66
69
  throw new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
67
70
  }
68
- // make sure that chain is still correct
69
- const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.shouldContinue);
71
+ // STEP 3: Send the transaction
72
+ // Make sure that the chain is still correct
73
+ const updatedSigner = yield (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
70
74
  if (!updatedSigner) {
71
- // chain switch was not successful, stop execution here
75
+ // Chain switch was not successful, stop execution here
72
76
  return step.execution;
73
77
  }
74
78
  signer = updatedSigner;
75
- // -> set step.execution
76
- swapProcess = swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
77
- if (!this.shouldContinue) {
78
- return step.execution; // stop before user interaction is needed
79
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
80
+ if (!this.allowUserInteraction) {
81
+ return step.execution;
79
82
  }
80
- // -> submit tx
81
- tx = yield signer.sendTransaction(transactionRequest);
83
+ // Submit the transaction
84
+ transaction = yield signer.sendTransaction(transactionRequest);
82
85
  }
83
- }
84
- catch (e) {
85
- const error = yield (0, parseError_1.parseError)(e, step, swapProcess);
86
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
87
- error: {
88
- message: error.message,
89
- htmlMessage: error.htmlMessage,
90
- code: error.code,
91
- },
86
+ // STEP 4: Wait for the transaction
87
+ swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
88
+ txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + transaction.hash,
89
+ txHash: transaction.hash,
92
90
  });
93
- statusManager.updateExecution(step, 'FAILED');
94
- throw error;
95
- }
96
- // Wait for Transaction
97
- swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
98
- txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash,
99
- txHash: tx.hash,
100
- });
101
- // -> waiting
102
- let receipt;
103
- try {
104
- receipt = yield tx.wait();
91
+ yield transaction.wait();
105
92
  }
106
93
  catch (e) {
107
- // -> set status
108
94
  if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
109
- receipt = e.replacement;
110
95
  swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
111
96
  txHash: e.replacement.hash,
112
97
  txLink: fromChain.metamask.blockExplorerUrls[0] +
@@ -127,6 +112,7 @@ class SwapExecutionManager {
127
112
  throw error;
128
113
  }
129
114
  }
115
+ // STEP 5: Wait for the receiving chain
130
116
  let statusResponse;
131
117
  try {
132
118
  if (!swapProcess.txHash) {
@@ -29,8 +29,8 @@ const switchChain = (signer, statusManager, step, switchChainHook, allowUserInte
29
29
  }
30
30
  // -> set status message
31
31
  step.execution = statusManager.initExecutionObject(step);
32
- statusManager.updateExecution(step, 'CHAIN_SWITCH_REQUIRED');
33
- let switchProcess = statusManager.findOrCreateProcess('SWITCH_CHAIN', step, 'PENDING');
32
+ statusManager.updateExecution(step, 'ACTION_REQUIRED');
33
+ let switchProcess = statusManager.findOrCreateProcess(step, 'SWITCH_CHAIN', 'ACTION_REQUIRED');
34
34
  if (!allowUserInteraction) {
35
35
  return;
36
36
  }
@@ -48,7 +48,7 @@ const switchChain = (signer, statusManager, step, switchChainHook, allowUserInte
48
48
  switchProcess = statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
49
49
  error: {
50
50
  message: error.message,
51
- code: error.code,
51
+ code: errors_1.LifiErrorCode.ChainSwitchError,
52
52
  },
53
53
  });
54
54
  statusManager.updateExecution(step, 'FAILED');
@@ -13,7 +13,7 @@ exports.checkPackageUpdates = exports.isSameToken = exports.getRandomNumber = ex
13
13
  const version_1 = require("./version");
14
14
  const ethereumRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
15
15
  // If ethereum.request() exists, the provider is probably EIP-1193 compliant.
16
- if (!ethereum || !ethereum.request) {
16
+ if (!(ethereum === null || ethereum === void 0 ? void 0 : ethereum.request)) {
17
17
  throw new Error('Provider not available.');
18
18
  }
19
19
  return ethereum.request({
@@ -16,6 +16,7 @@ const DefaultExecutionSettings = {
16
16
  switchChainHook: () => Promise.resolve(undefined),
17
17
  acceptSlippageUpdateHook: () => Promise.resolve(undefined),
18
18
  infiniteApproval: false,
19
+ executeInBackground: false,
19
20
  };
20
21
  class ConfigService {
21
22
  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 {};
@@ -77,7 +77,7 @@ const getTransactionFailedMessage = (step, txLink) => {
77
77
  };
78
78
  exports.getTransactionFailedMessage = getTransactionFailedMessage;
79
79
  const parseError = (e, step, process) => __awaiter(void 0, void 0, void 0, function* () {
80
- var _a, _b;
80
+ var _a, _b, _c;
81
81
  if (e instanceof errors_1.LifiError) {
82
82
  return e;
83
83
  }
@@ -88,12 +88,11 @@ const parseError = (e, step, process) => __awaiter(void 0, void 0, void 0, funct
88
88
  // rpc errors
89
89
  // underpriced errors are sent as internal errors, so we need to parse the message manually
90
90
  if (e.code === eth_rpc_errors_1.errorCodes.rpc.internal &&
91
- e.message &&
92
- e.message.includes('underpriced')) {
91
+ ((_a = e.message) === null || _a === void 0 ? void 0 : _a.includes('underpriced'))) {
93
92
  return new errors_1.RPCError(errors_1.LifiErrorCode.TransactionUnderpriced, 'Transaction is underpriced.', yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
94
93
  }
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'))) {
94
+ if (((_b = e.message) === null || _b === void 0 ? void 0 : _b.includes('intrinsic gas too low')) ||
95
+ ((_c = e.message) === null || _c === void 0 ? void 0 : _c.includes('out of gas'))) {
97
96
  return new errors_1.TransactionError(errors_1.LifiErrorCode.GasLimitError, 'Gas limit is too low.', yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
98
97
  }
99
98
  return new errors_1.RPCError(e.code, (0, eth_rpc_errors_1.getMessageFromCode)(e.code), yield (0, exports.getTransactionNotSentMessage)(step, process), e.stack);
@@ -27,7 +27,7 @@ const handleErrorType = (route, index) => {
27
27
  };
28
28
  const deleteFailedProcesses = (route, index) => {
29
29
  if (route.steps[index].execution) {
30
- route.steps[index].execution.process = route.steps[index].execution.process.filter((process) => process.status !== 'FAILED');
30
+ route.steps[index].execution.process = route.steps[index].execution.process.filter((process) => process.status === 'DONE');
31
31
  }
32
32
  };
33
33
  const deleteTransactionData = (route, index) => {
@@ -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.5.0";
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = exports.name = void 0;
4
4
  exports.name = '@lifi/sdk';
5
- exports.version = '1.4.1';
5
+ exports.version = '1.5.0';
@@ -42,7 +42,7 @@ export declare class StatusManager {
42
42
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
43
43
  * @return {Process}
44
44
  */
45
- findOrCreateProcess: (type: ProcessType, step: Step, status?: Status) => Process;
45
+ findOrCreateProcess: (step: Step, type: ProcessType, status?: Status) => Process;
46
46
  /**
47
47
  * Update a process object.
48
48
  * @param {Step} step The step where the process should be updated
@@ -60,6 +60,6 @@ export declare class StatusManager {
60
60
  */
61
61
  removeProcess: (step: Step, type: ProcessType) => void;
62
62
  updateStepInRoute: (step: Step) => Step;
63
- setShouldUpdate(value: boolean): void;
63
+ allowUpdates(value: boolean): void;
64
64
  }
65
65
  export {};
@@ -37,12 +37,17 @@ export class StatusManager {
37
37
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
38
38
  * @return {Process}
39
39
  */
40
- this.findOrCreateProcess = (type, step, status) => {
41
- if (!step.execution || !step.execution.process) {
40
+ this.findOrCreateProcess = (step, type, status) => {
41
+ var _a;
42
+ if (!((_a = step.execution) === null || _a === void 0 ? void 0 : _a.process)) {
42
43
  throw new Error("Execution hasn't been initialized.");
43
44
  }
44
45
  const process = step.execution.process.find((p) => p.type === type);
45
46
  if (process) {
47
+ if (status && process.status !== status) {
48
+ process.status = status;
49
+ this.updateStepInRoute(step);
50
+ }
46
51
  return process;
47
52
  }
48
53
  const newProcess = {
@@ -64,7 +69,7 @@ export class StatusManager {
64
69
  * @return {Process} The update process
65
70
  */
66
71
  this.updateProcess = (step, type, status, params) => {
67
- var _a;
72
+ var _a, _b, _c;
68
73
  if (!step.execution) {
69
74
  throw new Error("Can't update an empty step execution.");
70
75
  }
@@ -89,9 +94,6 @@ export class StatusManager {
89
94
  case 'ACTION_REQUIRED':
90
95
  step.execution.status = 'ACTION_REQUIRED';
91
96
  break;
92
- case 'CHAIN_SWITCH_REQUIRED':
93
- step.execution.status = 'CHAIN_SWITCH_REQUIRED';
94
- break;
95
97
  default:
96
98
  break;
97
99
  }
@@ -103,6 +105,11 @@ export class StatusManager {
103
105
  currentProcess[key] = value;
104
106
  }
105
107
  }
108
+ // Sort processes, the ones with DONE status go first
109
+ step.execution.process = [
110
+ ...(_b = step === null || step === void 0 ? void 0 : step.execution) === null || _b === void 0 ? void 0 : _b.process.filter((process) => process.status === 'DONE'),
111
+ ...(_c = step === null || step === void 0 ? void 0 : step.execution) === null || _c === void 0 ? void 0 : _c.process.filter((process) => process.status !== 'DONE'),
112
+ ];
106
113
  this.updateStepInRoute(step); // updates the step in the route
107
114
  return currentProcess;
108
115
  };
@@ -157,7 +164,7 @@ export class StatusManager {
157
164
  this.updateStepInRoute(step);
158
165
  return step;
159
166
  }
160
- setShouldUpdate(value) {
167
+ allowUpdates(value) {
161
168
  this.shouldUpdate = value;
162
169
  }
163
170
  }
@@ -1,14 +1,16 @@
1
1
  import { Signer } from 'ethers';
2
- import { HaltingSettings, InternalExecutionSettings, Step } from '../types';
2
+ import { InteractionSettings, InternalExecutionSettings, Step } from '../types';
3
3
  import { StatusManager } from './StatusManager';
4
4
  export declare class StepExecutor {
5
5
  settings: InternalExecutionSettings;
6
6
  statusManager: StatusManager;
7
7
  private swapExecutionManager;
8
8
  private bridgeExecutionManager;
9
+ allowUserInteraction: boolean;
9
10
  executionStopped: boolean;
10
11
  constructor(statusManager: StatusManager, settings: InternalExecutionSettings);
11
- stopStepExecution: (settings?: HaltingSettings) => void;
12
+ setInteraction: (settings?: InteractionSettings) => void;
13
+ checkChain: () => never;
12
14
  executeStep: (signer: Signer, step: Step) => Promise<Step>;
13
15
  private executeSwap;
14
16
  private executeCross;