@lifi/sdk 1.6.4 → 1.7.1

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 (82) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/Lifi.d.ts +1 -1
  3. package/dist/Lifi.js +5 -5
  4. package/dist/{cjs/execution/allowance.execute.d.ts → allowance/checkAllowance.d.ts} +1 -1
  5. package/dist/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
  6. package/dist/allowance/index.d.ts +2 -22
  7. package/dist/allowance/index.js +2 -68
  8. package/dist/allowance/tokenApproval.d.ts +22 -0
  9. package/dist/allowance/tokenApproval.js +68 -0
  10. package/dist/balance/checkBalance.d.ts +3 -0
  11. package/dist/{execution/balanceCheck.execute.js → balance/checkBalance.js} +12 -6
  12. package/dist/balance/getTokenBalance.d.ts +8 -0
  13. package/dist/{balances/index.js → balance/getTokenBalance.js} +3 -8
  14. package/dist/balance/index.d.ts +2 -0
  15. package/dist/balance/index.js +2 -0
  16. package/dist/{balances → balance}/utils.d.ts +0 -0
  17. package/dist/{balances → balance}/utils.js +1 -1
  18. package/dist/cjs/Lifi.d.ts +1 -1
  19. package/dist/cjs/Lifi.js +28 -5
  20. package/dist/{execution/allowance.execute.d.ts → cjs/allowance/checkAllowance.d.ts} +1 -1
  21. package/dist/cjs/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
  22. package/dist/cjs/allowance/index.d.ts +2 -22
  23. package/dist/cjs/allowance/index.js +15 -75
  24. package/dist/cjs/allowance/tokenApproval.d.ts +22 -0
  25. package/dist/cjs/allowance/tokenApproval.js +78 -0
  26. package/dist/cjs/balance/checkBalance.d.ts +3 -0
  27. package/dist/cjs/{execution/balanceCheck.execute.js → balance/checkBalance.js} +13 -7
  28. package/dist/cjs/balance/getTokenBalance.d.ts +8 -0
  29. package/dist/cjs/{balances/index.js → balance/getTokenBalance.js} +6 -7
  30. package/dist/cjs/balance/index.d.ts +2 -0
  31. package/dist/cjs/balance/index.js +18 -0
  32. package/dist/cjs/{balances → balance}/utils.d.ts +0 -0
  33. package/dist/cjs/{balances → balance}/utils.js +1 -1
  34. package/dist/cjs/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
  35. package/dist/cjs/execution/{bridges/bridge.execute.js → ExecutionManager.js} +52 -37
  36. package/dist/cjs/execution/StatusManager.d.ts +6 -3
  37. package/dist/cjs/execution/StatusManager.js +1 -3
  38. package/dist/cjs/execution/StepExecutor.d.ts +3 -5
  39. package/dist/cjs/execution/StepExecutor.js +7 -32
  40. package/dist/cjs/execution/stepComparison.d.ts +4 -4
  41. package/dist/cjs/execution/stepComparison.js +11 -7
  42. package/dist/cjs/execution/switchChain.js +1 -1
  43. package/dist/cjs/execution/utils.d.ts +1 -1
  44. package/dist/cjs/execution/utils.js +11 -10
  45. package/dist/cjs/services/ConfigService.js +1 -0
  46. package/dist/cjs/types/internal.types.d.ts +18 -24
  47. package/dist/cjs/utils/errors.d.ts +7 -3
  48. package/dist/cjs/utils/errors.js +9 -2
  49. package/dist/cjs/utils/multicall.d.ts +1 -1
  50. package/dist/cjs/utils/parseError.d.ts +0 -1
  51. package/dist/cjs/utils/parseError.js +1 -7
  52. package/dist/cjs/version.d.ts +1 -1
  53. package/dist/cjs/version.js +1 -1
  54. package/dist/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
  55. package/dist/execution/{bridges/bridge.execute.js → ExecutionManager.js} +49 -34
  56. package/dist/execution/StatusManager.d.ts +6 -3
  57. package/dist/execution/StatusManager.js +1 -3
  58. package/dist/execution/StepExecutor.d.ts +3 -5
  59. package/dist/execution/StepExecutor.js +7 -32
  60. package/dist/execution/stepComparison.d.ts +4 -4
  61. package/dist/execution/stepComparison.js +12 -8
  62. package/dist/execution/switchChain.js +1 -1
  63. package/dist/execution/utils.d.ts +1 -1
  64. package/dist/execution/utils.js +9 -8
  65. package/dist/services/ConfigService.js +1 -0
  66. package/dist/types/internal.types.d.ts +18 -24
  67. package/dist/utils/errors.d.ts +7 -3
  68. package/dist/utils/errors.js +7 -1
  69. package/dist/utils/multicall.d.ts +1 -1
  70. package/dist/utils/parseError.d.ts +0 -1
  71. package/dist/utils/parseError.js +0 -5
  72. package/dist/version.d.ts +1 -1
  73. package/dist/version.js +1 -1
  74. package/package.json +14 -16
  75. package/dist/balances/index.d.ts +0 -11
  76. package/dist/cjs/balances/index.d.ts +0 -11
  77. package/dist/cjs/execution/balanceCheck.execute.d.ts +0 -3
  78. package/dist/cjs/execution/bridges/bridge.execute.d.ts +0 -7
  79. package/dist/cjs/execution/exchanges/swap.execute.js +0 -152
  80. package/dist/execution/balanceCheck.execute.d.ts +0 -3
  81. package/dist/execution/bridges/bridge.execute.d.ts +0 -7
  82. 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
- }