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

feat: use Swap Component on TDP #6332

Merged
merged 39 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9698bd4
test: swap flow cypress tests
just-toby Apr 10, 2023
16dd116
fix: use default parameter
just-toby Apr 10, 2023
129b28a
feat: use Swap Component on TDP
just-toby Apr 11, 2023
7236cba
feat: auto nav for TDP tokens
just-toby Apr 11, 2023
29643f9
chore: merge
just-toby Apr 20, 2023
8a80368
chore: merge
just-toby Apr 20, 2023
0b68684
chore: merge
just-toby Apr 20, 2023
cab2b59
chore: merge
just-toby Apr 20, 2023
f5ab2b6
chore: merge
just-toby Apr 24, 2023
82ae506
chore: merge
just-toby Apr 24, 2023
275656e
fix: remove extra inputCurrency URL parsing logic
just-toby Apr 24, 2023
36a3a88
fix: undo last change
just-toby Apr 24, 2023
cc4fce9
fix: pass expected chain id to swap component
just-toby Apr 24, 2023
3c57ef8
fix: search for default tokens on unconnected networks if needed
just-toby Apr 24, 2023
8223dcd
test: e2e test for l2 token
just-toby Apr 24, 2023
15cac1f
fix: delete irrelevant tests
just-toby Apr 24, 2023
be8b155
fix: address comments
just-toby Apr 24, 2023
7408bc4
fix: lint error
just-toby Apr 24, 2023
18d9d83
test: update TDP e2e tests
just-toby Apr 24, 2023
9f3b158
fix: use pageChainId for filter
just-toby Apr 25, 2023
626234b
fix: rename chainId
just-toby Apr 25, 2023
8ddb100
fix: typecheck
just-toby Apr 25, 2023
aa51523
fix: chainId bug
just-toby Apr 25, 2023
8f2977a
chore: merge
just-toby Apr 25, 2023
ba7b6d3
fix: chainId required fixes
just-toby Apr 25, 2023
e49ee48
fix: bad merge in e2e test
just-toby Apr 26, 2023
21ee43e
chore: merge
just-toby Apr 26, 2023
e4c4e90
fix: remove unused test util
just-toby Apr 26, 2023
c3af201
fix: remove unnecessary variable
just-toby Apr 26, 2023
e7e2c38
fix: token defaults
just-toby Apr 26, 2023
9fb00db
chore: merge
just-toby Apr 26, 2023
a53d0a7
fix: address comments
just-toby Apr 27, 2023
3041007
fix: address comments and fix tests
just-toby Apr 27, 2023
74746f8
fix: e2e test formatting, remove Maybe<>
just-toby Apr 27, 2023
f09a181
fix: remove unused variable
just-toby Apr 27, 2023
022d132
chore: merge
just-toby Apr 28, 2023
05bc88e
fix: use feature flag for swap component on TDP
just-toby May 2, 2023
6f347a1
fix: back button
just-toby May 2, 2023
d131136
fix: merge
just-toby May 2, 2023
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
Prev Previous commit
Next Next commit
fix: e2e test formatting, remove Maybe<>
  • Loading branch information
just-toby committed Apr 27, 2023
commit 74746f8900307bfb0686aae541cf1ff6961435aa
108 changes: 54 additions & 54 deletions cypress/e2e/swap.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { USDC_MAINNET } from '../../src/constants/tokens'
import { UNI_GOERLI, WETH_GOERLI } from '../fixtures/constants'
import { WETH9 } from '@uniswap/sdk-core'

import { UNI as UNI_MAINNET, USDC_MAINNET } from '../../src/constants/tokens'
import { WETH_GOERLI } from '../fixtures/constants'
import { HardhatProvider } from '../support/hardhat'
import { getTestSelector } from '../utils'

Expand Down Expand Up @@ -180,69 +182,67 @@ describe('Swap', () => {
})
})
})
})

