@lifi/sdk 2.2.2 → 3.0.0-alpha.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.
- package/dist/LiFi.d.ts +69 -62
- package/dist/LiFi.js +71 -60
- package/dist/allowance/getAllowance.d.ts +6 -0
- package/dist/allowance/getAllowance.js +78 -0
- package/dist/allowance/index.d.ts +3 -2
- package/dist/allowance/index.js +3 -2
- package/dist/allowance/setAllowance.d.ts +5 -0
- package/dist/allowance/setAllowance.js +50 -0
- package/dist/allowance/types.d.ts +27 -0
- package/dist/allowance/types.js +1 -0
- package/dist/balance/getBalance.d.ts +2 -0
- package/dist/balance/getBalance.js +93 -0
- package/dist/balance/getTokenBalance.d.ts +2 -2
- package/dist/balance/getTokenBalance.js +4 -4
- package/dist/balance/index.d.ts +1 -12
- package/dist/balance/index.js +1 -9
- package/dist/cjs/LiFi.d.ts +69 -62
- package/dist/cjs/LiFi.js +69 -58
- package/dist/cjs/allowance/getAllowance.d.ts +6 -0
- package/dist/cjs/allowance/getAllowance.js +85 -0
- package/dist/cjs/allowance/index.d.ts +3 -2
- package/dist/cjs/allowance/index.js +3 -2
- package/dist/cjs/allowance/setAllowance.d.ts +5 -0
- package/dist/cjs/allowance/setAllowance.js +56 -0
- package/dist/cjs/allowance/types.d.ts +27 -0
- package/dist/cjs/allowance/types.js +2 -0
- package/dist/cjs/balance/getBalance.d.ts +2 -0
- package/dist/cjs/balance/getBalance.js +97 -0
- package/dist/cjs/balance/getTokenBalance.d.ts +2 -2
- package/dist/cjs/balance/getTokenBalance.js +6 -9
- package/dist/cjs/balance/index.d.ts +1 -12
- package/dist/cjs/balance/index.js +1 -9
- package/dist/cjs/connectors.d.ts +7 -5
- package/dist/cjs/connectors.js +45 -37
- package/dist/cjs/constants.d.ts +6 -0
- package/dist/cjs/constants.js +9 -0
- package/dist/cjs/execution/RouteExecutionManager.d.ts +24 -30
- package/dist/cjs/execution/RouteExecutionManager.js +27 -45
- package/dist/cjs/execution/StatusManager.d.ts +22 -23
- package/dist/cjs/execution/StatusManager.js +21 -22
- package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
- package/dist/cjs/execution/StepExecutionManager.js +118 -125
- package/dist/cjs/execution/StepExecutor.d.ts +4 -4
- package/dist/cjs/execution/StepExecutor.js +6 -6
- package/dist/cjs/execution/checkAllowance.d.ts +4 -0
- package/dist/cjs/execution/checkAllowance.js +77 -0
- package/dist/cjs/execution/checkBalance.d.ts +2 -0
- package/dist/cjs/execution/checkBalance.js +42 -0
- package/dist/cjs/execution/multisig.d.ts +4 -3
- package/dist/cjs/execution/multisig.js +9 -11
- package/dist/cjs/execution/prepareRestart.d.ts +3 -0
- package/dist/cjs/execution/prepareRestart.js +65 -0
- package/dist/cjs/execution/stepComparison.d.ts +3 -3
- package/dist/cjs/execution/stepComparison.js +2 -2
- package/dist/cjs/execution/switchChain.d.ts +13 -8
- package/dist/cjs/execution/switchChain.js +16 -11
- package/dist/cjs/execution/utils.d.ts +9 -3
- package/dist/cjs/execution/utils.js +19 -66
- package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -0
- package/dist/cjs/execution/waitForReceivingTransaction.js +58 -0
- package/dist/cjs/helpers.d.ts +7 -17
- package/dist/cjs/helpers.js +6 -38
- package/dist/cjs/request.js +1 -1
- package/dist/cjs/services/ApiService.d.ts +2 -2
- package/dist/cjs/services/ApiService.js +2 -0
- package/dist/cjs/services/ChainsService.d.ts +1 -1
- package/dist/cjs/services/ConfigService.d.ts +1 -1
- package/dist/cjs/services/ConfigService.js +1 -2
- package/dist/cjs/typeguards.d.ts +1 -1
- package/dist/cjs/types/abi.d.ts +5 -0
- package/dist/cjs/types/abi.js +45 -0
- package/dist/cjs/types/index.d.ts +2 -2
- package/dist/cjs/types/index.js +3 -3
- package/dist/cjs/types/internal.types.d.ts +24 -30
- package/dist/cjs/utils/errors.d.ts +15 -14
- package/dist/cjs/utils/errors.js +38 -37
- package/dist/cjs/utils/getMaxPriorityFeePerGas.d.ts +2 -0
- package/dist/cjs/utils/getMaxPriorityFeePerGas.js +22 -0
- package/dist/cjs/utils/index.d.ts +5 -0
- package/dist/cjs/utils/index.js +21 -0
- package/dist/cjs/utils/median.d.ts +1 -0
- package/dist/cjs/utils/median.js +15 -0
- package/dist/cjs/utils/parseError.d.ts +4 -4
- package/dist/cjs/utils/parseError.js +41 -38
- package/dist/cjs/utils/utils.d.ts +1 -16
- package/dist/cjs/utils/utils.js +9 -78
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.d.ts +7 -5
- package/dist/connectors.js +19 -35
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +6 -0
- package/dist/execution/RouteExecutionManager.d.ts +24 -30
- package/dist/execution/RouteExecutionManager.js +27 -45
- package/dist/execution/StatusManager.d.ts +22 -23
- package/dist/execution/StatusManager.js +22 -23
- package/dist/execution/StepExecutionManager.d.ts +3 -3
- package/dist/execution/StepExecutionManager.js +116 -123
- package/dist/execution/StepExecutor.d.ts +4 -4
- package/dist/execution/StepExecutor.js +6 -6
- package/dist/execution/checkAllowance.d.ts +4 -0
- package/dist/execution/checkAllowance.js +73 -0
- package/dist/execution/checkBalance.d.ts +2 -0
- package/dist/execution/checkBalance.js +38 -0
- package/dist/execution/multisig.d.ts +4 -3
- package/dist/execution/multisig.js +10 -12
- package/dist/execution/prepareRestart.d.ts +3 -0
- package/dist/execution/prepareRestart.js +61 -0
- package/dist/execution/stepComparison.d.ts +3 -3
- package/dist/execution/stepComparison.js +3 -3
- package/dist/execution/switchChain.d.ts +13 -8
- package/dist/execution/switchChain.js +17 -12
- package/dist/execution/utils.d.ts +9 -3
- package/dist/execution/utils.js +18 -61
- package/dist/execution/waitForReceivingTransaction.d.ts +3 -0
- package/dist/execution/waitForReceivingTransaction.js +51 -0
- package/dist/helpers.d.ts +7 -17
- package/dist/helpers.js +5 -34
- package/dist/request.js +2 -2
- package/dist/services/ApiService.d.ts +2 -2
- package/dist/services/ApiService.js +2 -0
- package/dist/services/ChainsService.d.ts +1 -1
- package/dist/services/ConfigService.d.ts +1 -1
- package/dist/services/ConfigService.js +2 -3
- package/dist/typeguards.d.ts +1 -1
- package/dist/types/abi.d.ts +5 -0
- package/dist/types/abi.js +42 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +2 -2
- package/dist/types/internal.types.d.ts +24 -30
- package/dist/utils/errors.d.ts +15 -14
- package/dist/utils/errors.js +36 -35
- package/dist/utils/getMaxPriorityFeePerGas.d.ts +2 -0
- package/dist/utils/getMaxPriorityFeePerGas.js +18 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/median.d.ts +1 -0
- package/dist/utils/median.js +11 -0
- package/dist/utils/parseError.d.ts +4 -4
- package/dist/utils/parseError.js +42 -39
- package/dist/utils/utils.d.ts +1 -16
- package/dist/utils/utils.js +7 -69
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +28 -27
- package/dist/allowance/checkAllowance.d.ts +0 -4
- package/dist/allowance/checkAllowance.js +0 -96
- package/dist/allowance/tokenApproval.d.ts +0 -22
- package/dist/allowance/tokenApproval.js +0 -59
- package/dist/allowance/utils.d.ts +0 -15
- package/dist/allowance/utils.js +0 -77
- package/dist/balance/checkBalance.d.ts +0 -3
- package/dist/balance/checkBalance.js +0 -38
- package/dist/balance/utils.d.ts +0 -6
- package/dist/balance/utils.js +0 -143
- package/dist/cjs/allowance/checkAllowance.d.ts +0 -4
- package/dist/cjs/allowance/checkAllowance.js +0 -103
- package/dist/cjs/allowance/tokenApproval.d.ts +0 -22
- package/dist/cjs/allowance/tokenApproval.js +0 -69
- package/dist/cjs/allowance/utils.d.ts +0 -15
- package/dist/cjs/allowance/utils.js +0 -87
- package/dist/cjs/balance/checkBalance.d.ts +0 -3
- package/dist/cjs/balance/checkBalance.js +0 -45
- package/dist/cjs/balance/utils.d.ts +0 -6
- package/dist/cjs/balance/utils.js +0 -150
- package/dist/cjs/types/ERC20.d.ts +0 -22
- package/dist/cjs/types/ERC20.js +0 -53
- package/dist/cjs/utils/getProvider.d.ts +0 -3
- package/dist/cjs/utils/getProvider.js +0 -11
- package/dist/cjs/utils/multicall.d.ts +0 -10
- package/dist/cjs/utils/multicall.js +0 -68
- package/dist/cjs/utils/multicallAbi.json +0 -313
- package/dist/cjs/utils/preRestart.d.ts +0 -3
- package/dist/cjs/utils/preRestart.js +0 -56
- package/dist/types/ERC20.d.ts +0 -22
- package/dist/types/ERC20.js +0 -50
- package/dist/utils/getProvider.d.ts +0 -3
- package/dist/utils/getProvider.js +0 -7
- package/dist/utils/multicall.d.ts +0 -10
- package/dist/utils/multicall.js +0 -61
- package/dist/utils/multicallAbi.json +0 -313
- package/dist/utils/preRestart.d.ts +0 -3
- package/dist/utils/preRestart.js +0 -52
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { publicActions } from 'viem';
|
|
2
|
+
import { LiFiErrorCode } from '../utils/errors';
|
|
3
|
+
export const prepareRestart = async (route, walletClient) => {
|
|
4
|
+
for (let index = 0; index < route.steps.length; index++) {
|
|
5
|
+
const step = route.steps[index];
|
|
6
|
+
const stepHasFailed = step.execution?.status === 'FAILED';
|
|
7
|
+
if (stepHasFailed) {
|
|
8
|
+
await handleErrorType(walletClient, step);
|
|
9
|
+
deleteFailedProcesses(step);
|
|
10
|
+
deleteTransactionData(step);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const handleErrorType = async (walletClient, step) => {
|
|
15
|
+
const client = walletClient.extend(publicActions);
|
|
16
|
+
const isGasLimitError = step.execution?.process.some((p) => p.error?.code === LiFiErrorCode.GasLimitError);
|
|
17
|
+
const isGasPriceError = step.execution?.process.some((p) => p.error?.code === LiFiErrorCode.TransactionUnderpriced);
|
|
18
|
+
const { transactionRequest } = step;
|
|
19
|
+
// if (isGasLimitError) {
|
|
20
|
+
// if (transactionRequest) {
|
|
21
|
+
// let gasLimit = transactionRequest.gasLimit
|
|
22
|
+
// try {
|
|
23
|
+
// gasLimit = await client.estimateGas(transactionRequest)
|
|
24
|
+
// } catch (error) {}
|
|
25
|
+
// if (gasLimit) {
|
|
26
|
+
// transactionRequest.gasLimit = BigNumber.from(
|
|
27
|
+
// `${(BigInt(gasLimit.toString()) * 125n) / 100n}`
|
|
28
|
+
// )
|
|
29
|
+
// }
|
|
30
|
+
// }
|
|
31
|
+
// step.estimate.gasCosts?.forEach(
|
|
32
|
+
// (gasCost) =>
|
|
33
|
+
// (gasCost.limit = `${Math.round(Number(gasCost.limit) * 1.25)}`)
|
|
34
|
+
// )
|
|
35
|
+
// }
|
|
36
|
+
// if (isGasPriceError) {
|
|
37
|
+
// if (transactionRequest) {
|
|
38
|
+
// let gasPrice = transactionRequest.gasPrice
|
|
39
|
+
// try {
|
|
40
|
+
// gasPrice = await client.getGasPrice()
|
|
41
|
+
// } catch (error) {}
|
|
42
|
+
// if (gasPrice) {
|
|
43
|
+
// transactionRequest.gasPrice = BigNumber.from(
|
|
44
|
+
// `${(BigInt(gasPrice.toString()) * 125n) / 100n}`
|
|
45
|
+
// )
|
|
46
|
+
// }
|
|
47
|
+
// }
|
|
48
|
+
// step.estimate.gasCosts?.forEach(
|
|
49
|
+
// (gasCost) =>
|
|
50
|
+
// (gasCost.price = `${Math.round(Number(gasCost.price) * 1.25)}`)
|
|
51
|
+
// )
|
|
52
|
+
// }
|
|
53
|
+
};
|
|
54
|
+
const deleteFailedProcesses = (step) => {
|
|
55
|
+
if (step.execution) {
|
|
56
|
+
step.execution.process = step.execution.process.filter((process) => process.status === 'DONE');
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const deleteTransactionData = (step) => {
|
|
60
|
+
step.transactionRequest = undefined;
|
|
61
|
+
};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { StatusManager } from '.';
|
|
2
|
-
import { InternalExecutionSettings, LifiStep } from '../types';
|
|
1
|
+
import type { StatusManager } from '.';
|
|
2
|
+
import type { InternalExecutionSettings, LifiStep } from '../types';
|
|
3
3
|
/**
|
|
4
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
6
|
* If no and if user interaction is allowed it triggers the acceptExchangeRateUpdateHook. If no user interaction is allowed it aborts.
|
|
7
|
-
*
|
|
8
7
|
* @param statusManager
|
|
9
8
|
* @param oldStep
|
|
10
9
|
* @param newStep
|
|
11
10
|
* @param settings
|
|
12
11
|
* @param allowUserInteraction
|
|
12
|
+
* @returns Return LifiStep
|
|
13
13
|
*/
|
|
14
14
|
export declare const stepComparison: (statusManager: StatusManager, oldStep: LifiStep, newStep: LifiStep, settings: InternalExecutionSettings, allowUserInteraction: boolean) => Promise<LifiStep>;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LiFiErrorCode, TransactionError } from '../utils/errors';
|
|
2
2
|
import { checkStepSlippageThreshold } from './utils';
|
|
3
3
|
/**
|
|
4
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
6
|
* If no and if user interaction is allowed it triggers the acceptExchangeRateUpdateHook. If no user interaction is allowed it aborts.
|
|
7
|
-
*
|
|
8
7
|
* @param statusManager
|
|
9
8
|
* @param oldStep
|
|
10
9
|
* @param newStep
|
|
11
10
|
* @param settings
|
|
12
11
|
* @param allowUserInteraction
|
|
12
|
+
* @returns Return LifiStep
|
|
13
13
|
*/
|
|
14
14
|
export const stepComparison = async (statusManager, oldStep, newStep, settings, allowUserInteraction) => {
|
|
15
15
|
// Check if changed exchange rate is in the range of slippage threshold
|
|
@@ -26,7 +26,7 @@ export const stepComparison = async (statusManager, oldStep, newStep, settings,
|
|
|
26
26
|
}
|
|
27
27
|
if (!allowStepUpdate) {
|
|
28
28
|
// The user declined the new exchange rate, so we are not going to proceed
|
|
29
|
-
throw new TransactionError(
|
|
29
|
+
throw new TransactionError(LiFiErrorCode.ExchangeRateUpdateCanceled, 'Exchange rate has changed!', `Transaction was not sent, your funds are still in your wallet.
|
|
30
30
|
The exchange rate has changed and the previous estimation can not be fulfilled due to value loss.`);
|
|
31
31
|
}
|
|
32
32
|
return statusManager.updateStepInRoute(newStep);
|
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import { LifiStep } from '@lifi/types';
|
|
2
|
-
import {
|
|
3
|
-
import { SwitchChainHook } from '../types';
|
|
4
|
-
import { StatusManager } from './StatusManager';
|
|
1
|
+
import type { LifiStep } from '@lifi/types';
|
|
2
|
+
import type { WalletClient } from 'viem';
|
|
3
|
+
import type { SwitchChainHook } from '../types';
|
|
4
|
+
import type { StatusManager } from './StatusManager';
|
|
5
5
|
/**
|
|
6
|
-
* This method checks whether the
|
|
7
|
-
* If yes it returns the
|
|
6
|
+
* This method checks whether the wallet client is configured for the correct chain.
|
|
7
|
+
* If yes it returns the wallet clien.
|
|
8
8
|
* If no and if user interaction is allowed it triggers the switchChainHook. If no user interaction is allowed it aborts.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* Account Type: local -
|
|
11
|
+
* We need to create and return a new WalletClient from the switchChainHook in order to continue execution on a new chain.
|
|
12
|
+
*
|
|
13
|
+
* Account Type: json-rpc -
|
|
14
|
+
* We can switch chain and return existing WalletClient from the switchChainHook in order to continue execution on a new chain.
|
|
15
|
+
* @param walletClient
|
|
11
16
|
* @param statusManager
|
|
12
17
|
* @param step
|
|
13
18
|
* @param switchChainHook
|
|
14
19
|
* @param allowUserInteraction
|
|
15
20
|
*/
|
|
16
|
-
export declare const switchChain: (
|
|
21
|
+
export declare const switchChain: (walletClient: WalletClient, statusManager: StatusManager, step: LifiStep, switchChainHook: SwitchChainHook, allowUserInteraction: boolean) => Promise<WalletClient | undefined>;
|
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LiFiErrorCode, ProviderError } from '../utils/errors';
|
|
2
2
|
/**
|
|
3
|
-
* This method checks whether the
|
|
4
|
-
* If yes it returns the
|
|
3
|
+
* This method checks whether the wallet client is configured for the correct chain.
|
|
4
|
+
* If yes it returns the wallet clien.
|
|
5
5
|
* If no and if user interaction is allowed it triggers the switchChainHook. If no user interaction is allowed it aborts.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* Account Type: local -
|
|
8
|
+
* We need to create and return a new WalletClient from the switchChainHook in order to continue execution on a new chain.
|
|
9
|
+
*
|
|
10
|
+
* Account Type: json-rpc -
|
|
11
|
+
* We can switch chain and return existing WalletClient from the switchChainHook in order to continue execution on a new chain.
|
|
12
|
+
* @param walletClient
|
|
8
13
|
* @param statusManager
|
|
9
14
|
* @param step
|
|
10
15
|
* @param switchChainHook
|
|
11
16
|
* @param allowUserInteraction
|
|
12
17
|
*/
|
|
13
|
-
export const switchChain = async (
|
|
18
|
+
export const switchChain = async (walletClient, statusManager, step, switchChainHook, allowUserInteraction) => {
|
|
14
19
|
// if we are already on the correct chain we can proceed directly
|
|
15
|
-
if ((await
|
|
16
|
-
return
|
|
20
|
+
if ((await walletClient.getChainId()) === step.action.fromChainId) {
|
|
21
|
+
return walletClient;
|
|
17
22
|
}
|
|
18
23
|
// -> set status message
|
|
19
24
|
step.execution = statusManager.initExecutionObject(step);
|
|
@@ -23,20 +28,20 @@ export const switchChain = async (signer, statusManager, step, switchChainHook,
|
|
|
23
28
|
return;
|
|
24
29
|
}
|
|
25
30
|
try {
|
|
26
|
-
const
|
|
27
|
-
const updatedChainId = await
|
|
31
|
+
const updatedWalletClient = await switchChainHook(step.action.fromChainId);
|
|
32
|
+
const updatedChainId = await updatedWalletClient?.getChainId();
|
|
28
33
|
if (updatedChainId !== step.action.fromChainId) {
|
|
29
|
-
throw new ProviderError(
|
|
34
|
+
throw new ProviderError(LiFiErrorCode.ChainSwitchError, 'Chain switch required.');
|
|
30
35
|
}
|
|
31
36
|
switchProcess = statusManager.updateProcess(step, switchProcess.type, 'DONE');
|
|
32
37
|
statusManager.updateExecution(step, 'PENDING');
|
|
33
|
-
return
|
|
38
|
+
return updatedWalletClient;
|
|
34
39
|
}
|
|
35
40
|
catch (error) {
|
|
36
41
|
statusManager.updateProcess(step, switchProcess.type, 'FAILED', {
|
|
37
42
|
error: {
|
|
38
43
|
message: error.message,
|
|
39
|
-
code:
|
|
44
|
+
code: LiFiErrorCode.ChainSwitchError,
|
|
40
45
|
},
|
|
41
46
|
});
|
|
42
47
|
statusManager.updateExecution(step, 'FAILED');
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import { LifiStep, ProcessType, Status, StatusMessage,
|
|
2
|
-
import { StatusManager } from '..';
|
|
3
|
-
export declare function waitForReceivingTransaction(txHash: string, statusManager: StatusManager, processType: ProcessType, step: LifiStep): Promise<StatusResponse>;
|
|
1
|
+
import type { LifiStep, ProcessType, Status, StatusMessage, Substatus } from '@lifi/types';
|
|
4
2
|
export declare function getProcessMessage(type: ProcessType, status: Status): string | undefined;
|
|
5
3
|
export declare function getSubstatusMessage(status: StatusMessage, substatus?: Substatus): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Used to check if changed exchange rate is in the range of slippage threshold.
|
|
6
|
+
* We use a slippage value as a threshold to trigger the rate change hook.
|
|
7
|
+
* This can result in almost doubled slippage for the user and need to be revisited.
|
|
8
|
+
* @param oldStep
|
|
9
|
+
* @param newStep
|
|
10
|
+
* @returns Boolean
|
|
11
|
+
*/
|
|
6
12
|
export declare function checkStepSlippageThreshold(oldStep: LifiStep, newStep: LifiStep): boolean;
|
package/dist/execution/utils.js
CHANGED
|
@@ -1,54 +1,3 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
import ApiService from '../services/ApiService';
|
|
3
|
-
import { ServerError } from '../utils/errors';
|
|
4
|
-
import { repeatUntilDone } from '../utils/utils';
|
|
5
|
-
const TRANSACTION_HASH_OBSERVERS = {};
|
|
6
|
-
export async function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
7
|
-
const getStatus = () => new Promise(async (resolve, reject) => {
|
|
8
|
-
let statusResponse;
|
|
9
|
-
try {
|
|
10
|
-
statusResponse = await ApiService.getStatus({
|
|
11
|
-
bridge: step.tool,
|
|
12
|
-
fromChain: step.action.fromChainId,
|
|
13
|
-
toChain: step.action.toChainId,
|
|
14
|
-
txHash,
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
catch (e) {
|
|
18
|
-
console.debug('Fetching status from backend failed.', e);
|
|
19
|
-
return resolve(undefined);
|
|
20
|
-
}
|
|
21
|
-
switch (statusResponse.status) {
|
|
22
|
-
case 'DONE':
|
|
23
|
-
return resolve(statusResponse);
|
|
24
|
-
case 'PENDING':
|
|
25
|
-
statusManager?.updateProcess(step, processType, 'PENDING', {
|
|
26
|
-
substatus: statusResponse.substatus,
|
|
27
|
-
substatusMessage: statusResponse.substatusMessage ||
|
|
28
|
-
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
29
|
-
txLink: statusResponse.bridgeExplorerLink,
|
|
30
|
-
});
|
|
31
|
-
return resolve(undefined);
|
|
32
|
-
case 'NOT_FOUND':
|
|
33
|
-
return resolve(undefined);
|
|
34
|
-
case 'FAILED':
|
|
35
|
-
default:
|
|
36
|
-
return reject();
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
let status;
|
|
40
|
-
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
41
|
-
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
TRANSACTION_HASH_OBSERVERS[txHash] = repeatUntilDone(getStatus, 5000);
|
|
45
|
-
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
46
|
-
}
|
|
47
|
-
if (!status.receiving) {
|
|
48
|
-
throw new ServerError("Status doesn't contain receiving information.");
|
|
49
|
-
}
|
|
50
|
-
return status;
|
|
51
|
-
}
|
|
52
1
|
const processMessages = {
|
|
53
2
|
TOKEN_ALLOWANCE: {
|
|
54
3
|
STARTED: 'Setting token allowance.',
|
|
@@ -106,17 +55,25 @@ export function getSubstatusMessage(status, substatus) {
|
|
|
106
55
|
const message = substatusMessages[status][substatus];
|
|
107
56
|
return message;
|
|
108
57
|
}
|
|
109
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Used to check if changed exchange rate is in the range of slippage threshold.
|
|
60
|
+
* We use a slippage value as a threshold to trigger the rate change hook.
|
|
61
|
+
* This can result in almost doubled slippage for the user and need to be revisited.
|
|
62
|
+
* @param oldStep
|
|
63
|
+
* @param newStep
|
|
64
|
+
* @returns Boolean
|
|
65
|
+
*/
|
|
110
66
|
export function checkStepSlippageThreshold(oldStep, newStep) {
|
|
111
|
-
const setSlippage =
|
|
112
|
-
const oldEstimatedToAmount =
|
|
113
|
-
const newEstimatedToAmount =
|
|
114
|
-
const amountDifference = oldEstimatedToAmount
|
|
67
|
+
const setSlippage = oldStep.action.slippage;
|
|
68
|
+
const oldEstimatedToAmount = BigInt(oldStep.estimate.toAmountMin);
|
|
69
|
+
const newEstimatedToAmount = BigInt(newStep.estimate.toAmountMin);
|
|
70
|
+
const amountDifference = oldEstimatedToAmount - newEstimatedToAmount;
|
|
115
71
|
// oldEstimatedToAmount can be 0 when we use conract calls
|
|
116
|
-
let actualSlippage =
|
|
117
|
-
if (oldEstimatedToAmount
|
|
118
|
-
actualSlippage =
|
|
72
|
+
let actualSlippage = 0;
|
|
73
|
+
if (oldEstimatedToAmount > 0) {
|
|
74
|
+
actualSlippage =
|
|
75
|
+
Number((amountDifference * 1000000000n) / oldEstimatedToAmount) /
|
|
76
|
+
1000000000;
|
|
119
77
|
}
|
|
120
|
-
return
|
|
121
|
-
actualSlippage.lte(setSlippage));
|
|
78
|
+
return actualSlippage <= setSlippage;
|
|
122
79
|
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { LifiStep, ProcessType, StatusResponse } from '@lifi/types';
|
|
2
|
+
import type { StatusManager } from '..';
|
|
3
|
+
export declare function waitForReceivingTransaction(txHash: string, statusManager: StatusManager, processType: ProcessType, step: LifiStep): Promise<StatusResponse>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import ApiService from '../services/ApiService';
|
|
2
|
+
import { ServerError } from '../utils/errors';
|
|
3
|
+
import { repeatUntilDone } from '../utils/utils';
|
|
4
|
+
import { getSubstatusMessage } from './utils';
|
|
5
|
+
const TRANSACTION_HASH_OBSERVERS = {};
|
|
6
|
+
export async function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
7
|
+
const getStatus = () => new Promise(async (resolve, reject) => {
|
|
8
|
+
let statusResponse;
|
|
9
|
+
try {
|
|
10
|
+
statusResponse = await ApiService.getStatus({
|
|
11
|
+
bridge: step.tool,
|
|
12
|
+
fromChain: step.action.fromChainId,
|
|
13
|
+
toChain: step.action.toChainId,
|
|
14
|
+
txHash,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.debug('Fetching status from backend failed.', e);
|
|
19
|
+
return resolve(undefined);
|
|
20
|
+
}
|
|
21
|
+
switch (statusResponse.status) {
|
|
22
|
+
case 'DONE':
|
|
23
|
+
return resolve(statusResponse);
|
|
24
|
+
case 'PENDING':
|
|
25
|
+
statusManager?.updateProcess(step, processType, 'PENDING', {
|
|
26
|
+
substatus: statusResponse.substatus,
|
|
27
|
+
substatusMessage: statusResponse.substatusMessage ||
|
|
28
|
+
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
29
|
+
txLink: statusResponse.bridgeExplorerLink,
|
|
30
|
+
});
|
|
31
|
+
return resolve(undefined);
|
|
32
|
+
case 'NOT_FOUND':
|
|
33
|
+
return resolve(undefined);
|
|
34
|
+
case 'FAILED':
|
|
35
|
+
default:
|
|
36
|
+
return reject();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
let status;
|
|
40
|
+
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
41
|
+
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
TRANSACTION_HASH_OBSERVERS[txHash] = repeatUntilDone(getStatus, 5000);
|
|
45
|
+
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
46
|
+
}
|
|
47
|
+
if (!status.receiving) {
|
|
48
|
+
throw new ServerError("Status doesn't contain receiving information.");
|
|
49
|
+
}
|
|
50
|
+
return status;
|
|
51
|
+
}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
import { LifiStep, Route
|
|
2
|
-
import { TenderlyResponse } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
5
|
-
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
6
|
-
* @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
|
|
7
|
-
*/
|
|
8
|
-
export declare const getEthereumDecryptionHook: (walletAddress: string) => (encryptedData: string) => Promise<string>;
|
|
9
|
-
/**
|
|
10
|
-
* Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
|
|
11
|
-
* @param {string} walletAddress - The wallet address of the user.
|
|
12
|
-
* @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
|
|
13
|
-
*/
|
|
14
|
-
export declare const getEthereumPublicKeyHook: (walletAddress: string) => () => Promise<string>;
|
|
1
|
+
import type { LifiStep, Route } from '@lifi/types';
|
|
2
|
+
import type { TenderlyResponse } from './types';
|
|
15
3
|
/**
|
|
16
4
|
* Returns a random number between min (inclusive) and max (inclusive)
|
|
5
|
+
* @param min - minimum number.
|
|
6
|
+
* @param max - maximum number.
|
|
7
|
+
* @returns - random number.
|
|
17
8
|
*/
|
|
18
9
|
export declare const getRandomNumber: (min: number, max: number) => number;
|
|
19
|
-
export declare const isSameToken: (tokenA: Token, tokenB: Token) => boolean;
|
|
20
10
|
export declare const checkPackageUpdates: (packageName?: string, packageVersion?: string, disableCheck?: boolean) => Promise<void>;
|
|
21
11
|
/**
|
|
22
12
|
* Converts a quote to Route
|
|
23
|
-
* @param
|
|
24
|
-
* @
|
|
13
|
+
* @param step - Step returned from the quote endpoint.
|
|
14
|
+
* @returns - The route to be executed.
|
|
25
15
|
* @throws {ValidationError} Throws a ValidationError if the step has missing values.
|
|
26
16
|
*/
|
|
27
17
|
export declare const convertQuoteToRoute: (step: LifiStep) => Route;
|
package/dist/helpers.js
CHANGED
|
@@ -1,44 +1,15 @@
|
|
|
1
1
|
import { request } from './request';
|
|
2
2
|
import { ValidationError } from './utils/errors';
|
|
3
3
|
import { name, version } from './version';
|
|
4
|
-
const ethereumRequest = async (method, params) => {
|
|
5
|
-
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
6
|
-
if (!ethereum?.request) {
|
|
7
|
-
throw new Error('Provider not available.');
|
|
8
|
-
}
|
|
9
|
-
return ethereum.request({
|
|
10
|
-
method,
|
|
11
|
-
params,
|
|
12
|
-
});
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
16
|
-
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
17
|
-
* @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
|
|
18
|
-
*/
|
|
19
|
-
export const getEthereumDecryptionHook = (walletAddress) => {
|
|
20
|
-
return (encryptedData) => {
|
|
21
|
-
return ethereumRequest('eth_decrypt', [encryptedData, walletAddress]);
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
|
|
26
|
-
* @param {string} walletAddress - The wallet address of the user.
|
|
27
|
-
* @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
|
|
28
|
-
*/
|
|
29
|
-
export const getEthereumPublicKeyHook = (walletAddress) => {
|
|
30
|
-
return () => {
|
|
31
|
-
return ethereumRequest('eth_getEncryptionPublicKey', [walletAddress]);
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
4
|
/**
|
|
35
5
|
* Returns a random number between min (inclusive) and max (inclusive)
|
|
6
|
+
* @param min - minimum number.
|
|
7
|
+
* @param max - maximum number.
|
|
8
|
+
* @returns - random number.
|
|
36
9
|
*/
|
|
37
10
|
export const getRandomNumber = (min, max) => {
|
|
38
11
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
39
12
|
};
|
|
40
|
-
export const isSameToken = (tokenA, tokenB) => tokenA.chainId === tokenB.chainId &&
|
|
41
|
-
tokenA.address.toLowerCase() === tokenB.address.toLowerCase();
|
|
42
13
|
function semverCompare(a, b) {
|
|
43
14
|
if (a.startsWith(b + '-')) {
|
|
44
15
|
return -1;
|
|
@@ -73,8 +44,8 @@ export const checkPackageUpdates = async (packageName, packageVersion, disableCh
|
|
|
73
44
|
};
|
|
74
45
|
/**
|
|
75
46
|
* Converts a quote to Route
|
|
76
|
-
* @param
|
|
77
|
-
* @
|
|
47
|
+
* @param step - Step returned from the quote endpoint.
|
|
48
|
+
* @returns - The route to be executed.
|
|
78
49
|
* @throws {ValidationError} Throws a ValidationError if the step has missing values.
|
|
79
50
|
*/
|
|
80
51
|
export const convertQuoteToRoute = (step) => {
|
package/dist/request.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ConfigService from './services/ConfigService';
|
|
2
2
|
import { HTTPError } from './utils/errors';
|
|
3
|
-
import {
|
|
3
|
+
import { wait } from './utils/utils';
|
|
4
4
|
import { version } from './version';
|
|
5
5
|
export const requestSettings = {
|
|
6
6
|
retries: 1,
|
|
@@ -45,7 +45,7 @@ export const request = async (url, options = {
|
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
47
|
if (options.retries > 0 && error?.status === 500) {
|
|
48
|
-
await
|
|
48
|
+
await wait(500);
|
|
49
49
|
return request(url, { ...options, retries: options.retries - 1 });
|
|
50
50
|
}
|
|
51
51
|
throw error;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ConnectionsRequest, ConnectionsResponse, ContractCallQuoteRequest, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, LifiStep, QuoteRequest, RequestOptions, TokensRequest, TokensResponse } from '@lifi/types';
|
|
2
|
-
import { ChainId, ChainKey, ExtendedChain, PossibilitiesRequest, PossibilitiesResponse, RoutesRequest, RoutesResponse, StatusResponse, Token, ToolsRequest, ToolsResponse } from '../types';
|
|
1
|
+
import type { ConnectionsRequest, ConnectionsResponse, ContractCallQuoteRequest, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, LifiStep, QuoteRequest, RequestOptions, TokensRequest, TokensResponse } from '@lifi/types';
|
|
2
|
+
import type { ChainId, ChainKey, ExtendedChain, PossibilitiesRequest, PossibilitiesResponse, RoutesRequest, RoutesResponse, StatusResponse, Token, ToolsRequest, ToolsResponse } from '../types';
|
|
3
3
|
declare const _default: {
|
|
4
4
|
getChains: (options?: RequestOptions | undefined) => Promise<ExtendedChain[]>;
|
|
5
5
|
getContractCallQuote: (requestConfig: ContractCallQuoteRequest, options?: RequestOptions | undefined) => Promise<LifiStep>;
|
|
@@ -4,6 +4,8 @@ import { ValidationError } from '../utils/errors';
|
|
|
4
4
|
import { parseBackendError } from '../utils/parseError';
|
|
5
5
|
import ConfigService from './ConfigService';
|
|
6
6
|
/**
|
|
7
|
+
* @param requestConfig
|
|
8
|
+
* @param options
|
|
7
9
|
* @deprecated We don't want to support this endpoint anymore in the future. /chains, /tools, /connections, and /tokens should be used instead
|
|
8
10
|
*/
|
|
9
11
|
const getPossibilities = async (requestConfig, options) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChainId
|
|
1
|
+
import { ChainId } from '../types';
|
|
2
2
|
const DefaultExecutionSettings = {
|
|
3
3
|
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
|
|
4
4
|
updateRouteHook: () => { },
|
|
@@ -44,8 +44,7 @@ class ConfigService {
|
|
|
44
44
|
this.config.integrator;
|
|
45
45
|
this.config.widgetVersion =
|
|
46
46
|
configUpdate.widgetVersion || this.config.widgetVersion;
|
|
47
|
-
this.config.multisigConfig
|
|
48
|
-
configUpdate.multisigConfig || this.config.multisigConfig;
|
|
47
|
+
this.config.multisig = configUpdate.multisigConfig || this.config.multisig;
|
|
49
48
|
return this.config;
|
|
50
49
|
};
|
|
51
50
|
this.updateChains = (chains) => {
|
package/dist/typeguards.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LifiStep, RoutesRequest, StaticToken } from './types';
|
|
1
|
+
import type { LifiStep, RoutesRequest, StaticToken } from './types';
|
|
2
2
|
export declare const isRoutesRequest: (routesRequest: RoutesRequest) => routesRequest is RoutesRequest;
|
|
3
3
|
export declare const isStep: (step: LifiStep) => step is LifiStep;
|
|
4
4
|
export declare const isToken: (token: StaticToken) => token is StaticToken;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const approveAbi = [
|
|
2
|
+
{
|
|
3
|
+
name: 'approve',
|
|
4
|
+
inputs: [
|
|
5
|
+
{ internalType: 'address', name: 'spender', type: 'address' },
|
|
6
|
+
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
|
|
7
|
+
],
|
|
8
|
+
outputs: [{ internalType: 'bool', name: 'approved', type: 'bool' }],
|
|
9
|
+
stateMutability: 'nonpayable',
|
|
10
|
+
type: 'function',
|
|
11
|
+
},
|
|
12
|
+
];
|
|
13
|
+
export const allowanceAbi = [
|
|
14
|
+
{
|
|
15
|
+
name: 'allowance',
|
|
16
|
+
inputs: [
|
|
17
|
+
{ internalType: 'address', name: 'owner', type: 'address' },
|
|
18
|
+
{ internalType: 'address', name: 'spender', type: 'address' },
|
|
19
|
+
],
|
|
20
|
+
outputs: [{ internalType: 'uint256', name: 'allowance', type: 'uint256' }],
|
|
21
|
+
stateMutability: 'view',
|
|
22
|
+
type: 'function',
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
export const getEthBalanceAbi = [
|
|
26
|
+
{
|
|
27
|
+
inputs: [{ name: '_owner', type: 'address' }],
|
|
28
|
+
name: 'getEthBalance',
|
|
29
|
+
outputs: [{ name: 'balance', type: 'uint256' }],
|
|
30
|
+
type: 'function',
|
|
31
|
+
stateMutability: 'view',
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
export const balanceOfAbi = [
|
|
35
|
+
{
|
|
36
|
+
inputs: [{ name: '_owner', type: 'address' }],
|
|
37
|
+
name: 'balanceOf',
|
|
38
|
+
outputs: [{ name: 'balance', type: 'uint256' }],
|
|
39
|
+
type: 'function',
|
|
40
|
+
stateMutability: 'view',
|
|
41
|
+
},
|
|
42
|
+
];
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED