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

feature(twap-orders): refactor NumberOfParts and Slippage (9) #2479

Merged

Conversation

nenadV91
Copy link
Contributor

Summary

Refactors NumberOfParts and Slippage components
As suggested by @shoom3301 these two components will not use the shared TradeNumberInput component

To test

  • no need to test functionality right now for this

@vercel
Copy link

vercel bot commented May 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
swap-dev 🔄 Building (Inspect) Visit Preview

🌃 Cosmos ↗︎

const setSlippage = useSetSlippage()

return useCallback(
(value: string) => {
// populate what the user typed and clear the error
setSlippageInput(value)
setSlippageError(false)
setSlippageError(null)
setSlippageWarning(null)

if (value.length === 0) {
Copy link
Collaborator

@shoom3301 shoom3301 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic with parsing value must be inside TradeNumberInput.

const onSlippageChange = (value: null | number) => {
    setSlippage(value === null ? 'auto' : value)
}

return < TradeNumberInput ...props, decimals={2} onChange={onSlippageChange}>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you meant inside the AdvancedOrdersWidget because slippage and number of parts use different parsing, so it can't be in TradeNumberInput. But this hook useParseSlippage has a lot of code and I don't want to put all that code inside the AdvancedOrdersWidget and the same for number of parts parsing. That is why I prefered to have 2 different components for slippage and number of parts

updateAdvancedOrdersSettingsAtom,
NumberOfPartsError,
} from '@cow/modules/advancedOrders/state/advancedOrdersSettingsAtom'
import { updateAdvancedOrdersSettingsAtom } from '@cow/modules/advancedOrders/state/advancedOrdersSettingsAtom'
import { MIN_PARTS_NUMBER, MAX_PARTS_NUMBER } from '@src/constants'

export function useParseNumberOfParts() {
Copy link
Collaborator

@shoom3301 shoom3301 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should continue to follow the pattern like in updateLimitOrdersAtom.
And I guess we won't need this hook if we move parsing logic to TradeNumberInput, see: https://github.com/cowprotocol/cowswap/pull/2479/files#r1198591948

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my comment above #2479 (comment)

const parseNumberOfParts = useParseNumberOfParts()

// Slippage
const [slippageInput, setSlippageInput] = useState('')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

In general, the logic should fit this diagram.
Basic principles:

  1. TradeNumberInput returns only number or null
  2. TradeNumberInput has precision a number of digits after comma (like here)
  3. Validation of slippage and numOfParts should be presented as a pure function because it's deterministic and depends only on number | null value
  4. Since a validation error is deterministic, we don't need to store it. It always can be calculated on flight

@nenadV91

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since a validation error is deterministic
I'm not sure about this, because we will probably use those errors for Buttons state (disabled/enabled) and I think its easier to just access it from the state instead of passing it down

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's not deterministic? It supposed to be a pure function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would store it because it will be used in different components

setSlippageError,
setSlippageWarning,
})
const displaySlippageValue = useDisplaySlippageValue(slippageInput)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the hook is redundant. The value of slippage in atom should be ready for displaying as is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set slippage to auto in atom but we don't want to show auto in UI 🤔

Copy link
Collaborator

@shoom3301 shoom3301 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments

@nenadV91 nenadV91 merged commit ae999a0 into feature/twap-orders-deadline-updates May 22, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 22, 2023
@alfetopito alfetopito deleted the feature/twap-orders-refactor branch May 23, 2023 09:22
@alfetopito
Copy link
Collaborator

@nenadV91 please get approvals before merging.
Or at least mention why you are bypassing approvals.

@nenadV91
Copy link
Contributor Author

@alfetopito I should have mentioned that we are merging all of these PR's into this one #2426, my mistake, sorry about that.
The PR's have been open for a while now and we need to move on so that is why.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants