Skip to content

Commit

Permalink
feat: add Uniswap ESLint config (#751)
Browse files Browse the repository at this point in the history
* feat: add uniswap eslint config

* chore: accidentally added while rebasing

* chore: fix lint issues
  • Loading branch information
grabbou authored Feb 8, 2023
1 parent b52f668 commit 8507a82
Show file tree
Hide file tree
Showing 28 changed files with 740 additions and 216 deletions.
26 changes: 7 additions & 19 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
/* eslint-env node */

require('@uniswap/eslint-config/load')

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json', './packages/*/tsconfig.json'],
},
plugins: ['@typescript-eslint', 'react-hooks', 'eslint-plugin-tsdoc'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
extends: '@uniswap/eslint-config/react',
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'tsdoc/syntax': 'warn',
},
env: {
browser: true,
},
'import/no-unused-modules': 'off'
}
}
6 changes: 0 additions & 6 deletions .prettierrc.json

This file was deleted.

1 change: 1 addition & 0 deletions example/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GnosisSafe } from '@web3-react/gnosis-safe'
import { MetaMask } from '@web3-react/metamask'
import { Network } from '@web3-react/network'
import { WalletConnect } from '@web3-react/walletconnect'

import { getName } from '../utils'
import { Accounts } from './Accounts'
import { Chain } from './Chain'
Expand Down
1 change: 1 addition & 0 deletions example/components/Chain.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Web3ReactHooks } from '@web3-react/core'

import { CHAINS } from '../chains'

export function Chain({ chainId }: { chainId: ReturnType<Web3ReactHooks['useChainId']> }) {
Expand Down
1 change: 1 addition & 0 deletions example/components/ConnectWithSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { MetaMask } from '@web3-react/metamask'
import { Network } from '@web3-react/network'
import { WalletConnect } from '@web3-react/walletconnect'
import { useCallback, useState } from 'react'

import { CHAINS, getAddChainParameters, URLS } from '../chains'

function ChainSelect({
Expand Down
1 change: 1 addition & 0 deletions example/components/ProviderExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useWeb3React, Web3ReactHooks, Web3ReactProvider } from '@web3-react/cor
import { MetaMask } from '@web3-react/metamask'
import { Network } from '@web3-react/network'
import { WalletConnect } from '@web3-react/walletconnect'

import { coinbaseWallet, hooks as coinbaseWalletHooks } from '../connectors/coinbaseWallet'
import { hooks as metaMaskHooks, metaMask } from '../connectors/metaMask'
import { hooks as networkHooks, network } from '../connectors/network'
Expand Down
1 change: 1 addition & 0 deletions example/components/connectorCards/CoinbaseWalletCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'

import { coinbaseWallet, hooks } from '../../connectors/coinbaseWallet'
import { Card } from '../Card'

Expand Down
1 change: 1 addition & 0 deletions example/components/connectorCards/GnosisSafeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'

import { gnosisSafe, hooks } from '../../connectors/gnosisSafe'
import { Card } from '../Card'

Expand Down
1 change: 1 addition & 0 deletions example/components/connectorCards/MetaMaskCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'

import { hooks, metaMask } from '../../connectors/metaMask'
import { Card } from '../Card'

Expand Down
1 change: 1 addition & 0 deletions example/components/connectorCards/NetworkCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react'

import { hooks, network } from '../../connectors/network'
import { Card } from '../Card'

Expand Down
1 change: 1 addition & 0 deletions example/components/connectorCards/WalletConnectCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URI_AVAILABLE } from '@web3-react/walletconnect'
import { useEffect, useState } from 'react'

import { hooks, walletConnect } from '../../connectors/walletConnect'
import { Card } from '../Card'

Expand Down
1 change: 1 addition & 0 deletions example/connectors/coinbaseWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CoinbaseWallet } from '@web3-react/coinbase-wallet'
import { initializeConnector } from '@web3-react/core'

import { URLS } from '../chains'

export const [coinbaseWallet, hooks] = initializeConnector<CoinbaseWallet>(
Expand Down
1 change: 1 addition & 0 deletions example/connectors/eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Eip1193Bridge } from '@ethersproject/experimental'
import { JsonRpcProvider } from '@ethersproject/providers'
import { initializeConnector } from '@web3-react/core'
import { EIP1193 } from '@web3-react/eip1193'

import { URLS } from '../chains'

class Eip1193BridgeWithoutAccounts extends Eip1193Bridge {
Expand Down
2 changes: 1 addition & 1 deletion example/connectors/empty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initializeConnector } from '@web3-react/core'
import { Empty, EMPTY } from '@web3-react/empty'
import { EMPTY, Empty } from '@web3-react/empty'

export const [empty, hooks] = initializeConnector<Empty>(() => EMPTY)
1 change: 1 addition & 0 deletions example/connectors/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeConnector } from '@web3-react/core'
import { Network } from '@web3-react/network'

import { URLS } from '../chains'

export const [network, hooks] = initializeConnector<Network>((actions) => new Network({ actions, urlMap: URLS }))
1 change: 1 addition & 0 deletions example/connectors/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeConnector } from '@web3-react/core'
import { Url } from '@web3-react/url'

import { URLS } from '../chains'

export const [url, hooks] = initializeConnector<Url>((actions) => new Url({ actions, url: URLS[1][0] }))
1 change: 1 addition & 0 deletions example/connectors/walletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeConnector } from '@web3-react/core'
import { WalletConnect } from '@web3-react/walletconnect'

import { URLS } from '../chains'

export const [walletConnect, hooks] = initializeConnector<WalletConnect>(
Expand Down
2 changes: 1 addition & 1 deletion example/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CoinbaseWallet } from '@web3-react/coinbase-wallet'
import { GnosisSafe } from '@web3-react/gnosis-safe'
import { MetaMask } from '@web3-react/metamask'
import { Network } from '@web3-react/network'
import { WalletConnect } from '@web3-react/walletconnect'
import type { Connector } from '@web3-react/types'
import { WalletConnect } from '@web3-react/walletconnect'

export function getName(connector: Connector) {
if (connector instanceof MetaMask) return 'MetaMask'
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
"@types/react": "^18.0.5",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
"@uniswap/eslint-config": "^1.1.1",
"@walletconnect/ethereum-provider": "^1.7.8",
"eslint": "^8.13.0",
"eslint-plugin-react-hooks": "^4.4.0",
"eslint-plugin-tsdoc": "^0.2.16",
"eth-provider": "^0.9.4",
"jest": "^27.5.1",
"lerna": "^4.0.0",
"prettier": "2.6.2",
"react": "^18.0.0",
"react-test-renderer": "^18.0.0",
"ts-jest": "^27.1.4",
Expand Down
34 changes: 18 additions & 16 deletions packages/coinbase-wallet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export class CoinbaseWallet extends Connector {

// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = await this.provider.request<string[]>({ method: 'eth_accounts' })
const accounts = await this.provider.request<string[]>({ method: 'eth_accounts' })
if (!accounts.length) throw new Error('No accounts returned')
const chainId = await this.provider.request<string>({ method: 'eth_chainId' })
const chainId = await this.provider.request<string>({ method: 'eth_chainId' })
this.actions.update({ chainId: parseChainId(chainId), accounts })
} catch (error) {
cancelActivation()
Expand All @@ -118,21 +118,23 @@ export class CoinbaseWallet extends Connector {
if (!desiredChainId || desiredChainId === parseChainId(this.provider.chainId)) return

const desiredChainIdHex = `0x${desiredChainId.toString(16)}`
return this.provider.request<void>({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
}).catch(async (error: ProviderRpcError) => {
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
if (!this.provider) throw new Error('No provider')
// if we're here, we can try to add a new network
return this.provider.request<void>({
method: 'wallet_addEthereumChain',
params: [{ ...desiredChainIdOrChainParameters, chainId: desiredChainIdHex }],
})
}
return this.provider
.request<void>({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
})
.catch(async (error: ProviderRpcError) => {
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
if (!this.provider) throw new Error('No provider')
// if we're here, we can try to add a new network
return this.provider.request<void>({
method: 'wallet_addEthereumChain',
params: [{ ...desiredChainIdOrChainParameters, chainId: desiredChainIdHex }],
})
}

throw error
})
throw error
})
}

const cancelActivation = this.actions.startActivation()
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BaseProvider, Web3Provider } from '@ethersproject/providers'
import type { Connector, Web3ReactStore } from '@web3-react/types'
import type { Context, MutableRefObject, ReactNode } from 'react'
import React, { createContext, useContext, useRef } from 'react'

import type { Web3ReactHooks, Web3ReactPriorityHooks } from './hooks'
import { getPriorityConnector } from './hooks'

Expand Down
15 changes: 9 additions & 6 deletions packages/eip1193/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export class EIP1193 extends Connector {
// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = await requestAccounts()
const chainId = await this.provider.request({ method: 'eth_chainId' }) as string
const chainId = (await this.provider.request({ method: 'eth_chainId' })) as string
this.actions.update({ chainId: parseChainId(chainId), accounts })
} catch (error) {
cancelActivation()
throw error
cancelActivation()
throw error
}
}

Expand All @@ -64,8 +64,11 @@ export class EIP1193 extends Connector {

/** {@inheritdoc Connector.activate} */
public async activate(): Promise<void> {
return this.activateAccounts(() => this.provider
.request({ method: 'eth_requestAccounts' })
.catch(() => this.provider.request({ method: 'eth_accounts' })) as Promise<string[]>)
return this.activateAccounts(
() =>
this.provider
.request({ method: 'eth_requestAccounts' })
.catch(() => this.provider.request({ method: 'eth_accounts' })) as Promise<string[]>
)
}
}
3 changes: 2 additions & 1 deletion packages/eip1193/src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ProviderRpcError, RequestArguments } from '@web3-react/types'
import { EventEmitter } from 'node:events'

