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: amount locking #5304

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
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
Expand Up @@ -224,7 +224,7 @@ export function RateInput() {
rateImpact={rateImpact}
toggleIcon={
<HoverTooltip
content="When enabled, the limit price stays fixed when changing the BUY amount. When disabled, the limit price will update based on the BUY amount changes."
content="When locked, the limit price stays fixed when changing the amounts. When unlocked, the limit price will update based on the amount changes."
wrapInContainer
placement="top-start"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useAtomValue, useSetAtom } from 'jotai'
import { useCallback } from 'react'

import { FractionUtils, isSellOrder } from '@cowprotocol/common-utils'
import { OrderKind } from '@cowprotocol/cow-sdk'
import { Currency, CurrencyAmount, Fraction, Price } from '@uniswap/sdk-core'
import { Currency, CurrencyAmount, Fraction } from '@uniswap/sdk-core'

import { Writeable } from 'types'

Expand All @@ -12,8 +11,6 @@ import { Field } from 'legacy/state/types'
import { useLimitOrdersDerivedState } from 'modules/limitOrders/hooks/useLimitOrdersDerivedState'
import { useUpdateLimitOrdersRawState } from 'modules/limitOrders/hooks/useLimitOrdersRawState'
import { LimitOrdersRawState } from 'modules/limitOrders/state/limitOrdersRawStateAtom'
import { limitOrdersSettingsAtom } from 'modules/limitOrders/state/limitOrdersSettingsAtom'
import { updateLimitRateAtom } from 'modules/limitOrders/state/limitRateAtom'

import { calculateAmountForRate } from 'utils/orderUtils/calculateAmountForRate'

Expand All @@ -25,46 +22,13 @@ type CurrencyAmountProps = {

export function useUpdateCurrencyAmount() {
const updateLimitOrdersState = useUpdateLimitOrdersRawState()
const { inputCurrency, outputCurrency, inputCurrencyAmount: currentInputAmount } = useLimitOrdersDerivedState()
const { limitPriceLocked } = useAtomValue(limitOrdersSettingsAtom)
const updateLimitRateState = useSetAtom(updateLimitRateAtom)
const { inputCurrency, outputCurrency } = useLimitOrdersDerivedState()

return useCallback(
(params: CurrencyAmountProps) => {
const { activeRate, amount, orderKind } = params
const field = isSellOrder(orderKind) ? Field.INPUT : Field.OUTPUT
const isBuyAmountChange = field === Field.OUTPUT

if (isBuyAmountChange) {
const update: Partial<Writeable<LimitOrdersRawState>> = {
orderKind,
outputCurrencyAmount: FractionUtils.serializeFractionToJSON(amount),
}

updateLimitOrdersState(update)

// If price is unlocked, update the rate based on the new amounts
if (!limitPriceLocked) {
// Calculate and update the new rate
if (amount && currentInputAmount) {
const newRate = new Price(
currentInputAmount.currency,
amount.currency,
currentInputAmount.quotient,
amount.quotient,
)
updateLimitRateState({
activeRate: FractionUtils.fractionLikeToFraction(newRate),
isTypedValue: false,
isRateFromUrl: false,
isAlternativeOrderRate: false,
})
}
}
return
}

// Normal flow for SELL amount changes
const calculatedAmount = calculateAmountForRate({
activeRate,
amount,
Expand All @@ -88,6 +52,6 @@ export function useUpdateCurrencyAmount() {

updateLimitOrdersState(update)
},
[inputCurrency, outputCurrency, updateLimitOrdersState, limitPriceLocked, updateLimitRateState, currentInputAmount],
[inputCurrency, outputCurrency, updateLimitOrdersState],
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const defaultLimitOrdersSettings: LimitOrdersSettingsState = {
deadlineMilliseconds: defaultLimitOrderDeadline.value,
customDeadlineTimestamp: null,
limitPricePosition: 'bottom',
limitPriceLocked: false,
limitPriceLocked: true,
ordersTableOnLeft: false,
isUsdValuesMode: false,
}
Expand Down
Loading