Skip to content

Commit

Permalink
fix: clean up integration exports (#171)
Browse files Browse the repository at this point in the history
* fix: export enums

* fix: simplify onSwitchTokens

* fix: allow boolean returns in handlers

* fix: simplify swap controller

* test: update switch tests
  • Loading branch information
zzmp authored Aug 25, 2022
1 parent 64605dd commit 5306dd0
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/components/ConnectWallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function ConnectWallet({ disabled }: ConnectWalletProps) {

const onConnectWalletClick = useAtomValue(onConnectWalletClickAtom)
const onClick = useCallback(async () => {
const open = await onConnectWalletClick?.()?.catch(() => false)
const open = await Promise.resolve(onConnectWalletClick?.()).catch(() => false)
setOpen(open ?? true)
}, [onConnectWalletClick])

Expand Down
2 changes: 1 addition & 1 deletion src/components/Swap/SwapButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default memo(function SwapButton({ disabled }: SwapButtonProps) {
: trade.state === TradeState.VALID
? {
onClick: async () => {
const open = await onReviewSwapClick?.()?.catch(() => false)
const open = await Promise.resolve(onReviewSwapClick?.())?.catch(() => false)
setOpen(open ?? true)
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/TokenSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default memo(function TokenSelect({ collapsed, disabled, field, onSelect,
const [open, setOpen] = useState(false)
const { onTokenSelectorClick } = useAtomValue(swapEventHandlersAtom)
const onOpen = useCallback(async () => {
const open = await onTokenSelectorClick?.(field)?.catch(() => false)
const open = await Promise.resolve(onTokenSelectorClick?.(field)).catch(() => false)
setOpen(open ?? true)
}, [field, onTokenSelectorClick])
const selectAndClose = useCallback(
Expand Down
12 changes: 2 additions & 10 deletions src/hooks/swap/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TradeType } from '@uniswap/sdk-core'
import { SupportedChainId } from 'constants/chains'
import { DAI, UNI, USDC_MAINNET } from 'constants/tokens'
import { useAtomValue } from 'jotai/utils'
Expand All @@ -19,13 +18,6 @@ const INITIAL_SWAP: Swap = {

describe('swap state', () => {
describe('useSwitchSwapCurrencies', () => {
const SWITCHED_SWAP = {
amount: INITIAL_SWAP.amount,
type: TradeType.EXACT_OUTPUT,
inputToken: INITIAL_SWAP[Field.OUTPUT],
outputToken: INITIAL_SWAP[Field.INPUT],
}

it('swaps currencies', () => {
const spy = jest.fn()
const { rerender } = renderHook(
Expand All @@ -40,7 +32,7 @@ describe('swap state', () => {
],
}
)
expect(spy).toHaveBeenCalledWith(SWITCHED_SWAP)
expect(spy).toHaveBeenCalled()

const { result } = rerender(() => useAtomValue(swapAtom))
expect(result.current).toMatchObject({
Expand All @@ -66,7 +58,7 @@ describe('swap state', () => {
],
}
)
expect(spy).toHaveBeenCalledWith(SWITCHED_SWAP)
expect(spy).toHaveBeenCalled()

const { result } = rerender(() => useAtomValue(swapAtom))
expect(result.current).toMatchObject(INITIAL_SWAP)
Expand Down
9 changes: 2 additions & 7 deletions src/hooks/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { useAtom } from 'jotai'
import { useAtomValue, useUpdateAtom } from 'jotai/utils'
import { useCallback, useMemo } from 'react'
Expand All @@ -23,12 +23,7 @@ export function useSwitchSwapCurrencies() {
const setSwap = useUpdateAtom(swapAtom)
return useCallback(() => {
setSwap((swap) => {
onSwitchTokens?.({
type: swap.independentField === Field.INPUT ? TradeType.EXACT_OUTPUT : TradeType.EXACT_INPUT,
amount: swap.amount,
inputToken: swap[Field.OUTPUT],
outputToken: swap[Field.INPUT],
})
onSwitchTokens?.()
const oldOutput = swap[Field.OUTPUT]
swap[Field.OUTPUT] = swap[Field.INPUT]
swap[Field.INPUT] = oldOutput
Expand Down
15 changes: 10 additions & 5 deletions src/hooks/swap/useSyncController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { TradeType } from '@uniswap/sdk-core'
import { Currency, TradeType } from '@uniswap/sdk-core'
import { useUpdateAtom } from 'jotai/utils'
import { useEffect, useRef } from 'react'
import { controlledAtom as swapAtom, Field, Swap, SwapController } from 'state/swap'
import { controlledAtom as swapAtom, Field, Swap } from 'state/swap'
import { controlledAtom as settingsAtom, Settings } from 'state/swap/settings'
export type { SwapController } from 'state/swap'

export type SwapSettingsController = Settings

export interface SwapController {
type?: TradeType
amount?: string
[Field.INPUT]?: Currency
[Field.OUTPUT]?: Currency
}

export default function useSyncController({
value,
settings,
Expand Down Expand Up @@ -36,10 +42,9 @@ function toSwap(value?: SwapController): Swap | undefined {
if (!value) return undefined

return {
...value,
independentField: value.type === TradeType.EXACT_INPUT ? Field.INPUT : Field.OUTPUT,
amount: value.amount || '',
[Field.INPUT]: value.inputToken,
[Field.OUTPUT]: value.outputToken,
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/hooks/useSyncWidgetEventHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { useUpdateAtom } from 'jotai/utils'
import { useEffect } from 'react'
import { onConnectWalletClickAtom } from 'state/wallet'

export type OnConnectWalletClick = () => void | Promise<boolean>
import { OnConnectWalletClick, onConnectWalletClickAtom } from 'state/wallet'
export type { OnConnectWalletClick } from 'state/wallet'

export interface WidgetEventHandlers {
onConnectWalletClick?: OnConnectWalletClick
}

export default function useSyncWidgetEventHandlers({ onConnectWalletClick }: WidgetEventHandlers): void {
const setOnConnectWalletClick = useUpdateAtom(onConnectWalletClickAtom)
useEffect(
() => setOnConnectWalletClick((old) => (old = onConnectWalletClick)),
[onConnectWalletClick, setOnConnectWalletClick]
)
useEffect(() => setOnConnectWalletClick(onConnectWalletClick), [onConnectWalletClick, setOnConnectWalletClick])
}
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'polyfills'
import Swap, { SwapProps } from 'components/Swap'
import Widget, { WidgetProps } from 'components/Widget'
export type { Provider as EthersProvider } from '@ethersproject/abstract-provider'
export type { Currency, TradeType } from '@uniswap/sdk-core'
export type { Currency } from '@uniswap/sdk-core'
export { TradeType } from '@uniswap/sdk-core'
export type { TokenInfo } from '@uniswap/token-lists'
export type { Provider as Eip1193Provider } from '@web3-react/types'
export type { ErrorHandler } from 'components/Error/ErrorBoundary'
Expand All @@ -17,7 +18,6 @@ export type { DefaultAddress, TokenDefaults } from 'hooks/swap/useSyncTokenDefau
export type { OnTxFail, OnTxSubmit, OnTxSuccess, TransactionEventHandlers } from 'hooks/transactions'
export type { OnConnectWalletClick, WidgetEventHandlers } from 'hooks/useSyncWidgetEventHandlers'
export type {
Field,
OnAmountChange,
OnReviewSwapClick,
OnSettingsReset,
Expand All @@ -28,6 +28,7 @@ export type {
OnTransactionDeadlineChange,
SwapEventHandlers,
} from 'state/swap'
export { Field } from 'state/swap'
export type { Slippage } from 'state/swap/settings'
export type { Theme } from 'theme'
export { darkTheme, defaultTheme, lightTheme } from 'theme'
Expand Down
20 changes: 5 additions & 15 deletions src/state/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Currency, TradeType } from '@uniswap/sdk-core'
import { Currency } from '@uniswap/sdk-core'
import { FeeOptions } from '@uniswap/v3-sdk'
import { SupportedChainId } from 'constants/chains'
import { nativeOnChain } from 'constants/tokens'
Expand All @@ -12,13 +12,6 @@ export enum Field {
OUTPUT = 'OUTPUT',
}

export interface SwapController {
type?: TradeType
amount?: string
inputToken?: Currency
outputToken?: Currency
}

export interface Swap {
independentField: Field
amount: string
Expand Down Expand Up @@ -59,23 +52,20 @@ export type OnTokenChange = (field: Field, token: Currency) => void
/** An integration hook called when the user enters a new amount. */
export type OnAmountChange = (field: Field, amount: string) => void

/**
* An integration hook called when the user switches the tokens.
* The values represent already-switched state, to make it easier to update controlled state.
*/
export type OnSwitchTokens = (values: SwapController) => void
/** An integration hook called when the user switches the tokens. */
export type OnSwitchTokens = () => void

/**
* An integration hook called when the user clicks 'Review swap'.
* If the hook resolves to false or rejects, the review dialog will not open.
*/
export type OnReviewSwapClick = () => void | Promise<boolean>
export type OnReviewSwapClick = () => void | boolean | Promise<boolean>

/**
* An integration hook called when the user clicks the token selector.
* If the hook resolve to false or rejects, the token selector will not open.
*/
export type OnTokenSelectorClick = (field: Field) => void | Promise<boolean>
export type OnTokenSelectorClick = (field: Field) => void | boolean | Promise<boolean>

export interface SwapEventHandlers {
onSettingsReset?: OnSettingsReset
Expand Down
4 changes: 3 additions & 1 deletion src/state/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { atom } from 'jotai'

export type OnConnectWalletClick = () => void | boolean | Promise<boolean>

// If set, allows integrator to add behavior when 'Connect wallet to swap' button is clicked
export const onConnectWalletClickAtom = atom<(() => void | Promise<boolean>) | undefined>(undefined)
export const onConnectWalletClickAtom = atom<OnConnectWalletClick | undefined>(undefined)

1 comment on commit 5306dd0

@vercel
Copy link

@vercel vercel bot commented on 5306dd0 Aug 25, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

widgets – ./

widgets-git-main-uniswap.vercel.app
widgets-seven-tau.vercel.app
widgets-uniswap.vercel.app

Please sign in to comment.