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

[SWAP REFACTORING #2] refactor swap business logic #1039

Merged
merged 107 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
48d45c1
Rename helpers to hooks
shoom3301 Aug 29, 2022
2449310
useSwapConfirmManager() to consolidate actions with Swap confirm state
shoom3301 Aug 29, 2022
2cec6ca
Move Price components to separated file
shoom3301 Aug 29, 2022
7288952
Move TradeBasicDetails components to separated file
shoom3301 Aug 29, 2022
d6bc6e7
Remove excess component EthWethWrapMessage
shoom3301 Aug 29, 2022
bb025f2
Move BottomGrouping to styled file
shoom3301 Aug 29, 2022
3c44267
Use ArrowWrapperLoader component directrly on Swap page
shoom3301 Aug 29, 2022
ec150f8
Use HighFeeWarning, NoImpactWarning components directly on Swap page
shoom3301 Aug 29, 2022
9ff198b
Move FeesDiscount component to separated file
shoom3301 Aug 29, 2022
8bbbd6b
Cleanup styled components in Swap page
shoom3301 Aug 29, 2022
ce5540e
Cleanup Swap component props
shoom3301 Aug 29, 2022
34584bb
Cleanup Swap component imports
shoom3301 Aug 29, 2022
90ce9d4
Make SwapButton component pure
shoom3301 Aug 29, 2022
91c5988
Refactor swap flow
shoom3301 Aug 30, 2022
0197429
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Aug 31, 2022
dbe9a93
Merge new ETH flow after refactoring
shoom3301 Aug 31, 2022
832058a
Refactor useDetectNativeToken()
shoom3301 Aug 31, 2022
e7a75db
Refactor wrap/unwrap flow
shoom3301 Sep 1, 2022
3f74f2f
Remove unused code
shoom3301 Sep 1, 2022
aff144d
Setup the limit orders route
shoom3301 Aug 22, 2022
1567639
Layout of Currency input for limit orders
shoom3301 Aug 22, 2022
959866e
Layout of Currency input with the amount to receive
shoom3301 Aug 22, 2022
682803e
Layout of CurrencyArrowSeparator
shoom3301 Aug 22, 2022
0e43631
Layout of TradeRates
shoom3301 Aug 22, 2022
ca6d388
Trade button layout
shoom3301 Aug 22, 2022
0a050b0
Dark theme fixes
shoom3301 Aug 22, 2022
e515646
New Swap page setup
shoom3301 Aug 26, 2022
f6af657
Use SwapHeader component for New Swap page
shoom3301 Aug 26, 2022
19eec82
Setup swap state
shoom3301 Sep 2, 2022
80b7eec
Select currency using search modal
shoom3301 Sep 2, 2022
e7997bc
Optimize Swap page rendering
shoom3301 Sep 2, 2022
6f38991
Show balance and fiat amounts
shoom3301 Sep 3, 2022
cc367a7
Show view amount for destination currency
shoom3301 Sep 4, 2022
824cc00
Show price impact
shoom3301 Sep 4, 2022
f76a1b8
Render receive amount
shoom3301 Sep 5, 2022
c9c1993
Transform useSwapButtonState() to getSwapButtonState()
shoom3301 Sep 5, 2022
8684131
Swap button state and swap confirm modals
shoom3301 Sep 5, 2022
8a69b63
Subsidy modal and Affiliate status check
shoom3301 Sep 6, 2022
8b851b6
New swap page warnings
shoom3301 Sep 6, 2022
19c9c6d
Fix setup swap state on network changes
shoom3301 Sep 6, 2022
7c1684d
Move useSwapButtonContext
shoom3301 Sep 6, 2022
ba22568
Refactor the transaction confirm modal usage
shoom3301 Sep 6, 2022
ebed54a
Make new swap components pure
shoom3301 Sep 6, 2022
830e6d6
Rename NewSwapPage to SwapForm
shoom3301 Sep 6, 2022
d60cda7
Recipient controls
shoom3301 Sep 6, 2022
8405834
Trade rate control
shoom3301 Sep 6, 2022
85adf25
Show high slippage warning
shoom3301 Sep 6, 2022
bd95408
Trade details (fee, slippage, discount)
shoom3301 Sep 6, 2022
a46facb
Fix import token dismiss
shoom3301 Sep 6, 2022
10695c2
Fix AffiliateStatusCheck displaying
shoom3301 Sep 6, 2022
8c82c0e
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Sep 6, 2022
a0700cc
Fix after merge develop
shoom3301 Sep 6, 2022
d144688
Fix lint errors
shoom3301 Sep 6, 2022
c712b69
Fix discount appearance
shoom3301 Sep 6, 2022
883da8c
Fix dark theme styles
shoom3301 Sep 6, 2022
cea0d71
Remove unused WrapUnwrap related code from Swap flow
shoom3301 Sep 7, 2022
8b56a0b
Documentation for Swap flow
shoom3301 Sep 7, 2022
d65ce1f
Deprecate outdated code
shoom3301 Sep 7, 2022
1bbbdbc
Wrap/unwrap flow documentation
shoom3301 Sep 7, 2022
3c155c3
CurrencyArrowSeparator fixture
shoom3301 Sep 7, 2022
3003a86
CurrencyInputPanel fixture
shoom3301 Sep 7, 2022
3f9afdc
CurrencySelectButton fixture
shoom3301 Sep 7, 2022
a28bd9f
TradeRates fixture
shoom3301 Sep 7, 2022
1f80ee2
SwapForm fixture
shoom3301 Sep 7, 2022
e01ae2d
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Sep 7, 2022
c6ec586
Fix code style issues with Prettier
lint-action Sep 7, 2022
b6de0d9
Merge branch 'refactor/wrap-native-token' of https://github.com/cowpr…
shoom3301 Sep 7, 2022
3d19392
Merge remote-tracking branch 'origin/refactor/new-swap-form' into ref…
shoom3301 Sep 7, 2022
2fbaa98
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Sep 7, 2022
b5d8b94
Merge branch 'refactor/swap-business-logic-1' of https://github.com/c…
shoom3301 Sep 7, 2022
d3ffab2
Merge branch 'refactor/separate-swap-logic-1' of https://github.com/c…
shoom3301 Sep 7, 2022
b2c99d3
Merge branch 'refactor/swap-business-logic-1' of https://github.com/c…
shoom3301 Sep 7, 2022
9fac07b
Merge branch 'refactor/wrap-native-token' of https://github.com/cowpr…
shoom3301 Sep 7, 2022
6f8c802
Fix cypress test
shoom3301 Sep 7, 2022
d4ad21d
Fix cypress test
shoom3301 Sep 7, 2022
2bedd82
Rename isSwapUnsupported
shoom3301 Sep 7, 2022
2c57ee9
Fix CompatibilityIssuesWarning displaying condition
shoom3301 Sep 7, 2022
55332e1
Cleanup ApproveButton component
shoom3301 Sep 7, 2022
d6abd24
Refactor SwapButton
shoom3301 Sep 7, 2022
5bbae94
Cleanup useHandleSwap()
shoom3301 Sep 8, 2022
bb20c79
Remove redundant comment
shoom3301 Sep 8, 2022
354bbdf
Remove redundant double negation
shoom3301 Sep 8, 2022
6234dc3
Remove redundant @src
shoom3301 Sep 8, 2022
58fb25d
Remove unused import
shoom3301 Sep 8, 2022
c17b9de
Wrap/unwrap fixes
shoom3301 Sep 9, 2022
c6c5692
fix: check is wallet connected first of all
shoom3301 Sep 9, 2022
7f6ce14
fix: don't set output currency by default
shoom3301 Sep 9, 2022
18c8e06
fix: show currencies amounts in the tokens list
shoom3301 Sep 19, 2022
7067117
fix: remove unused code
shoom3301 Sep 19, 2022
e2513a8
fix: check entered amount before tokens existing
shoom3301 Sep 19, 2022
6d8de0e
fix: fix swap form styles for Dark mode
shoom3301 Sep 19, 2022
c92a5d5
Reset swap form state when account changed
shoom3301 Sep 19, 2022
b3409b0
fix: fix currency input style with loading state
shoom3301 Sep 19, 2022
0e70b1e
fix: call usePriceImpact() only one
shoom3301 Sep 19, 2022
d830ecb
fix: move custom useAllTokens() to mod file
shoom3301 Sep 19, 2022
f7a24a2
Rename SwapButtonStateParams
shoom3301 Sep 19, 2022
422ab26
Remove xstate dependency
shoom3301 Sep 19, 2022
0a77cb4
Fix code style
shoom3301 Sep 19, 2022
ab4bc76
Fix code style
shoom3301 Sep 19, 2022
348c084
Fix code style
shoom3301 Sep 19, 2022
e155ef6
Merge pull request #1049 from cowprotocol/refactor/new-swap-review
shoom3301 Sep 19, 2022
084e448
Merge pull request #1041 from cowprotocol/refactor/new-swap-form-1
shoom3301 Sep 19, 2022
0e04879
Merge pull request #1046 from cowprotocol/refactor/new-swap-form-p4
shoom3301 Sep 19, 2022
e311588
Merge pull request #1045 from cowprotocol/refactor/new-swap-form-p3
shoom3301 Sep 19, 2022
91e7736
Merge pull request #1044 from cowprotocol/refactor/new-swap-form-p2
shoom3301 Sep 19, 2022
16e6bbb
Merge pull request #1043 from cowprotocol/refactor/new-swap-form-p1
shoom3301 Sep 19, 2022
06c42a4
Merge pull request #1038 from cowprotocol/refactor/wrap-native-token
shoom3301 Sep 19, 2022
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
6 changes: 3 additions & 3 deletions cypress-custom/integration/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Swap (custom)', () => {
cy.get('#swap-currency-input .token-amount-input').should('be.visible')
cy.get('#swap-currency-input .token-amount-input').type('0.5', { force: true, delay: 200 })
cy.get('#swap-currency-output .token-amount-input').should('not.equal', '')
cy.get('#swap-button').click()
cy.get('#swap-button').should('contain.text', 'Swap').click()
cy.get('#confirm-swap-or-send').should('contain', 'Confirm Swap')
})

