Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold committed Aug 16, 2023
1 parent d11240d commit 18e8e7d
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 71 deletions.
36 changes: 36 additions & 0 deletions src/assets/wallets-connect/rabby.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/components/FeeSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function FeeSelector({
(acc, cur, index) => ({ ...acc, [cur]: poolAddresses[index] }),
{} as { [key in FeeAmount]: string },
)
const tiersThatHasFarmV1 = FEE_AMOUNTS.filter((_fee, i) => {
const tiersThatHasFarmV1: FeeAmount[] = FEE_AMOUNTS.filter((_fee, i) => {
const poolAddress = poolAddresses[i].toLowerCase()
return farmingPoolAddress.includes(poolAddress)
})
Expand Down Expand Up @@ -249,7 +249,7 @@ function FeeSelector({
</Flex>

<Flex alignItems="center" sx={{ gap: '8px' }}>
{(tiersThatHasFarmV1.includes(feeAmount) ||
{(tiersThatHasFarmV1.includes(feeAmount as any) ||
activeFarmV2s?.find(item => item.poolAddress === poolByFeeAmount[feeAmount].toLowerCase())) && (
<FarmTag address={poolByFeeAmount[feeAmount]} />
)}
Expand Down
1 change: 1 addition & 0 deletions src/constants/connectors/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'constants/networks'

export const [injected, injectedHooks] = initializeConnector<MetaMask>(actions => new MetaMask({ actions }))
export const [rabby, rabbyHooks] = initializeConnector<MetaMask>(actions => new MetaMask({ actions }))
export const [krystal, krystalHooks] = initializeConnector<MetaMask>(actions => new MetaMask({ actions }))
export const [metaMask, metamaskHooks] = initializeConnector<MetaMask>(actions => new MetaMask({ actions }))
export const [gnosisSafe, gnosisSafeHooks] = initializeConnector<GnosisSafe>(actions => new GnosisSafe({ actions }))
Expand Down
5 changes: 4 additions & 1 deletion src/constants/connectors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const allNonMetamaskFlags = [
'isKrystal',
'isKrystalWallet',
] as const

export const getIsMetaMaskWallet = () =>
Boolean(window.ethereum?.isMetaMask && !allNonMetamaskFlags.some(flag => window.ethereum?.[flag]))

export const getIsRabbyWallet = () => Boolean(window.ethereum?.isRabby)

export const getIsKrystalWallet = () =>
Boolean(window.ethereum?.isKrystalWallet || (window.ethereum?.isKrystal && !getIsTrustWallet()))

Expand All @@ -37,6 +38,8 @@ export const getIsGenericInjector = () =>
!getIsCoinbaseWallet() &&
!getIsBraveWallet() &&
!getIsC98Wallet() &&
!getIsRabbyWallet() &&
!getIsKrystalWallet() &&
!getIsTrustWallet()

// https://eips.ethereum.org/EIPS/eip-1193#provider-errors
Expand Down
2 changes: 1 addition & 1 deletion src/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const NETWORKS_INFO = new Proxy(NETWORKS_INFO_CONFIG, {

// temporary disable Solana
// todo: either enable back or completely remove Solana from codebase
export const SUPPORTED_NETWORKS = Object.keys(NETWORKS_INFO).map(Number).filter(isEVM) as ChainId[]
export const SUPPORTED_NETWORKS = Object.keys(NETWORKS_INFO).map(Number).filter(isEVM)

export const MAINNET_NETWORKS = [
ChainId.MAINNET,
Expand Down
32 changes: 30 additions & 2 deletions src/constants/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import COINBASE from 'assets/wallets-connect/coinbase.svg'
import KRYSTAL from 'assets/wallets-connect/krystal.svg'
import METAMASK from 'assets/wallets-connect/metamask.svg'
import PHANTOM from 'assets/wallets-connect/phantom.svg'
import RABBY from 'assets/wallets-connect/rabby.svg'
import SAFE from 'assets/wallets-connect/safe.svg'
import SLOPE from 'assets/wallets-connect/slope.svg'
import SOLFLARE from 'assets/wallets-connect/solflare.svg'
Expand All @@ -31,6 +32,8 @@ import {
krystalHooks,
metaMask,
metamaskHooks,
rabby,
rabbyHooks,
trustWallet,
trustWalletHooks,
walletConnectV2,
Expand All @@ -51,6 +54,7 @@ import {
getIsGenericInjector,
getIsKrystalWallet,
getIsMetaMaskWallet,
getIsRabbyWallet,
getIsTrustWallet,
} from 'constants/connectors/utils'

Expand All @@ -72,6 +76,11 @@ const detectSafe = (): WalletReadyState => {
// return WalletReadyState.NotDetected
}

const detectRabby = (): WalletReadyState => {
if (getIsRabbyWallet()) return WalletReadyState.Installed
return WalletReadyState.NotDetected
}

const detectKrystal = (): WalletReadyState => {
if (getIsKrystalWallet()) return WalletReadyState.Installed
return WalletReadyState.NotDetected
Expand Down Expand Up @@ -157,6 +166,15 @@ export const SUPPORTED_WALLETS = {
installLink: 'https://safe.global/wallet',
readyState: detectSafe,
} as EVMWalletInfo,
RABBBY: {
connector: rabby,
hooks: rabbyHooks,
name: 'Rabby',
icon: RABBY,
iconLight: RABBY,
installLink: 'https://rabby.io',
readyState: detectRabby,
} as EVMWalletInfo,
KRYSTAL: {
connector: krystal,
hooks: krystalHooks,
Expand Down Expand Up @@ -243,10 +261,20 @@ export const SUPPORTED_WALLETS = {
readyState: detectTrustWallet,
} as EVMWalletInfo,
} as const
export type SUPPORTED_WALLET = keyof typeof SUPPORTED_WALLETS

export const connections = Object.values(SUPPORTED_WALLETS).filter(wallet => 'connector' in wallet) as EVMWalletInfo[]

export type SUPPORTED_WALLET = keyof typeof SUPPORTED_WALLETS

export const LOCALSTORAGE_LAST_WALLETKEY_EVM = 'last-wallet-key-evm'
export const LOCALSTORAGE_LAST_WALLETKEY_SOLANA = 'last-wallet-key-solana'
export const INJECTED_KEYS = [
'COIN98',
'BRAVE',
'METAMASK',
'COINBASE',
'TRUST_WALLET',
'KRYSTAL',
'RABBBY',
'INJECTED',
] as const
export type INJECTED_KEY = typeof INJECTED_KEYS[number]
3 changes: 2 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export function useActiveWeb3React(): {

const walletKeyEVM = useMemo(() => {
if (!isConnectedEVM) return undefined
const detectedWallet = detectInjectedType()
if (connectedConnectorEVM === walletConnectV2) {
return 'WALLET_CONNECT'
}
const detectedWallet = detectInjectedType()

return (
detectedWallet ??
(Object.keys(SUPPORTED_WALLETS) as SUPPORTED_WALLET[]).find(walletKey => {
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/web3/useEagerConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export function useEagerConnect() {
if (trying || tried) return
trying = true
let activated = false
try {
await tryActivation('SAFE', true)
activated = true
} catch {}
const lastWalletKeyEVM = localStorage.getItem(LOCALSTORAGE_LAST_WALLETKEY_EVM)
const lastWalletKeySolana = localStorage.getItem(LOCALSTORAGE_LAST_WALLETKEY_SOLANA)
await Promise.all([
Expand Down
1 change: 0 additions & 1 deletion src/hooks/web3/useWalletSupportedChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function useWalletSupportedChains() {
case walletConnectV2:
return [
...getChainsFromEIP155Accounts((connector as WalletConnectV2).provider?.session?.namespaces?.eip155?.accounts),
// ChainId.SOLANA,
]
default:
return SUPPORTED_NETWORKS
Expand Down
3 changes: 1 addition & 2 deletions src/pages/Bridge/BridgeTransferHistory/RouteCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useTheme } from 'styled-components'
import QuestionHelper from 'components/QuestionHelper'
import { NETWORKS_INFO, SUPPORTED_NETWORKS } from 'constants/networks'
import { useIsDarkMode } from 'state/user/hooks'
import { includes } from 'utils/array'

type Props = {
fromChainID: number
Expand All @@ -17,7 +16,7 @@ const RouteCell: React.FC<Props> = ({ fromChainID, toChainID }) => {
const theme = useTheme()

const renderChainIcon = (chainId: number) => {
if (includes(SUPPORTED_NETWORKS, chainId)) {
if (SUPPORTED_NETWORKS.includes(chainId)) {
const chainInfo = NETWORKS_INFO[chainId]
const src = isDark && chainInfo.iconDark ? chainInfo.iconDark : chainInfo.icon
return <img src={src} alt={chainInfo.name} style={{ width: '18px' }} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import ActionCell from 'pages/Bridge/BridgeTransferHistory/ActionCell'
import StatusBadge from 'pages/Bridge/BridgeTransferHistory/StatusBadge'
import TimeStatusCell from 'pages/Bridge/BridgeTransferHistory/TimeStatusCell'
import TokenReceiveCell from 'pages/Bridge/BridgeTransferHistory/TokenReceiveCell'
import { includes } from 'utils/array'

import { Props } from './index'

Expand Down Expand Up @@ -59,7 +58,7 @@ const ChainWrapper = styled.div`
color: ${({ theme }) => theme.subText};
`
const ChainDisplay: React.FC<{ chainId: number }> = ({ chainId }) => {
if (includes(SUPPORTED_NETWORKS, chainId)) {
if (SUPPORTED_NETWORKS.includes(chainId)) {
const chainInfo = NETWORKS_INFO[chainId]
return (
<ChainWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import ActionButtons from 'pages/CrossChain/TransfersHistory/HistoryTable/Action
import { DetailTransaction } from 'pages/CrossChain/TransfersHistory/HistoryTable/DetailTransaction'
import { useGetTransactionStatus } from 'pages/CrossChain/TransfersHistory/HistoryTable/TransactionItem'
import { CrossChainTransfer } from 'pages/CrossChain/useTransferHistory'
import { includes } from 'utils/array'

import TokenReceiveCell from './TokenReceiveCell'
import { Props } from './index'
Expand Down Expand Up @@ -65,7 +64,7 @@ const ChainWrapper = styled.div`
color: ${({ theme }) => theme.subText};
`
const ChainDisplay: React.FC<{ chainId: number }> = ({ chainId }) => {
if (includes(SUPPORTED_NETWORKS, chainId)) {
if (SUPPORTED_NETWORKS.includes(chainId)) {
const chainInfo = NETWORKS_INFO[chainId]
return (
<ChainWrapper>
Expand Down
3 changes: 1 addition & 2 deletions src/state/transactions/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { useSetClaimingCampaignRewardId } from 'state/campaigns/hooks'
import { AppDispatch, AppState } from 'state/index'
import { revokePermit } from 'state/user/actions'
import { findTx } from 'utils'
import { includes } from 'utils/array'

import { checkedTransaction, finalizeTransaction, removeTx, replaceTx } from './actions'
import { SerializableTransactionReceipt, TRANSACTION_TYPE, TransactionDetails } from './type'
Expand Down Expand Up @@ -134,7 +133,7 @@ export default function Updater(): null {
blockHash: receipt.blockHash,
status: receipt.status,
},
needCheckSubgraph: includes(NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES, transaction.type),
needCheckSubgraph: NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES.includes(transaction.type),
}),
)

Expand Down
8 changes: 8 additions & 0 deletions src/types/array.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface Array<T> {
/**
* Determines whether an array includes a certain element, returning true or false as appropriate.
* @param searchElement The element to search for.
* @param fromIndex The position in this array at which to begin searching for searchElement.
*/
includes(searchElement: any, fromIndex?: number): searchElement is T
}
7 changes: 7 additions & 0 deletions src/types/object.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface ObjectConstructor {
/**
* Returns the names of the enumerable string properties and methods of an object.
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
keys<T extends string>(o: { [keys in T]: unknown }): T[]
}
8 changes: 0 additions & 8 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ export const chunk = <T>(arr: readonly T[], chunkSize: number): T[][] => {
return result
}

export const includes = <T>(
srcStr: readonly T[] | T[],
searchElement: any,
fromIndex?: number | undefined,
): searchElement is T => {
return (srcStr as any[]).includes(searchElement, fromIndex)
}

export const uniqueArray = <T, U>(array: T[], keySelector = (item: T): U => item as any): T[] => {
const set = new Set<U>()
const result: T[] = []
Expand Down
62 changes: 21 additions & 41 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApolloClient, NormalizedCacheObject } from '@apollo/client'
import { BigNumber } from '@ethersproject/bignumber'
import { ChainId, Currency, CurrencyAmount, Percent, Token, WETH } from '@kyberswap/ks-sdk-core'
import { WalletReadyState } from '@solana/wallet-adapter-base'
import { PublicKey } from '@solana/web3.js'
import dayjs from 'dayjs'
import JSBI from 'jsbi'
Expand All @@ -12,11 +13,18 @@ import { ENV_KEY } from 'constants/env'
import { DEFAULT_GAS_LIMIT_MARGIN, ZERO_ADDRESS } from 'constants/index'
import { NETWORKS_INFO, SUPPORTED_NETWORKS, isEVM } from 'constants/networks'
import { KNC, KNCL_ADDRESS } from 'constants/tokens'
import { EVMWalletInfo, SUPPORTED_WALLET, SolanaWalletInfo, WalletInfo } from 'constants/wallets'
import {
EVMWalletInfo,
INJECTED_KEY,
INJECTED_KEYS,
SUPPORTED_WALLET,
SUPPORTED_WALLETS,
SolanaWalletInfo,
WalletInfo,
} from 'constants/wallets'
import store from 'state'
import { GroupedTxsByHash, TransactionDetails } from 'state/transactions/type'
import { chunk } from 'utils/array'
import checkForBraveBrowser from 'utils/checkForBraveBrowser'

export const isWalletAddressSolana = async (addr: string) => {
try {
Expand Down Expand Up @@ -430,54 +438,26 @@ export const deleteUnique = <T>(array: T[] | undefined, element: T): T[] => {
export const isEVMWallet = (wallet?: WalletInfo): wallet is EVMWalletInfo => !!wallet && 'connector' in wallet
export const isSolanaWallet = (wallet?: WalletInfo): wallet is SolanaWalletInfo => !!wallet && 'adapter' in wallet

enum WALLET_KEYS {
COIN98 = 'COIN98',
BRAVE = 'BRAVE',
METAMASK = 'METAMASK',
COINBASE = 'COINBASE',
TRUST_WALLET = 'TRUST_WALLET',
WALLET_CONNECT = 'WALLET_CONNECT',
}

// https://docs.metamask.io/guide/ethereum-provider.html#basic-usage
// https://docs.cloud.coinbase.com/wallet-sdk/docs/injected-provider#properties
// Coin98 and Brave wallet is overriding Metamask. So at a time, there is only 1 exists
export const detectInjectedType = (): WALLET_KEYS | null => {
const { ethereum } = window
// When Coinbase wallet connected will inject selectedProvider property and some others props
if (ethereum?.selectedProvider) {
if (ethereum?.selectedProvider?.isMetaMask) return WALLET_KEYS.METAMASK
if (ethereum?.selectedProvider?.isCoinbaseWallet) return WALLET_KEYS.COINBASE
}

if (ethereum?.isCoinbaseWallet) return WALLET_KEYS.COINBASE

if (ethereum?.isTrustWallet) return WALLET_KEYS.TRUST_WALLET

if (checkForBraveBrowser() && ethereum?.isBraveWallet) return WALLET_KEYS.BRAVE

if (ethereum?.isMetaMask) {
if (ethereum?.isCoin98) {
return WALLET_KEYS.COIN98
}
return WALLET_KEYS.METAMASK
}
if (JSON.parse(localStorage.walletconnect || '{}').connected) {
return WALLET_KEYS.WALLET_CONNECT
}
return null
export const detectInjectedType = (): INJECTED_KEY | undefined => {
return INJECTED_KEYS.find(walletKey => {
const wallet = SUPPORTED_WALLETS[walletKey]
return wallet.readyState() === WalletReadyState.Installed
})
}

export const isOverriddenWallet = (wallet: SUPPORTED_WALLET) => {
const injectedType = detectInjectedType()
return (
(wallet === WALLET_KEYS.COIN98 && injectedType === WALLET_KEYS.METAMASK) ||
(wallet === WALLET_KEYS.METAMASK && injectedType === WALLET_KEYS.COIN98) ||
(wallet === WALLET_KEYS.BRAVE && injectedType === WALLET_KEYS.COIN98) ||
(wallet === WALLET_KEYS.COIN98 && injectedType === WALLET_KEYS.BRAVE) ||
(wallet === WALLET_KEYS.COINBASE && injectedType === WALLET_KEYS.COIN98) ||
(wallet === 'COIN98' && injectedType === 'METAMASK') ||
(wallet === 'METAMASK' && injectedType === 'COIN98') ||
(wallet === 'BRAVE' && injectedType === 'COIN98') ||
(wallet === 'COIN98' && injectedType === 'BRAVE') ||
(wallet === 'COINBASE' && injectedType === 'COIN98') ||
// Coin98 turned off override MetaMask in setting
(wallet === WALLET_KEYS.COIN98 && window.coin98 && !window.ethereum?.isCoin98)
(wallet === 'COIN98' && window.coin98 && !window.ethereum?.isCoin98)
)
}

Expand Down
Loading

0 comments on commit 18e8e7d

Please sign in to comment.