Skip to content

Commit

Permalink
add slippage info
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Sep 8, 2023
1 parent 231d360 commit fd80b4b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
63 changes: 57 additions & 6 deletions src/features/swaps/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Refresh from '@assets/images/refresh.svg'
import AddressBookSelector, {
AddressBookRef,
} from '@components/AddressBookSelector'
import { ReAnimatedBox } from '@components/AnimatedBox'
import { ReAnimatedBlurBox, ReAnimatedBox } from '@components/AnimatedBox'
import Box from '@components/Box'
import ButtonPressable from '@components/ButtonPressable'
import CircleLoader from '@components/CircleLoader'
Expand Down Expand Up @@ -57,6 +57,7 @@ import { useAsync } from 'react-async-hook'
import { useTranslation } from 'react-i18next'
import { LayoutAnimation } from 'react-native'
import { Edge } from 'react-native-safe-area-context'
import { FadeInFast } from '@components/FadeInOut'
import { useSolana } from '../../solana/SolanaProvider'
import { solAddressIsValid } from '../../utils/accountUtils'
import SwapItem from './SwapItem'
Expand Down Expand Up @@ -84,6 +85,7 @@ const SwapScreen = () => {
const [inputAmount, setInputAmount] = useState<number>(0)
const [outputMint, setOutputMint] = useState<PublicKey>(HNT_MINT)
const [slippageBps, setSlippageBps] = useState<number>(50)
const [slippageInfoVisible, setSlippageInfoVisible] = useState(false)
const [solFee, setSolFee] = useState<BN | undefined>(SOL_TXN_FEE)
const [hasInsufficientBalance, setHasInsufficientBalance] = useState<
undefined | boolean
Expand Down Expand Up @@ -300,11 +302,21 @@ const SwapScreen = () => {
backgroundColor="surfaceSecondary"
opacity={disabled ? 0.3 : 1}
>
<Box flex={1} padding="ms" borderEndWidth={2} alignItems="center">
<TouchableOpacityBox
flex={1}
borderEndWidth={2}
alignItems="center"
justifyContent="center"
alignContent="center"
flexDirection="row"
paddingHorizontal="ms"
onPress={() => setSlippageInfoVisible(true)}
>
<Text variant="body3Medium" color="surfaceSecondaryText">
Slippage:
{t('swapsScreen.slippage')}
</Text>
</Box>
</TouchableOpacityBox>

{bpsOptions.map((bps, idx) => {
const isLast = idx === bpsOptions.length - 1
const isActive = slippageBps === bps
Expand Down Expand Up @@ -335,7 +347,7 @@ const SwapScreen = () => {
})}
</Box>
)
}, [slippageBps, setSlippageBps, outputMint])
}, [slippageBps, setSlippageBps, outputMint, setSlippageInfoVisible, t])

const setTokenTypeHandler = useCallback(
(mint: PublicKey) => {
Expand Down Expand Up @@ -655,7 +667,7 @@ const SwapScreen = () => {
marginHorizontal="m"
variant="body3Medium"
color="white"
i18nKey="swapsScreen.slippage"
i18nKey="swapsScreen.slippageLabelValue"
values={{ amount: slippageBps / 100 }}
/>
)}
Expand All @@ -682,6 +694,45 @@ const SwapScreen = () => {
</SafeAreaBox>
</ReAnimatedBox>
</TokenSelector>
{slippageInfoVisible ? (
<ReAnimatedBlurBox
visible
entering={FadeInFast}
position="absolute"
height="100%"
width="100%"
>
<Box
flexDirection="row"
justifyContent="space-between"
alignItems="center"
marginTop="l"
>
<Box flex={1}>
<CloseButton
marginStart="m"
onPress={() => setSlippageInfoVisible(false)}
/>
</Box>
<Box flex={1} alignItems="center" flexDirection="row">
<Text variant="h4" color="white" flex={1} textAlign="center">
{t('swapsScreen.slippage')}
</Text>
</Box>
<Box flex={1} />
</Box>
<Box flex={1} paddingHorizontal="m" marginTop="l">
<Text
variant="body1Medium"
color="white"
flex={1}
textAlign="justify"
>
{t('swapsScreen.slippageInfo')}
</Text>
</Box>
</ReAnimatedBlurBox>
) : undefined}
</HNTKeyboard>
</AddressBookSelector>
)
Expand Down
6 changes: 5 additions & 1 deletion src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ export default {
youReceive: 'You Receive',
chooseTokenToSwap: 'Choose a token to swap',
chooseTokenToReceive: 'Choose a token to receive',
slippage: '<b>Slippage</b> <secondaryText> {{ amount }}% </secondaryText>',
slippage: 'Slippage',
slippageLabelValue:
'<b>Slippage</b> <secondaryText> {{ amount }}% </secondaryText>',
slippageInfo:
'Slippage is the difference between the expected price of an order and the price when the order actually executes. The slippage percentage shows how much the price for a specific asset has moved. Due to the volatility of cryptocurrency, the price of an asset can fluctuate often depending on trade volume and activity. ',
minReceived:
'<b>Minimum Received</b> <secondaryText> {{ amount }} </secondaryText>',
swapComplete: 'Tokens swapped!',
Expand Down

0 comments on commit fd80b4b

Please sign in to comment.