Skip to content

Commit

Permalink
WIP - Bump swaps-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Nov 21, 2024
1 parent fec0c4e commit 58fe119
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 170 deletions.
2 changes: 1 addition & 1 deletion app/components/Nav/Main/RootRPCMethodsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { BN } from 'ethereumjs-util';
import Logger from '../../../util/Logger';
import TransactionTypes from '../../../core/TransactionTypes';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { query } from '@metamask/controller-utils';
import BigNumber from 'bignumber.js';
import { toLowerCaseEquals } from '../../../util/general';
Expand Down
13 changes: 7 additions & 6 deletions app/components/UI/AssetOverview/AssetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AppConstants from '../../../core/AppConstants';
import Engine from '../../../core/Engine';
import {
selectChainId,
selectSelectedNetworkClientId,
selectTicker,
} from '../../../selectors/networkController';
import {
Expand Down Expand Up @@ -45,7 +46,6 @@ import Routes from '../../../constants/navigation/Routes';
import TokenDetails from './TokenDetails';
import { RootState } from '../../../reducers';
import useGoToBridge from '../Bridge/utils/useGoToBridge';
import SwapsController from '@metamask/swaps-controller';
import { MetaMetricsEvents } from '../../../core/Analytics';
import { getDecimalChainId } from '../../../util/networks';
import { useMetrics } from '../../../components/hooks/useMetrics';
Expand Down Expand Up @@ -84,6 +84,7 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
const tokenBalances = useSelector(selectContractBalances);
const chainId = useSelector((state: RootState) => selectChainId(state));
const ticker = useSelector((state: RootState) => selectTicker(state));
const selectedNetworkClientId = useSelector(selectSelectedNetworkClientId);

const { data: prices = [], isLoading } = useTokenHistoricalPrices({
address: asset.isETH ? zeroAddress() : asset.address,
Expand All @@ -96,12 +97,12 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
const dispatch = useDispatch();

useEffect(() => {
const { SwapsController: SwapsControllerFromEngine } = Engine.context as {
SwapsController: SwapsController;
};
const { SwapsController } = Engine.context;
const fetchTokenWithCache = async () => {
try {
await SwapsControllerFromEngine.fetchTokenWithCache();
await SwapsController.fetchTokenWithCache({
networkClientId: selectedNetworkClientId,
});
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
Expand All @@ -112,7 +113,7 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
}
};
fetchTokenWithCache();
}, []);
}, [selectedNetworkClientId]);

const onReceive = () => {
navigation.navigate(Routes.QR_TAB_SWITCHER, {
Expand Down
21 changes: 17 additions & 4 deletions app/components/UI/Swaps/QuotesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import IonicIcon from 'react-native-vector-icons/Ionicons';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import BigNumber from 'bignumber.js';
import { useNavigation, useRoute } from '@react-navigation/native';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import {
WalletDevice,
TransactionStatus,
Expand Down Expand Up @@ -95,6 +95,7 @@ import {
} from '../../../util/address';
import {
selectChainId,
selectSelectedNetworkClientId,
selectTicker,
} from '../../../selectors/networkController';
import {
Expand Down Expand Up @@ -300,6 +301,7 @@ async function resetAndStartPolling({
destinationToken,
sourceAmount,
walletAddress,
networkClientId,
}) {
if (!sourceToken || !destinationToken) {
return;
Expand All @@ -312,8 +314,9 @@ async function resetAndStartPolling({
destinationToken,
sourceAmount,
walletAddress,
networkClientId,
});
await SwapsController.stopPollingAndResetState();
await SwapsController.stopPollingAndResetState;
await SwapsController.startFetchAndSetQuotes(
fetchParams,
fetchParams.metaData,
Expand Down Expand Up @@ -764,6 +767,8 @@ function SwapsQuotesView({
}
}, [error, navigation]);

const selectedNetworkClientId = selectSelectedNetworkClientId();

const handleRetryFetchQuotes = useCallback(() => {
if (error?.key === swapsUtils.SwapsError.QUOTES_EXPIRED_ERROR) {
navigation.setParams({ leftAction: strings('navigation.back') });
Expand All @@ -778,6 +783,7 @@ function SwapsQuotesView({
destinationToken,
sourceAmount,
walletAddress: selectedAddress,
networkClientId: selectedNetworkClientId,
});
} else {
navigation.pop();
Expand All @@ -790,6 +796,7 @@ function SwapsQuotesView({
sourceAmount,
selectedAddress,
navigation,
selectedNetworkClientId,
]);

const updateSwapsTransactions = useCallback(
Expand Down Expand Up @@ -1011,7 +1018,9 @@ function SwapsQuotesView({
CHAIN_IDS.LINEA_SEPOLIA,
].includes(chainId)
) {
Logger.log('Delaying submitting trade tx to make Linea confirmation more likely',);
Logger.log(
'Delaying submitting trade tx to make Linea confirmation more likely',
);
const waitPromise = new Promise((resolve) =>
setTimeout(resolve, 5000),
);
Expand Down Expand Up @@ -2156,7 +2165,11 @@ function SwapsQuotesView({
</QuotesSummary.Body>
</QuotesSummary>
)}
<StyledButton type="confirm" onPress={handleCompleteSwap} disabled={unableToSwap || isHandlingSwap || isAnimating}>
<StyledButton
type="confirm"
onPress={handleCompleteSwap}
disabled={unableToSwap || isHandlingSwap || isAnimating}
>
{strings('swaps.swap')}
</StyledButton>
<TouchableOpacity onPress={handleTermsPress} style={styles.termsButton}>
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/Swaps/SwapsLiveness.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { useCallback, useEffect } from 'react';
import { selectChainId } from '../../../selectors/networkController';
import { AppState } from 'react-native';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StyleSheet } from 'react-native';
import { connect } from 'react-redux';
import Modal from 'react-native-modal';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';

import EditPermission from '../../../Views/confirmations/components/ApproveTransactionReview/EditPermission';
import { fromTokenMinimalUnitString, hexToBN } from '../../../../util/number';
Expand Down
12 changes: 9 additions & 3 deletions app/components/UI/Swaps/components/LoadingAnimation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import React, {
useRef,
useState,
} from 'react';
import { useSelector } from 'react-redux';
import { Animated, View, StyleSheet, Image } from 'react-native';
import PropTypes from 'prop-types';
import { selectSelectedNetworkClientId } from '../../../../../selectors/networkController';
import Engine from '../../../../../core/Engine';
import Logger from '../../../../../util/Logger';
import Device from '../../../../../util/device';
Expand Down Expand Up @@ -129,6 +131,8 @@ function LoadingAnimation({
const [renderLogos, setRenderLogos] = useState(false);
const [currentQuoteIndex, setCurrentQuoteIndex] = useState(0);

const selectedNetworkClientId = useSelector(selectSelectedNetworkClientId);

/* References */
const foxRef = useRef();
const foxHeadPan = useRef(new Animated.ValueXY(0, 0)).current;
Expand Down Expand Up @@ -317,9 +321,11 @@ function LoadingAnimation({
return;
}
if (!aggregatorMetadata) {
const { SwapsController } = Engine.context;
try {
const { SwapsController } = Engine.context;
await SwapsController.fetchAggregatorMetadataWithCache();
await SwapsController.fetchAggregatorMetadataWithCache({
networkClientId: selectedNetworkClientId,
});
} catch (error) {
Logger.error(
error,
Expand All @@ -337,7 +343,7 @@ function LoadingAnimation({
setShouldStart(true);
}
})();
}, [aggregatorMetadata, hasStarted]);
}, [aggregatorMetadata, hasStarted, selectedNetworkClientId]);

/* Delay the logos rendering to avoid navigation transition lag */
useEffect(() => {
Expand Down
23 changes: 16 additions & 7 deletions app/components/UI/Swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TouchableOpacity,
InteractionManager,
} from 'react-native';
import { connect } from 'react-redux';
import { connect, useSelector } from 'react-redux';
import { useNavigation, useRoute } from '@react-navigation/native';
import { View as AnimatableView } from 'react-native-animatable';
import IonicIcon from 'react-native-vector-icons/Ionicons';
Expand All @@ -27,7 +27,7 @@ import {
safeNumberToBN,
} from '../../../util/number';
import { safeToChecksumAddress } from '../../../util/address';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { MetaMetricsEvents } from '../../../core/Analytics';

import {
Expand Down Expand Up @@ -70,6 +70,7 @@ import {
selectChainId,
selectNetworkConfigurations,
selectProviderConfig,
selectSelectedNetworkClientId,
} from '../../../selectors/networkController';
import {
selectConversionRate,
Expand Down Expand Up @@ -247,6 +248,8 @@ function SwapsAmountView({
hideTokenVerificationModal,
] = useModalHandler(false);

const selectedNetworkClientId = useSelector(selectSelectedNetworkClientId);

useEffect(() => {
navigation.setOptions(getSwapsAmountNavbar(navigation, route, colors));
}, [navigation, route, colors]);
Expand Down Expand Up @@ -295,16 +298,20 @@ function SwapsAmountView({
(async () => {
const { SwapsController } = Engine.context;
try {
await SwapsController.fetchAggregatorMetadataWithCache();
await SwapsController.fetchTopAssetsWithCache();
await SwapsController.fetchAggregatorMetadataWithCache({
networkClientId: selectSelectedNetworkClientId,
});
await SwapsController.fetchTopAssetsWithCache({
networkClientId: selectSelectedNetworkClientId,
});
} catch (error) {
Logger.error(
error,
'Swaps: Error while updating agg metadata and top assets in amount view',
);
}
})();
}, []);
}, [selectedNetworkClientId]);

useEffect(() => {
(async () => {
Expand All @@ -318,7 +325,9 @@ function SwapsAmountView({
setInitialLoadingTokens(true);
}
setLoadingTokens(true);
await SwapsController.fetchTokenWithCache();
await SwapsController.fetchTokenWithCache({
networkClientId: selectedNetworkClientId,
});
setLoadingTokens(false);
setInitialLoadingTokens(false);
} catch (error) {
Expand All @@ -331,7 +340,7 @@ function SwapsAmountView({
setInitialLoadingTokens(false);
}
})();
}, [swapsControllerTokens, swapsTokens]);
}, [swapsControllerTokens, swapsTokens, selectedNetworkClientId]);

const canSetAnInitialSourceToken =
!isSourceSet &&
Expand Down
5 changes: 4 additions & 1 deletion app/components/UI/Swaps/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react';
import BigNumber from 'bignumber.js';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { strings } from '../../../../../locales/i18n';
import AppConstants from '../../../../core/AppConstants';
import { NETWORKS_CHAIN_ID } from '../../../../constants/network';
Expand Down Expand Up @@ -118,13 +118,15 @@ export function getQuotesNavigationsParams(route) {
* @param {object} options.destinationToken destinationToken object from tokens API
* @param {string} sourceAmount Amount in minimal token units of sourceToken to be swapped
* @param {string} fromAddress Current address attempting to swap
* @param {string} networkClientId Current network client ID
*/
export function getFetchParams({
slippage = 1,
sourceToken,
destinationToken,
sourceAmount,
walletAddress,
networkClientId,
}) {
return {
slippage,
Expand All @@ -135,6 +137,7 @@ export function getFetchParams({
metaData: {
sourceTokenInfo: sourceToken,
destinationTokenInfo: destinationToken,
networkClientId,
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/Swaps/utils/useFetchTokenMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';

const defaultTokenMetadata = {
valid: null,
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/TransactionElement/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
calculateEIP1559GasFeeHexes,
} from '../../../util/transactions';
import { toChecksumAddress } from 'ethereumjs-util';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { isSwapsNativeAsset } from '../Swaps/utils';
import { toLowerCaseEquals } from '../../../util/general';
import Engine from '../../../core/Engine';
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/Asset/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { swapsUtils } from '@metamask/swaps-controller/';
import { swapsUtils } from '@metamask-previews/swaps-controller/';
import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import {
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/WalletActions/WalletActions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Third party dependencies.
import React, { useCallback, useMemo, useRef } from 'react';
import { View } from 'react-native';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { useDispatch, useSelector } from 'react-redux';
import { useNavigation } from '@react-navigation/native';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import Routes from '../../../../../constants/navigation/Routes';
import { getRampNetworks } from '../../../../../reducers/fiatOrders';
import { swapsLivenessSelector } from '../../../../../reducers/swaps';
import { isSwapsAllowed } from '../../../../UI/Swaps/utils';
import { swapsUtils } from '@metamask/swaps-controller';
import { swapsUtils } from '@metamask-previews/swaps-controller';
import { regex } from '../../../../../util/regex';
import { AmountViewSelectorsIDs } from '../../../../../../e2e/selectors/SendFlow/AmountView.selectors';
import { isNetworkRampNativeTokenSupported } from '../../../../../components/UI/Ramp/utils';
Expand Down
Loading

0 comments on commit 58fe119

Please sign in to comment.