Skip to content

Commit dd15a4f

Browse files
memoyilChefJerry
andauthored
feat: Add unverified hook warning tooltip message (#12472)
<!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces a new `whitelistLabeledHooksList` feature in the PancakeSwap codebase, enhancing the handling of hooks by adding a whitelist for certain hooks without metadata. Additionally, it optimizes currency balance calculations using `useMemo` across several components. ### Detailed summary - Added `whitelistLabeledHooksList` to manage whitelisted hooks. - Introduced `whitelisted hooks` in `packages/infinity-sdk/src/constants/hooksList/bsc.ts`. - Updated multiple components to utilize `useMemo` for currency balance calculations. - Refactored `useCurrencyBalances` to improve performance. - Enhanced `PoolTokenOverview` to check if hooks are whitelisted. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> --------- Co-authored-by: Chef Jerry <[email protected]>
1 parent 22e7ad6 commit dd15a4f

File tree

21 files changed

+114
-61
lines changed

21 files changed

+114
-61
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@pancakeswap/infinity-sdk': patch
3+
---
4+
5+
Added whitelistLabeledHooksList

apps/web/src/hooks/Tokens.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export function useToken(tokenAddress?: string, chainId?: number): ERC20Token |
280280
// otherwise returns the token
281281
export function useTokenByChainId(tokenAddress?: string, chainId?: number): ERC20Token | undefined | null {
282282
const unsupportedTokens = useUnsupportedTokens()
283-
const tokens = useAllTokensByChainIds(chainId ? [chainId] : [])
283+
const tokens = useAllTokensByChainIds(useMemo(() => (chainId ? [chainId] : []), [chainId]))
284284

285285
const address = safeGetAddress(tokenAddress)
286286

@@ -333,7 +333,7 @@ export function useTokensByChainId(
333333
chainId?: number,
334334
): Record<string, ERC20Token | undefined | null> {
335335
const unsupportedTokens = useUnsupportedTokens()
336-
const tokens = useAllTokensByChainIds(chainId ? [chainId] : [])
336+
const tokens = useAllTokensByChainIds(useMemo(() => (chainId ? [chainId] : []), [chainId]))
337337

338338
// Process addresses and separate existing tokens from ones that need to be fetched
339339
const processedData = useMemo(() => {

apps/web/src/state/wallet/hooks.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isSolana, NonEVMChainId } from '@pancakeswap/chains'
44
import { selectedEvmWalletAtom, selectedSolanaWalletAtom } from '@pancakeswap/ui-wallets/src/state/atom'
55
import useAccountActiveChain from 'hooks/useAccountActiveChain'
66
import { useLocalStorage, useWallet } from '@solana/wallet-adapter-react'
7-
import { EvmConnectorNames, SolanaProviderLocalStorageKey, WalletAdaptedNetwork } from '@pancakeswap/ui-wallets'
7+
import { SolanaProviderLocalStorageKey, WalletAdaptedNetwork } from '@pancakeswap/ui-wallets'
88
import { WalletName } from '@solana/wallet-adapter-base'
99
import { multicallABI } from 'config/abi/Multicall'
1010
import { FAST_INTERVAL } from 'config/constants'
@@ -20,7 +20,6 @@ import { publicClient } from 'utils/viem'
2020
import { Address, erc20Abi, getAddress, isAddress } from 'viem'
2121
import { useAccount, useBalance } from 'wagmi'
2222
import { useAtomValue } from 'jotai'
23-
import { useSelectedWallet } from '@pancakeswap/ui-wallets/src/state/hooks'
2423
import { useMultipleContractSingleDataWagmi } from '../multicall/hooks'
2524

2625
/**
@@ -104,6 +103,9 @@ export function useTokenBalance(account?: string, token?: Token): CurrencyAmount
104103
return tokenBalances[`${token.chainId}-${token.address}`]
105104
}
106105

106+
/**
107+
* Note: `currencies` should be memoized to prevent unnecessary recomputation and rerenders.
108+
*/
107109
export function useCurrencyBalances(
108110
account?: string,
109111
currencies?: (UnifiedCurrency | undefined | null)[],

apps/web/src/utils/getHookByAddress.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { ChainId } from '@pancakeswap/chains'
2-
import { HookData, hooksList } from '@pancakeswap/infinity-sdk'
2+
import { HookData, hooksList, whitelistLabeledHooksList } from '@pancakeswap/infinity-sdk'
3+
import { Address } from 'viem'
34
import keyBy from 'lodash/keyBy'
45
import memoize from 'lodash/memoize'
6+
import { isAddressEqual } from 'utils'
7+
import { ZERO_ADDRESS } from '@pancakeswap/sdk'
58

69
export const getHooksMap = memoize((chainId: number) => {
710
const list = hooksList[chainId] ?? []
@@ -11,3 +14,15 @@ export const getHooksMap = memoize((chainId: number) => {
1114
export const getHookByAddress = (chainId?: ChainId, address?: HookData['address']): HookData | undefined => {
1215
return chainId && address ? getHooksMap(chainId)[address.toLowerCase()] : undefined
1316
}
17+
18+
export const isHookWhitelisted = memoize(
19+
(chainId?: ChainId, address?: Address): boolean => {
20+
if (!chainId || !address) return false
21+
return Boolean(
22+
isAddressEqual(address, ZERO_ADDRESS) ||
23+
whitelistLabeledHooksList[chainId]?.some((addr) => isAddressEqual(addr, address)) ||
24+
getHookByAddress(chainId, address),
25+
)
26+
},
27+
(chainId, address) => `${chainId}#${address}`,
28+
)

apps/web/src/utils/getTokenLogoURL.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ChainId, isSolana, NonEVMChainId } from '@pancakeswap/chains'
22
import { Token } from '@pancakeswap/sdk'
33
import memoize from 'lodash/memoize'
44
import { safeGetAddress } from 'utils'
5-
import { isAddress } from 'viem'
65

76
const mapping = {
87
[ChainId.BSC]: 'smartchain',
@@ -14,27 +13,26 @@ const mapping = {
1413
[NonEVMChainId.SOLANA]: 'solana',
1514
}
1615

16+
const buildTrustWalletLogoURL = (address?: string, chainId?: number): string | null => {
17+
if (!address || !chainId || !mapping[chainId]) return null
18+
19+
const formattedAddress = isSolana(chainId) ? address : safeGetAddress(address)
20+
21+
if (!formattedAddress) return null
22+
23+
return `https://assets-cdn.trustwallet.com/blockchains/${mapping[chainId]}/assets/${formattedAddress}/logo.png`
24+
}
25+
1726
const getTokenLogoURL = memoize(
1827
(token?: Token) => {
19-
if (token && mapping[token.chainId] && isAddress(token.address)) {
20-
return `https://assets-cdn.trustwallet.com/blockchains/${mapping[token.chainId]}/assets/${
21-
isSolana(token.chainId) ? token.address : safeGetAddress(token.address)
22-
}/logo.png`
23-
}
24-
return null
28+
if (!token) return null
29+
return buildTrustWalletLogoURL(token.address, token.chainId)
2530
},
2631
(t) => `${t?.chainId}#${t?.address}`,
2732
)
2833

2934
export const getTokenLogoURLByAddress = memoize(
30-
(address?: string, chainId?: number) => {
31-
if (address && chainId && mapping[chainId] && isAddress(address)) {
32-
return `https://assets-cdn.trustwallet.com/blockchains/${mapping[chainId]}/assets/${safeGetAddress(
33-
address,
34-
)}/logo.png`
35-
}
36-
return null
37-
},
35+
(address?: string, chainId?: number) => buildTrustWalletLogoURL(address, chainId),
3836
(address, chainId) => `${chainId}#${address}`,
3937
)
4038

apps/web/src/views/AddLiquidityInfinity/components/SubmitButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ export const SubmitButton = (props: AtomBoxProps) => {
107107
}),
108108
[depositCurrencyAmount0, depositCurrencyAmount1],
109109
)
110-
const [currency0Balance, currency1Balance] = useCurrencyBalances(account ?? undefined, [currencyA, currencyB])
110+
const [currency0Balance, currency1Balance] = useCurrencyBalances(
111+
account ?? undefined,
112+
useMemo(() => [currencyA, currencyB], [currencyA, currencyB]),
113+
)
111114

112115
const {
113116
approve: approveACallback,

apps/web/src/views/CreateLiquidityPool/components/SubmitCreateButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ export const SubmitCreateButton: React.FC<SubmitCreateButtonProps> = ({ ...boxPr
178178
const { binStep, lowerBinId, upperBinId, activeId } = useInfinityBinQueryState()
179179
const { maxBinId, minBinId } = useBinIdRange()
180180

181-
const [currency0Balance, currency1Balance] = useCurrencyBalances(account, [currency0, currency1])
181+
const [currency0Balance, currency1Balance] = useCurrencyBalances(
182+
account,
183+
useMemo(() => [currency0, currency1], [currency0, currency1]),
184+
)
182185
const {
183186
approve: approveACallback,
184187
revoke: revokeACallback,

apps/web/src/views/IncreaseLiquidity/hooks/useIncreaseForm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export const useIncreaseForm = ({
3232
const [outputAmountRaw, setOutputAmountRaw] = useState('')
3333

3434
const { address: account } = useAccount()
35-
const [inputBalance, outputBalance] = useCurrencyBalances(account, [currency0, currency1])
35+
const [inputBalance, outputBalance] = useCurrencyBalances(
36+
account,
37+
useMemo(() => [currency0, currency1], [currency0, currency1]),
38+
)
3639

3740
const inputAmount: CurrencyAmount<Currency> | undefined = useMemo(() => {
3841
return tryParseCurrencyAmount(inputAmountRaw, currency0)

apps/web/src/views/PCSLimitOrders/hooks/useLimitOrderUserBalance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { BigNumber as BN } from 'bignumber.js'
66
import { formatAmount } from '@pancakeswap/utils/formatFractions'
77
import { Rounding } from '@pancakeswap/swap-sdk-core'
88
import { useUnifiedUSDPriceAmount } from 'hooks/useStablecoinPrice'
9-
import { useRouter } from 'next/router'
109
import { formattedAmountsAtom } from '../state/form/inputAtoms'
1110
import { Field } from '../types/limitOrder.types'
1211
import { inputCurrencyAtom, outputCurrencyAtom } from '../state/currency/currencyAtoms'
@@ -16,14 +15,15 @@ import { MIN_BNB_VALUE, MIN_USD_VALUE } from '../constants'
1615
* Check token balance for Limit Order
1716
*/
1817
export const useLimitOrderUserBalance = () => {
19-
const router = useRouter()
2018
const { account } = useAccountActiveChain()
2119
const inputCurrency = useAtomValue(inputCurrencyAtom)
2220
const outputCurrency = useAtomValue(outputCurrencyAtom)
2321
const formattedAmounts = useAtomValue(formattedAmountsAtom)
2422

25-
const [inputBalance] = useCurrencyBalances(account, [inputCurrency ?? undefined])
26-
const [outputBalance] = useCurrencyBalances(account, [outputCurrency ?? undefined])
23+
const [inputBalance, outputBalance] = useCurrencyBalances(
24+
account,
25+
useMemo(() => [inputCurrency ?? undefined, outputCurrency ?? undefined], [inputCurrency, outputCurrency]),
26+
)
2727

2828
const amountUSD = useUnifiedUSDPriceAmount(
2929
inputCurrency ?? undefined,

apps/web/src/views/Swap/Twap/Twap.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import { useQuoteContext } from 'quoter/hook/QuoteContext'
3030
import { multicallGasLimitAtom } from 'quoter/hook/useMulticallGasLimit'
3131
import { QuoteProvider } from 'quoter/QuoteProvider'
3232
import { createQuoteQuery } from 'quoter/utils/createQuoteQuery'
33-
import { memo, Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'
33+
import { memo, Suspense, useCallback, useMemo, useRef } from 'react'
3434
import { useCurrentBlock } from 'state/block/hooks'
3535
import { Field } from 'state/swap/actions'
3636
import { useDefaultsFromURLSearch, useSwapState } from 'state/swap/hooks'
3737
import { useSwapActionHandlers } from 'state/swap/useSwapActionHandlers'
38-
import { useCurrencyBalances } from 'state/wallet/hooks'
38+
import { useCurrencyBalance } from 'state/wallet/hooks'
3939
import { keyframes, styled } from 'styled-components'
4040
import { maxAmountSpend } from 'utils/maxAmountSpend'
4141
import { useAccount } from 'wagmi'
@@ -182,7 +182,7 @@ const TokenPanelInput = ({
182182
const { t } = useTranslation()
183183
const loadedUrlParams = useDefaultsFromURLSearch()
184184

185-
const [inputBalance] = useCurrencyBalances(account, [inputCurrency, outputCurrency])
185+
const inputBalance = useCurrencyBalance(account, inputCurrency)
186186

187187
const maxAmountInput = useMemo(() => maxAmountSpend(inputBalance), [inputBalance])
188188

0 commit comments

Comments
 (0)