Skip to content

Commit

Permalink
more strictness everywhere, fix a pair pricing issue in mint/hooks.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Aug 27, 2020
1 parent 610b7f4 commit 66a2006
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 105 deletions.
1 change: 1 addition & 0 deletions src/components/AccountDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export default function AccountDetails({
</>
)
}
return null
}

const clearAllTransactionsCallback = useCallback(
Expand Down
5 changes: 0 additions & 5 deletions src/components/AddressInputPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ const Input = styled.input<{ error?: boolean }>`
}
`

interface Value {
address: string
name?: string
}

export default function AddressInputPanel({
id,
value,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Popups/PopupItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function PopupItem({
const removePopup = useRemovePopup()
const removeThisPopup = useCallback(() => removePopup(popKey), [popKey, removePopup])
useEffect(() => {
if (removeAfterMs === null) return
if (removeAfterMs === null) return undefined

const timeout = setTimeout(() => {
removeThisPopup()
Expand Down
147 changes: 81 additions & 66 deletions src/components/Web3Status/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from 'react'
import { AbstractConnector } from '@web3-react/abstract-connector'
import styled, { css } from 'styled-components'
import { useTranslation } from 'react-i18next'
import { useWeb3React, UnsupportedChainIdError } from '@web3-react/core'
Expand Down Expand Up @@ -132,10 +133,41 @@ const SOCK = (
</span>
)

export default function Web3Status() {
// eslint-disable-next-line react/prop-types
function StatusIcon({ connector }: { connector: AbstractConnector }) {
if (connector === injected) {
return <Identicon />
} else if (connector === walletconnect) {
return (
<IconWrapper size={16}>
<img src={WalletConnectIcon} alt={''} />
</IconWrapper>
)
} else if (connector === walletlink) {
return (
<IconWrapper size={16}>
<img src={CoinbaseWalletIcon} alt={''} />
</IconWrapper>
)
} else if (connector === fortmatic) {
return (
<IconWrapper size={16}>
<img src={FortmaticIcon} alt={''} />
</IconWrapper>
)
} else if (connector === portis) {
return (
<IconWrapper size={16}>
<img src={PortisIcon} alt={''} />
</IconWrapper>
)
}
return null
}

function Web3StatusInner() {
const { t } = useTranslation()
const { active, account, connector, error } = useWeb3React()
const contextNetwork = useWeb3React(NetworkContextName)
const { account, connector, error } = useWeb3React()

const { ENSName } = useENSName(account)

Expand All @@ -147,83 +179,66 @@ export default function Web3Status() {
}, [allTransactions])

const pending = sortedRecentTransactions.filter(tx => !tx.receipt).map(tx => tx.hash)
const confirmed = sortedRecentTransactions.filter(tx => tx.receipt).map(tx => tx.hash)

const hasPendingTransactions = !!pending.length
const hasSocks = useHasSocks()
const toggleWalletModal = useWalletModalToggle()

// handle the logo we want to show with the account
function getStatusIcon() {
if (connector === injected) {
return <Identicon />
} else if (connector === walletconnect) {
return (
<IconWrapper size={16}>
<img src={WalletConnectIcon} alt={''} />
</IconWrapper>
)
} else if (connector === walletlink) {
return (
<IconWrapper size={16}>
<img src={CoinbaseWalletIcon} alt={''} />
</IconWrapper>
)
} else if (connector === fortmatic) {
return (
<IconWrapper size={16}>
<img src={FortmaticIcon} alt={''} />
</IconWrapper>
)
} else if (connector === portis) {
return (
<IconWrapper size={16}>
<img src={PortisIcon} alt={''} />
</IconWrapper>
)
}
if (account) {
return (
<Web3StatusConnected id="web3-status-connected" onClick={toggleWalletModal} pending={hasPendingTransactions}>
{hasPendingTransactions ? (
<RowBetween>
<Text>{pending?.length} Pending</Text> <Loader stroke="white" />
</RowBetween>
) : (
<>
{hasSocks ? SOCK : null}
<Text>{ENSName || shortenAddress(account)}</Text>
</>
)}
{!hasPendingTransactions && <StatusIcon connector={connector} />}
</Web3StatusConnected>
)
} else if (error) {
return (
<Web3StatusError onClick={toggleWalletModal}>
<NetworkIcon />
<Text>{error instanceof UnsupportedChainIdError ? 'Wrong Network' : 'Error'}</Text>
</Web3StatusError>
)
} else {
return (
<Web3StatusConnect id="connect-wallet" onClick={toggleWalletModal} faded={!account}>
<Text>{t('Connect to a wallet')}</Text>
</Web3StatusConnect>
)
}
}

function getWeb3Status() {
if (account) {
return (
<Web3StatusConnected id="web3-status-connected" onClick={toggleWalletModal} pending={hasPendingTransactions}>
{hasPendingTransactions ? (
<RowBetween>
<Text>{pending?.length} Pending</Text> <Loader stroke="white" />
</RowBetween>
) : (
<>
{hasSocks ? SOCK : null}
<Text>{ENSName || shortenAddress(account)}</Text>
</>
)}
{!hasPendingTransactions && getStatusIcon()}
</Web3StatusConnected>
)
} else if (error) {
return (
<Web3StatusError onClick={toggleWalletModal}>
<NetworkIcon />
<Text>{error instanceof UnsupportedChainIdError ? 'Wrong Network' : 'Error'}</Text>
</Web3StatusError>
)
} else {
return (
<Web3StatusConnect id="connect-wallet" onClick={toggleWalletModal} faded={!account}>
<Text>{t('Connect to a wallet')}</Text>
</Web3StatusConnect>
)
}
}
export default function Web3Status() {
const { active, account } = useWeb3React()
const contextNetwork = useWeb3React(NetworkContextName)

const { ENSName } = useENSName(account)

const allTransactions = useAllTransactions()

const sortedRecentTransactions = useMemo(() => {
const txs = Object.values(allTransactions)
return txs.filter(recentTransactionsOnly).sort(newTranscationsFirst)
}, [allTransactions])

const pending = sortedRecentTransactions.filter(tx => !tx.receipt).map(tx => tx.hash)
const confirmed = sortedRecentTransactions.filter(tx => tx.receipt).map(tx => tx.hash)

if (!contextNetwork.active && !active) {
return null
}

return (
<>
{getWeb3Status()}
<Web3StatusInner />
<WalletModal ENSName={ENSName} pendingTransactions={pending} confirmedTransactions={confirmed} />
</>
)
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ export function useInactiveListener(suppress = false) {
}
}
}
return
return undefined
}, [active, error, suppress, activate])
}
2 changes: 1 addition & 1 deletion src/hooks/useCopyClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function useCopyClipboard(timeout = 500): [boolean, (toCopy: stri
clearTimeout(hide)
}
}
return
return undefined
}, [isCopied, setIsCopied, timeout])

return [isCopied, staticCopy]
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export default function useInterval(callback: () => void, delay: null | number,
const id = setInterval(tick, delay)
return () => clearInterval(id)
}
return
return undefined
}, [delay, leading])
}
2 changes: 1 addition & 1 deletion src/hooks/useIsWindowVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function useIsWindowVisible(): boolean {
}, [setFocused])

