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 f0c6e60
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 163 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
2 changes: 1 addition & 1 deletion app/components/UI/AssetOverview/AssetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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 SwapsController from '@metamask-previews/swaps-controller';
import { MetaMetricsEvents } from '../../../core/Analytics';
import { getDecimalChainId } from '../../../util/networks';
import { useMetrics } from '../../../components/hooks/useMetrics';
Expand Down
17 changes: 13 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 @@ -305,15 +306,17 @@ async function resetAndStartPolling({
return;
}
const { SwapsController } = Engine.context;
const networkClientId = selectSelectedNetworkClientId();

const fetchParams = getFetchParams({
slippage,
sourceToken,
destinationToken,
sourceAmount,
walletAddress,
networkClientId,
});
await SwapsController.stopPollingAndResetState();
await SwapsController.stopPollingAndResetState;
await SwapsController.startFetchAndSetQuotes(
fetchParams,
fetchParams.metaData,
Expand Down Expand Up @@ -1011,7 +1014,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 +2161,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
8 changes: 6 additions & 2 deletions app/components/UI/Swaps/components/LoadingAnimation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from 'react';
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 @@ -317,9 +318,12 @@ function LoadingAnimation({
return;
}
if (!aggregatorMetadata) {
const { SwapsController } = Engine.context;
const networkClientId = selectSelectedNetworkClientId();
try {
const { SwapsController } = Engine.context;
await SwapsController.fetchAggregatorMetadataWithCache();
await SwapsController.fetchAggregatorMetadataWithCache({
networkClientId,
});
} catch (error) {
Logger.error(
error,
Expand Down
13 changes: 9 additions & 4 deletions app/components/UI/Swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -294,9 +295,12 @@ function SwapsAmountView({
useEffect(() => {
(async () => {
const { SwapsController } = Engine.context;
const networkClientId = selectSelectedNetworkClientId();
try {
await SwapsController.fetchAggregatorMetadataWithCache();
await SwapsController.fetchTopAssetsWithCache();
await SwapsController.fetchAggregatorMetadataWithCache({
networkClientId,
});
await SwapsController.fetchTopAssetsWithCache({ networkClientId });
} catch (error) {
Logger.error(
error,
Expand All @@ -309,6 +313,7 @@ function SwapsAmountView({
useEffect(() => {
(async () => {
const { SwapsController } = Engine.context;
const networkClientId = selectSelectedNetworkClientId();
try {
if (
!swapsControllerTokens ||
Expand All @@ -318,7 +323,7 @@ function SwapsAmountView({
setInitialLoadingTokens(true);
}
setLoadingTokens(true);
await SwapsController.fetchTokenWithCache();
await SwapsController.fetchTokenWithCache({ networkClientId });
setLoadingTokens(false);
setInitialLoadingTokens(false);
} catch (error) {
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
21 changes: 5 additions & 16 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ import {
import SwapsController, {
swapsUtils,
SwapsControllerState,
} from '@metamask/swaps-controller';
} from '@metamask-previews/swaps-controller';
import {
PPOMController,
PPOMControllerActions,
Expand Down Expand Up @@ -1626,24 +1626,21 @@ export class Engine {
swapsUtils.LINEA_CHAIN_ID,
swapsUtils.BASE_CHAIN_ID,
],
// @ts-expect-error TODO: Resolve new typing for restricted controller messenger
messenger: this.controllerMessenger.getRestricted({
name: 'SwapsController',
// TODO: allow these internal calls once GasFeeController
// export these action types and register its action handlers
// allowedActions: [
// 'GasFeeController:getEIP1559GasFeeEstimates',
// ],
allowedActions: [
'NetworkController:findNetworkClientIdByChainId',
'NetworkController:getNetworkClientById',
],
allowedEvents: [],
allowedActions: ['NetworkController:getNetworkClientById'],
allowedEvents: ['NetworkController:networkDidChange'],
}),
// TODO: Remove once GasFeeController exports this action type
fetchGasFeeEstimates: () => gasFeeController.fetchGasFeeEstimates(),
// @ts-expect-error TODO: Resolve mismatch between gas fee and swaps controller types
fetchEstimatedMultiLayerL1Fee,
pollCountLimit: AppConstants.SWAPS.POLL_COUNT_LIMIT,
}),
gasFeeController,
approvalController,
Expand Down Expand Up @@ -1924,8 +1921,7 @@ export class Engine {
}

configureControllersOnNetworkChange() {
const { AccountTrackerController, NetworkController, SwapsController } =
this.context;
const { AccountTrackerController, NetworkController } = this.context;
const { provider } = NetworkController.getProviderAndBlockTracker();

// Skip configuration if this is called before the provider is initialized
Expand All @@ -1934,13 +1930,6 @@ export class Engine {
}
provider.sendAsync = provider.sendAsync.bind(provider);

// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
SwapsController.setProvider(provider, {
chainId: NetworkController.getNetworkClientById(
NetworkController?.state.selectedNetworkClientId,
).configuration.chainId,
pollCountLimit: AppConstants.SWAPS.POLL_COUNT_LIMIT,
});
AccountTrackerController.refresh();
}

Expand Down
4 changes: 2 additions & 2 deletions app/reducers/swaps/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FeatureFlags } from '@metamask/swaps-controller/dist/types';
import { FeatureFlags } from '@metamask-previews/swaps-controller/dist/types';
import Device from '../../util/device';
import { CHAIN_ID_TO_NAME_MAP } from '@metamask/swaps-controller/dist/constants';
import { CHAIN_ID_TO_NAME_MAP } from '@metamask-previews/swaps-controller/dist/constants';

export const getChainFeatureFlags = (
featureFlags: FeatureFlags,
Expand Down
5 changes: 3 additions & 2 deletions app/selectors/networkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ export const selectNetworkStatus = createSelector(

export const selectNetworkConfigurations = createSelector(
selectNetworkControllerState,
(networkControllerState: NetworkState) =>
networkControllerState.networkConfigurationsByChainId,
(networkControllerState: NetworkState) => {
return networkControllerState.networkConfigurationsByChainId;
},
);

export const selectNetworkClientId = createSelector(
Expand Down
2 changes: 1 addition & 1 deletion app/selectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TokensControllerState,
TokenBalancesControllerState,
} from '@metamask/assets-controllers';
import SwapsController from '@metamask/swaps-controller';
import SwapsController from '@metamask-previews/swaps-controller';
import { NetworkState } from '@metamask/network-controller';
import { AddressBookControllerState } from '@metamask/address-book-controller';
import { BaseState } from '@metamask/base-controller';
Expand Down
Loading

0 comments on commit f0c6e60

Please sign in to comment.