Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(limit-orders): update price and amounts when tokens changed #5356

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import { useMemo } from 'react'
import { useSetAtom } from 'jotai/index'
import { useEffect, useMemo } from 'react'

import { usePrevious } from '@cowprotocol/common-hooks'
import { Price } from '@uniswap/sdk-core'

import { useSetupTradeAmountsFromUrl } from 'modules/trade'

import { TradeAmounts } from 'common/types'

import { useLimitOrdersRawState } from '../hooks/useLimitOrdersRawState'
import { useUpdateActiveRate } from '../hooks/useUpdateActiveRate'
import { updateLimitRateAtom } from '../state/limitRateAtom'

export function SetupLimitOrderAmountsFromUrlUpdater() {
const updateRate = useUpdateActiveRate()
const updateRateState = useSetAtom(updateLimitRateAtom)

const { inputCurrencyId, outputCurrencyId } = useLimitOrdersRawState()
const tokensPair = `${inputCurrencyId || ''}${outputCurrencyId || ''}`
const prevTokensPair = usePrevious(tokensPair)

/**
* In useUpdateActiveRate() we have a logic which depends on isRateFromUrl flag
* Mainly, it serves for keeping amounts static after coming from another trade widget
* But we should not prevent amounts and price update when we change tokens
* So, we reset isRateFromUrl flag when at least one of the tokens is changed
*/
useEffect(() => {
if (!tokensPair || !prevTokensPair || tokensPair === prevTokensPair) return

updateRateState({ isRateFromUrl: false })
}, [tokensPair, prevTokensPair, updateRateState])

const params = useMemo(() => {
return {
Expand Down
Loading