2021-02-04 19:15:23 +01:00
|
|
|
import { createSlice } from '@reduxjs/toolkit';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import log from 'loglevel';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-23 16:53:10 +02:00
|
|
|
import { captureMessage } from '@sentry/browser';
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
addToken,
|
2023-07-25 10:50:55 +02:00
|
|
|
addTransactionAndWaitForPublish,
|
2020-10-06 20:28:38 +02:00
|
|
|
fetchAndSetQuotes,
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
resetSwapsPostFetchState,
|
|
|
|
setBackgroundSwapRouteState,
|
|
|
|
setInitialGasEstimate,
|
|
|
|
setSwapsErrorKey,
|
|
|
|
setSwapsTxGasPrice,
|
|
|
|
stopPollingForQuotes,
|
|
|
|
resetBackgroundSwapsState,
|
|
|
|
setSwapsLiveness,
|
2022-02-18 17:48:38 +01:00
|
|
|
setSwapsFeatureFlags,
|
2020-11-12 20:12:04 +01:00
|
|
|
setSelectedQuoteAggId,
|
|
|
|
setSwapsTxGasLimit,
|
2022-02-18 17:48:38 +01:00
|
|
|
fetchSmartTransactionsLiveness,
|
|
|
|
signAndSendSmartTransaction,
|
|
|
|
updateSmartTransaction,
|
|
|
|
setSmartTransactionsRefreshInterval,
|
|
|
|
fetchSmartTransactionFees,
|
|
|
|
cancelSmartTransaction,
|
2022-03-23 20:28:26 +01:00
|
|
|
getTransactions,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../store/actions';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2021-05-06 16:14:42 +02:00
|
|
|
AWAITING_SIGNATURES_ROUTE,
|
2020-11-03 00:41:28 +01:00
|
|
|
AWAITING_SWAP_ROUTE,
|
|
|
|
BUILD_QUOTE_ROUTE,
|
|
|
|
LOADING_QUOTES_ROUTE,
|
|
|
|
SWAPS_ERROR_ROUTE,
|
|
|
|
SWAPS_MAINTENANCE_ROUTE,
|
2022-02-18 17:48:38 +01:00
|
|
|
SMART_TRANSACTION_STATUS_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../helpers/constants/routes';
|
2020-11-04 17:14:08 +01:00
|
|
|
import {
|
2021-07-09 17:24:00 +02:00
|
|
|
fetchSwapsFeatureFlags,
|
2020-11-04 17:14:08 +01:00
|
|
|
fetchSwapsGasPrices,
|
2021-04-23 16:53:10 +02:00
|
|
|
isContractAddressValid,
|
2021-07-09 17:24:00 +02:00
|
|
|
getSwapsLivenessForNetwork,
|
2022-02-18 17:48:38 +01:00
|
|
|
parseSmartTransactionsError,
|
2023-01-31 13:01:10 +01:00
|
|
|
StxErrorTypes,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../pages/swaps/swaps.util';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2021-07-30 13:35:30 +02:00
|
|
|
addHexes,
|
2023-01-20 18:04:37 +01:00
|
|
|
decGWEIToHexWEI,
|
|
|
|
decimalToHex,
|
|
|
|
getValueFromWeiHex,
|
|
|
|
hexWEIToDecGWEI,
|
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
getSelectedAccount,
|
|
|
|
getTokenExchangeRates,
|
2020-11-10 18:39:45 +01:00
|
|
|
getUSDConversionRate,
|
2021-03-18 11:20:06 +01:00
|
|
|
getSwapsDefaultToken,
|
|
|
|
getCurrentChainId,
|
2021-05-06 16:14:42 +02:00
|
|
|
isHardwareWallet,
|
2021-05-12 17:17:17 +02:00
|
|
|
getHardwareWalletType,
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../selectors';
|
2022-04-22 18:09:10 +02:00
|
|
|
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsEventCategory } from '../../../shared/constants/metametrics';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
ERROR_FETCHING_QUOTES,
|
|
|
|
QUOTES_NOT_AVAILABLE_ERROR,
|
2021-05-13 21:49:00 +02:00
|
|
|
CONTRACT_DATA_DISABLED_ERROR,
|
2020-10-06 20:28:38 +02:00
|
|
|
SWAP_FAILED_ERROR,
|
2020-10-21 22:05:48 +02:00
|
|
|
SWAPS_FETCH_ORDER_CONFLICT,
|
2022-02-18 17:48:38 +01:00
|
|
|
ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS,
|
2023-01-24 17:38:01 +01:00
|
|
|
Slippage,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../shared/constants/swaps';
|
2022-02-18 17:48:38 +01:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionType,
|
2022-05-03 17:55:41 +02:00
|
|
|
IN_PROGRESS_TRANSACTION_STATUSES,
|
2023-01-18 15:47:29 +01:00
|
|
|
SmartTransactionStatus,
|
2022-02-18 17:48:38 +01:00
|
|
|
} from '../../../shared/constants/transaction';
|
2022-10-13 21:46:23 +02:00
|
|
|
import { getGasFeeEstimates, getTokens } from '../metamask/metamask';
|
2022-04-26 19:07:39 +02:00
|
|
|
import { ORIGIN_METAMASK } from '../../../shared/constants/app';
|
2022-09-16 21:05:21 +02:00
|
|
|
import {
|
|
|
|
calcGasTotal,
|
|
|
|
calcTokenAmount,
|
|
|
|
} from '../../../shared/lib/transactions-controller-utils';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { EtherDenomination } from '../../../shared/constants/common';
|
|
|
|
import { Numeric } from '../../../shared/modules/Numeric';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-10-04 18:55:05 +02:00
|
|
|
export const GAS_PRICES_LOADING_STATES = {
|
2020-11-04 17:14:08 +01:00
|
|
|
INITIAL: 'INITIAL',
|
|
|
|
LOADING: 'LOADING',
|
|
|
|
FAILED: 'FAILED',
|
|
|
|
COMPLETED: 'COMPLETED',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const FALLBACK_GAS_MULTIPLIER = 1.5;
|
2020-12-15 18:16:51 +01:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
const initialState = {
|
|
|
|
aggregatorMetadata: null,
|
|
|
|
approveTxId: null,
|
2021-05-06 16:14:42 +02:00
|
|
|
tradeTxId: null,
|
2020-10-06 20:28:38 +02:00
|
|
|
balanceError: false,
|
|
|
|
fetchingQuotes: false,
|
|
|
|
fromToken: null,
|
2021-12-01 17:25:09 +01:00
|
|
|
fromTokenInputValue: '',
|
|
|
|
fromTokenError: null,
|
|
|
|
isFeatureFlagLoaded: false,
|
2023-01-24 17:38:01 +01:00
|
|
|
maxSlippage: Slippage.default,
|
2020-10-06 20:28:38 +02:00
|
|
|
quotesFetchStartTime: null,
|
2021-09-15 15:13:18 +02:00
|
|
|
reviewSwapClickedTimestamp: null,
|
2020-10-06 20:28:38 +02:00
|
|
|
topAssets: {},
|
|
|
|
toToken: null,
|
2020-11-04 17:14:08 +01:00
|
|
|
customGas: {
|
|
|
|
price: null,
|
|
|
|
limit: null,
|
|
|
|
loading: GAS_PRICES_LOADING_STATES.INITIAL,
|
|
|
|
priceEstimates: {},
|
|
|
|
fallBackPrice: null,
|
|
|
|
},
|
2022-02-18 17:48:38 +01:00
|
|
|
currentSmartTransactionsError: '',
|
|
|
|
swapsSTXLoading: false,
|
2023-06-15 20:17:21 +02:00
|
|
|
transactionSettingsOpened: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const slice = createSlice({
|
|
|
|
name: 'swaps',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
clearSwapsState: () => initialState,
|
|
|
|
navigatedBackToBuildQuote: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.approveTxId = null;
|
2021-05-06 16:14:42 +02:00
|
|
|
state.tradeTxId = null;
|
2021-02-04 19:15:23 +01:00
|
|
|
state.balanceError = false;
|
|
|
|
state.fetchingQuotes = false;
|
|
|
|
state.customGas.limit = null;
|
|
|
|
state.customGas.price = null;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
retriedGetQuotes: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.approveTxId = null;
|
|
|
|
state.balanceError = false;
|
|
|
|
state.fetchingQuotes = false;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
setAggregatorMetadata: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.aggregatorMetadata = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
setBalanceError: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.balanceError = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
setFetchingQuotes: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.fetchingQuotes = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
setFromToken: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.fromToken = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2021-12-01 17:25:09 +01:00
|
|
|
setFromTokenInputValue: (state, action) => {
|
|
|
|
state.fromTokenInputValue = action.payload;
|
|
|
|
},
|
|
|
|
setFromTokenError: (state, action) => {
|
|
|
|
state.fromTokenError = action.payload;
|
|
|
|
},
|
|
|
|
setIsFeatureFlagLoaded: (state, action) => {
|
|
|
|
state.isFeatureFlagLoaded = action.payload;
|
|
|
|
},
|
|
|
|
setMaxSlippage: (state, action) => {
|
|
|
|
state.maxSlippage = action.payload;
|
|
|
|
},
|
2020-10-06 20:28:38 +02:00
|
|
|
setQuotesFetchStartTime: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.quotesFetchStartTime = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2021-09-15 15:13:18 +02:00
|
|
|
setReviewSwapClickedTimestamp: (state, action) => {
|
|
|
|
state.reviewSwapClickedTimestamp = action.payload;
|
|
|
|
},
|
2020-10-06 20:28:38 +02:00
|
|
|
setTopAssets: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.topAssets = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
setToToken: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.toToken = action.payload;
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2020-11-12 20:12:04 +01:00
|
|
|
swapCustomGasModalClosed: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.price = null;
|
|
|
|
state.customGas.limit = null;
|
2020-11-12 20:12:04 +01:00
|
|
|
},
|
2020-11-04 17:14:08 +01:00
|
|
|
swapCustomGasModalPriceEdited: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.price = action.payload;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
swapCustomGasModalLimitEdited: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.limit = action.payload;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
swapGasPriceEstimatesFetchStarted: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.loading = GAS_PRICES_LOADING_STATES.LOADING;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
swapGasPriceEstimatesFetchFailed: (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.loading = GAS_PRICES_LOADING_STATES.FAILED;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
swapGasPriceEstimatesFetchCompleted: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.priceEstimates = action.payload.priceEstimates;
|
|
|
|
state.customGas.loading = GAS_PRICES_LOADING_STATES.COMPLETED;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
|
|
|
retrievedFallbackSwapsGasPrice: (state, action) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
state.customGas.fallBackPrice = action.payload;
|
2020-11-04 17:14:08 +01:00
|
|
|
},
|
2022-02-18 17:48:38 +01:00
|
|
|
setCurrentSmartTransactionsError: (state, action) => {
|
2023-01-31 13:01:10 +01:00
|
|
|
const errorType = Object.values(StxErrorTypes).includes(action.payload)
|
2022-02-18 17:48:38 +01:00
|
|
|
? action.payload
|
2023-05-25 19:30:10 +02:00
|
|
|
: StxErrorTypes.unavailable;
|
2022-02-18 17:48:38 +01:00
|
|
|
state.currentSmartTransactionsError = errorType;
|
|
|
|
},
|
|
|
|
setSwapsSTXSubmitLoading: (state, action) => {
|
|
|
|
state.swapsSTXLoading = action.payload || false;
|
|
|
|
},
|
2023-06-15 20:17:21 +02:00
|
|
|
setTransactionSettingsOpened: (state, action) => {
|
|
|
|
state.transactionSettingsOpened = Boolean(action.payload);
|
|
|
|
},
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { actions, reducer } = slice;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default reducer;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
// Selectors
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getAggregatorMetadata = (state) => state.swaps.aggregatorMetadata;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getBalanceError = (state) => state.swaps.balanceError;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getFromToken = (state) => state.swaps.fromToken;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-12-01 17:25:09 +01:00
|
|
|
export const getFromTokenError = (state) => state.swaps.fromTokenError;
|
|
|
|
|
|
|
|
export const getFromTokenInputValue = (state) =>
|
|
|
|
state.swaps.fromTokenInputValue;
|
|
|
|
|
|
|
|
export const getIsFeatureFlagLoaded = (state) =>
|
|
|
|
state.swaps.isFeatureFlagLoaded;
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const getSwapsSTXLoading = (state) => state.swaps.swapsSTXLoading;
|
|
|
|
|
2021-12-01 17:25:09 +01:00
|
|
|
export const getMaxSlippage = (state) => state.swaps.maxSlippage;
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getTopAssets = (state) => state.swaps.topAssets;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getToToken = (state) => state.swaps.toToken;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getFetchingQuotes = (state) => state.swaps.fetchingQuotes;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getQuotesFetchStartTime = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.quotesFetchStartTime;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-09-15 15:13:18 +02:00
|
|
|
export const getReviewSwapClickedTimestamp = (state) =>
|
|
|
|
state.swaps.reviewSwapClickedTimestamp;
|
|
|
|
|
2020-11-04 17:14:08 +01:00
|
|
|
export const getSwapsCustomizationModalPrice = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.price;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export const getSwapsCustomizationModalLimit = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.limit;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export const swapGasPriceEstimateIsLoading = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.loading === GAS_PRICES_LOADING_STATES.LOADING;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export const swapGasEstimateLoadingHasFailed = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.loading === GAS_PRICES_LOADING_STATES.INITIAL;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export const getSwapGasPriceEstimateData = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.priceEstimates;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export const getSwapsFallbackGasPrice = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.customGas.fallBackPrice;
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const getCurrentSmartTransactionsError = (state) =>
|
|
|
|
state.swaps.currentSmartTransactionsError;
|
|
|
|
|
2023-06-15 20:17:21 +02:00
|
|
|
export const getTransactionSettingsOpened = (state) =>
|
|
|
|
state.swaps.transactionSettingsOpened;
|
|
|
|
|
2020-11-04 17:14:08 +01:00
|
|
|
export function shouldShowCustomPriceTooLowWarning(state) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { average } = getSwapGasPriceEstimateData(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customGasPrice = getSwapsCustomizationModalPrice(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
if (!customGasPrice || average === undefined) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return false;
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
const customPriceRisksSwapFailure = new Numeric(
|
|
|
|
customGasPrice,
|
|
|
|
16,
|
|
|
|
EtherDenomination.WEI,
|
|
|
|
)
|
|
|
|
.toDenomination(EtherDenomination.GWEI)
|
|
|
|
.greaterThan(average, 10);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return customPriceRisksSwapFailure;
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
// Background selectors
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const getSwapsState = (state) => state.metamask.swapsState;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
export const getSwapsFeatureIsLive = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.swapsFeatureIsLive;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const getSmartTransactionsError = (state) =>
|
|
|
|
state.appState.smartTransactionsError;
|
|
|
|
|
|
|
|
export const getSmartTransactionsErrorMessageDismissed = (state) =>
|
|
|
|
state.appState.smartTransactionsErrorMessageDismissed;
|
|
|
|
|
|
|
|
export const getSmartTransactionsEnabled = (state) => {
|
|
|
|
const hardwareWalletUsed = isHardwareWallet(state);
|
|
|
|
const chainId = getCurrentChainId(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const isAllowedNetwork =
|
|
|
|
ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId);
|
2022-02-18 17:48:38 +01:00
|
|
|
const smartTransactionsFeatureFlagEnabled =
|
|
|
|
state.metamask.swapsState?.swapsFeatureFlags?.smartTransactions
|
|
|
|
?.extensionActive;
|
|
|
|
const smartTransactionsLiveness =
|
|
|
|
state.metamask.smartTransactionsState?.liveness;
|
|
|
|
return Boolean(
|
|
|
|
isAllowedNetwork &&
|
|
|
|
!hardwareWalletUsed &&
|
|
|
|
smartTransactionsFeatureFlagEnabled &&
|
|
|
|
smartTransactionsLiveness,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCurrentSmartTransactionsEnabled = (state) => {
|
|
|
|
const smartTransactionsEnabled = getSmartTransactionsEnabled(state);
|
|
|
|
const currentSmartTransactionsError = getCurrentSmartTransactionsError(state);
|
|
|
|
return smartTransactionsEnabled && !currentSmartTransactionsError;
|
|
|
|
};
|
|
|
|
|
2023-06-15 20:17:21 +02:00
|
|
|
export const getSwapRedesignEnabled = (state) => {
|
|
|
|
const swapRedesign =
|
|
|
|
state.metamask.swapsState?.swapsFeatureFlags?.swapRedesign;
|
|
|
|
if (swapRedesign === undefined) {
|
|
|
|
return true; // By default show the redesign if we don't have feature flags returned yet.
|
|
|
|
}
|
|
|
|
return swapRedesign.extensionActive;
|
|
|
|
};
|
|
|
|
|
2020-12-15 21:24:22 +01:00
|
|
|
export const getSwapsQuoteRefreshTime = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.swapsQuoteRefreshTime;
|
2020-12-15 21:24:22 +01:00
|
|
|
|
2021-09-15 15:13:18 +02:00
|
|
|
export const getSwapsQuotePrefetchingRefreshTime = (state) =>
|
|
|
|
state.metamask.swapsState.swapsQuotePrefetchingRefreshTime;
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getBackgroundSwapRouteState = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.routeState;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getCustomSwapsGas = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.customMaxGas;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getCustomSwapsGasPrice = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.customGasPrice;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
export const getCustomMaxFeePerGas = (state) =>
|
|
|
|
state.metamask.swapsState.customMaxFeePerGas;
|
|
|
|
|
|
|
|
export const getCustomMaxPriorityFeePerGas = (state) =>
|
|
|
|
state.metamask.swapsState.customMaxPriorityFeePerGas;
|
|
|
|
|
2021-08-06 21:31:30 +02:00
|
|
|
export const getSwapsUserFeeLevel = (state) =>
|
|
|
|
state.metamask.swapsState.swapsUserFeeLevel;
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getFetchParams = (state) => state.metamask.swapsState.fetchParams;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getQuotes = (state) => state.metamask.swapsState.quotes;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getQuotesLastFetched = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsState.quotesLastFetched;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const getSelectedQuote = (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { selectedAggId, quotes } = getSwapsState(state);
|
|
|
|
return quotes[selectedAggId];
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getSwapsErrorKey = (state) => getSwapsState(state)?.errorKey;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getShowQuoteLoadingScreen = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.swaps.showQuoteLoadingScreen;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getSwapsTokens = (state) => state.metamask.swapsState.tokens;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getSwapsWelcomeMessageSeenStatus = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.swapsWelcomeMessageHasBeenShown;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const getTopQuote = (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { topAggId, quotes } = getSwapsState(state);
|
|
|
|
return quotes[topAggId];
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getApproveTxId = (state) => state.metamask.swapsState.approveTxId;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getTradeTxId = (state) => state.metamask.swapsState.tradeTxId;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getUsedQuote = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
getSelectedQuote(state) || getTopQuote(state);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
// Compound selectors
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getDestinationTokenInfo = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
getFetchParams(state)?.metaData?.destinationTokenInfo;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-04 17:14:08 +01:00
|
|
|
export const getUsedSwapsGasPrice = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
getCustomSwapsGasPrice(state) || getSwapsFallbackGasPrice(state);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const getApproveTxParams = (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { approvalNeeded } =
|
|
|
|
getSelectedQuote(state) || getTopQuote(state) || {};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
if (!approvalNeeded) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const data = getSwapsState(state)?.customApproveTxData || approvalNeeded.data;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-23 17:51:43 +01:00
|
|
|
const gasPrice = getUsedSwapsGasPrice(state);
|
2021-02-04 19:15:23 +01:00
|
|
|
return { ...approvalNeeded, gasPrice, data };
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const getSmartTransactionsOptInStatus = (state) => {
|
|
|
|
return state.metamask.smartTransactionsState?.userOptIn;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCurrentSmartTransactions = (state) => {
|
|
|
|
return state.metamask.smartTransactionsState?.smartTransactions?.[
|
|
|
|
getCurrentChainId(state)
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getPendingSmartTransactions = (state) => {
|
|
|
|
const currentSmartTransactions = getCurrentSmartTransactions(state);
|
|
|
|
if (!currentSmartTransactions || currentSmartTransactions.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return currentSmartTransactions.filter(
|
2023-01-18 15:47:29 +01:00
|
|
|
(stx) => stx.status === SmartTransactionStatus.pending,
|
2022-02-18 17:48:38 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getSmartTransactionFees = (state) => {
|
|
|
|
return state.metamask.smartTransactionsState?.fees;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getSmartTransactionEstimatedGas = (state) => {
|
|
|
|
return state.metamask.smartTransactionsState?.estimatedGas;
|
|
|
|
};
|
|
|
|
|
2022-05-09 18:48:14 +02:00
|
|
|
export const getSwapsNetworkConfig = (state) => {
|
2022-02-18 17:48:38 +01:00
|
|
|
const {
|
|
|
|
swapsQuoteRefreshTime,
|
|
|
|
swapsQuotePrefetchingRefreshTime,
|
|
|
|
swapsStxGetTransactionsRefreshTime,
|
|
|
|
swapsStxBatchStatusRefreshTime,
|
|
|
|
swapsStxStatusDeadline,
|
2022-05-09 18:48:14 +02:00
|
|
|
swapsStxMaxFeeMultiplier,
|
2022-02-18 17:48:38 +01:00
|
|
|
} = state.metamask.swapsState;
|
|
|
|
return {
|
|
|
|
quoteRefreshTime: swapsQuoteRefreshTime,
|
|
|
|
quotePrefetchingRefreshTime: swapsQuotePrefetchingRefreshTime,
|
|
|
|
stxGetTransactionsRefreshTime: swapsStxGetTransactionsRefreshTime,
|
|
|
|
stxBatchStatusRefreshTime: swapsStxBatchStatusRefreshTime,
|
|
|
|
stxStatusDeadline: swapsStxStatusDeadline,
|
2022-05-09 18:48:14 +02:00
|
|
|
stxMaxFeeMultiplier: swapsStxMaxFeeMultiplier,
|
2022-02-18 17:48:38 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
// Actions / action-creators
|
|
|
|
|
|
|
|
const {
|
|
|
|
clearSwapsState,
|
|
|
|
navigatedBackToBuildQuote,
|
|
|
|
retriedGetQuotes,
|
2020-11-04 17:14:08 +01:00
|
|
|
swapGasPriceEstimatesFetchCompleted,
|
|
|
|
swapGasPriceEstimatesFetchStarted,
|
|
|
|
swapGasPriceEstimatesFetchFailed,
|
2020-10-06 20:28:38 +02:00
|
|
|
setAggregatorMetadata,
|
|
|
|
setBalanceError,
|
|
|
|
setFetchingQuotes,
|
|
|
|
setFromToken,
|
2021-12-01 17:25:09 +01:00
|
|
|
setFromTokenError,
|
|
|
|
setFromTokenInputValue,
|
|
|
|
setIsFeatureFlagLoaded,
|
|
|
|
setMaxSlippage,
|
2020-10-06 20:28:38 +02:00
|
|
|
setQuotesFetchStartTime,
|
2021-09-15 15:13:18 +02:00
|
|
|
setReviewSwapClickedTimestamp,
|
2020-10-06 20:28:38 +02:00
|
|
|
setTopAssets,
|
|
|
|
setToToken,
|
2020-11-04 17:14:08 +01:00
|
|
|
swapCustomGasModalPriceEdited,
|
|
|
|
swapCustomGasModalLimitEdited,
|
|
|
|
retrievedFallbackSwapsGasPrice,
|
2020-11-12 20:12:04 +01:00
|
|
|
swapCustomGasModalClosed,
|
2022-02-18 17:48:38 +01:00
|
|
|
setCurrentSmartTransactionsError,
|
|
|
|
setSwapsSTXSubmitLoading,
|
2023-06-15 20:17:21 +02:00
|
|
|
setTransactionSettingsOpened,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = actions;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export {
|
|
|
|
clearSwapsState,
|
|
|
|
setAggregatorMetadata,
|
|
|
|
setBalanceError,
|
|
|
|
setFetchingQuotes,
|
|
|
|
setFromToken as setSwapsFromToken,
|
2021-12-01 17:25:09 +01:00
|
|
|
setFromTokenError,
|
|
|
|
setFromTokenInputValue,
|
|
|
|
setIsFeatureFlagLoaded,
|
|
|
|
setMaxSlippage,
|
2020-10-06 20:28:38 +02:00
|
|
|
setQuotesFetchStartTime as setSwapQuotesFetchStartTime,
|
2021-09-15 15:13:18 +02:00
|
|
|
setReviewSwapClickedTimestamp,
|
2020-10-06 20:28:38 +02:00
|
|
|
setTopAssets,
|
|
|
|
setToToken as setSwapToToken,
|
2020-11-04 17:14:08 +01:00
|
|
|
swapCustomGasModalPriceEdited,
|
|
|
|
swapCustomGasModalLimitEdited,
|
2020-11-12 20:12:04 +01:00
|
|
|
swapCustomGasModalClosed,
|
2023-06-15 20:17:21 +02:00
|
|
|
setTransactionSettingsOpened,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const navigateBackToBuildQuote = (history) => {
|
|
|
|
return async (dispatch) => {
|
|
|
|
// TODO: Ensure any fetch in progress is cancelled
|
2021-02-04 19:15:23 +01:00
|
|
|
await dispatch(setBackgroundSwapRouteState(''));
|
|
|
|
dispatch(navigatedBackToBuildQuote());
|
|
|
|
history.push(BUILD_QUOTE_ROUTE);
|
|
|
|
};
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const prepareForRetryGetQuotes = () => {
|
|
|
|
return async (dispatch) => {
|
|
|
|
// TODO: Ensure any fetch in progress is cancelled
|
2021-02-04 19:15:23 +01:00
|
|
|
await dispatch(resetSwapsPostFetchState());
|
|
|
|
dispatch(retriedGetQuotes());
|
|
|
|
};
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export const prepareToLeaveSwaps = () => {
|
|
|
|
return async (dispatch) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(clearSwapsState());
|
|
|
|
await dispatch(resetBackgroundSwapsState());
|
|
|
|
};
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-12 20:12:04 +01:00
|
|
|
export const swapsQuoteSelected = (aggId) => {
|
|
|
|
return (dispatch) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapCustomGasModalLimitEdited(null));
|
|
|
|
dispatch(setSelectedQuoteAggId(aggId));
|
|
|
|
dispatch(setSwapsTxGasLimit(''));
|
|
|
|
};
|
|
|
|
};
|
2020-11-12 20:12:04 +01:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
export const fetchAndSetSwapsGasPriceInfo = () => {
|
|
|
|
return async (dispatch) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const basicEstimates = await dispatch(fetchMetaSwapsGasPriceEstimates());
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
if (basicEstimates?.fast) {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setSwapsTxGasPrice(decGWEIToHexWEI(basicEstimates.fast)));
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-05-03 17:55:41 +02:00
|
|
|
const disableStxIfRegularTxInProgress = (dispatch, transactions) => {
|
|
|
|
if (transactions?.length <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const transaction of transactions) {
|
|
|
|
if (IN_PROGRESS_TRANSACTION_STATUSES.includes(transaction.status)) {
|
|
|
|
dispatch(
|
2023-05-25 19:30:10 +02:00
|
|
|
setCurrentSmartTransactionsError(StxErrorTypes.regularTxPending),
|
2022-05-03 17:55:41 +02:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const fetchSwapsLivenessAndFeatureFlags = () => {
|
2021-04-14 09:16:27 +02:00
|
|
|
return async (dispatch, getState) => {
|
2021-07-09 17:24:00 +02:00
|
|
|
let swapsLivenessForNetwork = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
2022-03-23 20:28:26 +01:00
|
|
|
const state = getState();
|
|
|
|
const chainId = getCurrentChainId(state);
|
2021-04-14 09:16:27 +02:00
|
|
|
try {
|
2021-07-09 17:24:00 +02:00
|
|
|
const swapsFeatureFlags = await fetchSwapsFeatureFlags();
|
2022-02-18 17:48:38 +01:00
|
|
|
await dispatch(setSwapsFeatureFlags(swapsFeatureFlags));
|
|
|
|
if (ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId)) {
|
|
|
|
await dispatch(fetchSmartTransactionsLiveness());
|
2022-05-03 17:55:41 +02:00
|
|
|
const transactions = await getTransactions({
|
2022-03-23 20:28:26 +01:00
|
|
|
searchCriteria: {
|
|
|
|
from: state.metamask?.selectedAddress,
|
|
|
|
},
|
|
|
|
});
|
2022-05-03 17:55:41 +02:00
|
|
|
disableStxIfRegularTxInProgress(dispatch, transactions);
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
2021-07-09 17:24:00 +02:00
|
|
|
swapsLivenessForNetwork = getSwapsLivenessForNetwork(
|
2022-02-18 17:48:38 +01:00
|
|
|
chainId,
|
2023-01-31 13:01:10 +01:00
|
|
|
swapsFeatureFlags,
|
2021-04-14 09:16:27 +02:00
|
|
|
);
|
|
|
|
} catch (error) {
|
2022-02-18 17:48:38 +01:00
|
|
|
log.error(
|
|
|
|
'Failed to fetch Swaps feature flags and Swaps liveness, defaulting to false.',
|
|
|
|
error,
|
|
|
|
);
|
2021-04-14 09:16:27 +02:00
|
|
|
}
|
2021-07-09 17:24:00 +02:00
|
|
|
await dispatch(setSwapsLiveness(swapsLivenessForNetwork));
|
2021-12-01 17:25:09 +01:00
|
|
|
dispatch(setIsFeatureFlagLoaded(true));
|
2021-07-09 17:24:00 +02:00
|
|
|
return swapsLivenessForNetwork;
|
2021-04-14 09:16:27 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-13 21:46:23 +02:00
|
|
|
const isTokenAlreadyAdded = (tokenAddress, tokens) => {
|
|
|
|
if (!Array.isArray(tokens)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return tokens.find(
|
|
|
|
(token) => token.address.toLowerCase() === tokenAddress.toLowerCase(),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const fetchQuotesAndSetQuoteState = (
|
|
|
|
history,
|
|
|
|
inputValue,
|
|
|
|
maxSlippage,
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent,
|
2021-09-15 15:13:18 +02:00
|
|
|
pageRedirectionDisabled,
|
2020-11-03 00:41:28 +01:00
|
|
|
) => {
|
2020-10-06 20:28:38 +02:00
|
|
|
return async (dispatch, getState) => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const state = getState();
|
|
|
|
const chainId = getCurrentChainId(state);
|
2021-07-09 17:24:00 +02:00
|
|
|
let swapsLivenessForNetwork = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
try {
|
2021-07-09 17:24:00 +02:00
|
|
|
const swapsFeatureFlags = await fetchSwapsFeatureFlags();
|
|
|
|
swapsLivenessForNetwork = getSwapsLivenessForNetwork(
|
|
|
|
chainId,
|
2023-01-31 13:01:10 +01:00
|
|
|
swapsFeatureFlags,
|
2021-07-09 17:24:00 +02:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error('Failed to fetch Swaps liveness, defaulting to false.', error);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-07-09 17:24:00 +02:00
|
|
|
await dispatch(setSwapsLiveness(swapsLivenessForNetwork));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
if (!swapsLivenessForNetwork.swapsFeatureIsLive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await history.push(SWAPS_MAINTENANCE_ROUTE);
|
|
|
|
return;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchParams = getFetchParams(state);
|
|
|
|
const selectedAccount = getSelectedAccount(state);
|
|
|
|
const balanceError = getBalanceError(state);
|
2021-03-18 11:20:06 +01:00
|
|
|
const swapsDefaultToken = getSwapsDefaultToken(state);
|
2020-11-03 00:41:28 +01:00
|
|
|
const fetchParamsFromToken =
|
2021-03-18 11:20:06 +01:00
|
|
|
fetchParams?.metaData?.sourceTokenInfo?.symbol ===
|
|
|
|
swapsDefaultToken.symbol
|
|
|
|
? swapsDefaultToken
|
2021-02-04 19:15:23 +01:00
|
|
|
: fetchParams?.metaData?.sourceTokenInfo;
|
|
|
|
const selectedFromToken = getFromToken(state) || fetchParamsFromToken || {};
|
2020-11-03 00:41:28 +01:00
|
|
|
const selectedToToken =
|
2021-02-04 19:15:23 +01:00
|
|
|
getToToken(state) || fetchParams?.metaData?.destinationTokenInfo || {};
|
2020-10-06 20:28:38 +02:00
|
|
|
const {
|
|
|
|
address: fromTokenAddress,
|
|
|
|
symbol: fromTokenSymbol,
|
|
|
|
decimals: fromTokenDecimals,
|
|
|
|
iconUrl: fromTokenIconUrl,
|
|
|
|
balance: fromTokenBalance,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = selectedFromToken;
|
2023-06-15 20:17:21 +02:00
|
|
|
const { address: toTokenAddress, symbol: toTokenSymbol } = selectedToToken;
|
2021-09-15 15:13:18 +02:00
|
|
|
// pageRedirectionDisabled is true if quotes prefetching is active (a user is on the Build Quote page).
|
|
|
|
// In that case we just want to silently prefetch quotes without redirecting to the quotes loading page.
|
|
|
|
if (!pageRedirectionDisabled) {
|
|
|
|
await dispatch(setBackgroundSwapRouteState('loading'));
|
|
|
|
history.push(LOADING_QUOTES_ROUTE);
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setFetchingQuotes(true));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const contractExchangeRates = getTokenExchangeRates(state);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2021-03-29 23:05:36 +02:00
|
|
|
fromTokenAddress &&
|
2021-03-18 11:20:06 +01:00
|
|
|
fromTokenSymbol !== swapsDefaultToken.symbol &&
|
2020-11-03 00:41:28 +01:00
|
|
|
!contractExchangeRates[fromTokenAddress] &&
|
|
|
|
fromTokenBalance &&
|
|
|
|
new BigNumber(fromTokenBalance, 16).gt(0)
|
|
|
|
) {
|
|
|
|
dispatch(
|
|
|
|
addToken(
|
|
|
|
fromTokenAddress,
|
|
|
|
fromTokenSymbol,
|
|
|
|
fromTokenDecimals,
|
|
|
|
fromTokenIconUrl,
|
|
|
|
true,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const swapsTokens = getSwapsTokens(state);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const sourceTokenInfo =
|
|
|
|
swapsTokens?.find(({ address }) => address === fromTokenAddress) ||
|
2021-02-04 19:15:23 +01:00
|
|
|
selectedFromToken;
|
2020-11-03 00:41:28 +01:00
|
|
|
const destinationTokenInfo =
|
|
|
|
swapsTokens?.find(({ address }) => address === toTokenAddress) ||
|
2021-02-04 19:15:23 +01:00
|
|
|
selectedToToken;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setFromToken(selectedFromToken));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-05-12 17:17:17 +02:00
|
|
|
const hardwareWalletUsed = isHardwareWallet(state);
|
|
|
|
const hardwareWalletType = getHardwareWalletType(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const networkAndAccountSupports1559 =
|
|
|
|
checkNetworkAndAccountSupports1559(state);
|
2022-02-18 17:48:38 +01:00
|
|
|
const smartTransactionsOptInStatus = getSmartTransactionsOptInStatus(state);
|
|
|
|
const smartTransactionsEnabled = getSmartTransactionsEnabled(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const currentSmartTransactionsEnabled =
|
|
|
|
getCurrentSmartTransactionsEnabled(state);
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'Quotes Requested',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2020-12-02 22:41:30 +01:00
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
token_from: fromTokenSymbol,
|
|
|
|
token_from_amount: String(inputValue),
|
|
|
|
token_to: toTokenSymbol,
|
|
|
|
request_type: balanceError ? 'Quote' : 'Order',
|
|
|
|
slippage: maxSlippage,
|
2023-01-24 17:38:01 +01:00
|
|
|
custom_slippage: maxSlippage !== Slippage.default,
|
2021-05-12 17:17:17 +02:00
|
|
|
is_hardware_wallet: hardwareWalletUsed,
|
|
|
|
hardware_wallet_type: hardwareWalletType,
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_enabled: smartTransactionsEnabled,
|
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
2020-10-06 20:28:38 +02:00
|
|
|
anonymizedData: true,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchStartTime = Date.now();
|
|
|
|
dispatch(setQuotesFetchStartTime(fetchStartTime));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const fetchAndSetQuotesPromise = dispatch(
|
|
|
|
fetchAndSetQuotes(
|
|
|
|
{
|
|
|
|
slippage: maxSlippage,
|
|
|
|
sourceToken: fromTokenAddress,
|
|
|
|
destinationToken: toTokenAddress,
|
|
|
|
value: inputValue,
|
|
|
|
fromAddress: selectedAccount.address,
|
|
|
|
balanceError,
|
|
|
|
sourceDecimals: fromTokenDecimals,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sourceTokenInfo,
|
|
|
|
destinationTokenInfo,
|
|
|
|
accountBalance: selectedAccount.balance,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-08-03 00:52:18 +02:00
|
|
|
const gasPriceFetchPromise = networkAndAccountSupports1559
|
2021-07-30 13:35:30 +02:00
|
|
|
? null // For EIP 1559 we can get gas prices via "useGasFeeEstimates".
|
|
|
|
: dispatch(fetchAndSetSwapsGasPriceInfo());
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const [[fetchedQuotes, selectedAggId]] = await Promise.all([
|
|
|
|
fetchAndSetQuotesPromise,
|
|
|
|
gasPriceFetchPromise,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
if (Object.values(fetchedQuotes)?.length === 0) {
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'No Quotes Available',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2020-12-02 22:41:30 +01:00
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
token_from: fromTokenSymbol,
|
|
|
|
token_from_amount: String(inputValue),
|
|
|
|
token_to: toTokenSymbol,
|
|
|
|
request_type: balanceError ? 'Quote' : 'Order',
|
|
|
|
slippage: maxSlippage,
|
2023-01-24 17:38:01 +01:00
|
|
|
custom_slippage: maxSlippage !== Slippage.default,
|
2021-05-13 21:26:08 +02:00
|
|
|
is_hardware_wallet: hardwareWalletUsed,
|
|
|
|
hardware_wallet_type: hardwareWalletType,
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_enabled: smartTransactionsEnabled,
|
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
dispatch(setSwapsErrorKey(QUOTES_NOT_AVAILABLE_ERROR));
|
2020-10-06 20:28:38 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
const newSelectedQuote = fetchedQuotes[selectedAggId];
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'Quotes Received',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2020-12-02 22:41:30 +01:00
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
token_from: fromTokenSymbol,
|
|
|
|
token_from_amount: String(inputValue),
|
|
|
|
token_to: toTokenSymbol,
|
2020-11-03 00:41:28 +01:00
|
|
|
token_to_amount: calcTokenAmount(
|
|
|
|
newSelectedQuote.destinationAmount,
|
|
|
|
newSelectedQuote.decimals || 18,
|
|
|
|
),
|
2020-10-06 20:28:38 +02:00
|
|
|
request_type: balanceError ? 'Quote' : 'Order',
|
|
|
|
slippage: maxSlippage,
|
2023-01-24 17:38:01 +01:00
|
|
|
custom_slippage: maxSlippage !== Slippage.default,
|
2020-10-06 20:28:38 +02:00
|
|
|
response_time: Date.now() - fetchStartTime,
|
|
|
|
best_quote_source: newSelectedQuote.aggregator,
|
|
|
|
available_quotes: Object.values(fetchedQuotes)?.length,
|
2021-05-12 17:17:17 +02:00
|
|
|
is_hardware_wallet: hardwareWalletUsed,
|
|
|
|
hardware_wallet_type: hardwareWalletType,
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_enabled: smartTransactionsEnabled,
|
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
2020-10-06 20:28:38 +02:00
|
|
|
anonymizedData: true,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setInitialGasEstimate(selectedAggId));
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-10-21 22:05:48 +02:00
|
|
|
// A newer swap request is running, so simply bail and let the newer request respond
|
|
|
|
if (e.message === SWAPS_FETCH_ORDER_CONFLICT) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug(`Swap fetch order conflict detected; ignoring older request`);
|
|
|
|
return;
|
2020-10-21 22:05:48 +02:00
|
|
|
}
|
2020-10-26 23:22:46 +01:00
|
|
|
// TODO: Check for any errors we should expect to occur in production, and report others to Sentry
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(`Error fetching quotes: `, e);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setSwapsErrorKey(ERROR_FETCHING_QUOTES));
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setFetchingQuotes(false));
|
|
|
|
};
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2023-06-15 20:17:21 +02:00
|
|
|
const addTokenTo = (dispatch, state) => {
|
|
|
|
const fetchParams = getFetchParams(state);
|
|
|
|
const swapsDefaultToken = getSwapsDefaultToken(state);
|
|
|
|
const contractExchangeRates = getTokenExchangeRates(state);
|
|
|
|
const selectedToToken =
|
|
|
|
getToToken(state) || fetchParams?.metaData?.destinationTokenInfo || {};
|
|
|
|
const {
|
|
|
|
address: toTokenAddress,
|
|
|
|
symbol: toTokenSymbol,
|
|
|
|
decimals: toTokenDecimals,
|
|
|
|
iconUrl: toTokenIconUrl,
|
|
|
|
} = selectedToToken;
|
|
|
|
if (
|
|
|
|
toTokenAddress &&
|
|
|
|
toTokenSymbol !== swapsDefaultToken.symbol &&
|
|
|
|
contractExchangeRates[toTokenAddress] === undefined &&
|
|
|
|
!isTokenAlreadyAdded(toTokenAddress, getTokens(state))
|
|
|
|
) {
|
|
|
|
dispatch(
|
|
|
|
addToken(
|
|
|
|
toTokenAddress,
|
|
|
|
toTokenSymbol,
|
|
|
|
toTokenDecimals,
|
|
|
|
toTokenIconUrl,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const signAndSendSwapsSmartTransaction = ({
|
|
|
|
unsignedTransaction,
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent,
|
2022-02-18 17:48:38 +01:00
|
|
|
history,
|
2022-03-23 20:28:26 +01:00
|
|
|
additionalTrackingParams,
|
2022-02-18 17:48:38 +01:00
|
|
|
}) => {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
dispatch(setSwapsSTXSubmitLoading(true));
|
|
|
|
const state = getState();
|
|
|
|
const fetchParams = getFetchParams(state);
|
|
|
|
const { metaData, value: swapTokenValue, slippage } = fetchParams;
|
|
|
|
const { sourceTokenInfo = {}, destinationTokenInfo = {} } = metaData;
|
|
|
|
const usedQuote = getUsedQuote(state);
|
2022-05-09 18:48:14 +02:00
|
|
|
const swapsNetworkConfig = getSwapsNetworkConfig(state);
|
2022-02-18 17:48:38 +01:00
|
|
|
const chainId = getCurrentChainId(state);
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
setSmartTransactionsRefreshInterval(
|
2022-05-09 18:48:14 +02:00
|
|
|
swapsNetworkConfig?.stxBatchStatusRefreshTime,
|
2022-02-18 17:48:38 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const usedTradeTxParams = usedQuote.trade;
|
|
|
|
|
|
|
|
// update stx with data
|
|
|
|
const destinationValue = calcTokenAmount(
|
|
|
|
usedQuote.destinationAmount,
|
|
|
|
destinationTokenInfo.decimals || 18,
|
|
|
|
).toPrecision(8);
|
|
|
|
const smartTransactionsOptInStatus = getSmartTransactionsOptInStatus(state);
|
|
|
|
const smartTransactionsEnabled = getSmartTransactionsEnabled(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const currentSmartTransactionsEnabled =
|
|
|
|
getCurrentSmartTransactionsEnabled(state);
|
2022-02-18 17:48:38 +01:00
|
|
|
const swapMetaData = {
|
|
|
|
token_from: sourceTokenInfo.symbol,
|
|
|
|
token_from_amount: String(swapTokenValue),
|
|
|
|
token_to: destinationTokenInfo.symbol,
|
|
|
|
token_to_amount: destinationValue,
|
|
|
|
slippage,
|
|
|
|
custom_slippage: slippage !== 2,
|
|
|
|
best_quote_source: getTopQuote(state)?.aggregator,
|
|
|
|
available_quotes: getQuotes(state)?.length,
|
|
|
|
other_quote_selected:
|
|
|
|
usedQuote.aggregator !== getTopQuote(state)?.aggregator,
|
|
|
|
other_quote_selected_source:
|
|
|
|
usedQuote.aggregator === getTopQuote(state)?.aggregator
|
|
|
|
? ''
|
|
|
|
: usedQuote.aggregator,
|
|
|
|
average_savings: usedQuote.savings?.total,
|
|
|
|
performance_savings: usedQuote.savings?.performance,
|
|
|
|
fee_savings: usedQuote.savings?.fee,
|
|
|
|
median_metamask_fee: usedQuote.savings?.medianMetaMaskFee,
|
|
|
|
stx_enabled: smartTransactionsEnabled,
|
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
2022-03-23 20:28:26 +01:00
|
|
|
...additionalTrackingParams,
|
2022-02-18 17:48:38 +01:00
|
|
|
};
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent({
|
2022-02-18 17:48:38 +01:00
|
|
|
event: 'STX Swap Started',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2022-02-18 17:48:38 +01:00
|
|
|
sensitiveProperties: swapMetaData,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isContractAddressValid(usedTradeTxParams.to, chainId)) {
|
|
|
|
captureMessage('Invalid contract address', {
|
|
|
|
extra: {
|
|
|
|
token_from: swapMetaData.token_from,
|
|
|
|
token_to: swapMetaData.token_to,
|
|
|
|
contract_address: usedTradeTxParams.to,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await dispatch(setSwapsErrorKey(SWAP_FAILED_ERROR));
|
|
|
|
history.push(SWAPS_ERROR_ROUTE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const approveTxParams = getApproveTxParams(state);
|
|
|
|
let approvalTxUuid;
|
2022-05-26 19:56:28 +02:00
|
|
|
let updatedApproveTxParams;
|
2022-02-18 17:48:38 +01:00
|
|
|
try {
|
|
|
|
if (approveTxParams) {
|
2022-05-26 19:56:28 +02:00
|
|
|
updatedApproveTxParams = {
|
2022-02-18 17:48:38 +01:00
|
|
|
...approveTxParams,
|
|
|
|
value: '0x0',
|
|
|
|
};
|
2022-05-26 19:56:28 +02:00
|
|
|
}
|
|
|
|
const fees = await dispatch(
|
2022-08-09 19:56:52 +02:00
|
|
|
fetchSwapsSmartTransactionFees({
|
2022-05-26 19:56:28 +02:00
|
|
|
unsignedTransaction,
|
2022-08-09 19:56:52 +02:00
|
|
|
approveTxParams: updatedApproveTxParams,
|
|
|
|
fallbackOnNotEnoughFunds: true,
|
|
|
|
}),
|
2022-05-26 19:56:28 +02:00
|
|
|
);
|
|
|
|
if (!fees) {
|
|
|
|
log.error('"fetchSwapsSmartTransactionFees" failed');
|
|
|
|
dispatch(setSwapsSTXSubmitLoading(false));
|
2023-05-25 19:30:10 +02:00
|
|
|
dispatch(setCurrentSmartTransactionsError(StxErrorTypes.unavailable));
|
2022-05-26 19:56:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-06-15 20:17:21 +02:00
|
|
|
addTokenTo(dispatch, state);
|
2022-05-26 19:56:28 +02:00
|
|
|
if (approveTxParams) {
|
2022-02-18 17:48:38 +01:00
|
|
|
updatedApproveTxParams.gas = `0x${decimalToHex(
|
2022-05-26 19:56:28 +02:00
|
|
|
fees.approvalTxFees?.gasLimit || 0,
|
2022-02-18 17:48:38 +01:00
|
|
|
)}`;
|
|
|
|
approvalTxUuid = await dispatch(
|
|
|
|
signAndSendSmartTransaction({
|
|
|
|
unsignedTransaction: updatedApproveTxParams,
|
2022-05-26 19:56:28 +02:00
|
|
|
smartTransactionFees: fees.approvalTxFees,
|
2022-02-18 17:48:38 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-03-10 04:17:57 +01:00
|
|
|
unsignedTransaction.gas = `0x${decimalToHex(
|
2022-05-26 19:56:28 +02:00
|
|
|
fees.tradeTxFees?.gasLimit || 0,
|
2022-03-10 04:17:57 +01:00
|
|
|
)}`;
|
2022-02-18 17:48:38 +01:00
|
|
|
const uuid = await dispatch(
|
|
|
|
signAndSendSmartTransaction({
|
|
|
|
unsignedTransaction,
|
2022-05-26 19:56:28 +02:00
|
|
|
smartTransactionFees: fees.tradeTxFees,
|
2022-02-18 17:48:38 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const destinationTokenAddress = destinationTokenInfo.address;
|
|
|
|
const destinationTokenDecimals = destinationTokenInfo.decimals;
|
|
|
|
const destinationTokenSymbol = destinationTokenInfo.symbol;
|
|
|
|
const sourceTokenSymbol = sourceTokenInfo.symbol;
|
|
|
|
await dispatch(
|
|
|
|
updateSmartTransaction(uuid, {
|
2022-04-26 19:07:39 +02:00
|
|
|
origin: ORIGIN_METAMASK,
|
2022-02-18 17:48:38 +01:00
|
|
|
destinationTokenAddress,
|
|
|
|
destinationTokenDecimals,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
sourceTokenSymbol,
|
|
|
|
swapMetaData,
|
|
|
|
swapTokenValue,
|
2023-01-18 15:47:29 +01:00
|
|
|
type: TransactionType.swap,
|
2022-02-18 17:48:38 +01:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
if (approvalTxUuid) {
|
|
|
|
await dispatch(
|
|
|
|
updateSmartTransaction(approvalTxUuid, {
|
2022-04-26 19:07:39 +02:00
|
|
|
origin: ORIGIN_METAMASK,
|
2023-01-18 15:47:29 +01:00
|
|
|
type: TransactionType.swapApproval,
|
2022-02-18 17:48:38 +01:00
|
|
|
sourceTokenSymbol,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
history.push(SMART_TRANSACTION_STATUS_ROUTE);
|
|
|
|
dispatch(setSwapsSTXSubmitLoading(false));
|
|
|
|
} catch (e) {
|
|
|
|
console.log('signAndSendSwapsSmartTransaction error', e);
|
|
|
|
const {
|
|
|
|
swaps: { isFeatureFlagLoaded },
|
|
|
|
} = getState();
|
|
|
|
if (e.message.startsWith('Fetch error:') && isFeatureFlagLoaded) {
|
|
|
|
const errorObj = parseSmartTransactionsError(e.message);
|
2022-08-09 19:56:52 +02:00
|
|
|
dispatch(setCurrentSmartTransactionsError(errorObj?.error));
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-23 20:28:26 +01:00
|
|
|
export const signAndSendTransactions = (
|
|
|
|
history,
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent,
|
2022-03-23 20:28:26 +01:00
|
|
|
additionalTrackingParams,
|
|
|
|
) => {
|
2020-10-06 20:28:38 +02:00
|
|
|
return async (dispatch, getState) => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const state = getState();
|
|
|
|
const chainId = getCurrentChainId(state);
|
2021-05-06 16:14:42 +02:00
|
|
|
const hardwareWalletUsed = isHardwareWallet(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const networkAndAccountSupports1559 =
|
|
|
|
checkNetworkAndAccountSupports1559(state);
|
2021-07-09 17:24:00 +02:00
|
|
|
let swapsLivenessForNetwork = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
try {
|
2021-07-09 17:24:00 +02:00
|
|
|
const swapsFeatureFlags = await fetchSwapsFeatureFlags();
|
|
|
|
swapsLivenessForNetwork = getSwapsLivenessForNetwork(
|
|
|
|
chainId,
|
2023-01-31 13:01:10 +01:00
|
|
|
swapsFeatureFlags,
|
2021-07-09 17:24:00 +02:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error('Failed to fetch Swaps liveness, defaulting to false.', error);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-07-09 17:24:00 +02:00
|
|
|
await dispatch(setSwapsLiveness(swapsLivenessForNetwork));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
if (!swapsLivenessForNetwork.swapsFeatureIsLive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await history.push(SWAPS_MAINTENANCE_ROUTE);
|
|
|
|
return;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customSwapsGas = getCustomSwapsGas(state);
|
2021-07-30 13:35:30 +02:00
|
|
|
const customMaxFeePerGas = getCustomMaxFeePerGas(state);
|
|
|
|
const customMaxPriorityFeePerGas = getCustomMaxPriorityFeePerGas(state);
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchParams = getFetchParams(state);
|
|
|
|
const { metaData, value: swapTokenValue, slippage } = fetchParams;
|
|
|
|
const { sourceTokenInfo = {}, destinationTokenInfo = {} } = metaData;
|
|
|
|
await dispatch(setBackgroundSwapRouteState('awaiting'));
|
|
|
|
await dispatch(stopPollingForQuotes());
|
2021-05-06 16:14:42 +02:00
|
|
|
|
|
|
|
if (!hardwareWalletUsed) {
|
|
|
|
history.push(AWAITING_SWAP_ROUTE);
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { fast: fastGasEstimate } = getSwapGasPriceEstimateData(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
let maxFeePerGas;
|
|
|
|
let maxPriorityFeePerGas;
|
|
|
|
let baseAndPriorityFeePerGas;
|
2021-08-07 00:18:53 +02:00
|
|
|
let decEstimatedBaseFee;
|
2021-07-30 13:35:30 +02:00
|
|
|
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
const {
|
|
|
|
high: { suggestedMaxFeePerGas, suggestedMaxPriorityFeePerGas },
|
|
|
|
estimatedBaseFee = '0',
|
|
|
|
} = getGasFeeEstimates(state);
|
2021-08-07 00:18:53 +02:00
|
|
|
decEstimatedBaseFee = decGWEIToHexWEI(estimatedBaseFee);
|
2021-07-30 13:35:30 +02:00
|
|
|
maxFeePerGas =
|
|
|
|
customMaxFeePerGas || decGWEIToHexWEI(suggestedMaxFeePerGas);
|
|
|
|
maxPriorityFeePerGas =
|
|
|
|
customMaxPriorityFeePerGas ||
|
|
|
|
decGWEIToHexWEI(suggestedMaxPriorityFeePerGas);
|
|
|
|
baseAndPriorityFeePerGas = addHexes(
|
2021-08-07 00:18:53 +02:00
|
|
|
decEstimatedBaseFee,
|
2021-07-30 13:35:30 +02:00
|
|
|
maxPriorityFeePerGas,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const usedQuote = getUsedQuote(state);
|
|
|
|
const usedTradeTxParams = usedQuote.trade;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const estimatedGasLimit = new BigNumber(
|
|
|
|
usedQuote?.gasEstimate || `0x0`,
|
|
|
|
16,
|
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const estimatedGasLimitWithMultiplier = estimatedGasLimit
|
2020-12-15 18:16:51 +01:00
|
|
|
.times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10)
|
2020-11-03 00:41:28 +01:00
|
|
|
.round(0)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(16);
|
2020-11-03 00:41:28 +01:00
|
|
|
const maxGasLimit =
|
|
|
|
customSwapsGas ||
|
2020-12-15 18:16:51 +01:00
|
|
|
(usedQuote?.gasEstimate
|
|
|
|
? estimatedGasLimitWithMultiplier
|
2021-02-04 19:15:23 +01:00
|
|
|
: `0x${decimalToHex(usedQuote?.maxGas || 0)}`);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const usedGasPrice = getUsedSwapsGasPrice(state);
|
|
|
|
usedTradeTxParams.gas = maxGasLimit;
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
usedTradeTxParams.maxFeePerGas = maxFeePerGas;
|
|
|
|
usedTradeTxParams.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
|
|
delete usedTradeTxParams.gasPrice;
|
|
|
|
} else {
|
|
|
|
usedTradeTxParams.gasPrice = usedGasPrice;
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const usdConversionRate = getUSDConversionRate(state);
|
2020-11-03 00:41:28 +01:00
|
|
|
const destinationValue = calcTokenAmount(
|
|
|
|
usedQuote.destinationAmount,
|
|
|
|
destinationTokenInfo.decimals || 18,
|
2021-02-04 19:15:23 +01:00
|
|
|
).toPrecision(8);
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedGasLimitEstimate =
|
|
|
|
usedQuote?.gasEstimateWithRefund ||
|
2021-02-04 19:15:23 +01:00
|
|
|
`0x${decimalToHex(usedQuote?.averageGas || 0)}`;
|
2020-11-03 00:41:28 +01:00
|
|
|
const totalGasLimitEstimate = new BigNumber(usedGasLimitEstimate, 16)
|
|
|
|
.plus(usedQuote.approvalNeeded?.gas || '0x0', 16)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(16);
|
2020-11-10 18:39:45 +01:00
|
|
|
const gasEstimateTotalInUSD = getValueFromWeiHex({
|
2021-07-30 13:35:30 +02:00
|
|
|
value: calcGasTotal(
|
|
|
|
totalGasLimitEstimate,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559 ? baseAndPriorityFeePerGas : usedGasPrice,
|
2021-07-30 13:35:30 +02:00
|
|
|
),
|
2020-10-06 20:28:38 +02:00
|
|
|
toCurrency: 'usd',
|
2020-11-10 18:39:45 +01:00
|
|
|
conversionRate: usdConversionRate,
|
2020-10-06 20:28:38 +02:00
|
|
|
numberOfDecimals: 6,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2022-02-18 17:48:38 +01:00
|
|
|
const smartTransactionsOptInStatus = getSmartTransactionsOptInStatus(state);
|
|
|
|
const smartTransactionsEnabled = getSmartTransactionsEnabled(state);
|
2022-07-31 20:26:40 +02:00
|
|
|
const currentSmartTransactionsEnabled =
|
|
|
|
getCurrentSmartTransactionsEnabled(state);
|
2020-10-06 20:28:38 +02:00
|
|
|
const swapMetaData = {
|
|
|
|
token_from: sourceTokenInfo.symbol,
|
|
|
|
token_from_amount: String(swapTokenValue),
|
|
|
|
token_to: destinationTokenInfo.symbol,
|
|
|
|
token_to_amount: destinationValue,
|
|
|
|
slippage,
|
|
|
|
custom_slippage: slippage !== 2,
|
|
|
|
best_quote_source: getTopQuote(state)?.aggregator,
|
|
|
|
available_quotes: getQuotes(state)?.length,
|
2020-11-03 00:41:28 +01:00
|
|
|
other_quote_selected:
|
|
|
|
usedQuote.aggregator !== getTopQuote(state)?.aggregator,
|
|
|
|
other_quote_selected_source:
|
|
|
|
usedQuote.aggregator === getTopQuote(state)?.aggregator
|
|
|
|
? ''
|
|
|
|
: usedQuote.aggregator,
|
2020-11-10 18:39:45 +01:00
|
|
|
gas_fees: gasEstimateTotalInUSD,
|
2020-10-22 22:04:34 +02:00
|
|
|
estimated_gas: estimatedGasLimit.toString(10),
|
2020-11-04 17:14:08 +01:00
|
|
|
suggested_gas_price: fastGasEstimate,
|
|
|
|
used_gas_price: hexWEIToDecGWEI(usedGasPrice),
|
2020-11-09 20:49:41 +01:00
|
|
|
average_savings: usedQuote.savings?.total,
|
|
|
|
performance_savings: usedQuote.savings?.performance,
|
|
|
|
fee_savings: usedQuote.savings?.fee,
|
|
|
|
median_metamask_fee: usedQuote.savings?.medianMetaMaskFee,
|
2021-05-13 21:26:08 +02:00
|
|
|
is_hardware_wallet: hardwareWalletUsed,
|
|
|
|
hardware_wallet_type: getHardwareWalletType(state),
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_enabled: smartTransactionsEnabled,
|
2022-03-23 20:28:26 +01:00
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
2022-03-23 20:28:26 +01:00
|
|
|
...additionalTrackingParams,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
swapMetaData.max_fee_per_gas = maxFeePerGas;
|
|
|
|
swapMetaData.max_priority_fee_per_gas = maxPriorityFeePerGas;
|
|
|
|
swapMetaData.base_and_priority_fee_per_gas = baseAndPriorityFeePerGas;
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-03-29 19:19:40 +02:00
|
|
|
trackEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'Swap Started',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2020-12-02 22:41:30 +01:00
|
|
|
sensitiveProperties: swapMetaData,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
if (!isContractAddressValid(usedTradeTxParams.to, chainId)) {
|
2021-04-23 16:53:10 +02:00
|
|
|
captureMessage('Invalid contract address', {
|
|
|
|
extra: {
|
|
|
|
token_from: swapMetaData.token_from,
|
|
|
|
token_to: swapMetaData.token_to,
|
|
|
|
contract_address: usedTradeTxParams.to,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await dispatch(setSwapsErrorKey(SWAP_FAILED_ERROR));
|
|
|
|
history.push(SWAPS_ERROR_ROUTE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let finalApproveTxMeta;
|
|
|
|
const approveTxParams = getApproveTxParams(state);
|
2021-05-06 16:14:42 +02:00
|
|
|
|
|
|
|
// For hardware wallets we go to the Awaiting Signatures page first and only after a user
|
|
|
|
// completes 1 or 2 confirmations, we redirect to the Awaiting Swap page.
|
|
|
|
if (hardwareWalletUsed) {
|
|
|
|
history.push(AWAITING_SIGNATURES_ROUTE);
|
|
|
|
}
|
|
|
|
|
2023-06-15 20:17:21 +02:00
|
|
|
addTokenTo(dispatch, state);
|
2020-10-06 20:28:38 +02:00
|
|
|
if (approveTxParams) {
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
approveTxParams.maxFeePerGas = maxFeePerGas;
|
|
|
|
approveTxParams.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
|
|
delete approveTxParams.gasPrice;
|
|
|
|
}
|
2023-06-13 11:17:32 +02:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
try {
|
2023-07-25 10:50:55 +02:00
|
|
|
finalApproveTxMeta = await addTransactionAndWaitForPublish(
|
2023-06-13 11:17:32 +02:00
|
|
|
{ ...approveTxParams, amount: '0x0' },
|
|
|
|
{
|
|
|
|
requireApproval: false,
|
2023-07-25 10:50:55 +02:00
|
|
|
type: TransactionType.swapApproval,
|
2023-06-13 11:17:32 +02:00
|
|
|
swaps: {
|
|
|
|
hasApproveTx: true,
|
|
|
|
meta: {
|
|
|
|
type: TransactionType.swapApproval,
|
|
|
|
sourceTokenSymbol: sourceTokenInfo.symbol,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
} catch (e) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await dispatch(setSwapsErrorKey(SWAP_FAILED_ERROR));
|
|
|
|
history.push(SWAPS_ERROR_ROUTE);
|
|
|
|
return;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-07-25 10:50:55 +02:00
|
|
|
await addTransactionAndWaitForPublish(usedTradeTxParams, {
|
|
|
|
requireApproval: false,
|
|
|
|
type: TransactionType.swap,
|
|
|
|
swaps: {
|
|
|
|
hasApproveTx: Boolean(approveTxParams),
|
|
|
|
meta: {
|
|
|
|
estimatedBaseFee: decEstimatedBaseFee,
|
|
|
|
sourceTokenSymbol: sourceTokenInfo.symbol,
|
|
|
|
destinationTokenSymbol: destinationTokenInfo.symbol,
|
|
|
|
type: TransactionType.swap,
|
|
|
|
destinationTokenDecimals: destinationTokenInfo.decimals,
|
|
|
|
destinationTokenAddress: destinationTokenInfo.address,
|
|
|
|
swapMetaData,
|
|
|
|
swapTokenValue,
|
|
|
|
approvalTxId: finalApproveTxMeta?.id,
|
2023-06-13 11:17:32 +02:00
|
|
|
},
|
|
|
|
},
|
2023-07-25 10:50:55 +02:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
} catch (e) {
|
2021-05-13 21:49:00 +02:00
|
|
|
const errorKey = e.message.includes('EthAppPleaseEnableContractData')
|
|
|
|
? CONTRACT_DATA_DISABLED_ERROR
|
|
|
|
: SWAP_FAILED_ERROR;
|
2023-06-13 11:17:32 +02:00
|
|
|
console.error(e);
|
2021-05-13 21:49:00 +02:00
|
|
|
await dispatch(setSwapsErrorKey(errorKey));
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(SWAPS_ERROR_ROUTE);
|
|
|
|
return;
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 16:14:42 +02:00
|
|
|
// Only after a user confirms swapping on a hardware wallet (second `updateAndApproveTx` call above),
|
|
|
|
// we redirect to the Awaiting Swap page.
|
|
|
|
if (hardwareWalletUsed) {
|
|
|
|
history.push(AWAITING_SWAP_ROUTE);
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await forceUpdateMetamaskState(dispatch);
|
|
|
|
};
|
|
|
|
};
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
export function fetchMetaSwapsGasPriceEstimates() {
|
|
|
|
return async (dispatch, getState) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = getState();
|
2021-03-28 15:18:44 +02:00
|
|
|
const chainId = getCurrentChainId(state);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapGasPriceEstimatesFetchStarted());
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let priceEstimates;
|
2020-11-04 17:14:08 +01:00
|
|
|
try {
|
2021-11-30 19:56:28 +01:00
|
|
|
priceEstimates = await fetchSwapsGasPrices(chainId);
|
2020-11-04 17:14:08 +01:00
|
|
|
} catch (e) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.warn('Fetching swaps gas prices failed:', e);
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
if (!e.message?.match(/NetworkError|Fetch failed with status:/u)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw e;
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(swapGasPriceEstimatesFetchFailed());
|
2020-11-04 17:14:08 +01:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const gasPrice = await global.ethQuery.gasPrice();
|
|
|
|
const gasPriceInDecGWEI = hexWEIToDecGWEI(gasPrice.toString(10));
|
2020-11-04 17:14:08 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(retrievedFallbackSwapsGasPrice(gasPriceInDecGWEI));
|
|
|
|
return null;
|
2020-11-04 17:14:08 +01:00
|
|
|
} catch (networkGasPriceError) {
|
|
|
|
console.error(
|
|
|
|
`Failed to retrieve fallback gas price: `,
|
|
|
|
networkGasPriceError,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return null;
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
swapGasPriceEstimatesFetchCompleted({
|
|
|
|
priceEstimates,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return priceEstimates;
|
|
|
|
};
|
2020-11-04 17:14:08 +01:00
|
|
|
}
|
2022-02-18 17:48:38 +01:00
|
|
|
|
2022-08-09 19:56:52 +02:00
|
|
|
export function fetchSwapsSmartTransactionFees({
|
2022-02-18 17:48:38 +01:00
|
|
|
unsignedTransaction,
|
|
|
|
approveTxParams,
|
2022-08-09 19:56:52 +02:00
|
|
|
fallbackOnNotEnoughFunds = false,
|
|
|
|
}) {
|
2022-02-18 17:48:38 +01:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const {
|
2022-05-26 19:56:28 +02:00
|
|
|
swaps: { isFeatureFlagLoaded },
|
2022-02-18 17:48:38 +01:00
|
|
|
} = getState();
|
|
|
|
try {
|
2022-05-26 19:56:28 +02:00
|
|
|
return await dispatch(
|
|
|
|
fetchSmartTransactionFees(unsignedTransaction, approveTxParams),
|
2022-02-18 17:48:38 +01:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message.startsWith('Fetch error:') && isFeatureFlagLoaded) {
|
|
|
|
const errorObj = parseSmartTransactionsError(e.message);
|
2022-08-09 19:56:52 +02:00
|
|
|
if (
|
|
|
|
fallbackOnNotEnoughFunds ||
|
2023-05-25 19:30:10 +02:00
|
|
|
errorObj?.error !== StxErrorTypes.notEnoughFunds
|
2022-08-09 19:56:52 +02:00
|
|
|
) {
|
|
|
|
dispatch(setCurrentSmartTransactionsError(errorObj?.error));
|
|
|
|
}
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 19:56:28 +02:00
|
|
|
return null;
|
2022-02-18 17:48:38 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cancelSwapsSmartTransaction(uuid) {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
try {
|
|
|
|
await dispatch(cancelSmartTransaction(uuid));
|
|
|
|
} catch (e) {
|
|
|
|
const {
|
|
|
|
swaps: { isFeatureFlagLoaded },
|
|
|
|
} = getState();
|
|
|
|
if (e.message.startsWith('Fetch error:') && isFeatureFlagLoaded) {
|
|
|
|
const errorObj = parseSmartTransactionsError(e.message);
|
2022-08-09 19:56:52 +02:00
|
|
|
dispatch(setCurrentSmartTransactionsError(errorObj?.error));
|
2022-02-18 17:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|