@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
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.updatedStepMeetsSlippageConditions = exports.getSubstatusMessage = exports.getProcessMessage = exports.waitForReceivingTransaction = void 0;
15
+ exports.checkStepSlippageThreshold = exports.getSubstatusMessage = exports.getProcessMessage = exports.waitForReceivingTransaction = void 0;
16
16
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
17
17
  const ApiService_1 = __importDefault(require("../services/ApiService"));
18
18
  const errors_1 = require("../utils/errors");
@@ -76,20 +76,20 @@ const processMessages = {
76
76
  DONE: 'Chain switched successfully.',
77
77
  },
78
78
  SWAP: {
79
- STARTED: 'Preparing swap.',
79
+ STARTED: 'Preparing swap transaction.',
80
80
  ACTION_REQUIRED: 'Please sign the transaction.',
81
- PENDING: 'Swapping.',
81
+ PENDING: 'Waiting for swap transaction.',
82
82
  DONE: 'Swap completed.',
83
83
  },
84
84
  CROSS_CHAIN: {
85
- STARTED: 'Preparing transaction.',
85
+ STARTED: 'Preparing bridge transaction.',
86
86
  ACTION_REQUIRED: 'Please sign the transaction.',
87
- PENDING: 'Waiting for transaction.',
88
- DONE: 'Transaction approved.',
87
+ PENDING: 'Waiting for bridge transaction.',
88
+ DONE: 'Bridge transaction confirmed.',
89
89
  },
90
90
  RECEIVING_CHAIN: {
91
- PENDING: 'Waiting for receiving chain.',
92
- DONE: 'Funds received.',
91
+ PENDING: 'Waiting for destination chain.',
92
+ DONE: 'Bridge completed.',
93
93
  },
94
94
  TRANSACTION: {},
95
95
  };
@@ -124,7 +124,8 @@ function getSubstatusMessage(status, substatus) {
124
124
  return message;
125
125
  }
126
126
  exports.getSubstatusMessage = getSubstatusMessage;
