feat: export types (#3411)
* feat: export used types * fix: clean defaulting names * chore: rename to useSyncTokenDefaults
This commit is contained in:
parent
52af0e506b
commit
2de29129ed
@ -37,38 +37,33 @@ function Fixture() {
|
|||||||
defaultValue: FEE_RECIPIENT_OPTIONS[1],
|
defaultValue: FEE_RECIPIENT_OPTIONS[1],
|
||||||
})
|
})
|
||||||
|
|
||||||
const optionsToAddressMap: Record<string, string> = {
|
const optionsToAddressMap: Record<string, string | undefined> = {
|
||||||
none: '',
|
None: undefined,
|
||||||
Native: 'NATIVE',
|
Native: 'NATIVE',
|
||||||
DAI: DAI.address,
|
DAI: DAI.address,
|
||||||
USDC: USDC_MAINNET.address,
|
USDC: USDC_MAINNET.address,
|
||||||
}
|
}
|
||||||
const addressOptions = Object.keys(optionsToAddressMap)
|
const addressOptions = Object.keys(optionsToAddressMap)
|
||||||
const [defaultInput] = useSelect('defaultInputAddress', {
|
|
||||||
options: addressOptions,
|
const [defaultInputToken] = useSelect('defaultInputToken', {
|
||||||
defaultValue: addressOptions[2],
|
|
||||||
})
|
|
||||||
const inputOptions = ['', '0', '100', '-1']
|
|
||||||
const [defaultInputAmount] = useSelect('defaultInputAmount', {
|
|
||||||
options: inputOptions,
|
|
||||||
defaultValue: inputOptions[2],
|
|
||||||
})
|
|
||||||
const [defaultOutput] = useSelect('defaultOutputAddress', {
|
|
||||||
options: addressOptions,
|
options: addressOptions,
|
||||||
defaultValue: addressOptions[1],
|
defaultValue: addressOptions[1],
|
||||||
})
|
})
|
||||||
const [defaultOutputAmount] = useSelect('defaultOutputAmount', {
|
const [defaultInputAmount] = useValue('defaultInputAmount', { defaultValue: 1 })
|
||||||
options: inputOptions,
|
|
||||||
defaultValue: inputOptions[0],
|
const [defaultOutputToken] = useSelect('defaultOutputToken', {
|
||||||
|
options: addressOptions,
|
||||||
|
defaultValue: addressOptions[2],
|
||||||
})
|
})
|
||||||
|
const [defaultOutputAmount] = useValue('defaultOutputAmount', { defaultValue: 0 })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Swap
|
<Swap
|
||||||
convenienceFee={convenienceFee}
|
convenienceFee={convenienceFee}
|
||||||
convenienceFeeRecipient={convenienceFeeRecipient}
|
convenienceFeeRecipient={convenienceFeeRecipient}
|
||||||
defaultInputAddress={optionsToAddressMap[defaultInput]}
|
defaultInputTokenAddress={optionsToAddressMap[defaultInputToken]}
|
||||||
defaultInputAmount={defaultInputAmount}
|
defaultInputAmount={defaultInputAmount}
|
||||||
defaultOutputAddress={optionsToAddressMap[defaultOutput]}
|
defaultOutputTokenAddress={optionsToAddressMap[defaultOutputToken]}
|
||||||
defaultOutputAmount={defaultOutputAmount}
|
defaultOutputAmount={defaultOutputAmount}
|
||||||
tokenList={tokens}
|
tokenList={tokens}
|
||||||
onConnectWallet={() => console.log('onConnectWallet')} // this handler is included as a test of functionality, but only logs
|
onConnectWallet={() => console.log('onConnectWallet')} // this handler is included as a test of functionality, but only logs
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { BigNumber } from '@ethersproject/bignumber'
|
import { BigNumber } from '@ethersproject/bignumber'
|
||||||
import { DefaultAddress, SwapProps } from 'lib/components/Swap'
|
|
||||||
import { IntegrationError } from 'lib/errors'
|
import { IntegrationError } from 'lib/errors'
|
||||||
|
import { FeeOptions } from 'lib/hooks/swap/useSyncConvenienceFee'
|
||||||
|
import { DefaultAddress, TokenDefaults } from 'lib/hooks/swap/useSyncTokenDefaults'
|
||||||
import { PropsWithChildren, useEffect } from 'react'
|
import { PropsWithChildren, useEffect } from 'react'
|
||||||
|
|
||||||
import { isAddress } from '../../../utils'
|
import { isAddress } from '../../../utils'
|
||||||
@ -15,7 +16,7 @@ function isAddressOrAddressMap(addressOrMap: DefaultAddress): boolean {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidatorProps = PropsWithChildren<SwapProps>
|
type ValidatorProps = PropsWithChildren<TokenDefaults & FeeOptions>
|
||||||
|
|
||||||
export default function SwapPropValidator(props: ValidatorProps) {
|
export default function SwapPropValidator(props: ValidatorProps) {
|
||||||
const { convenienceFee, convenienceFeeRecipient } = props
|
const { convenienceFee, convenienceFeeRecipient } = props
|
||||||
@ -47,7 +48,7 @@ export default function SwapPropValidator(props: ValidatorProps) {
|
|||||||
}
|
}
|
||||||
}, [convenienceFee, convenienceFeeRecipient])
|
}, [convenienceFee, convenienceFeeRecipient])
|
||||||
|
|
||||||
const { defaultInputAddress, defaultInputAmount, defaultOutputAddress, defaultOutputAmount } = props
|
const { defaultInputTokenAddress, defaultInputAmount, defaultOutputTokenAddress, defaultOutputAmount } = props
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (defaultOutputAmount && defaultInputAmount) {
|
if (defaultOutputAmount && defaultInputAmount) {
|
||||||
throw new IntegrationError('defaultInputAmount and defaultOutputAmount may not both be defined.')
|
throw new IntegrationError('defaultInputAmount and defaultOutputAmount may not both be defined.')
|
||||||
@ -60,17 +61,25 @@ export default function SwapPropValidator(props: ValidatorProps) {
|
|||||||
`defaultOutputAmount must be a positive number. (You set it to ${defaultOutputAmount})`
|
`defaultOutputAmount must be a positive number. (You set it to ${defaultOutputAmount})`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (defaultInputAddress && !isAddressOrAddressMap(defaultInputAddress) && defaultInputAddress !== 'NATIVE') {
|
if (
|
||||||
|
defaultInputTokenAddress &&
|
||||||
|
!isAddressOrAddressMap(defaultInputTokenAddress) &&
|
||||||
|
defaultInputTokenAddress !== 'NATIVE'
|
||||||
|
) {
|
||||||
throw new IntegrationError(
|
throw new IntegrationError(
|
||||||
`defaultInputAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultInputAddress}`
|
`defaultInputTokenAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultInputTokenAddress}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (defaultOutputAddress && !isAddressOrAddressMap(defaultOutputAddress) && defaultOutputAddress !== 'NATIVE') {
|
if (
|
||||||
|
defaultOutputTokenAddress &&
|
||||||
|
!isAddressOrAddressMap(defaultOutputTokenAddress) &&
|
||||||
|
defaultOutputTokenAddress !== 'NATIVE'
|
||||||
|
) {
|
||||||
throw new IntegrationError(
|
throw new IntegrationError(
|
||||||
`defaultOutputAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultOutputAddress}`
|
`defaultOutputTokenAddress(es) must be a valid address or "NATIVE". (You set it to ${defaultOutputTokenAddress}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}, [defaultInputAddress, defaultInputAmount, defaultOutputAddress, defaultOutputAmount])
|
}, [defaultInputTokenAddress, defaultInputAmount, defaultOutputTokenAddress, defaultOutputAmount])
|
||||||
|
|
||||||
return <>{props.children}</>
|
return <>{props.children}</>
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ import { Trans } from '@lingui/macro'
|
|||||||
import { TokenInfo } from '@uniswap/token-lists'
|
import { TokenInfo } from '@uniswap/token-lists'
|
||||||
import { useAtom } from 'jotai'
|
import { useAtom } from 'jotai'
|
||||||
import { SwapInfoUpdater } from 'lib/hooks/swap/useSwapInfo'
|
import { SwapInfoUpdater } from 'lib/hooks/swap/useSwapInfo'
|
||||||
import useSyncConvenienceFee from 'lib/hooks/swap/useSyncConvenienceFee'
|
import useSyncConvenienceFee, { FeeOptions } from 'lib/hooks/swap/useSyncConvenienceFee'
|
||||||
import useSyncSwapDefaults from 'lib/hooks/swap/useSyncSwapDefaults'
|
import useSyncTokenDefaults, { TokenDefaults } from 'lib/hooks/swap/useSyncTokenDefaults'
|
||||||
import { usePendingTransactions } from 'lib/hooks/transactions'
|
import { usePendingTransactions } from 'lib/hooks/transactions'
|
||||||
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
|
||||||
import useHasFocus from 'lib/hooks/useHasFocus'
|
import useHasFocus from 'lib/hooks/useHasFocus'
|
||||||
@ -26,8 +26,6 @@ import SwapButton from './SwapButton'
|
|||||||
import SwapPropValidator from './SwapPropValidator'
|
import SwapPropValidator from './SwapPropValidator'
|
||||||
import Toolbar from './Toolbar'
|
import Toolbar from './Toolbar'
|
||||||
|
|
||||||
export type DefaultAddress = string | { [chainId: number]: string | 'NATIVE' } | 'NATIVE'
|
|
||||||
|
|
||||||
function getTransactionFromMap(
|
function getTransactionFromMap(
|
||||||
txs: { [hash: string]: Transaction },
|
txs: { [hash: string]: Transaction },
|
||||||
hash?: string
|
hash?: string
|
||||||
@ -44,20 +42,14 @@ function getTransactionFromMap(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SwapProps {
|
export interface SwapProps extends TokenDefaults, FeeOptions {
|
||||||
tokenList?: string | TokenInfo[]
|
tokenList?: string | TokenInfo[]
|
||||||
defaultInputAddress?: DefaultAddress
|
|
||||||
defaultInputAmount?: string
|
|
||||||
defaultOutputAddress?: DefaultAddress
|
|
||||||
defaultOutputAmount?: string
|
|
||||||
convenienceFee?: number
|
|
||||||
convenienceFeeRecipient?: string | { [chainId: number]: string }
|
|
||||||
onConnectWallet?: () => void
|
onConnectWallet?: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Swap(props: SwapProps) {
|
export default function Swap(props: SwapProps) {
|
||||||
useSyncTokenList(props.tokenList)
|
useSyncTokenList(props.tokenList)
|
||||||
useSyncSwapDefaults(props)
|
useSyncTokenDefaults(props)
|
||||||
useSyncConvenienceFee(props)
|
useSyncConvenienceFee(props)
|
||||||
|
|
||||||
const { active, account } = useActiveWeb3React()
|
const { active, account } = useActiveWeb3React()
|
||||||
|
@ -4,12 +4,12 @@ import { useUpdateAtom } from 'jotai/utils'
|
|||||||
import { feeOptionsAtom } from 'lib/state/swap'
|
import { feeOptionsAtom } from 'lib/state/swap'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
|
|
||||||
interface FeeOptionsArgs {
|
export interface FeeOptions {
|
||||||
convenienceFee?: number
|
convenienceFee?: number
|
||||||
convenienceFeeRecipient?: string | string | { [chainId: number]: string }
|
convenienceFeeRecipient?: string | string | { [chainId: number]: string }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useSyncConvenienceFee({ convenienceFee, convenienceFeeRecipient }: FeeOptionsArgs) {
|
export default function useSyncConvenienceFee({ convenienceFee, convenienceFeeRecipient }: FeeOptions) {
|
||||||
const { chainId } = useActiveWeb3React()
|
const { chainId } = useActiveWeb3React()
|
||||||
const updateFeeOptions = useUpdateAtom(feeOptionsAtom)
|
const updateFeeOptions = useUpdateAtom(feeOptionsAtom)
|
||||||
|
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
import { Currency } from '@uniswap/sdk-core'
|
import { Currency } from '@uniswap/sdk-core'
|
||||||
import { nativeOnChain } from 'constants/tokens'
|
import { nativeOnChain } from 'constants/tokens'
|
||||||
import { useUpdateAtom } from 'jotai/utils'
|
import { useUpdateAtom } from 'jotai/utils'
|
||||||
import { DefaultAddress } from 'lib/components/Swap'
|
|
||||||
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
|
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
|
||||||
import { useToken } from 'lib/hooks/useCurrency'
|
import { useToken } from 'lib/hooks/useCurrency'
|
||||||
import { Field, Swap, swapAtom } from 'lib/state/swap'
|
import { Field, Swap, swapAtom } from 'lib/state/swap'
|
||||||
import { useCallback, useLayoutEffect, useState } from 'react'
|
import { useCallback, useLayoutEffect, useState } from 'react'
|
||||||
|
|
||||||
|
export type DefaultAddress = string | { [chainId: number]: string | 'NATIVE' } | 'NATIVE'
|
||||||
|
|
||||||
|
export interface TokenDefaults {
|
||||||
|
defaultInputTokenAddress?: DefaultAddress
|
||||||
|
defaultInputAmount?: number | string
|
||||||
|
defaultOutputTokenAddress?: DefaultAddress
|
||||||
|
defaultOutputAmount?: number | string
|
||||||
|
}
|
||||||
|
|
||||||
function useDefaultToken(
|
function useDefaultToken(
|
||||||
defaultAddress: DefaultAddress | undefined,
|
defaultAddress: DefaultAddress | undefined,
|
||||||
chainId: number | undefined
|
chainId: number | undefined
|
||||||
@ -24,23 +32,16 @@ function useDefaultToken(
|
|||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UseSwapDefaultsArgs {
|
export default function useSyncTokenDefaults({
|
||||||
defaultInputAddress?: DefaultAddress
|
defaultInputTokenAddress,
|
||||||
defaultInputAmount?: string
|
|
||||||
defaultOutputAddress?: DefaultAddress
|
|
||||||
defaultOutputAmount?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function useSyncSwapDefaults({
|
|
||||||
defaultInputAddress,
|
|
||||||
defaultInputAmount,
|
defaultInputAmount,
|
||||||
defaultOutputAddress,
|
defaultOutputTokenAddress,
|
||||||
defaultOutputAmount,
|
defaultOutputAmount,
|
||||||
}: UseSwapDefaultsArgs) {
|
}: TokenDefaults) {
|
||||||
const updateSwap = useUpdateAtom(swapAtom)
|
const updateSwap = useUpdateAtom(swapAtom)
|
||||||
const { chainId } = useActiveWeb3React()
|
const { chainId } = useActiveWeb3React()
|
||||||
const defaultInputToken = useDefaultToken(defaultInputAddress, chainId)
|
const defaultInputToken = useDefaultToken(defaultInputTokenAddress, chainId)
|
||||||
const defaultOutputToken = useDefaultToken(defaultOutputAddress, chainId)
|
const defaultOutputToken = useDefaultToken(defaultOutputTokenAddress, chainId)
|
||||||
|
|
||||||
const setToDefaults = useCallback(() => {
|
const setToDefaults = useCallback(() => {
|
||||||
const defaultSwapState: Swap = {
|
const defaultSwapState: Swap = {
|
||||||
@ -49,14 +50,14 @@ export default function useSyncSwapDefaults({
|
|||||||
[Field.OUTPUT]: defaultOutputToken || undefined,
|
[Field.OUTPUT]: defaultOutputToken || undefined,
|
||||||
independentField: Field.INPUT,
|
independentField: Field.INPUT,
|
||||||
}
|
}
|
||||||
if (defaultInputAmount && defaultInputToken) {
|
if (defaultInputToken && defaultInputAmount) {
|
||||||
defaultSwapState.amount = defaultInputAmount
|
defaultSwapState.amount = defaultInputAmount.toString()
|
||||||
} else if (defaultOutputAmount && defaultOutputToken) {
|
} else if (defaultOutputToken && defaultOutputAmount) {
|
||||||
defaultSwapState.independentField = Field.OUTPUT
|
defaultSwapState.independentField = Field.OUTPUT
|
||||||
defaultSwapState.amount = defaultOutputAmount
|
defaultSwapState.amount = defaultOutputAmount.toString()
|
||||||
}
|
}
|
||||||
updateSwap((swap) => ({ ...swap, ...defaultSwapState }))
|
updateSwap((swap) => ({ ...swap, ...defaultSwapState }))
|
||||||
}, [defaultInputToken, defaultOutputToken, defaultInputAmount, defaultOutputAmount, updateSwap])
|
}, [defaultInputAmount, defaultInputToken, defaultOutputAmount, defaultOutputToken, updateSwap])
|
||||||
|
|
||||||
const [previousChainId, setPreviousChainId] = useState(chainId)
|
const [previousChainId, setPreviousChainId] = useState(chainId)
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
@ -1,10 +1,18 @@
|
|||||||
import Swap, { SwapProps } from './components/Swap'
|
import Swap, { SwapProps } from './components/Swap'
|
||||||
import Widget, { WidgetProps } from './components/Widget'
|
import Widget, { WidgetProps } from './components/Widget'
|
||||||
|
|
||||||
|
export type { ErrorHandler } from './components/Error/ErrorBoundary'
|
||||||
|
export type { FeeOptions } from './hooks/swap/useSyncConvenienceFee'
|
||||||
|
export type { DefaultAddress, TokenDefaults } from './hooks/swap/useSyncTokenDefaults'
|
||||||
|
export type { Theme } from './theme'
|
||||||
export { darkTheme, lightTheme } from './theme'
|
export { darkTheme, lightTheme } from './theme'
|
||||||
export type { Theme } from './theme/theme'
|
export type { Provider as EthersProvider } from '@ethersproject/abstract-provider'
|
||||||
|
export type { TokenInfo } from '@uniswap/token-lists'
|
||||||
|
export type { Provider as Eip1193Provider } from '@web3-react/types'
|
||||||
|
export type { SupportedLocale } from 'constants/locales'
|
||||||
export { SUPPORTED_LOCALES } from 'constants/locales'
|
export { SUPPORTED_LOCALES } from 'constants/locales'
|
||||||
|
|
||||||
type SwapWidgetProps = SwapProps & WidgetProps
|
export type SwapWidgetProps = SwapProps & WidgetProps
|
||||||
|
|
||||||
export function SwapWidget(props: SwapWidgetProps) {
|
export function SwapWidget(props: SwapWidgetProps) {
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user