@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.
- package/CHANGELOG.md +7 -0
- package/dist/Lifi.d.ts +7 -0
- package/dist/Lifi.js +72 -30
- package/dist/cjs/Lifi.d.ts +7 -0
- package/dist/cjs/Lifi.js +72 -30
- package/dist/cjs/execution/StatusManager.d.ts +2 -2
- package/dist/cjs/execution/StatusManager.js +14 -7
- package/dist/cjs/execution/StepExecutor.d.ts +4 -2
- package/dist/cjs/execution/StepExecutor.js +27 -16
- package/dist/cjs/execution/allowance.execute.d.ts +2 -2
- package/dist/cjs/execution/allowance.execute.js +14 -21
- package/dist/cjs/execution/bridges/bridge.execute.d.ts +2 -2
- package/dist/cjs/execution/bridges/bridge.execute.js +101 -90
- package/dist/cjs/execution/exchanges/swap.execute.d.ts +2 -2
- package/dist/cjs/execution/exchanges/swap.execute.js +38 -52
- package/dist/cjs/execution/switchChain.js +3 -3
- package/dist/cjs/helpers.js +1 -1
- package/dist/cjs/services/ConfigService.js +1 -0
- package/dist/cjs/types/internal.types.d.ts +5 -1
- package/dist/cjs/utils/parseError.js +4 -5
- package/dist/cjs/utils/preRestart.js +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/execution/StatusManager.d.ts +2 -2
- package/dist/execution/StatusManager.js +14 -7
- package/dist/execution/StepExecutor.d.ts +4 -2
- package/dist/execution/StepExecutor.js +27 -16
- package/dist/execution/allowance.execute.d.ts +2 -2
- package/dist/execution/allowance.execute.js +14 -21
- package/dist/execution/bridges/bridge.execute.d.ts +2 -2
- package/dist/execution/bridges/bridge.execute.js +102 -91
- package/dist/execution/exchanges/swap.execute.d.ts +2 -2
- package/dist/execution/exchanges/swap.execute.js +39 -53
- package/dist/execution/switchChain.js +3 -3
- package/dist/helpers.js +1 -1
- package/dist/services/ConfigService.js +1 -0
- package/dist/types/internal.types.d.ts +5 -1
- package/dist/utils/parseError.js +4 -5
- package/dist/utils/preRestart.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- 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
|
-
|
|
5
|
-
|
|
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.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
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
|
|
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
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
51
|
-
let crossChainProcess = statusManager.findOrCreateProcess('CROSS_CHAIN'
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
114
|
-
|
|
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
|
-
|
|
5
|
-
|
|
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.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
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
|
-
//
|
|
42
|
-
if (action.fromToken.address
|
|
43
|
-
yield (0, allowance_execute_1.checkAllowance)(signer, step,
|
|
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
|
-
//
|
|
46
|
-
|
|
47
|
-
let
|
|
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
|
-
//
|
|
53
|
-
|
|
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
|
-
|
|
58
|
+
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'STARTED');
|
|
59
|
+
// Check balance
|
|
57
60
|
yield (0, balanceCheck_execute_1.balanceCheck)(signer, step);
|
|
58
|
-
//
|
|
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.
|
|
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
|
-
//
|
|
69
|
-
|
|
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
|
-
//
|
|
75
|
+
// Chain switch was not successful, stop execution here
|
|
72
76
|
return step.execution;
|
|
73
77
|
}
|
|
74
78
|
signer = updatedSigner;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
//
|
|
81
|
-
|
|
83
|
+
// Submit the transaction
|
|
84
|
+
transaction = yield signer.sendTransaction(transactionRequest);
|
|
82
85
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
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, '
|
|
33
|
-
let switchProcess = statusManager.findOrCreateProcess('SWITCH_CHAIN',
|
|
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:
|
|
51
|
+
code: errors_1.LifiErrorCode.ChainSwitchError,
|
|
52
52
|
},
|
|
53
53
|
});
|
|
54
54
|
statusManager.updateExecution(step, 'FAILED');
|
package/dist/cjs/helpers.js
CHANGED
|
@@ -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 ||
|
|
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({
|
|
@@ -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
|
|
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 (((
|
|
96
|
-
((
|
|
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
|
|
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) => {
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const name = "@lifi/sdk";
|
|
2
|
-
export declare const version = "1.
|
|
2
|
+
export declare const version = "1.5.0";
|
package/dist/cjs/version.js
CHANGED
|
@@ -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: (
|
|
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
|
-
|
|
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 = (
|
|
41
|
-
|
|
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
|
-
|
|
167
|
+
allowUpdates(value) {
|
|
161
168
|
this.shouldUpdate = value;
|
|
162
169
|
}
|
|
163
170
|
}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { Signer } from 'ethers';
|
|
2
|
-
import {
|
|
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
|
-
|
|
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;
|