Skip to content

Commit

Permalink
fix: make slippage settings through
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Oct 14, 2024
1 parent be2e6cc commit 3b769af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@ import { bpsToPercent } from '@cowprotocol/common-utils'
import { mapSupportedNetworks, SupportedChainId } from '@cowprotocol/cow-sdk'
import { walletInfoAtom } from '@cowprotocol/wallet'

import { isEoaEthFlowAtom, tradeTypeAtom } from 'modules/trade'
import { TradeType } from 'modules/trade/types/TradeType'
import { isEoaEthFlowAtom } from 'modules/trade'

type SlippageBpsPerNetwork = Record<SupportedChainId, number | null>

type SlippagePerTradeType = Record<TradeType, SlippageBpsPerNetwork>

type SlippageType = 'smart' | 'default' | 'user'

const getDefaultSlippageState = () =>
Object.keys(TradeType).reduce((acc, tradeType) => {
acc[tradeType as TradeType] = mapSupportedNetworks(null)
return acc
}, {} as SlippagePerTradeType)

const normalTradeSlippageAtom = atomWithStorage<SlippagePerTradeType>('tradeSlippageAtom:v1', getDefaultSlippageState())
const normalTradeSlippageAtom = atomWithStorage<SlippageBpsPerNetwork>(
'swapSlippageAtom:v0',
mapSupportedNetworks(null),
)

const ethFlowSlippageAtom = atomWithStorage<SlippagePerTradeType>('ethFlowSlippageAtom:v1', getDefaultSlippageState())
const ethFlowSlippageAtom = atomWithStorage<SlippageBpsPerNetwork>('ethFlowSlippageAtom:v0', mapSupportedNetworks(null))

export const smartTradeSlippageAtom = atom<number | null>(null)

Expand All @@ -37,13 +31,10 @@ export const defaultSlippageAtom = atom((get) => {
const currentSlippageAtom = atom<number | null>((get) => {
const { chainId } = get(walletInfoAtom)
const isEoaEthFlow = get(isEoaEthFlowAtom)
const tradeTypeState = get(tradeTypeAtom)
const normalSlippage = get(normalTradeSlippageAtom)
const ethFlowSlippage = get(ethFlowSlippageAtom)

if (!tradeTypeState) return null

return (isEoaEthFlow ? ethFlowSlippage : normalSlippage)[tradeTypeState.tradeType]?.[chainId] ?? null
return (isEoaEthFlow ? ethFlowSlippage : normalSlippage)?.[chainId] ?? null
})

export const slippageValueAndTypeAtom = atom<{ type: SlippageType; value: number }>((get) => {
Expand All @@ -70,21 +61,12 @@ export const tradeSlippagePercentAtom = atom((get) => {
export const setTradeSlippageAtom = atom(null, (get, set, slippageBps: number | null) => {
const { chainId } = get(walletInfoAtom)
const isEoaEthFlow = get(isEoaEthFlowAtom)
const tradeTypeState = get(tradeTypeAtom)

if (tradeTypeState) {
const currentStateAtom = isEoaEthFlow ? ethFlowSlippageAtom : normalTradeSlippageAtom
const currentState = get(currentStateAtom)

set(currentStateAtom, {
...currentState,
[tradeTypeState.tradeType]: {
...currentState[tradeTypeState.tradeType],
[chainId]: slippageBps,
},
})
} else {
// It should not happen in the normal flow
console.error('Trade type is not set, cannot set slippage!')
}

const currentStateAtom = isEoaEthFlow ? ethFlowSlippageAtom : normalTradeSlippageAtom
const currentState = get(currentStateAtom)

set(currentStateAtom, {
...currentState,
[chainId]: slippageBps,
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const defaultYieldSettings: YieldSettingsState = {
}

export const { atom: yieldSettingsAtom, updateAtom: updateYieldSettingsAtom } = atomWithPartialUpdate(
atomWithStorage<YieldSettingsState>('yieldSettingsAtom:v1', defaultYieldSettings, getJotaiIsolatedStorage()),
atomWithStorage<YieldSettingsState>('yieldSettingsAtom:v0', defaultYieldSettings, getJotaiIsolatedStorage()),
)

0 comments on commit 3b769af

Please sign in to comment.