useEffect(() => {
if (!VISIBILITY_STATE_SUPPORTED) return
if (!VISIBILITY_STATE_SUPPORTED) return undefined

document.addEventListener('visibilitychange', listener)
return () => {
Expand Down
2 changes: 1 addition & 1 deletion src/state/application/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function Updater() {

// attach/detach listeners
useEffect(() => {
if (!library || !chainId || !windowVisible) return
if (!library || !chainId || !windowVisible) return undefined

setState({ chainId, blockNumber: null })

Expand Down
18 changes: 9 additions & 9 deletions src/state/mint/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function useDerivedMintInfo(
if (otherTypedValue && currencies[dependentField]) {
return tryParseAmount(otherTypedValue, currencies[dependentField])
}
return
return undefined
} else if (independentAmount) {
// we wrap the currencies just to get the price in terms of the other token
const wrappedIndependentAmount = wrappedCurrencyAmount(independentAmount, chainId)
Expand All @@ -85,28 +85,28 @@ export function useDerivedMintInfo(
: pair.priceOf(tokenB).quote(wrappedIndependentAmount)
return dependentCurrency === ETHER ? CurrencyAmount.ether(dependentTokenAmount.raw) : dependentTokenAmount
}
return
return undefined
} else {
return
return undefined
}
}, [noLiquidity, otherTypedValue, currencies, dependentField, independentAmount, currencyA, chainId, currencyB, pair])
const parsedAmounts: { [field in Field]: CurrencyAmount | undefined } = {
[Field.CURRENCY_A]: independentField === Field.CURRENCY_A ? independentAmount : dependentAmount,
[Field.CURRENCY_B]: independentField === Field.CURRENCY_A ? dependentAmount : independentAmount
}

const token0Price = pair?.token0Price
const price = useMemo(() => {
if (noLiquidity) {
const { [Field.CURRENCY_A]: currencyAAmount, [Field.CURRENCY_B]: currencyBAmount } = parsedAmounts
if (currencyAAmount && currencyBAmount) {
return new Price(currencyAAmount.currency, currencyBAmount.currency, currencyAAmount.raw, currencyBAmount.raw)
}
return
return undefined
} else {
return token0Price
const wrappedCurrencyA = wrappedCurrency(currencyA, chainId)
return pair && wrappedCurrencyA ? pair.priceOf(wrappedCurrencyA) : undefined
}
}, [noLiquidity, token0Price, parsedAmounts])
}, [chainId, currencyA, noLiquidity, pair, parsedAmounts])

// liquidity minted
const liquidityMinted = useMemo(() => {
Expand All @@ -118,15 +118,15 @@ export function useDerivedMintInfo(
if (pair && totalSupply && tokenAmountA && tokenAmountB) {
return pair.getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB)
} else {
return
return undefined
}
}, [parsedAmounts, chainId, pair, totalSupply])

const poolTokenPercentage = useMemo(() => {
if (liquidityMinted && totalSupply) {
return new Percent(liquidityMinted.raw, totalSupply.add(liquidityMinted).raw)
} else {
return
return undefined
}
}, [liquidityMinted, totalSupply])

Expand Down
5 changes: 3 additions & 2 deletions src/state/multicall/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function isMethodArg(x: unknown): x is MethodArg {

function isValidMethodArgs(x: unknown): x is MethodArgs | undefined {
return (
x === undefined || (Array.isArray(x) && x.every(y => isMethodArg(y) || (Array.isArray(y) && y.every(isMethodArg))))
x === undefined ||
(Array.isArray(x) && x.every(xi => isMethodArg(xi) || (Array.isArray(xi) && xi.every(isMethodArg))))
)
}

Expand Down Expand Up @@ -67,7 +68,7 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C
// update listeners when there is an actual change that persists for at least 100ms
useEffect(() => {
const callKeys: string[] = JSON.parse(serializedCallKeys)
if (!chainId || callKeys.length === 0) return
if (!chainId || callKeys.length === 0) return undefined
const calls = callKeys.map(key => parseCallKey(key))
dispatch(
addMulticallListeners({
Expand Down
4 changes: 2 additions & 2 deletions src/state/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function useSwapActionHandlers(): {
// try to parse a user entered amount for a given token
export function tryParseAmount(value?: string, currency?: Currency): CurrencyAmount | undefined {
if (!value || !currency) {
return
return undefined
}
try {
const typedValueParsed = parseUnits(value, currency.decimals).toString()
Expand All @@ -85,7 +85,7 @@ export function tryParseAmount(value?: string, currency?: Currency): CurrencyAmo
console.debug(`Failed to parse input amount: "${value}"`, error)
}
// necessary for all paths to return a value
return
return undefined
}

const BAD_RECIPIENT_ADDRESSES: string[] = [
Expand Down
6 changes: 3 additions & 3 deletions src/state/wallet/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function useTokenBalances(
// get the balance for a single token/account combo
export function useTokenBalance(account?: string, token?: Token): TokenAmount | undefined {
const tokenBalances = useTokenBalances(account, [token])
if (!token) return
if (!token) return undefined
return tokenBalances[token.address]
}

Expand All @@ -109,10 +109,10 @@ export function useCurrencyBalances(
return useMemo(
() =>
currencies?.map(currency => {
if (!account || !currency) return
if (!account || !currency) return undefined
if (currency instanceof Token) return tokenBalances[currency.address]
if (currency === ETHER) return ethBalance[account]
return
return undefined
}) ?? [],
[account, currencies, ethBalance, tokenBalances]
)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/maxAmountSpend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MIN_ETH } from '../constants'
* @param currencyAmount to return max of
*/
export function maxAmountSpend(currencyAmount?: CurrencyAmount): CurrencyAmount | undefined {
if (!currencyAmount) return
if (!currencyAmount) return undefined
if (currencyAmount.currency === ETHER) {
if (JSBI.greaterThan(currencyAmount.raw, MIN_ETH)) {
return CurrencyAmount.ether(JSBI.subtract(currencyAmount.raw, MIN_ETH))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/parseENSAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const ENS_NAME_REGEX = /^(([a-zA-Z0-9]+\.)+)eth(\/.*)?$/

export function parseENSAddress(ensAddress: string): { ensName: string; ensPath: string | undefined } | undefined {
const match = ensAddress.match(ENS_NAME_REGEX)
if (!match) return
if (!match) return undefined
return { ensName: `${match[1].toLowerCase()}eth`, ensPath: match[3] }
}
Loading

0 comments on commit 66a2006

Please sign in to comment.