127
- function updatedStepMeetsSlippageConditions(oldStep, newStep) {
127
+ // Used to check if changed exchange rate is in the range of slippage threshold
128
+ function checkStepSlippageThreshold(oldStep, newStep) {
128
129
  const setSlippage = new bignumber_js_1.default(oldStep.action.slippage);
129
130
  const oldEstimatedToAmount = new bignumber_js_1.default(oldStep.estimate.toAmountMin);
130
131
  const newEstimatedToAmount = new bignumber_js_1.default(newStep.estimate.toAmountMin);
@@ -133,4 +134,4 @@ function updatedStepMeetsSlippageConditions(oldStep, newStep) {
133
134
  return (newEstimatedToAmount.gte(oldEstimatedToAmount) &&
134
135
  actualSlippage.lte(setSlippage));
135
136
  }
136
- exports.updatedStepMeetsSlippageConditions = updatedStepMeetsSlippageConditions;
137
+ exports.checkStepSlippageThreshold = checkStepSlippageThreshold;
@@ -15,6 +15,7 @@ const DefaultExecutionSettings = {
15
15
  updateCallback: () => { },
16
16
  switchChainHook: () => Promise.resolve(undefined),
17
17
  acceptSlippageUpdateHook: () => Promise.resolve(undefined),
18
+ acceptExchangeRateUpdateHook: () => Promise.resolve(undefined),
18
19
  infiniteApproval: false,
19
20
  executeInBackground: false,
20
21
  };
@@ -8,7 +8,7 @@ export interface TokenWithAmounts extends Token {
8
8
  amount?: BigNumber;
9
9
  amountRendered?: string;
10
10
  }
11
- export declare type ParsedReceipt = {
11
+ export type ParsedReceipt = {
12
12
  fromAmount?: string;
13
13
  toAmount: string;
14
14
  gasUsed: string;
@@ -16,20 +16,14 @@ export declare type ParsedReceipt = {
16
16
  gasFee: string;
17
17
  toTokenAddress?: string;
18
18
  };
19
- interface ExecutionParams {
19
+ export interface ExecutionParams {
20
20
  signer: Signer;
21
21
  step: Step;
22
22
  statusManager: StatusManager;
23
23
  settings: InternalExecutionSettings;
24
24
  }
25
- export interface ExecuteSwapParams extends ExecutionParams {
26
- step: Step;
27
- }
28
- export interface ExecuteCrossParams extends ExecutionParams {
29
- step: Step;
30
- }
31
- export declare type CallbackFunction = (updatedRoute: Route) => void;
32
- export declare type Config = {
25
+ export type CallbackFunction = (updatedRoute: Route) => void;
26
+ export type Config = {
33
27
  apiUrl: string;
34
28
  rpcs: Record<ChainId, string[]>;
35
29
  multicallAddresses: Record<ChainId, string | undefined>;
@@ -37,7 +31,7 @@ export declare type Config = {
37
31
  defaultRouteOptions: RouteOptions;
38
32
  disableVersionCheck?: boolean;
39
33
  };
40
- export declare type ConfigUpdate = {
34
+ export type ConfigUpdate = {
41
35
  apiUrl?: string;
42
36
  rpcs?: Record<number, string[]>;
43
37
  multicallAddresses?: Record<number, string | undefined>;
@@ -45,7 +39,7 @@ export declare type ConfigUpdate = {
45
39
  defaultRouteOptions?: RouteOptions;
46
40
  disableVersionCheck?: boolean;
47
41
  };
48
- export declare type SwitchChainHook = (requiredChainId: number) => Promise<Signer | undefined>;
42
+ export type SwitchChainHook = (requiredChainId: number) => Promise<Signer | undefined>;
49
43
  export interface AcceptSlippageUpdateHookParams {
50
44
  toToken: Token;
51
45
  oldToAmount: string;
@@ -53,33 +47,34 @@ export interface AcceptSlippageUpdateHookParams {
53
47
  oldSlippage: number;
54
48
  newSlippage: number;
55
49
  }
56
- export declare type AcceptSlippageUpdateHook = (params: AcceptSlippageUpdateHookParams) => Promise<boolean | undefined>;
50
+ export type AcceptSlippageUpdateHook = (params: AcceptSlippageUpdateHookParams) => Promise<boolean | undefined>;
51
+ export interface ExchangeRateUpdateParams {
52
+ toToken: Token;
53
+ oldToAmount: string;
54
+ newToAmount: string;
55
+ }
56
+ export type AcceptExchangeRateUpdateHook = (params: ExchangeRateUpdateParams) => Promise<boolean | undefined>;
57
57
  export interface ExecutionData {
58
58
  route: Route;
59
59
  executors: StepExecutor[];
60
60
  settings: InternalExecutionSettings;
61
61
  }
62
- export interface ExecutionSettings {
63
- updateCallback?: CallbackFunction;
64
- switchChainHook?: SwitchChainHook;
65
- acceptSlippageUpdateHook?: AcceptSlippageUpdateHook;
66
- infiniteApproval?: boolean;
67
- executeInBackground?: boolean;
68
- }
69
- export interface InternalExecutionSettings extends ExecutionSettings {
62
+ export type ExecutionSettings = Partial<InternalExecutionSettings>;
63
+ export interface InternalExecutionSettings {
70
64
  updateCallback: CallbackFunction;
71
65
  switchChainHook: SwitchChainHook;
72
66
  acceptSlippageUpdateHook: AcceptSlippageUpdateHook;
67
+ acceptExchangeRateUpdateHook: AcceptExchangeRateUpdateHook;
73
68
  infiniteApproval: boolean;
74
69
  executeInBackground: boolean;
75
70
  }
76
- export declare type EnforcedObjectProperties<T> = T & {
71
+ export type EnforcedObjectProperties<T> = T & {
77
72
  [P in keyof T]-?: T[P];
78
73
  };
79
74
  export interface ActiveRouteDictionary {
80
75
  [k: string]: ExecutionData;
81
76
  }
82
- export declare type RevokeTokenData = {
77
+ export type RevokeTokenData = {
83
78
  token: Token;
84
79
  approvalAddress: string;
85
80
  };
@@ -88,4 +83,3 @@ export interface InteractionSettings {
88
83
  allowUpdates?: boolean;
89
84
  stopExecution?: boolean;
90
85
  }
91
- export {};
@@ -19,9 +19,10 @@ export declare enum LifiErrorCode {
19
19
  ChainSwitchError = 1007,
20
20
  TransactionUnprepared = 1008,
21
21
  GasLimitError = 1009,
22
- SlippageNotMet = 1010,
22
+ TransactionCanceled = 1010,
23
23
  SlippageError = 1011,
24
- TransactionRejected = 1012
24
+ TransactionRejected = 1012,
25
+ BalanceError = 1013
25
26
  }
26
27
  export declare enum MetaMaskRPCErrorCode {
27
28
  invalidInput = -32000,
@@ -43,7 +44,7 @@ export declare enum MetaMaskProviderErrorCode {
43
44
  disconnected = 4900,
44
45
  chainDisconnected = 4901
45
46
  }
46
- export declare type ErrorCode = LifiErrorCode | MetaMaskRPCErrorCode | MetaMaskProviderErrorCode;
47
+ export type ErrorCode = LifiErrorCode | MetaMaskRPCErrorCode | MetaMaskProviderErrorCode;
47
48
  export declare class LifiError extends Error {
48
49
  code: ErrorCode;
49
50
  htmlMessage?: string;
@@ -67,6 +68,9 @@ export declare class TransactionError extends LifiError {
67
68
  export declare class SlippageError extends LifiError {
68
69
  constructor(message: string, htmlMessage?: string, stack?: string);
69
70
  }
71
+ export declare class BalanceError extends LifiError {
72
+ constructor(message: string, htmlMessage?: string, stack?: string);
73
+ }
70
74
  export declare class NotFoundError extends LifiError {
71
75
  constructor(message: string, htmlMessage?: string, stack?: string);
72
76
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UnknownError = exports.NotFoundError = exports.SlippageError = exports.TransactionError = exports.ValidationError = exports.ServerError = exports.ProviderError = exports.RPCError = exports.LifiError = exports.MetaMaskProviderErrorCode = exports.MetaMaskRPCErrorCode = exports.LifiErrorCode = void 0;
3
+ exports.UnknownError = exports.NotFoundError = exports.BalanceError = exports.SlippageError = exports.TransactionError = exports.ValidationError = exports.ServerError = exports.ProviderError = exports.RPCError = exports.LifiError = exports.MetaMaskProviderErrorCode = exports.MetaMaskRPCErrorCode = exports.LifiErrorCode = void 0;
4
4
  var ErrorType;
5
5
  (function (ErrorType) {
6
6
  ErrorType["RPCError"] = "RPCError";
@@ -24,9 +24,10 @@ var LifiErrorCode;
24
24
  LifiErrorCode[LifiErrorCode["ChainSwitchError"] = 1007] = "ChainSwitchError";
25
25
  LifiErrorCode[LifiErrorCode["TransactionUnprepared"] = 1008] = "TransactionUnprepared";
26
26
  LifiErrorCode[LifiErrorCode["GasLimitError"] = 1009] = "GasLimitError";
27
- LifiErrorCode[LifiErrorCode["SlippageNotMet"] = 1010] = "SlippageNotMet";
27
+ LifiErrorCode[LifiErrorCode["TransactionCanceled"] = 1010] = "TransactionCanceled";
28
28
  LifiErrorCode[LifiErrorCode["SlippageError"] = 1011] = "SlippageError";
29
29
  LifiErrorCode[LifiErrorCode["TransactionRejected"] = 1012] = "TransactionRejected";
30
+ LifiErrorCode[LifiErrorCode["BalanceError"] = 1013] = "BalanceError";
30
31
  })(LifiErrorCode = exports.LifiErrorCode || (exports.LifiErrorCode = {}));
31
32
  var MetaMaskRPCErrorCode;
32
33
  (function (MetaMaskRPCErrorCode) {
@@ -102,6 +103,12 @@ class SlippageError extends LifiError {
102
103
  }
103
104
  }
104
105
  exports.SlippageError = SlippageError;
106
+ class BalanceError extends LifiError {
107
+ constructor(message, htmlMessage, stack) {
108
+ super(ErrorType.ValidationError, LifiErrorCode.BalanceError, message, htmlMessage, stack);
109
+ }
110
+ }
111
+ exports.BalanceError = BalanceError;
105
112
  class NotFoundError extends LifiError {
106
113
  constructor(message, htmlMessage, stack) {
107
114
  super(ErrorType.NotFoundError, LifiErrorCode.NotFound, message, htmlMessage, stack);
@@ -1,5 +1,5 @@
1
1
  import { Fragment, JsonFragment } from '@ethersproject/abi';
2
- export declare type MultiCallData = {
2
+ export type MultiCallData = {
3
3
  address: string;
4
4
  name: string;
5
5
  params?: any[];
@@ -35,4 +35,3 @@ export declare const getTransactionNotSentMessage: (step?: Step, process?: Proce
35
35
  export declare const getTransactionFailedMessage: (step: Step, txLink?: string) => string;
36
36
  export declare const parseError: (e: any, step?: Step, process?: Process) => Promise<LifiError>;
37
37
  export declare const parseBackendError: (e: any) => LifiError;
38
- export declare const getSlippageNotMetMessage: (step: Step) => string;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getSlippageNotMetMessage = exports.parseBackendError = exports.parseError = exports.getTransactionFailedMessage = exports.getTransactionNotSentMessage = void 0;
15
+ exports.parseBackendError = exports.parseError = exports.getTransactionFailedMessage = exports.getTransactionNotSentMessage = void 0;
16
16
  const types_1 = require("@lifi/types");
17
17
  const eth_rpc_errors_1 = require("eth-rpc-errors");
18
18
  const ChainsService_1 = __importDefault(require("../services/ChainsService"));
@@ -135,9 +135,3 @@ const parseBackendError = (e) => {
135
135
  return new errors_1.ServerError('Something went wrong.', undefined, e.stack);
136
136
  };
137
137
  exports.parseBackendError = parseBackendError;
138
- const getSlippageNotMetMessage = (step) => {
139
- const { slippage } = step.action;
140
- return `Transaction was not sent, your funds are still in your wallet.
141
- The updated quote for the current transaction does not meet your set slippage of ${slippage * 100}%.`;
142
- };
143
- exports.getSlippageNotMetMessage = getSlippageNotMetMessage;
@@ -1,2 +1,2 @@
1
1
  export declare const name = "@lifi/sdk";
2
- export declare const version = "1.6.4";
2
+ export declare const version = "1.7.1";
@@ -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.6.4';
5
+ exports.version = '1.7.1';
@@ -1,7 +1,7 @@
1
1
  import { Execution } from '@lifi/types';
2
- import { ExecuteSwapParams } from '../../types';
3
- export declare class SwapExecutionManager {
2
+ import { ExecutionParams } from '../types';
3
+ export declare class ExecutionManager {
4
4
  allowUserInteraction: boolean;
5
5
  allowInteraction: (value: boolean) => void;
6
- execute: ({ signer, step, statusManager, settings, }: ExecuteSwapParams) => Promise<Execution>;
6
+ execute: ({ signer, step, statusManager, settings, }: ExecutionParams) => Promise<Execution>;
7
7
  }
@@ -7,18 +7,18 @@ 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 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 { getSubstatusMessage, waitForReceivingTransaction } from '../utils';
21
- export class BridgeExecutionManager {
10
+ import { checkAllowance } from '../allowance';
11
+ import { checkBalance } from '../balance';
12
+ import ApiService from '../services/ApiService';
13
+ import ChainsService from '../services/ChainsService';
14
+ import { LifiErrorCode, TransactionError } from '../utils/errors';
15
+ import { getProvider } from '../utils/getProvider';
16
+ import { getTransactionFailedMessage, parseError } from '../utils/parseError';
17
+ import { isZeroAddress, personalizeStep } from '../utils/utils';
18
+ import { stepComparison } from './stepComparison';
19
+ import { switchChain } from './switchChain';
20
+ import { getSubstatusMessage, waitForReceivingTransaction } from './utils';
21
+ export class ExecutionManager {
22
22
  constructor() {
23
23
  this.allowUserInteraction = true;
24
24
  this.allowInteraction = (value) => {
@@ -30,19 +30,21 @@ export class BridgeExecutionManager {
30
30
  const chainsService = ChainsService.getInstance();
31
31
  const fromChain = yield chainsService.getChainById(step.action.fromChainId);
32
32
  const toChain = yield chainsService.getChainById(step.action.toChainId);
33
+ const isBridgeExecution = fromChain.id !== toChain.id;
34
+ const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
33
35
  // STEP 1: Check allowance
34
- const oldCrossProcess = step.execution.process.find((p) => p.type === 'CROSS_CHAIN');
36
+ const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
35
37
  // Check token approval only if fromToken is not the native token => no approval needed in that case
36
- if (!(oldCrossProcess === null || oldCrossProcess === void 0 ? void 0 : oldCrossProcess.txHash) &&
38
+ if (!(existingProcess === null || existingProcess === void 0 ? void 0 : existingProcess.txHash) &&
37
39
  !isZeroAddress(step.action.fromToken.address)) {
38
40
  yield checkAllowance(signer, step, statusManager, settings, fromChain, this.allowUserInteraction);
39
41
  }
40
42
  // STEP 2: Get transaction
41
- let crossChainProcess = statusManager.findOrCreateProcess(step, 'CROSS_CHAIN');
42
- if (crossChainProcess.status !== 'DONE') {
43
+ let process = statusManager.findOrCreateProcess(step, currentProcessType);
44
+ if (process.status !== 'DONE') {
43
45
  try {
44
46
  let transaction;
45
- if (crossChainProcess.txHash) {
47
+ if (process.txHash) {
46
48
  // Make sure that the chain is still correct
47
49
  const updatedSigner = yield switchChain(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
48
50
  if (!updatedSigner) {
@@ -51,17 +53,18 @@ export class BridgeExecutionManager {
51
53
  }
52
54
  signer = updatedSigner;
53
55
  // Load exiting transaction
54
- transaction = yield getProvider(signer).getTransaction(crossChainProcess.txHash);
56
+ transaction = yield getProvider(signer).getTransaction(process.txHash);
55
57
  }
56
58
  else {
57
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'STARTED');
59
+ process = statusManager.updateProcess(step, process.type, 'STARTED');
58
60
  // Check balance
59
- yield balanceCheck(signer, step);
61
+ yield checkBalance(signer, step);
60
62
  // Create new transaction
61
63
  if (!step.transactionRequest) {
62
64
  const personalizedStep = yield personalizeStep(signer, step);
63
65
  const updatedStep = yield ApiService.getStepTransaction(personalizedStep);
64
- step = Object.assign(Object.assign({}, (yield stepComparison(statusManager, personalizedStep, updatedStep, settings.acceptSlippageUpdateHook, this.allowUserInteraction))), { execution: step.execution });
66
+ const comparedStep = yield stepComparison(statusManager, personalizedStep, updatedStep, settings, this.allowUserInteraction);
67
+ step = Object.assign(Object.assign({}, comparedStep), { execution: step.execution });
65
68
  }
66
69
  const { transactionRequest } = step;
67
70
  if (!transactionRequest) {
@@ -75,14 +78,14 @@ export class BridgeExecutionManager {
75
78
  return step.execution;
76
79
  }
77
80
  signer = updatedSigner;
78
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'ACTION_REQUIRED');
81
+ process = statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
79
82
  if (!this.allowUserInteraction) {
80
83
  return step.execution;
81
84
  }
82
85
  // Submit the transaction
83
86
  transaction = yield signer.sendTransaction(transactionRequest);
84
87
  // STEP 4: Wait for the transaction
85
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', {
88
+ process = statusManager.updateProcess(step, process.type, 'PENDING', {
86
89
  txHash: transaction.hash,
87
90
  txLink: fromChain.metamask.blockExplorerUrls[0] +
88
91
  'tx/' +
@@ -90,11 +93,17 @@ export class BridgeExecutionManager {
90
93
  });
91
94
  }
92
95
  yield transaction.wait();
93
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'DONE');
96
+ process = statusManager.updateProcess(step, process.type, 'PENDING', {
97
+ txHash: transaction.hash,
98
+ txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + transaction.hash,
99
+ });
100
+ if (isBridgeExecution) {
101
+ process = statusManager.updateProcess(step, process.type, 'DONE');
102
+ }
94
103
  }
95
104
  catch (e) {
96
105
  if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
97
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'DONE', {
106
+ process = statusManager.updateProcess(step, process.type, 'DONE', {
98
107
  txHash: e.replacement.hash,
99
108
  txLink: fromChain.metamask.blockExplorerUrls[0] +
100
109
  'tx/' +
@@ -102,8 +111,8 @@ export class BridgeExecutionManager {
102
111
  });
103
112
  }
104
113
  else {
105
- const error = yield parseError(e, step, crossChainProcess);
106
- crossChainProcess = statusManager.updateProcess(step, crossChainProcess.type, 'FAILED', {
114
+ const error = yield parseError(e, step, process);
115
+ process = statusManager.updateProcess(step, process.type, 'FAILED', {
107
116
  error: {
108
117
  message: error.message,
109
118
  htmlMessage: error.htmlMessage,
@@ -116,14 +125,17 @@ export class BridgeExecutionManager {
116
125
  }
117
126
  }
118
127
  // STEP 5: Wait for the receiving chain
119
- let receivingChainProcess = statusManager.findOrCreateProcess(step, 'RECEIVING_CHAIN', 'PENDING');
128
+ const processTxHash = process.txHash;
129
+ if (isBridgeExecution) {
130
+ process = statusManager.findOrCreateProcess(step, 'RECEIVING_CHAIN', 'PENDING');
131
+ }
120
132
  let statusResponse;
121
133
  try {
122
- if (!crossChainProcess.txHash) {
134
+ if (!processTxHash) {
123
135
  throw new Error('Transaction hash is undefined.');
124
136
  }
125
- statusResponse = yield waitForReceivingTransaction(crossChainProcess.txHash, statusManager, receivingChainProcess.type, step);
126
- receivingChainProcess = statusManager.updateProcess(step, receivingChainProcess.type, 'DONE', {
137
+ statusResponse = yield waitForReceivingTransaction(processTxHash, statusManager, process.type, step);
138
+ process = statusManager.updateProcess(step, process.type, 'DONE', {
127
139
  substatus: statusResponse.substatus,
128
140
  substatusMessage: statusResponse.substatusMessage ||
129
141
  getSubstatusMessage(statusResponse.status, statusResponse.substatus),
@@ -136,16 +148,19 @@ export class BridgeExecutionManager {
136
148
  fromAmount: statusResponse.sending.amount,
137
149
  toAmount: (_c = statusResponse.receiving) === null || _c === void 0 ? void 0 : _c.amount,
138
150
  toToken: (_d = statusResponse.receiving) === null || _d === void 0 ? void 0 : _d.token,
139
- gasUsed: statusResponse.sending.gasUsed,
151
+ gasAmount: statusResponse.sending.gasAmount,
152
+ gasAmountUSD: statusResponse.sending.gasAmountUSD,
140
153
  gasPrice: statusResponse.sending.gasPrice,
154
+ gasToken: statusResponse.sending.gasToken,
155
+ gasUsed: statusResponse.sending.gasUsed,
141
156
  });
142
157
  }
143
158
  catch (e) {
144
- receivingChainProcess = statusManager.updateProcess(step, receivingChainProcess.type, 'FAILED', {
159
+ process = statusManager.updateProcess(step, process.type, 'FAILED', {
145
160
  error: {
146
161
  code: LifiErrorCode.TransactionFailed,
147
162
  message: 'Failed while waiting for receiving chain.',
148
- htmlMessage: getTransactionFailedMessage(step, crossChainProcess.txLink),
163
+ htmlMessage: getTransactionFailedMessage(step, process.txLink),
149
164
  },
150
165
  });
151
166
  statusManager.updateExecution(step, 'FAILED');
@@ -3,11 +3,14 @@ interface Receipt {
3
3
  fromAmount?: string;
4
4
  toAmount?: string;
5
5
  toToken?: Token;
6
- gasUsed?: string;
7
6
  gasPrice?: string;
7
+ gasUsed?: string;
8
+ gasToken?: Token;
9
+ gasAmount?: string;
10
+ gasAmountUSD?: string;
8
11
  }
9
- declare type InternalUpdateRouteCallback = (route: Route) => void;
10
- declare type OptionalParameters = Partial<Pick<Process, 'doneAt' | 'failedAt' | 'txHash' | 'txLink' | 'error' | 'substatus' | 'substatusMessage'>>;
12
+ type InternalUpdateRouteCallback = (route: Route) => void;
13
+ type OptionalParameters = Partial<Pick<Process, 'doneAt' | 'failedAt' | 'txHash' | 'txLink' | 'error' | 'substatus' | 'substatusMessage'>>;
11
14
  /**
12
15
  * Manages status updates of a route and provides various functions for tracking processes
13
16
  * @param {Route} route The route this StatusManger belongs to.
@@ -157,9 +157,7 @@ export class StatusManager {
157
157
  }
158
158
  step.execution.status = status;
159
159
  if (receipt) {
160
- step.execution.fromAmount = receipt.fromAmount;
161
- step.execution.toAmount = receipt.toAmount;
162
- step.execution.toToken = receipt.toToken;
160
+ step.execution = Object.assign(Object.assign({}, step.execution), receipt);
163
161
  }
164
162
  this.updateStepInRoute(step);
165
163
  return step;
@@ -1,17 +1,15 @@
1
1
  import { Signer } from 'ethers';
2
2
  import { InteractionSettings, InternalExecutionSettings, Step } from '../types';
3
+ import { ExecutionManager } from './ExecutionManager';
3
4
  import { StatusManager } from './StatusManager';
4
5
  export declare class StepExecutor {
5
- settings: InternalExecutionSettings;
6
+ executionManager: ExecutionManager;
6
7
  statusManager: StatusManager;
7
- private swapExecutionManager;
8
- private bridgeExecutionManager;
8
+ settings: InternalExecutionSettings;
9
9
  allowUserInteraction: boolean;
10
10
  executionStopped: boolean;
11
11
  constructor(statusManager: StatusManager, settings: InternalExecutionSettings);
12
12
  setInteraction: (settings?: InteractionSettings) => void;
13
13
  checkChain: () => never;
14
14
  executeStep: (signer: Signer, step: Step) => Promise<Step>;
15
- private executeSwap;
16
- private executeCross;
17
15
  }
@@ -7,8 +7,7 @@ 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 { BridgeExecutionManager } from './bridges/bridge.execute';
11
- import { SwapExecutionManager } from './exchanges/swap.execute';
10
+ import { ExecutionManager } from './ExecutionManager';
12
11
  import { switchChain } from './switchChain';
13
12
  // Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
14
13
  const defaultInteractionSettings = {
@@ -18,15 +17,12 @@ const defaultInteractionSettings = {
18
17
  };
19
18
  export class StepExecutor {
20
19
  constructor(statusManager, settings) {
21
- this.swapExecutionManager = new SwapExecutionManager();
22
- this.bridgeExecutionManager = new BridgeExecutionManager();
23
20
  this.allowUserInteraction = true;
24
21
  this.executionStopped = false;
25
22
  this.setInteraction = (settings) => {
26
23
  const interactionSettings = Object.assign(Object.assign({}, defaultInteractionSettings), settings);
27
24
  this.allowUserInteraction = interactionSettings.allowInteraction;
28
- this.swapExecutionManager.allowInteraction(interactionSettings.allowInteraction);
29
- this.bridgeExecutionManager.allowInteraction(interactionSettings.allowInteraction);
25
+ this.executionManager.allowInteraction(interactionSettings.allowInteraction);
30
26
  this.statusManager.allowUpdates(interactionSettings.allowUpdates);
31
27
  this.executionStopped = interactionSettings.stopExecution;
32
28
  };
@@ -44,37 +40,16 @@ export class StepExecutor {
44
40
  return step;
45
41
  }
46
42
  signer = updatedSigner;
47
- switch (step.type) {
48
- case 'lifi':
49
- case 'cross':
50
- yield this.executeCross(signer, step);
51
- break;
52
- case 'swap':
53
- yield this.executeSwap(signer, step);
54
- break;
55
- default:
56
- throw new Error('Unsupported step type.');
57
- }
58
- return step;
59
- });
60
- this.executeSwap = (signer, step) => {
61
- const swapParams = {
62
- signer,
63
- step,
64
- settings: this.settings,
65
- statusManager: this.statusManager,
66
- };
67
- return this.swapExecutionManager.execute(swapParams);
68
- };
69
- this.executeCross = (signer, step) => {
70
- const crossParams = {
43
+ const parameters = {
71
44
  signer,
72
45
  step,
73
46
  settings: this.settings,
74
47
  statusManager: this.statusManager,
75
48
  };
76
- return this.bridgeExecutionManager.execute(crossParams);
77
- };
49
+ yield this.executionManager.execute(parameters);
50
+ return step;
51
+ });
52
+ this.executionManager = new ExecutionManager();
78
53
  this.statusManager = statusManager;
79
54
  this.settings = settings;
80
55
  }
@@ -1,9 +1,9 @@
1
1
  import { StatusManager } from '.';
2
- import { AcceptSlippageUpdateHook, Step } from '../types';
2
+ import { InternalExecutionSettings, Step } from '../types';
3
3
  /**
4
- * This method checks whether the new and updated Step meets the required slippage conditions.
4
+ * This method checks whether the new and updated Step meets the required exchange rate conditions.
5
5
  * If yes it returns the updated Step.
6
- * If no and if user interaction is allowed it triggers the acceptSlippageUpdateHook. If no user interaction is allowed it aborts.
6
+ * If no and if user interaction is allowed it triggers the acceptExchangeRateUpdateHook. If no user interaction is allowed it aborts.
7
7
  *
8
8
  * @param statusManager
9
9
  * @param oldStep
@@ -11,4 +11,4 @@ import { AcceptSlippageUpdateHook, Step } from '../types';
11
11
  * @param acceptSlippageUpdateHook
12
12
  * @param allowUserInteraction
13
13
  */
14
- export declare const stepComparison: (statusManager: StatusManager, oldStep: Step, newStep: Step, acceptSlippageUpdateHook: AcceptSlippageUpdateHook, allowUserInteraction: boolean) => Promise<Step>;
14
+ export declare const stepComparison: (statusManager: StatusManager, oldStep: Step, newStep: Step, settings: InternalExecutionSettings, allowUserInteraction: boolean) => Promise<Step>;