@lifi/sdk 1.6.3 → 1.7.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 +9 -0
- package/dist/Lifi.d.ts +1 -1
- package/dist/Lifi.js +5 -5
- package/dist/{cjs/execution/allowance.execute.d.ts → allowance/checkAllowance.d.ts} +1 -1
- package/dist/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
- package/dist/allowance/index.d.ts +2 -22
- package/dist/allowance/index.js +2 -68
- package/dist/allowance/tokenApproval.d.ts +22 -0
- package/dist/allowance/tokenApproval.js +68 -0
- package/dist/balance/checkBalance.d.ts +3 -0
- package/dist/{execution/balanceCheck.execute.js → balance/checkBalance.js} +12 -6
- package/dist/balance/getTokenBalance.d.ts +8 -0
- package/dist/{balances/index.js → balance/getTokenBalance.js} +3 -8
- package/dist/balance/index.d.ts +2 -0
- package/dist/balance/index.js +2 -0
- package/dist/{balances → balance}/utils.d.ts +0 -0
- package/dist/{balances → balance}/utils.js +1 -1
- package/dist/cjs/Lifi.d.ts +1 -1
- package/dist/cjs/Lifi.js +28 -5
- package/dist/{execution/allowance.execute.d.ts → cjs/allowance/checkAllowance.d.ts} +1 -1
- package/dist/cjs/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
- package/dist/cjs/allowance/index.d.ts +2 -22
- package/dist/cjs/allowance/index.js +15 -75
- package/dist/cjs/allowance/tokenApproval.d.ts +22 -0
- package/dist/cjs/allowance/tokenApproval.js +78 -0
- package/dist/cjs/balance/checkBalance.d.ts +3 -0
- package/dist/cjs/{execution/balanceCheck.execute.js → balance/checkBalance.js} +13 -7
- package/dist/cjs/balance/getTokenBalance.d.ts +8 -0
- package/dist/cjs/{balances/index.js → balance/getTokenBalance.js} +6 -7
- package/dist/cjs/balance/index.d.ts +2 -0
- package/dist/cjs/balance/index.js +18 -0
- package/dist/cjs/{balances → balance}/utils.d.ts +0 -0
- package/dist/cjs/{balances → balance}/utils.js +1 -1
- package/dist/cjs/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
- package/dist/cjs/execution/{bridges/bridge.execute.js → ExecutionManager.js} +52 -37
- package/dist/cjs/execution/StatusManager.d.ts +6 -3
- package/dist/cjs/execution/StatusManager.js +1 -3
- package/dist/cjs/execution/StepExecutor.d.ts +3 -5
- package/dist/cjs/execution/StepExecutor.js +7 -32
- package/dist/cjs/execution/stepComparison.d.ts +4 -4
- package/dist/cjs/execution/stepComparison.js +11 -7
- package/dist/cjs/execution/switchChain.js +1 -1
- package/dist/cjs/execution/utils.d.ts +1 -1
- package/dist/cjs/execution/utils.js +11 -10
- package/dist/cjs/services/ConfigService.js +1 -0
- package/dist/cjs/types/internal.types.d.ts +18 -24
- package/dist/cjs/utils/errors.d.ts +7 -3
- package/dist/cjs/utils/errors.js +9 -2
- package/dist/cjs/utils/multicall.d.ts +1 -1
- package/dist/cjs/utils/parseError.d.ts +0 -1
- package/dist/cjs/utils/parseError.js +1 -7
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
- package/dist/execution/{bridges/bridge.execute.js → ExecutionManager.js} +49 -34
- package/dist/execution/StatusManager.d.ts +6 -3
- package/dist/execution/StatusManager.js +1 -3
- package/dist/execution/StepExecutor.d.ts +3 -5
- package/dist/execution/StepExecutor.js +7 -32
- package/dist/execution/stepComparison.d.ts +4 -4
- package/dist/execution/stepComparison.js +12 -8
- package/dist/execution/switchChain.js +1 -1
- package/dist/execution/utils.d.ts +1 -1
- package/dist/execution/utils.js +9 -8
- package/dist/services/ConfigService.js +1 -0
- package/dist/types/internal.types.d.ts +18 -24
- package/dist/utils/errors.d.ts +7 -3
- package/dist/utils/errors.js +7 -1
- package/dist/utils/multicall.d.ts +1 -1
- package/dist/utils/parseError.d.ts +0 -1
- package/dist/utils/parseError.js +0 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +13 -15
- package/dist/balances/index.d.ts +0 -11
- package/dist/cjs/balances/index.d.ts +0 -11
- package/dist/cjs/execution/balanceCheck.execute.d.ts +0 -3
- package/dist/cjs/execution/bridges/bridge.execute.d.ts +0 -7
- package/dist/cjs/execution/exchanges/swap.execute.js +0 -152
- package/dist/execution/balanceCheck.execute.d.ts +0 -3
- package/dist/execution/bridges/bridge.execute.d.ts +0 -7
- package/dist/execution/exchanges/swap.execute.js +0 -145
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import ApiService from '../../services/ApiService';
|
|
11
|
-
import ChainsService from '../../services/ChainsService';
|
|
12
|
-
import { LifiErrorCode, TransactionError } from '../../utils/errors';
|
|
13
|
-
import { getProvider } from '../../utils/getProvider';
|
|
14
|
-
import { getTransactionFailedMessage, parseError } from '../../utils/parseError';
|
|
15
|
-
import { isZeroAddress, personalizeStep } from '../../utils/utils';
|
|
16
|
-
import { checkAllowance } from '../allowance.execute';
|
|
17
|
-
import { balanceCheck } from '../balanceCheck.execute';
|
|
18
|
-
import { stepComparison } from '../stepComparison';
|
|
19
|
-
import { switchChain } from '../switchChain';
|
|
20
|
-
import { waitForReceivingTransaction } from '../utils';
|
|
21
|
-
export class SwapExecutionManager {
|
|
22
|
-
constructor() {
|
|
23
|
-
this.allowUserInteraction = true;
|
|
24
|
-
this.allowInteraction = (value) => {
|
|
25
|
-
this.allowUserInteraction = value;
|
|
26
|
-
};
|
|
27
|
-
this.execute = ({ signer, step, statusManager, settings, }) => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
var _a, _b, _c, _d;
|
|
29
|
-
step.execution = statusManager.initExecutionObject(step);
|
|
30
|
-
const chainsService = ChainsService.getInstance();
|
|
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);
|
|
35
|
-
}
|
|
36
|
-
// STEP 2: Get transaction
|
|
37
|
-
let swapProcess = statusManager.findOrCreateProcess(step, 'SWAP');
|
|
38
|
-
let transaction;
|
|
39
|
-
try {
|
|
40
|
-
if (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);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'STARTED');
|
|
53
|
-
// Check balance
|
|
54
|
-
yield balanceCheck(signer, step);
|
|
55
|
-
// Create new transaction
|
|
56
|
-
if (!step.transactionRequest) {
|
|
57
|
-
const personalizedStep = yield personalizeStep(signer, step);
|
|
58
|
-
const updatedStep = yield ApiService.getStepTransaction(personalizedStep);
|
|
59
|
-
step = Object.assign(Object.assign({}, (yield stepComparison(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.allowUserInteraction))), { execution: step.execution });
|
|
60
|
-
}
|
|
61
|
-
const { transactionRequest } = step;
|
|
62
|
-
if (!transactionRequest) {
|
|
63
|
-
throw new TransactionError(LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
|
|
64
|
-
}
|
|
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);
|
|
68
|
-
if (!updatedSigner) {
|
|
69
|
-
// Chain switch was not successful, stop execution here
|
|
70
|
-
return step.execution;
|
|
71
|
-
}
|
|
72
|
-
signer = updatedSigner;
|
|
73
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED');
|
|
74
|
-
if (!this.allowUserInteraction) {
|
|
75
|
-
return step.execution;
|
|
76
|
-
}
|
|
77
|
-
// Submit the transaction
|
|
78
|
-
transaction = yield signer.sendTransaction(transactionRequest);
|
|
79
|
-
}
|
|
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,
|
|
84
|
-
});
|
|
85
|
-
yield transaction.wait();
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
|
|
89
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'PENDING', {
|
|
90
|
-
txHash: e.replacement.hash,
|
|
91
|
-
txLink: fromChain.metamask.blockExplorerUrls[0] +
|
|
92
|
-
'tx/' +
|
|
93
|
-
e.replacement.hash,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
const error = yield parseError(e);
|
|
98
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
|
|
99
|
-
error: {
|
|
100
|
-
message: error.message,
|
|
101
|
-
htmlMessage: error.htmlMessage,
|
|
102
|
-
code: error.code,
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
statusManager.updateExecution(step, 'FAILED');
|
|
106
|
-
throw error;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
// STEP 5: Wait for the receiving chain
|
|
110
|
-
let statusResponse;
|
|
111
|
-
try {
|
|
112
|
-
if (!swapProcess.txHash) {
|
|
113
|
-
throw new Error('Transaction hash is undefined.');
|
|
114
|
-
}
|
|
115
|
-
statusResponse = yield waitForReceivingTransaction(swapProcess.txHash, statusManager, swapProcess.type, step);
|
|
116
|
-
}
|
|
117
|
-
catch (e) {
|
|
118
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'FAILED', {
|
|
119
|
-
error: {
|
|
120
|
-
code: LifiErrorCode.TransactionFailed,
|
|
121
|
-
message: 'Failed while waiting for receiving chain.',
|
|
122
|
-
htmlMessage: getTransactionFailedMessage(step, swapProcess.txLink),
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
statusManager.updateExecution(step, 'FAILED');
|
|
126
|
-
throw e;
|
|
127
|
-
}
|
|
128
|
-
swapProcess = statusManager.updateProcess(step, swapProcess.type, 'DONE', {
|
|
129
|
-
txHash: (_a = statusResponse.receiving) === null || _a === void 0 ? void 0 : _a.txHash,
|
|
130
|
-
txLink: fromChain.metamask.blockExplorerUrls[0] +
|
|
131
|
-
'tx/' +
|
|
132
|
-
((_b = statusResponse.receiving) === null || _b === void 0 ? void 0 : _b.txHash),
|
|
133
|
-
});
|
|
134
|
-
statusManager.updateExecution(step, 'DONE', {
|
|
135
|
-
fromAmount: statusResponse.sending.amount,
|
|
136
|
-
toAmount: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.amount,
|
|
137
|
-
toToken: (_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.token,
|
|
138
|
-
gasUsed: statusResponse.sending.gasUsed,
|
|
139
|
-
gasPrice: statusResponse.sending.gasPrice,
|
|
140
|
-
});
|
|
141
|
-
// DONE
|
|
142
|
-
return step.execution;
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|