Skip to content

Commit

Permalink
Fix inconsistencies between swaps store slippage and shared value (#6399
Browse files Browse the repository at this point in the history
)

* fix inconsistent slippage between swaps store and shared values

* fix initial swaps store value being stale from incoming initial params
  • Loading branch information
walmat authored Jan 22, 2025
1 parent 59b2f22 commit 7c716cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/__swaps__/screens/Swap/navigateToSwaps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { GasSpeed } from '@/__swaps__/types/gas';
import { Navigation } from '@/navigation';
import store from '@/redux/store';
import { SwapsState, useSwapsStore } from '@/state/swaps/swapsStore';
import { SwapsState, swapsStore, useSwapsStore } from '@/state/swaps/swapsStore';
import { setSelectedGasSpeed } from './hooks/useSelectedGas';
import { enableActionsOnReadOnlyWallet } from '@/config';
import walletTypes from '@/helpers/walletTypes';
import { watchingAlert } from '@/utils';
import Routes from '@/navigation/routesNames';
import { getDefaultSlippage, slippageInBipsToString } from '@/__swaps__/utils/swaps';
import { ChainId } from '@/state/backendNetworks/types';
import { getRemoteConfig } from '@/model/remoteConfig';

export type SwapsParams = Partial<
Pick<SwapsState, 'inputAsset' | 'outputAsset' | 'percentageToSell' | 'slippage'> & {
Expand Down Expand Up @@ -38,18 +41,27 @@ const getInputMethod = (params: SwapsParams) => {
return 'inputAmount';
};
export function getSwapsNavigationParams() {
const state = useSwapsStore.getState();
const params = (Navigation.getActiveRoute().params || {}) as SwapsParams;

const inputMethod = getInputMethod(params);
const inputAsset = params.inputAsset || state.inputAsset;
const outputAsset = params.outputAsset || state.outputAsset;
const chainId = inputAsset?.chainId || ChainId.mainnet;
const lastTypedInput = inputMethod === 'slider' ? 'inputAmount' : inputMethod;
const slippage =
params.slippage && !isNaN(+params.slippage) ? slippageInBipsToString(+params.slippage) : getDefaultSlippage(chainId, getRemoteConfig());

// Set the slippage in the swaps store to keep it in sync with the initial value
swapsStore.getState().setSlippage(slippage);

const state = useSwapsStore.getState();
return {
inputMethod,
lastTypedInput,
focusedInput: lastTypedInput,
inputAsset: params.inputAsset || state.inputAsset,
outputAsset: params.outputAsset || state.outputAsset,
inputAsset,
outputAsset,
slippage,
...params,
} as const;
}
6 changes: 2 additions & 4 deletions src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { userAssetsQueryKey } from '@/__swaps__/screens/Swap/resources/assets/us
import { AddressOrEth, ExtendedAnimatedAssetWithColors, ParsedSearchAsset } from '@/__swaps__/types/assets';
import { ChainId } from '@/state/backendNetworks/types';
import { SwapAssetType, inputKeys } from '@/__swaps__/types/swap';
import { clamp, getDefaultSlippageWorklet, parseAssetAndExtend } from '@/__swaps__/utils/swaps';
import { clamp, parseAssetAndExtend } from '@/__swaps__/utils/swaps';
import { analyticsV2 } from '@/analytics';
import { LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';
import { getProvider } from '@/handlers/web3';
Expand All @@ -49,13 +49,11 @@ import { haptics } from '@/utils';
import { CrosschainQuote, Quote, QuoteError, SwapType } from '@rainbow-me/swaps';

import { IS_IOS } from '@/env';
import { Address } from 'viem';
import { clearCustomGasSettings } from '../hooks/useCustomGas';
import { getGasSettingsBySpeed, getSelectedGas } from '../hooks/useSelectedGas';
import { useSwapOutputQuotesDisabled } from '../hooks/useSwapOutputQuotesDisabled';
import { SyncGasStateToSharedValues, SyncQuoteSharedValuesToState } from './SyncSwapStateAndSharedValues';
import { performanceTracking, Screens, TimeToSignOperation } from '@/state/performance/performance';
import { getRemoteConfig } from '@/model/remoteConfig';
import { useConnectedToAnvilStore } from '@/state/connectedToAnvil';
import { useBackendNetworksStore, getChainsNativeAssetWorklet } from '@/state/backendNetworks/backendNetworks';
import { getSwapsNavigationParams } from '../navigateToSwaps';
Expand Down Expand Up @@ -191,7 +189,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
);
const configProgress = useSharedValue<NavigationSteps>(NavigationSteps.INPUT_ELEMENT_FOCUSED);

const slippage = useSharedValue(getDefaultSlippageWorklet(initialSelectedInputAsset?.chainId || ChainId.mainnet, getRemoteConfig()));
const slippage = useSharedValue(initialValues.slippage);

const hasEnoughFundsForGas = useSharedValue<boolean | undefined>(undefined);

Expand Down

0 comments on commit 7c716cb

Please sign in to comment.