import type { ProviderRpcError, RequestArguments } from '@web3-react/types'

export class MockEIP1193Provider extends EventEmitter {
public chainId?: string
public accounts?: string[]
Expand Down
30 changes: 16 additions & 14 deletions packages/metamask/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
import { Connector } from '@web3-react/types'

type MetaMaskProvider = Provider & {
isMetaMask?: boolean; isConnected?: () => boolean
isMetaMask?: boolean
isConnected?: () => boolean
providers?: MetaMaskProvider[]
get chainId(): string
get accounts(): string[]
Expand Down Expand Up @@ -104,16 +105,16 @@ export class MetaMask extends Connector {

// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = await this.provider.request({ method: 'eth_accounts' }) as string[]
const accounts = (await this.provider.request({ method: 'eth_accounts' })) as string[]
if (!accounts.length) throw new Error('No accounts returned')
const chainId = await this.provider.request({ method: 'eth_chainId' }) as string
const chainId = (await this.provider.request({ method: 'eth_chainId' })) as string
this.actions.update({ chainId: parseChainId(chainId), accounts })
} catch (error) {
console.debug('Could not connect eagerly', error)
// we should be able to use `cancelActivation` here, but on mobile, metamask emits a 'connect'
// event, meaning that chainId is updated, and cancelActivation doesn't work because an intermediary
// update has occurred, so we reset state instead
this.actions.resetState()
console.debug('Could not connect eagerly', error)
// we should be able to use `cancelActivation` here, but on mobile, metamask emits a 'connect'
// event, meaning that chainId is updated, and cancelActivation doesn't work because an intermediary
// update has occurred, so we reset state instead
this.actions.resetState()
}
}

Expand All @@ -136,8 +137,8 @@ export class MetaMask extends Connector {

// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = await this.provider.request({ method: 'eth_requestAccounts' }) as string[]
const chainId = await this.provider.request({ method: 'eth_chainId' }) as string
const accounts = (await this.provider.request({ method: 'eth_requestAccounts' })) as string[]
const chainId = (await this.provider.request({ method: 'eth_chainId' })) as string
const receivedChainId = parseChainId(chainId)
const desiredChainId =
typeof desiredChainIdOrChainParameters === 'number'
Expand All @@ -151,10 +152,11 @@ export class MetaMask extends Connector {
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`

// if we're here, we can try to switch networks
return this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
})
return this.provider
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
})
.catch((error: ProviderRpcError) => {
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
if (!this.provider) throw new Error('No provider')
Expand Down
1 change: 1 addition & 0 deletions packages/network/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { JsonRpcProvider } from '@ethersproject/providers'
import type { ConnectionInfo } from '@ethersproject/web'
import type { Actions } from '@web3-react/types'
import { Connector } from '@web3-react/types'

import { getBestProvider } from './utils'

type url = string | ConnectionInfo
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EventEmitter } from 'node:events'

import type { StoreApi } from 'zustand'

export interface Web3ReactState {
Expand Down
4 changes: 3 additions & 1 deletion packages/walletconnect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { EventEmitter } from 'node:events'

import type WalletConnectProvider from '@walletconnect/ethereum-provider'
import type { IWCEthRpcConnectionOptions } from '@walletconnect/types'
import type { Actions, ProviderRpcError } from '@web3-react/types'
import { Connector } from '@web3-react/types'
import EventEmitter3 from 'eventemitter3'
import type { EventEmitter } from 'node:events'

import { getBestUrl } from './utils'

export const URI_AVAILABLE = 'URI_AVAILABLE'
Expand Down
Loading

0 comments on commit 8507a82

Please sign in to comment.