-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add swap settings and review panel implementation (#5748)
- Loading branch information
Showing
4 changed files
with
78 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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'; | ||
|
||
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 setSlippage = swapsStore(state => state.setSlippage); | ||
const setFlashbots = swapsStore(state => state.setFlashbots); | ||
|
||
const onToggleFlashbots = () => { | ||
'worklet'; | ||
|
||
const current = flashbots.value; | ||
flashbots.value = !current; | ||
runOnJS(setFlashbots)(!current); | ||
}; | ||
|
||
const onUpdateSlippage = (operation: 'plus' | 'minus') => { | ||
'worklet'; | ||
|
||
const value = operation === 'plus' ? slippageStep : -slippageStep; | ||
|
||
// if we're trying to decrement below the minimum, set to the minimum | ||
if (Number(slippage.value) + value <= slippageStep) { | ||
slippage.value = slippageStep.toFixed(1).toString(); | ||
} else { | ||
slippage.value = (Number(slippage.value) + value).toFixed(1).toString(); | ||
} | ||
|
||
runOnJS(setSlippage)(slippage.value); | ||
}; | ||
|
||
return { | ||
flashbots, | ||
slippage, | ||
|
||
onToggleFlashbots, | ||
onUpdateSlippage, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters