Skip to content

Commit

Permalink
Fix default slippage on mainnet and not being persisted (#5825)
Browse files Browse the repository at this point in the history
* fix default slippage on mainnet

* revert swapsStore persistence

* fix lint
  • Loading branch information
walmat authored Jun 11, 2024
1 parent f232316 commit 0619c71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/__swaps__/screens/Swap/hooks/useSwapSettings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ExtendedAnimatedAssetWithColors } from '@/__swaps__/types/assets';

import { SharedValue, runOnJS, useSharedValue } from 'react-native-reanimated';
import { swapsStore } from '@/state/swaps/swapsStore';
import { slippageStep } from '@/__swaps__/screens/Swap/constants';
import { getDefaultSlippageWorklet } from '@/__swaps__/utils/swaps';
import { ChainId } from '@/__swaps__/types/chains';
import { DEFAULT_CONFIG } from '@/model/remoteConfig';
import { getDefaultSlippageWorklet } from '@/__swaps__/utils/swaps';
import { getRemoteConfig } from '@/model/remoteConfig';
import { swapsStore } from '@/state/swaps/swapsStore';
import { SharedValue, runOnJS, useSharedValue } from 'react-native-reanimated';

export const useSwapSettings = ({ inputAsset }: { inputAsset: SharedValue<ExtendedAnimatedAssetWithColors | null> }) => {
const flashbots = useSharedValue(swapsStore.getState().flashbots);
const slippage = useSharedValue(getDefaultSlippageWorklet(inputAsset.value?.chainId ?? ChainId.mainnet, DEFAULT_CONFIG));
const slippage = useSharedValue(getDefaultSlippageWorklet(inputAsset.value?.chainId || ChainId.mainnet, getRemoteConfig()));

const setSlippage = swapsStore(state => state.setSlippage);
const setFlashbots = swapsStore(state => state.setFlashbots);
Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ export const DEFAULT_SLIPPAGE_BIPS = {
[ChainId.blast]: 200,
};

export const slippageInBipsToString = (slippageInBips: number) => (slippageInBips / 100).toString();
export const slippageInBipsToString = (slippageInBips: number) => (slippageInBips / 100).toFixed(1);

export const slippageInBipsToStringWorklet = (slippageInBips: number) => {
'worklet';
return (slippageInBips / 100).toFixed(1).toString();
return (slippageInBips / 100).toFixed(1);
};

export const getDefaultSlippage = (chainId: ChainId, config: RainbowConfig) => {
Expand Down
7 changes: 3 additions & 4 deletions src/state/swaps/swapsStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExtendedAnimatedAssetWithColors, ParsedSearchAsset } from '@/__swaps__/types/assets';
import { ChainId } from '@/__swaps__/types/chains';
import { getDefaultSlippage } from '@/__swaps__/utils/swaps';
import { DEFAULT_CONFIG } from '@/model/remoteConfig';
import { getRemoteConfig } from '@/model/remoteConfig';
import { createRainbowStore } from '@/state/internal/createRainbowStore';
import { CrosschainQuote, Quote, QuoteError, Source } from '@rainbow-me/swaps';

Expand Down Expand Up @@ -33,7 +33,7 @@ export const swapsStore = createRainbowStore<SwapsState>(
isSwapsOpen: false,
setIsSwapsOpen: (isSwapsOpen: boolean) => set({ isSwapsOpen }),

inputAsset: null, // TODO: Default to their largest balance asset (or ETH mainnet if user has no assets)
inputAsset: null,
outputAsset: null,

quote: null,
Expand All @@ -43,7 +43,7 @@ export const swapsStore = createRainbowStore<SwapsState>(

flashbots: false,
setFlashbots: (flashbots: boolean) => set({ flashbots }),
slippage: getDefaultSlippage(ChainId.mainnet, DEFAULT_CONFIG),
slippage: getDefaultSlippage(ChainId.mainnet, getRemoteConfig()),
setSlippage: (slippage: string) => set({ slippage }),
source: 'auto',
setSource: (source: Source | 'auto') => set({ source }),
Expand All @@ -56,7 +56,6 @@ export const swapsStore = createRainbowStore<SwapsState>(
return {
flashbots: state.flashbots,
source: state.source,
slippage: state.slippage,
};
},
}
Expand Down

0 comments on commit 0619c71

Please sign in to comment.