@lifi/sdk 3.0.0-alpha.4 → 3.0.0-alpha.7
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/dist/LiFi.d.ts +28 -21
- package/dist/LiFi.js +20 -13
- package/dist/allowance/setAllowance.d.ts +2 -1
- package/dist/allowance/setAllowance.js +23 -7
- package/dist/cjs/LiFi.d.ts +28 -21
- package/dist/cjs/LiFi.js +20 -13
- package/dist/cjs/allowance/setAllowance.d.ts +2 -1
- package/dist/cjs/allowance/setAllowance.js +23 -7
- package/dist/cjs/connectors.d.ts +1 -1
- package/dist/cjs/execution/BaseStepExecutor.d.ts +12 -0
- package/dist/cjs/execution/BaseStepExecutor.js +27 -0
- package/dist/cjs/execution/EVMStepExecutor.d.ts +13 -0
- package/dist/cjs/execution/EVMStepExecutor.js +292 -0
- package/dist/cjs/execution/RouteExecutionManager.d.ts +6 -7
- package/dist/cjs/execution/RouteExecutionManager.js +19 -19
- package/dist/cjs/execution/StatusManager.d.ts +8 -7
- package/dist/cjs/execution/StatusManager.js +1 -1
- package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
- package/dist/cjs/execution/StepExecutionManager.js +34 -29
- package/dist/cjs/execution/checkAllowance.d.ts +3 -2
- package/dist/cjs/execution/checkAllowance.js +1 -1
- package/dist/cjs/execution/checkBalance.d.ts +2 -2
- package/dist/cjs/execution/index.d.ts +1 -0
- package/dist/cjs/execution/index.js +1 -0
- package/dist/cjs/execution/multisig.d.ts +2 -2
- package/dist/cjs/execution/prepareRestart.d.ts +2 -3
- package/dist/cjs/execution/prepareRestart.js +2 -2
- package/dist/cjs/execution/stepComparison.d.ts +4 -3
- package/dist/cjs/execution/stepComparison.js +1 -1
- package/dist/cjs/execution/switchChain.d.ts +4 -3
- package/dist/cjs/execution/switchChain.js +1 -0
- package/dist/cjs/{types/internal.types.d.ts → execution/types.d.ts} +36 -96
- package/dist/cjs/execution/utils.d.ts +2 -2
- package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -3
- package/dist/cjs/helpers.d.ts +2 -2
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +6 -3
- package/dist/cjs/providers/BaseProvider.d.ts +7 -0
- package/dist/cjs/providers/BaseProvider.js +6 -0
- package/dist/cjs/providers/EVMProvider.d.ts +16 -0
- package/dist/cjs/providers/EVMProvider.js +28 -0
- package/dist/cjs/providers/SolanaProvider.d.ts +15 -0
- package/dist/cjs/providers/SolanaProvider.js +32 -0
- package/dist/cjs/providers/types.d.ts +0 -0
- package/dist/cjs/providers/types.js +1 -0
- package/dist/cjs/request.js +11 -5
- package/dist/cjs/services/ApiService.d.ts +4 -5
- package/dist/cjs/services/ApiService.js +2 -2
- package/dist/cjs/services/ConfigService.d.ts +6 -5
- package/dist/cjs/services/ConfigService.js +3 -3
- package/dist/cjs/typeguards.d.ts +2 -2
- package/dist/cjs/types/index.d.ts +1 -3
- package/dist/cjs/types/index.js +1 -5
- package/dist/cjs/types/internal.d.ts +60 -0
- package/dist/cjs/types/internal.js +2 -0
- package/dist/cjs/utils/parseError.d.ts +4 -4
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.d.ts +1 -1
- package/dist/execution/BaseStepExecutor.d.ts +12 -0
- package/dist/execution/BaseStepExecutor.js +23 -0
- package/dist/execution/EVMStepExecutor.d.ts +13 -0
- package/dist/execution/EVMStepExecutor.js +285 -0
- package/dist/execution/RouteExecutionManager.d.ts +6 -7
- package/dist/execution/RouteExecutionManager.js +19 -19
- package/dist/execution/StatusManager.d.ts +8 -7
- package/dist/execution/StatusManager.js +1 -1
- package/dist/execution/StepExecutionManager.d.ts +3 -3
- package/dist/execution/StepExecutionManager.js +34 -29
- package/dist/execution/checkAllowance.d.ts +3 -2
- package/dist/execution/checkAllowance.js +1 -1
- package/dist/execution/checkBalance.d.ts +2 -2
- package/dist/execution/index.d.ts +1 -0
- package/dist/execution/index.js +1 -0
- package/dist/execution/multisig.d.ts +2 -2
- package/dist/execution/prepareRestart.d.ts +2 -3
- package/dist/execution/prepareRestart.js +2 -2
- package/dist/execution/stepComparison.d.ts +4 -3
- package/dist/execution/stepComparison.js +1 -1
- package/dist/execution/switchChain.d.ts +4 -3
- package/dist/execution/switchChain.js +1 -0
- package/dist/{types/internal.types.d.ts → execution/types.d.ts} +36 -96
- package/dist/execution/utils.d.ts +2 -2
- package/dist/execution/waitForReceivingTransaction.d.ts +3 -3
- package/dist/helpers.d.ts +2 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/providers/BaseProvider.d.ts +7 -0
- package/dist/providers/BaseProvider.js +2 -0
- package/dist/providers/EVMProvider.d.ts +16 -0
- package/dist/providers/EVMProvider.js +24 -0
- package/dist/providers/SolanaProvider.d.ts +15 -0
- package/dist/providers/SolanaProvider.js +28 -0
- package/dist/providers/types.d.ts +0 -0
- package/dist/providers/types.js +1 -0
- package/dist/request.js +11 -5
- package/dist/services/ApiService.d.ts +4 -5
- package/dist/services/ApiService.js +2 -2
- package/dist/services/ConfigService.d.ts +6 -5
- package/dist/services/ConfigService.js +3 -3
- package/dist/typeguards.d.ts +2 -2
- package/dist/types/index.d.ts +1 -3
- package/dist/types/index.js +1 -3
- package/dist/types/internal.d.ts +60 -0
- package/dist/types/internal.js +1 -0
- package/dist/utils/parseError.d.ts +4 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +61 -62
- package/dist/cjs/execution/StepExecutor.d.ts +0 -15
- package/dist/cjs/execution/StepExecutor.js +0 -62
- package/dist/execution/StepExecutor.d.ts +0 -15
- package/dist/execution/StepExecutor.js +0 -58
- /package/dist/cjs/{types/internal.types.js → execution/types.js} +0 -0
- /package/dist/{types/internal.types.js → execution/types.js} +0 -0
|
@@ -281,10 +281,10 @@ const getAvailableConnections = async (connectionRequest) => {
|
|
|
281
281
|
url.searchParams.append('fromToken', fromToken);
|
|
282
282
|
}
|
|
283
283
|
if (toChain) {
|
|
284
|
-
url.searchParams.append('
|
|
284
|
+
url.searchParams.append('toChain', toChain);
|
|
285
285
|
}
|
|
286
286
|
if (toToken) {
|
|
287
|
-
url.searchParams.append('
|
|
287
|
+
url.searchParams.append('toToken', toToken);
|
|
288
288
|
}
|
|
289
289
|
const connectionRequestArrayParams = [
|
|
290
290
|
'allowBridges',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Chain
|
|
1
|
+
import type { Chain } from '@lifi/types';
|
|
2
|
+
import type { SDKConfig, SDKOptions } from '../types';
|
|
2
3
|
export default class ConfigService {
|
|
3
4
|
private static instance;
|
|
4
5
|
private readonly config;
|
|
@@ -12,12 +13,12 @@ export default class ConfigService {
|
|
|
12
13
|
* This call immediately returns the current config. It does not make sure that all chain data is already loaded
|
|
13
14
|
* Use this if you need access to basic information like API urls or settings
|
|
14
15
|
*/
|
|
15
|
-
getConfig: () =>
|
|
16
|
+
getConfig: () => SDKConfig;
|
|
16
17
|
/**
|
|
17
18
|
* This call waits for all setup promises to be done.
|
|
18
19
|
* Use this if you need access to chain data (RPCs or multicalls)
|
|
19
20
|
*/
|
|
20
|
-
getConfigAsync: () => Promise<
|
|
21
|
-
updateConfig: (configUpdate: Partial<
|
|
22
|
-
updateChains: (chains: Chain[]) =>
|
|
21
|
+
getConfigAsync: () => Promise<SDKConfig>;
|
|
22
|
+
updateConfig: (configUpdate: Partial<SDKOptions>) => SDKConfig;
|
|
23
|
+
updateChains: (chains: Chain[]) => SDKConfig;
|
|
23
24
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const types_1 = require("
|
|
3
|
+
const types_1 = require("@lifi/types");
|
|
4
4
|
const DefaultExecutionSettings = {
|
|
5
|
-
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
|
|
6
5
|
updateRouteHook: () => { },
|
|
7
6
|
switchChainHook: () => Promise.resolve(undefined),
|
|
8
7
|
acceptExchangeRateUpdateHook: () => Promise.resolve(undefined),
|
|
@@ -38,6 +37,7 @@ class ConfigService {
|
|
|
38
37
|
this.config.defaultExecutionSettings = Object.assign(this.config.defaultExecutionSettings, configUpdate.defaultExecutionSettings);
|
|
39
38
|
// OPTIONS
|
|
40
39
|
this.config.defaultRouteOptions = Object.assign(this.config.defaultRouteOptions, configUpdate.defaultRouteOptions);
|
|
40
|
+
this.config.apiKey = configUpdate.apiKey || this.config.apiKey;
|
|
41
41
|
this.config.userId = configUpdate.userId || this.config.userId;
|
|
42
42
|
this.config.integrator = configUpdate.integrator || this.config.integrator;
|
|
43
43
|
this.config.defaultRouteOptions.integrator =
|
|
@@ -46,7 +46,7 @@ class ConfigService {
|
|
|
46
46
|
this.config.integrator;
|
|
47
47
|
this.config.widgetVersion =
|
|
48
48
|
configUpdate.widgetVersion || this.config.widgetVersion;
|
|
49
|
-
this.config.multisig = configUpdate.
|
|
49
|
+
this.config.multisig = configUpdate.multisig || this.config.multisig;
|
|
50
50
|
return this.config;
|
|
51
51
|
};
|
|
52
52
|
this.updateChains = (chains) => {
|
package/dist/cjs/typeguards.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LiFiStep, RoutesRequest, StaticToken } from '@lifi/types';
|
|
2
2
|
export declare const isRoutesRequest: (routesRequest: RoutesRequest) => routesRequest is RoutesRequest;
|
|
3
|
-
export declare const isStep: (step:
|
|
3
|
+
export declare const isStep: (step: LiFiStep) => step is LiFiStep;
|
|
4
4
|
export declare const isToken: (token: StaticToken) => token is StaticToken;
|
package/dist/cjs/types/index.js
CHANGED
|
@@ -14,9 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.LiFiError = void 0;
|
|
18
|
-
__exportStar(require("@lifi/types"), exports);
|
|
19
|
-
var errors_1 = require("../utils/errors");
|
|
20
|
-
Object.defineProperty(exports, "LiFiError", { enumerable: true, get: function () { return errors_1.LiFiError; } });
|
|
21
17
|
__exportStar(require("./abi"), exports);
|
|
22
|
-
__exportStar(require("./internal
|
|
18
|
+
__exportStar(require("./internal"), exports);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ChainId, RouteOptions } from '@lifi/types';
|
|
2
|
+
import type { ExecutionSettings, InternalExecutionSettings, MultisigConfig } from '../execution/types';
|
|
3
|
+
import type { BaseProvider } from '../providers/BaseProvider';
|
|
4
|
+
export interface SDKConfig {
|
|
5
|
+
apiUrl: string;
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
rpcs: Record<ChainId, string[]>;
|
|
8
|
+
multicallAddresses: Record<ChainId, string | undefined>;
|
|
9
|
+
defaultExecutionSettings: InternalExecutionSettings;
|
|
10
|
+
defaultRouteOptions: RouteOptions;
|
|
11
|
+
disableVersionCheck?: boolean;
|
|
12
|
+
userId?: string;
|
|
13
|
+
integrator: string;
|
|
14
|
+
widgetVersion?: string;
|
|
15
|
+
multisig?: MultisigConfig;
|
|
16
|
+
providers?: BaseProvider[];
|
|
17
|
+
}
|
|
18
|
+
export interface SDKOptions extends Partial<Omit<SDKConfig, 'defaultExecutionSettings' | 'integrator'>> {
|
|
19
|
+
defaultExecutionSettings?: ExecutionSettings;
|
|
20
|
+
integrator: string;
|
|
21
|
+
}
|
|
22
|
+
export interface TenderlyResponse {
|
|
23
|
+
hash: string;
|
|
24
|
+
block_hash: string;
|
|
25
|
+
block_number: number;
|
|
26
|
+
from: string;
|
|
27
|
+
gas: number;
|
|
28
|
+
gas_price: number;
|
|
29
|
+
gas_fee_cap: number;
|
|
30
|
+
gas_tip_cap: number;
|
|
31
|
+
cumulative_gas_used: number;
|
|
32
|
+
gas_used: number;
|
|
33
|
+
effective_gas_price: number;
|
|
34
|
+
input: string;
|
|
35
|
+
nonce: number;
|
|
36
|
+
to: string;
|
|
37
|
+
index: number;
|
|
38
|
+
value: string;
|
|
39
|
+
access_list: unknown;
|
|
40
|
+
status: boolean;
|
|
41
|
+
addresses: string[];
|
|
42
|
+
contract_ids: string[];
|
|
43
|
+
network_id: string;
|
|
44
|
+
timestamp: string;
|
|
45
|
+
function_selector: string;
|
|
46
|
+
l1_block_number: number;
|
|
47
|
+
l1_timestamp: number;
|
|
48
|
+
deposit_tx: boolean;
|
|
49
|
+
system_tx: boolean;
|
|
50
|
+
mint: number;
|
|
51
|
+
sig: {
|
|
52
|
+
v: string;
|
|
53
|
+
r: string;
|
|
54
|
+
s: string;
|
|
55
|
+
};
|
|
56
|
+
error_message: string;
|
|
57
|
+
method: string;
|
|
58
|
+
decoded_input: unknown;
|
|
59
|
+
call_trace: unknown;
|
|
60
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LiFiStep, Process } from '@lifi/types';
|
|
2
2
|
import { LiFiError } from './errors';
|
|
3
3
|
/**
|
|
4
4
|
* Available MetaMask error codes:
|
|
@@ -31,7 +31,7 @@ import { LiFiError } from './errors';
|
|
|
31
31
|
* https://eips.ethereum.org/EIPS/eip-1474#error-codes
|
|
32
32
|
* https://eips.ethereum.org/EIPS/eip-1193#provider-errors
|
|
33
33
|
*/
|
|
34
|
-
export declare const getTransactionNotSentMessage: (step?:
|
|
35
|
-
export declare const getTransactionFailedMessage: (step:
|
|
36
|
-
export declare const parseError: (e: any, step?:
|
|
34
|
+
export declare const getTransactionNotSentMessage: (step?: LiFiStep, process?: Process) => Promise<string>;
|
|
35
|
+
export declare const getTransactionFailedMessage: (step: LiFiStep, txLink?: string) => Promise<string>;
|
|
36
|
+
export declare const parseError: (e: any, step?: LiFiStep, process?: Process) => Promise<LiFiError>;
|
|
37
37
|
export declare const parseBackendError: (e: any) => Promise<LiFiError>;
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const name = "@lifi/sdk";
|
|
2
|
-
export declare const version = "3.0.0-alpha.
|
|
2
|
+
export declare const version = "3.0.0-alpha.7";
|
package/dist/cjs/version.js
CHANGED
package/dist/connectors.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { ChainId } from '@lifi/types';
|
|
1
2
|
import type { PublicClient } from 'viem';
|
|
2
3
|
import type { Chain } from 'viem/chains';
|
|
3
|
-
import type { ChainId } from './types';
|
|
4
4
|
export declare const getChainById: (chainId: ChainId) => Chain | undefined;
|
|
5
5
|
export declare const getRpcUrl: (chainId: ChainId) => Promise<string>;
|
|
6
6
|
export declare const getRpcUrls: (chainId: ChainId) => Promise<string[]>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LiFiStep } from '@lifi/types';
|
|
2
|
+
import type { StatusManager } from './StatusManager';
|
|
3
|
+
import type { InteractionSettings, InternalExecutionSettings, StepExecutorOptions } from './types';
|
|
4
|
+
export declare abstract class BaseStepExecutor {
|
|
5
|
+
settings: InternalExecutionSettings;
|
|
6
|
+
statusManager: StatusManager;
|
|
7
|
+
allowUserInteraction: boolean;
|
|
8
|
+
allowExecution: boolean;
|
|
9
|
+
constructor(options: StepExecutorOptions);
|
|
10
|
+
setInteraction: (settings?: InteractionSettings) => void;
|
|
11
|
+
abstract executeStep(step: LiFiStep): Promise<LiFiStep>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
|
|
2
|
+
const defaultInteractionSettings = {
|
|
3
|
+
allowInteraction: true,
|
|
4
|
+
allowUpdates: true,
|
|
5
|
+
allowExecution: true,
|
|
6
|
+
};
|
|
7
|
+
export class BaseStepExecutor {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.allowUserInteraction = true;
|
|
10
|
+
this.allowExecution = true;
|
|
11
|
+
this.setInteraction = (settings) => {
|
|
12
|
+
const interactionSettings = {
|
|
13
|
+
...defaultInteractionSettings,
|
|
14
|
+
...settings,
|
|
15
|
+
};
|
|
16
|
+
this.allowUserInteraction = interactionSettings.allowInteraction;
|
|
17
|
+
this.statusManager.allowUpdates(interactionSettings.allowUpdates);
|
|
18
|
+
this.allowExecution = interactionSettings.allowExecution;
|
|
19
|
+
};
|
|
20
|
+
this.statusManager = options.statusManager;
|
|
21
|
+
this.settings = options.settings;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { LiFiStep } from '@lifi/types';
|
|
2
|
+
import type { WalletClient } from 'viem';
|
|
3
|
+
import { BaseStepExecutor } from './BaseStepExecutor';
|
|
4
|
+
import type { StepExecutorOptions } from './types';
|
|
5
|
+
export interface EVMStepExecutorOptions extends StepExecutorOptions {
|
|
6
|
+
walletClient: WalletClient;
|
|
7
|
+
}
|
|
8
|
+
export declare class EVMStepExecutor extends BaseStepExecutor {
|
|
9
|
+
walletClient: WalletClient;
|
|
10
|
+
constructor(options: EVMStepExecutorOptions);
|
|
11
|
+
checkChain: () => never;
|
|
12
|
+
executeStep: (step: LiFiStep) => Promise<LiFiStep>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { publicActions } from 'viem';
|
|
2
|
+
import ApiService from '../services/ApiService';
|
|
3
|
+
import ChainsService from '../services/ChainsService';
|
|
4
|
+
import ConfigService from '../services/ConfigService';
|
|
5
|
+
import { getMaxPriorityFeePerGas } from '../utils';
|
|
6
|
+
import { LiFiErrorCode, TransactionError, ValidationError, } from '../utils/errors';
|
|
7
|
+
import { getTransactionFailedMessage, parseError } from '../utils/parseError';
|
|
8
|
+
import { isZeroAddress } from '../utils/utils';
|
|
9
|
+
import { BaseStepExecutor } from './BaseStepExecutor';
|
|
10
|
+
import { checkAllowance } from './checkAllowance';
|
|
11
|
+
import { checkBalance } from './checkBalance';
|
|
12
|
+
import { updateMultisigRouteProcess } from './multisig';
|
|
13
|
+
import { stepComparison } from './stepComparison';
|
|
14
|
+
import { switchChain } from './switchChain';
|
|
15
|
+
import { getSubstatusMessage } from './utils';
|
|
16
|
+
import { waitForReceivingTransaction } from './waitForReceivingTransaction';
|
|
17
|
+
export class EVMStepExecutor extends BaseStepExecutor {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super(options);
|
|
20
|
+
// TODO: add checkChain method and update wallet client inside executors
|
|
21
|
+
// This can come in handy when we execute multiple routes simultaneously and
|
|
22
|
+
// should be sure that we are on the right chain when waiting for transactions.
|
|
23
|
+
this.checkChain = () => {
|
|
24
|
+
throw new Error('checkChain is not implemented.');
|
|
25
|
+
};
|
|
26
|
+
this.executeStep = async (step) => {
|
|
27
|
+
// Make sure that the chain is still correct
|
|
28
|
+
// Find if it's bridging and the step is waiting for a transaction on the receiving chain
|
|
29
|
+
const recievingChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
|
|
30
|
+
// If the step is waiting for a transaction on the receiving chain, we do not switch the chain
|
|
31
|
+
// All changes are already done from the source chain
|
|
32
|
+
// Return the step
|
|
33
|
+
if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION' ||
|
|
34
|
+
!recievingChainProcess) {
|
|
35
|
+
const updatedWalletClient = await switchChain(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
36
|
+
if (!updatedWalletClient) {
|
|
37
|
+
// Chain switch was not successful, stop execution here
|
|
38
|
+
return step;
|
|
39
|
+
}
|
|
40
|
+
this.walletClient = updatedWalletClient;
|
|
41
|
+
}
|
|
42
|
+
const client = this.walletClient.extend(publicActions);
|
|
43
|
+
const config = ConfigService.getInstance().getConfig();
|
|
44
|
+
const isMultisigWalletClient = !!config.multisig?.isMultisigWalletClient;
|
|
45
|
+
const multisigBatchTransactions = [];
|
|
46
|
+
const shouldBatchTransactions = config.multisig?.shouldBatchTransactions &&
|
|
47
|
+
!!config.multisig.sendBatchTransaction;
|
|
48
|
+
step.execution = this.statusManager.initExecutionObject(step);
|
|
49
|
+
const chainsService = ChainsService.getInstance();
|
|
50
|
+
const fromChain = await chainsService.getChainById(step.action.fromChainId);
|
|
51
|
+
const toChain = await chainsService.getChainById(step.action.toChainId);
|
|
52
|
+
const isBridgeExecution = fromChain.id !== toChain.id;
|
|
53
|
+
const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
|
|
54
|
+
// STEP 1: Check allowance
|
|
55
|
+
const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
|
|
56
|
+
// Check token approval only if fromToken is not the native token => no approval needed in that case
|
|
57
|
+
const checkForAllowance = !existingProcess?.txHash &&
|
|
58
|
+
!isZeroAddress(step.action.fromToken.address) &&
|
|
59
|
+
(shouldBatchTransactions || !isMultisigWalletClient);
|
|
60
|
+
if (checkForAllowance) {
|
|
61
|
+
const data = await checkAllowance(client, step, this.statusManager, this.settings, fromChain, this.allowUserInteraction, shouldBatchTransactions);
|
|
62
|
+
if (data) {
|
|
63
|
+
// allowance doesn't need value
|
|
64
|
+
const baseTransaction = {
|
|
65
|
+
to: step.action.fromToken.address,
|
|
66
|
+
data,
|
|
67
|
+
};
|
|
68
|
+
multisigBatchTransactions.push(baseTransaction);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// STEP 2: Get transaction
|
|
72
|
+
let process = this.statusManager.findOrCreateProcess(step, currentProcessType);
|
|
73
|
+
if (process.status !== 'DONE') {
|
|
74
|
+
const multisigProcess = step.execution.process.find((p) => !!p.multisigTxHash);
|
|
75
|
+
try {
|
|
76
|
+
if (isMultisigWalletClient && multisigProcess) {
|
|
77
|
+
const multisigTxHash = multisigProcess.multisigTxHash;
|
|
78
|
+
if (!multisigTxHash) {
|
|
79
|
+
throw new ValidationError('Multisig internal transaction hash is undefined.');
|
|
80
|
+
}
|
|
81
|
+
await updateMultisigRouteProcess(multisigTxHash, step, this.statusManager, process.type, fromChain);
|
|
82
|
+
}
|
|
83
|
+
let txHash;
|
|
84
|
+
if (process.txHash) {
|
|
85
|
+
// Make sure that the chain is still correct
|
|
86
|
+
const updatedWalletClient = await switchChain(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
87
|
+
if (!updatedWalletClient) {
|
|
88
|
+
// Chain switch was not successful, stop execution here
|
|
89
|
+
return step;
|
|
90
|
+
}
|
|
91
|
+
this.walletClient = updatedWalletClient;
|
|
92
|
+
// Load exiting transaction
|
|
93
|
+
txHash = process.txHash;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
process = this.statusManager.updateProcess(step, process.type, 'STARTED');
|
|
97
|
+
// Check balance
|
|
98
|
+
await checkBalance(client.account.address, step);
|
|
99
|
+
// Create new transaction
|
|
100
|
+
if (!step.transactionRequest) {
|
|
101
|
+
const updatedStep = await ApiService.getStepTransaction(step);
|
|
102
|
+
const comparedStep = await stepComparison(this.statusManager, step, updatedStep, this.settings, this.allowUserInteraction);
|
|
103
|
+
step = {
|
|
104
|
+
...comparedStep,
|
|
105
|
+
execution: step.execution,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!step.transactionRequest) {
|
|
109
|
+
throw new TransactionError(LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
|
|
110
|
+
}
|
|
111
|
+
// STEP 3: Send the transaction
|
|
112
|
+
// Make sure that the chain is still correct
|
|
113
|
+
const updatedWalletClient = await switchChain(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
114
|
+
if (!updatedWalletClient) {
|
|
115
|
+
// Chain switch was not successful, stop execution here
|
|
116
|
+
return step;
|
|
117
|
+
}
|
|
118
|
+
this.walletClient = updatedWalletClient;
|
|
119
|
+
process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
|
|
120
|
+
if (!this.allowUserInteraction) {
|
|
121
|
+
return step;
|
|
122
|
+
}
|
|
123
|
+
let transactionRequest = {
|
|
124
|
+
to: step.transactionRequest.to,
|
|
125
|
+
from: step.transactionRequest.from,
|
|
126
|
+
data: step.transactionRequest.data,
|
|
127
|
+
value: step.transactionRequest.value
|
|
128
|
+
? BigInt(step.transactionRequest.value)
|
|
129
|
+
: undefined,
|
|
130
|
+
gas: step.transactionRequest.gasLimit
|
|
131
|
+
? BigInt(step.transactionRequest.gasLimit)
|
|
132
|
+
: undefined,
|
|
133
|
+
// gasPrice: step.transactionRequest.gasPrice
|
|
134
|
+
// ? BigInt(step.transactionRequest.gasPrice as string)
|
|
135
|
+
// : undefined,
|
|
136
|
+
// maxFeePerGas: step.transactionRequest.maxFeePerGas
|
|
137
|
+
// ? BigInt(step.transactionRequest.maxFeePerGas as string)
|
|
138
|
+
// : undefined,
|
|
139
|
+
maxPriorityFeePerGas: this.walletClient.account?.type === 'local'
|
|
140
|
+
? await getMaxPriorityFeePerGas(client)
|
|
141
|
+
: step.transactionRequest.maxPriorityFeePerGas
|
|
142
|
+
? BigInt(step.transactionRequest.maxPriorityFeePerGas)
|
|
143
|
+
: undefined,
|
|
144
|
+
};
|
|
145
|
+
if (this.settings.updateTransactionRequestHook) {
|
|
146
|
+
const customizedTransactionRequest = await this.settings.updateTransactionRequestHook({
|
|
147
|
+
requestType: 'transaction',
|
|
148
|
+
...transactionRequest,
|
|
149
|
+
});
|
|
150
|
+
transactionRequest = {
|
|
151
|
+
...transactionRequest,
|
|
152
|
+
...customizedTransactionRequest,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (shouldBatchTransactions &&
|
|
156
|
+
config.multisig?.sendBatchTransaction) {
|
|
157
|
+
if (transactionRequest.to && transactionRequest.data) {
|
|
158
|
+
const populatedTransaction = {
|
|
159
|
+
value: transactionRequest.value,
|
|
160
|
+
to: transactionRequest.to,
|
|
161
|
+
data: transactionRequest.data,
|
|
162
|
+
};
|
|
163
|
+
multisigBatchTransactions.push(populatedTransaction);
|
|
164
|
+
txHash = await config.multisig?.sendBatchTransaction(multisigBatchTransactions);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
throw new TransactionError(LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
txHash = await this.walletClient.sendTransaction({
|
|
172
|
+
to: transactionRequest.to,
|
|
173
|
+
account: this.walletClient.account,
|
|
174
|
+
data: transactionRequest.data,
|
|
175
|
+
gas: transactionRequest.gas,
|
|
176
|
+
gasPrice: transactionRequest.gasPrice,
|
|
177
|
+
maxFeePerGas: transactionRequest.maxFeePerGas,
|
|
178
|
+
maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
|
|
179
|
+
chain: null,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// STEP 4: Wait for the transaction
|
|
183
|
+
if (isMultisigWalletClient) {
|
|
184
|
+
process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED', {
|
|
185
|
+
multisigTxHash: txHash,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
190
|
+
txHash: txHash,
|
|
191
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
let replacementReason;
|
|
196
|
+
const transactionReceipt = await client.waitForTransactionReceipt({
|
|
197
|
+
hash: txHash,
|
|
198
|
+
onReplaced: (response) => {
|
|
199
|
+
replacementReason = response.reason;
|
|
200
|
+
this.statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
201
|
+
txHash: response.transaction.hash,
|
|
202
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
if (replacementReason === 'cancelled') {
|
|
207
|
+
throw new TransactionError(LiFiErrorCode.TransactionCanceled, 'User canceled transaction.');
|
|
208
|
+
}
|
|
209
|
+
// if it's multisig wallet client and the process is in ACTION_REQUIRED
|
|
210
|
+
// then signatures are still needed
|
|
211
|
+
if (isMultisigWalletClient && process.status === 'ACTION_REQUIRED') {
|
|
212
|
+
await updateMultisigRouteProcess(transactionReceipt.transactionHash, step, this.statusManager, process.type, fromChain);
|
|
213
|
+
}
|
|
214
|
+
if (!isMultisigWalletClient) {
|
|
215
|
+
process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
216
|
+
txHash: transactionReceipt.transactionHash,
|
|
217
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (isBridgeExecution) {
|
|
221
|
+
process = this.statusManager.updateProcess(step, process.type, 'DONE');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch (e) {
|
|
225
|
+
const error = await parseError(e, step, process);
|
|
226
|
+
process = this.statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
227
|
+
error: {
|
|
228
|
+
message: error.message,
|
|
229
|
+
htmlMessage: error.htmlMessage,
|
|
230
|
+
code: error.code,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
this.statusManager.updateExecution(step, 'FAILED');
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// STEP 5: Wait for the receiving chain
|
|
238
|
+
const processTxHash = process.txHash;
|
|
239
|
+
if (isBridgeExecution) {
|
|
240
|
+
process = this.statusManager.findOrCreateProcess(step, 'RECEIVING_CHAIN', 'PENDING');
|
|
241
|
+
}
|
|
242
|
+
let statusResponse;
|
|
243
|
+
try {
|
|
244
|
+
if (!processTxHash) {
|
|
245
|
+
throw new Error('Transaction hash is undefined.');
|
|
246
|
+
}
|
|
247
|
+
statusResponse = (await waitForReceivingTransaction(processTxHash, this.statusManager, process.type, step));
|
|
248
|
+
const statusReceiving = statusResponse.receiving;
|
|
249
|
+
process = this.statusManager.updateProcess(step, process.type, 'DONE', {
|
|
250
|
+
substatus: statusResponse.substatus,
|
|
251
|
+
substatusMessage: statusResponse.substatusMessage ||
|
|
252
|
+
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
253
|
+
txHash: statusReceiving?.txHash,
|
|
254
|
+
txLink: `${toChain.metamask.blockExplorerUrls[0]}tx/${statusReceiving?.txHash}`,
|
|
255
|
+
});
|
|
256
|
+
this.statusManager.updateExecution(step, 'DONE', {
|
|
257
|
+
fromAmount: statusResponse.sending.amount,
|
|
258
|
+
toAmount: statusReceiving?.amount,
|
|
259
|
+
toToken: statusReceiving?.token,
|
|
260
|
+
gasAmount: statusResponse.sending.gasAmount,
|
|
261
|
+
gasAmountUSD: statusResponse.sending.gasAmountUSD,
|
|
262
|
+
gasPrice: statusResponse.sending.gasPrice,
|
|
263
|
+
gasToken: statusResponse.sending.gasToken,
|
|
264
|
+
gasUsed: statusResponse.sending.gasUsed,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
catch (e) {
|
|
268
|
+
const htmlMessage = await getTransactionFailedMessage(step, process.txLink);
|
|
269
|
+
process = this.statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
270
|
+
error: {
|
|
271
|
+
code: LiFiErrorCode.TransactionFailed,
|
|
272
|
+
message: 'Failed while waiting for receiving chain.',
|
|
273
|
+
htmlMessage,
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
this.statusManager.updateExecution(step, 'FAILED');
|
|
277
|
+
console.warn(e);
|
|
278
|
+
throw e;
|
|
279
|
+
}
|
|
280
|
+
// DONE
|
|
281
|
+
return step;
|
|
282
|
+
};
|
|
283
|
+
this.walletClient = options.walletClient;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -1,30 +1,29 @@
|
|
|
1
1
|
import type { Route } from '@lifi/types';
|
|
2
|
-
import type { WalletClient } from 'viem';
|
|
3
2
|
import ConfigService from '../services/ConfigService';
|
|
4
|
-
import type {
|
|
3
|
+
import type { SDKOptions } from '../types';
|
|
4
|
+
import type { ExecutionSettings } from './types';
|
|
5
5
|
export declare class RouteExecutionManager {
|
|
6
6
|
private executionDictionary;
|
|
7
7
|
private executionPromiseDictionary;
|
|
8
8
|
protected configService: ConfigService;
|
|
9
|
-
|
|
9
|
+
private providers;
|
|
10
|
+
constructor(options: SDKOptions);
|
|
10
11
|
/**
|
|
11
12
|
* Execute a route.
|
|
12
|
-
* @param walletClient - The walletClient required to send the transactions.
|
|
13
13
|
* @param route - The route that should be executed. Cannot be an active route.
|
|
14
14
|
* @param settings - An object containing settings and callbacks.
|
|
15
15
|
* @returns The executed route.
|
|
16
16
|
* @throws {LiFiError} Throws a LiFiError if the execution fails.
|
|
17
17
|
*/
|
|
18
|
-
executeRoute: (
|
|
18
|
+
executeRoute: (route: Route, settings?: ExecutionSettings) => Promise<Route>;
|
|
19
19
|
/**
|
|
20
20
|
* Resume the execution of a route that has been stopped or had an error while executing.
|
|
21
|
-
* @param walletClient - The walletClient required to send the transactions.
|
|
22
21
|
* @param route - The route that is to be executed. Cannot be an active route.
|
|
23
22
|
* @param settings - An object containing settings and callbacks.
|
|
24
23
|
* @returns The executed route.
|
|
25
24
|
* @throws {LiFiError} Throws a LiFiError if the execution fails.
|
|
26
25
|
*/
|
|
27
|
-
resumeRoute: (
|
|
26
|
+
resumeRoute: (route: Route, settings?: ExecutionSettings) => Promise<Route>;
|
|
28
27
|
private executeSteps;
|
|
29
28
|
/**
|
|
30
29
|
* Updates route execution to background or foreground state.
|