Expand All @@ -40,8 +40,8 @@ describe('Swap (custom)', () => {
// cy.get('.token-item-0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735').click({ force: true })
// input amounts
cy.get('#swap-currency-input .token-amount-input').should('be.visible')
cy.get('#swap-currency-input .token-amount-input').type('0.001', { force: true, delay: 400 })
cy.get('#swap-currency-input .token-amount-input').type('0.0007', { force: true, delay: 400 })
cy.get('#swap-currency-output .token-amount-input').should('not.equal', '')
cy.get('#swap-button').should('contain', 'Switch to WETH')
cy.get('#swap-button').should('contain', 'Swap with WETH')
})
})
2 changes: 1 addition & 1 deletion cypress-custom/integration/swapMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Swap (mod)', () => {
cy.get('#swap-currency-input .token-amount-input').should('be.visible')
cy.get('#swap-currency-input .token-amount-input').type('{selectall}{backspace}{selectall}{backspace}').type('0.5')
cy.get('#swap-currency-output .token-amount-input').should('not.equal', '')
cy.get('#swap-button').click()
cy.get('#swap-button').should('contain.text', 'Swap').click()
cy.get('#confirm-swap-or-send').should('contain', 'Confirm Swap')
})

Expand Down
2 changes: 1 addition & 1 deletion src/components/AddressInputPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default function AddressInputPanel({
<InputContainer>
<AutoColumn gap="md">
<RowBetween>
<ThemedText.Black color={theme.text2} fontWeight={500} fontSize={14}>
<ThemedText.Black color={theme.text1} fontWeight={500} fontSize={14}>
{label ?? <Trans>Recipient</Trans>}
</ThemedText.Black>
{address && chainId && (
Expand Down
4 changes: 2 additions & 2 deletions src/components/swap/SwapHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const StyledSwapHeader = styled.div`
color: ${({ theme }) => theme.text2};
`

export default function SwapHeader({ allowedSlippage }: { allowedSlippage: Percent }) {
export default function SwapHeader({ allowedSlippage, className }: { allowedSlippage: Percent; className?: string }) {
return (
<StyledSwapHeader>
<StyledSwapHeader className={className}>
<RowBetween>
<RowFixed>
<ThemedText.Black fontWeight={500} fontSize={16} style={{ marginRight: '8px' }}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/swap/confirmPriceImpactWithoutFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ export default function confirmPriceImpactWithoutFee(priceImpactWithoutFee: Perc
)}%. Please type the word "confirm" to continue with this swap.`
) === 'confirm'
)
} else if (!priceImpactWithoutFee.lessThan(ALLOWED_PRICE_IMPACT_HIGH)) {
}

if (!priceImpactWithoutFee.lessThan(ALLOWED_PRICE_IMPACT_HIGH)) {
return window.confirm(
`This swap has a price impact of at least ${ALLOWED_PRICE_IMPACT_HIGH.toFixed(
0
)}%. Please confirm that you would like to continue with this swap.`
)
}

return true
}
1 change: 1 addition & 0 deletions src/cosmos.decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MetaMask } from '@web3-react/metamask'
import { BlockNumberProvider } from '@src/lib/hooks/useBlockNumber'

const Wrapper = styled(Flex)`
font-family: 'Inter var', sans-serif;
padding: 1.2rem 0.6rem;
justify-content: center;
align-items: center;
Expand Down
33 changes: 33 additions & 0 deletions src/custom/components/InfoIcon/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Info } from 'react-feather'
import { MouseoverTooltipContent, TooltipContainer } from 'components/Tooltip'
import styled from 'styled-components/macro'
import { ReactNode } from 'react'

const StyledInfoIcon = styled(Info)`
opacity: 0.4;
stroke: ${({ theme }) => theme.text3};
line-height: 0;
vertical-align: middle;

:hover {
opacity: 1;
}
`

const StyledTooltipContainer = styled(TooltipContainer)`
font-size: 13px;
`

export interface InfoIconProps {
content: ReactNode
}

export function InfoIcon(props: InfoIconProps) {
const content = <StyledTooltipContainer>{props.content}</StyledTooltipContainer>

return (
<MouseoverTooltipContent wrap={false} content={content} placement="bottom">
<StyledInfoIcon size={16} />
</MouseoverTooltipContent>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Trans } from '@lingui/macro'
// import { Trade } from '@uniswap/router-sdk'
import { /* Currency, */ Percent /* , TradeType */ } from '@uniswap/sdk-core'
import { ReactNode, useCallback, useMemo } from 'react'
import { useCallback, useMemo } from 'react'
// import { InterfaceTrade } from 'state/routing/types'
// import { tradeMeaningfullyDiffers } from 'utils/tradeMeaningFullyDiffer'

Expand All @@ -16,13 +16,14 @@ import SwapModalHeader from 'components/swap/SwapModalHeader'
// MOD imports
import TradeGp from 'state/swap/TradeGp'
import { useWalletInfo } from 'hooks/useWalletInfo'
import { SwapConfirmState } from 'pages/Swap/state/swapConfirmAtom'

/**
* Returns true if the trade requires a confirmation of details before we can submit it
* @param tradeA trade A
* @param tradeB trade B
*/
/*
/*
function tradeMeaningfullyDiffers(
...args:
| [V2Trade<Currency, Currency, TradeType>, V2Trade<Currency, Currency, TradeType>]
Expand All @@ -42,35 +43,26 @@ function tradeMeaningfullyDiffers(tradeA: TradeGp, tradeB: TradeGp): boolean {

export default function ConfirmSwapModal({
trade,
originalTrade,
swapConfirmState,
onAcceptChanges,
allowedSlippage,
onConfirm,
onDismiss,
recipient,
priceImpact, // mod
swapErrorMessage,
isOpen,
attemptingTxn,
txHash,
PendingTextComponent, // mod
priceImpact,
PendingTextComponent,
}: {
isOpen: boolean
// trade: InterfaceTrade<Currency, Currency, TradeType> | undefined
// originalTrade: Trade<Currency, Currency, TradeType> | undefined
swapConfirmState: SwapConfirmState
trade: TradeGp | undefined
originalTrade: TradeGp | undefined
attemptingTxn: boolean
txHash: string | undefined
recipient: string | null
priceImpact?: Percent
allowedSlippage: Percent
onAcceptChanges: () => void
onConfirm: () => void
swapErrorMessage: ReactNode | undefined
onDismiss: () => void
PendingTextComponent: (props: { trade: TradeGp | undefined }) => JSX.Element // mod
}) {
const { swapErrorMessage, showConfirm, attemptingTxn, txHash, tradeToConfirm: originalTrade } = swapConfirmState
const { allowsOffchainSigning } = useWalletInfo()
const showAcceptChanges = useMemo(
() => Boolean(trade && originalTrade && tradeMeaningfullyDiffers(trade, originalTrade)),
Expand Down Expand Up @@ -124,7 +116,7 @@ export default function ConfirmSwapModal({

return (
<TransactionConfirmationModal
isOpen={isOpen}
isOpen={showConfirm}
onDismiss={onDismiss}
attemptingTxn={attemptingTxn}
hash={txHash}
Expand Down
3 changes: 3 additions & 0 deletions src/custom/components/swap/EthFlow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Wrap/unwrap flow

![wrap/unwrap flow](http://www.plantuml.com/plantuml/png/bLHDRzim3BtdLt3f8Oc1fjimO803R0Y6Tjrb1PeEkUmYMtIiLQ44IQuR3FllesGxTJzMO3d4iKG-liT7nWVXmYHVJSW41ATsTSZXfaQej6b1YuRA6ZqOqWTeQoGG49oG2AMX81JrL-StbsTR_e9vwuGzQoW-4iPYDL4LpBzvm7kBBi3US3QIA3JApmi84NH-GB-O05Wceu8o1DFpKNJ4cS9l3W28ODyHfGTXhJDtGZy50SYpR9qk4OAvHSgoLJfF6ZLMB75JcqPBA10a-jAf0YNNSjW5elO8Sp8T799KrUfO_LAm5ZBDheQeqd8cYQ26Ob08BQXariPrlJeI702PtpRsPvmsSEnPzavi427BfjidJPrc6PoUVXfnFMeVaSqt65wco0eKsIP0sJY7L1xwX2lBbxxbAcnlfTnU93FO1A4V6i1UZcEUsnW_9rPet3b4og-R7ZJDr4-EOrbvEhhWawLjzdengFV1s9TCY8YARlDrONWiOkuB6OFsFvE7vD5CFMjZcgcJuZs-hJxcF19jD8oyA5Fyynv0gqBDXjXOYNPSMWHaDwEewLhlZG-n4mxVm8NHKIs_CH4VqFhbSZcDv4wg3zKxBDWTal1r3NY7562MNg-SH3UBKNsZ7zlKkpZkDyotnrMBdUfWnp9wpXAjyIemsL9e3QQ2jB1nZnr7nff3qs3-OxJ0hwB6XtIYVHMsi-KLwMf78-dvMah-do5yR4SmNC-XREuPiOuh9cyUhdrdHhT6-HTjM4eab7ppgNfiwTfXvn08EW0wPvn_Ehx_O7aUWsV0MpsxnbzOXaIscyh_c9rKSqnscD_nl1_FkjRnFASVVm40)
51 changes: 51 additions & 0 deletions src/custom/components/swap/EthFlow/ethFlow.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@startuml
start
:;
note right: The flow launches only when a trade contains only ETH/WETH (wrap/unwrap flow);
if (Is expert mode enabled?) then (yes)
partition "Expert mode flow" {
if (Needs approval?) then (yes)
:**Approve token**;
note left: Should be described more;
else (no)
endif;
if (Has enough wrapped native token balance for trade?) then (no)
:**Wrap native token**;
note right: See **"Wrap/unwrap flow"** -->
else (yes)
endif;
:Set wrapped native token as input currency;
:Open swap confirmation modal;
if (Is swap confirmed?) then (yes)
#palegreen:Run swap flow \n (See: swapFlow.puml);
stop;
else (no)
#pink:Abort swap;
stop;
endif;
}
else (no)
partition "Wrap/unwrap flow" {
#b2e0f7:Show transaction confirmation modal with **pending state:** \n "Almost there! Follow these steps...";
#f4f1eb:Send GA event 'Send Order';
if (Is input currency native?) then (yes (Wrap flow))
:Send transaction with **deposit** call of WETH contract;
else (no (Unwrap flow))
:Send transaction with **withdraw** call of WETH contract;
endif;

#f4f1eb:Send GA event 'Sign Order';
:Add a new transaction to **redux enhancedTransactions state**;
:Close all modals;

if (Are there any errors?) then (yes)
#f4f1eb:Send GA event 'Error' or 'Reject';
#pink:Show transaction confirmation modal with **error state**;
stop;
else (no)
stop;
endif;
}
endif;
@enduml

12 changes: 7 additions & 5 deletions src/custom/components/swap/EthFlow/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BigNumber } from '@ethersproject/bignumber'
// eslint-disable-next-line no-restricted-imports
import { t } from '@lingui/macro'
import { useGasPrices } from 'state/gas/hooks'
import { PendingHashMap, Props } from 'components/swap/EthFlow'
import { PendingHashMap, EthFlowProps } from 'components/swap/EthFlow'
import { ActivityDerivedState } from 'components/AccountDetails/Transaction'
import { useSingleActivityState } from 'hooks/useActivityDerivedState'
import { ApprovalState } from 'hooks/useApproveCallback'
Expand Down Expand Up @@ -311,10 +311,13 @@ export function _getCurrencyForVisualiser<T>(native: T, wrapped: T, isWrap: bool
}
}

export type ActionButtonParams = Pick<Props, 'isNativeIn'> &
Pick<DerivedEthFlowStateProps, 'approveError' | 'wrapError' | 'approveState' | 'wrapState' | 'isExpertMode'> &
export type ActionButtonParams = Pick<
DerivedEthFlowStateProps,
'approveError' | 'wrapError' | 'approveState' | 'wrapState' | 'isExpertMode'
> &
Pick<ModalTextContentProps, 'nativeSymbol' | 'wrappedSymbol' | 'state'> & {
isWrap: boolean
isNativeIn: boolean
loading: boolean
handleSwap: (showSwapModal?: boolean) => Promise<void>
handleApprove: () => Promise<void>
Expand Down Expand Up @@ -417,8 +420,7 @@ export function _getActionButtonProps({
export function useEthFlowStatesAndSetters({
chainId,
approvalState,
wrapState,
}: Pick<Props, 'approvalState' | 'wrapState'> & {
}: Pick<EthFlowProps, 'approvalState'> & {
chainId?: number
}) {
const [pendingHashMap, setPendingHashMap] = useState<PendingHashMap>({
Expand Down
Loading