Skip to content

Commit

Permalink
feat: add dynamic config for slippage (#4419)
Browse files Browse the repository at this point in the history
### Description

This PR adds plumbing to show the allowed slippage. Our swap endpoint
accepts a slippagePercentage query param already. and is defaulting to
0.3% if not provided. Since we now need this on the client, it made
sense to me to control this remotely.

The dynamic config setup is
[here](https://console.statsig.com/4plizaPmWwPL21ASV4QAO0/dynamic_configs/swap_config).

### Test plan

Tested that we can still get swap quotes as usual.

### Related issues

Related to RET-865

### Backwards compatibility

Y
  • Loading branch information
kathaypacific authored Nov 6, 2023
1 parent dac4ea5 commit 5901fc0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/statsig/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ export const DynamicConfigs = {
disabledMediaPlaybackRequiresUserActionOrigins: [] as string[],
},
},
[StatsigDynamicConfigs.SWAP_CONFIG]: {
configName: StatsigDynamicConfigs.SWAP_CONFIG,
defaultValues: {
maxSlippagePercentage: '0.3',
},
},
}
1 change: 1 addition & 0 deletions src/statsig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum StatsigDynamicConfigs {
WALLET_NETWORK_TIMEOUT_SECONDS = 'wallet_network_timeout_seconds',
MULTI_CHAIN_FEATURES = 'multi_chain_features',
DAPP_WEBVIEW_CONFIG = 'dapp_webview_config',
SWAP_CONFIG = 'swap_config',
}