describe('Swap on Token Detail Page', () => {
beforeEach(() => {
// On mobile widths, we just link back to /swap instead of rendering the swap component.
cy.viewport(1200, 800)
cy.visit(`/tokens/goerli/${UNI_GOERLI}`).then(() => {
cy.wait('@eth_blockNumber')
cy.scrollTo('top')
})
describe('Swap on Token Detail Page', () => {
beforeEach(() => {
// On mobile widths, we just link back to /swap instead of rendering the swap component.
cy.viewport(1200, 800)
cy.visit(`/tokens/ethereum/${UNI_MAINNET[1].address}`, { ethereum: 'hardhat' }).then(() => {
cy.wait('@eth_blockNumber')
cy.scrollTo('top')
})
})

it('should have the expected output for a tokens detail page', () => {
verifyAmount('input', '')
verifyToken('input', null)
verifyAmount('output', null)
verifyToken('output', 'UNI')
})
it('should have the expected output for a tokens detail page', () => {
verifyAmount('input', '')
verifyToken('input', null)
verifyAmount('output', null)
verifyToken('output', 'UNI')
})

it('should automatically navigate to the new TDP', () => {
selectToken('WETH', 'output')
cy.url().should('include', `${WETH_GOERLI}`)
cy.url().should('not.include', `${UNI_GOERLI}`)
})
it('should automatically navigate to the new TDP', () => {
selectToken('WETH', 'output')
cy.url().should('include', `${WETH9[1].address}`)
cy.url().should('not.include', `${UNI_MAINNET[1].address}`)
})

it('should not share swap state with the main swap page', () => {
verifyToken('output', 'UNI')
selectToken('WETH', 'input')
cy.visit('/swap')
cy.contains('UNI').should('not.exist')
cy.contains('WETH').should('not.exist')
})
it('should not share swap state with the main swap page', () => {
verifyToken('output', 'UNI')
selectToken('WETH', 'input')
cy.visit('/swap')
cy.contains('UNI').should('not.exist')
cy.contains('WETH').should('not.exist')
})

it('can enter an amount into input', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('0.001').should('have.value', '0.001')
})
it('can enter an amount into input', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('0.001').should('have.value', '0.001')
})

it('zero swap amount', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('0.0').should('have.value', '0.0')
})
it('zero swap amount', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('0.0').should('have.value', '0.0')
})

it('invalid swap amount', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('\\').should('have.value', '')
})
it('invalid swap amount', () => {
cy.get('#swap-currency-input .token-amount-input').clear().type('\\').should('have.value', '')
})

it('can enter an amount into output', () => {
cy.get('#swap-currency-output .token-amount-input').clear().type('0.001').should('have.value', '0.001')
})
it('can enter an amount into output', () => {
cy.get('#swap-currency-output .token-amount-input').clear().type('0.001').should('have.value', '0.001')
})

it('zero output amount', () => {
cy.get('#swap-currency-output .token-amount-input').clear().type('0.0').should('have.value', '0.0')
})
it('zero output amount', () => {
cy.get('#swap-currency-output .token-amount-input').clear().type('0.0').should('have.value', '0.0')
})

it('should show a L2 token even if the user is connected to a different network', () => {
cy.visit('/tokens').then(() => {
cy.wait('@eth_blockNumber')
})
cy.get(getTestSelector('tokens-network-filter-selected')).click()
cy.get(getTestSelector('tokens-network-filter-option-arbitrum')).click()
cy.get(getTestSelector('tokens-network-filter-selected')).should('contain', 'Arbitrum')
cy.get(getTestSelector('token-table-row-ARB')).click()
verifyToken('output', 'ARB')
cy.contains('Connect to Arbitrum').should('exist')
})
it('should show a L2 token even if the user is connected to a different network', () => {
cy.visit('/tokens', { ethereum: 'hardhat' })
cy.get(getTestSelector('tokens-network-filter-selected')).click()
cy.get(getTestSelector('tokens-network-filter-option-arbitrum')).click()
cy.get(getTestSelector('tokens-network-filter-selected')).should('contain', 'Arbitrum')
cy.get(getTestSelector('token-table-row-ARB')).click()
verifyToken('output', 'ARB')
cy.contains('Connect to Arbitrum').should('exist')
})
})
})
5 changes: 1 addition & 4 deletions src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,7 @@ export function getSwapCurrencyId(currency: Currency): string {
if (currency.isToken) {
return currency.address
}
if (currency.isNative) {
return NATIVE_CHAIN_ID
}
return ''
return NATIVE_CHAIN_ID
}

export const TOKEN_SHORTHANDS: { [shorthand: string]: { [chainId in SupportedChainId]?: string } } = {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useWeb3React } from '@web3-react/core'
import { getChainInfo } from 'constants/chainInfo'
import { SupportedChainId } from 'constants/chains'
import { DEFAULT_INACTIVE_LIST_URLS, DEFAULT_LIST_OF_LISTS } from 'constants/lists'
import { Maybe } from 'graphql/jsutils/Maybe'
import { useCurrencyFromMap, useTokenFromMapOrNetwork } from 'lib/hooks/useCurrency'
import { getTokenFilter } from 'lib/hooks/useTokenList/filtering'
import { useMemo } from 'react'
Expand All @@ -14,6 +13,8 @@ import { WrappedTokenInfo } from '../state/lists/wrappedTokenInfo'
import { useUserAddedTokens } from '../state/user/hooks'
import { TokenAddressMap, useUnsupportedTokenList } from './../state/lists/hooks'

type Maybe<T> = T | null | undefined

// reduce token map into standard address <-> Token mapping, optionally include user added tokens
function useTokensFromMap(tokenMap: TokenAddressMap, chainId: Maybe<SupportedChainId>): { [address: string]: Token } {
return useMemo(() => {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/hooks/useCurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { parseBytes32String } from '@ethersproject/strings'
import { Currency, Token } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
import { isSupportedChain, SupportedChainId } from 'constants/chains'
import { Maybe } from 'graphql/jsutils/Maybe'
import { useBytes32TokenContract, useTokenContract } from 'hooks/useContract'
import { NEVER_RELOAD, useSingleCallResult } from 'lib/hooks/multicall'
import useNativeCurrency from 'lib/hooks/useNativeCurrency'
Expand Down Expand Up @@ -95,7 +94,7 @@ export function useTokenFromMapOrNetwork(tokens: TokenMap, tokenAddress?: string
*/
export function useCurrencyFromMap(
tokens: TokenMap,
chainId: Maybe<SupportedChainId>,
chainId: SupportedChainId | undefined,
currencyId?: string | null
): Currency | null | undefined {
const nativeCurrency = useNativeCurrency(chainId)
Expand Down
3 changes: 1 addition & 2 deletions src/lib/hooks/useNativeCurrency.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { NativeCurrency, Token } from '@uniswap/sdk-core'
import { SupportedChainId } from 'constants/chains'
import { nativeOnChain } from 'constants/tokens'
import { Maybe } from 'graphql/jsutils/Maybe'
import { useMemo } from 'react'

export default function useNativeCurrency(chainId: Maybe<SupportedChainId>): NativeCurrency | Token {
export default function useNativeCurrency(chainId: SupportedChainId | null | undefined): NativeCurrency | Token {
return useMemo(
() =>
chainId
Expand Down
3 changes: 1 addition & 2 deletions src/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { ArrowWrapper, PageWrapper, SwapCallbackError, SwapWrapper } from '../..
import SwapHeader from '../../components/swap/SwapHeader'
import { SwitchLocaleLink } from '../../components/SwitchLocaleLink'
import { getSwapCurrencyId, TOKEN_SHORTHANDS } from '../../constants/tokens'
import { NATIVE_CHAIN_ID } from '../../constants/tokens'
import { useCurrency, useDefaultActiveTokens } from '../../hooks/Tokens'
import { useIsSwapUnsupported } from '../../hooks/useIsSwapUnsupported'
import useWrapCallback, { WrapErrorText, WrapType } from '../../hooks/useWrapCallback'
Expand Down Expand Up @@ -502,7 +501,7 @@ export function Swap({
onCurrencyChange?.({
[Field.INPUT]: state[Field.INPUT],
[Field.OUTPUT]: {
currencyId: outputCurrency.isToken ? outputCurrency.address : outputCurrency.isNative ? NATIVE_CHAIN_ID : '',
currencyId: getSwapCurrencyId(outputCurrency),
},
})
},
Expand Down
4 changes: 1 addition & 3 deletions src/utils/addressesAreEquivalent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Maybe } from 'graphql/jsutils/Maybe'

export function addressesAreEquivalent(a: Maybe<string>, b: Maybe<string>) {
export function addressesAreEquivalent(a: string | null | undefined, b: string | null | undefined) {
if (!a || !b) return false
return a === b || a.toLowerCase() === b.toLowerCase()
}
3 changes: 1 addition & 2 deletions src/utils/supportedChainId.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SupportedChainId } from 'constants/chains'
import { Maybe } from 'graphql/jsutils/Maybe'

/**
* Returns the input chain ID if chain is supported. If not, return undefined
* @param chainId a chain ID, which will be returned if it is a supported chain ID
*/
export function supportedChainId(chainId: Maybe<number>): SupportedChainId | undefined {
export function supportedChainId(chainId: number | null | undefined): SupportedChainId | undefined {
if (typeof chainId === 'number' && chainId in SupportedChainId) {
return chainId
}
Expand Down