export enum StatsigFeatureGates {
Expand Down
11 changes: 7 additions & 4 deletions src/swap/SwapScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ jest.mock('src/statsig', () => {
return {
getExperimentParams: (_: any) => mockExperimentParams(),
getFeatureGate: jest.fn(),
getDynamicConfigParams: () => ({
maxSlippagePercentage: '0.3',
}),
}
})

Expand Down Expand Up @@ -339,7 +342,7 @@ describe('SwapScreen', () => {
expect(mockFetch.mock.calls[0][0]).toEqual(
`${
networkConfig.approveSwapUrl
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&sellAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}`
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&sellAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}&slippagePercentage=0.3`
)

expect(swapToContainer).toHaveTextContent('1 CELO ≈ 1.23456 cUSD')
Expand Down Expand Up @@ -373,7 +376,7 @@ describe('SwapScreen', () => {
expect(mockFetch.mock.calls[0][0]).toEqual(
`${
networkConfig.approveSwapUrl
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&buyAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}`
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&buyAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}&slippagePercentage=0.3`
)

expect(swapToContainer).toHaveTextContent('1 CELO ≈ 8.10000 cUSD')
Expand Down Expand Up @@ -531,7 +534,7 @@ describe('SwapScreen', () => {
expect(mockFetch.mock.calls[0][0]).toEqual(
`${
networkConfig.approveSwapUrl
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&sellAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}`
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&sellAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}&slippagePercentage=0.3`
)

expect(swapToContainer).toHaveTextContent('1 CELO ≈ 1,23456 cUSD')
Expand Down Expand Up @@ -573,7 +576,7 @@ describe('SwapScreen', () => {
expect(mockFetch.mock.calls[0][0]).toEqual(
`${
networkConfig.approveSwapUrl
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&buyAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}`
}?buyToken=${mockCusdAddress}&sellToken=${mockCeloAddress}&buyAmount=1234000000000000000&userAddress=${mockAccount.toLowerCase()}&slippagePercentage=0.3`
)

expect(swapToContainer).toHaveTextContent('1 CELO ≈ 8,10000 cUSD')
Expand Down
20 changes: 11 additions & 9 deletions src/swap/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ import { TRANSACTION_FEES_LEARN_MORE } from 'src/brandingConfig'
import BackButton from 'src/components/BackButton'
import BottomSheet, { BottomSheetRefType } from 'src/components/BottomSheet'
import Button, { BtnSizes, BtnTypes } from 'src/components/Button'
import CustomHeader from 'src/components/header/CustomHeader'
import KeyboardAwareScrollView from 'src/components/KeyboardAwareScrollView'
import KeyboardSpacer from 'src/components/KeyboardSpacer'
import TokenBottomSheet, { TokenPickerOrigin } from 'src/components/TokenBottomSheet'
import Warning from 'src/components/Warning'
import CustomHeader from 'src/components/header/CustomHeader'
import { SWAP_LEARN_MORE } from 'src/config'
import { useMaxSendAmountByAddress } from 'src/fees/hooks'
import { FeeType } from 'src/fees/reducer'
import { navigate } from 'src/navigator/NavigationService'
import { Screens } from 'src/navigator/Screens'
import { StackParamList } from 'src/navigator/types'
import { NETWORK_NAMES } from 'src/shared/conts'
import { getExperimentParams, getFeatureGate } from 'src/statsig'
import { ExperimentConfigs } from 'src/statsig/constants'
import { StatsigExperiments, StatsigFeatureGates } from 'src/statsig/types'
import { getDynamicConfigParams, getExperimentParams, getFeatureGate } from 'src/statsig'
import { DynamicConfigs, ExperimentConfigs } from 'src/statsig/constants'
import { StatsigDynamicConfigs, StatsigExperiments, StatsigFeatureGates } from 'src/statsig/types'
import colors from 'src/styles/colors'
import fontStyles from 'src/styles/fonts'
import { Spacing } from 'src/styles/styles'
import variables from 'src/styles/variables'
import PreparedTransactionsReviewBottomSheet from 'src/swap/PreparedTransactionsReviewBottomSheet'
import { priceImpactWarningThresholdSelector, swapInfoSelector } from 'src/swap/selectors'
import { setSwapUserInput } from 'src/swap/slice'
import SwapAmountInput from 'src/swap/SwapAmountInput'
import SwapTransactionDetails from 'src/swap/SwapTransactionDetails'
import { priceImpactWarningThresholdSelector, swapInfoSelector } from 'src/swap/selectors'
import { setSwapUserInput } from 'src/swap/slice'
import { Field, SwapAmount } from 'src/swap/types'
import useSwapQuote from 'src/swap/useSwapQuote'
import { useTokenInfoByAddress } from 'src/tokens/hooks'
Expand Down Expand Up @@ -68,10 +68,12 @@ export function SwapScreen({ route }: Props) {
const { swappingNonNativeTokensEnabled } = getExperimentParams(
ExperimentConfigs[StatsigExperiments.SWAPPING_NON_NATIVE_TOKENS]
)

const { swapBuyAmountEnabled } = getExperimentParams(
ExperimentConfigs[StatsigExperiments.SWAP_BUY_AMOUNT]
)
const slippagePercentage = getDynamicConfigParams(
DynamicConfigs[StatsigDynamicConfigs.SWAP_CONFIG]
).maxSlippagePercentage

const useViemForSwap = getFeatureGate(StatsigFeatureGates.USE_VIEM_FOR_SWAP)

Expand Down Expand Up @@ -110,7 +112,7 @@ export function SwapScreen({ route }: Props) {
const fromTokenBalance = useTokenInfoByAddress(fromToken?.address)?.balance ?? new BigNumber(0)

const { exchangeRate, refreshQuote, fetchSwapQuoteError, fetchingSwapQuote, clearQuote } =
useSwapQuote()
useSwapQuote(slippagePercentage)

// Parsed swap amounts (BigNumber)
const parsedSwapAmount = useMemo(
Expand Down Expand Up @@ -441,7 +443,7 @@ export function SwapScreen({ route }: Props) {
networkFee={new BigNumber(1)}
networkFeeInfoBottomSheetRef={networkFeeInfoBottomSheetRef}
feeTokenId={getTokenId(networkConfig.defaultNetworkId)}
slippagePercentage={0.3}
slippagePercentage={slippagePercentage}
/>
)}

Expand Down
4 changes: 2 additions & 2 deletions src/swap/SwapTransactionDetails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('SwapTransactionDetails', () => {
networkFee={new BigNumber(0.0001)}
networkFeeInfoBottomSheetRef={{ current: null }}
feeTokenId={'someId'}
slippagePercentage={0.5}
slippagePercentage={'0.5'}
/>
</Provider>
)
Expand All @@ -33,7 +33,7 @@ describe('SwapTransactionDetails', () => {
networkFee={new BigNumber(0.0001)}
networkFeeInfoBottomSheetRef={{ current: null }}
feeTokenId={'someId'}
slippagePercentage={0.5}
slippagePercentage={'0.5'}
/>
</Provider>
)
Expand Down
2 changes: 1 addition & 1 deletion src/swap/SwapTransactionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Props {
networkId?: NetworkId
networkFee: BigNumber
networkFeeInfoBottomSheetRef: React.RefObject<BottomSheetRefType>
slippagePercentage: number
slippagePercentage: string
feeTokenId: string
}

Expand Down
3 changes: 2 additions & 1 deletion src/swap/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function prepareSwapTransactions(
})
}

const useSwapQuote = () => {
const useSwapQuote = (slippagePercentage: string) => {
const walletAddress = useSelector(walletAddressSelector)
const useGuaranteedPrice = useSelector(guaranteedSwapPriceEnabledSelector)
const [exchangeRate, setExchangeRate] = useState<QuoteResult | null>(null)
Expand Down Expand Up @@ -143,6 +143,7 @@ const useSwapQuote = () => {
sellToken: fromToken.address,
[swapAmountParam]: swapAmountInWei.toFixed(0, BigNumber.ROUND_DOWN),
userAddress: walletAddress ?? '',
slippagePercentage,
}
const queryParams = new URLSearchParams({ ...params }).toString()
const requestUrl = `${networkConfig.approveSwapUrl}?${queryParams}`
Expand Down

0 comments on commit 5901fc0

Please sign in to comment.